Shaders: sine and cosine in turns, exact zeros at half and quarter turns
v_sin and v_cos (f32 and f16) take an angle in turns. The whole turns now come off first (x - round_even(x), exact), so a large angle keeps its fraction of a turn instead of losing it when scaled to radians. The sine of a half turn and the cosine of a quarter turn are exactly +0, as the hardware gives; the host's functions of the rounded angle give about 1e-7. A zero operand passes unreduced, so sin(-0) stays -0. float_semantics_are_the_consoles asserts the exact zeros (it failed before this change). The compiler the tests use takes whole turns off itself, so the large-angle path cannot be reached from HLSL and rests on the measurements cited in issue #2. Left from the issue: denormal operands of the legacy multiply, which needs the float mode register as state first. Refs #2 Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
36eb0b4ea7
commit
0b499d7afd
2 files changed
+53
-23
No files matched your search
@@ -978,8 +978,12 @@ fn translated_mesh_shaders_draw_as_pc_ones() {
|
||||
/// Where PC code and the console differ by design, the translation must
|
||||
/// give the console's results: float-to-int conversions saturate and give
|
||||
/// 0 for NaN (`v_cvt_i32/u32_f32`), the legacy multiply the compiler
|
||||
/// uses for `*` gives 0 for 0 × NaN, and `frac` never reaches 1.0 (it gives
|
||||
/// the largest value below; some hosts' own `frac` gives 1.0).
|
||||
/// uses for `*` gives 0 for 0 × NaN, `frac` never reaches 1.0 (it gives
|
||||
/// the largest value below; some hosts' own `frac` gives 1.0), and sine
|
||||
/// and cosine take turns: the sine of a half turn and the cosine of a
|
||||
/// quarter turn are exactly +0 (the host's functions of the rounded angle
|
||||
/// give about 1e-7). The compiler takes whole turns off itself, so large
|
||||
/// angles cannot be tried from here.
|
||||
#[test]
|
||||
fn float_semantics_are_the_consoles() {
|
||||
use super::host::{Dispatch, View};
|
||||
@@ -1001,6 +1005,9 @@ fn float_semantics_are_the_consoles() {
|
||||
outb.Store(32 + 4 * i, asuint(x * k.w));
|
||||
float y = i == 0 ? k.w - 1e-10 : i == 1 ? k.z : i == 2 ? k.w - 1e-8 : k.z + 0.25;
|
||||
outb.Store(48 + 4 * i, asuint(frac(y)));
|
||||
float r = i == 0 ? k.w + 3.14159274 : i == 1 ? k.w + 1.0
|
||||
: i == 2 ? k.w - 3.14159274 : k.w + 1.57079637;
|
||||
outb.Store(64 + 4 * i, asuint(i == 3 ? cos(r) : sin(r)));
|
||||
}"#;
|
||||
let cs = translate(&compile(&console, &dir, src, "cs", "cs_6_5")).unwrap();
|
||||
let (root, pso) = host.compute_pipeline(&cs).unwrap();
|
||||
@@ -1012,7 +1019,7 @@ fn float_semantics_are_the_consoles() {
|
||||
View::Texture(1, 1, &[[0.0; 4]]),
|
||||
View::Texture(1, 1, &[[0.0; 4]]),
|
||||
View::Texture(1, 1, &[[0.0; 4]]),
|
||||
View::RawUav(16),
|
||||
View::RawUav(20),
|
||||
View::TextureUav(1, 1),
|
||||
];
|
||||
let out = host
|
||||
@@ -1029,7 +1036,7 @@ fn float_semantics_are_the_consoles() {
|
||||
.unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
assert_eq!(
|
||||
out[0],
|
||||
out[0][..16],
|
||||
[
|
||||
0x7FFF_FFFF,
|
||||
0x8000_0000,
|
||||
@@ -1049,6 +1056,12 @@ fn float_semantics_are_the_consoles() {
|
||||
0.75f32.to_bits(), // frac
|
||||
]
|
||||
);
|
||||
// sin(pi), sin(-pi) and cos(pi/2): the compiler turns radians into
|
||||
// exactly 0.5, -0.5 and 0.25 turns.
|
||||
assert_eq!([out[0][16], out[0][18], out[0][19]], [0, 0, 0]);
|
||||
// An ordinary angle is untouched: sin(1 rad).
|
||||
let one = f32::from_bits(out[0][17]);
|
||||
assert!((one - 0.841_470_96).abs() < 1e-4, "{one}");
|
||||
}
|
||||
|
||||
/// Equal bits, both zero (the console's legacy multiply gives +0 where PC
|
||||
|
||||
@@ -1418,6 +1418,32 @@ impl<'a> Lower<'a> {
|
||||
self.s.m.select(c, over, k, r)
|
||||
}
|
||||
|
||||
/// Sine, or cosine, of an angle in turns, as the hardware takes it. The
|
||||
/// whole turns are taken off first (`x - round_even(x)`, exact, within
|
||||
/// half a turn), so a large angle keeps its fraction; scaling to radians
|
||||
/// first rounds it away. A zero passes unreduced (the sine of -0 is -0).
|
||||
/// The sine of a half turn and the cosine of a quarter turn are exactly
|
||||
/// +0 on the hardware, where the host's function of the rounded angle
|
||||
/// gives about 1e-7. Infinities and NaN give NaN.
|
||||
/// shortcut: below 2^-12 turns the hardware scales by a slightly
|
||||
/// different constant than 2π (a last-place difference).
|
||||
fn trig_turns(&mut self, x: V, cosine: bool) -> V {
|
||||
let c = self.cur;
|
||||
let zero = self.s.m.f32(0);
|
||||
let whole = self.unary(26, x);
|
||||
let part = self.s.m.bin(c, Bin::Sub, x, whole);
|
||||
let is_zero = self.s.m.cmp(c, pred::OEQ, x, zero);
|
||||
let part = self.s.m.select(c, is_zero, x, part);
|
||||
let k = self.s.m.f32(TWO_PI.to_bits());
|
||||
let angle = self.s.m.bin(c, Bin::Mul, part, k);
|
||||
let r = self.unary(if cosine { 12 } else { 13 }, angle);
|
||||
let at = if cosine { 0.25f32 } else { 0.5 };
|
||||
let at = self.s.m.f32(at.to_bits());
|
||||
let size = self.unary(6, part);
|
||||
let exact = self.s.m.cmp(c, pred::OEQ, size, at);
|
||||
self.s.m.select(c, exact, zero, r)
|
||||
}
|
||||
|
||||
/// The high dword of a 32 × 32-bit product.
|
||||
fn mul_hi(&mut self, a: V, b: V, signed: bool) -> V {
|
||||
let c = self.cur;
|
||||
@@ -3669,7 +3695,7 @@ impl<'a> Lower<'a> {
|
||||
"v_rsq_f32" | "v_sqrt_f32" | "v_log_f32" | "v_exp_f32" | "v_fract_f32"
|
||||
| "v_trunc_f32" | "v_floor_f32" | "v_ceil_f32" | "v_rndne_f32" | "v_sin_f32"
|
||||
| "v_cos_f32" => {
|
||||
let mut a = f(self, 0)?;
|
||||
let a = f(self, 0)?;
|
||||
let op = match name {
|
||||
"v_rsq_f32" => 25,
|
||||
"v_sqrt_f32" => 24,
|
||||
@@ -3683,15 +3709,10 @@ impl<'a> Lower<'a> {
|
||||
"v_sin_f32" => 13,
|
||||
_ => 12,
|
||||
};
|
||||
if op == 13 || op == 12 {
|
||||
// The hardware takes revolutions.
|
||||
let k = self.s.m.f32(TWO_PI.to_bits());
|
||||
a = self.s.m.bin(c, Bin::Mul, a, k);
|
||||
}
|
||||
let r = if op == 22 {
|
||||
self.fract(a, f32::from_bits(0x3F7F_FFFF))
|
||||
} else {
|
||||
self.unary(op, a)
|
||||
let r = match op {
|
||||
12 | 13 => self.trig_turns(a, op == 12),
|
||||
22 => self.fract(a, f32::from_bits(0x3F7F_FFFF)),
|
||||
_ => self.unary(op, a),
|
||||
};
|
||||
float(self, r)
|
||||
}
|
||||
@@ -4275,7 +4296,7 @@ impl<'a> Lower<'a> {
|
||||
self.s.m.bin(c, Bin::SDiv, one, a)
|
||||
}
|
||||
_ => {
|
||||
let mut a = self.half_fsrc(inst, 0)?;
|
||||
let a = self.half_fsrc(inst, 0)?;
|
||||
let op = match name {
|
||||
"v_sqrt_f16" => 24,
|
||||
"v_rsq_f16" => 25,
|
||||
@@ -4289,15 +4310,11 @@ impl<'a> Lower<'a> {
|
||||
"v_sin_f16" => 13,
|
||||
_ => 12,
|
||||
};
|
||||
if op == 13 || op == 12 {
|
||||
let k = self.s.m.f32(TWO_PI.to_bits());
|
||||
a = self.s.m.bin(c, Bin::Mul, a, k);
|
||||
}
|
||||
if op == 22 {
|
||||
match op {
|
||||
12 | 13 => self.trig_turns(a, op == 12),
|
||||
// 0x3bff as a half: 1 - 2^-11.
|
||||
self.fract(a, 1.0 - 1.0 / 2048.0)
|
||||
} else {
|
||||
self.unary(op, a)
|
||||
22 => self.fract(a, 1.0 - 1.0 / 2048.0),
|
||||
_ => self.unary(op, a),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user