Skip to content

Add AArch64 NEON/SVE support and Exp2/Exp10 functions - #6

Merged
Algunenano merged 15 commits into
masterfrom
neon_base
Mar 25, 2026
Merged

Algunenano merged 15 commits into
masterfrom
neon_base

Conversation

@Algunenano

@Algunenano Algunenano commented Feb 27, 2026 •

Copy link
Copy Markdown
Member

Summary

  • AArch64 NEON SIMD support: adds a complete NEON backend for all functions (exp, exp2, exp10, log, sigmoid, tanh) using 128-bit NEON intrinsics with native FMA. Always enabled on AArch64 — no runtime dispatch needed.
  • Exp2 and Exp10 functions: new public API functions that reuse the existing Pow2V kernel with different scaling factors (1.0 for exp2, log2(10) for exp10).
  • S_Constants deduplication: refactored SIMD constant definitions to eliminate duplication between x86 and ARM via S_ConstantsArch base structs and auto type deduction (net -109 lines).
  • Tests and benchmarks: full test coverage for exp2/exp10 across all architectures, extended benchmark and eval tools.

Test plan

  • Verify AArch64 build and tests on ARM hardware
  • Verify exp2 and exp10 implementation, test and results
  • All 108 unit tests pass (exp, exp2, exp10, log, sigm, tanh × 18 configurations each) on x86, NEON and SVE
  • Run benchmarks

🤖 Generated with Claude Code

Algunenano and others added 4 commits February 27, 2026 14:10
Add 128-bit NEON `S_SIMDV` specializations for float (`<1,4>`, `<2,4>`,
`<4,4>`) and double (`<1,8>`, `<2,8>`) using `float32x4_t`/`float64x2_t`
vector types. The generic polynomial evaluation and performer machinery
(`EvalPolynomial`, `Pow2V`, `LnV`, `S_Pow`, `S_Sigmoid`, `S_Tanh`,
`S_Ln`) work unchanged through the `S_SIMDV` abstraction.

Key design decisions:
- Architecture detection via `FASTOPS_X86` / `FASTOPS_ARM64` macros
- `FASTOPS_MAX_REG_WIDTH` = 16 on ARM (128-bit) vs 32 on x86 (256-bit)
- Native FMA (`vfmaq`) always available on AArch64, no fallback needed
- Native `vcvtnq_s64_f64` / `vcvtq_f64_s64` for double-int64 conversion
  instead of the x86 magic-constant trick
- `TestZF` / `TestCF` emulated via `vmaxvq_u32` horizontal max
- `S_Constants` stores NEON vector types with constexpr brace init
- `S_MemCpyPerformer` / `S_MemSetPerformer` use NEON load/store for
  16-byte batches; 32-byte cases compile-time excluded on ARM
- `plain/ops_plain.cpp`: guard `#include <xmmintrin.h>` for x86 only

Build system detects architecture and links neon + plain on ARM,
or avx + avx2 + plain on x86. x86 tests still pass.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Both leverage the existing `Pow2V` kernel with different multiply factors:
- `Exp2(x)` = 2^x uses factor 1.0 (direct Pow2V call)
- `Exp10(x)` = 10^x uses factor log2(10) (new `c_log2_10` constant)

Full dispatch plumbing across all architectures (AVX, AVX2, NEON, plain
scalar fallback), public API, unit tests (18 configurations each), eval
tool, and benchmark tool. Accuracy matches `Exp` — slightly relaxed for
`Exp10` due to the extra multiplication.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Introduce `S_ConstantsArch<N>` base structs that provide
architecture-specific SIMD helpers (`Set1`, `Set1Int`, `CastSi`,
`CastPf`, `Source`, `Target`), then define all ~40 constants once in
shared `S_Constants<N>` using `auto` type deduction. This eliminates
maintaining two identical copies of every constant value.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Algunenano and others added 2 commits March 2, 2026 12:09
Guard x86-specific intrinsics in `fmath.hpp`, `fastops_ut.cpp`,
`benchmark.cpp`, and `eval.cpp` so the project compiles on AArch64.

Add NEON test structs (`TNeonExp`, `TNeonLog`, etc.) to the unit test,
replacing AVX/AVX2 structs on ARM.

