76 changes: 30 additions & 46 deletions libc/src/math/generic/sincosf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,35 @@
namespace __llvm_libc {

// Exceptional values
static constexpr int N_EXCEPTS = 10;
static constexpr int N_EXCEPTS = 6;

static constexpr uint32_t EXCEPT_INPUTS[N_EXCEPTS] = {
0x3b5637f5, // x = 0x1.ac6feap-9
0x3fa7832a, // x = 0x1.4f0654p0
0x46199998, // x = 0x1.33333p13
0x55325019, // x = 0x1.64a032p43
0x55cafb2a, // x = 0x1.95f654p44
0x5922aa80, // x = 0x1.4555p51
0x5aa4542c, // x = 0x1.48a858p54
0x5f18b878, // x = 0x1.3170fp63
0x6115cb11, // x = 0x1.2b9622p67
0x7beef5ef, // x = 0x1.ddebdep120
0x46199998, // x = 0x1.33333p13 x
0x55325019, // x = 0x1.64a032p43 x
0x5922aa80, // x = 0x1.4555p51 x
0x5f18b878, // x = 0x1.3170fp63 x
0x6115cb11, // x = 0x1.2b9622p67 x
0x7beef5ef, // x = 0x1.ddebdep120 x
};

static constexpr uint32_t EXCEPT_OUTPUTS_SIN[N_EXCEPTS][4] = {
{0x3b5637dc, 1, 0, 0}, // x = 0x1.ac6feap-9, sin(x) = 0x1.ac6fb8p-9 (RZ)
{0x3f7741b5, 1, 0, 1}, // x = 0x1.4f0654p0, sin(x) = 0x1.ee836ap-1 (RZ)
{0xbeb1fa5d, 0, 1, 0}, // x = 0x1.33333p13, sin(x) = -0x1.63f4bap-2 (RZ)
{0xbf171adf, 0, 1, 1}, // x = 0x1.64a032p43, sin(x) = -0x1.2e35bep-1 (RZ)
{0xbf7e7a16, 0, 1, 1}, // x = 0x1.95f654p44, sin(x) = -0x1.fcf42cp-1 (RZ)
{0xbf587521, 0, 1, 1}, // x = 0x1.4555p51, sin(x) = -0x1.b0ea42p-1 (RZ)
{0x3f5f5646, 1, 0, 0}, // x = 0x1.48a858p54, sin(x) = 0x1.beac8cp-1 (RZ)
{0x3dad60f6, 1, 0, 1}, // x = 0x1.3170fp63, sin(x) = 0x1.5ac1ecp-4 (RZ)
{0xbe7cc1e0, 0, 1, 1}, // x = 0x1.2b9622p67, sin(x) = -0x1.f983cp-3 (RZ)
{0xbf587d1b, 0, 1, 1}, // x = 0x1.ddebdep120, sin(x) = -0x1.b0fa36p-1 (RZ)
};

static constexpr uint32_t EXCEPT_OUTPUTS_COS[N_EXCEPTS][4] = {
{0x3f7fffa6, 1, 0, 0}, // x = 0x1.ac6feap-9, cos(x) = 0x1.ffff4cp-1 (RZ)
{0x3e84aabf, 1, 0, 1}, // x = 0x1.4f0654p0, cos(x) = 0x1.09557ep-2 (RZ)
{0xbf70090b, 0, 1, 0}, // x = 0x1.33333p13, cos(x) = -0x1.e01216p-1 (RZ)
{0x3f4ea5d2, 1, 0, 0}, // x = 0x1.64a032p43, cos(x) = 0x1.9d4ba4p-1 (RZ)
{0x3ddf11f3, 1, 0, 1}, // x = 0x1.95f654p44, cos(x) = 0x1.be23e6p-4 (RZ)
{0x3f08aebe, 1, 0, 1}, // x = 0x1.4555p51, cos(x) = 0x1.115d7cp-1 (RZ)
{0x3efa40a4, 1, 0, 0}, // x = 0x1.48a858p54, cos(x) = 0x1.f48148p-2 (RZ)
{0x3f7f14bb, 1, 0, 0}, // x = 0x1.3170fp63, cos(x) = 0x1.fe2976p-1 (RZ)
{0x3f78142e, 1, 0, 1}, // x = 0x1.2b9622p67, cos(x) = 0x1.f0285cp-1 (RZ)
{0x3f08a21c, 1, 0, 0}, // x = 0x1.ddebdep120, cos(x) = 0x1.114438p-1 (RZ)
};

// Fast sincosf implementation. Worst-case ULP is 0.5607, maximum relative
// error is 0.5303 * 2^-23. A single-step range reduction is used for
// small values. Large inputs have their range reduced using fast integer
// arithmetic.
LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
Expand All @@ -71,25 +55,25 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
double xd = static_cast<double>(x);

// Range reduction:
// For |x| > pi/16, we perform range reduction as follows:
// For |x| >= 2^-12, we perform range reduction as follows:
// Find k and y such that:
// x = (k + y) * pi/16
// x = (k + y) * pi/32
// k is an integer
// |y| < 0.5
// For small range (|x| < 2^46 when FMA instructions are available, 2^22
// For small range (|x| < 2^45 when FMA instructions are available, 2^22
// otherwise), this is done by performing:
// k = round(x * 16/pi)
// y = x * 16/pi - k
// For large range, we will omit all the higher parts of 16/pi such that the
// least significant bits of their full products with x are larger than 31,
// k = round(x * 32/pi)
// y = x * 32/pi - k
// For large range, we will omit all the higher parts of 32/pi such that the
// least significant bits of their full products with x are larger than 63,
// since:
// sin((k + y + 32*i) * pi/16) = sin(x + i * 2pi) = sin(x), and
// cos((k + y + 32*i) * pi/16) = cos(x + i * 2pi) = cos(x).
// sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x), and
// cos((k + y + 64*i) * pi/32) = cos(x + i * 2pi) = cos(x).
//
// When FMA instructions are not available, we store the digits of 16/pi in
// When FMA instructions are not available, we store the digits of 32/pi in
// chunks of 28-bit precision. This will make sure that the products:
// x * SIXTEEN_OVER_PI_28[i] are all exact.
// When FMA instructions are available, we simply store the digits of 16/pi in
// x * THIRTYTWO_OVER_PI_28[i] are all exact.
// When FMA instructions are available, we simply store the digits of326/pi in
// chunks of doubles (53-bit of precision).
// So when multiplying by the largest values of single precision, the
// resulting output should be correct up to 2^(-208 + 128) ~ 2^-80. By the
Expand All @@ -102,13 +86,13 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
//
// Once k and y are computed, we then deduce the answer by the sine and cosine
// of sum formulas:
// sin(x) = sin((k + y)*pi/16)
// = sin(y*pi/16) * cos(k*pi/16) + cos(y*pi/16) * sin(k*pi/16)
// cos(x) = cos((k + y)*pi/16)
// = cos(y*pi/16) * cos(k*pi/16) - sin(y*pi/16) * sin(k*pi/16)
// The values of sin(k*pi/16) and cos(k*pi/16) for k = 0..31 are precomputed
// and stored using a vector of 32 doubles. Sin(y*pi/16) and cos(y*pi/16) are
// computed using degree-7 and degree-8 minimax polynomials generated by
// sin(x) = sin((k + y)*pi/32)
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
// cos(x) = cos((k + y)*pi/32)
// = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
// The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed
// and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
// computed using degree-7 and degree-6 minimax polynomials generated by
// Sollya respectively.

// |x| < 0x1.0p-12f
Expand Down Expand Up @@ -195,12 +179,12 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
}

