aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/digest/crc32c/crc32c.cpp
blob: bc9de5213413e16ee1c2a4ae591aca7a37f6e0a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "crc32c.h"

#include <util/generic/singleton.h>

#include <contrib/libs/crcutil/interface.h>

namespace {
    typedef crcutil_interface::CRC TCrc;

    struct TCrcUtilSse4 {
        TCrc* const Pimpl;

        TCrcUtilSse4() noexcept
            : Pimpl(TCrc::Create(0x82f63b78, 0, 32, true, 0, 0, 0, TCrc::IsSSE42Available(), nullptr))
        {
        }

        ~TCrcUtilSse4() noexcept {
            Pimpl->Delete();
        }

        inline ui32 Extend(ui32 init, const void* data, size_t n) const noexcept {
            crcutil_interface::UINT64 sum = init;
            Pimpl->Compute(data, n, &sum);
            return (ui32)sum;
        }

        inline ui32 Combine(ui32 blockACrc, ui32 blockBCrc, size_t blockBSize) const noexcept {
            crcutil_interface::UINT64 sum = blockACrc;
            Pimpl->Concatenate(blockBCrc, 0, blockBSize, &sum);
            return (ui32)sum;
        }
    };
}

ui32 Crc32c(const void* p, size_t size) noexcept {
    return Singleton<TCrcUtilSse4>()->Extend(0, p, size);
}

ui32 Crc32cExtend(ui32 init, const void* data, size_t n) noexcept {
    return Singleton<TCrcUtilSse4>()->Extend(init, data, n);
}

ui32 Crc32cCombine(ui32 blockACrc, ui32 blockBCrc, size_t blockBSize) noexcept {
    return Singleton<TCrcUtilSse4>()->Combine(blockACrc, blockBCrc, blockBSize);
}

bool HaveFastCrc32c() noexcept {
    return TCrc::IsSSE42Available();
}