• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  * Optimized by Bruce D. Evans.
5  */
6 /*
7  * ====================================================
8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9  *
10  * Developed at SunPro, a Sun Microsystems, Inc. business.
11  * Permission to use, copy, modify, and distribute this
12  * software is freely granted, provided that this notice
13  * is preserved.
14  * ====================================================
15  */
16 
17 use super::{k_cosf, k_sinf, rem_pio2f};
18 
19 /* Small multiples of pi/2 rounded to double precision. */
20 const PI_2: f32 = 0.5 * 3.1415926535897931160E+00;
21 const S1PIO2: f32 = 1.0 * PI_2; /* 0x3FF921FB, 0x54442D18 */
22 const S2PIO2: f32 = 2.0 * PI_2; /* 0x400921FB, 0x54442D18 */
23 const S3PIO2: f32 = 3.0 * PI_2; /* 0x4012D97C, 0x7F3321D2 */
24 const S4PIO2: f32 = 4.0 * PI_2; /* 0x401921FB, 0x54442D18 */
25 
sincosf(x: f32) -> (f32, f32)26 pub fn sincosf(x: f32) -> (f32, f32) {
27     let s: f32;
28     let c: f32;
29     let mut ix: u32;
30     let sign: bool;
31 
32     ix = x.to_bits();
33     sign = (ix >> 31) != 0;
34     ix &= 0x7fffffff;
35 
36     /* |x| ~<= pi/4 */
37     if ix <= 0x3f490fda {
38         /* |x| < 2**-12 */
39         if ix < 0x39800000 {
40             /* raise inexact if x!=0 and underflow if subnormal */
41 
42             let x1p120 = f32::from_bits(0x7b800000); // 0x1p120 == 2^120
43             if ix < 0x00100000 {
44                 force_eval!(x / x1p120);
45             } else {
46                 force_eval!(x + x1p120);
47             }
48             return (x, 1.0);
49         }
50         return (k_sinf(x as f64), k_cosf(x as f64));
51     }
52 
53     /* |x| ~<= 5*pi/4 */
54     if ix <= 0x407b53d1 {
55         if ix <= 0x4016cbe3 {
56             /* |x| ~<= 3pi/4 */
57             if sign {
58                 s = -k_cosf((x + S1PIO2) as f64);
59                 c = k_sinf((x + S1PIO2) as f64);
60             } else {
61                 s = k_cosf((S1PIO2 - x) as f64);
62                 c = k_sinf((S1PIO2 - x) as f64);
63             }
64         }
65         /* -sin(x+c) is not correct if x+c could be 0: -0 vs +0 */
66         else {
67             if sign {
68                 s = -k_sinf((x + S2PIO2) as f64);
69                 c = -k_cosf((x + S2PIO2) as f64);
70             } else {
71                 s = -k_sinf((x - S2PIO2) as f64);
72                 c = -k_cosf((x - S2PIO2) as f64);
73             }
74         }
75 
76         return (s, c);
77     }
78 
79     /* |x| ~<= 9*pi/4 */
80     if ix <= 0x40e231d5 {
81         if ix <= 0x40afeddf {
82             /* |x| ~<= 7*pi/4 */
83             if sign {
84                 s = k_cosf((x + S3PIO2) as f64);
85                 c = -k_sinf((x + S3PIO2) as f64);
86             } else {
87                 s = -k_cosf((x - S3PIO2) as f64);
88                 c = k_sinf((x - S3PIO2) as f64);
89             }
90         } else {
91             if sign {
92                 s = k_sinf((x + S4PIO2) as f64);
93                 c = k_cosf((x + S4PIO2) as f64);
94             } else {
95                 s = k_sinf((x - S4PIO2) as f64);
96                 c = k_cosf((x - S4PIO2) as f64);
97             }
98         }
99 
100         return (s, c);
101     }
102 
103     /* sin(Inf or NaN) is NaN */
104     if ix >= 0x7f800000 {
105         let rv = x - x;
106         return (rv, rv);
107     }
108 
109     /* general argument reduction needed */
110     let (n, y) = rem_pio2f(x);
111     s = k_sinf(y);
112     c = k_cosf(y);
113     match n & 3 {
114         0 => (s, c),
115         1 => (c, -s),
116         2 => (-s, -c),
117         3 => (-c, s),
118         #[cfg(debug_assertions)]
119         _ => unreachable!(),
120         #[cfg(not(debug_assertions))]
121         _ => (0.0, 1.0),
122     }
123 }
124 
125 #[cfg(test)]
126 mod tests {
127     use super::sincosf;
128     use crate::_eqf;
129 
130     #[test]
with_pi()131     fn with_pi() {
132         let (s, c) = sincosf(core::f32::consts::PI);
133         _eqf(s.abs(), 0.0).unwrap();
134         _eqf(c, -1.0).unwrap();
135     }
136 
137     #[test]
rotational_symmetry()138     fn rotational_symmetry() {
139         use core::f32::consts::PI;
140         const N: usize = 24;
141         for n in 0..N {
142             let theta = 2. * PI * (n as f32) / (N as f32);
143             let (s, c) = sincosf(theta);
144             let (s_plus, c_plus) = sincosf(theta + 2. * PI);
145             let (s_minus, c_minus) = sincosf(theta - 2. * PI);
146 
147             const TOLERANCE: f32 = 1e-6;
148             assert!((s - s_plus).abs() < TOLERANCE);
149             assert!((s - s_minus).abs() < TOLERANCE);
150             assert!((c - c_plus).abs() < TOLERANCE);
151             assert!((c - c_minus).abs() < TOLERANCE);
152         }
153     }
154 }
155