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

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 06:00:53 PST 2023


================
@@ -169,8 +171,49 @@ 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, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
+LIBC_INLINE T nexttoward(T from, long double to) {
----------------
lntue wrote:

Essentially, when we do SFINAE with `template<T, U = enable_if_t<..., void>>`, we do not intend to let user use the second template parameter, but nothing prevents user code to be `func<T, U>(...)`, and it might silently get wrong function.  If all users do is to call `func(...)` or `func<T>` then it doesn't matter.

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


More information about the llvm-commits mailing list