]> git.nega.tv - josh/narcissus/commitdiff
Be more defensive when doing range reduction
authorJoshua Simmons <josh@nega.tv>
Fri, 21 Oct 2022 21:05:21 +0000 (23:05 +0200)
committerJoshua Simmons <josh@nega.tv>
Fri, 21 Oct 2022 21:05:21 +0000 (23:05 +0200)
f32 as u32 saturates in rust, using to_int_unchecked wasn't currently
saturating on x86, but it's conceivable that it might. Go the long way
around by converting first to i32 and then to u32 in order to avoid the
possibility.

narcissus-maths/src/sin_cos_pi.rs
narcissus-maths/src/tan_pi.rs

index 1d213c11e71050009b70072dc1f4c9afc9b35a6a..0f1450f7bc61a95c091db2ce3c5193062e623bac 100644 (file)
@@ -50,7 +50,7 @@ pub fn sin_cos_pi_f32(a: f32) -> (f32, f32) {
 
     // Range reduction.
     let r = (a + a).round();
-    let i: u32 = unsafe { r.to_int_unchecked() };
+    let i = unsafe { r.to_int_unchecked::<i32>() } as u32;
     let r = r.mul_add(-0.5, a);
 
     let sx = (i >> 1) << 31;
index 3601d38a9fd0b0a657f34330525e2b5fb03583aa..16d1a5a0d72f2511126aea8d6cb5b455be404830 100644 (file)
@@ -32,7 +32,7 @@ pub fn tan_pi_f32(a: f32) -> f32 {
 
     // Range reduction.
     let r = (a + a).round();
-    let i: u32 = unsafe { r.to_int_unchecked() };
+    let i = unsafe { r.to_int_unchecked::<i32>() } as u32;
     let r = r.mul_add(-0.5, a);
 
     let e = if i.wrapping_add(1) & 2 != 0 {