// Combine the results with the sine and cosine of sum formulas:
// sin(x) = sin((k + y)*pi/16)
// = sin(y*pi/16) * cos(k*pi/16) + cos(y*pi/16) * sin(k*pi/16)
// sin(x) = sin((k + y)*pi/32)
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
// = sin_y * cos_k + (1 + cosm1_y) * sin_k
// = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
// cos(x) = cos((k + y)*pi/16)
// = cos(y*pi/16) * cos(k*pi/16) - sin(y*pi/16) * sin(k*pi/16)
// cos(x) = cos((k + y)*pi/32)
// = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
// = cosm1_y * cos_k + sin_y * sin_k
// = (cosm1_y * cos_k + cos_k) + sin_y * sin_k
double sin_k, cos_k, sin_y, cosm1_y;
Expand Down
74 changes: 43 additions & 31 deletions libc/src/math/generic/sincosf_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@ using __llvm_libc::generic::small_range_reduction;

namespace __llvm_libc {

// Lookup table for sin(k * pi / 16) with k = 0, ..., 31.
// Lookup table for sin(k * pi / 32) with k = 0, ..., 63.
// Table is generated with Sollya as follow:
// > display = hexadecimal;
// > for k from 0 to 31 do { D(sin(k * pi/16)); };
const double SIN_K_PI_OVER_16[32] = {
0x0.0000000000000p+0, 0x1.8f8b83c69a60bp-3, 0x1.87de2a6aea963p-2,
0x1.1c73b39ae68c8p-1, 0x1.6a09e667f3bcdp-1, 0x1.a9b66290ea1a3p-1,
0x1.d906bcf328d46p-1, 0x1.f6297cff75cb0p-1, 0x1.0000000000000p+0,
0x1.f6297cff75cb0p-1, 0x1.d906bcf328d46p-1, 0x1.a9b66290ea1a3p-1,
0x1.6a09e667f3bcdp-1, 0x1.1c73b39ae68c8p-1, 0x1.87de2a6aea963p-2,
0x1.8f8b83c69a60bp-3, 0x0.0000000000000p+0, -0x1.8f8b83c69a60bp-3,
-0x1.87de2a6aea963p-2, -0x1.1c73b39ae68c8p-1, -0x1.6a09e667f3bcdp-1,
-0x1.a9b66290ea1a3p-1, -0x1.d906bcf328d46p-1, -0x1.f6297cff75cb0p-1,
-0x1.0000000000000p+0, -0x1.f6297cff75cb0p-1, -0x1.d906bcf328d46p-1,
-0x1.a9b66290ea1a3p-1, -0x1.6a09e667f3bcdp-1, -0x1.1c73b39ae68c8p-1,
-0x1.87de2a6aea963p-2, -0x1.8f8b83c69a60bp-3};
// > for k from 0 to 63 do { D(sin(k * pi/32)); };
const double SIN_K_PI_OVER_32[64] = {
0x0.0000000000000p+0, 0x1.917a6bc29b42cp-4, 0x1.8f8b83c69a60bp-3,
0x1.294062ed59f06p-2, 0x1.87de2a6aea963p-2, 0x1.e2b5d3806f63bp-2,
0x1.1c73b39ae68c8p-1, 0x1.44cf325091dd6p-1, 0x1.6a09e667f3bcdp-1,
0x1.8bc806b151741p-1, 0x1.a9b66290ea1a3p-1, 0x1.c38b2f180bdb1p-1,
0x1.d906bcf328d46p-1, 0x1.e9f4156c62ddap-1, 0x1.f6297cff75cbp-1,
0x1.fd88da3d12526p-1, 0x1.0000000000000p+0, 0x1.fd88da3d12526p-1,
0x1.f6297cff75cbp-1, 0x1.e9f4156c62ddap-1, 0x1.d906bcf328d46p-1,
0x1.c38b2f180bdb1p-1, 0x1.a9b66290ea1a3p-1, 0x1.8bc806b151741p-1,
0x1.6a09e667f3bcdp-1, 0x1.44cf325091dd6p-1, 0x1.1c73b39ae68c8p-1,
0x1.e2b5d3806f63bp-2, 0x1.87de2a6aea963p-2, 0x1.294062ed59f06p-2,
0x1.8f8b83c69a60bp-3, 0x1.917a6bc29b42cp-4, 0x0.0000000000000p+0,
-0x1.917a6bc29b42cp-4, -0x1.8f8b83c69a60bp-3, -0x1.294062ed59f06p-2,
-0x1.87de2a6aea963p-2, -0x1.e2b5d3806f63bp-2, -0x1.1c73b39ae68c8p-1,
-0x1.44cf325091dd6p-1, -0x1.6a09e667f3bcdp-1, -0x1.8bc806b151741p-1,
-0x1.a9b66290ea1a3p-1, -0x1.c38b2f180bdb1p-1, -0x1.d906bcf328d46p-1,
-0x1.e9f4156c62ddap-1, -0x1.f6297cff75cbp-1, -0x1.fd88da3d12526p-1,
-0x1.0000000000000p+0, -0x1.fd88da3d12526p-1, -0x1.f6297cff75cbp-1,
-0x1.e9f4156c62ddap-1, -0x1.d906bcf328d46p-1, -0x1.c38b2f180bdb1p-1,
-0x1.a9b66290ea1a3p-1, -0x1.8bc806b151741p-1, -0x1.6a09e667f3bcdp-1,
-0x1.44cf325091dd6p-1, -0x1.1c73b39ae68c8p-1, -0x1.e2b5d3806f63bp-2,
-0x1.87de2a6aea963p-2, -0x1.294062ed59f06p-2, -0x1.8f8b83c69a60bp-3,
-0x1.917a6bc29b42cp-4,
};

static inline void sincosf_eval(double xd, uint32_t x_abs, double &sin_k,
double &cos_k, double &sin_y, double &cosm1_y) {
Expand All @@ -58,29 +70,29 @@ static inline void sincosf_eval(double xd, uint32_t x_abs, double &sin_k,
k = large_range_reduction(xd, x_bits.get_exponent(), y);
}

// After range reduction, k = round(x * 16 / pi) and y = (x * 16 / pi) - k.
// After range reduction, k = round(x * 32 / pi) and y = (x * 32 / pi) - k.
// So k is an integer and -0.5 <= y <= 0.5.
// Then sin(x) = sin((k + y)*pi/16)
// = sin(y*pi/16) * cos(k*pi/16) + cos(y*pi/16) * sin(k*pi/16)
// Then sin(x) = sin((k + y)*pi/32)
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)

sin_k = SIN_K_PI_OVER_16[k & 31];
// cos(k * pi/16) = sin(k * pi/16 + pi/2) = sin((k + 8) * pi/16).
// cos_k = y * cos(k * pi/16)
cos_k = SIN_K_PI_OVER_16[(k + 8) & 31];
sin_k = SIN_K_PI_OVER_32[k & 63];
// cos(k * pi/32) = sin(k * pi/32 + pi/2) = sin((k + 16) * pi/32).
// cos_k = cos(k * pi/32)
cos_k = SIN_K_PI_OVER_32[(k + 16) & 63];

double ysq = y * y;

// Degree-6 minimax even polynomial for sin(y*pi/16)/y generated by Sollya
// Degree-6 minimax even polynomial for sin(y*pi/32)/y generated by Sollya
// with:
// > Q = fpminimax(sin(y*pi/16)/y, [|0, 2, 4, 6|], [|D...|], [0, 0.5]);
sin_y = y * fputil::polyeval(ysq, 0x1.921fb54442d17p-3, -0x1.4abbce6256adp-10,
0x1.466bc5a5ac6b3p-19, -0x1.32bdcb4207562p-29);
// Degree-8 minimax even polynomial for cos(y*pi/16) generated by Sollya with:
// > P = fpminimax(cos(x*pi/16), [|0, 2, 4, 6, 8|], [|1, D...|], [0, 0.5]);
// Note that cosm1_y = cos(y*pi/16) - 1.
cosm1_y =
ysq * fputil::polyeval(ysq, -0x1.3bd3cc9be45dcp-6, 0x1.03c1f081b08ap-14,
-0x1.55d3c6fb0fb6ep-24, 0x1.e1d3d60f58873p-35);
// > Q = fpminimax(sin(y*pi/32)/y, [|0, 2, 4, 6|], [|D...|], [0, 0.5]);
sin_y =
y * fputil::polyeval(ysq, 0x1.921fb54442d18p-4, -0x1.4abbce625abb1p-13,
0x1.466bc624f2776p-24, -0x1.32c3a619d4a7ep-36);
// Degree-8 minimax even polynomial for cos(y*pi/32) generated by Sollya with:
// > P = fpminimax(cos(x*pi/32), [|0, 2, 4, 6|], [|1, D...|], [0, 0.5]);
// Note that cosm1_y = cos(y*pi/32) - 1.
cosm1_y = ysq * fputil::polyeval(ysq, -0x1.3bd3cc9be430bp-8,
0x1.03c1f070c2e27p-18, -0x1.55cc84bd942p-30);
}

} // namespace __llvm_libc
Expand Down
56 changes: 25 additions & 31 deletions libc/src/math/generic/sinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/PolyEval.h"
#include "src/__support/FPUtil/except_value_utils.h"
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/common.h"

