Repository navigation
Add AArch64 NEON/SVE support and Exp2/Exp10 functions - #6
Conversation
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]>
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]>
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]>
|
NEON comparison (graviton 3): x86 comparison: |
|
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: |
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]>
|
Testing SVE dispatch vs NEON: |
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]>
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]>
This comment was marked as outdated.
This comment was marked as outdated.
|
I'm removing the 2x SVE. Does not look beneficial at all |
- 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]>
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]>
|
Updated ARM benchmark: |
|
+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 |
|
It's not 4, it's 2 (NEON and SVE support, and 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. |
|
@rschu1ze Do you have time to review this? Otherwise I'll find somebody else |
|
Yes, I can review today but I'd appreciate a counter-review: ClickHouse/ClickHouse#100516 |
| void Exp2(const double* from, size_t size, double* to); | ||
| ``` | ||
|
|
||
| ### Accuracy by version |
There was a problem hiding this comment.
Is below information about accuracies generated, and if yes, can it be trusted?
There was a problem hiding this comment.
It comes from the existing Exp as it uses the same internal kernel. It's verified with the eval tests
There was a problem hiding this comment.
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_AMD64from theif - exclude
NO_ARMV81_OR_HIGHERbuilds which are without NEON.
There was a problem hiding this comment.
| #include <cmath> | ||
| #include <contrib/libs/fmath/fmath.hpp> | ||
|
|
||
| #if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86) |
There was a problem hiding this comment.
Minor: Nowhere in this file, SSE intrinsics are used. I don't think we need the include.
|
|
||
| void Exp10Plain(const float* from, size_t size, float* to) { | ||
| for (size_t i = 0; i < size; ++i) { | ||
| #ifdef __GLIBC__ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
|
||
| 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); } |
There was a problem hiding this comment.
(I actually think we can dumb this down further and not distinguish AVX1 and AVX2. The differences in practice are academic.)
There was a problem hiding this comment.
Yes. Once we have x86-v3 this dispatch will be removed and we'll use AVX2 directly (ClickHouse/ClickHouse#90043)
| #include "fastops.h" | ||
|
|
||
| #include <fastops/avx2/ops_avx2.h> | ||
| #if defined(__aarch64__) || defined(_M_ARM64) |
There was a problem hiding this comment.
I think #if defined(__aarch64__) does the trick.
There was a problem hiding this comment.
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]>
Summary
Exp2andExp10functions: new public API functions that reuse the existingPow2Vkernel with different scaling factors (1.0for exp2,log2(10)for exp10).S_Constantsdeduplication: refactored SIMD constant definitions to eliminate duplication between x86 and ARM viaS_ConstantsArchbase structs andautotype deduction (net -109 lines).Test plan
🤖 Generated with Claude Code