[libc-commits] [llvm] [libc] [libc][math] Implement nexttoward functions (PR #72763)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Nov 30 08:30:40 PST 2023


================
@@ -169,8 +171,50 @@ LIBC_INLINE T nextafter(T from, T to) {
     int_val = (to_bits.uintval() & sign_mask) + UIntType(1);
   }
 
+  UIntType exponent_bits = int_val & FloatProperties<T>::EXPONENT_MASK;
+  if (exponent_bits == UIntType(0))
+    raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
+  else if (exponent_bits == FloatProperties<T>::EXPONENT_MASK)
+    raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
+
+  return cpp::bit_cast<T>(int_val);
+}
+
+template <typename T>
+LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
+nexttoward(T from, long double to) {
+  FPBits<T> from_bits(from);
+  if (from_bits.is_nan())
+    return from;
+
+  FPBits<long double> to_bits(to);
+  if (to_bits.is_nan())
+    return to;
----------------
nickdesaulniers wrote:

I'm observing the following warnings when building `ninja check-libc` with clang:
```
[286/3491] Building CXX object projects/libc/src/math/gene...es/libc.src.math.generic.nexttowardf.dir/nexttowardf.cpp.o
In file included from /android0/llvm-project/libc/src/math/generic/nexttowardf.cpp:10:
/android0/llvm-project/libc/src/__support/FPUtil/ManipulationFunctions.h:191:12: warning: implicit conversion loses floating-point precision: 'long double' to 'cpp::enable_if_t<cpp::is_floating_point_v<float>, float>' (aka 'float') [-Wimplicit-float-conversion]
    return to;
    ~~~~~~ ^~
/android0/llvm-project/libc/src/math/generic/nexttowardf.cpp:16:18: note: in instantiation of function template specialization '__llvm_libc_18_0_0_git::fputil::nexttoward<float>' requested here
  return fputil::nexttoward(x, y);
                 ^
In file included from /android0/llvm-project/libc/src/math/generic/nexttowardf.cpp:10:
/android0/llvm-project/libc/src/__support/FPUtil/ManipulationFunctions.h:194:12: warning: implicit conversion loses floating-point precision: 'long double' to 'cpp::enable_if_t<cpp::is_floating_point_v<float>, float>' (aka 'float') [-Wimplicit-float-conversion]
    return to;
    ~~~~~~ ^~
```

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


More information about the libc-commits mailing list