const_soft_float/soft_f32/
sin.rs

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
17use core::f64::consts::FRAC_PI_2;
18
19use crate::soft_f64::SoftF64;
20
21use super::{
22    helpers::{k_cosf, k_sinf, rem_pio2f},
23    SoftF32,
24};
25
26/* Small multiples of pi/2 rounded to double precision. */
27const S1_PIO2: SoftF64 = SoftF64(1.).mul(SoftF64(FRAC_PI_2)); /* 0x3FF921FB, 0x54442D18 */
28const S2_PIO2: SoftF64 = SoftF64(2.).mul(SoftF64(FRAC_PI_2)); /* 0x400921FB, 0x54442D18 */
29const S3_PIO2: SoftF64 = SoftF64(3.).mul(SoftF64(FRAC_PI_2)); /* 0x4012D97C, 0x7F3321D2 */
30const S4_PIO2: SoftF64 = SoftF64(4.).mul(SoftF64(FRAC_PI_2)); /* 0x401921FB, 0x54442D18 */
31
32pub const fn sinf(x: SoftF32) -> SoftF32 {
33    let x64 = SoftF64(x.0 as f64);
34
35    let x1p120 = SoftF32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120
36
37    let mut ix = x.to_bits();
38    let sign = (ix >> 31) != 0;
39    ix &= 0x7fffffff;
40
41    if ix <= 0x3f490fda {
42        /* |x| ~<= pi/4 */
43        if ix < 0x39800000 {
44            /* |x| < 2**-12 */
45            /* raise inexact if x!=0 and underflow if subnormal */
46            if ix < 0x00800000 {
47                let _ = x.div(x1p120);
48            } else {
49                let _ = x.add(x1p120);
50            };
51            return x;
52        }
53        return k_sinf(x64);
54    }
55    if ix <= 0x407b53d1 {
56        /* |x| ~<= 5*pi/4 */
57        if ix <= 0x4016cbe3 {
58            /* |x| ~<= 3pi/4 */
59            if sign {
60                return k_cosf(x64.add(S1_PIO2)).neg();
61            } else {
62                return k_cosf(x64.sub(S1_PIO2));
63            }
64        }
65        return k_sinf(if sign {
66            x64.add(S2_PIO2).neg()
67        } else {
68            x64.sub(S2_PIO2).neg()
69        });
70    }
71    if ix <= 0x40e231d5 {
72        /* |x| ~<= 9*pi/4 */
73        if ix <= 0x40afeddf {
74            /* |x| ~<= 7*pi/4 */
75            if sign {
76                return k_cosf(x64.add(S3_PIO2));
77            } else {
78                return k_cosf(x64.sub(S3_PIO2)).neg();
79            }
80        }
81        return k_sinf(if sign {
82            x64.add(S4_PIO2)
83        } else {
84            x64.sub(S4_PIO2)
85        });
86    }
87
88    /* sin(Inf or NaN) is NaN */
89    if ix >= 0x7f800000 {
90        return x.sub(x);
91    }
92
93    /* general argument reduction needed */
94    let (n, y) = rem_pio2f(x);
95    match n & 3 {
96        0 => k_sinf(y),
97        1 => k_cosf(y),
98        2 => k_sinf(y.neg()),
99        _ => k_cosf(y).neg(),
100    }
101}
102
103#[cfg(test)]
104mod test {
105    use core::f32::consts::{FRAC_2_PI, FRAC_PI_2, FRAC_PI_3, PI};
106
107    use super::*;
108
109    #[test]
110    fn test_basic() {
111        for val in [0.0, FRAC_PI_3, FRAC_PI_2, PI, FRAC_2_PI] {
112            assert_eq!(SoftF32(val).sin().to_f32(), val.sin())
113        }
114    }
115}