Path: blob/main/contrib/llvm-project/llvm/lib/Support/BLAKE3/blake3_neon.c
35266 views
#include "blake3_impl.h"12#if BLAKE3_USE_NEON34#include <arm_neon.h>56#ifdef __ARM_BIG_ENDIAN7#error "This implementation only supports little-endian ARM."8// It might be that all we need for big-endian support here is to get the loads9// and stores right, but step zero would be finding a way to test it in CI.10#endif1112INLINE uint32x4_t loadu_128(const uint8_t src[16]) {13// vld1q_u32 has alignment requirements. Don't use it.14uint32x4_t x;15memcpy(&x, src, 16);16return x;17}1819INLINE void storeu_128(uint32x4_t src, uint8_t dest[16]) {20// vst1q_u32 has alignment requirements. Don't use it.21memcpy(dest, &src, 16);22}2324INLINE uint32x4_t add_128(uint32x4_t a, uint32x4_t b) {25return vaddq_u32(a, b);26}2728INLINE uint32x4_t xor_128(uint32x4_t a, uint32x4_t b) {29return veorq_u32(a, b);30}3132INLINE uint32x4_t set1_128(uint32_t x) { return vld1q_dup_u32(&x); }3334INLINE uint32x4_t set4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {35uint32_t array[4] = {a, b, c, d};36return vld1q_u32(array);37}3839INLINE uint32x4_t rot16_128(uint32x4_t x) {40return vorrq_u32(vshrq_n_u32(x, 16), vshlq_n_u32(x, 32 - 16));41}4243INLINE uint32x4_t rot12_128(uint32x4_t x) {44return vorrq_u32(vshrq_n_u32(x, 12), vshlq_n_u32(x, 32 - 12));45}4647INLINE uint32x4_t rot8_128(uint32x4_t x) {48return vorrq_u32(vshrq_n_u32(x, 8), vshlq_n_u32(x, 32 - 8));49}5051INLINE uint32x4_t rot7_128(uint32x4_t x) {52return vorrq_u32(vshrq_n_u32(x, 7), vshlq_n_u32(x, 32 - 7));53}5455// TODO: compress_neon5657// TODO: hash2_neon5859/*60* ----------------------------------------------------------------------------61* hash4_neon62* ----------------------------------------------------------------------------63*/6465INLINE void round_fn4(uint32x4_t v[16], uint32x4_t m[16], size_t r) {66v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);67v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);68v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);69v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);70v[0] = add_128(v[0], v[4]);71v[1] = add_128(v[1], v[5]);72v[2] = add_128(v[2], v[6]);73v[3] = add_128(v[3], v[7]);74v[12] = xor_128(v[12], v[0]);75v[13] = xor_128(v[13], v[1]);76v[14] = xor_128(v[14], v[2]);77v[15] = xor_128(v[15], v[3]);78v[12] = rot16_128(v[12]);79v[13] = rot16_128(v[13]);80v[14] = rot16_128(v[14]);81v[15] = rot16_128(v[15]);82v[8] = add_128(v[8], v[12]);83v[9] = add_128(v[9], v[13]);84v[10] = add_128(v[10], v[14]);85v[11] = add_128(v[11], v[15]);86v[4] = xor_128(v[4], v[8]);87v[5] = xor_128(v[5], v[9]);88v[6] = xor_128(v[6], v[10]);89v[7] = xor_128(v[7], v[11]);90v[4] = rot12_128(v[4]);91v[5] = rot12_128(v[5]);92v[6] = rot12_128(v[6]);93v[7] = rot12_128(v[7]);94v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);95v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);96v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);97v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);98v[0] = add_128(v[0], v[4]);99v[1] = add_128(v[1], v[5]);100v[2] = add_128(v[2], v[6]);101v[3] = add_128(v[3], v[7]);102v[12] = xor_128(v[12], v[0]);103v[13] = xor_128(v[13], v[1]);104v[14] = xor_128(v[14], v[2]);105v[15] = xor_128(v[15], v[3]);106v[12] = rot8_128(v[12]);107v[13] = rot8_128(v[13]);108v[14] = rot8_128(v[14]);109v[15] = rot8_128(v[15]);110v[8] = add_128(v[8], v[12]);111v[9] = add_128(v[9], v[13]);112v[10] = add_128(v[10], v[14]);113v[11] = add_128(v[11], v[15]);114v[4] = xor_128(v[4], v[8]);115v[5] = xor_128(v[5], v[9]);116v[6] = xor_128(v[6], v[10]);117v[7] = xor_128(v[7], v[11]);118v[4] = rot7_128(v[4]);119v[5] = rot7_128(v[5]);120v[6] = rot7_128(v[6]);121v[7] = rot7_128(v[7]);122123v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);124v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);125v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);126v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);127v[0] = add_128(v[0], v[5]);128v[1] = add_128(v[1], v[6]);129v[2] = add_128(v[2], v[7]);130v[3] = add_128(v[3], v[4]);131v[15] = xor_128(v[15], v[0]);132v[12] = xor_128(v[12], v[1]);133v[13] = xor_128(v[13], v[2]);134v[14] = xor_128(v[14], v[3]);135v[15] = rot16_128(v[15]);136v[12] = rot16_128(v[12]);137v[13] = rot16_128(v[13]);138v[14] = rot16_128(v[14]);139v[10] = add_128(v[10], v[15]);140v[11] = add_128(v[11], v[12]);141v[8] = add_128(v[8], v[13]);142v[9] = add_128(v[9], v[14]);143v[5] = xor_128(v[5], v[10]);144v[6] = xor_128(v[6], v[11]);145v[7] = xor_128(v[7], v[8]);146v[4] = xor_128(v[4], v[9]);147v[5] = rot12_128(v[5]);148v[6] = rot12_128(v[6]);149v[7] = rot12_128(v[7]);150v[4] = rot12_128(v[4]);151v[0] = add_128(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);152v[1] = add_128(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);153v[2] = add_128(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);154v[3] = add_128(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);155v[0] = add_128(v[0], v[5]);156v[1] = add_128(v[1], v[6]);157v[2] = add_128(v[2], v[7]);158v[3] = add_128(v[3], v[4]);159v[15] = xor_128(v[15], v[0]);160v[12] = xor_128(v[12], v[1]);161v[13] = xor_128(v[13], v[2]);162v[14] = xor_128(v[14], v[3]);163v[15] = rot8_128(v[15]);164v[12] = rot8_128(v[12]);165v[13] = rot8_128(v[13]);166v[14] = rot8_128(v[14]);167v[10] = add_128(v[10], v[15]);168v[11] = add_128(v[11], v[12]);169v[8] = add_128(v[8], v[13]);170v[9] = add_128(v[9], v[14]);171v[5] = xor_128(v[5], v[10]);172v[6] = xor_128(v[6], v[11]);173v[7] = xor_128(v[7], v[8]);174v[4] = xor_128(v[4], v[9]);175v[5] = rot7_128(v[5]);176v[6] = rot7_128(v[6]);177v[7] = rot7_128(v[7]);178v[4] = rot7_128(v[4]);179}180181INLINE void transpose_vecs_128(uint32x4_t vecs[4]) {182// Individually transpose the four 2x2 sub-matrices in each corner.183uint32x4x2_t rows01 = vtrnq_u32(vecs[0], vecs[1]);184uint32x4x2_t rows23 = vtrnq_u32(vecs[2], vecs[3]);185186// Swap the top-right and bottom-left 2x2s (which just got transposed).187vecs[0] =188vcombine_u32(vget_low_u32(rows01.val[0]), vget_low_u32(rows23.val[0]));189vecs[1] =190vcombine_u32(vget_low_u32(rows01.val[1]), vget_low_u32(rows23.val[1]));191vecs[2] =192vcombine_u32(vget_high_u32(rows01.val[0]), vget_high_u32(rows23.val[0]));193vecs[3] =194vcombine_u32(vget_high_u32(rows01.val[1]), vget_high_u32(rows23.val[1]));195}196197INLINE void transpose_msg_vecs4(const uint8_t *const *inputs,198size_t block_offset, uint32x4_t out[16]) {199out[0] = loadu_128(&inputs[0][block_offset + 0 * sizeof(uint32x4_t)]);200out[1] = loadu_128(&inputs[1][block_offset + 0 * sizeof(uint32x4_t)]);201out[2] = loadu_128(&inputs[2][block_offset + 0 * sizeof(uint32x4_t)]);202out[3] = loadu_128(&inputs[3][block_offset + 0 * sizeof(uint32x4_t)]);203out[4] = loadu_128(&inputs[0][block_offset + 1 * sizeof(uint32x4_t)]);204out[5] = loadu_128(&inputs[1][block_offset + 1 * sizeof(uint32x4_t)]);205out[6] = loadu_128(&inputs[2][block_offset + 1 * sizeof(uint32x4_t)]);206out[7] = loadu_128(&inputs[3][block_offset + 1 * sizeof(uint32x4_t)]);207out[8] = loadu_128(&inputs[0][block_offset + 2 * sizeof(uint32x4_t)]);208out[9] = loadu_128(&inputs[1][block_offset + 2 * sizeof(uint32x4_t)]);209out[10] = loadu_128(&inputs[2][block_offset + 2 * sizeof(uint32x4_t)]);210out[11] = loadu_128(&inputs[3][block_offset + 2 * sizeof(uint32x4_t)]);211out[12] = loadu_128(&inputs[0][block_offset + 3 * sizeof(uint32x4_t)]);212out[13] = loadu_128(&inputs[1][block_offset + 3 * sizeof(uint32x4_t)]);213out[14] = loadu_128(&inputs[2][block_offset + 3 * sizeof(uint32x4_t)]);214out[15] = loadu_128(&inputs[3][block_offset + 3 * sizeof(uint32x4_t)]);215transpose_vecs_128(&out[0]);216transpose_vecs_128(&out[4]);217transpose_vecs_128(&out[8]);218transpose_vecs_128(&out[12]);219}220221INLINE void load_counters4(uint64_t counter, bool increment_counter,222uint32x4_t *out_low, uint32x4_t *out_high) {223uint64_t mask = (increment_counter ? ~0 : 0);224*out_low = set4(225counter_low(counter + (mask & 0)), counter_low(counter + (mask & 1)),226counter_low(counter + (mask & 2)), counter_low(counter + (mask & 3)));227*out_high = set4(228counter_high(counter + (mask & 0)), counter_high(counter + (mask & 1)),229counter_high(counter + (mask & 2)), counter_high(counter + (mask & 3)));230}231232static233void blake3_hash4_neon(const uint8_t *const *inputs, size_t blocks,234const uint32_t key[8], uint64_t counter,235bool increment_counter, uint8_t flags,236uint8_t flags_start, uint8_t flags_end, uint8_t *out) {237uint32x4_t h_vecs[8] = {238set1_128(key[0]), set1_128(key[1]), set1_128(key[2]), set1_128(key[3]),239set1_128(key[4]), set1_128(key[5]), set1_128(key[6]), set1_128(key[7]),240};241uint32x4_t counter_low_vec, counter_high_vec;242load_counters4(counter, increment_counter, &counter_low_vec,243&counter_high_vec);244uint8_t block_flags = flags | flags_start;245246for (size_t block = 0; block < blocks; block++) {247if (block + 1 == blocks) {248block_flags |= flags_end;249}250uint32x4_t block_len_vec = set1_128(BLAKE3_BLOCK_LEN);251uint32x4_t block_flags_vec = set1_128(block_flags);252uint32x4_t msg_vecs[16];253transpose_msg_vecs4(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);254255uint32x4_t v[16] = {256h_vecs[0], h_vecs[1], h_vecs[2], h_vecs[3],257h_vecs[4], h_vecs[5], h_vecs[6], h_vecs[7],258set1_128(IV[0]), set1_128(IV[1]), set1_128(IV[2]), set1_128(IV[3]),259counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,260};261round_fn4(v, msg_vecs, 0);262round_fn4(v, msg_vecs, 1);263round_fn4(v, msg_vecs, 2);264round_fn4(v, msg_vecs, 3);265round_fn4(v, msg_vecs, 4);266round_fn4(v, msg_vecs, 5);267round_fn4(v, msg_vecs, 6);268h_vecs[0] = xor_128(v[0], v[8]);269h_vecs[1] = xor_128(v[1], v[9]);270h_vecs[2] = xor_128(v[2], v[10]);271h_vecs[3] = xor_128(v[3], v[11]);272h_vecs[4] = xor_128(v[4], v[12]);273h_vecs[5] = xor_128(v[5], v[13]);274h_vecs[6] = xor_128(v[6], v[14]);275h_vecs[7] = xor_128(v[7], v[15]);276277block_flags = flags;278}279280transpose_vecs_128(&h_vecs[0]);281transpose_vecs_128(&h_vecs[4]);282// The first four vecs now contain the first half of each output, and the283// second four vecs contain the second half of each output.284storeu_128(h_vecs[0], &out[0 * sizeof(uint32x4_t)]);285storeu_128(h_vecs[4], &out[1 * sizeof(uint32x4_t)]);286storeu_128(h_vecs[1], &out[2 * sizeof(uint32x4_t)]);287storeu_128(h_vecs[5], &out[3 * sizeof(uint32x4_t)]);288storeu_128(h_vecs[2], &out[4 * sizeof(uint32x4_t)]);289storeu_128(h_vecs[6], &out[5 * sizeof(uint32x4_t)]);290storeu_128(h_vecs[3], &out[6 * sizeof(uint32x4_t)]);291storeu_128(h_vecs[7], &out[7 * sizeof(uint32x4_t)]);292}293294/*295* ----------------------------------------------------------------------------296* hash_many_neon297* ----------------------------------------------------------------------------298*/299300void blake3_compress_in_place_portable(uint32_t cv[8],301const uint8_t block[BLAKE3_BLOCK_LEN],302uint8_t block_len, uint64_t counter,303uint8_t flags);304305INLINE void hash_one_neon(const uint8_t *input, size_t blocks,306const uint32_t key[8], uint64_t counter,307uint8_t flags, uint8_t flags_start, uint8_t flags_end,308uint8_t out[BLAKE3_OUT_LEN]) {309uint32_t cv[8];310memcpy(cv, key, BLAKE3_KEY_LEN);311uint8_t block_flags = flags | flags_start;312while (blocks > 0) {313if (blocks == 1) {314block_flags |= flags_end;315}316// TODO: Implement compress_neon. However note that according to317// https://github.com/BLAKE2/BLAKE2/commit/7965d3e6e1b4193438b8d3a656787587d2579227,318// compress_neon might not be any faster than compress_portable.319blake3_compress_in_place_portable(cv, input, BLAKE3_BLOCK_LEN, counter,320block_flags);321input = &input[BLAKE3_BLOCK_LEN];322blocks -= 1;323block_flags = flags;324}325memcpy(out, cv, BLAKE3_OUT_LEN);326}327328void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs,329size_t blocks, const uint32_t key[8],330uint64_t counter, bool increment_counter,331uint8_t flags, uint8_t flags_start,332uint8_t flags_end, uint8_t *out) {333while (num_inputs >= 4) {334blake3_hash4_neon(inputs, blocks, key, counter, increment_counter, flags,335flags_start, flags_end, out);336if (increment_counter) {337counter += 4;338}339inputs += 4;340num_inputs -= 4;341out = &out[4 * BLAKE3_OUT_LEN];342}343while (num_inputs > 0) {344hash_one_neon(inputs[0], blocks, key, counter, flags, flags_start,345flags_end, out);346if (increment_counter) {347counter += 1;348}349inputs += 1;350num_inputs -= 1;351out = &out[BLAKE3_OUT_LEN];352}353}354355#endif // BLAKE3_USE_NEON356357358