[libc-commits] [libc] [libc][math] Add bit-rounding roundf implementation (PR #221648)

via libc-commits libc-commits at lists.llvm.org
Mon Sep 7 07:03:01 PDT 2026


================
@@ -104,6 +104,31 @@ LIBC_INLINE constexpr T floor(T x) {
   }
 }
 
+LIBC_INLINE constexpr float round(float x) {
----------------
sriramshastry wrote:

Thanks, updated this to replace the generic `fputil::round<T>` implementation
instead of adding a float-only overload. I also added comments around the
special-value, sub-unit, already-integral, and fractional-mask cases.

The core bit operation is:

`  round_u = (x_u + 2^(k - 1)) & ~(2^k - 1)`

where `k = FRACTION_LEN - exponent` is the number of fractional bits to clear.
This combines the old truncation plus half-bit check into one integer operation
while preserving C round() halfway-away-from-zero behavior.

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


More information about the libc-commits mailing list