| 1 |
/* |
| 2 |
* arm/crc32_pmull_wide.h - gzip CRC-32 with PMULL (extra-wide version) |
| 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 PMULL-based crc32_arm functions. |
| 30 |
* The "parameters" are: |
| 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 |
* This is the extra-wide version; it uses an unusually large stride length of |
| 40 |
* 12, and it assumes that crc32 instructions are available too. It's intended |
| 41 |
* for powerful CPUs that support both pmull and crc32 instructions, but where |
| 42 |
* throughput of pmull and xor (given enough instructions issued in parallel) is |
| 43 |
* significantly higher than that of crc32, thus making the crc32 instructions |
| 44 |
* (counterintuitively) not actually the fastest way to compute the CRC-32. The |
| 45 |
* Apple M1 processor is an example of such a CPU. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include "crc32_pmull_helpers.h" |
| 49 |
|
| 50 |
static ATTRIBUTES u32 |
| 51 |
ADD_SUFFIX(crc32_arm)(u32 crc, const u8 *p, size_t len) |
| 52 |
{ |
| 53 |
uint8x16_t v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11; |
| 54 |
|
| 55 |
if (len < 3 * 192) { |
| 56 |
static const u64 _aligned_attribute(16) mults[3][2] = { |
| 57 |
{ CRC32_X543_MODG, CRC32_X479_MODG }, /* 4 vecs */ |
| 58 |
{ CRC32_X287_MODG, CRC32_X223_MODG }, /* 2 vecs */ |
| 59 |
{ CRC32_X159_MODG, CRC32_X95_MODG }, /* 1 vecs */ |
| 60 |
}; |
| 61 |
poly64x2_t multipliers_4, multipliers_2, multipliers_1; |
| 62 |
|
| 63 |
if (len < 64) |
| 64 |
goto tail; |
| 65 |
multipliers_4 = load_multipliers(mults[0]); |
| 66 |
multipliers_2 = load_multipliers(mults[1]); |
| 67 |
multipliers_1 = load_multipliers(mults[2]); |
| 68 |
/* |
| 69 |
* Short length; don't bother aligning the pointer, and fold |
| 70 |
* 64 bytes (4 vectors) at a time, at most. |
| 71 |
*/ |
| 72 |
v0 = veorq_u8(vld1q_u8(p + 0), u32_to_bytevec(crc)); |
| 73 |
v1 = vld1q_u8(p + 16); |
| 74 |
v2 = vld1q_u8(p + 32); |
| 75 |
v3 = vld1q_u8(p + 48); |
| 76 |
p += 64; |
| 77 |
len -= 64; |
| 78 |
while (len >= 64) { |
| 79 |
v0 = fold_vec(v0, vld1q_u8(p + 0), multipliers_4); |
| 80 |
v1 = fold_vec(v1, vld1q_u8(p + 16), multipliers_4); |
| 81 |
v2 = fold_vec(v2, vld1q_u8(p + 32), multipliers_4); |
| 82 |
v3 = fold_vec(v3, vld1q_u8(p + 48), multipliers_4); |
| 83 |
p += 64; |
| 84 |
len -= 64; |
| 85 |
} |
| 86 |
v0 = fold_vec(v0, v2, multipliers_2); |
| 87 |
v1 = fold_vec(v1, v3, multipliers_2); |
| 88 |
if (len >= 32) { |
| 89 |
v0 = fold_vec(v0, vld1q_u8(p + 0), multipliers_2); |
| 90 |
v1 = fold_vec(v1, vld1q_u8(p + 16), multipliers_2); |
| 91 |
p += 32; |
| 92 |
len -= 32; |
| 93 |
} |
| 94 |
v0 = fold_vec(v0, v1, multipliers_1); |
| 95 |
} else { |
| 96 |
static const u64 _aligned_attribute(16) mults[4][2] = { |
| 97 |
{ CRC32_X1567_MODG, CRC32_X1503_MODG }, /* 12 vecs */ |
| 98 |
{ CRC32_X799_MODG, CRC32_X735_MODG }, /* 6 vecs */ |
| 99 |
{ CRC32_X415_MODG, CRC32_X351_MODG }, /* 3 vecs */ |
| 100 |
{ CRC32_X159_MODG, CRC32_X95_MODG }, /* 1 vecs */ |
| 101 |
}; |
| 102 |
const poly64x2_t multipliers_12 = load_multipliers(mults[0]); |
| 103 |
const poly64x2_t multipliers_6 = load_multipliers(mults[1]); |
| 104 |
const poly64x2_t multipliers_3 = load_multipliers(mults[2]); |
| 105 |
const poly64x2_t multipliers_1 = load_multipliers(mults[3]); |
| 106 |
const size_t align = -(uintptr_t)p & 15; |
| 107 |
const uint8x16_t *vp; |
| 108 |
|
| 109 |
/* Align p to the next 16-byte boundary. */ |
| 110 |
if (align) { |
| 111 |
if (align & 1) |
| 112 |
crc = __crc32b(crc, *p++); |
| 113 |
if (align & 2) { |
| 114 |
crc = __crc32h(crc, le16_bswap(*(u16 *)p)); |
| 115 |
p += 2; |
| 116 |
} |
| 117 |
if (align & 4) { |
| 118 |
crc = __crc32w(crc, le32_bswap(*(u32 *)p)); |
| 119 |
p += 4; |
| 120 |
} |
| 121 |
if (align & 8) { |
| 122 |
crc = __crc32d(crc, le64_bswap(*(u64 *)p)); |
| 123 |
p += 8; |
| 124 |
} |
| 125 |
len -= align; |
| 126 |
} |
| 127 |
vp = (const uint8x16_t *)p; |
| 128 |
v0 = veorq_u8(*vp++, u32_to_bytevec(crc)); |
| 129 |
v1 = *vp++; |
| 130 |
v2 = *vp++; |
| 131 |
v3 = *vp++; |
| 132 |
v4 = *vp++; |
| 133 |
v5 = *vp++; |
| 134 |
v6 = *vp++; |
| 135 |
v7 = *vp++; |
| 136 |
v8 = *vp++; |
| 137 |
v9 = *vp++; |
| 138 |
v10 = *vp++; |
| 139 |
v11 = *vp++; |
| 140 |
len -= 192; |
| 141 |
/* Fold 192 bytes (12 vectors) at a time. */ |
| 142 |
do { |
| 143 |
v0 = fold_vec(v0, *vp++, multipliers_12); |
| 144 |
v1 = fold_vec(v1, *vp++, multipliers_12); |
| 145 |
v2 = fold_vec(v2, *vp++, multipliers_12); |
| 146 |
v3 = fold_vec(v3, *vp++, multipliers_12); |
| 147 |
v4 = fold_vec(v4, *vp++, multipliers_12); |
| 148 |
v5 = fold_vec(v5, *vp++, multipliers_12); |
| 149 |
v6 = fold_vec(v6, *vp++, multipliers_12); |
| 150 |
v7 = fold_vec(v7, *vp++, multipliers_12); |
| 151 |
v8 = fold_vec(v8, *vp++, multipliers_12); |
| 152 |
v9 = fold_vec(v9, *vp++, multipliers_12); |
| 153 |
v10 = fold_vec(v10, *vp++, multipliers_12); |
| 154 |
v11 = fold_vec(v11, *vp++, multipliers_12); |
| 155 |
len -= 192; |
| 156 |
} while (len >= 192); |
| 157 |
|
| 158 |
/* |
| 159 |
* Fewer than 192 bytes left. Fold v0-v11 down to just v0, |
| 160 |
* while processing up to 144 more bytes. |
| 161 |
*/ |
| 162 |
v0 = fold_vec(v0, v6, multipliers_6); |
| 163 |
v1 = fold_vec(v1, v7, multipliers_6); |
| 164 |
v2 = fold_vec(v2, v8, multipliers_6); |
| 165 |
v3 = fold_vec(v3, v9, multipliers_6); |
| 166 |
v4 = fold_vec(v4, v10, multipliers_6); |
| 167 |
v5 = fold_vec(v5, v11, multipliers_6); |
| 168 |
if (len >= 96) { |
| 169 |
v0 = fold_vec(v0, *vp++, multipliers_6); |
| 170 |
v1 = fold_vec(v1, *vp++, multipliers_6); |
| 171 |
v2 = fold_vec(v2, *vp++, multipliers_6); |
| 172 |
v3 = fold_vec(v3, *vp++, multipliers_6); |
| 173 |
v4 = fold_vec(v4, *vp++, multipliers_6); |
| 174 |
v5 = fold_vec(v5, *vp++, multipliers_6); |
| 175 |
len -= 96; |
| 176 |
} |
| 177 |
v0 = fold_vec(v0, v3, multipliers_3); |
| 178 |
v1 = fold_vec(v1, v4, multipliers_3); |
| 179 |
v2 = fold_vec(v2, v5, multipliers_3); |
| 180 |
if (len >= 48) { |
| 181 |
v0 = fold_vec(v0, *vp++, multipliers_3); |
| 182 |
v1 = fold_vec(v1, *vp++, multipliers_3); |
| 183 |
v2 = fold_vec(v2, *vp++, multipliers_3); |
| 184 |
len -= 48; |
| 185 |
} |
| 186 |
v0 = fold_vec(v0, v1, multipliers_1); |
| 187 |
v0 = fold_vec(v0, v2, multipliers_1); |
| 188 |
p = (const u8 *)vp; |
| 189 |
} |
| 190 |
/* Reduce 128 to 32 bits using crc32 instructions. */ |
| 191 |
crc = __crc32d(0, vgetq_lane_u64(vreinterpretq_u64_u8(v0), 0)); |
| 192 |
crc = __crc32d(crc, vgetq_lane_u64(vreinterpretq_u64_u8(v0), 1)); |
| 193 |
tail: |
| 194 |
/* Finish up the remainder using crc32 instructions. */ |
| 195 |
if (len & 32) { |
| 196 |
crc = __crc32d(crc, get_unaligned_le64(p + 0)); |
| 197 |
crc = __crc32d(crc, get_unaligned_le64(p + 8)); |
| 198 |
crc = __crc32d(crc, get_unaligned_le64(p + 16)); |
| 199 |
crc = __crc32d(crc, get_unaligned_le64(p + 24)); |
| 200 |
p += 32; |
| 201 |
} |
| 202 |
if (len & 16) { |
| 203 |
crc = __crc32d(crc, get_unaligned_le64(p + 0)); |
| 204 |
crc = __crc32d(crc, get_unaligned_le64(p + 8)); |
| 205 |
p += 16; |
| 206 |
} |
| 207 |
if (len & 8) { |
| 208 |
crc = __crc32d(crc, get_unaligned_le64(p)); |
| 209 |
p += 8; |
| 210 |
} |
| 211 |
if (len & 4) { |
| 212 |
crc = __crc32w(crc, get_unaligned_le32(p)); |
| 213 |
p += 4; |
| 214 |
} |
| 215 |
if (len & 2) { |
| 216 |
crc = __crc32h(crc, get_unaligned_le16(p)); |
| 217 |
p += 2; |
| 218 |
} |
| 219 |
if (len & 1) |
| 220 |
crc = __crc32b(crc, *p); |
| 221 |
return crc; |
| 222 |
} |
| 223 |
|
| 224 |
#undef SUFFIX |
| 225 |
#undef ATTRIBUTES |
| 226 |
#undef ENABLE_EOR3 |