Fix two bugs found while running the test suite on AArch64:

- `BlendVF` in `SIMDFunctions.h`: the NEON implementation used
  `vbslq_f32` (bitwise blend), but the calling code in `Pow2V` passes
  an integer value as a mask and relies on x86 `_mm256_blendv_ps`
  sign-bit selection semantics.  Fixed by broadcasting the sign bit via
  `vshrq_n_s32(..., 31)` before calling `vbslq_f32`, for both float and
  double variants.

- `Exp2Plain` / `Exp10Plain` in `ops_plain.cpp` used
  `fmath::expd(x * ln2)` / `fmath::expd(x * ln10)` as baselines.
  These disagree with `std::exp2f` / `std::exp10f` at underflow
  boundaries.  Switched to the libc functions directly.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Add NEON struct wrappers (`TNeonExp`, `TNeonLog`, etc.) and a full
`RunBenchmark<T>` implementation for AArch64, mirroring the existing
x86 AVX/AVX2 path.

Each function benchmarks four variants: libc reference, plain
scalar, NEON inexact, and NEON exact.

Also move `BenchmarkFunc` above the architecture guard so it is shared
between the x86 and ARM paths.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member Author

NEON comparison (graviton 3):

  NEON Performance Report vs libc Reference — AArch64 (clang 21, Release)

  Setup: 100 vectors × 1000 elements, best of 200 iterations. Times in seconds per batch.

  Float (32-bit)

  ┌──────────┬────────────┬──────────────┬────────────┬─────────────────┬───────────────┐
  │ Function │ libc (ref) │ NEON inexact │ NEON exact │ Speedup inexact │ Speedup exact │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ exp      │ 326 µs     │ 81 µs        │ 169 µs     │ 4.0×            │ 1.9×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ exp2     │ 281 µs     │ 71 µs        │ 158 µs     │ 3.9×            │ 1.8×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ exp10    │ 412 µs     │ 81 µs        │ 168 µs     │ 5.1×            │ 2.4×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ log      │ 320 µs     │ 125 µs       │ 158 µs     │ 2.6×            │ 2.0×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ sigmoid  │ 452 µs     │ 95 µs        │ 188 µs     │ 4.8×            │ 2.4×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ tanh     │ 1784 µs    │ 104 µs       │ 203 µs     │ 17×             │ 8.8×          │
  └──────────┴────────────┴──────────────┴────────────┴─────────────────┴───────────────┘

  Double (64-bit)

  ┌──────────┬────────────┬──────────────┬────────────┬─────────────────┬───────────────┐
  │ Function │ libc (ref) │ NEON inexact │ NEON exact │ Speedup inexact │ Speedup exact │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ exp      │ 357 µs     │ 159 µs       │ 363 µs     │ 2.2×            │ ~1×           │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ exp2     │ 319 µs     │ 138 µs       │ 339 µs     │ 2.3×            │ ~1×           │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ exp10    │ 373 µs     │ 160 µs       │ 363 µs     │ 2.3×            │ ~1×           │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ log      │ 427 µs     │ 255 µs       │ 372 µs     │ 1.7×            │ 1.1×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ sigmoid  │ 439 µs     │ 188 µs       │ 398 µs     │ 2.3×            │ 1.1×          │
  ├──────────┼────────────┼──────────────┼────────────┼─────────────────┼───────────────┤
  │ tanh     │ 1863 µs    │ 206 µs       │ 420 µs     │ 9.0×            │ 4.4×          │
  └──────────┴────────────┴──────────────┴────────────┴─────────────────┴───────────────┘

  Notes

  - Inexact mode (lower precision, ~3 ULP) delivers the largest gains — 4–5× for float exp/sigmoid, and a standout 17× for float tanh (the libc scalar tanh is particularly slow on AArch64).
  - Exact mode (≤1 ULP) is roughly 2× faster than libc for floats. For doubles its throughput matches libc since both operate at full 64-bit precision across the 2-lane NEON register.
  - The double inexact path still gives ~2× because it avoids the libm call overhead and processes 2 doubles in parallel with a tighter polynomial approximation.

x86 comparison:

