[libc-commits] [PATCH] D120579: Use __builtin_clz to find leading 1 in generic sqrt (where possible)

Tue Ly via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Feb 25 14:06:56 PST 2022


lntue added inline comments.


================
Comment at: libc/src/__support/FPUtil/generic/sqrt.h:66
 inline void normalize<long double>(int &exponent, __uint128_t &mantissa) {
-  // Use binary search to shift the leading 1 bit similar to float.
-  // With MantissaWidth<long double> = 112, it will take
-  // ceil(log2(112)) = 7 steps checking the mantissa bits.
+  // There is no __builtin_clz for 128-bit integers, so use binary search to
+  // shift the leading 1 bit. With MantissaWidth<long double> = 112, it will
----------------
Can you do a simple perf test to see if using clz for 64 bits is faster than binary search?  Something like:


```
uint64_t hi_bits = static_cast<uint64_t>(mantissa >> 64);
int shift  = hi_bits ? (clz(hi_bits) - 15) : (clz(static_cast<uint64_t>(mantissa)) + 49);
exponent -= shift;
mantissa <<= shift;
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120579/new/

https://reviews.llvm.org/D120579



More information about the libc-commits mailing list