| 1 |
/* |
| 2 |
* arm/adler32_impl.h - ARM implementations of Adler-32 checksum algorithm |
| 3 |
* |
| 4 |
* Copyright 2016 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 |
#ifndef LIB_ARM_ADLER32_IMPL_H |
| 29 |
#define LIB_ARM_ADLER32_IMPL_H |
| 30 |
|
| 31 |
#include "cpu_features.h" |
| 32 |
|
| 33 |
/* Regular NEON implementation */ |
| 34 |
#if HAVE_NEON_INTRIN && CPU_IS_LITTLE_ENDIAN() |
| 35 |
# define adler32_arm_neon adler32_arm_neon |
| 36 |
# if HAVE_NEON_NATIVE |
| 37 |
/* |
| 38 |
* Use no attributes if none are needed, to support old versions of clang |
| 39 |
* that don't accept the simd target attribute. |
| 40 |
*/ |
| 41 |
# define ATTRIBUTES |
| 42 |
# elif defined(ARCH_ARM32) |
| 43 |
# define ATTRIBUTES _target_attribute("fpu=neon") |
| 44 |
# elif defined(__clang__) |
| 45 |
# define ATTRIBUTES _target_attribute("simd") |
| 46 |
# else |
| 47 |
# define ATTRIBUTES _target_attribute("+simd") |
| 48 |
# endif |
| 49 |
static ATTRIBUTES MAYBE_UNUSED u32 |
| 50 |
adler32_arm_neon(u32 adler, const u8 *p, size_t len) |
| 51 |
{ |
| 52 |
static const u16 _aligned_attribute(16) mults[64] = { |
| 53 |
64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, |
| 54 |
48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, |
| 55 |
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, |
| 56 |
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, |
| 57 |
}; |
| 58 |
const uint16x8_t mults_a = vld1q_u16(&mults[0]); |
| 59 |
const uint16x8_t mults_b = vld1q_u16(&mults[8]); |
| 60 |
const uint16x8_t mults_c = vld1q_u16(&mults[16]); |
| 61 |
const uint16x8_t mults_d = vld1q_u16(&mults[24]); |
| 62 |
const uint16x8_t mults_e = vld1q_u16(&mults[32]); |
| 63 |
const uint16x8_t mults_f = vld1q_u16(&mults[40]); |
| 64 |
const uint16x8_t mults_g = vld1q_u16(&mults[48]); |
| 65 |
const uint16x8_t mults_h = vld1q_u16(&mults[56]); |
| 66 |
u32 s1 = adler & 0xFFFF; |
| 67 |
u32 s2 = adler >> 16; |
| 68 |
|
| 69 |
/* |
| 70 |
* If the length is large and the pointer is misaligned, align it. |
| 71 |
* For smaller lengths, just take the misaligned load penalty. |
| 72 |
*/ |
| 73 |
if (unlikely(len > 32768 && ((uintptr_t)p & 15))) { |
| 74 |
do { |
| 75 |
s1 += *p++; |
| 76 |
s2 += s1; |
| 77 |
len--; |
| 78 |
} while ((uintptr_t)p & 15); |
| 79 |
s1 %= DIVISOR; |
| 80 |
s2 %= DIVISOR; |
| 81 |
} |
| 82 |
|
| 83 |
while (len) { |
| 84 |
/* |
| 85 |
* Calculate the length of the next data chunk such that s1 and |
| 86 |
* s2 are guaranteed to not exceed UINT32_MAX. |
| 87 |
*/ |
| 88 |
size_t n = MIN(len, MAX_CHUNK_LEN & ~63); |
| 89 |
|
| 90 |
len -= n; |
| 91 |
|
| 92 |
if (n >= 64) { |
| 93 |
uint32x4_t v_s1 = vdupq_n_u32(0); |
| 94 |
uint32x4_t v_s2 = vdupq_n_u32(0); |
| 95 |
/* |
| 96 |
* v_byte_sums_* contain the sum of the bytes at index i |
| 97 |
* across all 64-byte segments, for each index 0..63. |
| 98 |
*/ |
| 99 |
uint16x8_t v_byte_sums_a = vdupq_n_u16(0); |
| 100 |
uint16x8_t v_byte_sums_b = vdupq_n_u16(0); |
| 101 |
uint16x8_t v_byte_sums_c = vdupq_n_u16(0); |
| 102 |
uint16x8_t v_byte_sums_d = vdupq_n_u16(0); |
| 103 |
uint16x8_t v_byte_sums_e = vdupq_n_u16(0); |
| 104 |
uint16x8_t v_byte_sums_f = vdupq_n_u16(0); |
| 105 |
uint16x8_t v_byte_sums_g = vdupq_n_u16(0); |
| 106 |
uint16x8_t v_byte_sums_h = vdupq_n_u16(0); |
| 107 |
|
| 108 |
s2 += s1 * (n & ~63); |
| 109 |
|
| 110 |
do { |
| 111 |
/* Load the next 64 data bytes. */ |
| 112 |
const uint8x16_t data_a = vld1q_u8(p + 0); |
| 113 |
const uint8x16_t data_b = vld1q_u8(p + 16); |
| 114 |
const uint8x16_t data_c = vld1q_u8(p + 32); |
| 115 |
const uint8x16_t data_d = vld1q_u8(p + 48); |
| 116 |
uint16x8_t tmp; |
| 117 |
|
| 118 |
/* |
| 119 |
* Accumulate the previous s1 counters into the |
| 120 |
* s2 counters. The needed multiplication by 64 |
| 121 |
* is delayed to later. |
| 122 |
*/ |
| 123 |
v_s2 = vaddq_u32(v_s2, v_s1); |
| 124 |
|
| 125 |
/* |
| 126 |
* Add the 64 data bytes to their v_byte_sums |
| 127 |
* counters, while also accumulating the sums of |
| 128 |
* each adjacent set of 4 bytes into v_s1. |
| 129 |
*/ |
| 130 |
tmp = vpaddlq_u8(data_a); |
| 131 |
v_byte_sums_a = vaddw_u8(v_byte_sums_a, |
| 132 |
vget_low_u8(data_a)); |
| 133 |
v_byte_sums_b = vaddw_u8(v_byte_sums_b, |
| 134 |
vget_high_u8(data_a)); |
| 135 |
tmp = vpadalq_u8(tmp, data_b); |
| 136 |
v_byte_sums_c = vaddw_u8(v_byte_sums_c, |
| 137 |
vget_low_u8(data_b)); |
| 138 |
v_byte_sums_d = vaddw_u8(v_byte_sums_d, |
| 139 |
vget_high_u8(data_b)); |
| 140 |
tmp = vpadalq_u8(tmp, data_c); |
| 141 |
v_byte_sums_e = vaddw_u8(v_byte_sums_e, |
| 142 |
vget_low_u8(data_c)); |
| 143 |
v_byte_sums_f = vaddw_u8(v_byte_sums_f, |
| 144 |
vget_high_u8(data_c)); |
| 145 |
tmp = vpadalq_u8(tmp, data_d); |
| 146 |
v_byte_sums_g = vaddw_u8(v_byte_sums_g, |
| 147 |
vget_low_u8(data_d)); |
| 148 |
v_byte_sums_h = vaddw_u8(v_byte_sums_h, |
| 149 |
vget_high_u8(data_d)); |
| 150 |
v_s1 = vpadalq_u16(v_s1, tmp); |
| 151 |
|
| 152 |
p += 64; |
| 153 |
n -= 64; |
| 154 |
} while (n >= 64); |
| 155 |
|
| 156 |
/* s2 = 64*s2 + (64*bytesum0 + 63*bytesum1 + ... + 1*bytesum63) */ |
| 157 |
#ifdef ARCH_ARM32 |
| 158 |
# define umlal2(a, b, c) vmlal_u16((a), vget_high_u16(b), vget_high_u16(c)) |
| 159 |
#else |
| 160 |
# define umlal2 vmlal_high_u16 |
| 161 |
#endif |
| 162 |
v_s2 = vqshlq_n_u32(v_s2, 6); |
| 163 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_a), |
| 164 |
vget_low_u16(mults_a)); |
| 165 |
v_s2 = umlal2(v_s2, v_byte_sums_a, mults_a); |
| 166 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_b), |
| 167 |
vget_low_u16(mults_b)); |
| 168 |
v_s2 = umlal2(v_s2, v_byte_sums_b, mults_b); |
| 169 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_c), |
| 170 |
vget_low_u16(mults_c)); |
| 171 |
v_s2 = umlal2(v_s2, v_byte_sums_c, mults_c); |
| 172 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_d), |
| 173 |
vget_low_u16(mults_d)); |
| 174 |
v_s2 = umlal2(v_s2, v_byte_sums_d, mults_d); |
| 175 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_e), |
| 176 |
vget_low_u16(mults_e)); |
| 177 |
v_s2 = umlal2(v_s2, v_byte_sums_e, mults_e); |
| 178 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_f), |
| 179 |
vget_low_u16(mults_f)); |
| 180 |
v_s2 = umlal2(v_s2, v_byte_sums_f, mults_f); |
| 181 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_g), |
| 182 |
vget_low_u16(mults_g)); |
| 183 |
v_s2 = umlal2(v_s2, v_byte_sums_g, mults_g); |
| 184 |
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_h), |
| 185 |
vget_low_u16(mults_h)); |
| 186 |
v_s2 = umlal2(v_s2, v_byte_sums_h, mults_h); |
| 187 |
#undef umlal2 |
| 188 |
|
| 189 |
/* Horizontal sum to finish up */ |
| 190 |
#ifdef ARCH_ARM32 |
| 191 |
s1 += vgetq_lane_u32(v_s1, 0) + vgetq_lane_u32(v_s1, 1) + |
| 192 |
vgetq_lane_u32(v_s1, 2) + vgetq_lane_u32(v_s1, 3); |
| 193 |
s2 += vgetq_lane_u32(v_s2, 0) + vgetq_lane_u32(v_s2, 1) + |
| 194 |
vgetq_lane_u32(v_s2, 2) + vgetq_lane_u32(v_s2, 3); |
| 195 |
#else |
| 196 |
s1 += vaddvq_u32(v_s1); |
| 197 |
s2 += vaddvq_u32(v_s2); |
| 198 |
#endif |
| 199 |
} |
| 200 |
/* |
| 201 |
* Process the last 0 <= n < 64 bytes of the chunk using |
| 202 |
* scalar instructions and reduce s1 and s2 mod DIVISOR. |
| 203 |
*/ |
| 204 |
ADLER32_CHUNK(s1, s2, p, n); |
| 205 |
} |
| 206 |
return (s2 << 16) | s1; |
| 207 |
} |
| 208 |
#undef ATTRIBUTES |
| 209 |
#endif /* Regular NEON implementation */ |
| 210 |
|
| 211 |
/* NEON+dotprod implementation */ |
| 212 |
#if HAVE_DOTPROD_INTRIN && CPU_IS_LITTLE_ENDIAN() && \ |
| 213 |
!defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_DOTPROD) |
| 214 |
# define adler32_arm_neon_dotprod adler32_arm_neon_dotprod |
| 215 |
# ifdef __clang__ |
| 216 |
# define ATTRIBUTES _target_attribute("dotprod") |
| 217 |
/* |
| 218 |
* Both gcc and binutils originally considered dotprod to depend on |
| 219 |
* arch=armv8.2-a or later. This was fixed in gcc 13.2 by commit |
| 220 |
* 9aac37ab8a7b ("aarch64: Remove architecture dependencies from intrinsics") |
| 221 |
* and in binutils 2.41 by commit 205e4380c800 ("aarch64: Remove version |
| 222 |
* dependencies from features"). Unfortunately, always using arch=armv8.2-a |
| 223 |
* causes build errors with some compiler options because it may reduce the |
| 224 |
* arch rather than increase it. Therefore we try to omit the arch whenever |
| 225 |
* possible. If gcc is 14 or later, then both gcc and binutils are probably |
| 226 |
* fixed, so we omit the arch. We also omit the arch if a feature that |
| 227 |
* depends on armv8.2-a or later (in gcc 13.1 and earlier) is present. |
| 228 |
*/ |
| 229 |
# elif GCC_PREREQ(14, 0) || defined(__ARM_FEATURE_JCVT) \ |
| 230 |
|| defined(__ARM_FEATURE_DOTPROD) |
| 231 |
# define ATTRIBUTES _target_attribute("+dotprod") |
| 232 |
# else |
| 233 |
# define ATTRIBUTES _target_attribute("arch=armv8.2-a+dotprod") |
| 234 |
# endif |
| 235 |
static ATTRIBUTES u32 |
| 236 |
adler32_arm_neon_dotprod(u32 adler, const u8 *p, size_t len) |
| 237 |
{ |
| 238 |
static const u8 _aligned_attribute(16) mults[64] = { |
| 239 |
64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, |
| 240 |
48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, |
| 241 |
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, |
| 242 |
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, |
| 243 |
}; |
| 244 |
const uint8x16_t mults_a = vld1q_u8(&mults[0]); |
| 245 |
const uint8x16_t mults_b = vld1q_u8(&mults[16]); |
| 246 |
const uint8x16_t mults_c = vld1q_u8(&mults[32]); |
| 247 |
const uint8x16_t mults_d = vld1q_u8(&mults[48]); |
| 248 |
const uint8x16_t ones = vdupq_n_u8(1); |
| 249 |
u32 s1 = adler & 0xFFFF; |
| 250 |
u32 s2 = adler >> 16; |
| 251 |
|
| 252 |
/* |
| 253 |
* If the length is large and the pointer is misaligned, align it. |
| 254 |
* For smaller lengths, just take the misaligned load penalty. |
| 255 |
*/ |
| 256 |
if (unlikely(len > 32768 && ((uintptr_t)p & 15))) { |
| 257 |
do { |
| 258 |
s1 += *p++; |
| 259 |
s2 += s1; |
| 260 |
len--; |
| 261 |
} while ((uintptr_t)p & 15); |
| 262 |
s1 %= DIVISOR; |
| 263 |
s2 %= DIVISOR; |
| 264 |
} |
| 265 |
|
| 266 |
while (len) { |
| 267 |
/* |
| 268 |
* Calculate the length of the next data chunk such that s1 and |
| 269 |
* s2 are guaranteed to not exceed UINT32_MAX. |
| 270 |
*/ |
| 271 |
size_t n = MIN(len, MAX_CHUNK_LEN & ~63); |
| 272 |
|
| 273 |
len -= n; |
| 274 |
|
| 275 |
if (n >= 64) { |
| 276 |
uint32x4_t v_s1_a = vdupq_n_u32(0); |
| 277 |
uint32x4_t v_s1_b = vdupq_n_u32(0); |
| 278 |
uint32x4_t v_s1_c = vdupq_n_u32(0); |
| 279 |
uint32x4_t v_s1_d = vdupq_n_u32(0); |
| 280 |
uint32x4_t v_s2_a = vdupq_n_u32(0); |
| 281 |
uint32x4_t v_s2_b = vdupq_n_u32(0); |
| 282 |
uint32x4_t v_s2_c = vdupq_n_u32(0); |
| 283 |
uint32x4_t v_s2_d = vdupq_n_u32(0); |
| 284 |
uint32x4_t v_s1_sums_a = vdupq_n_u32(0); |
| 285 |
uint32x4_t v_s1_sums_b = vdupq_n_u32(0); |
| 286 |
uint32x4_t v_s1_sums_c = vdupq_n_u32(0); |
| 287 |
uint32x4_t v_s1_sums_d = vdupq_n_u32(0); |
| 288 |
uint32x4_t v_s1; |
| 289 |
uint32x4_t v_s2; |
| 290 |
uint32x4_t v_s1_sums; |
| 291 |
|
| 292 |
s2 += s1 * (n & ~63); |
| 293 |
|
| 294 |
do { |
| 295 |
uint8x16_t data_a = vld1q_u8(p + 0); |
| 296 |
uint8x16_t data_b = vld1q_u8(p + 16); |
| 297 |
uint8x16_t data_c = vld1q_u8(p + 32); |
| 298 |
uint8x16_t data_d = vld1q_u8(p + 48); |
| 299 |
|
| 300 |
v_s1_sums_a = vaddq_u32(v_s1_sums_a, v_s1_a); |
| 301 |
v_s1_a = vdotq_u32(v_s1_a, data_a, ones); |
| 302 |
v_s2_a = vdotq_u32(v_s2_a, data_a, mults_a); |
| 303 |
|
| 304 |
v_s1_sums_b = vaddq_u32(v_s1_sums_b, v_s1_b); |
| 305 |
v_s1_b = vdotq_u32(v_s1_b, data_b, ones); |
| 306 |
v_s2_b = vdotq_u32(v_s2_b, data_b, mults_b); |
| 307 |
|
| 308 |
v_s1_sums_c = vaddq_u32(v_s1_sums_c, v_s1_c); |
| 309 |
v_s1_c = vdotq_u32(v_s1_c, data_c, ones); |
| 310 |
v_s2_c = vdotq_u32(v_s2_c, data_c, mults_c); |
| 311 |
|
| 312 |
v_s1_sums_d = vaddq_u32(v_s1_sums_d, v_s1_d); |
| 313 |
v_s1_d = vdotq_u32(v_s1_d, data_d, ones); |
| 314 |
v_s2_d = vdotq_u32(v_s2_d, data_d, mults_d); |
| 315 |
|
| 316 |
p += 64; |
| 317 |
n -= 64; |
| 318 |
} while (n >= 64); |
| 319 |
|
| 320 |
v_s1 = vaddq_u32(vaddq_u32(v_s1_a, v_s1_b), |
| 321 |
vaddq_u32(v_s1_c, v_s1_d)); |
| 322 |
v_s2 = vaddq_u32(vaddq_u32(v_s2_a, v_s2_b), |
| 323 |
vaddq_u32(v_s2_c, v_s2_d)); |
| 324 |
v_s1_sums = vaddq_u32(vaddq_u32(v_s1_sums_a, |
| 325 |
v_s1_sums_b), |
| 326 |
vaddq_u32(v_s1_sums_c, |
| 327 |
v_s1_sums_d)); |
| 328 |
v_s2 = vaddq_u32(v_s2, vqshlq_n_u32(v_s1_sums, 6)); |
| 329 |
|
| 330 |
s1 += vaddvq_u32(v_s1); |
| 331 |
s2 += vaddvq_u32(v_s2); |
| 332 |
} |
| 333 |
/* |
| 334 |
* Process the last 0 <= n < 64 bytes of the chunk using |
| 335 |
* scalar instructions and reduce s1 and s2 mod DIVISOR. |
| 336 |
*/ |
| 337 |
ADLER32_CHUNK(s1, s2, p, n); |
| 338 |
} |
| 339 |
return (s2 << 16) | s1; |
| 340 |
} |
| 341 |
#undef ATTRIBUTES |
| 342 |
#endif /* NEON+dotprod implementation */ |
| 343 |
|
| 344 |
#if defined(adler32_arm_neon_dotprod) && defined(__ARM_FEATURE_DOTPROD) |
| 345 |
#define DEFAULT_IMPL adler32_arm_neon_dotprod |
| 346 |
#else |
| 347 |
static inline adler32_func_t |
| 348 |
arch_select_adler32_func(void) |
| 349 |
{ |
| 350 |
const u32 features MAYBE_UNUSED = get_arm_cpu_features(); |
| 351 |
|
| 352 |
#ifdef adler32_arm_neon_dotprod |
| 353 |
if (HAVE_NEON(features) && HAVE_DOTPROD(features)) |
| 354 |
return adler32_arm_neon_dotprod; |
| 355 |
#endif |
| 356 |
#ifdef adler32_arm_neon |
| 357 |
if (HAVE_NEON(features)) |
| 358 |
return adler32_arm_neon; |
| 359 |
#endif |
| 360 |
return NULL; |
| 361 |
} |
| 362 |
#define arch_select_adler32_func arch_select_adler32_func |
| 363 |
#endif |
| 364 |
|
| 365 |
#endif /* LIB_ARM_ADLER32_IMPL_H */ |