const_soft_float/soft_f64/helpers/k_sin.rs
1// origin: FreeBSD /usr/src/lib/msun/src/k_sin.c,
2// https://github.com/rust-lang/libm/blob/4c8a973741c014b11ce7f1477693a3e5d4ef9609/src/math/k_sin.rs
3//
4// ====================================================
5// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6//
7// Developed at SunSoft, a Sun Microsystems, Inc. business.
8// Permission to use, copy, modify, and distribute this
9// software is freely granted, provided that this notice
10// is preserved.
11// ====================================================
12
13use crate::soft_f64::SoftF64;
14
15const S1: SoftF64 = SoftF64(-1.66666666666666324348e-01); /* 0xBFC55555, 0x55555549 */
16const S2: SoftF64 = SoftF64(8.33333333332248946124e-03); /* 0x3F811111, 0x1110F8A6 */
17const S3: SoftF64 = SoftF64(-1.98412698298579493134e-04); /* 0xBF2A01A0, 0x19C161D5 */
18const S4: SoftF64 = SoftF64(2.75573137070700676789e-06); /* 0x3EC71DE3, 0x57B1FE7D */
19const S5: SoftF64 = SoftF64(-2.50507602534068634195e-08); /* 0xBE5AE5E6, 0x8A2B9CEB */
20const S6: SoftF64 = SoftF64(1.58969099521155010221e-10); /* 0x3DE5D93A, 0x5ACFD57C */
21
22// kernel sin function on ~[-pi/4, pi/4] (except on -0), pi/4 ~ 0.7854
23// Input x is assumed to be bounded by ~pi/4 in magnitude.
24// Input y is the tail of x.
25// Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
26//
27// Algorithm
28// 1. Since sin(-x) = -sin(x), we need only to consider positive x.
29// 2. Callers must return sin(-0) = -0 without calling here since our
30// odd polynomial is not evaluated in a way that preserves -0.
31// Callers may do the optimization sin(x) ~ x for tiny x.
32// 3. sin(x) is approximated by a polynomial of degree 13 on
33// [0,pi/4]
34// 3 13
35// sin(x) ~ x + S1*x + ... + S6*x
36// where
37//
38// |sin(x) 2 4 6 8 10 12 | -58
39// |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
40// | x |
41//
42// 4. sin(x+y) = sin(x) + sin'(x')*y
43// ~ sin(x) + (1-x*x/2)*y
44// For better accuracy, let
45// 3 2 2 2 2
46// r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
47// then 3 2
48// sin(x) = x + (S1*x + (x *(r-y/2)+y))
49#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
50pub(crate) const fn k_sin(x: SoftF64, y: SoftF64, iy: i32) -> SoftF64 {
51 let z = x.mul(x);
52 let w = z.mul(z);
53 let r = S2
54 .add(z.mul(S3.add(z.mul(S4))))
55 .add(z.mul(w.mul(S5.add(z.mul(S6)))));
56 let v = z.mul(x);
57 if iy == 0 {
58 x.add(v.mul(S1.add(z.mul(r))))
59 } else {
60 x.sub((z.mul(SoftF64(0.5).mul(y).sub(v.mul(r))).sub(y)).sub(v.mul(S1)))
61 }
62}