[libc-commits] [libc] [llvm] [libc][math][c23] Improve rsqrtf16() function (PR #160639)
via libc-commits
libc-commits at lists.llvm.org
Sat Jun 6 08:34:03 PDT 2026
================
@@ -24,6 +24,193 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
+namespace rsqrtf16_internal {
+
+// Fixed-point computations below use Q29: the integer N represents
+// N * 2^-29. Multiplying two Q29 values produces a Q58 value, so products are
+// shifted right by RSQRT_FRACTION_BITS to return to Q29.
+LIBC_INLINE_VAR constexpr int RSQRT_FRACTION_BITS = 29;
+LIBC_INLINE_VAR constexpr int64_t ONE = int64_t(1) << RSQRT_FRACTION_BITS;
+LIBC_INLINE_VAR constexpr int64_t THREE_HALVES = 3 * (ONE >> 1);
+
+// Midpoint lookup table for 1/sqrt(x) on 16 sub-intervals of [0.5;1).
+// Values are stored in Q29 fixed-point format. The Newton step and exact
+// rounding below correct the seed before producing the final half result.
+LIBC_INLINE_VAR constexpr uint32_t RSQRT_APPROX[16] = {
+ 747'657'839, 725'981'977, 706'088'274, 687'745'184,
+ 670'761'200, 654'976'372, 640'255'922, 626'485'368,
+ 613'566'757, 601'415'717, 589'959'130, 579'133'272,
+ 568'882'316, 559'157'115, 549'914'212, 541'115'017,
+};
+LIBC_INLINE_VAR constexpr int64_t ONE_OVER_SQRT2 = 0x16a09e60;
+
+LIBC_INLINE constexpr int floor_log2(uint64_t x) {
+ return 63 - cpp::countl_zero(x);
+}
+
+LIBC_INLINE constexpr int64_t initial_approximation(uint32_t x_mant) {
+ return RSQRT_APPROX[(x_mant - 0x0400) >> 6];
+}
+
+LIBC_INLINE constexpr int64_t newton_raphson(uint32_t m, int64_t y) {
+ // Refine y ~= 1/sqrt(m) with:
+ // y_{n+1} = y_n * (1.5 - 0.5 * m * y_n^2)
+ // where both m and y are stored in Q29.
+ int64_t y2 = (y * y) >> RSQRT_FRACTION_BITS;
+ int64_t my2 = (static_cast<int64_t>(m) * y2) >> RSQRT_FRACTION_BITS;
+ int64_t factor = THREE_HALVES - (my2 >> 1);
+ return (y * factor) >> RSQRT_FRACTION_BITS;
+}
+
+LIBC_INLINE constexpr uint16_t fixed_to_half_bits(uint64_t y, int scale_exp) {
----------------
lntue wrote:
Add some comments for this functions, like what's its expected output.
https://github.com/llvm/llvm-project/pull/160639
More information about the libc-commits
mailing list