GPU: a half-float rounding carry raises the exponent, not halves the value
f32_to_half rounds to nearest by adding half an ulp to the mantissa, then or-ed the shifted mantissa into the exponent field. When the rounding carried out of the mantissa, that carry is the exponent's low bit: or-ed into an odd exponent it was lost, so values just below a power of two came out at the power below (2 - 2^-12 gave 1.0, not 2.0). Rectangle lists use it for the fourth corner of half-float elements. The carry is now added into the exponent.
This commit is contained in:
1 parent
35e8407c9c
commit
7129bde6a8
1 file changed
+22
-1
@@ -129,7 +129,10 @@ fn f32_to_half(v: f32) -> u16 {
|
||||
if exp <= 0 {
|
||||
return sign; // too small: zero
|
||||
}
|
||||
sign | (exp as u16) << 10 | ((man + 0x1000) >> 13) as u16
|
||||
// Rounded to nearest: a carry out of the mantissa is added into the
|
||||
// exponent (`2 - 2^-12` is 2.0); at most it reaches infinity, 0x7c00.
|
||||
let half = (exp as u32) << 10;
|
||||
sign | (half + ((man + 0x1000) >> 13)) as u16
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -165,6 +168,24 @@ mod tests {
|
||||
assert_eq!(half_to_f32(0xc000), -2.0);
|
||||
}
|
||||
|
||||
/// A rounding that carries out of the mantissa raises the exponent
|
||||
/// (the carry used to be or-ed into an odd exponent, halving the value).
|
||||
#[test]
|
||||
fn halves_round_up_into_the_next_power_of_two() {
|
||||
assert_eq!(f32_to_half(2.0 - 2f32.powi(-12)), 0x4000);
|
||||
assert_eq!(f32_to_half(0.5 - 2f32.powi(-13)), 0x3800);
|
||||
assert_eq!(f32_to_half(65520.0), 0x7c00, "past the largest half");
|
||||
// An R16_FLOAT element at 1.0, 2^-12 and 1.0: the fourth corner is
|
||||
// 2 - 2^-12, which rounds to 2.0.
|
||||
let src: Vec<u8> = [0x3c00_u16, 0x0c00, 0x3c00]
|
||||
.iter()
|
||||
.flat_map(|h| h.to_le_bytes())
|
||||
.collect();
|
||||
let mut dst = vec![0u8; 6 * 2];
|
||||
expand_rects(&src, &mut dst, 2, &[(0, 54)]);
|
||||
assert_eq!(dst[10..], 0x4000_u16.to_le_bytes());
|
||||
}
|
||||
|
||||
/// An element that reaches past the stride (float4 at 4 in 8-byte
|
||||
/// vertices, RGBA8 at the stride itself): the components inside get
|
||||
/// the fourth corner, the rest keep the first vertex's bytes. The old
|
||||
|
||||
Reference in new issue
Block a user