Benchmark Report: AVX2 vs Plain (reference)                                                                                              
                                                                                                                                           
  Float (single precision)                                                                                                                 
                  
  ┌──────────┬─────────────┬────────┬────────┬────────┬────────────┬────────────────────┐
  │ Function │ libc (slow) │ plain  │  AVX   │  AVX2  │ AVX2 exact │ Plain/AVX2 speedup │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ exp      │ 0.1782      │ 0.1665 │ 0.0388 │ 0.0384 │ 0.0387     │ 4.34x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ exp2     │ 0.1662      │ 0.1799 │ 0.0419 │ 0.0407 │ 0.0420     │ 4.43x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ exp10    │ 0.2291      │ 0.1806 │ 0.0410 │ 0.0400 │ 0.0407     │ 4.52x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ log      │ 0.1869      │ 0.1904 │ 0.0477 │ 0.0443 │ 0.0466     │ 4.30x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ sigmoid  │ 0.1842      │ 0.2232 │ 0.0437 │ 0.0412 │ 0.0431     │ 5.42x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ tanh     │ 0.2365      │ 0.2360 │ 0.0443 │ 0.0412 │ 0.0444     │ 5.73x              │
  └──────────┴─────────────┴────────┴────────┴────────┴────────────┴────────────────────┘

  Double precision

  ┌──────────┬─────────────┬────────┬────────┬────────┬────────────┬────────────────────┐
  │ Function │ libc (slow) │ plain  │  AVX   │  AVX2  │ AVX2 exact │ Plain/AVX2 speedup │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ exp      │ 0.2844      │ 0.1667 │ 0.0710 │ 0.0751 │ 0.0712     │ 2.22x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ exp2     │ 0.1968      │ 0.1729 │ 0.0699 │ 0.0749 │ 0.0713     │ 2.31x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ exp10    │ 0.5132      │ 0.1726 │ 0.0712 │ 0.0743 │ 0.0712     │ 2.32x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ log      │ 0.2393      │ 0.2380 │ 0.0923 │ 0.0882 │ 0.0954     │ 2.70x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ sigmoid  │ 0.2906      │ 0.1974 │ 0.0721 │ 0.0708 │ 0.0741     │ 2.79x              │
  ├──────────┼─────────────┼────────┼────────┼────────┼────────────┼────────────────────┤
  │ tanh     │ 0.7246      │ 0.2217 │ 0.0725 │ 0.0699 │ 0.0750     │ 3.17x              │
  └──────────┴─────────────┴────────┴────────┴────────┴────────────┴────────────────────┘

  Key observations

  - AVX2 float is consistently 4.3-5.7x faster than plain — roughly matching the 8-wide SIMD lane width for float.
  - AVX2 double is 2.2-3.2x faster than plain — consistent with 4-wide SIMD lanes for double.
  - AVX vs AVX2: Minimal difference on float (AVX2 only ~1-5% faster), which makes sense since the math uses 256-bit ops in both. AVX2
  gains more on integer shuffles/blends used in the implementations.
  - Exact vs fast variants: The "exact" versions cost 0-15% more than the fast approximations across the board.
  - Dispatched path correctly routes to AVX2 when available — its numbers match AVX2 within noise.
  - Plain is sometimes slower than libc (e.g., sigmoid float: 0.223 vs 0.184) — this is the scalar fastops approximation, which has more
  overhead than a tight scalar loop for some functions.
  - exp2 and exp10 (new functions on this branch) show performance consistent with exp, as expected since they share the same polynomial
  core with different scaling constants.

@Algunenano

Copy link
Copy Markdown
Member Author

NEON was only doing 2 Float64 loads (128b) at a time which wasn't beneficial in multiple cases vs the libc implementation. But we can switch to do 4 to see more improvements:

