| 1 | /* Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 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 | #include <algorithm> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "unittest/gunit/benchmark.h" |
| 29 | #include "unittest/gunit/mysys_util.h" |
| 30 | |
| 31 | #include "my_checksum.h" |
| 32 | // Unit tests for my_checksum function implemented with zlib and hardware |
| 33 | // intrinsics where supported. |
| 34 | using namespace mycrc32; |
| 35 | |
| 36 | namespace mysys_my_checksum { |
| 37 | std::uint32_t VerifyChecksumFuncs(const unsigned char *buf, |
| 38 | std::size_t length) { |
| 39 | std::uint32_t crc_seed = 0xbadcafe; |
| 40 | std::uint32_t expected_crc = crc32_z(crc_seed, buf, length); |
| 41 | |
| 42 | EXPECT_EQ(expected_crc, my_checksum(crc_seed, buf, length)); |
| 43 | EXPECT_EQ(expected_crc, PunnedCrc32<std::uint64_t>(crc_seed, buf, length)); |
| 44 | return expected_crc; |
| 45 | } |
| 46 | |
| 47 | TEST(MysysMyChecksum, EmptyBuffer) { |
| 48 | unsigned char b[1] = {'0'}; |
| 49 | EXPECT_EQ(0xbadcafe, VerifyChecksumFuncs(b, 0)); |
| 50 | } |
| 51 | |
| 52 | TEST(MysysMyChecksum, TenBytesZero) { |
| 53 | unsigned char b[10] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'}; |
| 54 | EXPECT_EQ(272755629U, VerifyChecksumFuncs(b, 10)); |
| 55 | } |
| 56 | |
| 57 | TEST(MysysMyChecksum, TenBytesFF) { |
| 58 | unsigned char b[10] = {0xff, 0xff, 0xff, 0xff, 0xff, |
| 59 | 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 60 | EXPECT_EQ(533143559U, VerifyChecksumFuncs(b, 10)); |
| 61 | } |
| 62 | |
| 63 | TEST(MysysMyChecksum, ThirtyOneBytes) { |
| 64 | alignas(alignof(std::int64_t)) unsigned char b[] = { |
| 65 | 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, |
| 66 | 0x44, 0x33, 0x22, 0x11, 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, |
| 67 | 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0}; |
| 68 | EXPECT_EQ(2359828439U, VerifyChecksumFuncs(b, sizeof(b))); |
| 69 | EXPECT_EQ(1093230115U, VerifyChecksumFuncs(b + 1, sizeof(b) - 1)); |
| 70 | EXPECT_EQ(3891498923U, VerifyChecksumFuncs(b + 4, sizeof(b) - 4)); |
| 71 | EXPECT_EQ(561217492U, VerifyChecksumFuncs(b + 7, sizeof(b) - 7)); |
| 72 | } |
| 73 | |
| 74 | TEST(MysysMyChecksum, IntegerCrc32_8bit) { |
| 75 | unsigned char b = 0xba; |
| 76 | std::uint32_t crc = 0xff; |
| 77 | std::uint32_t zres = crc32_z(~crc, &b, 1U); |
| 78 | EXPECT_EQ(IntegerCrc32(crc, b), ~zres); |
| 79 | } |
| 80 | |
| 81 | TEST(MysysMyChecksum, IntegerCrc32_16bit) { |
| 82 | unsigned char value_bytes[] = {0xaa, 0xbb}; |
| 83 | |
| 84 | std::uint32_t crc = 0xbadcafe; |
| 85 | std::uint32_t zres = crc32_z(~crc, value_bytes, sizeof(value_bytes)); |
| 86 | |
| 87 | std::uint16_t value; |
| 88 | memcpy(&value, value_bytes, sizeof(value_bytes)); |
| 89 | |
| 90 | EXPECT_EQ(IntegerCrc32(crc, value), ~zres); |
| 91 | } |
| 92 | |
| 93 | TEST(MysysMyChecksum, IntegerCrc32_32bit) { |
| 94 | unsigned char value_bytes[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
| 95 | std::uint32_t crc = 0xbadcafe; |
| 96 | std::uint32_t zres = crc32_z(~crc, value_bytes, sizeof(value_bytes)); |
| 97 | std::uint32_t value; |
| 98 | memcpy(&value, value_bytes, sizeof(value_bytes)); |
| 99 | EXPECT_EQ(IntegerCrc32(crc, value), ~zres); |
| 100 | } |
| 101 | |
| 102 | TEST(MysysMyChecksum, IntegerCrc32_double_32bit) { |
| 103 | unsigned char value_bytes[] = {0x99, 0x11, 0xaa, 0xbb, |
| 104 | 0xcc, 0xdd, 0xee, 0xff}; |
| 105 | std::uint32_t crc = 0xbadcafe; |
| 106 | std::uint32_t zres = crc32_z(~crc, value_bytes, sizeof(value_bytes)); |
| 107 | std::uint32_t value1, value2; |
| 108 | memcpy(&value1, value_bytes, sizeof(value1)); |
| 109 | memcpy(&value2, value_bytes + sizeof(value1), sizeof(value2)); |
| 110 | |
| 111 | std::uint32_t crc1 = IntegerCrc32(crc, value1); |
| 112 | EXPECT_EQ(IntegerCrc32(crc1, value2), ~zres); |
| 113 | } |
| 114 | |
| 115 | TEST(MysysMyChecksum, IntegerCrc32_64bit) { |
| 116 | unsigned char value_bytes[] = {0x11, 0x22, 0x33, 0x44, |
| 117 | 0x55, 0x66, 0x77, 0x88}; |
| 118 | std::uint64_t value; |
| 119 | memcpy(&value, value_bytes, sizeof(value)); |
| 120 | std::uint32_t crc = 0xbadcafe; |
| 121 | std::uint32_t zres = crc32_z(~crc, value_bytes, sizeof(value_bytes)); |
| 122 | EXPECT_EQ(IntegerCrc32(crc, value), ~zres); |
| 123 | } |
| 124 | |
| 125 | static volatile std::uint32_t do_not_optimize = 0; |
| 126 | // 50k buffer, 8-byte using crc32_z directly |
| 127 | static void BM_crc32_z_50k(size_t num_iterations) { |
| 128 | StopBenchmarkTiming(); |
| 129 | |
| 130 | alignas(alignof(std::uint64_t)) unsigned char buf[50000]; |
| 131 | unsigned char v = 0xda; |
| 132 | std::generate(buf, buf + sizeof(buf), [&] { return ++v; }); |
| 133 | |
| 134 | std::uint32_t crc = 0xdeadcafe; |
| 135 | StartBenchmarkTiming(); |
| 136 | |
| 137 | for (size_t i = 0; i < num_iterations; ++i) { |
| 138 | crc = crc32_z(crc, buf, sizeof(buf) - 1); |
| 139 | } |
| 140 | |
| 141 | StopBenchmarkTiming(); |
| 142 | do_not_optimize = crc; |
| 143 | } |
| 144 | BENCHMARK(BM_crc32_z_50k) |
| 145 | |
| 146 | // 50k buffer, 8-byte using my_checksum (will use intrinsics on ARM) |
| 147 | // |
| 148 | static void BM_my_checksum_50k(size_t num_iterations) { |
| 149 | StopBenchmarkTiming(); |
| 150 | |
| 151 | alignas(alignof(std::uint64_t)) unsigned char buf[50000]; |
| 152 | unsigned char v = 0xda; |
| 153 | std::generate(buf, buf + sizeof(buf), [&] { return ++v; }); |
| 154 | |
| 155 | std::uint32_t crc = 0xdeadcafe; |
| 156 | StartBenchmarkTiming(); |
| 157 | |
| 158 | for (size_t i = 0; i < num_iterations; ++i) { |
| 159 | crc = my_checksum(crc, buf, sizeof(buf) - 1); |
| 160 | } |
| 161 | |
| 162 | StopBenchmarkTiming(); |
| 163 | do_not_optimize = crc; |
| 164 | } |
| 165 | BENCHMARK(BM_my_checksum_50k) |
| 166 | |
| 167 | #ifdef HAVE_ARMV8_CRC32_INTRINSIC |
| 168 | |
| 169 | // Baseline 8 bit integer using crc32_z |
| 170 | static void BM_crc32_z_8bit(size_t num_iterations) { |
| 171 | StopBenchmarkTiming(); |
| 172 | |
| 173 | std::uint32_t crc = 0xdeadcafe; |
| 174 | StartBenchmarkTiming(); |
| 175 | |
| 176 | for (size_t i = 0; i < num_iterations; ++i) { |
| 177 | std::uint8_t b = static_cast<std::uint8_t>(i); |
| 178 | crc = crc32_z(crc, &b, 1); |
| 179 | } |
| 180 | |
| 181 | StopBenchmarkTiming(); |
| 182 | do_not_optimize = crc; |
| 183 | } |
| 184 | BENCHMARK(BM_crc32_z_8bit) |
| 185 | |
| 186 | // 8-bit integer using intrinsic wrapper overload |
| 187 | static void BM_IntegerCrc32_8bit(size_t num_iterations) { |
| 188 | StopBenchmarkTiming(); |
| 189 | |
| 190 | std::uint32_t crc = 0xdeadcafe; |
| 191 | StartBenchmarkTiming(); |
| 192 | |
| 193 | for (size_t i = 0; i < num_iterations; ++i) { |
| 194 | crc = IntegerCrc32(crc, static_cast<std::uint8_t>(i)); |
| 195 | } |
| 196 | |
| 197 | StopBenchmarkTiming(); |
| 198 | do_not_optimize = crc; |
| 199 | } |
| 200 | BENCHMARK(BM_IntegerCrc32_8bit) |
| 201 | |
| 202 | // Baseline 64-bit integer suing crc32_z |
| 203 | static void BM_crc32_z_64bit(size_t num_iterations) { |
| 204 | StopBenchmarkTiming(); |
| 205 | |
| 206 | std::uint32_t crc = 0xdeadcafe; |
| 207 | StartBenchmarkTiming(); |
| 208 | |
| 209 | for (size_t i = 0; i < num_iterations; ++i) { |
| 210 | unsigned char buf[8]; |
| 211 | memcpy(buf, &i, sizeof(i)); |
| 212 | crc = crc32_z(crc, buf, 8); |
| 213 | } |
| 214 | |
| 215 | StopBenchmarkTiming(); |
| 216 | do_not_optimize = crc; |
| 217 | } |
| 218 | BENCHMARK(BM_crc32_z_64bit) |
| 219 | |
| 220 | // 64-bit integer using intrinsic wrapper overload |
| 221 | static void BM_IntegerCrc32_64bit(size_t num_iterations) { |
| 222 | StopBenchmarkTiming(); |
| 223 | |
| 224 | std::uint32_t crc = 0xdeadcafe; |
| 225 | StartBenchmarkTiming(); |
| 226 | |
| 227 | for (size_t i = 0; i < num_iterations; ++i) { |
| 228 | std::uint64_t v = i; |
| 229 | crc = IntegerCrc32(crc, v); |
| 230 | } |
| 231 | |
| 232 | StopBenchmarkTiming(); |
| 233 | do_not_optimize = crc; |
| 234 | } |
| 235 | BENCHMARK(BM_IntegerCrc32_64bit) |
| 236 | |
| 237 | // PunnedCrc32 algo 50k with 8-byte slices |
| 238 | static void BM_PunnedCrc32_50k(size_t num_iterations) { |
| 239 | StopBenchmarkTiming(); |
| 240 | |
| 241 | alignas(alignof(std::uint64_t)) unsigned char buf[50000]; |
| 242 | unsigned char v = 0xda; |
| 243 | std::generate(buf, buf + sizeof(buf), [&] { return ++v; }); |
| 244 | |
| 245 | std::uint32_t crc = 0xdeadcafe; |
| 246 | StartBenchmarkTiming(); |
| 247 | |
| 248 | for (size_t i = 0; i < num_iterations; ++i) { |
| 249 | crc = PunnedCrc32<std::uint64_t>(crc, buf, sizeof(buf) - 1); |
| 250 | } |
| 251 | |
| 252 | StopBenchmarkTiming(); |
| 253 | do_not_optimize = crc; |
| 254 | } |
| 255 | BENCHMARK(BM_PunnedCrc32_50k) |
| 256 | |
| 257 | #endif /* HAVE_ARMV8_CRC32_INTRINSIC */ |
| 258 | |
| 259 | } // namespace mysys_my_checksum |