[libc-commits] [libc] [llvm] [libc][math] Optimize exp2 and refactor shared double-precision exp utilities. (PR #231041)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 07:03:04 PST 2026
================
@@ -68,6 +95,452 @@ ziv_test_denorm(int hi, double mid, double lo, double err) {
}
}
+namespace math {
+namespace double_eval {
+
+using fputil::DoubleDouble;
+
+// log(2) in double-double precision, generated by Sollya with:
+// > display = hexadecimal!;
+// > prec = 500;
+// > hi = round(log(2), D, RN);
+// > lo = round(log(2) - hi, D, RN);
+// > print("{", lo, ",", hi, "};");
+LIBC_INLINE_VAR constexpr DoubleDouble LOG_2 = {0x1.abc9e3b39803fp-56,
+ 0x1.62e42fefa39efp-1};
+
+// Directly add an integer `hi` to the exponent field of a double precision `r`.
+// It is equivalent to scaling `r` by 2^hi when both `r` and the resulting bits
+// are normal.
+// The default implementation is simply bit cast `r` to int64`, shift `hi` to
+// the exponent field, then add `r` bits and shifted `hi`, before bit cast the
+// result back to double.
+// The addition is done in integer, and most likely generated as addition in the
+// general purpose registers (GPR), causing a full round trip from
+// floating-point/SIMD register -> GPR -> FP/SIMD.
+// When SSE2 or NEON are available, we can use the compiler intrinsic to keep
+// `r` in the FP/SIMD register, and perform the integer addition in the FP/SIMD
+// registers instead. This in general results in a reduction in latency since
+// `r` is normally on the critical path.
+LIBC_ALWAYS_INLINE double scale_exp2(double r, int hi) {
+ using FPBits = typename fputil::FPBits<double>;
+#ifdef LIBC_TARGET_CPU_HAS_SSE2
+ // Move `hi` to SIMD register and shift to exponent field position.
+ __m128i v_exp = _mm_slli_epi64(_mm_cvtsi32_si128(hi), FPBits::FRACTION_LEN);
+ // Load `r` and "bit_cast" it to integer SIMD type.
+ __m128i v_r = _mm_castpd_si128(_mm_set_sd(r));
+ // Perform integer addition in SIMD registers and bit cast the result back to
+ // double.
+ return _mm_cvtsd_f64(_mm_castsi128_pd(_mm_add_epi64(v_exp, v_r)));
+#elif defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_NEON)
+ // Shift `hi` to exponent field position and move it to SIMD register.
+ int64x1_t v_exp =
+ vdup_n_s64(static_cast<int64_t>(hi) << FPBits::FRACTION_LEN);
+ // Bit cast `r` to integer SIMD type.
+ int64x1_t v_r = vreinterpret_s64_f64(vdup_n_f64(r));
+ // Perform integer addition in SIMD registers and bit cast the result back to
+ // double.
+ return vget_lane_f64(vreinterpret_f64_s64(vadd_s64(v_exp, v_r)), 0);
+#else
+ int64_t exp_hi = static_cast<int64_t>(hi) * (1LL << FPBits::FRACTION_LEN);
+ return cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(r));
+#endif // LIBC_TARGET_CPU_HAS_SSE2
+}
+
+// Check for exceptional cases (tiny, overflow, underflow, inf, or nan) for
+// exp, exp2, and exp10. Callers have already verified that x is in the exact
+// tiny, underflow, or overflow/inf/nan range for the specific function before
+// calling this helper.
+LIBC_NOINLINE LIBC_INLINE double set_exceptional(double x) {
----------------
lntue wrote:
needs inline for header-defined functions to dedup across TUs, while no_inline to prevent being inlined at call sites.
https://github.com/llvm/llvm-project/pull/231041
More information about the libc-commits
mailing list