Files
Jungfraujoch/compression/BitShuffleBlock.h
T
leonarski_f 5c8011fe67 compression: verify the bitshuffle seam for Apple Silicon; SIMD test knows NEON
The x86/ARM split asked for on macOS already exists: BitShuffleBlock.h (fbc507839) sends
the per-block transform to bitshuffle_hperf everywhere except aarch64 with NEON, where it
uses the classic bitshuffle's NEON path, and both call sites go through it. Nothing new is
needed for Apple Silicon - clang --target=arm64-apple-macos defines __aarch64__ and
__ARM_NEON exactly as aarch64 Linux does, both vendored files compile to arm64 Mach-O
objects, the seam resolves to bshuf_(un)trans_bit_elem and bshuf_using_NEON() is 1. The
header now says why the switch is a preprocessor one: a universal build compiles it once
per architecture, which a CMake-time answer cannot follow.

What had never happened is the NEON code actually running. It has now, under
qemu-aarch64 in the project's cross image: 504 blocks (elem 1/2/4/8, 8 elements up to the
128 kB block, odd multiples of 8, random and detector-like data) plus 54 whole-buffer
bitshuffle/LZ4 streams with element counts that are no multiple of 8 or of the block. The
output is byte-identical (same md5) across aarch64 NEON, aarch64 without NEON (seam falls
back to hperf's portable code), x86 hperf AVX2 via ifunc, hperf SSE2, hperf portable
fallback, and classic SSE2 / AVX2 / scalar; every implementation decodes every other.

Throughput, one thread on a loaded Zen 3, 16/32-bit, GB/s encode/decode: hperf AVX2
~9-12/~10-11, classic AVX2 ~8/~5.5-8, classic SSE2 ~4-5/~4.5-5.5, hperf portable fallback
auto-vectorised ~2.8/~5, not vectorised ~1/~1.8. Classic 128-bit SIMD beating hperf's
fallback is the x86 stand-in for the choice the seam makes on ARM; no ARM hardware was
available, so the NEON-vs-fallback ranking on a real core is still unmeasured.

The one thing that would have failed on aarch64 is the Bshuf_SSE test, which required
SSE2 outright. It now accepts NEON as well, which also makes it the check that a Mac or
DGX Spark build did not end up on the scalar path.

Not built: no CMake configure or jfjoch_test build was run on this machine (busy); the
test expression was compiled and run standalone on x86 only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit 5957b15493dfd0cc8a18fb780a18c2573a6eccde)
2026-09-20 18:45:04 +02:00

46 lines
2.2 KiB
C

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <bitshuffle/bitshuffle_internals.h>
#include <bitshuffle_hperf/bitshuffle.h>
// One bitshuffle block, transformed by whichever of the two vendored implementations is SIMD on
// this architecture. The two write byte-identical output and each decodes the other's, so the file
// format does not depend on the build host.
//
// bitshuffle_hperf is x86-only: outside SSE2 its whole vector body is compiled out and what remains
// is a scalar fallback. The classic bitshuffle has an aarch64 NEON path (bitshuffle_core.c,
// USEARMNEON), so aarch64 uses that instead - measured against the hperf scalar fallback it is
// ~2.5x on encode and ~1.7x on decode. Everywhere else hperf wins outright (~2x over classic SSE2),
// so it stays the default.
//
// The condition mirrors USEARMNEON in bitshuffle_core.c exactly. With NEON off the classic path
// falls back to a scalar of its own that is slower than hperf's, so it must not be selected then.
// It is a preprocessor test rather than a CMake one on purpose: Apple Silicon defines the same two
// macros as aarch64 Linux, and a macOS universal build compiles this header once per architecture,
// which a single configure-time answer could not follow.
#if (defined(__ARM_NEON__) || (__ARM_NEON)) && defined(__aarch64__)
// The classic entry points allocate their own block-sized scratch, so the caller's goes unused.
inline int64_t JFJochBitShuffleBlock(char *out, const char *in, char *, size_t size, size_t elem_size) {
return bshuf_trans_bit_elem(in, out, size, elem_size);
}
inline int64_t JFJochBitUnshuffleBlock(char *out, const char *in, char *, size_t size, size_t elem_size) {
return bshuf_untrans_bit_elem(in, out, size, elem_size);
}
#else
inline int64_t JFJochBitShuffleBlock(char *out, const char *in, char *scratch, size_t size, size_t elem_size) {
return bitshuf_encode_block(out, in, scratch, size, elem_size);
}
inline int64_t JFJochBitUnshuffleBlock(char *out, const char *in, char *scratch, size_t size, size_t elem_size) {
return bitshuf_decode_block(out, in, scratch, size, elem_size);
}
#endif