Skip to content

Commit 08ac738

Browse files
committed
fixup! [libc][math][c23] Add f16subf C23 math function
Fix double rounding error.
1 parent 311a349 commit 08ac738

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

libc/src/__support/FPUtil/generic/add_sub.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,21 @@ add_or_sub(InType x, InType y) {
9898
}
9999
}
100100

101-
return static_cast<OutType>(y);
101+
// volatile prevents Clang from converting tmp to OutType and then
102+
// immediately back to InType before negating it, resulting in double
103+
// rounding.
104+
volatile InType tmp = y;
105+
if constexpr (IsSub)
106+
tmp = -tmp;
107+
return static_cast<OutType>(tmp);
102108
}
103109

104-
if (y_bits.is_zero())
105-
return static_cast<OutType>(x);
110+
if (y_bits.is_zero()) {
111+
volatile InType tmp = y;
112+
if constexpr (IsSub)
113+
tmp = -tmp;
114+
return static_cast<OutType>(tmp);
115+
}
106116
}
107117

108118
InType x_abs = x_bits.abs().get_val();

0 commit comments

Comments
 (0)