Skip to contentSkip to conversation

zstd: Build with -O3 and use shift mask on arm64

PATCH New Jenhao Chen 2 participants 3 messages Oct 30 – Nov 2 Zstd

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zstd: Build with -O3 and use shift mask on arm64

Upstream user-space zstd builds with -O3 by default, and lib/lz4/Makefile already sets ccflags-y += -O3. However, lib/zstd/Makefile compiles at -O2, and BIT_getMiddleBits() only enables the branchless shift-and-mask path on x86_64, falling back to a BIT_mask[] lookup table load on arm64.

Add ccflags-y += -O3 to lib/zstd/Makefile and enable the shift-and-mask path in BIT_getMiddleBits() on __aarch64__. On arm64, this reduces zstd CPU cycles by ~7% and improves decompression throughput by 12-23%.

---
 lib/zstd/Makefile           | 2 ++
 lib/zstd/common/bitstream.h | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
lib/zstd/Makefile +2 -0
index be218b5e0ed59..900922ab5b94a 100644
@@ -12,6 +12,8 @@ obj-$(CONFIG_ZSTD_COMPRESS) += zstd_compress.o
 obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd_decompress.o
 obj-$(CONFIG_ZSTD_COMMON) += zstd_common.o
 
+ccflags-y += -O3
+
 zstd_compress-y := \
 		zstd_compress_module.o \
 		compress/fse_compress.o \
lib/zstd/common/bitstream.h +1 -1
index 86439da0eea76..01e69ab7053b4 100644
@@ -299,7 +299,7 @@ FORCE_INLINE_TEMPLATE BitContainerType BIT_getMiddleBits(BitContainerType bitCon
      * such cpus old (pre-Haswell, 2013) and their performance is not of that
      * importance.
      */
-#if defined(__x86_64__) || defined(_M_X64)
+#if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__)
     return (bitContainer >> (start & regMask)) & ((((U64)1) << nbBits) - 1);
 #else
     return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
Signature · 1 line
2.56.0.317.g454f8302e1-goog
#1 lore raw
kernel test robot noticed the following build errors:

Hi Jenhao,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-nonmm-unstable]
[also build test ERROR on linus/master v7.3 next-20261030] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jenhao-Chen/zstd-Build-with-O3-and-use-shift-mask-on-arm64/20261030-121633
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable
patch link:    https://lore.kernel.org/r/20261030121633.258612-1-jenhaochen%40google.com
patch subject: [PATCH] zstd: Build with -O3 and use shift mask on arm64
config: nios2-randconfig-1000-20261101 (https://download.01.org/0day-ci/archive/20261101/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261101/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: lib/zstd/zstd_compress.ko: symbol '__divsi3_table' undefined!

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#2 lore raw

PATCH v2 1/1 zstd: Use shift mask in BIT_getMiddleBits() on arm64

In reply to Jenhao Chen

In ZSTD_decodeSequence(), ZSTD_updateFseStateWithDInfo() calls BIT_readBits() -> BIT_lookBits() -> BIT_getMiddleBits() three times per decoded sequence (for literal length, match length, and offset FSE states). However, BIT_getMiddleBits() only enables the branchless register shift-and-mask path on x86_64, falling back to a BIT_mask[] lookup table load on arm64.

Enable the shift-and-mask path in BIT_getMiddleBits() on __aarch64__ so the mask is computed directly in registers instead of loading from BIT_mask[]. On arm64 at -O2, this eliminates three table loads per decoded sequence, shrinks zstd decompression .text by 212 bytes, and improves decompression throughput by 1-3%.

---
Changes in v2:
- Drop the Makefile -O3 change, which triggered an undefined __divsi3_table reference in zstd_compress.ko on nios2-linux-gcc (reported by kernel test robot <[email protected]>) and previously caused stack frame size warnings on parisc (commit 7416cdc9b9c1).
- Update the commit message with isolated -O2 numbers for the BIT_getMiddleBits() shift-mask change alone.
- Link to v1: https://lore.kernel.org/r/[email protected]/

 lib/zstd/common/bitstream.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/zstd/common/bitstream.h +1 -1
index 86439da0eea76..01e69ab7053b4 100644
@@ -299,7 +299,7 @@ FORCE_INLINE_TEMPLATE BitContainerType BIT_getMiddleBits(BitContainerType bitCon
      * such cpus old (pre-Haswell, 2013) and their performance is not of that
      * importance.
      */
-#if defined(__x86_64__) || defined(_M_X64)
+#if defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__)
     return (bitContainer >> (start & regMask)) & ((((U64)1) << nbBits) - 1);
 #else
     return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];
Signature · 1 line
2.56.0.317.g454f8302e1-goog