forked from fastio/1store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.hh
101 lines (94 loc) · 2.66 KB
/
crc.hh
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (C) 2015 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
#include <smmintrin.h>
namespace utils {
class crc32 {
uint32_t _r = 0;
public:
// All process() functions assume input is in
// host byte order (i.e. equivalent to storing
// the value in a buffer and crcing the buffer).
void process(int8_t in) {
_r = _mm_crc32_u8(_r, in);
}
void process(uint8_t in) {
_r = _mm_crc32_u8(_r, in);
}
void process(int16_t in) {
_r = _mm_crc32_u16(_r, in);
}
void process(uint16_t in) {
_r = _mm_crc32_u16(_r, in);
}
void process(int32_t in) {
_r = _mm_crc32_u32(_r, in);
}
void process(uint32_t in) {
_r = _mm_crc32_u32(_r, in);
}
void process(int64_t in) {
_r = _mm_crc32_u64(_r, in);
}
void process(uint64_t in) {
_r = _mm_crc32_u64(_r, in);
}
void process(const uint8_t* in, size_t size) {
if ((reinterpret_cast<uintptr_t>(in) & 1) && size >= 1) {
process(*in);
++in;
--size;
}
if ((reinterpret_cast<uintptr_t>(in) & 3) && size >= 2) {
process(*reinterpret_cast<const uint16_t*>(in));
in += 2;
size -= 2;
}
if ((reinterpret_cast<uintptr_t>(in) & 7) && size >= 4) {
process(*reinterpret_cast<const uint32_t*>(in));
in += 4;
size -= 4;
}
// FIXME: do in three parallel loops
while (size >= 8) {
process(*reinterpret_cast<const uint64_t*>(in));
in += 8;
size -= 8;
}
if (size >= 4) {
process(*reinterpret_cast<const uint32_t*>(in));
in += 4;
size -= 4;
}
if (size >= 2) {
process(*reinterpret_cast<const uint16_t*>(in));
in += 2;
size -= 2;
}
if (size >= 1) {
process(*in);
}
}
uint32_t get() const {
return _r;
}
};
}