| 1 | // Copyright 2019 Google LLC |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include <stddef.h> |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include <cmath> // std::isfinite |
| 20 | |
| 21 | #undef HWY_TARGET_INCLUDE |
| 22 | #define HWY_TARGET_INCLUDE "tests/demote_test.cc" |
| 23 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 24 | #include "hwy/highway.h" |
| 25 | #include "hwy/tests/test_util-inl.h" |
| 26 | |
| 27 | // Causes build timeout. |
| 28 | #if !HWY_IS_MSAN |
| 29 | |
| 30 | HWY_BEFORE_NAMESPACE(); |
| 31 | namespace hwy { |
| 32 | namespace HWY_NAMESPACE { |
| 33 | |
| 34 | template <typename T, HWY_IF_FLOAT(T)> |
| 35 | bool IsFiniteT(T t) { |
| 36 | return std::isfinite(t); |
| 37 | } |
| 38 | // Wrapper avoids calling std::isfinite for integer types (ambiguous). |
| 39 | template <typename T, HWY_IF_NOT_FLOAT(T)> |
| 40 | bool IsFiniteT(T /*unused*/) { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | template <typename ToT> |
| 45 | struct TestDemoteTo { |
| 46 | template <typename T, class D> |
| 47 | HWY_NOINLINE void operator()(T /*unused*/, D from_d) { |
| 48 | static_assert(!IsFloat<ToT>(), "Use TestDemoteToFloat for float output"); |
| 49 | static_assert(sizeof(T) > sizeof(ToT), "Input type must be wider"); |
| 50 | const Rebind<ToT, D> to_d; |
| 51 | |
| 52 | const size_t N = Lanes(from_d); |
| 53 | auto from = AllocateAligned<T>(N); |
| 54 | auto expected = AllocateAligned<ToT>(N); |
| 55 | |
| 56 | // Narrower range in the wider type, for clamping before we cast |
| 57 | const T min = LimitsMin<ToT>(); |
| 58 | const T max = LimitsMax<ToT>(); |
| 59 | |
| 60 | const auto value_ok = [&](T& value) { |
| 61 | if (!IsFiniteT(value)) return false; |
| 62 | return true; |
| 63 | }; |
| 64 | |
| 65 | RandomState rng; |
| 66 | for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) { |
| 67 | for (size_t i = 0; i < N; ++i) { |
| 68 | do { |
| 69 | const uint64_t bits = rng(); |
| 70 | CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size |
| 71 | } while (!value_ok(from[i])); |
| 72 | expected[i] = static_cast<ToT>(HWY_MIN(HWY_MAX(min, from[i]), max)); |
| 73 | } |
| 74 | |
| 75 | const auto in = Load(from_d, from.get()); |
| 76 | HWY_ASSERT_VEC_EQ(to_d, expected.get(), DemoteTo(to_d, in)); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | HWY_NOINLINE void TestAllDemoteToInt() { |
| 82 | ForDemoteVectors<TestDemoteTo<uint8_t>>()(int16_t()); |
| 83 | ForDemoteVectors<TestDemoteTo<uint8_t>, 2>()(int32_t()); |
| 84 | |
| 85 | ForDemoteVectors<TestDemoteTo<int8_t>>()(int16_t()); |
| 86 | ForDemoteVectors<TestDemoteTo<int8_t>, 2>()(int32_t()); |
| 87 | |
| 88 | const ForDemoteVectors<TestDemoteTo<uint16_t>> to_u16; |
| 89 | to_u16(int32_t()); |
| 90 | |
| 91 | const ForDemoteVectors<TestDemoteTo<int16_t>> to_i16; |
| 92 | to_i16(int32_t()); |
| 93 | } |
| 94 | |
| 95 | HWY_NOINLINE void TestAllDemoteToMixed() { |
| 96 | #if HWY_HAVE_FLOAT64 |
| 97 | const ForDemoteVectors<TestDemoteTo<int32_t>> to_i32; |
| 98 | to_i32(double()); |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | template <typename ToT> |
| 103 | struct TestDemoteToFloat { |
| 104 | template <typename T, class D> |
| 105 | HWY_NOINLINE void operator()(T /*unused*/, D from_d) { |
| 106 | // For floats, we clamp differently and cannot call LimitsMin. |
| 107 | static_assert(IsFloat<ToT>(), "Use TestDemoteTo for integer output"); |
| 108 | static_assert(sizeof(T) > sizeof(ToT), "Input type must be wider"); |
| 109 | const Rebind<ToT, D> to_d; |
| 110 | |
| 111 | const size_t N = Lanes(from_d); |
| 112 | auto from = AllocateAligned<T>(N); |
| 113 | auto expected = AllocateAligned<ToT>(N); |
| 114 | |
| 115 | RandomState rng; |
| 116 | for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) { |
| 117 | for (size_t i = 0; i < N; ++i) { |
| 118 | do { |
| 119 | const uint64_t bits = rng(); |
| 120 | CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size |
| 121 | } while (!IsFiniteT(from[i])); |
| 122 | const T magn = std::abs(from[i]); |
| 123 | const T max_abs = HighestValue<ToT>(); |
| 124 | // NOTE: std:: version from C++11 cmath is not defined in RVV GCC, see |
| 125 | // https://lists.freebsd.org/pipermail/freebsd-current/2014-January/048130.html |
| 126 | const T clipped = copysign(HWY_MIN(magn, max_abs), from[i]); |
| 127 | expected[i] = static_cast<ToT>(clipped); |
| 128 | } |
| 129 | |
| 130 | HWY_ASSERT_VEC_EQ(to_d, expected.get(), |
| 131 | DemoteTo(to_d, Load(from_d, from.get()))); |
| 132 | } |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | HWY_NOINLINE void TestAllDemoteToFloat() { |
| 137 | // Must test f16 separately because we can only load/store/convert them. |
| 138 | |
| 139 | #if HWY_HAVE_FLOAT64 |
| 140 | const ForDemoteVectors<TestDemoteToFloat<float>, 1> to_float; |
| 141 | to_float(double()); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | template <class D> |
| 146 | AlignedFreeUniquePtr<float[]> ReorderBF16TestCases(D d, size_t& padded) { |
| 147 | const float test_cases[] = { |
| 148 | // Same as BF16TestCases: |
| 149 | // +/- 1 |
| 150 | 1.0f, |
| 151 | -1.0f, |
| 152 | // +/- 0 |
| 153 | 0.0f, |
| 154 | -0.0f, |
| 155 | // near 0 |
| 156 | 0.25f, |
| 157 | -0.25f, |
| 158 | // +/- integer |
| 159 | 4.0f, |
| 160 | -32.0f, |
| 161 | // positive +/- delta |
| 162 | 2.015625f, |
| 163 | 3.984375f, |
| 164 | // negative +/- delta |
| 165 | -2.015625f, |
| 166 | -3.984375f, |
| 167 | |
| 168 | // No huge values - would interfere with sum. But add more to fill 2 * N: |
| 169 | -2.0f, |
| 170 | -10.0f, |
| 171 | 0.03125f, |
| 172 | 1.03125f, |
| 173 | 1.5f, |
| 174 | 2.0f, |
| 175 | 4.0f, |
| 176 | 5.0f, |
| 177 | 6.0f, |
| 178 | 8.0f, |
| 179 | 10.0f, |
| 180 | 256.0f, |
| 181 | 448.0f, |
| 182 | 2080.0f, |
| 183 | }; |
| 184 | const size_t kNumTestCases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 185 | const size_t N = Lanes(d); |
| 186 | padded = RoundUpTo(kNumTestCases, 2 * N); // allow loading pairs of vectors |
| 187 | auto in = AllocateAligned<float>(padded); |
| 188 | auto expected = AllocateAligned<float>(padded); |
| 189 | std::copy(test_cases, test_cases + kNumTestCases, in.get()); |
| 190 | std::fill(in.get() + kNumTestCases, in.get() + padded, 0.0f); |
| 191 | return in; |
| 192 | } |
| 193 | |
| 194 | class TestReorderDemote2To { |
| 195 | // In-place N^2 selection sort to avoid dependencies |
| 196 | void Sort(float* p, size_t count) { |
| 197 | for (size_t i = 0; i < count - 1; ++i) { |
| 198 | // Find min_element |
| 199 | size_t idx_min = i; |
| 200 | for (size_t j = i + 1; j < count; j++) { |
| 201 | if (p[j] < p[idx_min]) { |
| 202 | idx_min = j; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Swap with current |
| 207 | const float tmp = p[i]; |
| 208 | p[i] = p[idx_min]; |
| 209 | p[idx_min] = tmp; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | public: |
| 214 | template <typename TF32, class DF32> |
| 215 | HWY_NOINLINE void operator()(TF32 /*t*/, DF32 d32) { |
| 216 | #if HWY_TARGET != HWY_SCALAR |
| 217 | size_t padded; |
| 218 | auto in = ReorderBF16TestCases(d32, padded); |
| 219 | |
| 220 | using TBF16 = bfloat16_t; |
| 221 | const Repartition<TBF16, DF32> dbf16; |
| 222 | const Half<decltype(dbf16)> dbf16_half; |
| 223 | const size_t N = Lanes(d32); |
| 224 | auto temp16 = AllocateAligned<TBF16>(2 * N); |
| 225 | auto expected = AllocateAligned<float>(2 * N); |
| 226 | auto actual = AllocateAligned<float>(2 * N); |
| 227 | |
| 228 | for (size_t i = 0; i < padded; i += 2 * N) { |
| 229 | const auto f0 = Load(d32, &in[i + 0]); |
| 230 | const auto f1 = Load(d32, &in[i + N]); |
| 231 | const auto v16 = ReorderDemote2To(dbf16, f0, f1); |
| 232 | Store(v16, dbf16, temp16.get()); |
| 233 | const auto promoted0 = PromoteTo(d32, Load(dbf16_half, temp16.get() + 0)); |
| 234 | const auto promoted1 = PromoteTo(d32, Load(dbf16_half, temp16.get() + N)); |
| 235 | |
| 236 | // Smoke test: sum should be same (with tolerance for non-associativity) |
| 237 | const auto sum_expected = GetLane(SumOfLanes(d32, Add(f0, f1))); |
| 238 | const auto sum_actual = |
| 239 | GetLane(SumOfLanes(d32, Add(promoted0, promoted1))); |
| 240 | |
| 241 | HWY_ASSERT(sum_expected - 1E-4 <= sum_actual && |
| 242 | sum_actual <= sum_expected + 1E-4); |
| 243 | |
| 244 | // Ensure values are the same after sorting to undo the Reorder |
| 245 | Store(f0, d32, expected.get() + 0); |
| 246 | Store(f1, d32, expected.get() + N); |
| 247 | Store(promoted0, d32, actual.get() + 0); |
| 248 | Store(promoted1, d32, actual.get() + N); |
| 249 | Sort(expected.get(), 2 * N); |
| 250 | Sort(actual.get(), 2 * N); |
| 251 | HWY_ASSERT_VEC_EQ(d32, expected.get() + 0, Load(d32, actual.get() + 0)); |
| 252 | HWY_ASSERT_VEC_EQ(d32, expected.get() + N, Load(d32, actual.get() + N)); |
| 253 | } |
| 254 | #else // HWY_SCALAR |
| 255 | (void)d32; |
| 256 | #endif |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | HWY_NOINLINE void TestAllReorderDemote2To() { |
| 261 | ForShrinkableVectors<TestReorderDemote2To>()(float()); |
| 262 | } |
| 263 | |
| 264 | struct TestI32F64 { |
| 265 | template <typename TF, class DF> |
| 266 | HWY_NOINLINE void operator()(TF /*unused*/, const DF df) { |
| 267 | using TI = int32_t; |
| 268 | const Rebind<TI, DF> di; |
| 269 | const size_t N = Lanes(df); |
| 270 | |
| 271 | // Integer positive |
| 272 | HWY_ASSERT_VEC_EQ(di, Iota(di, TI(4)), DemoteTo(di, Iota(df, TF(4.0)))); |
| 273 | |
| 274 | // Integer negative |
| 275 | HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N)), DemoteTo(di, Iota(df, -TF(N)))); |
| 276 | |
| 277 | // Above positive |
| 278 | HWY_ASSERT_VEC_EQ(di, Iota(di, TI(2)), DemoteTo(di, Iota(df, TF(2.001)))); |
| 279 | |
| 280 | // Below positive |
| 281 | HWY_ASSERT_VEC_EQ(di, Iota(di, TI(3)), DemoteTo(di, Iota(df, TF(3.9999)))); |
| 282 | |
| 283 | const TF eps = static_cast<TF>(0.0001); |
| 284 | // Above negative |
| 285 | HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N)), |
| 286 | DemoteTo(di, Iota(df, -TF(N + 1) + eps))); |
| 287 | |
| 288 | // Below negative |
| 289 | HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N + 1)), |
| 290 | DemoteTo(di, Iota(df, -TF(N + 1) - eps))); |
| 291 | |
| 292 | // Huge positive float |
| 293 | HWY_ASSERT_VEC_EQ(di, Set(di, LimitsMax<TI>()), |
| 294 | DemoteTo(di, Set(df, TF(1E12)))); |
| 295 | |
| 296 | // Huge negative float |
| 297 | HWY_ASSERT_VEC_EQ(di, Set(di, LimitsMin<TI>()), |
| 298 | DemoteTo(di, Set(df, TF(-1E12)))); |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | HWY_NOINLINE void TestAllI32F64() { |
| 303 | #if HWY_HAVE_FLOAT64 |
| 304 | ForDemoteVectors<TestI32F64>()(double()); |
| 305 | #endif |
| 306 | } |
| 307 | |
| 308 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 309 | } // namespace HWY_NAMESPACE |
| 310 | } // namespace hwy |
| 311 | HWY_AFTER_NAMESPACE(); |
| 312 | |
| 313 | #endif // !HWY_IS_MSAN |
| 314 | |
| 315 | #if HWY_ONCE |
| 316 | |
| 317 | namespace hwy { |
| 318 | #if !HWY_IS_MSAN |
| 319 | HWY_BEFORE_TEST(HwyDemoteTest); |
| 320 | HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToInt); |
| 321 | HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToMixed); |
| 322 | HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToFloat); |
| 323 | HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllReorderDemote2To); |
| 324 | HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllI32F64); |
| 325 | #endif // !HWY_IS_MSAN |
| 326 | } // namespace hwy |
| 327 | |
| 328 | #endif |