| 1 | /* Copyright (c) 2021, 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 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <cstdint> |
| 28 | #include "unittest/gunit/benchmark.h" |
| 29 | |
| 30 | #include "storage/innobase/include/ut0rnd.h" |
| 31 | |
| 32 | namespace innodb_ut0math_unittest { |
| 33 | |
| 34 | /* Correctness test for math functions. */ |
| 35 | TEST(ut0math, countr_zero) { |
| 36 | ASSERT_EQ(ut::countr_zero(0), 64); |
| 37 | ASSERT_EQ(ut::countr_zero(~uint64_t{0}), 0); |
| 38 | for (int p = 0; p < 64; ++p) { |
| 39 | ASSERT_EQ(ut::countr_zero(uint64_t{1} << p), p); |
| 40 | ASSERT_EQ(ut::countr_zero(~uint64_t{0} << p), p); |
| 41 | for (int t = 0; t < 100; ++t) { |
| 42 | const auto r = (uint64_t)(rand() | 1) << p; |
| 43 | ASSERT_EQ(ut::countr_zero(r), p); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | TEST(ut0math, div_ceil) { |
| 48 | ASSERT_EQ(ut::div_ceil(7, 3), 3); |
| 49 | ASSERT_EQ(ut::div_ceil(7, -3), -2); |
| 50 | ASSERT_EQ(ut::div_ceil(-7, 3), -2); |
| 51 | ASSERT_EQ(ut::div_ceil(-7, -3), 3); |
| 52 | ASSERT_EQ(ut::div_ceil(uint8_t{247}, uint8_t{10}), uint8_t{25}); |
| 53 | ASSERT_EQ(ut::div_ceil(uint8_t{255}, uint8_t{1}), uint8_t{255}); |
| 54 | ASSERT_EQ(ut::div_ceil(int8_t{-128}, int8_t{1}), int8_t{-128}); |
| 55 | } |
| 56 | |
| 57 | void test_multiply_uint64(uint64_t x, uint64_t y) { |
| 58 | const auto xy = x * y; |
| 59 | uint64_t hi; |
| 60 | uint64_t lo = ut::detail::multiply_uint64_portable(x, y, hi); |
| 61 | ASSERT_EQ(hi, 0); |
| 62 | ASSERT_EQ(lo, xy); |
| 63 | } |
| 64 | |
| 65 | TEST(ut0math, multiply_uint64_portable) { |
| 66 | for (int i = 0; i < 100000; i++) { |
| 67 | const uint64_t x = ut::random_64() >> 2; |
| 68 | if (x < 10) continue; |
| 69 | |
| 70 | /* The x*max_y will fit into 64bits. */ |
| 71 | const uint64_t max_y = std::numeric_limits<uint64_t>::max() / x; |
| 72 | |
| 73 | test_multiply_uint64(x, max_y); |
| 74 | for (int j = 0; j < 10; j++) { |
| 75 | const uint64_t y = (ut::random_64() >> 2) % (max_y + 1); |
| 76 | test_multiply_uint64(x, y); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* Correctness of the multiply_uint64_portable tested using Chinese Rest |
| 82 | Theorem. */ |
| 83 | TEST(ut0math, multiply_uint64_portable_chinese) { |
| 84 | for (int k = 0; k < 100; k++) { |
| 85 | /* Choose a random 32bit prime and calculate 2^64 % P. This value will be |
| 86 | used to calculate value of (128bit integer % P). ut::find_prime finds a |
| 87 | prime bigger than specified argument, so we pass value shifted by more than |
| 88 | 32 bits. */ |
| 89 | const uint64_t P = ut::find_prime(ut::random_64() >> 34); |
| 90 | ASSERT_LT(P, 1ULL << 32); |
| 91 | const uint64_t two_to_32_mod_P = (1ULL << 32) % P; |
| 92 | const uint64_t two_to_64_mod_P = (two_to_32_mod_P * two_to_32_mod_P) % P; |
| 93 | for (int i = 0; i < 10000; i++) { |
| 94 | const uint64_t x = ut::random_64(); |
| 95 | const uint64_t y = ut::random_64(); |
| 96 | |
| 97 | uint64_t hi; |
| 98 | const uint64_t lo = ut::detail::multiply_uint64_portable(x, y, hi); |
| 99 | /* Does the result agree modulo P? */ |
| 100 | const uint64_t expected = ((x % P) * (y % P)) % P; |
| 101 | const uint64_t actual = (hi % P * two_to_64_mod_P % P + lo % P) % P; |
| 102 | |
| 103 | ASSERT_EQ(actual, expected); |
| 104 | /* Does the result agree modulo 2^64? */ |
| 105 | ASSERT_EQ(lo, static_cast<uint64_t>(x * y)); |
| 106 | /* If the above two conditions are met, then it means the result is |
| 107 | correct modulo (P * 2^64). We could add a second random prime Q to mix |
| 108 | to have the multiplication result checked against (P * Q * 2^64), which |
| 109 | would be almost 2^128 bit number, but the 96bits we have now seems enough |
| 110 | having the P is chosen randomly. */ |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | TEST(ut0math, divide_uint128) { |
| 116 | for (int i = 0; i < 1000000; i++) { |
| 117 | const uint64_t x = ut::random_64(); |
| 118 | const uint64_t y = ut::random_64(); |
| 119 | if (x == 0 || y == 0) continue; |
| 120 | uint64_t high; |
| 121 | uint64_t low = ut::detail::multiply_uint64_portable(x, y, high); |
| 122 | ASSERT_EQ(ut::divide_128(high, low, x), y); |
| 123 | ASSERT_EQ(ut::divide_128(high, low, y), x); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | TEST(ut0math, fast_modulo) { |
| 128 | for (int i = 0; i < 1000000; i++) { |
| 129 | const uint64_t x = ut::random_64(); |
| 130 | const uint64_t y = ut::random_64(); |
| 131 | if (y == 0) continue; |
| 132 | ut::fast_modulo_t mod{y}; |
| 133 | ASSERT_EQ(x % y, x % mod); |
| 134 | } |
| 135 | for (int i = 0; i < 1000000; i++) { |
| 136 | const uint64_t x = ut::random_64(); |
| 137 | const uint64_t y = ut::random_64() % 1000 + 1; |
| 138 | ASSERT_EQ(x % y, x % ut::fast_modulo_t{y}); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* Micro-benchmark raw fast modulo performance. */ |
| 143 | |
| 144 | static void BM_FAST_MODULO_CALCULATE(const size_t num_iterations) { |
| 145 | uint32_t fold = 0; |
| 146 | ut::fast_modulo_t mod{123 + num_iterations}; |
| 147 | for (size_t n = 0; n < num_iterations * 1000; n++) { |
| 148 | fold += n % mod; |
| 149 | } |
| 150 | EXPECT_NE(0U, fold); // To keep the compiler from optimizing it away. |
| 151 | SetBytesProcessed(num_iterations * 1000); |
| 152 | } |
| 153 | BENCHMARK(BM_FAST_MODULO_CALCULATE) |
| 154 | |
| 155 | static void BM_MODULO_CALCULATE_CONSTEXPR_MOD(const size_t num_iterations) { |
| 156 | uint32_t fold = 0; |
| 157 | constexpr uint64_t mod{123}; |
| 158 | for (size_t n = 0; n < num_iterations * 1000; n++) { |
| 159 | fold += n % mod; |
| 160 | } |
| 161 | EXPECT_NE(0U, fold); // To keep the compiler from optimizing it away. |
| 162 | SetBytesProcessed(num_iterations * 1000); |
| 163 | } |
| 164 | BENCHMARK(BM_MODULO_CALCULATE_CONSTEXPR_MOD) |
| 165 | |
| 166 | static void BM_MODULO_CALCULATE_VARIABLE_MOD(const size_t num_iterations) { |
| 167 | uint32_t fold = 0; |
| 168 | const uint64_t mod{123 + num_iterations}; |
| 169 | for (size_t n = 0; n < num_iterations * 1000; n++) { |
| 170 | fold += n % mod; |
| 171 | } |
| 172 | EXPECT_NE(0U, fold); // To keep the compiler from optimizing it away. |
| 173 | SetBytesProcessed(num_iterations * 1000); |
| 174 | } |
| 175 | BENCHMARK(BM_MODULO_CALCULATE_VARIABLE_MOD) |
| 176 | |
| 177 | } // namespace innodb_ut0math_unittest |