Skip to content

Improve pow and powi correctness and compile-time support - #94

Open
jmh530 wants to merge 4 commits into
libmir:masterfrom
jmh530:fix-pow-powi
Open

jmh530 wants to merge 4 commits into
libmir:masterfrom
jmh530:fix-pow-powi

Conversation

@jmh530

@jmh530 jmh530 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Enable compile-time evaluation of pow and powi with LDC, preserve large integer exponents in Windows float powi, and align special-value behavior across runtime and CTFE.

LDC now delegates power calculations to Phobos during CTFE while retaining LLVM intrinsics at runtime. Public signatures, return types, and attributes remain unchanged.

On Microsoft runtime targets, LLVM lowers float powi through powf, rounding integer exponents beyond float’s exact range. This can make powi(-1.0f, int.max) return +1. Use the double-precision intrinsic for these exponents, with an out-of-line helper to prevent speculative evaluation of both paths.

Normalize special values before the Phobos fallback, including pow(1, NaN), unit bases with infinite exponents, signed zero, and infinite bases with fractional exponents. Determine parity without integer casts so extended-precision exponents beyond long.max remain supported.

Add runtime and CTFE regressions across float, double, and real. Enable mir_core_test in coverage builds so the existing CI command runs the version-gated tests.

Validation:

  • Windows DMD 2.113 and LDC 1.43 coverage suites passed before the final test-block split.
  • Focused optimized power tests passed after that split on both Windows compilers and Linux LDC 1.36.
  • An optimized LDC full-suite failure in complex logarithm tests was reproduced on the unchanged baseline.
  • GDC, macOS, and 32-bit targets were not tested locally.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

Define unittest-cov explicitly with mir_core_test enabled so the
existing CI coverage command runs the gated unit tests. Previously,
DUB's default coverage build omitted this version identifier.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Delegate pow and powi to Phobos during CTFE while retaining LLVM
intrinsics at runtime. Preserve function signatures, return types,
and attributes.

This enables compile-time calculations in downstream code, including
mir-stat's Weibull PDF and raw moments. Add regression tests covering
floating-point types, exponent boundaries, and function-pointer use.

Prefer LLVM_major when available to avoid deprecation diagnostics,
with a fallback for older LDC releases.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
On Microsoft runtime targets, LLVM lowers float powi to powf after
converting the exponent to float. Beyond 2^24, this can round odd
exponents to even values, making powi(-1.0f, int.max) return +1.

Use the double-precision intrinsic outside float's exact integer
range, then convert the result back to the original type. Double
represents every int exponent exactly.

Keep the fallback out of line to prevent LLVM from evaluating both
precision paths before selecting a result. Retain the existing
float intrinsic path for exponents within the exact range.

Add regressions for exponent boundaries, negative powers, signed
zero, infinities, and NaN.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Phobos runtime pow differs from LLVM and compile-time evaluation
for several special inputs, including pow(1, NaN), unit bases with
infinite exponents, and infinite bases with fractional exponents.

Handle these cases before invoking the Phobos fallback, following
libm conventions. Preserve signed-zero results and determine
exponent parity without integer casts, allowing extended-precision
real exponents beyond long.max.

Apply the same normalization to LDC's CTFE fallback while retaining
its runtime LLVM intrinsic. Leave ordinary finite calculations with
their existing backends.

Document the conventions and add explicit expected-value tests
across float, double, and real at runtime and during CTFE.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant