Skip to main content

c/
math_trig.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// Translated from musl: sin, cos, tan, sinf, cosf, tanf,
5// __sin, __cos, __tan, __rem_pio2, __rem_pio2f, __rem_pio2_large,
6// __sindf, __cosdf, __tandf
7
8const INIT_JK: [i32; 4] = [3, 4, 4, 6];
9
10const IPIO2: [i32; 66] = [
11    0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
12    0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A,
13    0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129,
14    0xA73EE8, 0x8235F5, 0x2EBB44, 0x84E99C, 0x7026B4, 0x5F7E41,
15    0x3991D6, 0x398353, 0x39F49C, 0x845F8B, 0xBDF928, 0x3B1FF8,
16    0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, 0x367ECF,
17    0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5,
18    0xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08,
19    0x560330, 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3,
20    0x91615E, 0xE61B08, 0x659985, 0x5F14A0, 0x68408D, 0xFFD880,
21    0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B,
22];
23
24const PIO2: [f64; 8] = [
25    asdouble(0x3FF921FB40000000),
26    asdouble(0x3E74442D00000000),
27    asdouble(0x3CF8469880000000),
28    asdouble(0x3B78CC5160000000),
29    asdouble(0x39F01B8380000000),
30    asdouble(0x387A252040000000),
31    asdouble(0x36E3822280000000),
32    asdouble(0x3569F31D00000000),
33];
34
35const S1PIO2: f64 = asdouble(0x3FF921FB54442D18);
36const S2PIO2: f64 = asdouble(0x400921FB54442D18);
37const S3PIO2: f64 = asdouble(0x4012D97C7F3321D2);
38const S4PIO2: f64 = asdouble(0x401921FB54442D18);
39
40#[allow(non_snake_case)]
41fn __rem_pio2_large(x: &[f64], y: &mut [f64], e0: i32, nx: i32, prec: i32) -> i32 {
42    let mut iq = [0i32; 20];
43    let mut f = [0.0f64; 30];
44    let mut fq = [0.0f64; 20];
45    let mut q = [0.0f64; 20];
46
47    let jk = INIT_JK[prec as usize];
48    let jp = jk;
49    let jx = nx - 1;
50    let mut jv = (e0 - 3) / 24;
51    if jv < 0 { jv = 0; }
52    let mut q0 = e0 - 24 * (jv + 1);
53
54    let mut j = jv - jx;
55    let m = jx + jk;
56    for ii in 0..=m {
57        f[ii as usize] = if j < 0 { 0.0 } else { IPIO2[j as usize] as f64 };
58        j += 1;
59    }
60
61    for ii in 0..=jk {
62        let mut fw = 0.0f64;
63        for jj in 0..=jx {
64            fw += x[jj as usize] * f[(jx + ii - jj) as usize];
65        }
66        q[ii as usize] = fw;
67    }
68
69    let mut jz = jk;
70    let mut need_recompute = true;
71    let mut n_val = 0i32;
72    let mut ih_val = 0i32;
73    let mut z = 0.0f64;
74
75    while need_recompute {
76        need_recompute = false;
77        // distill q[] into iq[]
78        z = q[jz as usize];
79        j = jz;
80        let mut ii = 0i32;
81        while j > 0 {
82            let fw = (z * p2d(-24)) as i32;
83            iq[ii as usize] = (z - fw as f64 * p2d(24)) as i32;
84            z = q[(j - 1) as usize] + fw as f64;
85            ii += 1;
86            j -= 1;
87        }
88
89        z = scalbn(z, q0);
90        z -= 8.0 * floor(z * 0.125);
91        n_val = z as i32;
92        z -= n_val as f64;
93        ih_val = 0;
94        if q0 > 0 {
95            let i_part = iq[(jz - 1) as usize] >> (24 - q0);
96            n_val += i_part;
97            iq[(jz - 1) as usize] -= i_part << (24 - q0);
98            ih_val = iq[(jz - 1) as usize] >> (23 - q0);
99        } else if q0 == 0 {
100            ih_val = iq[(jz - 1) as usize] >> 23;
101        } else if z >= 0.5 {
102            ih_val = 2;
103        }
104
105        if ih_val > 0 {
106            n_val += 1;
107            let mut carry = 0i32;
108            for idx in 0..(jz as usize) {
109                let jv = iq[idx];
110                if carry == 0 {
111                    if jv != 0 {
112                        carry = 1;
113                        iq[idx] = 0x1000000 - jv;
114                    }
115                } else {
116                    iq[idx] = 0xffffff - jv;
117                }
118            }
119            if q0 > 0 {
120                match q0 {
121                    1 => { iq[(jz - 1) as usize] &= 0x7fffff; }
122                    2 => { iq[(jz - 1) as usize] &= 0x3fffff; }
123                    _ => {}
124                }
125            }
126            if ih_val == 2 {
127                z = 1.0 - z;
128                if carry != 0 {
129                    z -= scalbn(1.0, q0);
130                }
131            }
132        }
133
134        // check recomputation
135        if z == 0.0 {
136            j = 0;
137            let mut idx = jz - 1;
138            while idx >= jk {
139                j |= iq[idx as usize];
140                idx -= 1;
141            }
142            if j == 0 {
143                let mut k = 1i32;
144                while iq[(jk - k) as usize] == 0 { k += 1; }
145                for idx2 in (jz + 1)..=(jz + k) {
146                    f[(jx + idx2) as usize] = IPIO2[(jv + idx2) as usize] as f64;
147                    let mut fw = 0.0f64;
148                    for jj in 0..=jx {
149                        fw += x[jj as usize] * f[(jx + idx2 - jj) as usize];
150                    }
151                    q[idx2 as usize] = fw;
152                }
153                jz += k;
154                need_recompute = true;
155            }
156        }
157    }
158
159    // chop off zero terms
160    if z == 0.0 {
161        jz -= 1;
162        q0 -= 24;
163        while iq[jz as usize] == 0 {
164            jz -= 1;
165            q0 -= 24;
166        }
167    } else {
168        z = scalbn(z, -q0);
169        if z >= p2d(24) {
170            let fw = (z * p2d(-24)) as i32;
171            iq[jz as usize] = (z - fw as f64 * p2d(24)) as i32;
172            jz += 1;
173            q0 += 24;
174            iq[jz as usize] = fw;
175        } else {
176            iq[jz as usize] = z as i32;
177        }
178    }
179
180    // convert integer chunks to float
181    let mut fw = scalbn(1.0, q0);
182    for idx in (0..=(jz as usize)).rev() {
183        q[idx] = fw * iq[idx] as f64;
184        fw *= p2d(-24);
185    }
186
187    // compute PIo2 * q
188    for idx in (0..=(jz as usize)).rev() {
189        fw = 0.0;
190        for k in 0..=(jp as usize) {
191            if k <= (jz as usize - idx) {
192                fw += PIO2[k] * q[idx + k];
193            }
194        }
195        fq[(jz as usize) - idx] = fw;
196    }
197
198    // compress fq[] into y[]
199    match prec {
200        0 => {
201            fw = 0.0;
202            for idx in (0..=(jz as usize)).rev() {
203                fw += fq[idx];
204            }
205            y[0] = if ih_val == 0 { fw } else { -fw };
206        }
207        1 | 2 => {
208            fw = 0.0;
209            for idx in (0..=(jz as usize)).rev() {
210                fw += fq[idx];
211            }
212            y[0] = if ih_val == 0 { fw } else { -fw };
213            fw = fq[0] - fw;
214            for idx in 1..=(jz as usize) {
215                fw += fq[idx];
216            }
217            y[1] = if ih_val == 0 { fw } else { -fw };
218        }
219        _ => {
220            for idx in (1..=(jz as usize)).rev() {
221                let tmp = fq[idx - 1] + fq[idx];
222                fq[idx] += fq[idx - 1] - tmp;
223                fq[idx - 1] = tmp;
224            }
225            for idx in (2..=(jz as usize)).rev() {
226                let tmp = fq[idx - 1] + fq[idx];
227                fq[idx] += fq[idx - 1] - tmp;
228                fq[idx - 1] = tmp;
229            }
230            fw = 0.0;
231            for idx in (2..=(jz as usize)).rev() {
232                fw += fq[idx];
233            }
234            if ih_val == 0 {
235                y[0] = fq[0]; y[1] = fq[1]; y[2] = fw;
236            } else {
237                y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
238            }
239        }
240    }
241    n_val & 7
242}
243
244const REM_PIO2_TOINT: f64 = asdouble(0x4338000000000000);
245const REM_PIO2_PIO4: f64 = asdouble(0x3FE921FB54442D18);
246const REM_PIO2_INVPIO2: f64 = asdouble(0x3FE45F306DC9C883);
247const REM_PIO2_PIO2_1: f64 = asdouble(0x3FF921FB54400000);
248const REM_PIO2_PIO2_1T: f64 = asdouble(0x3DD0B4611A626331);
249const REM_PIO2_PIO2_2: f64 = asdouble(0x3DD0B4611A600000);
250const REM_PIO2_PIO2_2T: f64 = asdouble(0x3BA3198A2E037073);
251const REM_PIO2_PIO2_3: f64 = asdouble(0x3BA3198A2E000000);
252const REM_PIO2_PIO2_3T: f64 = asdouble(0x397B839A252049C1);
253
254/// Reduce x by n*pi/2 when |x| is small enough that two-word subtraction is
255/// exact enough.  Keeps the original operation order.
256#[inline]
257fn rem_pio2_small(x: f64, sign: i32, n: f64, y: &mut [f64; 2]) -> i32 {
258    if sign == 0 {
259        let z = x - n * REM_PIO2_PIO2_1;
260        y[0] = z - n * REM_PIO2_PIO2_1T;
261        y[1] = (z - y[0]) - n * REM_PIO2_PIO2_1T;
262        n as i32
263    } else {
264        let z = x + n * REM_PIO2_PIO2_1;
265        y[0] = z + n * REM_PIO2_PIO2_1T;
266        y[1] = (z - y[0]) + n * REM_PIO2_PIO2_1T;
267        -(n as i32)
268    }
269}
270
271fn __rem_pio2(x: f64, y: &mut [f64; 2]) -> i32 {
272    let ix64 = asuint64(x);
273    let sign = (ix64 >> 63) as i32;
274    let ix = ((ix64 >> 32) & 0x7fffffff) as u32;
275
276    if ix <= 0x400f6a7a {
277        if (ix & 0xfffff) == 0x921fb {
278            // cancellation -- use medium case
279            return rem_pio2_medium(x, ix, y);
280        }
281        if ix <= 0x4002d97c {
282            return rem_pio2_small(x, sign, 1.0, y);
283        } else {
284            return rem_pio2_small(x, sign, 2.0, y);
285        }
286    }
287    if ix <= 0x401c463b {
288        if ix <= 0x4015fdbc {
289            if ix == 0x4012d97c {
290                return rem_pio2_medium(x, ix, y);
291            }
292            return rem_pio2_small(x, sign, 3.0, y);
293        } else {
294            if ix == 0x401921fb {
295                return rem_pio2_medium(x, ix, y);
296            }
297            return rem_pio2_small(x, sign, 4.0, y);
298        }
299    }
300    if ix < 0x413921fb {
301        return rem_pio2_medium(x, ix, y);
302    }
303    // all other (large) arguments
304    if ix >= 0x7ff00000 {
305        y[0] = x - x;
306        y[1] = y[0];
307        return 0;
308    }
309    // set z = scalbn(|x|,-ilogb(x)+23)
310    let mut ux = ix64;
311    ux &= (-1i64 as u64) >> 12;
312    ux |= ((0x3ff + 23) as u64) << 52;
313    let mut z = asdouble(ux);
314    let mut tx = [0.0f64; 3];
315    for i in 0..2 {
316        tx[i] = z as i32 as f64;
317        z = (z - tx[i]) * p2d(24);
318    }
319    tx[2] = z;
320    let mut i = 2i32;
321    while tx[i as usize] == 0.0 {
322        i -= 1;
323    }
324    let n = __rem_pio2_large(&tx, y, (ix >> 20) as i32 - (0x3ff + 23), i + 1, 1);
325    if sign != 0 {
326        y[0] = -y[0];
327        y[1] = -y[1];
328        return -(n as i32);
329    }
330    n as i32
331}
332
333fn rem_pio2_medium(x: f64, ix: u32, y: &mut [f64; 2]) -> i32 {
334    let mut fn_val = x as f64 * REM_PIO2_INVPIO2 + REM_PIO2_TOINT - REM_PIO2_TOINT;
335    let mut n = fn_val as i32;
336    let mut r = x - fn_val * REM_PIO2_PIO2_1;
337    let mut w = fn_val * REM_PIO2_PIO2_1T;
338    if predict_false(r - w < -REM_PIO2_PIO4) {
339        n -= 1;
340        fn_val -= 1.0;
341        r = x - fn_val * REM_PIO2_PIO2_1;
342        w = fn_val * REM_PIO2_PIO2_1T;
343    } else if predict_false(r - w > REM_PIO2_PIO4) {
344        n += 1;
345        fn_val += 1.0;
346        r = x - fn_val * REM_PIO2_PIO2_1;
347        w = fn_val * REM_PIO2_PIO2_1T;
348    }
349    y[0] = r - w;
350    let ey = ((asuint64(y[0]) >> 52) & 0x7ff) as i32;
351    let ex = (ix >> 20) as i32;
352    if ex - ey > 16 {
353        let t = r;
354        w = fn_val * REM_PIO2_PIO2_2;
355        r = t - w;
356        w = fn_val * REM_PIO2_PIO2_2T - ((t - r) - w);
357        y[0] = r - w;
358        let ey = ((asuint64(y[0]) >> 52) & 0x7ff) as i32;
359        if ex - ey > 49 {
360            let t = r;
361            w = fn_val * REM_PIO2_PIO2_3;
362            r = t - w;
363            w = fn_val * REM_PIO2_PIO2_3T - ((t - r) - w);
364            y[0] = r - w;
365        }
366    }
367    y[1] = (r - y[0]) - w;
368    n
369}
370
371const REMPIO2F_TOINT: f64 = asdouble(0x4338000000000000);
372const REMPIO2F_PIO4: f64 = asdouble(0x3FE921FB60000000);
373const REMPIO2F_INVPIO2: f64 = asdouble(0x3FE45F306DC9C883);
374const REMPIO2F_PIO2_1: f64 = asdouble(0x3FF921FB50000000);
375const REMPIO2F_PIO2_1T: f64 = asdouble(0x3E5110B4611A6263);
376
377fn __rem_pio2f(x: f32, y: &mut f64) -> i32 {
378    let ix = asuint(x) & 0x7fffffff;
379    if ix < 0x4dc90fdb {
380        let mut fn_val = x as f64 * REMPIO2F_INVPIO2 + REMPIO2F_TOINT - REMPIO2F_TOINT;
381        let mut n = fn_val as i32;
382        *y = x as f64 - fn_val * REMPIO2F_PIO2_1 - fn_val * REMPIO2F_PIO2_1T;
383        if predict_false(*y < -REMPIO2F_PIO4) {
384            n -= 1;
385            fn_val -= 1.0;
386            *y = x as f64 - fn_val * REMPIO2F_PIO2_1 - fn_val * REMPIO2F_PIO2_1T;
387        } else if predict_false(*y > REMPIO2F_PIO4) {
388            n += 1;
389            fn_val += 1.0;
390            *y = x as f64 - fn_val * REMPIO2F_PIO2_1 - fn_val * REMPIO2F_PIO2_1T;
391        }
392        return n;
393    }
394    if ix >= 0x7f800000 {
395        *y = x as f64 - x as f64;
396        return 0;
397    }
398    let sign = (asuint(x) >> 31) as i32;
399    let e0 = ((ix >> 23) as i32) - (0x7f + 23);
400    let ux = ix - ((e0 as u32) << 23);
401    let tx = [asfloat(ux) as f64];
402    let mut ty = [0.0f64];
403    let n = __rem_pio2_large(&tx, &mut ty, e0, 1, 0);
404    if sign != 0 {
405        *y = -ty[0];
406        return -(n as i32);
407    }
408    *y = ty[0];
409    n as i32
410}
411
412// __sin kernel
413const SIN_S1: f64 = asdouble(0xBFC5555555555549);
414const SIN_S2: f64 = asdouble(0x3F8111111110F8A6);
415const SIN_S3: f64 = asdouble(0xBF2A01A019C161D5);
416const SIN_S4: f64 = asdouble(0x3EC71DE357B1FE7D);
417const SIN_S5: f64 = asdouble(0xBE5AE5E68A2B9CEB);
418const SIN_S6: f64 = asdouble(0x3DE5D93A5ACFD57C);
419
420fn __sin(x: f64, y: f64, iy: i32) -> f64 {
421    let z = x * x;
422    let w = z * z;
423    let r = SIN_S2 + z * (SIN_S3 + z * SIN_S4) + z * w * (SIN_S5 + z * SIN_S6);
424    let v = z * x;
425    if iy == 0 {
426        x + v * (SIN_S1 + z * r)
427    } else {
428        x - ((z * (0.5 * y - v * r) - y) - v * SIN_S1)
429    }
430}
431
432// __cos kernel
433const COS_C1: f64 = asdouble(0x3FA555555555554C);
434const COS_C2: f64 = asdouble(0xBF56C16C16C15177);
435const COS_C3: f64 = asdouble(0x3EFA01A019CB1590);
436const COS_C4: f64 = asdouble(0xBE927E4F809C52AD);
437const COS_C5: f64 = asdouble(0x3E21EE9EBDB4B1C4);
438const COS_C6: f64 = asdouble(0xBDA8FAE9BE8838D4);
439
440fn __cos(x: f64, y: f64) -> f64 {
441    let z = x * x;
442    let w = z * z;
443    let r = z * (COS_C1 + z * (COS_C2 + z * COS_C3)) + w * w * (COS_C4 + z * (COS_C5 + z * COS_C6));
444    let hz = 0.5 * z;
445    let w = 1.0 - hz;
446    w + (((1.0 - w) - hz) + (z * r - x * y))
447}
448
449// __tan kernel
450const TAN_T: [f64; 13] = [
451    asdouble(0x3FD5555555555563),
452    asdouble(0x3FC111111110FE7A),
453    asdouble(0x3FABA1BA1BB341FE),
454    asdouble(0x3F9664F48406D637),
455    asdouble(0x3F8226E3E96E8493),
456    asdouble(0x3F6D6D22C9560328),
457    asdouble(0x3F57DBC8FEE08315),
458    asdouble(0x3F4344D8F2F26501),
459    asdouble(0x3F3026F71A8D1068),
460    asdouble(0x3F147E88A03792A6),
461    asdouble(0x3F12B80F32F0A7E9),
462    asdouble(0xBEF375CBDB605373),
463    asdouble(0x3EFB2A7074BF7AD4),
464];
465const TAN_PIO4: f64 = asdouble(0x3FE921FB54442D18);
466const TAN_PIO4LO: f64 = asdouble(0x3C81A62633145C07);
467
468fn __tan(x: f64, y: f64, odd: i32) -> f64 {
469    let hx = (asuint64(x) >> 32) as u32;
470    let big = (hx & 0x7fffffff) >= 0x3FE59428;
471    let (x, y, sign) = if big {
472        let sign = hx >> 31;
473        let (nx, ny) = if sign != 0 { (-x, -y) } else { (x, y) };
474        ((TAN_PIO4 - nx) + (TAN_PIO4LO - ny), 0.0, sign)
475    } else {
476        (x, y, 0u32)
477    };
478    let z = x * x;
479    let w = z * z;
480    let r = TAN_T[1] + w * (TAN_T[3] + w * (TAN_T[5] + w * (TAN_T[7] + w * (TAN_T[9] + w * TAN_T[11]))));
481    let v = z * (TAN_T[2] + w * (TAN_T[4] + w * (TAN_T[6] + w * (TAN_T[8] + w * (TAN_T[10] + w * TAN_T[12])))));
482    let s = z * x;
483    let r = y + z * (s * (r + v) + y) + s * TAN_T[0];
484    let w = x + r;
485    if big {
486        let s_val = 1.0 - 2.0 * odd as f64;
487        let v = s_val - 2.0 * (x + (r - w * w / (w + s_val)));
488        return if sign != 0 { -v } else { v };
489    }
490    if odd == 0 {
491        return w;
492    }
493    // -1.0/(x+r) has up to 2ulp error, so compute it accurately
494    let w0 = {
495        let mut tmp = w;
496        unsafe { set_low_word(&mut tmp, 0); }
497        tmp
498    };
499    let v = r - (w0 - x);
500    let mut a0 = -1.0 / w;
501    let a = a0;
502    unsafe { set_low_word(&mut a0, 0); }
503    a0 + a * (1.0 + a0 * w0 + a0 * v)
504}
505
506// __sindf kernel
507const SINDF_S1: f64 = asdouble(0xBFC5555554CBAC77);
508const SINDF_S2: f64 = asdouble(0x3F811110896EFBB2);
509const SINDF_S3: f64 = asdouble(0xBF2A00F9E2CAE774);
510const SINDF_S4: f64 = asdouble(0x3EC6CD878C3B46A7);
511
512fn __sindf(x: f64) -> f32 {
513    let z = x * x;
514    let w = z * z;
515    let r = SINDF_S3 + z * SINDF_S4;
516    let s = z * x;
517    ((x + s * (SINDF_S1 + z * SINDF_S2)) + s * w * r) as f32
518}
519
520// __cosdf kernel
521const COSDF_C0: f64 = asdouble(0xBFDFFFFFFD0C5E81);
522const COSDF_C1: f64 = asdouble(0x3FA55553E1053A42);
523const COSDF_C2: f64 = asdouble(0xBF56C087E80F1E27);
524const COSDF_C3: f64 = asdouble(0x3EF99342E0EE5069);
525
526fn __cosdf(x: f64) -> f32 {
527    let z = x * x;
528    let w = z * z;
529    let r = COSDF_C2 + z * COSDF_C3;
530    ((1.0 + z * COSDF_C0) + w * COSDF_C1 + (w * z) * r) as f32
531}
532
533// __tandf kernel
534const TANDF_T: [f64; 6] = [
535    asdouble(0x3FD5554D3418C99F),
536    asdouble(0x3FC112FD38999F72),
537    asdouble(0x3FAB54C91D865AFE),
538    asdouble(0x3F991DF3908C33CE),
539    asdouble(0x3F685DADFCECF44E),
540    asdouble(0x3F8362B9BF971BCD),
541];
542
543fn __tandf(x: f64, odd: i32) -> f32 {
544    let z = x * x;
545    let r = TANDF_T[4] + z * TANDF_T[5];
546    let t = TANDF_T[2] + z * TANDF_T[3];
547    let w = z * z;
548    let s = z * x;
549    let u = TANDF_T[0] + z * TANDF_T[1];
550    let r = (x + s * u) + (s * w) * (t + w * r);
551    if odd != 0 { (-1.0 / r) as f32 } else { r as f32 }
552}
553
554// ============================================================
555// sin, cos, tan (double precision)
556// ============================================================
557
558#[no_mangle]
559pub extern "C" fn sin(x: f64) -> f64 {
560    let mut ix = (asuint64(x) >> 32) as u32;
561    ix &= 0x7fffffff;
562
563    if ix <= 0x3fe921fb {
564        if ix < 0x3e500000 {
565            force_eval(if ix < 0x00100000 { x / p2f(120) as f64 } else { x + p2f(120) as f64 });
566            return x;
567        }
568        return __sin(x, 0.0, 0);
569    }
570    if ix >= 0x7ff00000 {
571        return x - x;
572    }
573    let mut y = [0.0f64; 2];
574    let n = __rem_pio2(x, &mut y);
575    match n & 3 {
576        0 =>  __sin(y[0], y[1], 1),
577        1 =>  __cos(y[0], y[1]),
578        2 => -__sin(y[0], y[1], 1),
579        _ => -__cos(y[0], y[1]),
580    }
581}
582
583#[no_mangle]
584pub extern "C" fn cos(x: f64) -> f64 {
585    let mut ix = (asuint64(x) >> 32) as u32;
586    ix &= 0x7fffffff;
587
588    if ix <= 0x3fe921fb {
589        if ix < 0x3e46a09e {
590            force_eval(x + p2f(120) as f64);
591            return 1.0;
592        }
593        return __cos(x, 0.0);
594    }
595    if ix >= 0x7ff00000 {
596        return x - x;
597    }
598    let mut y = [0.0f64; 2];
599    let n = __rem_pio2(x, &mut y);
600    match n & 3 {
601        0 =>  __cos(y[0], y[1]),
602        1 => -__sin(y[0], y[1], 1),
603        2 => -__cos(y[0], y[1]),
604        _ =>  __sin(y[0], y[1], 1),
605    }
606}
607
608#[no_mangle]
609pub extern "C" fn tan(x: f64) -> f64 {
610    let mut ix = (asuint64(x) >> 32) as u32;
611    ix &= 0x7fffffff;
612
613    if ix <= 0x3fe921fb {
614        if ix < 0x3e400000 {
615            force_eval(if ix < 0x00100000 { x / p2f(120) as f64 } else { x + p2f(120) as f64 });
616            return x;
617        }
618        return __tan(x, 0.0, 0);
619    }
620    if ix >= 0x7ff00000 {
621        return x - x;
622    }
623    let mut y = [0.0f64; 2];
624    let n = __rem_pio2(x, &mut y);
625    __tan(y[0], y[1], n & 1)
626}
627
628// ============================================================
629// sinf, cosf, tanf (single precision)
630// ============================================================
631
632#[no_mangle]
633pub extern "C" fn sinf(x: f32) -> f32 {
634    let ix = asuint(x);
635    let sign = ix >> 31;
636    let ix = ix & 0x7fffffff;
637
638    if ix <= 0x3f490fda {
639        if ix < 0x39800000 {
640            force_eval(if ix < 0x00800000 { x / p2f(120) } else { x + p2f(120) });
641            return x;
642        }
643        return __sindf(x as f64);
644    }
645    if ix <= 0x407b53d1 {
646        if ix <= 0x4016cbe3 {
647            return if sign != 0 { -__cosdf(x as f64 + S1PIO2) } else { __cosdf(x as f64 - S1PIO2) };
648        }
649        return __sindf(if sign != 0 { -(x as f64 + S2PIO2) } else { -(x as f64 - S2PIO2) });
650    }
651    if ix <= 0x40e231d5 {
652        if ix <= 0x40afeddf {
653            return if sign != 0 { __cosdf(x as f64 + S3PIO2) } else { -__cosdf(x as f64 - S3PIO2) };
654        }
655        return __sindf(if sign != 0 { x as f64 + S4PIO2 } else { x as f64 - S4PIO2 });
656    }
657    if ix >= 0x7f800000 {
658        return x - x;
659    }
660    let mut y = 0.0f64;
661    let n = __rem_pio2f(x, &mut y);
662    match n & 3 {
663        0 =>  __sindf(y),
664        1 =>  __cosdf(y),
665        2 =>  __sindf(-y),
666        _ => -__cosdf(y),
667    }
668}
669
670#[no_mangle]
671pub extern "C" fn cosf(x: f32) -> f32 {
672    let ix = asuint(x);
673    let sign = ix >> 31;
674    let ix = ix & 0x7fffffff;
675
676    if ix <= 0x3f490fda {
677        if ix < 0x39800000 {
678            force_eval(x + p2f(120));
679            return 1.0f32;
680        }
681        return __cosdf(x as f64);
682    }
683    if ix <= 0x407b53d1 {
684        if ix > 0x4016cbe3 {
685            return -__cosdf(if sign != 0 { x as f64 + S2PIO2 } else { x as f64 - S2PIO2 });
686        } else {
687            return if sign != 0 { __sindf(x as f64 + S1PIO2) } else { __sindf(S1PIO2 - x as f64) };
688        }
689    }
690    if ix <= 0x40e231d5 {
691        if ix > 0x40afeddf {
692            return __cosdf(if sign != 0 { x as f64 + S4PIO2 } else { x as f64 - S4PIO2 });
693        } else {
694            return if sign != 0 { __sindf(-x as f64 - S3PIO2) } else { __sindf(x as f64 - S3PIO2) };
695        }
696    }
697    if ix >= 0x7f800000 {
698        return x - x;
699    }
700    let mut y = 0.0f64;
701    let n = __rem_pio2f(x, &mut y);
702    match n & 3 {
703        0 =>  __cosdf(y),
704        1 =>  __sindf(-y),
705        2 => -__cosdf(y),
706        _ =>  __sindf(y),
707    }
708}
709
710#[no_mangle]
711pub extern "C" fn tanf(x: f32) -> f32 {
712    let ix = asuint(x);
713    let sign = ix >> 31;
714    let ix = ix & 0x7fffffff;
715
716    if ix <= 0x3f490fda {
717        if ix < 0x39800000 {
718            force_eval(if ix < 0x00800000 { x / p2f(120) } else { x + p2f(120) });
719            return x;
720        }
721        return __tandf(x as f64, 0);
722    }
723    if ix <= 0x407b53d1 {
724        if ix <= 0x4016cbe3 {
725            return __tandf(if sign != 0 { x as f64 + S1PIO2 } else { x as f64 - S1PIO2 }, 1);
726        } else {
727            return __tandf(if sign != 0 { x as f64 + S2PIO2 } else { x as f64 - S2PIO2 }, 0);
728        }
729    }
730    if ix <= 0x40e231d5 {
731        if ix <= 0x40afeddf {
732            return __tandf(if sign != 0 { x as f64 + S3PIO2 } else { x as f64 - S3PIO2 }, 1);
733        } else {
734            return __tandf(if sign != 0 { x as f64 + S4PIO2 } else { x as f64 - S4PIO2 }, 0);
735        }
736    }
737    if ix >= 0x7f800000 {
738        return x - x;
739    }
740    let mut y = 0.0f64;
741    let n = __rem_pio2f(x, &mut y);
742    __tandf(y, n & 1)
743}