mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Use Intel SSE 4.2 CRC instructions where available.
Modern x86 and x86-64 processors with SSE 4.2 support have special instructions, crc32b and crc32q, for calculating CRC-32C. They greatly speed up CRC calculation. Whether the instructions can be used or not depends on the compiler and the target architecture. If generation of SSE 4.2 instructions is allowed for the target (-msse4.2 flag on gcc and clang), use them. If they are not allowed by default, but the compiler supports the -msse4.2 flag to enable them, compile just the CRC-32C function with -msse4.2 flag, and check at runtime whether the processor we're running on supports it. If it doesn't, fall back to the slicing-by-8 algorithm. (With the common defaults on current operating systems, the runtime-check variant is what you get in practice.) Abhijit Menon-Sen, heavily modified by me, reviewed by Andres Freund.
This commit is contained in:
@ -3,6 +3,25 @@
|
||||
* pg_crc32c.h
|
||||
* Routines for computing CRC-32C checksums.
|
||||
*
|
||||
* The speed of CRC-32C calculation has a big impact on performance, so we
|
||||
* jump through some hoops to get the best implementation for each
|
||||
* platform. Some CPU architectures have special instructions for speeding
|
||||
* up CRC calculations (e.g. Intel SSE 4.2), on other platforms we use the
|
||||
* Slicing-by-8 algorithm which uses lookup tables.
|
||||
*
|
||||
* The public interface consists of four macros:
|
||||
*
|
||||
* INIT_CRC32C(crc)
|
||||
* Initialize a CRC accumulator
|
||||
*
|
||||
* COMP_CRC32C(crc, data, len)
|
||||
* Accumulate some (more) bytes into a CRC
|
||||
*
|
||||
* FIN_CRC32C(crc)
|
||||
* Finish a CRC calculation
|
||||
*
|
||||
* EQ_CRC32C(c1, c2)
|
||||
* Check for equality of two CRCs.
|
||||
*
|
||||
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
@ -16,9 +35,32 @@
|
||||
|
||||
typedef uint32 pg_crc32c;
|
||||
|
||||
/* The INIT and EQ macros are the same for all implementations. */
|
||||
#define INIT_CRC32C(crc) ((crc) = 0xFFFFFFFF)
|
||||
#define EQ_CRC32C(c1, c2) ((c1) == (c2))
|
||||
|
||||
#if defined(USE_SSE42_CRC32C)
|
||||
/* Use SSE4.2 instructions. */
|
||||
#define COMP_CRC32C(crc, data, len) \
|
||||
((crc) = pg_comp_crc32c_sse42((crc), (data), (len)))
|
||||
#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
|
||||
|
||||
extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len);
|
||||
|
||||
#elif defined(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK)
|
||||
/*
|
||||
* Use SSE4.2 instructions, but perform a runtime check first to check that
|
||||
* they are available.
|
||||
*/
|
||||
#define COMP_CRC32C(crc, data, len) \
|
||||
((crc) = pg_comp_crc32c((crc), (data), (len)))
|
||||
#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
|
||||
|
||||
extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len);
|
||||
extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
|
||||
extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len);
|
||||
|
||||
#else
|
||||
/*
|
||||
* Use slicing-by-8 algorithm.
|
||||
*
|
||||
@ -46,4 +88,6 @@ typedef uint32 pg_crc32c;
|
||||
|
||||
extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* PG_CRC32C_H */
|
||||
|
Reference in New Issue
Block a user