1//===-- Compute sin + cos for small angles ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINCOS_EVAL_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_SINCOS_EVAL_H
11
12#include "src/__support/FPUtil/double_double.h"
13#include "src/__support/FPUtil/dyadic_float.h"
14#include "src/__support/FPUtil/multiply_add.h"
15#include "src/__support/FPUtil/polyeval.h"
16#include "src/__support/integer_literals.h"
17#include "src/__support/macros/attributes.h"
18#include "src/__support/macros/config.h"
19
20namespace LIBC_NAMESPACE_DECL {
21
22namespace math {
23
24namespace sincos_eval_internal {
25
26using fputil::DoubleDouble;
27using DFloat128 = fputil::DyadicFloat<128>;
28
29LIBC_INLINE double sincos_eval(const DoubleDouble &u, DoubleDouble &sin_u,
30 DoubleDouble &cos_u) {
31 // Evaluate sin(y) = sin(x - k * (pi/128))
32 // We use the degree-7 Taylor approximation:
33 // sin(y) ~ y - y^3/3! + y^5/5! - y^7/7!
34 // Then the error is bounded by:
35 // |sin(y) - (y - y^3/3! + y^5/5! - y^7/7!)| < |y|^9/9! < 2^-54/9! < 2^-72.
36 // For y ~ u_hi + u_lo, fully expanding the polynomial and drop any terms
37 // < ulp(u_hi^3) gives us:
38 // y - y^3/3! + y^5/5! - y^7/7! = ...
39 // ~ u_hi + u_hi^3 * (-1/6 + u_hi^2 * (1/120 - u_hi^2 * 1/5040)) +
40 // + u_lo (1 + u_hi^2 * (-1/2 + u_hi^2 / 24))
41 double u_hi_sq = u.hi * u.hi; // Error < ulp(u_hi^2) < 2^(-6 - 52) = 2^-58.
42 // p1 ~ 1/120 + u_hi^2 / 5040.
43 double p1 = fputil::sub_multiply(x: 0x1.1111111111111p-7, y: u_hi_sq,
44 z: 0x1.a01a01a01a01ap-13);
45 // q1 ~ -1/2 + u_hi^2 / 24.
46 double q1 = fputil::multiply_sub(x: u_hi_sq, y: 0x1.5555555555555p-5, z: 0x1.0p-1);
47 double u_hi_3 = u_hi_sq * u.hi;
48 // p2 ~ -1/6 + u_hi^2 (1/120 - u_hi^2 * 1/5040)
49 double p2 = fputil::multiply_sub(x: u_hi_sq, y: p1, z: 0x1.5555555555555p-3);
50 // q2 ~ 1 + u_hi^2 (-1/2 + u_hi^2 / 24)
51 double q2 = fputil::multiply_add(x: u_hi_sq, y: q1, z: 1.0);
52 double sin_lo = fputil::multiply_add(x: u_hi_3, y: p2, z: u.lo * q2);
53 // Overall, |sin(y) - (u_hi + sin_lo)| < 2*ulp(u_hi^3) < 2^-69.
54
55 // Evaluate cos(y) = cos(x - k * (pi/128))
56 // We use the degree-8 Taylor approximation:
57 // cos(y) ~ 1 - y^2/2 + y^4/4! - y^6/6! + y^8/8!
58 // Then the error is bounded by:
59 // |cos(y) - (...)| < |y|^10/10! < 2^-81
60 // For y ~ u_hi + u_lo, fully expanding the polynomial and drop any terms
61 // < ulp(u_hi^3) gives us:
62 // 1 - y^2/2 + y^4/4! - y^6/6! + y^8/8! = ...
63 // ~ 1 - u_hi^2/2 + u_hi^4(1/24 + u_hi^2 (-1/720 + u_hi^2/40320)) +
64 // + u_hi u_lo (-1 + u_hi^2/6)
65 // We compute 1 - u_hi^2 accurately:
66 // v_hi + v_lo ~ 1 - u_hi^2/2
67 // with error <= 2^-105.
68 double u_hi_neg_half = (-0.5) * u.hi;
69 DoubleDouble v;
70
71#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
72 v.hi = fputil::multiply_add(u.hi, u_hi_neg_half, 1.0);
73 v.lo = 1.0 - v.hi; // Exact
74 v.lo = fputil::multiply_add(u.hi, u_hi_neg_half, v.lo);
75#else
76 DoubleDouble u_hi_sq_neg_half = fputil::exact_mult(a: u.hi, b: u_hi_neg_half);
77 v = fputil::exact_add(a: 1.0, b: u_hi_sq_neg_half.hi);
78 v.lo += u_hi_sq_neg_half.lo;
79#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
80
81 // r1 ~ -1/720 + u_hi^2 / 40320
82 double r1 = fputil::multiply_sub(x: u_hi_sq, y: 0x1.a01a01a01a01ap-16,
83 z: 0x1.6c16c16c16c17p-10);
84 // s1 ~ -1 + u_hi^2 / 6
85 double s1 = fputil::multiply_sub(x: u_hi_sq, y: 0x1.5555555555555p-3, z: 1.0);
86 double u_hi_4 = u_hi_sq * u_hi_sq;
87 double u_hi_u_lo = u.hi * u.lo;
88 // r2 ~ 1/24 + u_hi^2 (-1/720 + u_hi^2 / 40320)
89 double r2 = fputil::multiply_add(x: u_hi_sq, y: r1, z: 0x1.5555555555555p-5);
90 // s2 ~ v_lo + u_hi * u_lo * (-1 + u_hi^2 / 6)
91 double s2 = fputil::multiply_add(x: u_hi_u_lo, y: s1, z: v.lo);
92 double cos_lo = fputil::multiply_add(x: u_hi_4, y: r2, z: s2);
93 // Overall, |cos(y) - (v_hi + cos_lo)| < 2*ulp(u_hi^4) < 2^-75.
94
95 sin_u = fputil::exact_add(a: u.hi, b: sin_lo);
96 cos_u = fputil::exact_add(a: v.hi, b: cos_lo);
97
98 return fputil::multiply_add(x: fputil::FPBits<double>(u_hi_3).abs().get_val(),
99 y: 0x1.0p-51, z: 0x1.0p-105);
100}
101
102// sin(u) ~ x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - x^11/11! + x^13/13!
103LIBC_INLINE_VAR constexpr DFloat128 SIN_COEFFS[7] = {
104 {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1
105 {Sign::NEG, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // -1/3!
106 {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/5!
107 {Sign::NEG, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // -1/7!
108 {Sign::POS, -146, 0xb8ef1d2a'b6399c7d'560e4472'800b8ef2_u128}, // 1/9!
109 {Sign::NEG, -153, 0xd7322b3f'aa271c7f'3a3f25c1'bee38f10_u128}, // -1/11!
110 {Sign::POS, -160, 0xb092309d'43684be5'1c198e91'd7b4269e_u128}, // 1/13!
111};
112
113// cos(u) ~ 1 - x^2/2 + x^4/4! - x^6/6! + x^8/8! - x^10/10! + x^12/12!
114LIBC_INLINE_VAR constexpr DFloat128 COS_COEFFS[7] = {
115 {Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
116 {Sign::NEG, -128, 0x80000000'00000000'00000000'00000000_u128}, // 1/2
117 {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/4!
118 {Sign::NEG, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/6!
119 {Sign::POS, -143, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 1/8!
120 {Sign::NEG, -149, 0x93f27dbb'c4fae397'780b69f5'333c725b_u128}, // 1/10!
121 {Sign::POS, -156, 0x8f76c77f'c6c4bdaa'26d4c3d6'7f425f60_u128}, // 1/12!
122};
123
124LIBC_INLINE void sincos_eval(const DFloat128 &u, DFloat128 &sin_u,
125 DFloat128 &cos_u) {
126 DFloat128 u_sq = fputil::quick_mul(a: u, b: u);
127 sin_u = fputil::quick_mul(a: u, b: fputil::loop_polyeval(x: u_sq, a: SIN_COEFFS));
128 cos_u = fputil::loop_polyeval(x: u_sq, a: COS_COEFFS);
129}
130
131} // namespace sincos_eval_internal
132
133} // namespace math
134
135} // namespace LIBC_NAMESPACE_DECL
136
137#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINCOS_EVAL_H
138