NEON Performance Report — Before vs After 2× Unrolling
  Setup: 100 vectors × 1000 elements, best of 200 iterations.

  Float (32-bit) — S_SIMDV<4,4> → S_SIMDV<8,4>

  ┌──────────┬─────────┬────────────┬───────────┬──────┬───────┐
  │ Function │  Mode   │ libc (ref) │ NEON time │ Was  │  Now  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp      │ inexact │ 326 µs     │ 70 µs     │ 4.0× │ 4.7×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp      │ exact   │ 326 µs     │ 146 µs    │ 1.9× │ 2.2×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp2     │ inexact │ 289 µs     │ 63 µs     │ 3.9× │ 4.6×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp2     │ exact   │ 289 µs     │ 137 µs    │ 1.8× │ 2.1×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp10    │ inexact │ 403 µs     │ 71 µs     │ 5.1× │ 5.7×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp10    │ exact   │ 403 µs     │ 148 µs    │ 2.4× │ 2.7×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ log      │ inexact │ 334 µs     │ 113 µs    │ 2.6× │ 3.0×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ log      │ exact   │ 334 µs     │ 140 µs    │ 2.0× │ 2.4×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ sigmoid  │ inexact │ 453 µs     │ 84 µs     │ 4.8× │ 5.4×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ sigmoid  │ exact   │ 453 µs     │ 154 µs    │ 2.4× │ 2.9×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ tanh     │ inexact │ 1788 µs    │ 90 µs     │ 17×  │ 19.8× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ tanh     │ exact   │ 1788 µs    │ 159 µs    │ 8.8× │ 11.2× │
  └──────────┴─────────┴────────────┴───────────┴──────┴───────┘

  Double (64-bit) — S_SIMDV<2,8> → S_SIMDV<4,8>

  ┌──────────┬─────────┬────────────┬───────────┬──────┬───────┐
  │ Function │  Mode   │ libc (ref) │ NEON time │ Was  │  Now  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp      │ inexact │ 338 µs     │ 136 µs    │ 2.2× │ 2.5×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp      │ exact   │ 338 µs     │ 305 µs    │ ~1×  │ 1.11× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp2     │ inexact │ 317 µs     │ 123 µs    │ 2.3× │ 2.6×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp2     │ exact   │ 317 µs     │ 281 µs    │ ~1×  │ 1.13× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp10    │ inexact │ 374 µs     │ 136 µs    │ 2.3× │ 2.7×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ exp10    │ exact   │ 374 µs     │ 305 µs    │ ~1×  │ 1.23× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ log      │ inexact │ 424 µs     │ 226 µs    │ 1.7× │ 1.9×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ log      │ exact   │ 424 µs     │ 310 µs    │ 1.1× │ 1.37× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ sigmoid  │ inexact │ 442 µs     │ 167 µs    │ 2.3× │ 2.6×  │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ sigmoid  │ exact   │ 442 µs     │ 321 µs    │ 1.1× │ 1.38× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ tanh     │ inexact │ 1862 µs    │ 179 µs    │ 9.0× │ 10.4× │
  ├──────────┼─────────┼────────────┼───────────┼──────┼───────┤
  │ tanh     │ exact   │ 1862 µs    │ 330 µs    │ 4.4× │ 5.6×  │
  └──────────┴─────────┴────────────┴───────────┴──────┴───────┘

Add `S_SIMDV<8,4>` (8 floats) and `S_SIMDV<4,8>` (4 doubles) using
pairs of 128-bit NEON registers, and raise `FASTOPS_MAX_REG_WIDTH` to
32 on ARM so the performer loop processes two 128-bit chunks per
iteration.  This gives the out-of-order engine two independent
dependency chains to overlap, partially hiding FMA latency.

Benchmark results (Graviton, clang 21, -O3):
- Float exact: 1.8–8.8× → 2.1–11.2× vs libc
- Float inexact: 4.0–17× → 4.6–19.8× vs libc
- Double exact: ~1× → 1.1–1.4× vs libc
- Double inexact: 2.2–2.3× → 2.5–2.7× vs libc

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member Author

