pg_crc32c.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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-2016, 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. #include "port/pg_bswap.h"
  36. typedef uint32 pg_crc32c;
  37. /* The INIT and EQ macros are the same for all implementations. */
  38. #define INIT_CRC32C(crc) ((crc) = 0xFFFFFFFF)
  39. #define EQ_CRC32C(c1, c2) ((c1) == (c2))
  40. #if defined(USE_SSE42_CRC32C)
  41. /* Use SSE4.2 instructions. */
  42. #define COMP_CRC32C(crc, data, len) \
  43. ((crc) = pg_comp_crc32c_sse42((crc), (data), (len)))
  44. #define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
  45. extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len);
  46. #elif defined(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK)
  47. /*
  48. * Use SSE4.2 instructions, but perform a runtime check first to check that
  49. * they are available.
  50. */
  51. #define COMP_CRC32C(crc, data, len) \
  52. ((crc) = pg_comp_crc32c((crc), (data), (len)))
  53. #define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
  54. extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len);
  55. extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
  56. extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len);
  57. #else
  58. /*
  59. * Use slicing-by-8 algorithm.
  60. *
  61. * On big-endian systems, the intermediate value is kept in reverse byte
  62. * order, to avoid byte-swapping during the calculation. FIN_CRC32C reverses
  63. * the bytes to the final order.
  64. */
  65. #define COMP_CRC32C(crc, data, len) \
  66. ((crc) = pg_comp_crc32c_sb8((crc), (data), (len)))
  67. #ifdef WORDS_BIGENDIAN
  68. #define FIN_CRC32C(crc) ((crc) = BSWAP32(crc) ^ 0xFFFFFFFF)
  69. #else
  70. #define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)
  71. #endif
  72. extern pg_crc32c pg_comp_crc32c_sb8(pg_crc32c crc, const void *data, size_t len);
  73. #endif
  74. #endif /* PG_CRC32C_H */