|
| 1 | +//===-- double-precision sinpi function ----------------------------------===// |
| 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 | +#include "src/math/sinpi.h" |
| 10 | +#include "sincos_eval.h" |
| 11 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 12 | +#include "src/__support/FPUtil/FPBits.h" |
| 13 | +#include "src/__support/FPUtil/PolyEval.h" |
| 14 | +#include "src/__support/FPUtil/multiply_add.h" |
| 15 | +#include "src/__support/common.h" |
| 16 | +#include "src/__support/macros/config.h" |
| 17 | +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 18 | + |
| 19 | +namespace LIBC_NAMESPACE_DECL { |
| 20 | + |
| 21 | +LLVM_LIBC_FUNCTION(double, sinpi, (double x)) { |
| 22 | + using FPBits = typename fputil::FPBits<double>; |
| 23 | + FPBits xbits(x); |
| 24 | + |
| 25 | + uint64_t x_u = xbits.uintval(); |
| 26 | + uint64_t x_abs = x_u & 0x7fff'ffffU; |
| 27 | + long double xd = static_cast<long double>(x); |
| 28 | + |
| 29 | + if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { |
| 30 | + if (x_abs == 0x7c00) { |
| 31 | + fputil::set_errno_if_required(EDOM); |
| 32 | + fputil::raise_except_if_required(FE_INVALID); |
| 33 | + } |
| 34 | + return x + FPBits::quiet_nan().get_val(); |
| 35 | + } else { |
| 36 | + return FPBits::zero(xbits.sign()).get_val(); |
| 37 | + } |
| 38 | + |
| 39 | + if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) { |
| 40 | + if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) { |
| 41 | + if (LIBC_UNLIKELY(x_abs == 0U)) { |
| 42 | + return x; |
| 43 | + } |
| 44 | + long double xdpi = xd * 0x1.921fb5444d18p1; |
| 45 | + return static_cast<double>(xdpi); |
| 46 | + } |
| 47 | + long double xsq = xd * xd; |
| 48 | + |
| 49 | + long double result = fputil::polyeval(xsq,0x1.921fb54442d183f07b2385653d8p1, -0x1.4abbce625bd95cdc955aeed9abcp2, 0x1.466bc6769ddfdb085486c0ff3ep1, -0x1.32d2c4a48bfd71fa9cdf60a0e4p-1, 0x1.502cbd2c72e3168ff209bc7656cp-4); |
| 50 | + |
| 51 | + return static_cast<double>(xd * result); |
| 52 | + } |
| 53 | + |
| 54 | + long double sin_k, cos_k, sin_y, cosm1_y; |
| 55 | + sincospi_eval(xd, sin_k, cos_k, sin_y, cosm1_y); |
| 56 | + |
| 57 | + if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) |
| 58 | + return FPBits::zero(xbits.sign()).get_val(); |
| 59 | + |
| 60 | + return static_cast<double>(fputil::multiply_add( |
| 61 | + sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); |
| 62 | +} |
| 63 | +} |
0 commit comments