MySQL 8.4.3
Source Code Documentation
my_checksum.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef MY_CHECKSUM_INCLUDED
26#define MY_CHECKSUM_INCLUDED
27
28/**
29 @file include/my_checksum.h
30 Abstraction functions over zlib/intrinsics.
31*/
32
33#include <cassert>
34#include <cstdint> // std::uint32_t
35#include <limits> // std::numeric_limits
36#include <type_traits> // std::is_convertible
37
38#include <zlib.h> // crc32_z
39
40#include "my_compiler.h" // My_ATTRIBUTE
41#include "my_config.h"
42
43#ifdef HAVE_ARMV8_CRC32_INTRINSIC
44#include <arm_acle.h> // __crc32x
45#include <asm/hwcap.h> // HWCAP_CRC32
46#include <sys/auxv.h> // getauxval
47#endif /* HAVE_ARMV8_CRC32_INTRINSIC */
48
49namespace mycrc32 {
50#ifdef HAVE_ARMV8_CRC32_INTRINSIC
51const bool auxv_at_hwcap = (getauxval(AT_HWCAP) & HWCAP_CRC32);
52
53// Some overloads for these since to allow us to work genericly
54MY_ATTRIBUTE((target("+crc")))
55inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint8_t b) {
56 return __crc32b(crc, b);
57}
58
59MY_ATTRIBUTE((target("+crc")))
60inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint16_t h) {
61 return __crc32h(crc, h);
62}
63
64MY_ATTRIBUTE((target("+crc")))
65inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint32_t w) {
66 return __crc32w(crc, w);
67}
68
69MY_ATTRIBUTE((target("+crc")))
70inline std::uint32_t IntegerCrc32(std::uint32_t crc, std::uint64_t d) {
71 return __crc32d(crc, d);
72}
73#else /* HAVE_ARMV8_CRC32_INTRINSIC */
74
75template <class I>
76inline std::uint32_t IntegerCrc32(std::uint32_t crc, I i) {
77 unsigned char buf[sizeof(I)];
78 memcpy(buf, &i, sizeof(I));
79 crc = ~crc;
80 crc = crc32_z(crc, buf, sizeof(I));
81 return ~crc;
82}
83
84#endif /* HAVE_ARMV8_CRC32_INTRINSIC */
85
86template <class PT>
87inline std::uint32_t PunnedCrc32(std::uint32_t crc, const unsigned char *buf,
88 size_t len) {
89 crc = ~crc;
90 const unsigned char *plast = buf + (len / sizeof(PT)) * sizeof(PT);
91 const unsigned char *last = buf + len;
92 for (; buf < plast; buf += sizeof(PT)) {
93 PT pv;
94 memcpy(&pv, buf, sizeof(PT));
95 crc = IntegerCrc32(crc, pv);
96 }
97
98 for (; buf < last; ++buf) {
99 crc = IntegerCrc32(crc, *buf);
100 }
101
102 return (~crc);
103}
104} // namespace mycrc32
105
106using ha_checksum = std::uint32_t;
107
108/**
109 Calculate a CRC32 checksum for a memoryblock.
110
111 @param crc Start value for crc.
112 @param pos Pointer to memory block.
113 @param length Length of the block.
114
115 @returns Updated checksum.
116*/
117inline ha_checksum my_checksum(ha_checksum crc, const unsigned char *pos,
118 size_t length) {
119#ifdef HAVE_ARMV8_CRC32_INTRINSIC
120 if (mycrc32::auxv_at_hwcap)
121 return mycrc32::PunnedCrc32<std::uint64_t>(crc, pos, length);
122#endif // HAVE_ARMV8_CRC32_INTRINSIC
123 static_assert(std::is_convertible<uLong, ha_checksum>::value,
124 "uLong cannot be converted to ha_checksum");
125 assert(crc32_z(crc, pos, length) <= std::numeric_limits<ha_checksum>::max());
126 return crc32_z(crc, pos, length);
127}
128#endif /* not defined(MY_CEHCKSUM_INCLUDED) */
static char buf[MAX_BUF]
Definition: conf_to_src.cc:73
ha_checksum my_checksum(ha_checksum crc, const unsigned char *pos, size_t length)
Calculate a CRC32 checksum for a memoryblock.
Definition: my_checksum.h:117
std::uint32_t ha_checksum
Definition: my_checksum.h:106
Header for compiler-dependent features.
Definition: buf0block_hint.cc:30
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: my_checksum.h:49
std::uint32_t IntegerCrc32(std::uint32_t crc, I i)
Definition: my_checksum.h:76
std::uint32_t PunnedCrc32(std::uint32_t crc, const unsigned char *buf, size_t len)
Definition: my_checksum.h:87