]> git.nega.tv - josh/narcissus/commitdiff
narcissus-maths: Use `f32_to_i32` for `sin_cos_pi` and `tan_pi`
authorJoshua Simmons <josh@nega.tv>
Sun, 5 May 2024 14:14:11 +0000 (16:14 +0200)
committerJoshua Simmons <josh@nega.tv>
Sun, 5 May 2024 14:14:11 +0000 (16:14 +0200)
engine/narcissus-maths/src/sin_cos_pi.rs
engine/narcissus-maths/src/tan_pi.rs

index d46e21d2641a978f5c02ae66f64369c3940e33d1..76c400b741e4350d1faec0affdc199f75612e061 100644 (file)
@@ -2,6 +2,8 @@
 //
 // Sollya code for generating these polynomials is in `doc/sincostan.sollya`
 
+use crate::f32_to_i32;
+
 // constants for sin(pi x), cos(pi x) for x on [-1/4,1/4]
 const F32_SIN_PI_7_K: [f32; 3] = unsafe {
     std::mem::transmute::<[u32; 3], _>([
@@ -51,8 +53,7 @@ pub fn sin_cos_pi_f32(a: f32) -> (f32, f32) {
     // Range reduction.
     let r = (a + a).round_ties_even();
 
-    // SAFETY: The clamp above avoids the possibility of overflow here.
-    let i = unsafe { r.to_int_unchecked::<i32>() } as u32;
+    let i = f32_to_i32(r) as u32;
     let r = r.mul_add(-0.5, a);
 
     let sx = (i >> 1) << 31;
index 7a6f852cd6bb3b43544e322a3eccc0f97250ec6c..2a7393f7645feb0b1348094675939dccd33a7c42 100644 (file)
@@ -4,6 +4,8 @@
 //
 // Sollya code for generating these polynomials is in `doc/sincostan.sollya`
 
+use crate::f32_to_i32;
+
 const F32_TAN_PI_15_K: [f32; 7] = unsafe {
     std::mem::transmute::<[u32; 7], _>([
         0x41255def, // 0x1.4abbdep3
@@ -34,8 +36,7 @@ pub fn tan_pi_f32(a: f32) -> f32 {
     // Range reduction.
     let r = (a + a).round_ties_even();
 
-    // SAFETY: The clamp above avoids the possibility of overflow here.
-    let i = unsafe { r.to_int_unchecked::<i32>() } as u32;
+    let i = f32_to_i32(r) as u32;
     let r = r.mul_add(-0.5, a);
 
     let e = if i.wrapping_add(1) & 2 != 0 {