From 3c7efcd1e5c9bd1f3b70ce50a027b9be5c900d0c Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 26 Sep 2026 19:35:07 +0200 Subject: [PATCH] FFTW: build the NEON codelets on aarch64 FFTW's CMake build knows only SSE/SSE2/AVX/AVX2; NEON exists only in its autotools build (--enable-neon). The ENABLE_NEON this file set was silently ignored, so on Apple Silicon and the Linux aarch64 build FFTW ran its scalar codelets (config.h: HAVE_NEON undefined, no simd/neon objects). The NEON sources are now added to fftw3f the way its CMakeLists adds the SSE2/AVX ones, with HAVE_NEON as a target definition (the config.h template carries it only as a comment). No compiler flag: NEON is part of every aarch64 CPU. FFTW 3.3.11 would not help - its CMake build has no NEON either, and its Apple ARM cycle counter only matters to FFTW_MEASURE planning, while every plan here is FFTW_ESTIMATE. FFTW's runtime NEON probe on unix/linux executes .long 0xf2000150; on aarch64 that decodes as ands x16, x10, #0x100000001 and runs without SIGILL (checked on Apple Silicon), so the codelets are used on Linux too. Measured on Apple Silicon against the scalar build: batched 1D r2c 1.8x (power-of-two length) and 1.3x (730), 2D r2c 1024^2 1.9x, 3D c2r 128^3 2.6x, spectra equal to 1.6e-8 relative. rugnux -X FFTW on 360 frames of the rotation test set: merged .hkl byte-identical, report identical; CPU time unchanged (300 s), so FFTs are a small share of that run. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15579e366..33fd421d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -402,13 +402,12 @@ SET(ENABLE_FLOAT ON CACHE BOOL "" FORCE) SET(BUILD_TESTS OFF CACHE BOOL "" FORCE) # SIMD codelets are runtime-dispatched (cpuid), so enabling AVX2 does NOT require an AVX2 CPU # -- FFTW falls back at runtime. The codelet sets are arch-specific, so guard by processor: -# x86 gets SSE2/AVX/AVX2, aarch64 (e.g. Grace on DGX Spark) gets NEON; other arches build scalar. +# x86 gets SSE2/AVX/AVX2, aarch64 (Apple Silicon, Grace on DGX Spark) gets NEON - added below the +# FetchContent, as FFTW's CMake build has no switch for it; other arches build scalar. IF (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64") SET(ENABLE_SSE2 ON CACHE BOOL "" FORCE) SET(ENABLE_AVX ON CACHE BOOL "" FORCE) SET(ENABLE_AVX2 ON CACHE BOOL "" FORCE) -ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|ARM64") - SET(ENABLE_NEON ON CACHE BOOL "" FORCE) ENDIF() FetchContent_Declare(fftw URL https://www.fftw.org/fftw-3.3.10.tar.gz @@ -419,6 +418,19 @@ FetchContent_MakeAvailable(fftw) # the build tree (FetchContent) leaves the header unreachable. On Linux a system-installed # fftw3.h masks this; on Windows it does not. Add the source-tree api/ dir for build-tree use. TARGET_INCLUDE_DIRECTORIES(fftw3f INTERFACE $) +# NEON: only FFTW's autotools build knows it (--enable-neon); its CMake build silently builds scalar +# code on ARM (and ignores an ENABLE_NEON, as this file once set). So the NEON codelets are added +# the way its CMakeLists adds the SSE2/AVX ones: the sources, and HAVE_NEON so that dft/conf.c and +# rdft/conf.c register them - as a definition, because its config.h template carries HAVE_NEON only +# as a comment. NEON is part of every aarch64 CPU, so no compiler flag is needed. It is single +# precision, which is all that is built here. Measured on Apple Silicon against the scalar build +# (same results to 1e-8): the indexer's batched 1D r2c 1.8x faster at a power-of-two length and 1.3x +# at 730, the 2D r2c of the beam-centre search 1.9x, a 128^3 c2r map 2.6x. +IF (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|ARM64") + FILE(GLOB _fftw_neon_sources ${fftw_SOURCE_DIR}/dft/simd/neon/*.c ${fftw_SOURCE_DIR}/rdft/simd/neon/*.c) + TARGET_SOURCES(fftw3f PRIVATE ${_fftw_neon_sources}) + TARGET_COMPILE_DEFINITIONS(fftw3f PRIVATE HAVE_NEON=1) +ENDIF() ADD_COMPILE_DEFINITIONS(JFJOCH_USE_FFTW) # Eigen (header-only), consumed by the analysis libraries directly and by Ceres and ffbidx through