Files
Jungfraujoch/compression/BitShuffleBlock.h
T
leonarski_fandClaude Opus 5 fbc5078399 compression: use the NEON bitshuffle on aarch64
bitshuffle_hperf is an x86-only implementation - its entire vector body sits behind
__i386__/__x86_64__, so on aarch64 every entry point compiles down to the scalar
fallback. Measured against its own SIMD path that costs 8.3x on encode and 3.8x on
decode, and it is the transform behind every compressed image the writer produces and
every one the reader, preview and XDS plugin take apart again.

The classic bitshuffle vendored beside it does have an aarch64 NEON path, and is already
compiled into the same target, so this costs nothing new. BitShuffleBlock.h picks
bshuf_trans_bit_elem/bshuf_untrans_bit_elem there and keeps bitshuf_encode_block /
bitshuf_decode_block everywhere else, where hperf is about twice classic SSE2 and remains
the better choice. The expected aarch64 gain is ~2.5x encode / ~1.7x decode: classic NEON
is 128-bit and carries an extra pass, so it recovers part of the gap rather than all of
it. The condition mirrors USEARMNEON in bitshuffle_core.c exactly, because with NEON off
the classic scalar path is slower than hperf's and must not be selected.

Swapping implementations is only safe while the two agree bit for bit - otherwise an ARM
build would write files an x86 build could not read. They do: verified byte-identical
output and mutual cross-decoding for elem_size 1/2/4/8 over block sizes from 8 to 65536
elements. Both are always compiled in, so the new test holds them to it on every
architecture, not just the one that would notice.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 21:27:27 +02:00

43 lines
1.9 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.
#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