Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 15 | #include "absl/random/internal/generate_real.h" |
| 16 | |
| 17 | #include <cfloat> |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | #include <string> |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 21 | |
| 22 | #include "gtest/gtest.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 23 | #include "absl/flags/flag.h" |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 24 | #include "absl/numeric/bits.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 25 | |
| 26 | ABSL_FLAG(int64_t, absl_random_test_trials, 50000, |
| 27 | "Number of trials for the probability tests."); |
| 28 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 29 | using absl::random_internal::GenerateNegativeTag; |
| 30 | using absl::random_internal::GeneratePositiveTag; |
| 31 | using absl::random_internal::GenerateRealFromBits; |
| 32 | using absl::random_internal::GenerateSignedTag; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 36 | TEST(GenerateRealTest, U64ToFloat_Positive_NoZero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 37 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 38 | return GenerateRealFromBits<float, GeneratePositiveTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 39 | }; |
| 40 | EXPECT_EQ(ToFloat(0x0000000000000000), 2.710505431e-20f); |
| 41 | EXPECT_EQ(ToFloat(0x0000000000000001), 5.421010862e-20f); |
| 42 | EXPECT_EQ(ToFloat(0x8000000000000000), 0.5); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 43 | EXPECT_EQ(ToFloat(0x8000000000000001), 0.5); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 44 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 0.9999999404f); |
| 45 | } |
| 46 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 47 | TEST(GenerateRealTest, U64ToFloat_Positive_Zero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 48 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 49 | return GenerateRealFromBits<float, GeneratePositiveTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 50 | }; |
| 51 | EXPECT_EQ(ToFloat(0x0000000000000000), 0.0); |
| 52 | EXPECT_EQ(ToFloat(0x0000000000000001), 5.421010862e-20f); |
| 53 | EXPECT_EQ(ToFloat(0x8000000000000000), 0.5); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 54 | EXPECT_EQ(ToFloat(0x8000000000000001), 0.5); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 55 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 0.9999999404f); |
| 56 | } |
| 57 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 58 | TEST(GenerateRealTest, U64ToFloat_Negative_NoZero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 59 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 60 | return GenerateRealFromBits<float, GenerateNegativeTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 61 | }; |
| 62 | EXPECT_EQ(ToFloat(0x0000000000000000), -2.710505431e-20f); |
| 63 | EXPECT_EQ(ToFloat(0x0000000000000001), -5.421010862e-20f); |
| 64 | EXPECT_EQ(ToFloat(0x8000000000000000), -0.5); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 65 | EXPECT_EQ(ToFloat(0x8000000000000001), -0.5); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 66 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f); |
| 67 | } |
| 68 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 69 | TEST(GenerateRealTest, U64ToFloat_Negative_Zero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 70 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 71 | return GenerateRealFromBits<float, GenerateNegativeTag, true>(a); |
| 72 | }; |
| 73 | EXPECT_EQ(ToFloat(0x0000000000000000), 0.0); |
| 74 | EXPECT_EQ(ToFloat(0x0000000000000001), -5.421010862e-20f); |
| 75 | EXPECT_EQ(ToFloat(0x8000000000000000), -0.5); |
| 76 | EXPECT_EQ(ToFloat(0x8000000000000001), -0.5); |
| 77 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f); |
| 78 | } |
| 79 | |
| 80 | TEST(GenerateRealTest, U64ToFloat_Signed_NoZero_Test) { |
| 81 | auto ToFloat = [](uint64_t a) { |
| 82 | return GenerateRealFromBits<float, GenerateSignedTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 83 | }; |
| 84 | EXPECT_EQ(ToFloat(0x0000000000000000), 5.421010862e-20f); |
| 85 | EXPECT_EQ(ToFloat(0x0000000000000001), 1.084202172e-19f); |
| 86 | EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), 0.9999999404f); |
| 87 | EXPECT_EQ(ToFloat(0x8000000000000000), -5.421010862e-20f); |
| 88 | EXPECT_EQ(ToFloat(0x8000000000000001), -1.084202172e-19f); |
| 89 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f); |
| 90 | } |
| 91 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 92 | TEST(GenerateRealTest, U64ToFloat_Signed_Zero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 93 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 94 | return GenerateRealFromBits<float, GenerateSignedTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 95 | }; |
| 96 | EXPECT_EQ(ToFloat(0x0000000000000000), 0); |
| 97 | EXPECT_EQ(ToFloat(0x0000000000000001), 1.084202172e-19f); |
| 98 | EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), 0.9999999404f); |
| 99 | EXPECT_EQ(ToFloat(0x8000000000000000), 0); |
| 100 | EXPECT_EQ(ToFloat(0x8000000000000001), -1.084202172e-19f); |
| 101 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f); |
| 102 | } |
| 103 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 104 | TEST(GenerateRealTest, U64ToFloat_Signed_Bias_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 105 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 106 | return GenerateRealFromBits<float, GenerateSignedTag, true>(a, 1); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 107 | }; |
| 108 | EXPECT_EQ(ToFloat(0x0000000000000000), 0); |
| 109 | EXPECT_EQ(ToFloat(0x0000000000000001), 2 * 1.084202172e-19f); |
| 110 | EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), 2 * 0.9999999404f); |
| 111 | EXPECT_EQ(ToFloat(0x8000000000000000), 0); |
| 112 | EXPECT_EQ(ToFloat(0x8000000000000001), 2 * -1.084202172e-19f); |
| 113 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 2 * -0.9999999404f); |
| 114 | } |
| 115 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 116 | TEST(GenerateRealTest, U64ToFloatTest) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 117 | auto ToFloat = [](uint64_t a) -> float { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 118 | return GenerateRealFromBits<float, GeneratePositiveTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | EXPECT_EQ(ToFloat(0x0000000000000000), 0.0f); |
| 122 | |
| 123 | EXPECT_EQ(ToFloat(0x8000000000000000), 0.5f); |
| 124 | EXPECT_EQ(ToFloat(0x8000000000000001), 0.5f); |
| 125 | EXPECT_EQ(ToFloat(0x800000FFFFFFFFFF), 0.5f); |
| 126 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 0.9999999404f); |
| 127 | |
| 128 | EXPECT_GT(ToFloat(0x0000000000000001), 0.0f); |
| 129 | |
| 130 | EXPECT_NE(ToFloat(0x7FFFFF0000000000), ToFloat(0x7FFFFEFFFFFFFFFF)); |
| 131 | |
| 132 | EXPECT_LT(ToFloat(0xFFFFFFFFFFFFFFFF), 1.0f); |
| 133 | int32_t two_to_24 = 1 << 24; |
| 134 | EXPECT_EQ(static_cast<int32_t>(ToFloat(0xFFFFFFFFFFFFFFFF) * two_to_24), |
| 135 | two_to_24 - 1); |
| 136 | EXPECT_NE(static_cast<int32_t>(ToFloat(0xFFFFFFFFFFFFFFFF) * two_to_24 * 2), |
| 137 | two_to_24 * 2 - 1); |
| 138 | EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), ToFloat(0xFFFFFF0000000000)); |
| 139 | EXPECT_NE(ToFloat(0xFFFFFFFFFFFFFFFF), ToFloat(0xFFFFFEFFFFFFFFFF)); |
| 140 | EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), ToFloat(0x7FFFFF8000000000)); |
| 141 | EXPECT_NE(ToFloat(0x7FFFFFFFFFFFFFFF), ToFloat(0x7FFFFF7FFFFFFFFF)); |
| 142 | EXPECT_EQ(ToFloat(0x3FFFFFFFFFFFFFFF), ToFloat(0x3FFFFFC000000000)); |
| 143 | EXPECT_NE(ToFloat(0x3FFFFFFFFFFFFFFF), ToFloat(0x3FFFFFBFFFFFFFFF)); |
| 144 | |
| 145 | // For values where every bit counts, the values scale as multiples of the |
| 146 | // input. |
| 147 | for (int i = 0; i < 100; ++i) { |
| 148 | EXPECT_EQ(i * ToFloat(0x0000000000000001), ToFloat(i)); |
| 149 | } |
| 150 | |
| 151 | // For each i: value generated from (1 << i). |
| 152 | float exp_values[64]; |
| 153 | exp_values[63] = 0.5f; |
| 154 | for (int i = 62; i >= 0; --i) exp_values[i] = 0.5f * exp_values[i + 1]; |
| 155 | constexpr uint64_t one = 1; |
| 156 | for (int i = 0; i < 64; ++i) { |
| 157 | EXPECT_EQ(ToFloat(one << i), exp_values[i]); |
| 158 | for (int j = 1; j < FLT_MANT_DIG && i - j >= 0; ++j) { |
| 159 | EXPECT_NE(exp_values[i] + exp_values[i - j], exp_values[i]); |
| 160 | EXPECT_EQ(ToFloat((one << i) + (one << (i - j))), |
| 161 | exp_values[i] + exp_values[i - j]); |
| 162 | } |
| 163 | for (int j = FLT_MANT_DIG; i - j >= 0; ++j) { |
| 164 | EXPECT_EQ(exp_values[i] + exp_values[i - j], exp_values[i]); |
| 165 | EXPECT_EQ(ToFloat((one << i) + (one << (i - j))), exp_values[i]); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 170 | TEST(GenerateRealTest, U64ToDouble_Positive_NoZero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 171 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 172 | return GenerateRealFromBits<double, GeneratePositiveTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | EXPECT_EQ(ToDouble(0x0000000000000000), 2.710505431213761085e-20); |
| 176 | EXPECT_EQ(ToDouble(0x0000000000000001), 5.42101086242752217004e-20); |
| 177 | EXPECT_EQ(ToDouble(0x0000000000000002), 1.084202172485504434e-19); |
| 178 | EXPECT_EQ(ToDouble(0x8000000000000000), 0.5); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 179 | EXPECT_EQ(ToDouble(0x8000000000000001), 0.5); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 180 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), 0.999999999999999888978); |
| 181 | } |
| 182 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 183 | TEST(GenerateRealTest, U64ToDouble_Positive_Zero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 184 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 185 | return GenerateRealFromBits<double, GeneratePositiveTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | EXPECT_EQ(ToDouble(0x0000000000000000), 0.0); |
| 189 | EXPECT_EQ(ToDouble(0x0000000000000001), 5.42101086242752217004e-20); |
| 190 | EXPECT_EQ(ToDouble(0x8000000000000000), 0.5); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 191 | EXPECT_EQ(ToDouble(0x8000000000000001), 0.5); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 192 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), 0.999999999999999888978); |
| 193 | } |
| 194 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 195 | TEST(GenerateRealTest, U64ToDouble_Negative_NoZero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 196 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 197 | return GenerateRealFromBits<double, GenerateNegativeTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | EXPECT_EQ(ToDouble(0x0000000000000000), -2.710505431213761085e-20); |
| 201 | EXPECT_EQ(ToDouble(0x0000000000000001), -5.42101086242752217004e-20); |
| 202 | EXPECT_EQ(ToDouble(0x0000000000000002), -1.084202172485504434e-19); |
| 203 | EXPECT_EQ(ToDouble(0x8000000000000000), -0.5); |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 204 | EXPECT_EQ(ToDouble(0x8000000000000001), -0.5); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 205 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978); |
| 206 | } |
| 207 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 208 | TEST(GenerateRealTest, U64ToDouble_Negative_Zero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 209 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 210 | return GenerateRealFromBits<double, GenerateNegativeTag, true>(a); |
| 211 | }; |
| 212 | |
| 213 | EXPECT_EQ(ToDouble(0x0000000000000000), 0.0); |
| 214 | EXPECT_EQ(ToDouble(0x0000000000000001), -5.42101086242752217004e-20); |
| 215 | EXPECT_EQ(ToDouble(0x0000000000000002), -1.084202172485504434e-19); |
| 216 | EXPECT_EQ(ToDouble(0x8000000000000000), -0.5); |
| 217 | EXPECT_EQ(ToDouble(0x8000000000000001), -0.5); |
| 218 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978); |
| 219 | } |
| 220 | |
| 221 | TEST(GenerateRealTest, U64ToDouble_Signed_NoZero_Test) { |
| 222 | auto ToDouble = [](uint64_t a) { |
| 223 | return GenerateRealFromBits<double, GenerateSignedTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | EXPECT_EQ(ToDouble(0x0000000000000000), 5.42101086242752217004e-20); |
| 227 | EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19); |
| 228 | EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), 0.999999999999999888978); |
| 229 | EXPECT_EQ(ToDouble(0x8000000000000000), -5.42101086242752217004e-20); |
| 230 | EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19); |
| 231 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978); |
| 232 | } |
| 233 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 234 | TEST(GenerateRealTest, U64ToDouble_Signed_Zero_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 235 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 236 | return GenerateRealFromBits<double, GenerateSignedTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 237 | }; |
| 238 | EXPECT_EQ(ToDouble(0x0000000000000000), 0); |
| 239 | EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19); |
| 240 | EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), 0.999999999999999888978); |
| 241 | EXPECT_EQ(ToDouble(0x8000000000000000), 0); |
| 242 | EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19); |
| 243 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978); |
| 244 | } |
| 245 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 246 | TEST(GenerateRealTest, U64ToDouble_GenerateSignedTag_Bias_Test) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 247 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 248 | return GenerateRealFromBits<double, GenerateSignedTag, true>(a, -1); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 249 | }; |
| 250 | EXPECT_EQ(ToDouble(0x0000000000000000), 0); |
| 251 | EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19 / 2); |
| 252 | EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), 0.999999999999999888978 / 2); |
| 253 | EXPECT_EQ(ToDouble(0x8000000000000000), 0); |
| 254 | EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19 / 2); |
| 255 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978 / 2); |
| 256 | } |
| 257 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 258 | TEST(GenerateRealTest, U64ToDoubleTest) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 259 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 260 | return GenerateRealFromBits<double, GeneratePositiveTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | EXPECT_EQ(ToDouble(0x0000000000000000), 0.0); |
| 264 | EXPECT_EQ(ToDouble(0x0000000000000000), 0.0); |
| 265 | |
| 266 | EXPECT_EQ(ToDouble(0x0000000000000001), 5.42101086242752217004e-20); |
| 267 | EXPECT_EQ(ToDouble(0x7fffffffffffffef), 0.499999999999999944489); |
| 268 | EXPECT_EQ(ToDouble(0x8000000000000000), 0.5); |
| 269 | |
| 270 | // For values > 0.5, RandU64ToDouble discards up to 11 bits. (64-53). |
| 271 | EXPECT_EQ(ToDouble(0x8000000000000001), 0.5); |
| 272 | EXPECT_EQ(ToDouble(0x80000000000007FF), 0.5); |
| 273 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), 0.999999999999999888978); |
| 274 | EXPECT_NE(ToDouble(0x7FFFFFFFFFFFF800), ToDouble(0x7FFFFFFFFFFFF7FF)); |
| 275 | |
| 276 | EXPECT_LT(ToDouble(0xFFFFFFFFFFFFFFFF), 1.0); |
| 277 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), ToDouble(0xFFFFFFFFFFFFF800)); |
| 278 | EXPECT_NE(ToDouble(0xFFFFFFFFFFFFFFFF), ToDouble(0xFFFFFFFFFFFFF7FF)); |
| 279 | EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFFC00)); |
| 280 | EXPECT_NE(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFFBFF)); |
| 281 | EXPECT_EQ(ToDouble(0x3FFFFFFFFFFFFFFF), ToDouble(0x3FFFFFFFFFFFFE00)); |
| 282 | EXPECT_NE(ToDouble(0x3FFFFFFFFFFFFFFF), ToDouble(0x3FFFFFFFFFFFFDFF)); |
| 283 | |
| 284 | EXPECT_EQ(ToDouble(0x1000000000000001), 0.0625); |
| 285 | EXPECT_EQ(ToDouble(0x2000000000000001), 0.125); |
| 286 | EXPECT_EQ(ToDouble(0x3000000000000001), 0.1875); |
| 287 | EXPECT_EQ(ToDouble(0x4000000000000001), 0.25); |
| 288 | EXPECT_EQ(ToDouble(0x5000000000000001), 0.3125); |
| 289 | EXPECT_EQ(ToDouble(0x6000000000000001), 0.375); |
| 290 | EXPECT_EQ(ToDouble(0x7000000000000001), 0.4375); |
| 291 | EXPECT_EQ(ToDouble(0x8000000000000001), 0.5); |
| 292 | EXPECT_EQ(ToDouble(0x9000000000000001), 0.5625); |
| 293 | EXPECT_EQ(ToDouble(0xa000000000000001), 0.625); |
| 294 | EXPECT_EQ(ToDouble(0xb000000000000001), 0.6875); |
| 295 | EXPECT_EQ(ToDouble(0xc000000000000001), 0.75); |
| 296 | EXPECT_EQ(ToDouble(0xd000000000000001), 0.8125); |
| 297 | EXPECT_EQ(ToDouble(0xe000000000000001), 0.875); |
| 298 | EXPECT_EQ(ToDouble(0xf000000000000001), 0.9375); |
| 299 | |
| 300 | // Large powers of 2. |
| 301 | int64_t two_to_53 = int64_t{1} << 53; |
| 302 | EXPECT_EQ(static_cast<int64_t>(ToDouble(0xFFFFFFFFFFFFFFFF) * two_to_53), |
| 303 | two_to_53 - 1); |
| 304 | EXPECT_NE(static_cast<int64_t>(ToDouble(0xFFFFFFFFFFFFFFFF) * two_to_53 * 2), |
| 305 | two_to_53 * 2 - 1); |
| 306 | |
| 307 | // For values where every bit counts, the values scale as multiples of the |
| 308 | // input. |
| 309 | for (int i = 0; i < 100; ++i) { |
| 310 | EXPECT_EQ(i * ToDouble(0x0000000000000001), ToDouble(i)); |
| 311 | } |
| 312 | |
| 313 | // For each i: value generated from (1 << i). |
| 314 | double exp_values[64]; |
| 315 | exp_values[63] = 0.5; |
| 316 | for (int i = 62; i >= 0; --i) exp_values[i] = 0.5 * exp_values[i + 1]; |
| 317 | constexpr uint64_t one = 1; |
| 318 | for (int i = 0; i < 64; ++i) { |
| 319 | EXPECT_EQ(ToDouble(one << i), exp_values[i]); |
| 320 | for (int j = 1; j < DBL_MANT_DIG && i - j >= 0; ++j) { |
| 321 | EXPECT_NE(exp_values[i] + exp_values[i - j], exp_values[i]); |
| 322 | EXPECT_EQ(ToDouble((one << i) + (one << (i - j))), |
| 323 | exp_values[i] + exp_values[i - j]); |
| 324 | } |
| 325 | for (int j = DBL_MANT_DIG; i - j >= 0; ++j) { |
| 326 | EXPECT_EQ(exp_values[i] + exp_values[i - j], exp_values[i]); |
| 327 | EXPECT_EQ(ToDouble((one << i) + (one << (i - j))), exp_values[i]); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 332 | TEST(GenerateRealTest, U64ToDoubleSignedTest) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 333 | auto ToDouble = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 334 | return GenerateRealFromBits<double, GenerateSignedTag, false>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | EXPECT_EQ(ToDouble(0x0000000000000000), 5.42101086242752217004e-20); |
| 338 | EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19); |
| 339 | |
| 340 | EXPECT_EQ(ToDouble(0x8000000000000000), -5.42101086242752217004e-20); |
| 341 | EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19); |
| 342 | |
| 343 | const double e_plus = ToDouble(0x0000000000000001); |
| 344 | const double e_minus = ToDouble(0x8000000000000001); |
| 345 | EXPECT_EQ(e_plus, 1.084202172485504434e-19); |
| 346 | EXPECT_EQ(e_minus, -1.084202172485504434e-19); |
| 347 | |
| 348 | EXPECT_EQ(ToDouble(0x3fffffffffffffef), 0.499999999999999944489); |
| 349 | EXPECT_EQ(ToDouble(0xbfffffffffffffef), -0.499999999999999944489); |
| 350 | |
| 351 | // For values > 0.5, RandU64ToDouble discards up to 10 bits. (63-53). |
| 352 | EXPECT_EQ(ToDouble(0x4000000000000000), 0.5); |
| 353 | EXPECT_EQ(ToDouble(0x4000000000000001), 0.5); |
| 354 | EXPECT_EQ(ToDouble(0x40000000000003FF), 0.5); |
| 355 | |
| 356 | EXPECT_EQ(ToDouble(0xC000000000000000), -0.5); |
| 357 | EXPECT_EQ(ToDouble(0xC000000000000001), -0.5); |
| 358 | EXPECT_EQ(ToDouble(0xC0000000000003FF), -0.5); |
| 359 | |
| 360 | EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFe), 0.999999999999999888978); |
| 361 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFe), -0.999999999999999888978); |
| 362 | |
| 363 | EXPECT_NE(ToDouble(0x7FFFFFFFFFFFF800), ToDouble(0x7FFFFFFFFFFFF7FF)); |
| 364 | |
| 365 | EXPECT_LT(ToDouble(0x7FFFFFFFFFFFFFFF), 1.0); |
| 366 | EXPECT_GT(ToDouble(0x7FFFFFFFFFFFFFFF), 0.9999999999); |
| 367 | |
| 368 | EXPECT_GT(ToDouble(0xFFFFFFFFFFFFFFFe), -1.0); |
| 369 | EXPECT_LT(ToDouble(0xFFFFFFFFFFFFFFFe), -0.999999999); |
| 370 | |
| 371 | EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFe), ToDouble(0xFFFFFFFFFFFFFC00)); |
| 372 | EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFFC00)); |
| 373 | EXPECT_NE(ToDouble(0xFFFFFFFFFFFFFFFe), ToDouble(0xFFFFFFFFFFFFF3FF)); |
| 374 | EXPECT_NE(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFF3FF)); |
| 375 | |
| 376 | EXPECT_EQ(ToDouble(0x1000000000000001), 0.125); |
| 377 | EXPECT_EQ(ToDouble(0x2000000000000001), 0.25); |
| 378 | EXPECT_EQ(ToDouble(0x3000000000000001), 0.375); |
| 379 | EXPECT_EQ(ToDouble(0x4000000000000001), 0.5); |
| 380 | EXPECT_EQ(ToDouble(0x5000000000000001), 0.625); |
| 381 | EXPECT_EQ(ToDouble(0x6000000000000001), 0.75); |
| 382 | EXPECT_EQ(ToDouble(0x7000000000000001), 0.875); |
| 383 | EXPECT_EQ(ToDouble(0x7800000000000001), 0.9375); |
| 384 | EXPECT_EQ(ToDouble(0x7c00000000000001), 0.96875); |
| 385 | EXPECT_EQ(ToDouble(0x7e00000000000001), 0.984375); |
| 386 | EXPECT_EQ(ToDouble(0x7f00000000000001), 0.9921875); |
| 387 | |
| 388 | // 0x8000000000000000 ~= 0 |
| 389 | EXPECT_EQ(ToDouble(0x9000000000000001), -0.125); |
| 390 | EXPECT_EQ(ToDouble(0xa000000000000001), -0.25); |
| 391 | EXPECT_EQ(ToDouble(0xb000000000000001), -0.375); |
| 392 | EXPECT_EQ(ToDouble(0xc000000000000001), -0.5); |
| 393 | EXPECT_EQ(ToDouble(0xd000000000000001), -0.625); |
| 394 | EXPECT_EQ(ToDouble(0xe000000000000001), -0.75); |
| 395 | EXPECT_EQ(ToDouble(0xf000000000000001), -0.875); |
| 396 | |
| 397 | // Large powers of 2. |
| 398 | int64_t two_to_53 = int64_t{1} << 53; |
| 399 | EXPECT_EQ(static_cast<int64_t>(ToDouble(0x7FFFFFFFFFFFFFFF) * two_to_53), |
| 400 | two_to_53 - 1); |
| 401 | EXPECT_EQ(static_cast<int64_t>(ToDouble(0xFFFFFFFFFFFFFFFF) * two_to_53), |
| 402 | -(two_to_53 - 1)); |
| 403 | |
| 404 | EXPECT_NE(static_cast<int64_t>(ToDouble(0x7FFFFFFFFFFFFFFF) * two_to_53 * 2), |
| 405 | two_to_53 * 2 - 1); |
| 406 | |
| 407 | // For values where every bit counts, the values scale as multiples of the |
| 408 | // input. |
| 409 | for (int i = 1; i < 100; ++i) { |
| 410 | EXPECT_EQ(i * e_plus, ToDouble(i)) << i; |
| 411 | EXPECT_EQ(i * e_minus, ToDouble(0x8000000000000000 | i)) << i; |
| 412 | } |
| 413 | } |
| 414 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 415 | TEST(GenerateRealTest, ExhaustiveFloat) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 416 | auto ToFloat = [](uint64_t a) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 417 | return GenerateRealFromBits<float, GeneratePositiveTag, true>(a); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
| 420 | // Rely on RandU64ToFloat generating values from greatest to least when |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 421 | // supplied with uint64_t values from greatest (0xfff...) to least (0x0). |
| 422 | // Thus, this algorithm stores the previous value, and if the new value is at |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 423 | // greater than or equal to the previous value, then there is a collision in |
| 424 | // the generation algorithm. |
| 425 | // |
| 426 | // Use the computation below to convert the random value into a result: |
| 427 | // double res = a() * (1.0f - sample) + b() * sample; |
| 428 | float last_f = 1.0, last_g = 2.0; |
| 429 | uint64_t f_collisions = 0, g_collisions = 0; |
| 430 | uint64_t f_unique = 0, g_unique = 0; |
| 431 | uint64_t total = 0; |
| 432 | auto count = [&](const float r) { |
| 433 | total++; |
| 434 | // `f` is mapped to the range [0, 1) (default) |
| 435 | const float f = 0.0f * (1.0f - r) + 1.0f * r; |
| 436 | if (f >= last_f) { |
| 437 | f_collisions++; |
| 438 | } else { |
| 439 | f_unique++; |
| 440 | last_f = f; |
| 441 | } |
| 442 | // `g` is mapped to the range [1, 2) |
| 443 | const float g = 1.0f * (1.0f - r) + 2.0f * r; |
| 444 | if (g >= last_g) { |
| 445 | g_collisions++; |
| 446 | } else { |
| 447 | g_unique++; |
| 448 | last_g = g; |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | size_t limit = absl::GetFlag(FLAGS_absl_random_test_trials); |
| 453 | |
| 454 | // Generate all uint64_t which have unique floating point values. |
| 455 | // Counting down from 0xFFFFFFFFFFFFFFFFu ... 0x0u |
| 456 | uint64_t x = ~uint64_t(0); |
| 457 | for (; x != 0 && limit > 0;) { |
| 458 | constexpr int kDig = (64 - FLT_MANT_DIG); |
| 459 | // Set a decrement value & the next point at which to change |
| 460 | // the decrement value. By default these are 1, 0. |
| 461 | uint64_t dec = 1; |
| 462 | uint64_t chk = 0; |
| 463 | |
| 464 | // Adjust decrement and check value based on how many leading 0 |
| 465 | // bits are set in the current value. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 466 | const int clz = absl::countl_zero(x); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 467 | if (clz < kDig) { |
| 468 | dec <<= (kDig - clz); |
| 469 | chk = (~uint64_t(0)) >> (clz + 1); |
| 470 | } |
| 471 | for (; x > chk && limit > 0; x -= dec) { |
| 472 | count(ToFloat(x)); |
| 473 | --limit; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | static_assert(FLT_MANT_DIG == 24, |
| 478 | "The float type is expected to have a 24 bit mantissa."); |
| 479 | |
| 480 | if (limit != 0) { |
| 481 | // There are between 2^28 and 2^29 unique values in the range [0, 1). For |
| 482 | // the low values of x, there are 2^24 -1 unique values. Once x > 2^24, |
| 483 | // there are 40 * 2^24 unique values. Thus: |
| 484 | // (2 + 4 + 8 ... + 2^23) + 40 * 2^23 |
| 485 | EXPECT_LT(1 << 28, f_unique); |
| 486 | EXPECT_EQ((1 << 24) + 40 * (1 << 23) - 1, f_unique); |
| 487 | EXPECT_EQ(total, f_unique); |
| 488 | EXPECT_EQ(0, f_collisions); |
| 489 | |
| 490 | // Expect at least 2^23 unique values for the range [1, 2) |
| 491 | EXPECT_LE(1 << 23, g_unique); |
| 492 | EXPECT_EQ(total - g_unique, g_collisions); |
| 493 | } |
| 494 | } |
| 495 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 496 | } // namespace |