[libc-commits] [libc] [llvm] [libc][math][c23] Add {, u}fromfp{, x}{, f, l, f128} functions (PR #86003)

via libc-commits libc-commits at lists.llvm.org
Wed Mar 20 13:52:56 PDT 2024


================
@@ -151,28 +151,30 @@ LIBC_INLINE T round_using_current_rounding_mode(T x) {
 
   bool is_neg = bits.is_neg();
   int exponent = bits.get_exponent();
-  int rounding_mode = quick_get_round();
 
   // If the exponent is greater than the most negative mantissa
   // exponent, then x is already an integer.
   if (exponent >= static_cast<int>(FPBits<T>::FRACTION_LEN))
     return x;
 
   if (exponent <= -1) {
-    switch (rounding_mode) {
-    case FE_DOWNWARD:
+    switch (rnd) {
+    case FP_INT_DOWNWARD:
       return is_neg ? T(-1.0) : T(0.0);
-    case FE_UPWARD:
+    case FP_INT_UPWARD:
       return is_neg ? T(-0.0) : T(1.0);
-    case FE_TOWARDZERO:
+    case FP_INT_TOWARDZERO:
       return is_neg ? T(-0.0) : T(0.0);
-    case FE_TONEAREST:
+    case FP_INT_TONEARESTFROMZERO:
+      if (exponent < -1)
+        return is_neg ? T(-0.0) : T(0.0); // abs(x) < 0.5
+      return is_neg ? T(-1.0) : T(1.0);   // abs(x) >= 0.5
+    case FP_INT_TONEAREST:
+    default:
----------------
overmighty wrote:

Section 7.12.9.10 of https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf says:

> If the value of the `rnd` argument is not equal to the value of a math rounding direction macro (7.12), the direction of rounding is unspecified.

I chose to add a fallback to `FP_INT_TONEAREST`. Should we document this somewhere?

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


More information about the libc-commits mailing list