Testing SVE dispatch vs NEON:

  Performance Report — NEON (2× unrolled) vs SVE (256-bit VLA)
  Setup: 100 vectors × 1000 elements, best of 200 iterations. Graviton 3 (Neoverse V1, 256-bit SVE).

  Float (32-bit)

  ┌──────────┬─────────┬────────────┬───────────┬───────┬───────────┬───────┬─────────┐
  │ Function │  Mode   │ libc (ref) │ NEON time │ vs.   │ SVE time  │ vs.   │ SVE vs  │
  │          │         │            │           │ libc  │           │ libc  │ NEON    │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ exp      │ inexact │ 331 µs     │  70 µs    │ 4.7×  │  49 µs    │ 6.8×  │ 1.45×   │
  │ exp      │ exact   │ 331 µs     │ 147 µs    │ 2.3×  │ 102 µs    │ 3.2×  │ 1.44×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ exp2     │ inexact │ 280 µs     │  63 µs    │ 4.4×  │  45 µs    │ 6.3×  │ 1.41×   │
  │ exp2     │ exact   │ 280 µs     │ 136 µs    │ 2.1×  │  94 µs    │ 3.0×  │ 1.45×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ exp10    │ inexact │ 418 µs     │  70 µs    │ 6.0×  │  48 µs    │ 8.7×  │ 1.44×   │
  │ exp10    │ exact   │ 418 µs     │ 146 µs    │ 2.9×  │ 102 µs    │ 4.1×  │ 1.44×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ log      │ inexact │ 327 µs     │ 112 µs    │ 2.9×  │  95 µs    │ 3.5×  │ 1.19×   │
  │ log      │ exact   │ 327 µs     │ 139 µs    │ 2.4×  │ 137 µs    │ 2.4×  │ 1.02×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ sigmoid  │ inexact │ 468 µs     │  82 µs    │ 5.7×  │  56 µs    │ 8.4×  │ 1.47×   │
  │ sigmoid  │ exact   │ 468 µs     │ 153 µs    │ 3.1×  │ 108 µs    │ 4.3×  │ 1.41×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ tanh     │ inexact │ 1799 µs    │  90 µs    │ 20.0× │  62 µs    │ 28.9× │ 1.44×   │
  │ tanh     │ exact   │ 1799 µs    │ 159 µs    │ 11.3× │ 110 µs    │ 16.4× │ 1.44×   │
  └──────────┴─────────┴────────────┴───────────┴───────┴───────────┴───────┴─────────┘

  Double (64-bit)

  ┌──────────┬─────────┬────────────┬───────────┬───────┬───────────┬───────┬─────────┐
  │ Function │  Mode   │ libc (ref) │ NEON time │ vs.   │ SVE time  │ vs.   │ SVE vs  │
  │          │         │            │           │ libc  │           │ libc  │ NEON    │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ exp      │ inexact │ 340 µs     │ 137 µs    │ 2.5×  │  97 µs    │ 3.5×  │ 1.42×   │
  │ exp      │ exact   │ 340 µs     │ 306 µs    │ 1.1×  │ 212 µs    │ 1.6×  │ 1.44×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ exp2     │ inexact │ 349 µs     │ 124 µs    │ 2.8×  │  89 µs    │ 3.9×  │ 1.39×   │
  │ exp2     │ exact   │ 349 µs     │ 282 µs    │ 1.2×  │ 196 µs    │ 1.8×  │ 1.43×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ exp10    │ inexact │ 382 µs     │ 137 µs    │ 2.8×  │  97 µs    │ 3.9×  │ 1.42×   │
  │ exp10    │ exact   │ 382 µs     │ 306 µs    │ 1.2×  │ 211 µs    │ 1.8×  │ 1.45×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ log      │ inexact │ 421 µs     │ 227 µs    │ 1.9×  │ 190 µs    │ 2.2×  │ 1.19×   │
  │ log      │ exact   │ 421 µs     │ 311 µs    │ 1.4×  │ 296 µs    │ 1.4×  │ 1.05×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ sigmoid  │ inexact │ 433 µs     │ 167 µs    │ 2.6×  │ 114 µs    │ 3.8×  │ 1.47×   │
  │ sigmoid  │ exact   │ 433 µs     │ 321 µs    │ 1.3×  │ 241 µs    │ 1.8×  │ 1.33×   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼───────────┼───────┼─────────┤
  │ tanh     │ inexact │ 1866 µs    │ 180 µs    │ 10.4× │ 121 µs    │ 15.4× │ 1.48×   │
  │ tanh     │ exact   │ 1866 µs    │ 330 µs    │ 5.7×  │ 240 µs    │ 7.8×  │ 1.38×   │
  └──────────┴─────────┴────────────┴───────────┴───────┴───────────┴───────┴─────────┘

  Summary: SVE is consistently 1.4–1.5× faster than NEON across most functions for both float and double. The exception is log exact mode, which sees only ~1.02–1.05× improvement — likely due to branch-heavy
  denormal handling that doesn't benefit as much from wider vectors. The best gains are in tanh and sigmoid where SVE reaches up to 28.9× vs libc (float inexact tanh).

