[libc-commits] [libc] [libc][math][c23] Add f16sqrt{, l, f128} C23 math functions (PR #96642)
via libc-commits
libc-commits at lists.llvm.org
Sun Jun 30 14:38:03 PDT 2024
================
@@ -146,91 +144,19 @@ sqrt(InType x) {
}
// We compute one more iteration in order to round correctly.
- bool lsb = (y & (InStorageType(1) << EXTRA_FRACTION_LEN)) !=
- 0; // Least significant bit
- bool rb = false; // Round bit
r <<= 2;
- InStorageType tmp = (y << 2) + 1;
+ y <<= 2;
+ InStorageType tmp = y + 1;
if (r >= tmp) {
r -= tmp;
- rb = true;
- }
-
- bool sticky = false;
-
- if constexpr (EXTRA_FRACTION_LEN > 0) {
- sticky = rb || (y & EXTRA_FRACTION_MASK) != 0;
- rb = (y & (InStorageType(1) << (EXTRA_FRACTION_LEN - 1))) != 0;
- }
-
- // Remove hidden bit and append the exponent field.
- x_exp = ((x_exp >> 1) + OutFPBits::EXP_BIAS);
-
- OutStorageType y_out = static_cast<OutStorageType>(
- ((y - ONE) >> EXTRA_FRACTION_LEN) |
- (static_cast<OutStorageType>(x_exp) << OutFPBits::FRACTION_LEN));
-
- if constexpr (EXTRA_FRACTION_LEN > 0) {
- if (x_exp >= OutFPBits::MAX_BIASED_EXPONENT) {
- switch (quick_get_round()) {
- case FE_TONEAREST:
- case FE_UPWARD:
- return OutFPBits::inf().get_val();
- default:
- return OutFPBits::max_normal().get_val();
- }
- }
-
- if (x_exp <
- -OutFPBits::EXP_BIAS - OutFPBits::SIG_LEN + EXTRA_FRACTION_LEN) {
- switch (quick_get_round()) {
- case FE_UPWARD:
- return OutFPBits::min_subnormal().get_val();
- default:
- return OutType(0.0);
- }
- }
-
- if (x_exp <= 0) {
- int underflow_extra_fraction_len = EXTRA_FRACTION_LEN - x_exp + 1;
- InStorageType underflow_extra_fraction_mask =
- (InStorageType(1) << underflow_extra_fraction_len) - 1;
-
- rb = (y & (InStorageType(1) << (underflow_extra_fraction_len - 1))) !=
- 0;
- OutStorageType subnormal_mant =
- static_cast<OutStorageType>(y >> underflow_extra_fraction_len);
- lsb = (subnormal_mant & 1) != 0;
- sticky = sticky || (y & underflow_extra_fraction_mask) != 0;
-
- switch (quick_get_round()) {
- case FE_TONEAREST:
- if (rb && (lsb || sticky))
- ++subnormal_mant;
- break;
- case FE_UPWARD:
- if (rb || sticky)
- ++subnormal_mant;
- break;
- }
-
- return cpp::bit_cast<OutType>(subnormal_mant);
- }
- }
-
- switch (quick_get_round()) {
- case FE_TONEAREST:
- // Round to nearest, ties to even
- if (rb && (lsb || (r != 0)))
- ++y_out;
- break;
- case FE_UPWARD:
- if (rb || (r != 0) || sticky)
- ++y_out;
- break;
+ // Rounding bit.
+ y |= 1 << 1;
----------------
lntue wrote:
Just put `y |= 2` is fine.
Also, can you add a TODO on the main for loop to reduce it to `OutFPBits::FRACTION_LEN + 2` or 3?
https://github.com/llvm/llvm-project/pull/96642
More information about the libc-commits
mailing list