keccak-fast.c

Minimal SIMD keccak implementation
git clone git://git.finwo.net/lib/keccak-fast.c
Log | Files | Refs | README | LICENSE

commit ac6c9d81ddd20adbc4c4ce392428cead428c881c
parent 5365e8ba2caa7a1810f2e05ee9af90c30a82d6f2
Author: finwo <finwo@pm.me>
Date:   Sun, 11 Oct 2026 05:11:44 +0200

Load/store optimizations on avx for turboshake

Diffstat:
Msrc/backend/avx2.c | 77++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/backend/avx512.c | 69++++++++++++++++++++++++++++++++++-----------------------------------
2 files changed, 72 insertions(+), 74 deletions(-)

diff --git a/src/backend/avx2.c b/src/backend/avx2.c @@ -108,13 +108,9 @@ static inline void kf_avx2_round_x4(__m256i v[25], uint64_t rc) { v[0] = _mm256_xor_si256(v[0], _mm256_set1_epi64x((long long)rc)); } -__attribute__((target("avx2"))) -static void kf_permute_x4(uint64_t s[4][25]) { - __m256i v[25]; - __m256i t[4]; - uint64_t tail[4]; - -/* 4x4 transpose of 64-bit elements: 4 unpack + 4 lane permute. */ +/* 4x4 transpose of 64-bit elements: 4 unpack + 4 lane permute. Shared by the + 24-round and 12-round permutes; expands against the local names s, v, t and + tail. */ #define KF_TRANSPOSE4(T) \ do { \ __m256i t0 = _mm256_unpacklo_epi64((T)[0], (T)[1]); \ @@ -136,19 +132,6 @@ static void kf_permute_x4(uint64_t s[4][25]) { for (int j = 0; j < 4; j++) v[(B) + j] = t[j]; \ } while (0) - KF_LOAD4(0); - KF_LOAD4(4); - KF_LOAD4(8); - KF_LOAD4(12); - KF_LOAD4(16); - KF_LOAD4(20); - v[24] = _mm256_set_epi64x((long long)s[3][24], (long long)s[2][24], - (long long)s[1][24], (long long)s[0][24]); - -#undef KF_LOAD4 - - for (int i = 0; i < 24; i++) kf_avx2_round_x4(v, RC[i]); - #define KF_STORE4(B) \ do { \ for (int j = 0; j < 4; j++) t[j] = v[(B) + j]; \ @@ -158,34 +141,50 @@ static void kf_permute_x4(uint64_t s[4][25]) { } \ } while (0) - KF_STORE4(0); - KF_STORE4(4); - KF_STORE4(8); - KF_STORE4(12); - KF_STORE4(16); - KF_STORE4(20); +#define KF_LOAD4_ALL() \ + do { \ + KF_LOAD4(0); \ + KF_LOAD4(4); \ + KF_LOAD4(8); \ + KF_LOAD4(12); \ + KF_LOAD4(16); \ + KF_LOAD4(20); \ + v[24] = _mm256_set_epi64x((long long)s[3][24], (long long)s[2][24], \ + (long long)s[1][24], (long long)s[0][24]); \ + } while (0) -#undef KF_STORE4 -#undef KF_TRANSPOSE4 +#define KF_STORE4_ALL() \ + do { \ + KF_STORE4(0); \ + KF_STORE4(4); \ + KF_STORE4(8); \ + KF_STORE4(12); \ + KF_STORE4(16); \ + KF_STORE4(20); \ + _mm256_storeu_si256((void *)tail, v[24]); \ + for (int i = 0; i < 4; i++) s[i][24] = tail[i]; \ + } while (0) + +__attribute__((target("avx2"))) +static void kf_permute_x4(uint64_t s[4][25]) { + __m256i v[25]; + __m256i t[4]; + uint64_t tail[4]; - _mm256_storeu_si256((void *)tail, v[24]); - for (int i = 0; i < 4; i++) s[i][24] = tail[i]; + KF_LOAD4_ALL(); + for (int i = 0; i < 24; i++) kf_avx2_round_x4(v, RC[i]); + KF_STORE4_ALL(); } __attribute__((target("avx2"))) static void kf_permute_x4_12(uint64_t s[4][25]) { __m256i v[25]; - uint64_t tmp[4]; + __m256i t[4]; + uint64_t tail[4]; - for (int j = 0; j < 25; j++) { - for (int i = 0; i < 4; i++) tmp[i] = s[i][j]; - v[j] = _mm256_loadu_si256((const __m256i *)tmp); - } + KF_LOAD4_ALL(); for (int i = 12; i < 24; i++) kf_avx2_round_x4(v, RC[i]); - for (int j = 0; j < 25; j++) { - _mm256_storeu_si256((__m256i *)tmp, v[j]); - for (int i = 0; i < 4; i++) s[i][j] = tmp[i]; - } + KF_STORE4_ALL(); } __attribute__((target("avx2"))) diff --git a/src/backend/avx512.c b/src/backend/avx512.c @@ -105,13 +105,9 @@ static inline void kf_avx512_round_x8(__m512i v[25], uint64_t rc) { v[0] = _mm512_xor_si512(v[0], _mm512_set1_epi64((long long)rc)); } -__attribute__((target("avx512f"))) -static void kf_permute_x8(uint64_t s[8][25]) { - __m512i v[25]; - __m512i t[8]; - uint64_t tail[8]; - -/* 8x8 transpose of 64-bit elements: 8 unpack + 16 shuffle. */ +/* 8x8 transpose of 64-bit elements: 8 unpack + 16 shuffle. Shared by the + 24-round and 12-round permutes; expands against the local names s, v, t and + tail. */ #define KF_TRANSPOSE8(T) \ do { \ __m512i t0 = _mm512_unpacklo_epi64((T)[0], (T)[1]); \ @@ -149,18 +145,6 @@ static void kf_permute_x8(uint64_t s[8][25]) { for (int j = 0; j < 8; j++) v[(B) + j] = t[j]; \ } while (0) - KF_LOAD8(0); - KF_LOAD8(8); - KF_LOAD8(16); - v[24] = _mm512_set_epi64((long long)s[7][24], (long long)s[6][24], - (long long)s[5][24], (long long)s[4][24], - (long long)s[3][24], (long long)s[2][24], - (long long)s[1][24], (long long)s[0][24]); - -#undef KF_LOAD8 - - for (int i = 0; i < 24; i++) kf_avx512_round_x8(v, RC[i]); - #define KF_STORE8(B) \ do { \ for (int j = 0; j < 8; j++) t[j] = v[(B) + j]; \ @@ -170,31 +154,46 @@ static void kf_permute_x8(uint64_t s[8][25]) { } \ } while (0) - KF_STORE8(0); - KF_STORE8(8); - KF_STORE8(16); +#define KF_LOAD8_ALL() \ + do { \ + KF_LOAD8(0); \ + KF_LOAD8(8); \ + KF_LOAD8(16); \ + v[24] = _mm512_set_epi64((long long)s[7][24], (long long)s[6][24], \ + (long long)s[5][24], (long long)s[4][24], \ + (long long)s[3][24], (long long)s[2][24], \ + (long long)s[1][24], (long long)s[0][24]); \ + } while (0) -#undef KF_STORE8 -#undef KF_TRANSPOSE8 +#define KF_STORE8_ALL() \ + do { \ + KF_STORE8(0); \ + KF_STORE8(8); \ + KF_STORE8(16); \ + _mm512_storeu_si512((void *)tail, v[24]); \ + for (int i = 0; i < 8; i++) s[i][24] = tail[i]; \ + } while (0) + +__attribute__((target("avx512f"))) +static void kf_permute_x8(uint64_t s[8][25]) { + __m512i v[25]; + __m512i t[8]; + uint64_t tail[8]; - _mm512_storeu_si512((void *)tail, v[24]); - for (int i = 0; i < 8; i++) s[i][24] = tail[i]; + KF_LOAD8_ALL(); + for (int i = 0; i < 24; i++) kf_avx512_round_x8(v, RC[i]); + KF_STORE8_ALL(); } __attribute__((target("avx512f"))) static void kf_permute_x8_12(uint64_t s[8][25]) { __m512i v[25]; - uint64_t tmp[8]; + __m512i t[8]; + uint64_t tail[8]; - for (int j = 0; j < 25; j++) { - for (int i = 0; i < 8; i++) tmp[i] = s[i][j]; - v[j] = _mm512_loadu_si512((const __m512i *)tmp); - } + KF_LOAD8_ALL(); for (int i = 12; i < 24; i++) kf_avx512_round_x8(v, RC[i]); - for (int j = 0; j < 25; j++) { - _mm512_storeu_si512((__m512i *)tmp, v[j]); - for (int i = 0; i < 8; i++) s[i][j] = tmp[i]; - } + KF_STORE8_ALL(); } __attribute__((target("avx512f")))