Add a vector-length-agnostic (VLA) SVE backend that uses `S_SIMDV<0, ElemSize>`
specializations (N=0 as the SVE tag). The SVE loop uses `svwhilelt` predicates
for natural tail handling, eliminating the complex `AVXCopy` remainder logic.

Runtime dispatch via `HaveSve` (checking HWCAP_SVE) selects SVE on any
SVE-capable machine — including 128-bit SVE2 (Graviton 4), where the
predicated loop is still cleaner than the NEON paired-register emulation.

On Graviton 3 (256-bit SVE), benchmarks show ~1.4× speedup over NEON
across most functions (exp, sigmoid, tanh), with `tanh` reaching 28.9×
vs libc in float inexact mode.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Algunenano Algunenano changed the title Add AArch64 NEON support and Exp2/Exp10 functions Add AArch64 NEON/SVE support and Exp2/Exp10 functions Mar 2, 2026
Algunenano and others added 2 commits March 3, 2026 12:14
The non-x86 scalar fallback inside `expd_v` was dead code — the only
caller (`TFmathExp` in the benchmark) also uses `__m128`/`exp_ps`, so
it is x86-only anyway. Move the whole function inside an outer x86
`#if defined(...)` block instead.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Document AArch64 NEON/SVE runtime dispatch and `HaveSve()` detection
- Fix alignment note: on AArch64 all loads/stores are naturally
  unaligned so the alignment flag has no effect
- Update benchmark tool description to mention NEON/SVE
- Guard `expd_v` behind x86 preprocessor check (dead non-x86 fallback)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Algunenano

This comment was marked as outdated.

@Algunenano

Copy link
Copy Markdown
Member Author

I'm removing the 2x SVE. Does not look beneficial at all

@Algunenano
Algunenano marked this pull request as ready for review March 15, 2026 12:14
Algunenano and others added 2 commits March 15, 2026 13:43
- Guard `exp10f`/`exp10` with `#ifdef __GLIBC__`, fall back to
  `exp(x * ln(10))` on non-glibc platforms (macOS, musl, MSVC)
- Guard `getauxval` in SVE detection with `#if defined(__linux__)`,
  return false on non-Linux platforms
- Replace dummy `Set(double, double)` in SVE with `__builtin_unreachable()`
  since the N==0 code path never calls it
- Add missing install targets for NEON/SVE headers on AArch64 and
  AVX/AVX2 headers on x86
- Use `target_compile_options(PRIVATE)` instead of `CMAKE_CXX_FLAGS`
  in NEON and SVE CMakeLists to avoid polluting parent project flags
- Add comment on SVE specializations living in SIMDFunctions.h

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Use `svrintn` before `svcvt` in SVE `CVTF2I` to guarantee
  round-to-nearest-even regardless of FPCR state, matching NEON
  `vcvtnq` and x86 `cvtps2dq` semantics
- Document the broadcast invariant on SVE Cast(float32x4_t) and
  Cast(float64x2_t): all S_Constants values must be uniform (Set1)
  since only lane 0 is extracted and broadcast to SVE width

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Algunenano and others added 2 commits March 15, 2026 13:58
Consistent with the x86 benchmark path and the glibc guard in
ops_plain.cpp.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
SVE runtime detection uses Linux-specific getauxval, and SVE is not
exposed on non-Linux AArch64 platforms (macOS, FreeBSD). Guard the
SVE include, dispatch calls, CMake subdirectory, and install target
behind Linux checks so non-Linux AArch64 builds go straight to NEON.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member Author

Updated ARM benchmark:

