[libc][math] Fix setting exceptional value for tanf to work with gcc.
authorTue Ly <lntue@google.com>
Wed, 1 Feb 2023 19:25:25 +0000 (14:25 -0500)
committerTue Ly <lntue@google.com>
Wed, 1 Feb 2023 20:53:38 +0000 (15:53 -0500)
See https://github.com/llvm/llvm-project/issues/59866

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143098

libc/src/math/generic/tanf.cpp

index 8b720c48e5dece8690db012ffd58ee4b12f4d68f..0f3961ee5422a62b3b3f1fae496f2d127fa40c32 100644 (file)
@@ -97,7 +97,12 @@ LLVM_LIBC_FUNCTION(float, tanf, (float x)) {
     // |x| = 0x1.143ec4p0
     float sign = x_sign ? -1.0f : 1.0f;
 
-    return fputil::multiply_add(sign, 0x1.ddf9f4p0f, sign * 0x1.1p-24f);
+    // volatile is used to prevent compiler (gcc) from optimizing the
+    // computation, making the results incorrect in different rounding modes.
+    volatile float tmp = 0x1.ddf9f4p0f;
+    tmp = fputil::multiply_add(sign, tmp, sign * 0x1.1p-24f);
+
+    return tmp;
   }
 
   // |x| > 0x1.ada6a8p+27f