#include <errno.h>

#if defined(LIBC_TARGET_HAS_FMA)
#include "range_reduction_fma.h"
// using namespace __llvm_libc::fma;
using __llvm_libc::fma::N_EXCEPTS;
using __llvm_libc::fma::SinfExcepts;
#else
#include "range_reduction.h"
// using namespace __llvm_libc::generic;
using __llvm_libc::generic::N_EXCEPTS;
using __llvm_libc::generic::SinfExcepts;
#endif

namespace __llvm_libc {
Expand All @@ -41,23 +34,23 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
double xd = static_cast<double>(x);

// Range reduction:
// For |x| > pi/16, we perform range reduction as follows:
// For |x| > pi/32, we perform range reduction as follows:
// Find k and y such that:
// x = (k + y) * pi/16
// x = (k + y) * pi/32
// k is an integer
// |y| < 0.5
// For small range (|x| < 2^46 when FMA instructions are available, 2^22
// For small range (|x| < 2^45 when FMA instructions are available, 2^22
// otherwise), this is done by performing:
// k = round(x * 16/pi)
// y = x * 16/pi - k
// For large range, we will omit all the higher parts of 16/pi such that the
// least significant bits of their full products with x are larger than 31,
// since sin((k + y + 32*i) * pi/16) = sin(x + i * 2pi) = sin(x).
// k = round(x * 32/pi)
// y = x * 32/pi - k
// For large range, we will omit all the higher parts of 32/pi such that the
// least significant bits of their full products with x are larger than 63,
// since sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x).
//
// When FMA instructions are not available, we store the digits of 16/pi in
// When FMA instructions are not available, we store the digits of 32/pi in
// chunks of 28-bit precision. This will make sure that the products:
// x * SIXTEEN_OVER_PI_28[i] are all exact.
// When FMA instructions are available, we simply store the digits of 16/pi in
// x * THIRTYTWO_OVER_PI_28[i] are all exact.
// When FMA instructions are available, we simply store the digits of 32/pi in
// chunks of doubles (53-bit of precision).
// So when multiplying by the largest values of single precision, the
// resulting output should be correct up to 2^(-208 + 128) ~ 2^-80. By the
Expand All @@ -70,11 +63,11 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
//
// Once k and y are computed, we then deduce the answer by the sine of sum
// formula:
// sin(x) = sin((k + y)*pi/16)
// = sin(y*pi/16) * cos(k*pi/16) + cos(y*pi/16) * sin(k*pi/16)
// The values of sin(k*pi/16) and cos(k*pi/16) for k = 0..31 are precomputed
// and stored using a vector of 32 doubles. Sin(y*pi/16) and cos(y*pi/16) are
// computed using degree-7 and degree-8 minimax polynomials generated by
// sin(x) = sin((k + y)*pi/32)
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
// The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
// and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
// computed using degree-7 and degree-6 minimax polynomials generated by
// Sollya respectively.

// |x| <= pi/16
Expand Down Expand Up @@ -129,12 +122,13 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
return xd * result;
}

using ExceptChecker = typename fputil::ExceptionChecker<float, N_EXCEPTS>;
{
float result;
if (ExceptChecker::check_odd_func(SinfExcepts, x_abs, xbits.get_sign(),
result))
return result;
if (unlikely(x_abs == 0x4619'9998U)) { // x = 0x1.33333p13
float r = -0x1.63f4bap-2f;
int rounding = fputil::get_round();
bool sign = xbits.get_sign();
if ((rounding == FE_DOWNWARD && !sign) || (rounding == FE_UPWARD && sign))
r = -0x1.63f4bcp-2f;
return xbits.get_sign() ? -r : r;
}

if (unlikely(x_abs >= 0x7f80'0000U)) {
Expand All @@ -147,8 +141,8 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
}

// Combine the results with the sine of sum formula:
// sin(x) = sin((k + y)*pi/16)
// = sin(y*pi/16) * cos(k*pi/16) + cos(y*pi/16) * sin(k*pi/16)
// sin(x) = sin((k + y)*pi/32)
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
// = sin_y * cos_k + (1 + cosm1_y) * sin_k
// = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
double sin_k, cos_k, sin_y, cosm1_y;
Expand Down