[libc-commits] [libc] [libc][math] Improve performance of double precision trig functions. (PR #111793)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Oct 10 08:52:10 PDT 2024


================
@@ -42,22 +39,29 @@ LLVM_LIBC_FUNCTION(double, cos, (double x)) {
 
   DoubleDouble y;
   unsigned k;
-  generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
+  LargeRangeReduction range_reduction_large{};
 
-  // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
+  // |x| < 2^16.
   if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
-    // |x| < 2^-27
-    if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) {
-      // Signed zeros.
-      if (LIBC_UNLIKELY(x == 0.0))
-        return 1.0;
-
-      // For |x| < 2^-27, |cos(x) - 1| < |x|^2/2 < 2^-54 = ulp(1 - 2^-53)/2.
-      return fputil::round_result_slightly_down(1.0);
+    // |x| < 2^-7
+    if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) {
+      // |x| < 2^-27
+      if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) {
+        // Signed zeros.
+        if (LIBC_UNLIKELY(x == 0.0))
+          return 1.0;
----------------
nickdesaulniers wrote:

It's unlikely that `cos` is called with `0.0`? I'd imagine you wouldn't have `cos(0.0)` explicitly in your code, but perhaps due to constant folding, a value of `0.0` for `x` at runtime is perhaps more common?

https://github.com/llvm/llvm-project/pull/111793


More information about the libc-commits mailing list