Skip to content

Commit ac640c6

Browse files
authored
[libc][math] Ensure fputil::nextafter is called with correct type args (#75009)
Follow up to #73698 (comment)
1 parent 65a1841 commit ac640c6

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ LIBC_INLINE T ldexp(T x, int exp) {
144144
return normal;
145145
}
146146

147-
template <
148-
typename T, typename U,
149-
cpp::enable_if_t<cpp::is_floating_point_v<T> && cpp::is_floating_point_v<U>,
150-
int> = 0>
147+
template <typename T, typename U,
148+
cpp::enable_if_t<cpp::is_floating_point_v<T> &&
149+
cpp::is_floating_point_v<U> &&
150+
(sizeof(T) <= sizeof(U)),
151+
int> = 0>
151152
LIBC_INLINE T nextafter(T from, U to) {
152153
FPBits<T> from_bits(from);
153154
if (from_bits.is_nan())
@@ -157,6 +158,9 @@ LIBC_INLINE T nextafter(T from, U to) {
157158
if (to_bits.is_nan())
158159
return static_cast<T>(to);
159160

161+
// NOTE: This would work only if `U` has a greater or equal precision than
162+
// `T`. Otherwise `from` could loose its precision and the following statement
163+
// could incorrectly evaluate to `true`.
160164
if (static_cast<U>(from) == to)
161165
return static_cast<T>(to);
162166

0 commit comments

Comments
 (0)