Skip to content

float32 Log: correctly rounded (CORE-MATH logf) with an AVX2 kernel at NumPy parity - #91

Merged
tannevaled merged 2 commits into
mainfrom
perf/float32-log-cr
Nov 2, 2026
Merged

tannevaled merged 2 commits into
mainfrom
perf/float32-log-cr

Conversation

@tannevaled

@tannevaled tannevaled commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

float32 Log is now correctly rounded and at NumPy's speed on amd64 with AVX2. Before this PR it was 3× slower than NumPy, whose own source puts its float32 log at 3.83 ULP.

The algorithm: CORE-MATH's correctly rounded binary32 logf (src/binary32/log/logf.c, Sibidanov and Zimmermann, MIT). Arm's optimized-routines has no correctly rounded logf.

  • A 64-entry table and a degree-3 polynomial in float64 bound the result. When both bounds round to the same float32, that is the answer.
  • Otherwise a degree-8 polynomial decides. That happens for 762 750 of the 2³¹ positive inputs.
  • logCR transcribes it one C operation per Go operation. Every product is converted to float64 explicitly, so arm64, ppc64le and s390x cannot fuse it.

Proof over all 2³² float32 inputs (NDARRAY_EXHAUSTIVE=1):

  • TestLogCRMatchesLog32: logCR equals v0.33.0's log32 on every input, with 0 differences on darwin/arm64, Zen 3 and POWER9. That log32 widens to float64 Log and uses an exception table; it shares no code with logCR.
  • TestLog32CorrectlyRounded: Log32 against the 300-bit reference gives 0 incorrectly rounded on darwin/arm64 and linux/ppc64le (POWER9), both the Go path, and on Zen 3 (the AVX2 kernel).

The kernel: log32CRAVX2 (go-asmgen, AVX2) runs the fast path on 8 floats at a time.

  • It gathers the tables with VGATHERDPD and uses VMULPD/VADDPD in logCR's order, never fused.
  • A block it cannot finish is not stored and is handed back to logCR: an input that is not a positive normal float32, or bounds that round apart.
  • It is bit-identical to logCR on 300 000 inputs and at every length 0–70, in place too. It is fenced, and its constant-block offsets are tested.
float32 Log, 16 Ki, one Zen 3 core (load 0.35) time
v0.33.0 91.5 µs
this PR (AVX2) 31.1 µs
NumPy 2.5.4 31.0 µs

Through the API against v0.33.0 (abbench, 8 interleaved rounds, load ≤ 8): −65% at 16 Ki and −68% at 1 Mi, p = 0.000.

The old via64 log32 and its exception table move to via64_oracle_test.go as the oracle. The audit entry is in SECURITY.md. Still open: the s390x exhaustive run (LinuxONE unreachable today; CI's s390x lane runs the sample), and the SIMD kernels for the other architectures.

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 2, 2026 12:40
logCR transcribes CORE-MATH's binary32 logf (Sibidanov and Zimmermann,
MIT): a 64-entry table and a degree-3 polynomial in float64 bound the
result; a degree-8 polynomial decides the 762 750 positive inputs whose
bounds round apart. Products are converted to float64 explicitly so no
architecture fuses them. TestLogCRMatchesLog32 finds it equal to log32,
already proven correctly rounded, on all 2^32 inputs.

log32CRAVX2 runs the fast path on 8 floats at a time (VGATHERDPD for the
tables, unfused VMULPD/VADDPD in logCR's order) and hands back each block
it cannot finish to logCR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Log32 and Log32P now run log32Fast: log32CRAVX2 on amd64 with AVX2, logCR
in Go elsewhere. TestLog32CorrectlyRounded, run on all 2^32 inputs against
the 300-bit reference, finds 0 incorrectly rounded on darwin/arm64 (Go) and
Zen 3 (AVX2).

The via64 log32 and its 24-entry exception table move to a test file as
the independent oracle. perf.md, README and SECURITY.md describe the new
path; float32 Log goes from 3x NumPy to parity on one Zen 3 core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit c5ddb62 into main Nov 2, 2026
12 checks passed
@tannevaled
tannevaled deleted the perf/float32-log-cr branch November 2, 2026 21:03
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