-
Notifications
You must be signed in to change notification settings - Fork 343
/
my_checksum.h
128 lines (104 loc) · 4.05 KB
/
my_checksum.h
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Copyright (c) 2020, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program 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, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef MY_CHECKSUM_INCLUDED
#define MY_CHECKSUM_INCLUDED
/**
@file include/my_checksum.h
Abstraction functions over zlib/intrinsics.
*/
#include <cassert>
#include <cstdint> // std::uint32_t
#include <limits> // std::numeric_limits
#include <type_traits> // std::is_convertible
#include <zlib.h> // crc32_z
#include "my_compiler.h" // My_ATTRIBUTE
#include "my_config.h"
#ifdef HAVE_ARMV8_CRC32_INTRINSIC
#include <arm_acle.h> // __crc32x
#include <asm/hwcap.h> // HWCAP_CRC32
#include <sys/auxv.h> // getauxval
#endif /* HAVE_ARMV8_CRC32_INTRINSIC */
namespace mycrc32 {
#ifdef HAVE_ARMV8_CRC32_INTRINSIC
const bool auxv_at_hwcap = (getauxval(AT_HWCAP) & HWCAP_CRC32);
// Some overloads for these since to allow us to work genericly
MY_ATTRIBUTE((target("+crc")))
inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint8_t b) {
return __crc32b(crc, b);
}
MY_ATTRIBUTE((target("+crc")))
inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint16_t h) {
return __crc32h(crc, h);
}
MY_ATTRIBUTE((target("+crc")))
inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint32_t w) {
return __crc32w(crc, w);
}
MY_ATTRIBUTE((target("+crc")))
inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint64_t d) {
return __crc32d(crc, d);
}
#else /* HAVE_ARMV8_CRC32_INTRINSIC */
template <class I>
inline std::uint32_t IntegerCrc32(std::uint32_t crc, I i) {
unsigned char buf[sizeof(I)];
memcpy(buf, &i, sizeof(I));
crc = ~crc;
crc = crc32_z(crc, buf, sizeof(I));
return ~crc;
}
#endif /* HAVE_ARMV8_CRC32_INTRINSIC */
template <class PT>
inline std::uint32_t PunnedCrc32(std::uint32_t crc, const unsigned char *buf,
size_t len) {
crc = ~crc;
const unsigned char *plast = buf + (len / sizeof(PT)) * sizeof(PT);
const unsigned char *last = buf + len;
for (; buf < plast; buf += sizeof(PT)) {
PT pv;
memcpy(&pv, buf, sizeof(PT));
crc = IntegerCrc32(crc, pv);
}
for (; buf < last; ++buf) {
crc = IntegerCrc32(crc, *buf);
}
return (~crc);
}
} // namespace mycrc32
using ha_checksum = std::uint32_t;
/**
Calculate a CRC32 checksum for a memoryblock.
@param crc Start value for crc.
@param pos Pointer to memory block.
@param length Length of the block.
@returns Updated checksum.
*/
inline ha_checksum my_checksum(ha_checksum crc, const unsigned char *pos,
size_t length) {
#ifdef HAVE_ARMV8_CRC32_INTRINSIC
if (mycrc32::auxv_at_hwcap)
return mycrc32::PunnedCrc32<std::uint64_t>(crc, pos, length);
#endif // HAVE_ARMV8_CRC32_INTRINSIC
static_assert(std::is_convertible<uLong, ha_checksum>::value,
"uLong cannot be converted to ha_checksum");
assert(crc32_z(crc, pos, length) <= std::numeric_limits<ha_checksum>::max());
return crc32_z(crc, pos, length);
}
#endif /* not defined(MY_CEHCKSUM_INCLUDED) */