[libc-commits] [libc] [libc][math] Ensure fputil::nextafter is called with correct type args (PR #75009)
via libc-commits
libc-commits at lists.llvm.org
Sun Dec 10 12:00:46 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Nishant Mittal (nishantwrp)
<details>
<summary>Changes</summary>
Follow up to https://github.com/llvm/llvm-project/pull/73698#discussion_r1411134482
---
Full diff: https://github.com/llvm/llvm-project/pull/75009.diff
1 Files Affected:
- (modified) libc/src/__support/FPUtil/ManipulationFunctions.h (+8-4)
``````````diff
diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h
index 9d3fd075be471..08adb074b121f 100644
--- a/libc/src/__support/FPUtil/ManipulationFunctions.h
+++ b/libc/src/__support/FPUtil/ManipulationFunctions.h
@@ -144,10 +144,11 @@ LIBC_INLINE T ldexp(T x, int exp) {
return normal;
}
-template <
- typename T, typename U,
- cpp::enable_if_t<cpp::is_floating_point_v<T> && cpp::is_floating_point_v<U>,
- int> = 0>
+template <typename T, typename U,
+ cpp::enable_if_t<cpp::is_floating_point_v<T> &&
+ cpp::is_floating_point_v<U> &&
+ (sizeof(T) <= sizeof(U)),
+ int> = 0>
LIBC_INLINE T nextafter(T from, U to) {
FPBits<T> from_bits(from);
if (from_bits.is_nan())
@@ -157,6 +158,9 @@ LIBC_INLINE T nextafter(T from, U to) {
if (to_bits.is_nan())
return static_cast<T>(to);
+ // NOTE: This would work only if `U` has a greater or equal precision than
+ // `T`. Otherwise `from` could loose its precision and the following statement
+ // could incorrectly evaluate to `true`.
if (static_cast<U>(from) == to)
return static_cast<T>(to);
``````````
</details>
https://github.com/llvm/llvm-project/pull/75009
More information about the libc-commits
mailing list