libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
crc64_vmull.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM)
20
21#include <arm_neon.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <string.h>
25
26#if defined(_MSC_VER)
27#define AARU_ALIGN16 __declspec(align(16))
28#else
29#define AARU_ALIGN16 __attribute__((aligned(16)))
30#endif
31
32#include <aaruformat.h>
33
34#include "arm_vmull.h"
35#include "log.h"
36
37static const uint8_t shuffleMasks[] = {
38 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
39 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81, 0x80,
40};
41
42TARGET_WITH_SIMD FORCE_INLINE void shiftRight128(uint64x2_t in, size_t n, uint64x2_t *outLeft, uint64x2_t *outRight)
43{
44 const uint64x2_t maskA =
45 vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)(const uint64x2_t *)(shuffleMasks + (16 - n))));
46 uint64x2_t b = vreinterpretq_u64_u8(vceqq_u8(vreinterpretq_u8_u64(vreinterpretq_u64_u32(vdupq_n_u32(0))),
47 vreinterpretq_u8_u64(vreinterpretq_u64_u32(vdupq_n_u32(0)))));
48 const uint64x2_t maskB = vreinterpretq_u64_u32(veorq_u32(vreinterpretq_u32_u64(maskA), vreinterpretq_u32_u64(b)));
49
50 *outLeft = mm_shuffle_epi8(in, maskB);
51 *outRight = mm_shuffle_epi8(in, maskA);
52}
53
54TARGET_WITH_SIMD FORCE_INLINE uint64x2_t fold(uint64x2_t in, uint64x2_t foldConstants)
55{
56 return veorq_u64(sse2neon_vmull_p64(vget_low_u64(in), vget_low_u64(foldConstants)),
57 sse2neon_vmull_p64(vget_high_u64(in), vget_high_u64(foldConstants)));
58}
59
68AARU_EXPORT TARGET_WITH_SIMD uint64_t AARU_CALL aaruf_crc64_vmull(uint64_t previous_crc, const uint8_t *data, long len)
69{
70 TRACE("Entering aaruf_crc64_vmull(%llu, %p, %ld)", previous_crc, data, len);
71
72 const uint64_t k1 = 0xe05dd497ca393ae4; // bitReflect(expMod65(128 + 64, poly, 1)) << 1;
73 const uint64_t k2 = 0xdabe95afc7875f40; // bitReflect(expMod65(128, poly, 1)) << 1;
74 const uint64_t mu = 0x9c3e466c172963d5; // (bitReflect(div129by65(poly)) << 1) | 1;
75 const uint64_t p = 0x92d8af2baf0e1e85; // (bitReflect(poly) << 1) | 1;
76
77 const uint64x2_t foldConstants1 = vcombine_u64(vcreate_u64(k1), vcreate_u64(k2));
78 const uint64x2_t foldConstants2 = vcombine_u64(vcreate_u64(mu), vcreate_u64(p));
79
80 const uint8_t *end = data + len;
81
82 // Align pointers
83 const uint64x2_t *alignedData = (const uint64x2_t *)((uintptr_t)data & ~(uintptr_t)15);
84 const uint64x2_t *alignedEnd = (const uint64x2_t *)(((uintptr_t)end + 15) & ~(uintptr_t)15);
85
86 const size_t leadInSize = data - (const uint8_t *)alignedData;
87 const size_t leadOutSize = (const uint8_t *)alignedEnd - end;
88
89 const size_t alignedLength = alignedEnd - alignedData;
90
91 const uint64x2_t leadInMask =
92 vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)(const uint64x2_t *)(shuffleMasks + (16 - leadInSize))));
93 uint64x2_t a = vreinterpretq_u64_u32(vdupq_n_u32(0));
94 uint64x2_t b = vreinterpretq_u64_u32(
95 vld1q_u32((const uint32_t *)alignedData)); // Use a signed shift right to create a mask with the sign bit
96 const uint64x2_t data0 =
97 vreinterpretq_u64_u8(vbslq_u8(vreinterpretq_u8_s8(vshrq_n_s8(vreinterpretq_s8_u64(leadInMask), 7)),
98 vreinterpretq_u8_u64(b), vreinterpretq_u8_u64(a)));
99
100 const uint64x2_t initialCrc = vsetq_lane_u64(~previous_crc, vdupq_n_u64(0), 0);
101
102 uint64x2_t R;
103 if(alignedLength == 1)
104 {
105 // Single data block, initial CRC possibly bleeds into zero padding
106 uint64x2_t crc0, crc1;
107 shiftRight128(initialCrc, 16 - len, &crc0, &crc1);
108
109 uint64x2_t A, B;
110 shiftRight128(data0, leadOutSize, &A, &B);
111
112 const uint64x2_t P = veorq_u64(A, crc0);
113 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)),
114 veorq_u64(mm_srli_si128(P, 8), mm_slli_si128(crc1, 8)));
115 }
116 else if(alignedLength == 2)
117 {
118 const uint64x2_t data1 = vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)(alignedData + 1)));
119
120 if(len < 8)
121 {
122 // Initial CRC bleeds into the zero padding
123 uint64x2_t crc0, crc1;
124 shiftRight128(initialCrc, 16 - len, &crc0, &crc1);
125
126 uint64x2_t A, B, C, D;
127 shiftRight128(data0, leadOutSize, &A, &B);
128 shiftRight128(data1, leadOutSize, &C, &D);
129
130 const uint64x2_t P = veorq_u64(veorq_u64(B, C), crc0);
131 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)),
132 veorq_u64(mm_srli_si128(P, 8), mm_slli_si128(crc1, 8)));
133 }
134 else
135 {
136 // We can fit the initial CRC into the data without bleeding into the zero padding
137 uint64x2_t crc0, crc1;
138 shiftRight128(initialCrc, leadInSize, &crc0, &crc1);
139
140 uint64x2_t A, B, C, D;
141 shiftRight128(veorq_u64(data0, crc0), leadOutSize, &A, &B);
142 shiftRight128(veorq_u64(data1, crc1), leadOutSize, &C, &D);
143
144 const uint64x2_t P = veorq_u64(fold(A, foldConstants1), veorq_u64(B, C));
145 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)), mm_srli_si128(P, 8));
146 }
147 }
148 else
149 {
150 alignedData++;
151 len -= 16 - leadInSize;
152
153 // Initial CRC can simply be added to data
154 uint64x2_t crc0, crc1;
155 shiftRight128(initialCrc, leadInSize, &crc0, &crc1);
156
157 uint64x2_t accumulator = veorq_u64(fold(veorq_u64(crc0, data0), foldConstants1), crc1);
158
159 while(len >= 32)
160 {
161 accumulator = fold(veorq_u64(vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)alignedData)), accumulator),
162 foldConstants1);
163
164 len -= 16;
165 alignedData++;
166 }
167
168 uint64x2_t P;
169 if(len == 16)
170 P = veorq_u64(accumulator, vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)alignedData)));
171 else
172 {
173 // When len is between 16 and 32, we need both blocks but must be careful not to read past buffer end
174 const uint64x2_t end0 =
175 veorq_u64(accumulator, vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)alignedData)));
176
177 // For the second block, always use safe copy to avoid buffer overflow
178 // The algorithm expects to read up to alignedEnd, but ASan prevents over-reading
179 AARU_ALIGN16 uint8_t temp[16] = {0};
180 const uint8_t *nextBlockAddr = (const uint8_t *)(alignedData + 1);
181
182 // Only copy bytes that are actually within the original buffer
183 if(nextBlockAddr < end)
184 {
185 size_t available = (size_t)(end - nextBlockAddr);
186 if(available > 16) available = 16;
187 memcpy(temp, nextBlockAddr, available);
188 }
189
190 const uint64x2_t end1 = vreinterpretq_u64_u32(vld1q_u32((const uint32_t *)temp));
191
192 uint64x2_t A, B, C, D;
193 shiftRight128(end0, leadOutSize, &A, &B);
194 shiftRight128(end1, leadOutSize, &C, &D);
195
196 P = veorq_u64(fold(A, foldConstants1),
197 vreinterpretq_u64_u32(vorrq_u32(vreinterpretq_u32_u64(B), vreinterpretq_u32_u64(C))));
198 }
199
200 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)), mm_srli_si128(P, 8));
201 }
202
203 // Final Barrett reduction
204 const uint64x2_t T1 = sse2neon_vmull_p64(vget_low_u64(R), vget_low_u64(foldConstants2));
205 const uint64x2_t T2 = veorq_u64(
206 veorq_u64(sse2neon_vmull_p64(vget_low_u64(T1), vget_high_u64(foldConstants2)), mm_slli_si128(T1, 8)), R);
207
208 TRACE("Exiting aaruf_crc64_vmull()");
209
210 return ~vgetq_lane_u64(T2, 1);
211}
212
213#endif
#define AARU_CALL
Definition decls.h:46
#define AARU_EXPORT
Definition decls.h:55
#define FORCE_INLINE
Definition decls.h:64
#define TRACE(fmt,...)
Definition log.h:25