summaryrefslogtreecommitdiff
path: root/libc/src/math/generic/sinf.cpp
AgeCommit message (Collapse)Author
2025-10-21[libc][math] Add float-only implementation for sinf / cosf. (#161680)lntue
Based on the double precision's sin/cos fast path algorithm: Step 1: Perform range reduction `y = x mod pi/8` with target errors < 2^-54. This is because the worst case mod pi/8 for single precision is ~2^-31, so to have up to 1 ULP errors from the range reduction, the targeted errors should `be 2^(-31 - 23) = 2^-54`. Step 2: Polynomial approximation We use degree-5 and degree-4 polynomials to approximate sin and cos of the reduced angle respectively. Step 3: Combine the results using trig identities ```math \begin{align*} \sin(x) &= \sin(y) \cdot \cos(k \cdot \frac{\pi}{8}) + \cos(y) \cdot \sin(k \cdot \frac{\pi}{8}) \\ \cos(x) &= \cos(y) \cdot \cos(k \cdot \frac{\pi}{8}) - \sin(y) \cdot \sin(k \cdot \frac{\pi}{8}) \end{align*} ``` Overall errors: <= 3 ULPs for default rounding modes (tested exhaustively). Current limitation: large range reduction requires FMA instructions for binary32. This restriction will be removed in the followup PR. --------- Co-authored-by: Petr Hosek <phosek@google.com>
2025-08-11[libc][math] Refactor cosf implementation to header-only in ↵Muhammad Bassiouni
src/__support/math folder. (#152069) Part of #147386 in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-04-08[libc][math] Fix signaling NaN handling for math functions. (#133347)wldfngrs
Add tests for signaling NaNs, and fix function behavior for handling signaling NaN input. Fixes https://github.com/llvm/llvm-project/issues/124812
2025-03-11[libc][math] Skip checking for exceptional values when ↵lntue
LIBC_MATH_SKIP_ACCURATE_PASS is set. (#130811)
2025-03-11[libc] Provide more fine-grained control of FMA instruction for ARM targets. ↵lntue
(#130700)
2024-10-05[libc] remove errno.h includes (#110934)Job Henandez Lara
2024-07-12[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)Petr Hosek
This is a part of #97655.
2024-07-12Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace ↵Mehdi Amini
declaration" (#98593) Reverts llvm/llvm-project#98075 bots are broken
2024-07-11[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)Petr Hosek
This is a part of #97655.
2024-01-30[libc][NFC] Rename `FPBits` nan functions (#79998)Guillaume Chatelet
- [libc][NFC] Rename FPBits nan functions - rename build_signaling_nan in signaling_nan - rename build_quiet_nan to quiet_nan
2024-01-23[reland][libc] Remove unnecessary `FPBits` functions and properties (#79128)Guillaume Chatelet
- reland #79113 - Fix aarch64 RISC-V build
2024-01-23Revert "[libc] Remove unnecessary `FPBits` functions and properties" (#79118)Guillaume Chatelet
Reverts llvm/llvm-project#79113 It broke aarch64 build bot machines.
2024-01-23[libc] Remove unnecessary `FPBits` functions and properties (#79113)Guillaume Chatelet
This patch reduces the surface of `FPBits`.
2024-01-18[libc][NFC] Introduce a Sign type for FPBits (#78500)Guillaume Chatelet
Another patch is needed to cover `DyadicFloat` and `NormalFloat` constructors.
2023-09-26[libc] Mass replace enclosing namespace (#67032)Guillaume Chatelet
This is step 4 of https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079
2023-06-12[libc] Add platform independent floating point rounding mode checks.Tue Ly
Many math functions need to check for floating point rounding modes to return correct values. Currently most of them use the internal implementation of `fegetround`, which is platform-dependent and blocking math functions to be enabled on platforms with unimplemented `fegetround`. In this change, we add platform independent rounding mode checks and switching math functions to use them instead. https://github.com/llvm/llvm-project/issues/63016 Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D152280
2023-03-23[libc] Fix some math conversion warningsAlex Brachet
Differential Revision: https://reviews.llvm.org/D146738
2023-03-07[libc][math] Switch math functions to use libc_errno and fix some errno and ↵Tue Ly
floating point exceptions. Switch math functions to use libc_errno and fix some errno and floating point exceptions Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D145349
2023-02-24[libc][math] Set floating point exceptions for exp*f, sinhf, and coshf.Tue Ly
Set FE_OVERFLOW and FE_UNDERFLOW for expf, exp2f, exp10f, expm1f, sinhf and coshf. Reviewed By: sivachandra, renyichen Differential Revision: https://reviews.llvm.org/D144340
2023-02-10[libc] Move likely/unlikely to the optimization headerGuillaume Chatelet
2023-02-09[libc][NFC] Move cpu_features.h to properties subfolderGuillaume Chatelet
2023-02-07[libc][NFC] Rename macrosGuillaume Chatelet
2023-02-07[libc][NFC] Move attributes from common to macros folderGuillaume Chatelet
2023-02-07[libc][NFC] Move cpu_features to macros folderGuillaume Chatelet
2023-01-25[libc][NFC] Detect host CPU features using try_compile instead of try_run.Guillaume Chatelet
This implements the same behavior as D141997 but makes sure that the same detection mechanism is used between CMake and source code. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D142108
2023-01-24Revert D142108 "[reland][libc][NFC] Detect host CPU features using ↵Guillaume Chatelet
try_compile instead of try_run." The build bots are failing. This reverts commit c84d74f5bfe810744de1268eb0516a6622e4aa73.
2023-01-24[reland][libc][NFC] Detect host CPU features using try_compile instead of ↵Guillaume Chatelet
try_run. This implements the same behavior as D141997 but makes sure that the same detection mechanism is used between CMake and source code. Differential Revision: https://reviews.llvm.org/D142108
2023-01-23Revert D142108 "[libc][NFC] Detect host CPU features using try_compile ↵Guillaume Chatelet
instead of try_run." Build bots are failing. https://lab.llvm.org/buildbot/#/builders/90/builds/44634 This reverts commit 9acc2f37bdfce08ca0c2faec03392db10d1bb7a9.
2023-01-23[libc][NFC] Detect host CPU features using try_compile instead of try_run.Guillaume Chatelet
This implements the same behavior as D141997 but makes sure that the same detection mechanism is used between CMake and source code. Differential Revision: https://reviews.llvm.org/D142108
2022-08-11[libc] Change sinf/cosf range reduction to mod pi/32 to be shared with tanf.Tue Ly
Change sinf/cosf range reduction to mod pi/32 to be shared with tanf, since polynomial approximations for tanf on subintervals of length pi/16 do not provide enough accuracy. Reviewed By: orex Differential Revision: https://reviews.llvm.org/D131652
2022-08-05[libc] Implement sincosf function correctly rounded to all rounding modes.Tue Ly
Refactor common range reductions and evaluations for sinf, cosf, and sincosf. Added exhaustive tests for sincosf. Performance before the patch: ``` System LIBC reciprocal throughput : 30.205 LIBC reciprocal throughput : 30.533 System LIBC latency : 67.961 LIBC latency : 61.564 ``` Performance after the patch: ``` System LIBC reciprocal throughput : 30.409 LIBC reciprocal throughput : 20.273 System LIBC latency : 67.527 LIBC latency : 61.959 ``` Reviewed By: orex Differential Revision: https://reviews.llvm.org/D130901
2022-07-27[libc] Change sinf range reduction to mod pi/16 to be shared with cosf.Tue Ly
Change `sinf` range reduction to mod pi/16 to be shared with `cosf`. Previously, `sinf` used range reduction `mod pi`, but this cannot be used to implement `cosf` since the minimax algorithm for `cosf` does not converge due to critical points at `pi/2`. In order to be able to share the same range reduction functions for both `sinf` and `cosf`, we change the range reduction to `mod pi/16` for the following reasons: - The table size is sufficiently small: 32 entries for `sin(k * pi/16)` with `k = 0..31`. It could be reduced to 16 entries if we treat the final sign separately, with an extra multiplication at the end. - The polynomials' degrees are reduced to 7/8 from 15, with extra computations to combine `sin` and `cos` with trig sum equality. - The number of exceptional cases reduced to 2 (with FMA) and 3 (without FMA). - The latency is reduced while maintaining similar throughput as before. Reviewed By: zimmermann6 Differential Revision: https://reviews.llvm.org/D130629
2022-07-25[libc] Raise FE_INVALID for sinf with +- inf inputs.Tue Ly
2022-07-22[libc] Implement sinf function that is correctly rounded to all rounding modes.Tue Ly
Implement sinf function that is correctly rounded to all rounding modes. - We use a simple range reduction for `pi/16 < |x|` : Let `k = round(x / pi)` and `y = (x/pi) - k`. So `k` is an integer and `-0.5 <= y <= 0.5`. Then ``` sin(x) = sin(y*pi + k*pi) = (-1)^(k & 1) * sin(y*pi) ~ (-1)^(k & 1) * y * P(y^2) ``` where `y*P(y^2)` is a degree-15 minimax polynomial generated by Sollya with: ``` > P = fpminimax(sin(x*pi)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|D...|], [0, 0.5]); ``` - Performance benchmark using perf tool from CORE-MATH project (https://gitlab.inria.fr/core-math/core-math/-/tree/master) on Ryzen 1700: Before this patch (not correctly rounded): ``` $ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh sinf CORE-MATH reciprocal throughput : 17.892 System LIBC reciprocal throughput : 25.559 LIBC reciprocal throughput : 29.381 ``` After this patch (correctly rounded): ``` $ CORE_MATH_PERF_MODE="rdtsc" ./perf.sh sinf CORE-MATH reciprocal throughput : 17.896 System LIBC reciprocal throughput : 25.740 LIBC reciprocal throughput : 27.872 LIBC reciprocal throughput : 20.012 (with `-msse4.2` flag) LIBC reciprocal throughput : 14.244 (with `-mfma` flag) ``` Reviewed By: zimmermann6 Differential Revision: https://reviews.llvm.org/D123154
2022-01-19[libc] Add a naming rule for global constants.Siva Chandra Reddy
Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D117645
2021-12-07[libc] apply new lint rulesMichael Jones
This patch applies the lint rules described in the previous patch. There was also a significant amount of effort put into manually fixing things, since all of the templated functions, or structs defined in /spec, were not updated and had to be handled manually. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D114302
2021-02-03[libc][NFC] Move generic math implementations to the generic directory.Siva Chandra
This expands the pattern suggest in https://reviews.llvm.org/D95850 to all math functions.