From 1d8762599f45b17656b187a440b4bfdf90592309 Mon Sep 17 00:00:00 2001 From: Nishant Mittal Date: Sun, 10 Dec 2023 19:57:11 +0000 Subject: [PATCH] [libc][math] Ensure fputil::nextafter is called with correct type args --- libc/src/__support/FPUtil/ManipulationFunctions.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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, - int> = 0> +template && + cpp::is_floating_point_v && + (sizeof(T) <= sizeof(U)), + int> = 0> LIBC_INLINE T nextafter(T from, U to) { FPBits 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(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(from) == to) return static_cast(to);