● All 60 tests PASSED. Here are the benchmark results:

  Float (32-bit) — 100 vectors × 1000 elements, best of 200 iterations

  ┌──────────┬─────────┬────────────┬───────────┬───────┬──────────┬───────┬────────┐
  │ Function │  Mode   │ libc (ref) │ NEON time │ vs.   │ SVE time │ vs.   │ SVE vs │
  │          │         │            │           │ libc  │          │ libc  │ NEON   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ exp      │ inexact │ 317 µs     │  71 µs    │ 4.5×  │  51 µs   │ 6.2×  │ 1.38×  │
  │ exp      │ exact   │ 317 µs     │ 148 µs    │ 2.1×  │ 105 µs   │ 3.0×  │ 1.41×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ exp2     │ inexact │ 288 µs     │  64 µs    │ 4.5×  │  47 µs   │ 6.1×  │ 1.35×  │
  │ exp2     │ exact   │ 288 µs     │ 137 µs    │ 2.1×  │ 100 µs   │ 2.9×  │ 1.37×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ exp10    │ inexact │ 739 µs     │  70 µs    │ 10.6× │  51 µs   │ 14.5× │ 1.37×  │
  │ exp10    │ exact   │ 739 µs     │ 147 µs    │ 5.0×  │ 104 µs   │ 7.1×  │ 1.41×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ log      │ inexact │ 320 µs     │ 113 µs    │ 2.8×  │  95 µs   │ 3.4×  │ 1.19×  │
  │ log      │ exact   │ 320 µs     │ 139 µs    │ 2.3×  │ 137 µs   │ 2.3×  │ 1.01×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ sigmoid  │ inexact │ 452 µs     │  83 µs    │ 5.4×  │  59 µs   │ 7.6×  │ 1.40×  │
  │ sigmoid  │ exact   │ 452 µs     │ 153 µs    │ 3.0×  │ 111 µs   │ 4.1×  │ 1.38×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ tanh     │ inexact │ 1784 µs    │  90 µs    │ 19.8× │  63 µs   │ 28.3× │ 1.43×  │
  │ tanh     │ exact   │ 1784 µs    │ 159 µs    │ 11.2× │ 115 µs   │ 15.5× │ 1.38×  │
  └──────────┴─────────┴────────────┴───────────┴───────┴──────────┴───────┴────────┘

  Double (64-bit)

  ┌──────────┬─────────┬────────────┬───────────┬───────┬──────────┬───────┬────────┐
  │ Function │  Mode   │ libc (ref) │ NEON time │ vs.   │ SVE time │ vs.   │ SVE vs │
  │          │         │            │           │ libc  │          │ libc  │ NEON   │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ exp      │ inexact │ 337 µs     │ 137 µs    │ 2.5×  │ 100 µs   │ 3.4×  │ 1.36×  │
  │ exp      │ exact   │ 337 µs     │ 306 µs    │ 1.1×  │ 214 µs   │ 1.6×  │ 1.43×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ exp2     │ inexact │ 330 µs     │ 123 µs    │ 2.7×  │  92 µs   │ 3.6×  │ 1.34×  │
  │ exp2     │ exact   │ 330 µs     │ 281 µs    │ 1.2×  │ 197 µs   │ 1.7×  │ 1.43×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ exp10    │ inexact │ 1342 µs    │ 137 µs    │ 9.8×  │ 100 µs   │ 13.4× │ 1.36×  │
  │ exp10    │ exact   │ 1342 µs    │ 306 µs    │ 4.4×  │ 216 µs   │ 6.2×  │ 1.41×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ log      │ inexact │ 416 µs     │ 227 µs    │ 1.8×  │ 188 µs   │ 2.2×  │ 1.21×  │
  │ log      │ exact   │ 416 µs     │ 311 µs    │ 1.3×  │ 297 µs   │ 1.4×  │ 1.05×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ sigmoid  │ inexact │ 429 µs     │ 167 µs    │ 2.6×  │ 118 µs   │ 3.6×  │ 1.41×  │
  │ sigmoid  │ exact   │ 429 µs     │ 321 µs    │ 1.3×  │ 235 µs   │ 1.8×  │ 1.37×  │
  ├──────────┼─────────┼────────────┼───────────┼───────┼──────────┼───────┼────────┤
  │ tanh     │ inexact │ 1859 µs    │ 179 µs    │ 10.4× │ 123 µs   │ 15.1× │ 1.46×  │
  │ tanh     │ exact   │ 1859 µs    │ 330 µs    │ 5.6×  │ 234 µs   │ 7.9×  │ 1.41×  │
  └──────────┴─────────┴────────────┴───────────┴───────┴──────────┴───────┴────────┘

  Results are consistent with previous runs. SVE remains ~1.3–1.5× faster than NEON across the board, with log exact being the outlier at ~1.01–1.05×. Note that the exp10 and exp10 double libc reference times are
  higher this run (739 µs / 1342 µs vs ~400 µs before) — likely transient CPU contention affecting the scalar libc baseline, but the NEON/SVE times are stable.

