const_soft_float/soft_f64/
sin.rs

1// origin: FreeBSD /usr/src/lib/msun/src/s_sin.c, https://github.com/rust-lang/libm/blob/4c8a973741c014b11ce7f1477693a3e5d4ef9609/src/math/sin.rs */
2//
3// ====================================================
4// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5//
6// Developed at SunPro, a Sun Microsystems, Inc. business.
7// Permission to use, copy, modify, and distribute this
8// software is freely granted, provided that this notice
9// is preserved.
10// ====================================================
11
12use super::{
13    helpers::{k_cos, k_sin, rem_pio2},
14    SoftF64,
15};
16
17// sin(x)
18// Return sine function of x.
19//
20// kernel function:
21//      k_sin            ... sine function on [-pi/4,pi/4]
22//      k_cos            ... cose function on [-pi/4,pi/4]
23//      rem_pio2         ... argument reduction routine
24//
25// Method.rounded
26//      Let S,C and T denote the sin, cos and tan respectively on
27//      [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
28//      in [-pi/4 , +pi/4], and let n = k mod 4.
29//      We have
30//
31//          n        sin(x)      cos(x)        tan(x)
32//     ----------------------------------------------------------
33//          0          S           C             T
34//          1          C          -S            -1/T
35//          2         -S          -C             T
36//          3         -C           S            -1/T
37//     ----------------------------------------------------------
38//
39// Special cases:
40//      Let trig be any of sin, cos, or tan.
41//      trig(+-INF)  is NaN, with signals;
42//      trig(NaN)    is that NaN;
43//
44// Accuracy:
45//      TRIG(x) returns trig(x) nearly rounded
46pub(crate) const fn sin(x: SoftF64) -> SoftF64 {
47    let x1p120 = SoftF64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120
48
49    /* High word of x. */
50    let ix = (SoftF64::to_bits(x) >> 32) as u32 & 0x7fffffff;
51
52    /* |x| ~< pi/4 */
53    if ix <= 0x3fe921fb {
54        if ix < 0x3e500000 {
55            /* |x| < 2**-26 */
56            /* raise inexact if x != 0 and underflow if subnormal*/
57            if ix < 0x00100000 {
58                x.div(x1p120);
59            } else {
60                x.add(x1p120);
61            }
62            return x;
63        }
64        return k_sin(x, SoftF64::ZERO, 0);
65    }
66
67    /* sin(Inf or NaN) is NaN */
68    if ix >= 0x7ff00000 {
69        return x.sub(x);
70    }
71
72    /* argument reduction needed */
73    let (n, y0, y1) = rem_pio2(x);
74    match n & 3 {
75        0 => k_sin(y0, y1, 1),
76        1 => k_cos(y0, y1),
77        2 => k_sin(y0, y1, 1).neg(),
78        _ => k_cos(y0, y1).neg(),
79    }
80}
81
82#[cfg(test)]
83mod test {
84    use crate::soft_f64::SoftF64;
85
86    #[test]
87    fn test_near_pi() {
88        let x = SoftF64::from_bits(0x400921fb000FD5DD); // 3.141592026217707
89        let sx = SoftF64::from_bits(0x3ea50d15ced1a4a2); // 6.273720864039205e-7
90        let result = x.sin().0;
91        assert_eq!(result, sx.0);
92    }
93
94    #[test]
95    fn test_large_neg() {
96        assert_eq!(SoftF64(-1647101.0).sin().to_f64(), (-1647101.0_f64).sin())
97    }
98}