| 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 | #include <string.h> |
| 19 | |
| 20 | #include <cmath> // std::isfinite |
| 21 | |
| 22 | #include "hwy/base.h" |
| 23 | |
| 24 | #undef HWY_TARGET_INCLUDE |
| 25 | #define HWY_TARGET_INCLUDE "tests/convert_test.cc" |
| 26 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 27 | #include "hwy/highway.h" |
| 28 | #include "hwy/tests/test_util-inl.h" |
| 29 | |
| 30 | HWY_BEFORE_NAMESPACE(); |
| 31 | namespace hwy { |
| 32 | namespace HWY_NAMESPACE { |
| 33 | |
| 34 | // Cast and ensure bytes are the same. Called directly from TestAllBitCast or |
| 35 | // via TestBitCastFrom. |
| 36 | template <typename ToT> |
| 37 | struct TestBitCast { |
| 38 | template <typename T, class D> |
| 39 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 40 | const Repartition<ToT, D> dto; |
| 41 | const size_t N = Lanes(d); |
| 42 | const size_t Nto = Lanes(dto); |
| 43 | if (N == 0 || Nto == 0) return; |
| 44 | HWY_ASSERT_EQ(N * sizeof(T), Nto * sizeof(ToT)); |
| 45 | const auto vf = Iota(d, 1); |
| 46 | const auto vt = BitCast(dto, vf); |
| 47 | // Must return the same bits |
| 48 | auto from_lanes = AllocateAligned<T>(Lanes(d)); |
| 49 | auto to_lanes = AllocateAligned<ToT>(Lanes(dto)); |
| 50 | Store(vf, d, from_lanes.get()); |
| 51 | Store(vt, dto, to_lanes.get()); |
| 52 | HWY_ASSERT( |
| 53 | BytesEqual(from_lanes.get(), to_lanes.get(), Lanes(d) * sizeof(T))); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | // From D to all types. |
| 58 | struct TestBitCastFrom { |
| 59 | template <typename T, class D> |
| 60 | HWY_NOINLINE void operator()(T t, D d) { |
| 61 | TestBitCast<uint8_t>()(t, d); |
| 62 | TestBitCast<uint16_t>()(t, d); |
| 63 | TestBitCast<uint32_t>()(t, d); |
| 64 | #if HWY_HAVE_INTEGER64 |
| 65 | TestBitCast<uint64_t>()(t, d); |
| 66 | #endif |
| 67 | TestBitCast<int8_t>()(t, d); |
| 68 | TestBitCast<int16_t>()(t, d); |
| 69 | TestBitCast<int32_t>()(t, d); |
| 70 | #if HWY_HAVE_INTEGER64 |
| 71 | TestBitCast<int64_t>()(t, d); |
| 72 | #endif |
| 73 | TestBitCast<float>()(t, d); |
| 74 | #if HWY_HAVE_FLOAT64 |
| 75 | TestBitCast<double>()(t, d); |
| 76 | #endif |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | HWY_NOINLINE void TestAllBitCast() { |
| 81 | // For HWY_SCALAR and partial vectors, we can only cast to same-sized types: |
| 82 | // the former can't partition its single lane, and the latter can be smaller |
| 83 | // than a destination type. |
| 84 | const ForPartialVectors<TestBitCast<uint8_t>> to_u8; |
| 85 | to_u8(uint8_t()); |
| 86 | to_u8(int8_t()); |
| 87 | |
| 88 | const ForPartialVectors<TestBitCast<int8_t>> to_i8; |
| 89 | to_i8(uint8_t()); |
| 90 | to_i8(int8_t()); |
| 91 | |
| 92 | const ForPartialVectors<TestBitCast<uint16_t>> to_u16; |
| 93 | to_u16(uint16_t()); |
| 94 | to_u16(int16_t()); |
| 95 | |
| 96 | const ForPartialVectors<TestBitCast<int16_t>> to_i16; |
| 97 | to_i16(uint16_t()); |
| 98 | to_i16(int16_t()); |
| 99 | |
| 100 | const ForPartialVectors<TestBitCast<uint32_t>> to_u32; |
| 101 | to_u32(uint32_t()); |
| 102 | to_u32(int32_t()); |
| 103 | to_u32(float()); |
| 104 | |
| 105 | const ForPartialVectors<TestBitCast<int32_t>> to_i32; |
| 106 | to_i32(uint32_t()); |
| 107 | to_i32(int32_t()); |
| 108 | to_i32(float()); |
| 109 | |
| 110 | #if HWY_HAVE_INTEGER64 |
| 111 | const ForPartialVectors<TestBitCast<uint64_t>> to_u64; |
| 112 | to_u64(uint64_t()); |
| 113 | to_u64(int64_t()); |
| 114 | #if HWY_HAVE_FLOAT64 |
| 115 | to_u64(double()); |
| 116 | #endif |
| 117 | |
| 118 | const ForPartialVectors<TestBitCast<int64_t>> to_i64; |
| 119 | to_i64(uint64_t()); |
| 120 | to_i64(int64_t()); |
| 121 | #if HWY_HAVE_FLOAT64 |
| 122 | to_i64(double()); |
| 123 | #endif |
| 124 | #endif // HWY_HAVE_INTEGER64 |
| 125 | |
| 126 | const ForPartialVectors<TestBitCast<float>> to_float; |
| 127 | to_float(uint32_t()); |
| 128 | to_float(int32_t()); |
| 129 | to_float(float()); |
| 130 | |
| 131 | #if HWY_HAVE_FLOAT64 |
| 132 | const ForPartialVectors<TestBitCast<double>> to_double; |
| 133 | to_double(double()); |
| 134 | #if HWY_HAVE_INTEGER64 |
| 135 | to_double(uint64_t()); |
| 136 | to_double(int64_t()); |
| 137 | #endif // HWY_HAVE_INTEGER64 |
| 138 | #endif // HWY_HAVE_FLOAT64 |
| 139 | |
| 140 | #if HWY_TARGET != HWY_SCALAR |
| 141 | // For non-scalar vectors, we can cast all types to all. |
| 142 | ForAllTypes(ForGEVectors<64, TestBitCastFrom>()); |
| 143 | #endif |
| 144 | } |
| 145 | |
| 146 | template <typename ToT> |
| 147 | struct TestPromoteTo { |
| 148 | template <typename T, class D> |
| 149 | HWY_NOINLINE void operator()(T /*unused*/, D from_d) { |
| 150 | static_assert(sizeof(T) < sizeof(ToT), "Input type must be narrower"); |
| 151 | const Rebind<ToT, D> to_d; |
| 152 | |
| 153 | const size_t N = Lanes(from_d); |
| 154 | auto from = AllocateAligned<T>(N); |
| 155 | auto expected = AllocateAligned<ToT>(N); |
| 156 | |
| 157 | RandomState rng; |
| 158 | for (size_t rep = 0; rep < AdjustedReps(200); ++rep) { |
| 159 | for (size_t i = 0; i < N; ++i) { |
| 160 | const uint64_t bits = rng(); |
| 161 | CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size |
| 162 | expected[i] = from[i]; |
| 163 | } |
| 164 | |
| 165 | HWY_ASSERT_VEC_EQ(to_d, expected.get(), |
| 166 | PromoteTo(to_d, Load(from_d, from.get()))); |
| 167 | } |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | HWY_NOINLINE void TestAllPromoteTo() { |
| 172 | const ForPromoteVectors<TestPromoteTo<uint16_t>, 1> to_u16div2; |
| 173 | to_u16div2(uint8_t()); |
| 174 | |
| 175 | const ForPromoteVectors<TestPromoteTo<uint32_t>, 2> to_u32div4; |
| 176 | to_u32div4(uint8_t()); |
| 177 | |
| 178 | const ForPromoteVectors<TestPromoteTo<uint32_t>, 1> to_u32div2; |
| 179 | to_u32div2(uint16_t()); |
| 180 | |
| 181 | const ForPromoteVectors<TestPromoteTo<int16_t>, 1> to_i16div2; |
| 182 | to_i16div2(uint8_t()); |
| 183 | to_i16div2(int8_t()); |
| 184 | |
| 185 | const ForPromoteVectors<TestPromoteTo<int32_t>, 1> to_i32div2; |
| 186 | to_i32div2(uint16_t()); |
| 187 | to_i32div2(int16_t()); |
| 188 | |
| 189 | const ForPromoteVectors<TestPromoteTo<int32_t>, 2> to_i32div4; |
| 190 | to_i32div4(uint8_t()); |
| 191 | to_i32div4(int8_t()); |
| 192 | |
| 193 | // Must test f16/bf16 separately because we can only load/store/convert them. |
| 194 | |
| 195 | #if HWY_HAVE_INTEGER64 |
| 196 | const ForPromoteVectors<TestPromoteTo<uint64_t>, 1> to_u64div2; |
| 197 | to_u64div2(uint32_t()); |
| 198 | |
| 199 | const ForPromoteVectors<TestPromoteTo<int64_t>, 1> to_i64div2; |
| 200 | to_i64div2(int32_t()); |
| 201 | #endif |
| 202 | |
| 203 | #if HWY_HAVE_FLOAT64 |
| 204 | const ForPromoteVectors<TestPromoteTo<double>, 1> to_f64div2; |
| 205 | to_f64div2(int32_t()); |
| 206 | to_f64div2(float()); |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | template <typename T, HWY_IF_FLOAT(T)> |
| 211 | bool IsFinite(T t) { |
| 212 | return std::isfinite(t); |
| 213 | } |
| 214 | // Wrapper avoids calling std::isfinite for integer types (ambiguous). |
| 215 | template <typename T, HWY_IF_NOT_FLOAT(T)> |
| 216 | bool IsFinite(T /*unused*/) { |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | template <class D> |
| 221 | AlignedFreeUniquePtr<float[]> F16TestCases(D d, size_t& padded) { |
| 222 | const float test_cases[] = { |
| 223 | // +/- 1 |
| 224 | 1.0f, -1.0f, |
| 225 | // +/- 0 |
| 226 | 0.0f, -0.0f, |
| 227 | // near 0 |
| 228 | 0.25f, -0.25f, |
| 229 | // +/- integer |
| 230 | 4.0f, -32.0f, |
| 231 | // positive near limit |
| 232 | 65472.0f, 65504.0f, |
| 233 | // negative near limit |
| 234 | -65472.0f, -65504.0f, |
| 235 | // positive +/- delta |
| 236 | 2.00390625f, 3.99609375f, |
| 237 | // negative +/- delta |
| 238 | -2.00390625f, -3.99609375f, |
| 239 | // No infinity/NaN - implementation-defined due to ARM. |
| 240 | }; |
| 241 | constexpr size_t kNumTestCases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 242 | const size_t N = Lanes(d); |
| 243 | HWY_ASSERT(N != 0); |
| 244 | padded = RoundUpTo(kNumTestCases, N); // allow loading whole vectors |
| 245 | auto in = AllocateAligned<float>(padded); |
| 246 | auto expected = AllocateAligned<float>(padded); |
| 247 | size_t i = 0; |
| 248 | for (; i < kNumTestCases; ++i) { |
| 249 | in[i] = test_cases[i]; |
| 250 | } |
| 251 | for (; i < padded; ++i) { |
| 252 | in[i] = 0.0f; |
| 253 | } |
| 254 | return in; |
| 255 | } |
| 256 | |
| 257 | struct TestF16 { |
| 258 | template <typename TF32, class DF32> |
| 259 | HWY_NOINLINE void operator()(TF32 /*t*/, DF32 d32) { |
| 260 | #if HWY_HAVE_FLOAT16 |
| 261 | size_t padded; |
| 262 | const size_t N = Lanes(d32); // same count for f16 |
| 263 | HWY_ASSERT(N != 0); |
| 264 | auto in = F16TestCases(d32, padded); |
| 265 | using TF16 = float16_t; |
| 266 | const Rebind<TF16, DF32> d16; |
| 267 | auto temp16 = AllocateAligned<TF16>(N); |
| 268 | |
| 269 | for (size_t i = 0; i < padded; i += N) { |
| 270 | const auto loaded = Load(d32, &in[i]); |
| 271 | Store(DemoteTo(d16, loaded), d16, temp16.get()); |
| 272 | HWY_ASSERT_VEC_EQ(d32, loaded, PromoteTo(d32, Load(d16, temp16.get()))); |
| 273 | } |
| 274 | #else |
| 275 | (void)d32; |
| 276 | #endif |
| 277 | } |
| 278 | }; |
| 279 | |
| 280 | HWY_NOINLINE void TestAllF16() { ForDemoteVectors<TestF16>()(float()); } |
| 281 | |
| 282 | template <class D> |
| 283 | AlignedFreeUniquePtr<float[]> BF16TestCases(D d, size_t& padded) { |
| 284 | const float test_cases[] = { |
| 285 | // +/- 1 |
| 286 | 1.0f, -1.0f, |
| 287 | // +/- 0 |
| 288 | 0.0f, -0.0f, |
| 289 | // near 0 |
| 290 | 0.25f, -0.25f, |
| 291 | // +/- integer |
| 292 | 4.0f, -32.0f, |
| 293 | // positive near limit |
| 294 | 3.389531389251535E38f, 1.99384199368e+38f, |
| 295 | // negative near limit |
| 296 | -3.389531389251535E38f, -1.99384199368e+38f, |
| 297 | // positive +/- delta |
| 298 | 2.015625f, 3.984375f, |
| 299 | // negative +/- delta |
| 300 | -2.015625f, -3.984375f, |
| 301 | }; |
| 302 | constexpr size_t kNumTestCases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 303 | const size_t N = Lanes(d); |
| 304 | HWY_ASSERT(N != 0); |
| 305 | padded = RoundUpTo(kNumTestCases, N); // allow loading whole vectors |
| 306 | auto in = AllocateAligned<float>(padded); |
| 307 | auto expected = AllocateAligned<float>(padded); |
| 308 | size_t i = 0; |
| 309 | for (; i < kNumTestCases; ++i) { |
| 310 | in[i] = test_cases[i]; |
| 311 | } |
| 312 | for (; i < padded; ++i) { |
| 313 | in[i] = 0.0f; |
| 314 | } |
| 315 | return in; |
| 316 | } |
| 317 | |
| 318 | struct TestBF16 { |
| 319 | template <typename TF32, class DF32> |
| 320 | HWY_NOINLINE void operator()(TF32 /*t*/, DF32 d32) { |
| 321 | #if !defined(HWY_EMULATE_SVE) |
| 322 | size_t padded; |
| 323 | auto in = BF16TestCases(d32, padded); |
| 324 | using TBF16 = bfloat16_t; |
| 325 | #if HWY_TARGET == HWY_SCALAR |
| 326 | const Rebind<TBF16, DF32> dbf16; // avoid 4/2 = 2 lanes |
| 327 | #else |
| 328 | const Repartition<TBF16, DF32> dbf16; |
| 329 | #endif |
| 330 | const Half<decltype(dbf16)> dbf16_half; |
| 331 | const size_t N = Lanes(d32); |
| 332 | HWY_ASSERT(Lanes(dbf16_half) <= N); |
| 333 | auto temp16 = AllocateAligned<TBF16>(N); |
| 334 | |
| 335 | for (size_t i = 0; i < padded; i += N) { |
| 336 | const auto loaded = Load(d32, &in[i]); |
| 337 | const auto v16 = DemoteTo(dbf16_half, loaded); |
| 338 | Store(v16, dbf16_half, temp16.get()); |
| 339 | const auto v16_loaded = Load(dbf16_half, temp16.get()); |
| 340 | HWY_ASSERT_VEC_EQ(d32, loaded, PromoteTo(d32, v16_loaded)); |
| 341 | } |
| 342 | #else |
| 343 | (void)d32; |
| 344 | #endif |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | HWY_NOINLINE void TestAllBF16() { ForShrinkableVectors<TestBF16>()(float()); } |
| 349 | |
| 350 | struct TestConvertU8 { |
| 351 | template <typename T, class D> |
| 352 | HWY_NOINLINE void operator()(T /*unused*/, const D du32) { |
| 353 | const Rebind<uint8_t, D> du8; |
| 354 | const auto wrap = Set(du32, 0xFF); |
| 355 | HWY_ASSERT_VEC_EQ(du8, Iota(du8, 0), U8FromU32(And(Iota(du32, 0), wrap))); |
| 356 | HWY_ASSERT_VEC_EQ(du8, Iota(du8, 0x7F), |
| 357 | U8FromU32(And(Iota(du32, 0x7F), wrap))); |
| 358 | } |
| 359 | }; |
| 360 | |
| 361 | HWY_NOINLINE void TestAllConvertU8() { |
| 362 | ForDemoteVectors<TestConvertU8, 2>()(uint32_t()); |
| 363 | } |
| 364 | |
| 365 | template <typename From, typename To, class D> |
| 366 | constexpr bool IsSupportedTruncation() { |
| 367 | return (sizeof(To) < sizeof(From)) && |
| 368 | (Pow2(Rebind<To, D>()) + 3 >= static_cast<int>(CeilLog2(sizeof(To)))); |
| 369 | } |
| 370 | |
| 371 | struct TestTruncateTo { |
| 372 | template <typename From, typename To, class D, |
| 373 | hwy::EnableIf<!IsSupportedTruncation<From, To, D>()>* = nullptr> |
| 374 | HWY_NOINLINE void testTo(From, To, const D) { |
| 375 | // do nothing |
| 376 | } |
| 377 | |
| 378 | template <typename From, typename To, class D, |
| 379 | hwy::EnableIf<IsSupportedTruncation<From, To, D>()>* = nullptr> |
| 380 | HWY_NOINLINE void testTo(From, To, const D d) { |
| 381 | constexpr uint32_t base = 0xFA578D00; |
| 382 | const Rebind<To, D> dTo; |
| 383 | const auto src = Iota(d, static_cast<From>(base)); |
| 384 | const auto expected = Iota(dTo, static_cast<To>(base)); |
| 385 | const VFromD<decltype(dTo)> actual = TruncateTo(dTo, src); |
| 386 | HWY_ASSERT_VEC_EQ(dTo, expected, actual); |
| 387 | } |
| 388 | |
| 389 | template <typename T, class D> |
| 390 | HWY_NOINLINE void operator()(T from, const D d) { |
| 391 | testTo<T, uint8_t, D>(from, uint8_t(), d); |
| 392 | testTo<T, uint16_t, D>(from, uint16_t(), d); |
| 393 | testTo<T, uint32_t, D>(from, uint32_t(), d); |
| 394 | } |
| 395 | }; |
| 396 | |
| 397 | HWY_NOINLINE void TestAllTruncate() { |
| 398 | ForUnsignedTypes(ForPartialVectors<TestTruncateTo>()); |
| 399 | } |
| 400 | |
| 401 | // Separate function to attempt to work around a compiler bug on ARM: when this |
| 402 | // is merged with TestIntFromFloat, outputs match a previous Iota(-(N+1)) input. |
| 403 | struct TestIntFromFloatHuge { |
| 404 | template <typename TF, class DF> |
| 405 | HWY_NOINLINE void operator()(TF /*unused*/, const DF df) { |
| 406 | // The ARMv7 manual says that float->int saturates, i.e. chooses the |
| 407 | // nearest representable value. This works correctly on armhf with GCC, but |
| 408 | // not with clang. For reasons unknown, MSVC also runs into an out-of-memory |
| 409 | // error here. |
| 410 | #if HWY_COMPILER_CLANG || HWY_COMPILER_MSVC |
| 411 | (void)df; |
| 412 | #else |
| 413 | using TI = MakeSigned<TF>; |
| 414 | const Rebind<TI, DF> di; |
| 415 | |
| 416 | // Workaround for incorrect 32-bit GCC codegen for SSSE3 - Print-ing |
| 417 | // the expected lvalue also seems to prevent the issue. |
| 418 | const size_t N = Lanes(df); |
| 419 | auto expected = AllocateAligned<TI>(N); |
| 420 | |
| 421 | // Huge positive |
| 422 | Store(Set(di, LimitsMax<TI>()), di, expected.get()); |
| 423 | HWY_ASSERT_VEC_EQ(di, expected.get(), ConvertTo(di, Set(df, TF(1E20)))); |
| 424 | |
| 425 | // Huge negative |
| 426 | Store(Set(di, LimitsMin<TI>()), di, expected.get()); |
| 427 | HWY_ASSERT_VEC_EQ(di, expected.get(), ConvertTo(di, Set(df, TF(-1E20)))); |
| 428 | #endif |
| 429 | } |
| 430 | }; |
| 431 | |
| 432 | class TestIntFromFloat { |
| 433 | template <typename TF, class DF> |
| 434 | static HWY_NOINLINE void TestPowers(TF /*unused*/, const DF df) { |
| 435 | using TI = MakeSigned<TF>; |
| 436 | const Rebind<TI, DF> di; |
| 437 | constexpr size_t kBits = sizeof(TF) * 8; |
| 438 | |
| 439 | // Powers of two, plus offsets to set some mantissa bits. |
| 440 | const int64_t ofs_table[3] = {0LL, 3LL << (kBits / 2), 1LL << (kBits - 15)}; |
| 441 | for (int sign = 0; sign < 2; ++sign) { |
| 442 | for (size_t shift = 0; shift < kBits - 1; ++shift) { |
| 443 | for (int64_t ofs : ofs_table) { |
| 444 | const int64_t mag = (int64_t{1} << shift) + ofs; |
| 445 | const int64_t val = sign ? mag : -mag; |
| 446 | HWY_ASSERT_VEC_EQ(di, Set(di, static_cast<TI>(val)), |
| 447 | ConvertTo(di, Set(df, static_cast<TF>(val)))); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | template <typename TF, class DF> |
| 454 | static HWY_NOINLINE void TestRandom(TF /*unused*/, const DF df) { |
| 455 | using TI = MakeSigned<TF>; |
| 456 | const Rebind<TI, DF> di; |
| 457 | const size_t N = Lanes(df); |
| 458 | |
| 459 | // TF does not have enough precision to represent TI. |
| 460 | const double min = static_cast<double>(LimitsMin<TI>()); |
| 461 | const double max = static_cast<double>(LimitsMax<TI>()); |
| 462 | |
| 463 | // Also check random values. |
| 464 | auto from = AllocateAligned<TF>(N); |
| 465 | auto expected = AllocateAligned<TI>(N); |
| 466 | RandomState rng; |
| 467 | for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) { |
| 468 | for (size_t i = 0; i < N; ++i) { |
| 469 | do { |
| 470 | const uint64_t bits = rng(); |
| 471 | CopyBytes<sizeof(TF)>(&bits, &from[i]); // not same size |
| 472 | } while (!std::isfinite(from[i])); |
| 473 | if (from[i] >= max) { |
| 474 | expected[i] = LimitsMax<TI>(); |
| 475 | } else if (from[i] <= min) { |
| 476 | expected[i] = LimitsMin<TI>(); |
| 477 | } else { |
| 478 | expected[i] = static_cast<TI>(from[i]); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | HWY_ASSERT_VEC_EQ(di, expected.get(), |
| 483 | ConvertTo(di, Load(df, from.get()))); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | public: |
| 488 | template <typename TF, class DF> |
| 489 | HWY_NOINLINE void operator()(TF tf, const DF df) { |
| 490 | using TI = MakeSigned<TF>; |
| 491 | const Rebind<TI, DF> di; |
| 492 | const size_t N = Lanes(df); |
| 493 | |
| 494 | // Integer positive |
| 495 | HWY_ASSERT_VEC_EQ(di, Iota(di, TI(4)), ConvertTo(di, Iota(df, TF(4.0)))); |
| 496 | |
| 497 | // Integer negative |
| 498 | HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N)), ConvertTo(di, Iota(df, -TF(N)))); |
| 499 | |
| 500 | // Above positive |
| 501 | HWY_ASSERT_VEC_EQ(di, Iota(di, TI(2)), ConvertTo(di, Iota(df, TF(2.001)))); |
| 502 | |
| 503 | // Below positive |
| 504 | HWY_ASSERT_VEC_EQ(di, Iota(di, TI(3)), ConvertTo(di, Iota(df, TF(3.9999)))); |
| 505 | |
| 506 | const TF eps = static_cast<TF>(0.0001); |
| 507 | // Above negative |
| 508 | HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N)), |
| 509 | ConvertTo(di, Iota(df, -TF(N + 1) + eps))); |
| 510 | |
| 511 | // Below negative |
| 512 | HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N + 1)), |
| 513 | ConvertTo(di, Iota(df, -TF(N + 1) - eps))); |
| 514 | |
| 515 | TestPowers(tf, df); |
| 516 | TestRandom(tf, df); |
| 517 | } |
| 518 | }; |
| 519 | |
| 520 | HWY_NOINLINE void TestAllIntFromFloat() { |
| 521 | ForFloatTypes(ForPartialVectors<TestIntFromFloatHuge>()); |
| 522 | ForFloatTypes(ForPartialVectors<TestIntFromFloat>()); |
| 523 | } |
| 524 | |
| 525 | struct TestFloatFromInt { |
| 526 | template <typename TF, class DF> |
| 527 | HWY_NOINLINE void operator()(TF /*unused*/, const DF df) { |
| 528 | using TI = MakeSigned<TF>; |
| 529 | const RebindToSigned<DF> di; |
| 530 | const size_t N = Lanes(df); |
| 531 | |
| 532 | // Integer positive |
| 533 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(4.0)), ConvertTo(df, Iota(di, TI(4)))); |
| 534 | |
| 535 | // Integer negative |
| 536 | HWY_ASSERT_VEC_EQ(df, Iota(df, -TF(N)), ConvertTo(df, Iota(di, -TI(N)))); |
| 537 | |
| 538 | // Max positive |
| 539 | HWY_ASSERT_VEC_EQ(df, Set(df, TF(LimitsMax<TI>())), |
| 540 | ConvertTo(df, Set(di, LimitsMax<TI>()))); |
| 541 | |
| 542 | // Min negative |
| 543 | HWY_ASSERT_VEC_EQ(df, Set(df, TF(LimitsMin<TI>())), |
| 544 | ConvertTo(df, Set(di, LimitsMin<TI>()))); |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | HWY_NOINLINE void TestAllFloatFromInt() { |
| 549 | ForFloatTypes(ForPartialVectors<TestFloatFromInt>()); |
| 550 | } |
| 551 | |
| 552 | struct TestFloatFromUint { |
| 553 | template <typename TF, class DF> |
| 554 | HWY_NOINLINE void operator()(TF /*unused*/, const DF df) { |
| 555 | using TU = MakeUnsigned<TF>; |
| 556 | const RebindToUnsigned<DF> du; |
| 557 | |
| 558 | // Integer positive |
| 559 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(4.0)), ConvertTo(df, Iota(du, TU(4)))); |
| 560 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(65535.0)), |
| 561 | ConvertTo(df, Iota(du, 65535))); // 2^16-1 |
| 562 | if (sizeof(TF) > 4) { |
| 563 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(4294967295.0)), |
| 564 | ConvertTo(df, Iota(du, 4294967295ULL))); // 2^32-1 |
| 565 | } |
| 566 | |
| 567 | // Max positive |
| 568 | HWY_ASSERT_VEC_EQ(df, Set(df, TF(LimitsMax<TU>())), |
| 569 | ConvertTo(df, Set(du, LimitsMax<TU>()))); |
| 570 | |
| 571 | // Zero |
| 572 | HWY_ASSERT_VEC_EQ(df, Zero(df), ConvertTo(df, Zero(du))); |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | HWY_NOINLINE void TestAllFloatFromUint() { |
| 577 | ForFloatTypes(ForPartialVectors<TestFloatFromUint>()); |
| 578 | } |
| 579 | |
| 580 | struct TestI32F64 { |
| 581 | template <typename TF, class DF> |
| 582 | HWY_NOINLINE void operator()(TF /*unused*/, const DF df) { |
| 583 | using TI = int32_t; |
| 584 | const Rebind<TI, DF> di; |
| 585 | const size_t N = Lanes(df); |
| 586 | |
| 587 | // Integer positive |
| 588 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(4.0)), PromoteTo(df, Iota(di, TI(4)))); |
| 589 | |
| 590 | // Integer negative |
| 591 | HWY_ASSERT_VEC_EQ(df, Iota(df, -TF(N)), PromoteTo(df, Iota(di, -TI(N)))); |
| 592 | |
| 593 | // Above positive |
| 594 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(2.0)), PromoteTo(df, Iota(di, TI(2)))); |
| 595 | |
| 596 | // Below positive |
| 597 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(4.0)), PromoteTo(df, Iota(di, TI(4)))); |
| 598 | |
| 599 | // Above negative |
| 600 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(-4.0)), PromoteTo(df, Iota(di, TI(-4)))); |
| 601 | |
| 602 | // Below negative |
| 603 | HWY_ASSERT_VEC_EQ(df, Iota(df, TF(-2.0)), PromoteTo(df, Iota(di, TI(-2)))); |
| 604 | |
| 605 | // Max positive int |
| 606 | HWY_ASSERT_VEC_EQ(df, Set(df, TF(LimitsMax<TI>())), |
| 607 | PromoteTo(df, Set(di, LimitsMax<TI>()))); |
| 608 | |
| 609 | // Min negative int |
| 610 | HWY_ASSERT_VEC_EQ(df, Set(df, TF(LimitsMin<TI>())), |
| 611 | PromoteTo(df, Set(di, LimitsMin<TI>()))); |
| 612 | } |
| 613 | }; |
| 614 | |
| 615 | HWY_NOINLINE void TestAllI32F64() { |
| 616 | #if HWY_HAVE_FLOAT64 |
| 617 | ForDemoteVectors<TestI32F64>()(double()); |
| 618 | #endif |
| 619 | } |
| 620 | |
| 621 | |
| 622 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 623 | } // namespace HWY_NAMESPACE |
| 624 | } // namespace hwy |
| 625 | HWY_AFTER_NAMESPACE(); |
| 626 | |
| 627 | #if HWY_ONCE |
| 628 | |
| 629 | namespace hwy { |
| 630 | HWY_BEFORE_TEST(HwyConvertTest); |
| 631 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllBitCast); |
| 632 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllPromoteTo); |
| 633 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllF16); |
| 634 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllBF16); |
| 635 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllConvertU8); |
| 636 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllTruncate); |
| 637 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllIntFromFloat); |
| 638 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllFloatFromInt); |
| 639 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllFloatFromUint); |
| 640 | HWY_EXPORT_AND_TEST_P(HwyConvertTest, TestAllI32F64); |
| 641 | } // namespace hwy |
| 642 | |
| 643 | #endif |