@rschu1ze

Copy link
Copy Markdown
Member

+3100 LOC and four different types of improvements according to the PR message. This cannot be reasonably be reviewed by a human. It would be nicer to split this PR into smaller pieces (or a series of smaller pieces), and integrate each of these separately as PRs into master.

@Algunenano

Copy link
Copy Markdown
Member Author

It's not 4, it's 2 (NEON and SVE support, and Exp2 and Exp10 functions), but the second one is disabled and not introduced in ClickHouse PRs. The other 2 are purely based on those.

I can remove exp2 and exp10, but they are tested and they are not used, so I don't see how can I break this further.

@Algunenano

Copy link
Copy Markdown
Member Author

@rschu1ze Do you have time to review this? Otherwise I'll find somebody else

@rschu1ze

Copy link
Copy Markdown
Member

Yes, I can review today but I'd appreciate a counter-review: ClickHouse/ClickHouse#100516

Comment thread README.md
void Exp2(const double* from, size_t size, double* to);
```

### Accuracy by version

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is below information about accuracies generated, and if yes, can it be trusted?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from the existing Exp as it uses the same internal kernel. It's verified with the eval tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the PR msg, this PR unconditionally assumes NEON on ARM (no runtime capability detection).

I see this in contrib/fastops-cmake/CMakeLists.txt:

if(ARCH_AMD64 AND NOT OS_FREEBSD AND NOT OS_DARWIN)
    option(ENABLE_FASTOPS "Enable fast vectorized mathematical functions library by Mikhail Parakhin" ${ENABLE_LIBRARIES})
elseif(ENABLE_FASTOPS)
    message (${RECONFIGURE_MESSAGE_LEVEL} "Fastops library is supported on x86_64 only, and not FreeBSD or Darwin")
endif()

So we will need to do two things:

  • remove ARCH_AMD64 from the if
  • exclude NO_ARMV81_OR_HIGHER builds which are without NEON.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread fastops/plain/ops_plain.cpp Outdated
#include <cmath>
#include <contrib/libs/fmath/fmath.hpp>

#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Nowhere in this file, SSE intrinsics are used. I don't think we need the include.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will remove


void Exp10Plain(const float* from, size_t size, float* to) {
for (size_t i = 0; i < size; ++i) {
#ifdef __GLIBC__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really clear why the GLIBC dispatch is needed. Anyways, I assume the "plain" path is never exercised so we can probably simplify to one path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In x86 we have dynamic dispatch, and it defers to plain when AVX is not supported. Since FreeBSD is not GLIBC compatible (doesn't implement these 2 functions), doing it this way allows us to support FreeBSD without changes for x86.

Comment thread fastops/fastops.cpp

template <bool I_Exact, bool I_OutAligned>
void Exp2(const float* from, size_t size, float* to) {
if (HaveAvx2()) { Exp2Avx2<I_Exact, I_OutAligned>(from, size, to); }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I actually think we can dumb this down further and not distinguish AVX1 and AVX2. The differences in practice are academic.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Once we have x86-v3 this dispatch will be removed and we'll use AVX2 directly (ClickHouse/ClickHouse#90043)

Comment thread fastops/fastops.cpp Outdated
#include "fastops.h"

#include <fastops/avx2/ops_avx2.h>
#if defined(__aarch64__) || defined(_M_ARM64)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #if defined(__aarch64__) does the trick.

@Algunenano Algunenano Mar 24, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Apparently _M_ARM64 is specific for MSVC which we don't care about and doesn't support NEON/SVE instrinsics, so no need to worry about it

- Remove unused `xmmintrin.h` include from ops_plain.cpp (no SSE
  intrinsics are used in the plain fallback)
- Simplify AArch64 detection from `__aarch64__ || _M_ARM64` to just
  `__aarch64__` — MSVC ARM64 does not support the NEON/SVE intrinsics
  we use

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Algunenano
Algunenano merged commit 074cdf6 into master Mar 25, 2026
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.

2 participants