PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_crc32c.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_crc32c.h
4 * Routines for computing CRC-32C checksums.
5 *
6 * The speed of CRC-32C calculation has a big impact on performance, so we
7 * jump through some hoops to get the best implementation for each
8 * platform. Some CPU architectures have special instructions for speeding
9 * up CRC calculations (e.g. Intel SSE 4.2), on other platforms we use the
10 * Slicing-by-8 algorithm which uses lookup tables.
11 *
12 * The public interface consists of four macros:
13 *
14 * INIT_CRC32C(crc)
15 * Initialize a CRC accumulator
16 *
17 * COMP_CRC32C(crc, data, len)
18 * Accumulate some (more) bytes into a CRC
19 *
20 * FIN_CRC32C(crc)
21 * Finish a CRC calculation
22 *
23 * EQ_CRC32C(c1, c2)
24 * Check for equality of two CRCs.
25 *
26 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
27 * Portions Copyright (c) 1994, Regents of the University of California
28 *
29 * src/include/port/pg_crc32c.h
30 *
31 *-------------------------------------------------------------------------
32 */
33#ifndef PG_CRC32C_H
34#define PG_CRC32C_H
35
36#include "port/pg_bswap.h"
37
39
40/* The INIT and EQ macros are the same for all implementations. */
41#define INIT_CRC32C(crc) ((crc) = 0xFFFFFFFF)
42#define EQ_CRC32C(c1, c2) ((c1) == (c2))
43
44#if defined(USE_SSE42_CRC32C)
45/*
46 * Use either Intel SSE 4.2 or AVX-512 instructions. We don't need a runtime check
47 * for SSE 4.2, so we can inline those in some cases.
48 */
49
50#include <nmmintrin.h>
51
52#define COMP_CRC32C(crc, data, len) \
53 ((crc) = pg_comp_crc32c_dispatch((crc), (data), (len)))
54#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
55
56extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len);
57extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len);
58#ifdef USE_AVX512_CRC32C_WITH_RUNTIME_CHECK
59extern pg_crc32c pg_comp_crc32c_avx512(pg_crc32c crc, const void *data, size_t len);
60#endif
61
62/*
63 * We can only get here if the host compiler targets SSE 4.2, but on some
64 * systems gcc and clang don't have the same built-in targets, so we still
65 * must use a function attribute here to accommodate "--with-llvm" builds.
66 */
72{
73 if (__builtin_constant_p(len) && len < 32)
74 {
75 const unsigned char *p = (const unsigned char *) data;
76
77 /*
78 * For small constant inputs, inline the computation to avoid a
79 * function call and allow the compiler to unroll loops.
80 */
81#if SIZEOF_VOID_P >= 8
82 for (; len >= 8; p += 8, len -= 8)
83 crc = _mm_crc32_u64(crc, *(const uint64 *) p);
84#endif
85 for (; len >= 4; p += 4, len -= 4)
86 crc = _mm_crc32_u32(crc, *(const uint32 *) p);
87 for (; len > 0; --len)
88 crc = _mm_crc32_u8(crc, *p++);
89 return crc;
90 }
91 else
92 /* Otherwise, use a runtime check for AVX-512 instructions. */
93 return pg_comp_crc32c(crc, data, len);
94}
95
96#elif defined(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK)
97
98/*
99 * Use Intel SSE 4.2 or AVX-512 instructions, but perform a runtime check first
100 * to check that they are available.
101 */
102#define COMP_CRC32C(crc, data, len) \
103 ((crc) = pg_comp_crc32c((crc), (data), (len)))
104#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
105
106extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
107extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len);
108extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len);
109#ifdef USE_AVX512_CRC32C_WITH_RUNTIME_CHECK
110extern pg_crc32c pg_comp_crc32c_avx512(pg_crc32c crc, const void *data, size_t len);
111#endif
112
113#elif defined(USE_ARMV8_CRC32C)
114/*
115 * Use either ARMv8 CRC Extension or CRYPTO Extension (PMULL) instructions.
116 */
117#ifdef HAVE_PG_INTEGER_CONSTANT_P
118/*
119 * We don't need a runtime check for CRC, so for constant inputs, where
120 * we assume the input is small, we can avoid an indirect function call.
121 */
122#define COMP_CRC32C(crc, data, len) \
123 ((crc) = pg_integer_constant_p(len) ? \
124 pg_comp_crc32c_armv8((crc), (data), (len)) : \
125 pg_comp_crc32c((crc), (data), (len)))
126#else
127#define COMP_CRC32C(crc, data, len) \
128 ((crc) = pg_comp_crc32c((crc), (data), (len)))
129#endif /* HAVE_PG_INTEGER_CONSTANT_P */
130
131#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
132
133extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len);
134extern pg_crc32c pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len);
135#ifdef USE_PMULL_CRC32C_WITH_RUNTIME_CHECK
136extern pg_crc32c pg_comp_crc32c_pmull(pg_crc32c crc, const void *data, size_t len);
137#endif
138
139#elif defined(USE_LOONGARCH_CRC32C)
140/* Use LoongArch CRCC instructions. */
141
142#define COMP_CRC32C(crc, data, len) \
143 ((crc) = pg_comp_crc32c_loongarch((crc), (data), (len)))
144#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
145
146extern pg_crc32c pg_comp_crc32c_loongarch(pg_crc32c crc, const void *data, size_t len);
147
148#elif defined(USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK)
149
150/*
151 * Use either ARMv8 CRC Extension or CRYPTO Extension (PMULL) instructions,
152 * but perform a runtime check first to check that they are available.
153 */
154#define COMP_CRC32C(crc, data, len) \
155 ((crc) = pg_comp_crc32c((crc), (data), (len)))
156#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
157
158extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
159extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len);
160extern pg_crc32c pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len);
161#ifdef USE_PMULL_CRC32C_WITH_RUNTIME_CHECK
162extern pg_crc32c pg_comp_crc32c_pmull(pg_crc32c crc, const void *data, size_t len);
163#endif
164
165#else
166/*
167 * Use slicing-by-8 algorithm.
168 *
169 * On big-endian systems, the intermediate value is kept in reverse byte
170 * order, to avoid byte-swapping during the calculation. FIN_CRC32C reverses
171 * the bytes to the final order.
172 */
173#define COMP_CRC32C(crc, data, len) \
174 ((crc) = pg_comp_crc32c_sb8((crc), (data), (len)))
175#ifdef WORDS_BIGENDIAN
176#define FIN_CRC32C(crc) ((crc) = pg_bswap32(crc) ^ 0xFFFFFFFF)
177#else
178#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
179#endif
180
181extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
182
183#endif
184
185#endif /* PG_CRC32C_H */
#define pg_attribute_no_sanitize_alignment()
Definition c.h:215
uint64_t uint64
Definition c.h:625
uint32_t uint32
Definition c.h:624
#define pg_attribute_target(...)
Definition c.h:238
uint32 pg_crc32c
Definition pg_crc32c.h:38
pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len)
pg_crc32c pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len)
pg_crc32c(* pg_comp_crc32c)(pg_crc32c crc, const void *data, size_t len)
pg_crc32c pg_comp_crc32c_loongarch(pg_crc32c crc, const void *data, size_t len)
const void size_t len
const void * data
return crc
static int fb(int x)