crc32c.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "crc32c.h"
  2. #include <util/generic/singleton.h>
  3. #include <contrib/libs/crcutil/interface.h>
  4. namespace {
  5. typedef crcutil_interface::CRC TCrc;
  6. struct TCrcUtilSse4 {
  7. TCrc* const Pimpl;
  8. TCrcUtilSse4() noexcept
  9. : Pimpl(TCrc::Create(0x82f63b78, 0, 32, true, 0, 0, 0, TCrc::IsSSE42Available(), nullptr))
  10. {
  11. }
  12. ~TCrcUtilSse4() noexcept {
  13. Pimpl->Delete();
  14. }
  15. inline ui32 Extend(ui32 init, const void* data, size_t n) const noexcept {
  16. crcutil_interface::UINT64 sum = init;
  17. Pimpl->Compute(data, n, &sum);
  18. return (ui32)sum;
  19. }
  20. inline ui32 Combine(ui32 blockACrc, ui32 blockBCrc, size_t blockBSize) const noexcept {
  21. crcutil_interface::UINT64 sum = blockACrc;
  22. Pimpl->Concatenate(blockBCrc, 0, blockBSize, &sum);
  23. return (ui32)sum;
  24. }
  25. };
  26. }
  27. ui32 Crc32c(const void* p, size_t size) noexcept {
  28. return Singleton<TCrcUtilSse4>()->Extend(0, p, size);
  29. }
  30. ui32 Crc32cExtend(ui32 init, const void* data, size_t n) noexcept {
  31. return Singleton<TCrcUtilSse4>()->Extend(init, data, n);
  32. }
  33. ui32 Crc32cCombine(ui32 blockACrc, ui32 blockBCrc, size_t blockBSize) noexcept {
  34. return Singleton<TCrcUtilSse4>()->Combine(blockACrc, blockBCrc, blockBSize);
  35. }
  36. bool HaveFastCrc32c() noexcept {
  37. return TCrc::IsSSE42Available();
  38. }