Debian Codegraph @

lib/arm/crc32_pmull_helpers.h

raw
1 /*
2 * arm/crc32_pmull_helpers.h - helper functions for CRC-32 folding with PMULL
3 *
4 * Copyright 2022 Eric Biggers
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 /*
29 * This file is a "template" for instantiating helper functions for CRC folding
30 * with pmull instructions. It accepts the following parameters:
31 *
32 * SUFFIX:
33 * Name suffix to append to all instantiated functions.
34 * ATTRIBUTES:
35 * Target function attributes to use.
36 * ENABLE_EOR3:
37 * Use the eor3 instruction (from the sha3 extension).
38 */
39
40 /* Create a vector with 'a' in the first 4 bytes, and the rest zeroed out. */
41 #undef u32_to_bytevec
42 static forceinline ATTRIBUTES uint8x16_t
43 ADD_SUFFIX(u32_to_bytevec)(u32 a)
44 {
45 return vreinterpretq_u8_u32(vsetq_lane_u32(a, vdupq_n_u32(0), 0));
46 }
47 #define u32_to_bytevec ADD_SUFFIX(u32_to_bytevec)
48
49 /* Load two 64-bit values into a vector. */
50 #undef load_multipliers
51 static forceinline ATTRIBUTES poly64x2_t
52 ADD_SUFFIX(load_multipliers)(const u64 p[2])
53 {
54 return vreinterpretq_p64_u64(vld1q_u64(p));
55 }
56 #define load_multipliers ADD_SUFFIX(load_multipliers)
57
58 /* Do carryless multiplication of the low halves of two vectors. */
59 #undef clmul_low
60 static forceinline ATTRIBUTES uint8x16_t
61 ADD_SUFFIX(clmul_low)(uint8x16_t a, poly64x2_t b)
62 {
63 return vreinterpretq_u8_p128(
64 compat_vmull_p64(vgetq_lane_p64(vreinterpretq_p64_u8(a), 0),
65 vgetq_lane_p64(b, 0)));
66 }
67 #define clmul_low ADD_SUFFIX(clmul_low)
68
69 /* Do carryless multiplication of the high halves of two vectors. */
70 #undef clmul_high
71 static forceinline ATTRIBUTES uint8x16_t
72 ADD_SUFFIX(clmul_high)(uint8x16_t a, poly64x2_t b)
73 {
74 #ifdef __clang__
75 /*
76 * Use inline asm to ensure that pmull2 is really used. This works
77 * around clang bug https://github.com/llvm/llvm-project/issues/52868.
78 */
79 uint8x16_t res;
80
81 __asm__("pmull2 %0.1q, %1.2d, %2.2d" : "=w" (res) : "w" (a), "w" (b));
82 return res;
83 #else
84 return vreinterpretq_u8_p128(vmull_high_p64(vreinterpretq_p64_u8(a), b));
85 #endif
86 }
87 #define clmul_high ADD_SUFFIX(clmul_high)
88
89 #undef eor3
90 static forceinline ATTRIBUTES uint8x16_t
91 ADD_SUFFIX(eor3)(uint8x16_t a, uint8x16_t b, uint8x16_t c)
92 {
93 #if ENABLE_EOR3
94 return veor3q_u8(a, b, c);
95 #else
96 return veorq_u8(veorq_u8(a, b), c);
97 #endif
98 }
99 #define eor3 ADD_SUFFIX(eor3)
100
101 #undef fold_vec
102 static forceinline ATTRIBUTES uint8x16_t
103 ADD_SUFFIX(fold_vec)(uint8x16_t src, uint8x16_t dst, poly64x2_t multipliers)
104 {
105 uint8x16_t a = clmul_low(src, multipliers);
106 uint8x16_t b = clmul_high(src, multipliers);
107
108 return eor3(a, b, dst);
109 }
110 #define fold_vec ADD_SUFFIX(fold_vec)
111
112 /*
113 * Given v containing a 16-byte polynomial, and a pointer 'p' that points to the
114 * next '1 <= len <= 15' data bytes, rearrange the concatenation of v and the
115 * data into vectors x0 and x1 that contain 'len' bytes and 16 bytes,
116 * respectively. Then fold x0 into x1 and return the result. Assumes that
117 * 'p + len - 16' is in-bounds.
118 */
119 #undef fold_partial_vec
120 static forceinline ATTRIBUTES MAYBE_UNUSED uint8x16_t
121 ADD_SUFFIX(fold_partial_vec)(uint8x16_t v, const u8 *p, size_t len,
122 poly64x2_t multipliers_1)
123 {
124 /*
125 * vqtbl1q_u8(v, shift_tab[len..len+15]) left shifts v by 16-len bytes.
126 * vqtbl1q_u8(v, shift_tab[len+16..len+31]) right shifts v by len bytes.
127 */
128 static const u8 shift_tab[48] = {
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
130 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
131 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
132 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
135 };
136 const uint8x16_t lshift = vld1q_u8(&shift_tab[len]);
137 const uint8x16_t rshift = vld1q_u8(&shift_tab[len + 16]);
138 uint8x16_t x0, x1, bsl_mask;
139
140 /* x0 = v left-shifted by '16 - len' bytes */
141 x0 = vqtbl1q_u8(v, lshift);
142
143 /* Create a vector of '16 - len' 0x00 bytes, then 'len' 0xff bytes. */
144 bsl_mask = vreinterpretq_u8_s8(
145 vshrq_n_s8(vreinterpretq_s8_u8(rshift), 7));
146
147 /*
148 * x1 = the last '16 - len' bytes from v (i.e. v right-shifted by 'len'
149 * bytes) followed by the remaining data.
150 */
151 x1 = vbslq_u8(bsl_mask /* 0 bits select from arg3, 1 bits from arg2 */,
152 vld1q_u8(p + len - 16), vqtbl1q_u8(v, rshift));
153
154 return fold_vec(x0, x1, multipliers_1);
155 }
156 #define fold_partial_vec ADD_SUFFIX(fold_partial_vec)