Skip to content

Commit b51a078

Browse files
committed
crc: Designate {Mu,Kn}Constant as consteval
This ensures compile-time generation of these constants rather than calling it from the main loops. This turns a lot of the constants into simple memory-loads or embeds the load directly into the instruction!
1 parent ba3f784 commit b51a078

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

source/CRC/CRC32-a64.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static const CRC32TableT& GetCRC32Table(Polynomial Poly)
8989
}
9090
}
9191

92-
constexpr std::uint32_t BitReverse32(std::uint32_t Value)
92+
consteval std::uint32_t BitReverse32(std::uint32_t Value)
9393
{
9494
std::uint32_t Reversed = 0;
9595
for( std::uint32_t BitIndex = 0u; BitIndex < 32u; ++BitIndex )
@@ -101,7 +101,7 @@ constexpr std::uint32_t BitReverse32(std::uint32_t Value)
101101
}
102102

103103
// BitReverse(x^(shift) mod P(x) << 32) << 1
104-
constexpr std::uint64_t
104+
consteval std::uint64_t
105105
KnConstant(std::uint32_t ByteShift, std::uint32_t Polynomial)
106106
{
107107
std::uint32_t Remainder = 1u << 24;
@@ -125,7 +125,7 @@ constexpr std::uint64_t
125125
}
126126

127127
// BitReverse(x^64 / P(x)) << 1
128-
constexpr std::uint64_t MuConstant(uint32_t Polynomial)
128+
consteval std::uint64_t MuConstant(uint32_t Polynomial)
129129
{
130130
std::uint32_t Remainder = 1u << 24;
131131
std::uint32_t Quotient = 0u;
@@ -206,7 +206,7 @@ std::uint32_t CRC32_PMULL(std::span<const std::byte> Data, std::uint32_t CRC)
206206
// Fold 512 bits at a time
207207
for( ; Data.size() >= 64; Data = Data.subspan(64) )
208208
{
209-
static const poly64x2_t K1K2 = poly64x2_t{
209+
const poly64x2_t K1K2 = poly64x2_t{
210210
KnConstant(64 + 4, Polynomial),
211211
KnConstant(64 - 4, Polynomial),
212212
};
@@ -231,7 +231,7 @@ std::uint32_t CRC32_PMULL(std::span<const std::byte> Data, std::uint32_t CRC)
231231
}
232232

233233
// Reduce 512 to 128
234-
static const poly64x2_t K3K4 = poly64x2_t{
234+
const poly64x2_t K3K4 = poly64x2_t{
235235
KnConstant(16 + 4, Polynomial),
236236
KnConstant(16 - 4, Polynomial),
237237
};
@@ -277,7 +277,7 @@ std::uint32_t CRC32_PMULL(std::span<const std::byte> Data, std::uint32_t CRC)
277277

278278
CRCVec.val[0] = veorq_u64(Upper64, MulHiLo);
279279

280-
static const poly64x2_t K5K0 = poly64x2_t{
280+
const poly64x2_t K5K0 = poly64x2_t{
281281
KnConstant(8, Polynomial),
282282
0ull,
283283
};
@@ -292,7 +292,7 @@ std::uint32_t CRC32_PMULL(std::span<const std::byte> Data, std::uint32_t CRC)
292292

293293
// Reduce 64 to 32
294294
{
295-
static const poly64x2_t Poly = poly64x2_t{
295+
const poly64x2_t Poly = poly64x2_t{
296296
KnConstant(4, Polynomial) | 1,
297297
MuConstant(Polynomial),
298298
};
@@ -320,7 +320,6 @@ std::uint32_t Checksum(
320320

321321
if( Poly == Polynomial::CRC32 )
322322
{
323-
const std::uint32_t Polynomial32 = std::uint32_t(Poly);
324323
if( Data.size() >= 64 )
325324
{
326325
if( Data.size() % 16 == 0 )

source/CRC/CRC32-x64.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static const CRC32TableT& GetCRC32Table(Polynomial Poly)
7676
}
7777
}
7878

79-
constexpr std::uint32_t BitReverse32(std::uint32_t Value)
79+
consteval std::uint32_t BitReverse32(std::uint32_t Value)
8080
{
8181
std::uint32_t Reversed = 0;
8282
for( std::uint32_t BitIndex = 0u; BitIndex < 32u; ++BitIndex )
@@ -88,7 +88,7 @@ constexpr std::uint32_t BitReverse32(std::uint32_t Value)
8888
}
8989

9090
// BitReverse(x^(shift) mod P(x) << 32) << 1
91-
constexpr std::uint64_t
91+
consteval std::uint64_t
9292
KnConstant(std::uint32_t ByteShift, std::uint32_t Polynomial)
9393
{
9494
std::uint32_t Remainder = 1u << 24;
@@ -112,7 +112,7 @@ constexpr std::uint64_t
112112
}
113113

114114
// BitReverse(x^64 / P(x)) << 1
115-
constexpr std::uint64_t MuConstant(uint32_t Polynomial)
115+
consteval std::uint64_t MuConstant(uint32_t Polynomial)
116116
{
117117
std::uint32_t Remainder = 1u << 24;
118118
std::uint32_t Quotient = 0u;
@@ -173,7 +173,7 @@ std::uint32_t
173173
// Todo: VPCLMULQDQ(AVX2, AVX512)
174174
for( ; Data.size() >= 64; Data = Data.subspan(64) )
175175
{
176-
static const __m128i K1K2 = _mm_set_epi64x(
176+
const __m128i K1K2 = _mm_set_epi64x(
177177
KnConstant(64 - 4, Polynomial), KnConstant(64 + 4, Polynomial));
178178

179179
const __m128i MulLo0 = _mm_clmulepi64_si128(CRCVec0, K1K2, 0b0000'0000);
@@ -202,7 +202,7 @@ std::uint32_t
202202
}
203203

204204
// Reduce 512 to 128
205-
static const __m128i K3K4 = _mm_set_epi64x(
205+
const __m128i K3K4 = _mm_set_epi64x(
206206
KnConstant(16 - 4, Polynomial), KnConstant(16 + 4, Polynomial));
207207

208208
// Reduce Vec1 into Vec0
@@ -239,7 +239,7 @@ std::uint32_t
239239
}
240240

241241
// Reduce 128 to 64
242-
static const __m128i Lo32Mask64 = _mm_set1_epi64x(0xFFFFFFFF);
242+
const __m128i Lo32Mask64 = _mm_set1_epi64x(0xFFFFFFFF);
243243
{
244244
const __m128i MulHiLo
245245
= _mm_clmulepi64_si128(CRCVec0, K3K4, 0b0001'0000);
@@ -248,8 +248,7 @@ std::uint32_t
248248

249249
CRCVec0 = _mm_xor_si128(Upper64, MulHiLo);
250250

251-
static const __m128i K5K0
252-
= _mm_cvtsi64_si128(KnConstant(8, Polynomial));
251+
const __m128i K5K0 = _mm_cvtsi64_si128(KnConstant(8, Polynomial));
253252

254253
const __m128i Upper96 = _mm_srli_si128(CRCVec0, 4);
255254

@@ -262,7 +261,7 @@ std::uint32_t
262261

263262
// Reduce 64 to 32
264263
{
265-
static const __m128i Poly = _mm_set_epi64x(
264+
const __m128i Poly = _mm_set_epi64x(
266265
MuConstant(Polynomial), KnConstant(4, Polynomial) | 1);
267266

268267
__m128i Trunc32 = _mm_and_si128(CRCVec0, Lo32Mask64);

0 commit comments

Comments
 (0)