From: Joshua Simmons Date: Fri, 21 Oct 2022 21:05:21 +0000 (+0200) Subject: Be more defensive when doing range reduction X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=0872ec6e4ac9ae4778b9caf5ac875fba727323bf;p=josh%2Fnarcissus Be more defensive when doing range reduction 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. --- diff --git a/narcissus-maths/src/sin_cos_pi.rs b/narcissus-maths/src/sin_cos_pi.rs index 1d213c1..0f1450f 100644 --- a/narcissus-maths/src/sin_cos_pi.rs +++ b/narcissus-maths/src/sin_cos_pi.rs @@ -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::() } as u32; let r = r.mul_add(-0.5, a); let sx = (i >> 1) << 31; diff --git a/narcissus-maths/src/tan_pi.rs b/narcissus-maths/src/tan_pi.rs index 3601d38..16d1a5a 100644 --- a/narcissus-maths/src/tan_pi.rs +++ b/narcissus-maths/src/tan_pi.rs @@ -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::() } as u32; let r = r.mul_add(-0.5, a); let e = if i.wrapping_add(1) & 2 != 0 {