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