1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
// Copyright 2019 Google LLC
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm> // std::fill
#include <cmath> // std::isfinite
#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "tests/demote_test.cc"
#include "hwy/foreach_target.h" // IWYU pragma: keep
#include "hwy/highway.h"
#include "hwy/tests/test_util-inl.h"
// Causes build timeout.
#if !HWY_IS_MSAN
HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
template <typename T, HWY_IF_FLOAT(T)>
bool IsFiniteT(T t) {
return std::isfinite(t);
}
// Wrapper avoids calling std::isfinite for integer types (ambiguous).
template <typename T, HWY_IF_NOT_FLOAT(T)>
bool IsFiniteT(T /*unused*/) {
return true;
}
template <typename ToT>
struct TestDemoteTo {
template <typename T, class D>
HWY_NOINLINE void operator()(T /*unused*/, D from_d) {
static_assert(!IsFloat<ToT>(), "Use TestDemoteToFloat for float output");
static_assert(sizeof(T) > sizeof(ToT), "Input type must be wider");
const Rebind<ToT, D> to_d;
const size_t N = Lanes(from_d);
auto from = AllocateAligned<T>(N);
auto expected = AllocateAligned<ToT>(N);
HWY_ASSERT(from && expected);
// Narrower range in the wider type, for clamping before we cast
const T min = static_cast<T>(IsSigned<T>() ? LimitsMin<ToT>() : ToT{0});
const T max = LimitsMax<ToT>();
RandomState rng;
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < N; ++i) {
const uint64_t bits = rng();
CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size
expected[i] = static_cast<ToT>(HWY_MIN(HWY_MAX(min, from[i]), max));
}
const auto in = Load(from_d, from.get());
HWY_ASSERT_VEC_EQ(to_d, expected.get(), DemoteTo(to_d, in));
}
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < N; ++i) {
const uint64_t bits = rng();
CopyBytes<sizeof(ToT)>(&bits, &expected[i]); // not same size
if (!IsSigned<T>() && IsSigned<ToT>()) {
expected[i] &= static_cast<ToT>(max);
}
from[i] = static_cast<T>(expected[i]);
}
const auto in = Load(from_d, from.get());
HWY_ASSERT_VEC_EQ(to_d, expected.get(), DemoteTo(to_d, in));
}
}
};
HWY_NOINLINE void TestAllDemoteToInt() {
const ForDemoteVectors<TestDemoteTo<uint8_t>> from_i16_to_u8;
from_i16_to_u8(int16_t());
from_i16_to_u8(uint16_t());
const ForDemoteVectors<TestDemoteTo<int8_t>> from_i16_to_i8;
from_i16_to_i8(int16_t());
from_i16_to_i8(uint16_t());
const ForDemoteVectors<TestDemoteTo<uint8_t>, 2> from_i32_to_u8;
from_i32_to_u8(int32_t());
from_i32_to_u8(uint32_t());
const ForDemoteVectors<TestDemoteTo<int8_t>, 2> from_i32_to_i8;
from_i32_to_i8(int32_t());
from_i32_to_i8(uint32_t());
#if HWY_HAVE_INTEGER64
const ForDemoteVectors<TestDemoteTo<uint8_t>, 3> from_i64_to_u8;
from_i64_to_u8(int64_t());
from_i64_to_u8(uint64_t());
const ForDemoteVectors<TestDemoteTo<int8_t>, 3> from_i64_to_i8;
from_i64_to_i8(int64_t());
from_i64_to_i8(uint64_t());
#endif
const ForDemoteVectors<TestDemoteTo<uint16_t>> from_i32_to_u16;
from_i32_to_u16(int32_t());
from_i32_to_u16(uint32_t());
const ForDemoteVectors<TestDemoteTo<int16_t>> from_i32_to_i16;
from_i32_to_i16(int32_t());
from_i32_to_i16(uint32_t());
#if HWY_HAVE_INTEGER64
const ForDemoteVectors<TestDemoteTo<uint16_t>, 2> from_i64_to_u16;
from_i64_to_u16(int64_t());
from_i64_to_u16(uint64_t());
const ForDemoteVectors<TestDemoteTo<int16_t>, 2> from_i64_to_i16;
from_i64_to_i16(int64_t());
from_i64_to_i16(uint64_t());
const ForDemoteVectors<TestDemoteTo<uint32_t>> from_i64_to_u32;
from_i64_to_u32(int64_t());
from_i64_to_u32(uint64_t());
const ForDemoteVectors<TestDemoteTo<int32_t>> from_i64_to_i32;
from_i64_to_i32(int64_t());
from_i64_to_i32(uint64_t());
#endif
}
HWY_NOINLINE void TestAllDemoteToMixed() {
#if HWY_HAVE_FLOAT64
const ForDemoteVectors<TestDemoteTo<int32_t>> to_i32;
to_i32(double());
#endif
}
template <typename ToT>
struct TestDemoteToFloat {
template <typename T, class D>
HWY_NOINLINE void operator()(T /*unused*/, D from_d) {
// For floats, we clamp differently and cannot call LimitsMin.
static_assert(IsFloat<ToT>(), "Use TestDemoteTo for integer output");
static_assert(sizeof(T) > sizeof(ToT), "Input type must be wider");
const Rebind<ToT, D> to_d;
const size_t N = Lanes(from_d);
auto from = AllocateAligned<T>(N);
auto expected = AllocateAligned<ToT>(N);
HWY_ASSERT(from && expected);
RandomState rng;
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < N; ++i) {
do {
const uint64_t bits = rng();
CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size
} while (!IsFiniteT(from[i]));
const T magn = std::abs(from[i]);
const T max_abs = HighestValue<ToT>();
// NOTE: std:: version from C++11 cmath is not defined in RVV GCC, see
// https://lists.freebsd.org/pipermail/freebsd-current/2014-January/048130.html
const T clipped = copysign(HWY_MIN(magn, max_abs), from[i]);
expected[i] = static_cast<ToT>(clipped);
}
HWY_ASSERT_VEC_EQ(to_d, expected.get(),
DemoteTo(to_d, Load(from_d, from.get())));
}
}
};
HWY_NOINLINE void TestAllDemoteToFloat() {
// Must test f16 separately because we can only load/store/convert them.
#if HWY_HAVE_FLOAT64
const ForDemoteVectors<TestDemoteToFloat<float>, 1> to_float;
to_float(double());
#endif
}
struct TestDemoteToBF16 {
template <typename T, class D>
HWY_NOINLINE void operator()(T /*unused*/, D from_d) {
// For floats, we clamp differently and cannot call LimitsMin.
static_assert(IsSame<T, float>(),
"TestDemoteToBF16 can only be called if T is float");
const Rebind<bfloat16_t, D> to_d;
const Rebind<uint32_t, D> du32;
const Rebind<uint16_t, D> du16;
const size_t N = Lanes(from_d);
auto from = AllocateAligned<T>(N);
auto expected = AllocateAligned<bfloat16_t>(N);
HWY_ASSERT(from && expected);
const auto u16_zero_vect = Zero(du16);
const auto u16_one_vect = Set(du16, 1);
RandomState rng;
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < N; ++i) {
do {
const uint64_t bits = rng();
CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size
} while (!IsFiniteT(from[i]));
uint32_t fromBits;
CopyBytes<sizeof(uint32_t)>(&from[i], &fromBits);
uint16_t bf16Bits = static_cast<uint16_t>(fromBits >> 16);
CopyBytes<sizeof(uint16_t)>(&bf16Bits, &expected[i]);
}
const auto in = Load(from_d, from.get());
const auto actual = DemoteTo(to_d, in);
// Adjust expected to account for any possible rounding that was
// carried out by the DemoteTo operation
auto expected_vect = BitCast(du16, Load(to_d, expected.get()));
const auto low_f32_bits = TruncateTo(du16, BitCast(du32, in));
// max_diff_from_expected is equal to (low_f32_bits == 0 ? 0 : 1)
const auto max_diff_from_expected =
Add(VecFromMask(du16, Eq(low_f32_bits, u16_zero_vect)), u16_one_vect);
// expected_adj is equal to (actual_bits - expected_bits == 1 &&
// max_diff_from_expected != 0) ? 1 : 0, where actual_bits is the bits of
// actual and expected_bits is the bits of expected.
auto expected_adj =
And(max_diff_from_expected,
VecFromMask(du16, Eq(Sub(BitCast(du16, actual), expected_vect),
u16_one_vect)));
// Increment expected_vect by expected_adj
expected_vect = Add(expected_vect, expected_adj);
// Store the adjusted expected_vect back into expected
Store(BitCast(to_d, expected_vect), to_d, expected.get());
HWY_ASSERT_VEC_EQ(to_d, expected.get(), actual);
}
}
};
HWY_NOINLINE void TestAllDemoteToBF16() {
const ForDemoteVectors<TestDemoteToBF16, 1> to_bf16;
to_bf16(float());
}
template <class D>
AlignedFreeUniquePtr<float[]> ReorderBF16TestCases(D d, size_t& padded) {
const float test_cases[] = {
// Same as BF16TestCases:
// +/- 1
1.0f,
-1.0f,
// +/- 0
0.0f,
-0.0f,
// near 0
0.25f,
-0.25f,
// +/- integer
4.0f,
-32.0f,
// positive +/- delta
2.015625f,
3.984375f,
// negative +/- delta
-2.015625f,
-3.984375f,
// No huge values - would interfere with sum. But add more to fill 2 * N:
-2.0f,
-10.0f,
0.03125f,
1.03125f,
1.5f,
2.0f,
4.0f,
5.0f,
6.0f,
8.0f,
10.0f,
256.0f,
448.0f,
2080.0f,
};
const size_t kNumTestCases = sizeof(test_cases) / sizeof(test_cases[0]);
const size_t N = Lanes(d);
padded = RoundUpTo(kNumTestCases, 2 * N); // allow loading pairs of vectors
auto in = AllocateAligned<float>(padded);
auto expected = AllocateAligned<float>(padded);
HWY_ASSERT(in && expected);
std::copy(test_cases, test_cases + kNumTestCases, in.get());
std::fill(in.get() + kNumTestCases, in.get() + padded, 0.0f);
return in;
}
class TestReorderDemote2To {
// In-place N^2 selection sort to avoid dependencies
void Sort(float* p, size_t count) {
for (size_t i = 0; i < count - 1; ++i) {
// Find min_element
size_t idx_min = i;
for (size_t j = i + 1; j < count; j++) {
if (p[j] < p[idx_min]) {
idx_min = j;
}
}
// Swap with current
const float tmp = p[i];
p[i] = p[idx_min];
p[idx_min] = tmp;
}
}
public:
template <typename TF32, class DF32>
HWY_NOINLINE void operator()(TF32 /*t*/, DF32 d32) {
#if HWY_TARGET != HWY_SCALAR
size_t padded;
auto in = ReorderBF16TestCases(d32, padded);
using TBF16 = bfloat16_t;
const Repartition<TBF16, DF32> dbf16;
const Half<decltype(dbf16)> dbf16_half;
const size_t N = Lanes(d32);
auto temp16 = AllocateAligned<TBF16>(2 * N);
auto expected = AllocateAligned<float>(2 * N);
auto actual = AllocateAligned<float>(2 * N);
HWY_ASSERT(temp16 && expected && actual);
for (size_t i = 0; i < padded; i += 2 * N) {
const auto f0 = Load(d32, &in[i + 0]);
const auto f1 = Load(d32, &in[i + N]);
const auto v16 = ReorderDemote2To(dbf16, f0, f1);
Store(v16, dbf16, temp16.get());
const auto promoted0 = PromoteTo(d32, Load(dbf16_half, temp16.get() + 0));
const auto promoted1 = PromoteTo(d32, Load(dbf16_half, temp16.get() + N));
// Smoke test: sum should be same (with tolerance for non-associativity)
const auto sum_expected = ReduceSum(d32, Add(f0, f1));
const auto sum_actual = ReduceSum(d32, Add(promoted0, promoted1));
HWY_ASSERT(sum_expected - 1E-4 <= sum_actual &&
sum_actual <= sum_expected + 1E-4);
// Ensure values are the same after sorting to undo the Reorder
Store(f0, d32, expected.get() + 0);
Store(f1, d32, expected.get() + N);
Store(promoted0, d32, actual.get() + 0);
Store(promoted1, d32, actual.get() + N);
Sort(expected.get(), 2 * N);
Sort(actual.get(), 2 * N);
HWY_ASSERT_VEC_EQ(d32, expected.get() + 0, Load(d32, actual.get() + 0));
HWY_ASSERT_VEC_EQ(d32, expected.get() + N, Load(d32, actual.get() + N));
}
#else // HWY_SCALAR
(void)d32;
#endif
}
};
class TestIntegerReorderDemote2To {
#if HWY_TARGET != HWY_SCALAR
private:
// In-place N^2 selection sort to avoid dependencies
template <class T>
static void Sort(T* p, size_t count) {
for (size_t i = 0; i < count - 1; ++i) {
// Find min_element
size_t idx_min = i;
for (size_t j = i + 1; j < count; j++) {
if (p[j] < p[idx_min]) {
idx_min = j;
}
}
// Swap with current
const T tmp = p[i];
p[i] = p[idx_min];
p[idx_min] = tmp;
}
}
template <class T, class D, class DN>
static void DoIntegerReorderDemote2ToTest(DN dn, T /* t */, D d) {
using TN = TFromD<DN>;
const size_t N = Lanes(d);
const size_t twiceN = N * 2;
auto from = AllocateAligned<T>(twiceN);
auto expected = AllocateAligned<TN>(twiceN);
auto actual = AllocateAligned<TN>(twiceN);
HWY_ASSERT(from && expected && actual);
// Narrower range in the wider type, for clamping before we cast
const T min = static_cast<T>(IsSigned<T>() ? LimitsMin<TN>() : TN{0});
const T max = LimitsMax<TN>();
RandomState rng;
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < twiceN; ++i) {
const uint64_t bits = rng();
CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size
expected[i] = static_cast<TN>(HWY_MIN(HWY_MAX(min, from[i]), max));
}
const auto in_1 = Load(d, from.get());
const auto in_2 = Load(d, from.get() + N);
const auto demoted_vect = ReorderDemote2To(dn, in_1, in_2);
Store(demoted_vect, dn, actual.get());
Sort(actual.get(), twiceN);
Sort(expected.get(), twiceN);
HWY_ASSERT_VEC_EQ(dn, expected.get(), Load(dn, actual.get()));
}
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < twiceN; ++i) {
const uint64_t bits = rng();
CopyBytes<sizeof(TN)>(&bits, &expected[i]); // not same size
if (!IsSigned<T>() && IsSigned<TN>()) {
expected[i] &= static_cast<TN>(max);
}
from[i] = static_cast<T>(expected[i]);
}
const auto in_1 = Load(d, from.get());
const auto in_2 = Load(d, from.get() + N);
const auto demoted_vect = ReorderDemote2To(dn, in_1, in_2);
Store(demoted_vect, dn, actual.get());
Sort(actual.get(), twiceN);
Sort(expected.get(), twiceN);
HWY_ASSERT_VEC_EQ(dn, expected.get(), Load(dn, actual.get()));
}
}
#endif
public:
template <typename T, class D>
HWY_NOINLINE void operator()(T /*t*/, D d) {
#if HWY_TARGET != HWY_SCALAR
const RepartitionToNarrow<D> dn;
const RebindToSigned<decltype(dn)> dn_i;
const RebindToUnsigned<decltype(dn)> dn_u;
DoIntegerReorderDemote2ToTest(dn_i, T(), d);
DoIntegerReorderDemote2ToTest(dn_u, T(), d);
#else
(void)d;
#endif
}
};
HWY_NOINLINE void TestAllReorderDemote2To() {
ForUI163264(ForShrinkableVectors<TestIntegerReorderDemote2To>());
ForShrinkableVectors<TestReorderDemote2To>()(float());
}
struct TestFloatOrderedDemote2To {
template <typename TF, class DF>
HWY_NOINLINE void operator()(TF /*t*/, DF df) {
#if HWY_TARGET != HWY_SCALAR
const Repartition<bfloat16_t, decltype(df)> dbf16;
const RebindToUnsigned<decltype(dbf16)> du16;
const RebindToUnsigned<decltype(df)> du32;
const Half<decltype(du16)> du16_half;
const size_t N = Lanes(df);
const size_t twiceN = N * 2;
auto from = AllocateAligned<TF>(twiceN);
auto expected = AllocateAligned<bfloat16_t>(twiceN);
HWY_ASSERT(from && expected);
const auto u16_zero_vect = Zero(du16);
const auto u16_one_vect = Set(du16, 1);
RandomState rng;
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < twiceN; ++i) {
do {
const uint64_t bits = rng();
CopyBytes<sizeof(TF)>(&bits, &from[i]); // not same size
} while (!IsFiniteT(from[i]));
uint32_t u32Bits;
CopyBytes<sizeof(uint32_t)>(&from[i], &u32Bits);
const uint16_t expected_bf16_bits =
static_cast<uint16_t>(u32Bits >> 16);
CopyBytes<sizeof(bfloat16_t)>(&expected_bf16_bits, &expected[i]);
}
const auto in_1 = Load(df, from.get());
const auto in_2 = Load(df, from.get() + N);
const auto actual = OrderedDemote2To(dbf16, in_1, in_2);
// Adjust expected to account for any possible rounding that was
// carried out by the OrderedDemote2To operation
auto expected_vect = BitCast(du16, Load(dbf16, expected.get()));
const auto low_f32_bits =
Combine(du16, TruncateTo(du16_half, BitCast(du32, in_2)),
TruncateTo(du16_half, BitCast(du32, in_1)));
// max_diff_from_expected is equal to (low_f32_bits == 0 ? 0 : 1)
const auto max_diff_from_expected =
Add(VecFromMask(du16, Eq(low_f32_bits, u16_zero_vect)), u16_one_vect);
// expected_adj is equal to (actual_bits - expected_bits == 1 &&
// max_diff_from_expected != 0) ? 1 : 0, where actual_bits is the bits of
// actual and expected_bits is the bits of expected.
auto expected_adj =
And(max_diff_from_expected,
VecFromMask(du16, Eq(Sub(BitCast(du16, actual), expected_vect),
u16_one_vect)));
// Increment expected_vect by expected_adj
expected_vect = Add(expected_vect, expected_adj);
// Store the adjusted expected_vect back into expected
Store(BitCast(dbf16, expected_vect), dbf16, expected.get());
HWY_ASSERT_VEC_EQ(dbf16, expected.get(), actual);
}
#else
(void)df;
#endif
}
};
class TestIntegerOrderedDemote2To {
#if HWY_TARGET != HWY_SCALAR
private:
template <class T, class D, class DN>
static void DoIntegerOrderedDemote2ToTest(DN dn, T /*t*/, D d) {
using TN = TFromD<DN>;
const size_t N = Lanes(d);
const size_t twiceN = N * 2;
auto from = AllocateAligned<T>(twiceN);
auto expected = AllocateAligned<TN>(twiceN);
HWY_ASSERT(from && expected);
// Narrower range in the wider type, for clamping before we cast
const T min = static_cast<T>(IsSigned<T>() ? LimitsMin<TN>() : TN{0});
const T max = LimitsMax<TN>();
RandomState rng;
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < twiceN; ++i) {
const uint64_t bits = rng();
CopyBytes<sizeof(T)>(&bits, &from[i]); // not same size
expected[i] = static_cast<TN>(HWY_MIN(HWY_MAX(min, from[i]), max));
}
const auto in_1 = Load(d, from.get());
const auto in_2 = Load(d, from.get() + N);
const auto actual = OrderedDemote2To(dn, in_1, in_2);
HWY_ASSERT_VEC_EQ(dn, expected.get(), actual);
}
for (size_t rep = 0; rep < AdjustedReps(1000); ++rep) {
for (size_t i = 0; i < twiceN; ++i) {
const uint64_t bits = rng();
CopyBytes<sizeof(TN)>(&bits, &expected[i]); // not same size
if (!IsSigned<T>() && IsSigned<TN>()) {
expected[i] &= static_cast<TN>(max);
}
from[i] = static_cast<T>(expected[i]);
}
const auto in_1 = Load(d, from.get());
const auto in_2 = Load(d, from.get() + N);
const auto actual = OrderedDemote2To(dn, in_1, in_2);
HWY_ASSERT_VEC_EQ(dn, expected.get(), actual);
}
}
#endif
public:
template <typename T, class D>
HWY_NOINLINE void operator()(T /*t*/, D d) {
#if HWY_TARGET != HWY_SCALAR
const RepartitionToNarrow<D> dn;
const RebindToSigned<decltype(dn)> dn_i;
const RebindToUnsigned<decltype(dn)> dn_u;
DoIntegerOrderedDemote2ToTest(dn_i, T(), d);
DoIntegerOrderedDemote2ToTest(dn_u, T(), d);
#else
(void)d;
#endif
}
};
HWY_NOINLINE void TestAllOrderedDemote2To() {
ForUI163264(ForShrinkableVectors<TestIntegerOrderedDemote2To>());
ForShrinkableVectors<TestFloatOrderedDemote2To>()(float());
}
struct TestI32F64 {
template <typename TF, class DF>
HWY_NOINLINE void operator()(TF /*unused*/, const DF df) {
using TI = int32_t;
const Rebind<TI, DF> di;
const size_t N = Lanes(df);
// Integer positive
HWY_ASSERT_VEC_EQ(di, Iota(di, TI(4)), DemoteTo(di, Iota(df, TF(4.0))));
// Integer negative
HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N)), DemoteTo(di, Iota(df, -TF(N))));
// Above positive
HWY_ASSERT_VEC_EQ(di, Iota(di, TI(2)), DemoteTo(di, Iota(df, TF(2.001))));
// Below positive
HWY_ASSERT_VEC_EQ(di, Iota(di, TI(3)), DemoteTo(di, Iota(df, TF(3.9999))));
const TF eps = static_cast<TF>(0.0001);
// Above negative
HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N)),
DemoteTo(di, Iota(df, -TF(N + 1) + eps)));
// Below negative
HWY_ASSERT_VEC_EQ(di, Iota(di, -TI(N + 1)),
DemoteTo(di, Iota(df, -TF(N + 1) - eps)));
// Huge positive float
HWY_ASSERT_VEC_EQ(di, Set(di, LimitsMax<TI>()),
DemoteTo(di, Set(df, TF(1E12))));
// Huge negative float
HWY_ASSERT_VEC_EQ(di, Set(di, LimitsMin<TI>()),
DemoteTo(di, Set(df, TF(-1E12))));
}
};
HWY_NOINLINE void TestAllI32F64() {
#if HWY_HAVE_FLOAT64
ForDemoteVectors<TestI32F64>()(double());
#endif
}
// NOLINTNEXTLINE(google-readability-namespace-comments)
} // namespace HWY_NAMESPACE
} // namespace hwy
HWY_AFTER_NAMESPACE();
#endif // !HWY_IS_MSAN
#if HWY_ONCE
namespace hwy {
#if !HWY_IS_MSAN
HWY_BEFORE_TEST(HwyDemoteTest);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToInt);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToMixed);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToFloat);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllDemoteToBF16);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllReorderDemote2To);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllOrderedDemote2To);
HWY_EXPORT_AND_TEST_P(HwyDemoteTest, TestAllI32F64);
#endif // !HWY_IS_MSAN
} // namespace hwy
#endif
|