-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathexecute_floating_point_test.cpp
2128 lines (1806 loc) · 85.3 KB
/
execute_floating_point_test.cpp
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Fizzy: A fast WebAssembly interpreter
// Copyright 2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0
#include "execute.hpp"
#include "instructions.hpp"
#include "parser.hpp"
#include "trunc_boundaries.hpp"
#include <gtest/gtest.h>
#include <test/utils/asserts.hpp>
#include <test/utils/floating_point_utils.hpp>
#include <test/utils/hex.hpp>
#include <array>
#include <cfenv>
#include <cmath>
// Enables access and modification of the floating-point environment.
// Here we use it to change rounding direction in tests.
// Although required by the C standard, neither GCC nor Clang supports it.
#pragma STDC FENV_ACCESS ON
using namespace fizzy;
using namespace fizzy::test;
MATCHER_P(CanonicalNaN, value, "result with a canonical NaN")
{
(void)value;
if (arg.trapped || !arg.has_value)
return false;
const auto result_value = arg.value.template as<value_type>();
return FP<value_type>{result_value}.is_canonical_nan();
}
MATCHER_P(ArithmeticNaN, value, "result with an arithmetic NaN")
{
(void)value;
if (arg.trapped || !arg.has_value)
return false;
const auto result_value = arg.value.template as<value_type>();
return FP<value_type>{result_value}.is_arithmetic_nan();
}
namespace
{
constexpr auto all_rounding_directions = {FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO};
template <typename T>
class TestValues
{
using Limits = typename FP<T>::Limits;
inline static const std::array m_values{
T{0.0},
Limits::denorm_min(),
std::nextafter(Limits::denorm_min(), Limits::infinity()),
std::nextafter(Limits::min(), T{0}),
Limits::min(),
std::nextafter(Limits::min(), Limits::infinity()),
std::nextafter(T{1.0}, T{0}),
T{1.0},
std::nextafter(T{1.0}, Limits::infinity()),
std::nextafter(Limits::max(), T{0}),
Limits::max(),
Limits::infinity(),
// Canonical NaN:
FP<T>::nan(FP<T>::canon),
// Arithmetic NaNs:
FP<T>::nan((FP<T>::canon << 1) - 1), // All bits set.
FP<T>::nan(FP<T>::canon | (FP<T>::canon >> 1)), // Two top bits set.
FP<T>::nan(FP<T>::canon + 1),
// Signaling (not arithmetic) NaNs:
FP<T>::nan(FP<T>::canon >> 1), // "Standard" signaling NaN.
FP<T>::nan(2),
FP<T>::nan(1),
};
public:
using Iterator = typename decltype(m_values)::const_iterator;
static constexpr auto num_nans = 7;
static constexpr auto num_positive = m_values.size() - num_nans;
static constexpr Iterator first_non_zero = &m_values[1];
static constexpr Iterator canonical_nan = &m_values[num_positive];
static constexpr Iterator first_noncanonical_nan = canonical_nan + 1;
static constexpr Iterator infinity = &m_values[num_positive - 1];
class Range
{
Iterator m_begin;
Iterator m_end;
public:
constexpr Range(Iterator begin, Iterator end) noexcept : m_begin{begin}, m_end{end} {}
constexpr Iterator begin() const { return m_begin; }
constexpr Iterator end() const { return m_end; }
constexpr size_t size() const { return static_cast<size_t>(m_end - m_begin); }
};
// The list of positive floating-point values without zero, infinity and NaNs.
static constexpr Range positive_nonzero_finite() noexcept { return {first_non_zero, infinity}; }
// The list of positive floating-point values without zero and NaNs (includes infinity).
static constexpr Range positive_nonzero_infinite() noexcept
{
return {first_non_zero, canonical_nan};
}
// The list of positive NaN values.
static constexpr Range positive_nans() noexcept { return {canonical_nan, m_values.end()}; }
// The list of positive non-canonical NaN values (including signaling NaNs).
static constexpr Range positive_noncanonical_nans() noexcept
{
return {first_noncanonical_nan, m_values.end()};
}
// The list of positive floating-point values with zero, infinity and NaNs.
static constexpr Range positive_all() noexcept { return {m_values.begin(), m_values.end()}; }
// The list of floating-point values, including infinities.
// They are strictly ordered (ordered_values[i] < ordered_values[j] for i<j).
// Therefore -0 is omitted. This allows determining the relation of any pair of values only
// knowing values' position in the array.
static auto& ordered() noexcept
{
static const auto ordered_values = [] {
constexpr auto ps = positive_nonzero_infinite();
std::array<T, ps.size() * 2 + 1> a;
auto it = std::begin(a);
it = std::transform(std::make_reverse_iterator(std::end(ps)),
std::make_reverse_iterator(std::begin(ps)), it, std::negate<T>{});
*it++ = T{0.0};
std::copy(std::begin(ps), std::end(ps), it);
return a;
}();
return ordered_values;
}
// The list of floating-point values, including infinities and NaNs.
// They are strictly ordered (ordered_values[i] < ordered_values[j] for i<j) or NaNs.
// Therefore -0 is omitted. This allows determining the relation of any pair of values only
// knowing values' position in the array.
static auto& ordered_and_nans() noexcept
{
static const auto ordered_values = [] {
const auto& without_nans = ordered();
const auto nans = positive_nans();
std::array<T, positive_all().size() * 2 - 1> a;
auto it = std::begin(a);
it = std::copy(std::begin(without_nans), std::end(without_nans), it);
it = std::copy(std::begin(nans), std::end(nans), it);
std::transform(std::begin(nans), std::end(nans), it, std::negate<T>{});
return a;
}();
return ordered_values;
}
};
} // namespace
TEST(execute_floating_point, f32_const)
{
/* wat2wasm
(func (result f32)
f32.const 4194304.1
)
*/
const auto wasm = from_hex("0061736d010000000105016000017d030201000a09010700430000804a0b");
auto instance = instantiate(parse(wasm));
EXPECT_THAT(execute(*instance, 0, {}), Result(4194304.1f));
}
TEST(execute_floating_point, f64_const)
{
/* wat2wasm
(func (result f64)
f64.const 8589934592.1
)
*/
const auto wasm =
from_hex("0061736d010000000105016000017c030201000a0d010b0044cdcc0000000000420b");
auto instance = instantiate(parse(wasm));
EXPECT_THAT(execute(*instance, 0, {}), Result(8589934592.1));
}
/// Compile-time information about a Wasm type.
template <typename T>
struct WasmTypeTraits;
template <>
struct WasmTypeTraits<float>
{
static constexpr auto name = "f32";
static constexpr auto valtype = ValType::f32;
};
template <>
struct WasmTypeTraits<double>
{
static constexpr auto name = "f64";
static constexpr auto valtype = ValType::f64;
};
struct WasmTypeName
{
template <typename T>
static std::string GetName(int)
{
return WasmTypeTraits<T>::name;
}
};
template <typename T>
class execute_floating_point_types : public testing::Test
{
public:
using L = typename FP<T>::Limits;
// The [int_only_begin; int_only_end) is the range of floating-point numbers, where each
// representable number is an integer and there are no fractional numbers between them.
// These numbers are represented as mantissa [0x1.00..0; 0x1.ff..f]
// and exponent 2**(mantissa digits without implicit leading 1).
// The main point of using int_only_begin is to tests nearest() as for int_only_begin - 0.5 and
// int_only_begin - 1.5 we have equal distance to nearby integer values.
// (Integer shift is used instead of std::pow() because of the clang compiler bug).
static constexpr auto int_only_begin = T{uint64_t{1} << (L::digits - 1)};
static constexpr auto int_only_end = T{uint64_t{1} << L::digits};
// The list of rounding test cases as pairs (input, expected_trunc) with only positive inputs.
inline static const std::pair<T, T> positive_trunc_tests[] = {
// Checks the following rule common for all rounding instructions:
// f(+-0) = +-0
{0, 0},
{L::denorm_min(), 0},
{L::min(), 0},
{T{0.1f}, T{0}},
{std::nextafter(T{0.5}, T{0}), T{0}},
{T{0.5}, T{0}},
{std::nextafter(T{0.5}, T{1}), T{0}},
{std::nextafter(T{1}, T{0}), T{0}},
{T{1}, T{1}},
{std::nextafter(T{1}, T{2}), T{1}},
{std::nextafter(T{1.5}, T{1}), T{1}},
{T{1.5}, T{1}},
{std::nextafter(T{1.5}, T{2}), T{1}},
{std::nextafter(T{2}, T{1}), T{1}},
{T{2}, T{2}},
{std::nextafter(T{2}, T{3}), T{2}},
{int_only_begin - T{2}, int_only_begin - T{2}},
{int_only_begin - T{1.5}, int_only_begin - T{2}},
{int_only_begin - T{1}, int_only_begin - T{1}},
{int_only_begin - T{0.5}, int_only_begin - T{1}},
{int_only_begin, int_only_begin},
{int_only_begin + T{1}, int_only_begin + T{1}},
{int_only_end - T{1}, int_only_end - T{1}},
{int_only_end, int_only_end},
{int_only_end + T{2}, int_only_end + T{2}},
{L::max(), L::max()},
// Checks the following rule common for all rounding instructions:
// f(+-inf) = +-inf
{L::infinity(), L::infinity()},
};
/// Creates a wasm module with a single function for the given instructions opcode.
/// The opcode is converted to match the type, e.g. f32_add -> f64_add.
static bytes get_numeric_instruction_code(
bytes_view template_code, Instr template_opcode, Instr opcode)
{
constexpr auto f64_variant_offset =
static_cast<uint8_t>(Instr::f64_add) - static_cast<uint8_t>(Instr::f32_add);
// Convert to f64 variant if needed.
const auto typed_opcode =
std::is_same_v<T, double> ?
static_cast<uint8_t>(static_cast<uint8_t>(opcode) + f64_variant_offset) :
static_cast<uint8_t>(opcode);
bytes wasm{template_code};
constexpr auto template_type = static_cast<uint8_t>(ValType::f32);
const auto template_opcode_byte = static_cast<uint8_t>(template_opcode);
const auto opcode_arity = get_instruction_type_table()[template_opcode_byte].inputs.size();
EXPECT_EQ(std::count(wasm.begin(), wasm.end(), template_type), opcode_arity + 1);
EXPECT_EQ(std::count(wasm.begin(), wasm.end(), template_opcode_byte), 1);
std::replace(wasm.begin(), wasm.end(), template_type,
static_cast<uint8_t>(WasmTypeTraits<T>::valtype));
std::replace(wasm.begin(), wasm.end(), template_opcode_byte, typed_opcode);
return wasm;
}
static bytes get_unop_code(Instr opcode)
{
/* wat2wasm
(func (param f32) (result f32)
(f32.abs (local.get 0))
)
*/
auto wasm = from_hex("0061736d0100000001060160017d017d030201000a0701050020008b0b");
return get_numeric_instruction_code(wasm, Instr::f32_abs, opcode);
}
static bytes get_binop_code(Instr opcode)
{
/* wat2wasm
(func (param f32 f32) (result f32)
(f32.add (local.get 0) (local.get 1))
)
*/
auto wasm = from_hex("0061736d0100000001070160027d7d017d030201000a0901070020002001920b");
return get_numeric_instruction_code(wasm, Instr::f32_add, opcode);
}
};
using FloatingPointTypes = testing::Types<float, double>;
TYPED_TEST_SUITE(execute_floating_point_types, FloatingPointTypes, WasmTypeName);
TYPED_TEST(execute_floating_point_types, test_values_selftest)
{
using TV = TestValues<TypeParam>;
EXPECT_EQ(std::size(TV::positive_nonzero_infinite()), TV::num_positive - 1);
EXPECT_EQ(std::size(TV::positive_nonzero_finite()), TV::num_positive - 2);
EXPECT_EQ(std::size(TV::positive_nans()), TV::num_nans);
EXPECT_EQ(std::size(TV::positive_noncanonical_nans()), TV::num_nans - 1);
EXPECT_EQ(std::size(TV::positive_all()), TV::num_positive + TV::num_nans);
for (const auto nan : TV::positive_nans())
EXPECT_TRUE(std::isnan(nan));
EXPECT_TRUE(FP<TypeParam>{*TV::canonical_nan}.is_canonical_nan());
for (const auto nan : TV::positive_noncanonical_nans())
{
EXPECT_TRUE(std::isnan(nan));
EXPECT_FALSE(FP<TypeParam>{nan}.is_canonical_nan());
}
for (const auto p : TV::positive_all())
EXPECT_EQ(std::signbit(p), 0);
const auto& ordered = TV::ordered();
auto current = ordered[0];
EXPECT_EQ(current, -FP<TypeParam>::Limits::infinity());
for (size_t i = 1; i < ordered.size(); ++i)
{
EXPECT_LT(current, ordered[i]);
current = ordered[i];
}
const auto& ordered_and_nans = TV::ordered_and_nans();
current = ordered_and_nans[0];
EXPECT_EQ(current, -FP<TypeParam>::Limits::infinity());
size_t nan_start_index = 0;
for (size_t i = 1; i < ordered_and_nans.size(); ++i)
{
if (std::isnan(ordered_and_nans[i]))
{
nan_start_index = i;
break;
}
EXPECT_LT(current, ordered_and_nans[i]);
current = ordered_and_nans[i];
}
ASSERT_NE(nan_start_index, 0);
for (size_t i = nan_start_index; i < ordered_and_nans.size(); ++i)
{
EXPECT_TRUE(std::isnan(ordered_and_nans[i]));
}
}
TYPED_TEST(execute_floating_point_types, nan_matchers)
{
using testing::Not;
using FP = FP<TypeParam>;
EXPECT_THAT(Void, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(Trap, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{TypeParam{}}}, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{FP::nan(FP::canon)}}, CanonicalNaN(TypeParam{}));
EXPECT_THAT(ExecutionResult{Value{-FP::nan(FP::canon)}}, CanonicalNaN(TypeParam{}));
EXPECT_THAT(ExecutionResult{Value{FP::nan(FP::canon + 1)}}, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{-FP::nan(FP::canon + 1)}}, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{FP::nan(1)}}, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{-FP::nan(1)}}, Not(CanonicalNaN(TypeParam{})));
EXPECT_THAT(Void, Not(ArithmeticNaN(TypeParam{})));
EXPECT_THAT(Trap, Not(ArithmeticNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{TypeParam{}}}, Not(ArithmeticNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{FP::nan(FP::canon)}}, ArithmeticNaN(TypeParam{}));
EXPECT_THAT(ExecutionResult{Value{-FP::nan(FP::canon)}}, ArithmeticNaN(TypeParam{}));
EXPECT_THAT(ExecutionResult{Value{FP::nan(FP::canon + 1)}}, ArithmeticNaN(TypeParam{}));
EXPECT_THAT(ExecutionResult{Value{-FP::nan(FP::canon + 1)}}, ArithmeticNaN(TypeParam{}));
EXPECT_THAT(ExecutionResult{Value{FP::nan(1)}}, Not(ArithmeticNaN(TypeParam{})));
EXPECT_THAT(ExecutionResult{Value{-FP::nan(1)}}, Not(ArithmeticNaN(TypeParam{})));
}
TYPED_TEST(execute_floating_point_types, unop_nan_propagation)
{
// Tests NaN propagation in unary instructions (unop).
// If NaN input is canonical NN, the result must be the canonical NaN.
// Otherwise, the result must be an arithmetic NaN.
// The list of instructions to be tested.
// Only f32 variants, but f64 variants are going to be covered as well.
constexpr Instr opcodes[] = {
Instr::f32_ceil,
Instr::f32_floor,
Instr::f32_trunc,
Instr::f32_nearest,
Instr::f32_sqrt,
};
for (const auto op : opcodes)
{
auto instance = instantiate(parse(this->get_unop_code(op)));
const auto cnan = FP<TypeParam>::nan(FP<TypeParam>::canon);
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
EXPECT_THAT(execute(*instance, 0, {cnan}), CanonicalNaN(TypeParam{}));
EXPECT_THAT(execute(*instance, 0, {-cnan}), CanonicalNaN(TypeParam{}));
for (const auto nan : TestValues<TypeParam>::positive_noncanonical_nans())
{
const auto res1 = execute(*instance, 0, {nan});
EXPECT_THAT(res1, ArithmeticNaN(TypeParam{}))
<< std::hex << FP<TypeParam>{res1.value.template as<TypeParam>()}.nan_payload();
const auto res2 = execute(*instance, 0, {-nan});
EXPECT_THAT(res2, ArithmeticNaN(TypeParam{}))
<< std::hex << FP<TypeParam>{res2.value.template as<TypeParam>()}.nan_payload();
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
}
TYPED_TEST(execute_floating_point_types, binop_nan_propagation)
{
// Tests NaN propagation in binary instructions (binop).
// If all NaN inputs are canonical the result must be the canonical NaN.
// Otherwise, the result must be an arithmetic NaN.
// The list of instructions to be tested.
// Only f32 variants, but f64 variants are going to be covered as well.
constexpr Instr opcodes[] = {
Instr::f32_add,
Instr::f32_sub,
Instr::f32_mul,
Instr::f32_div,
Instr::f32_min,
Instr::f32_max,
};
for (const auto op : opcodes)
{
auto instance = instantiate(parse(this->get_binop_code(op)));
const auto exec = [&](auto arg1, auto arg2) { return execute(*instance, 0, {arg1, arg2}); };
constexpr auto q = TypeParam{1.0};
const auto cnan = FP<TypeParam>::nan(FP<TypeParam>::canon);
// TODO: Consider more restrictive tests where the sign of NaN values is also checked.
EXPECT_THAT(exec(q, cnan), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(q, -cnan), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(cnan, q), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(-cnan, q), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(cnan, cnan), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(cnan, -cnan), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(-cnan, cnan), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(-cnan, -cnan), CanonicalNaN(TypeParam{}));
for (const auto nan : TestValues<TypeParam>::positive_noncanonical_nans())
{
EXPECT_THAT(exec(q, nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(q, -nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(nan, q), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-nan, q), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(nan, nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(nan, -nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-nan, nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-nan, -nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(nan, cnan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(nan, -cnan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-nan, cnan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-nan, -cnan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(cnan, nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(cnan, -nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-cnan, nan), ArithmeticNaN(TypeParam{}));
EXPECT_THAT(exec(-cnan, -nan), ArithmeticNaN(TypeParam{}));
}
}
}
TYPED_TEST(execute_floating_point_types, compare)
{
/* wat2wasm
(func (param f32 f32) (result i32) (f32.eq (local.get 0) (local.get 1)))
(func (param f32 f32) (result i32) (f32.ne (local.get 0) (local.get 1)))
(func (param f32 f32) (result i32) (f32.lt (local.get 0) (local.get 1)))
(func (param f32 f32) (result i32) (f32.gt (local.get 0) (local.get 1)))
(func (param f32 f32) (result i32) (f32.le (local.get 0) (local.get 1)))
(func (param f32 f32) (result i32) (f32.ge (local.get 0) (local.get 1)))
(func (param f64 f64) (result i32) (f64.eq (local.get 0) (local.get 1)))
(func (param f64 f64) (result i32) (f64.ne (local.get 0) (local.get 1)))
(func (param f64 f64) (result i32) (f64.lt (local.get 0) (local.get 1)))
(func (param f64 f64) (result i32) (f64.gt (local.get 0) (local.get 1)))
(func (param f64 f64) (result i32) (f64.le (local.get 0) (local.get 1)))
(func (param f64 f64) (result i32) (f64.ge (local.get 0) (local.get 1)))
*/
const auto wasm = from_hex(
"0061736d01000000010d0260027d7d017f60027c7c017f030d0c0000000000000101010101010a610c07002000"
"20015b0b0700200020015c0b0700200020015d0b0700200020015e0b0700200020015f0b070020002001600b07"
"0020002001610b070020002001620b070020002001630b070020002001640b070020002001650b070020002001"
"660b");
auto instance = instantiate(parse(wasm));
constexpr FuncIdx func_offset = std::is_same_v<TypeParam, float> ? 0 : 6;
const auto eq = [&](auto a, auto b) { return execute(*instance, func_offset + 0, {a, b}); };
const auto ne = [&](auto a, auto b) { return execute(*instance, func_offset + 1, {a, b}); };
const auto lt = [&](auto a, auto b) { return execute(*instance, func_offset + 2, {a, b}); };
const auto gt = [&](auto a, auto b) { return execute(*instance, func_offset + 3, {a, b}); };
const auto le = [&](auto a, auto b) { return execute(*instance, func_offset + 4, {a, b}); };
const auto ge = [&](auto a, auto b) { return execute(*instance, func_offset + 5, {a, b}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
// Check every pair from cartesian product of ordered_values.
const auto& ordered_values = TestValues<TypeParam>::ordered_and_nans();
for (size_t i = 0; i < std::size(ordered_values); ++i)
{
for (size_t j = 0; j < std::size(ordered_values); ++j)
{
const auto a = ordered_values[i];
const auto b = ordered_values[j];
if (std::isnan(a) || std::isnan(b))
{
EXPECT_THAT(eq(a, b), Result(0)) << a << "==" << b;
EXPECT_THAT(ne(a, b), Result(1)) << a << "!=" << b;
EXPECT_THAT(lt(a, b), Result(0)) << a << "<" << b;
EXPECT_THAT(gt(a, b), Result(0)) << a << ">" << b;
EXPECT_THAT(le(a, b), Result(0)) << a << "<=" << b;
EXPECT_THAT(ge(a, b), Result(0)) << a << ">=" << b;
}
else
{
EXPECT_THAT(eq(a, b), Result(uint32_t{i == j})) << a << "==" << b;
EXPECT_THAT(ne(a, b), Result(uint32_t{i != j})) << a << "!=" << b;
EXPECT_THAT(lt(a, b), Result(uint32_t{i < j})) << a << "<" << b;
EXPECT_THAT(gt(a, b), Result(uint32_t{i > j})) << a << ">" << b;
EXPECT_THAT(le(a, b), Result(uint32_t{i <= j})) << a << "<=" << b;
EXPECT_THAT(ge(a, b), Result(uint32_t{i >= j})) << a << ">=" << b;
}
}
}
// Negative zero. This is separate set of checks because -0.0 cannot be placed
// in the ordered_values array as -0.0 == 0.0.
EXPECT_THAT(eq(TypeParam{-0.0}, TypeParam{0.0}), Result(1));
EXPECT_THAT(ne(TypeParam{-0.0}, TypeParam{0.0}), Result(0));
EXPECT_THAT(lt(TypeParam{-0.0}, TypeParam{0.0}), Result(0));
EXPECT_THAT(gt(TypeParam{-0.0}, TypeParam{0.0}), Result(0));
EXPECT_THAT(le(TypeParam{-0.0}, TypeParam{0.0}), Result(1));
EXPECT_THAT(ge(TypeParam{-0.0}, TypeParam{0.0}), Result(1));
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, abs)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_abs)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
for (const auto p : TestValues<TypeParam>::positive_all())
{
// fabs(+-p) = +p
EXPECT_THAT(exec(p), Result(p));
EXPECT_THAT(exec(-p), Result(p));
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, neg)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_neg)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
for (const auto p : TestValues<TypeParam>::positive_all())
{
// fneg(+-p) = -+p
EXPECT_THAT(exec(p), Result(-p));
EXPECT_THAT(exec(-p), Result(p));
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, ceil)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_ceil)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
// fceil(-q) = -0 (if -1 < -q < 0)
// (also included in positive_trunc_tests, here for explicitness).
EXPECT_THAT(exec(std::nextafter(-TypeParam{1}, TypeParam{0})), Result(-TypeParam{0}));
EXPECT_THAT(exec(-FP<TypeParam>::Limits::denorm_min()), Result(-TypeParam{0}));
for (const auto& [arg, expected_trunc] :
execute_floating_point_types<TypeParam>::positive_trunc_tests)
{
// For positive values, the ceil() is trunc() + 1,
// unless the input is already an integer.
const auto expected_positive =
(arg == expected_trunc) ? expected_trunc : expected_trunc + TypeParam{1};
EXPECT_THAT(exec(arg), Result(expected_positive)) << arg << ": " << expected_positive;
// For negative values, the ceil() is trunc().
EXPECT_THAT(exec(-arg), Result(-expected_trunc)) << -arg << ": " << -expected_trunc;
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, floor)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_floor)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
// ffloor(+q) = +0 (if 0 < +q < 1)
// (also included in positive_trunc_tests, here for explicitness).
EXPECT_THAT(exec(FP<TypeParam>::Limits::denorm_min()), Result(TypeParam{0}));
EXPECT_THAT(exec(std::nextafter(TypeParam{1}, TypeParam{0})), Result(TypeParam{0}));
for (const auto& [arg, expected_trunc] :
execute_floating_point_types<TypeParam>::positive_trunc_tests)
{
// For positive values, the floor() is trunc().
EXPECT_THAT(exec(arg), Result(expected_trunc)) << arg << ": " << expected_trunc;
// For negative values, the floor() is trunc() - 1, unless the input is already an
// integer.
const auto expected_negative =
(arg == expected_trunc) ? -expected_trunc : -expected_trunc - TypeParam{1};
EXPECT_THAT(exec(-arg), Result(expected_negative)) << -arg << ": " << expected_negative;
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, trunc)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_trunc)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
// ftrunc(+q) = +0 (if 0 < +q < 1)
// (also included in positive_trunc_tests, here for explicitness).
EXPECT_THAT(exec(FP<TypeParam>::Limits::denorm_min()), Result(TypeParam{0}));
EXPECT_THAT(exec(std::nextafter(TypeParam{1}, TypeParam{0})), Result(TypeParam{0}));
// ftrunc(-q) = -0 (if -1 < -q < 0)
// (also included in positive_trunc_tests, here for explicitness).
EXPECT_THAT(exec(std::nextafter(-TypeParam{1}, TypeParam{0})), Result(-TypeParam{0}));
EXPECT_THAT(exec(-FP<TypeParam>::Limits::denorm_min()), Result(-TypeParam{0}));
for (const auto& [arg, expected] :
execute_floating_point_types<TypeParam>::positive_trunc_tests)
{
EXPECT_THAT(exec(arg), Result(expected)) << arg << ": " << expected;
EXPECT_THAT(exec(-arg), Result(-expected)) << -arg << ": " << -expected;
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, nearest)
{
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_nearest)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
ASSERT_EQ(std::fegetround(), FE_TONEAREST);
for (const auto rounding_direction : all_rounding_directions)
{
ASSERT_EQ(std::fesetround(rounding_direction), 0);
SCOPED_TRACE(rounding_direction);
// fnearest(+q) = +0 (if 0 < +q <= 0.5)
// (also included in positive_trunc_tests, here for explicitness).
EXPECT_THAT(exec(FP<TypeParam>::Limits::denorm_min()), Result(TypeParam{0}));
EXPECT_THAT(exec(std::nextafter(TypeParam{0.5}, TypeParam{0})), Result(TypeParam{0}));
EXPECT_THAT(exec(TypeParam{0.5}), Result(TypeParam{0}));
// fnearest(-q) = -0 (if -0.5 <= -q < 0)
// (also included in positive_trunc_tests, here for explicitness).
EXPECT_THAT(exec(-TypeParam{0.5}), Result(-TypeParam{0}));
EXPECT_THAT(exec(std::nextafter(-TypeParam{0.5}, TypeParam{0})), Result(-TypeParam{0}));
EXPECT_THAT(exec(-FP<TypeParam>::Limits::denorm_min()), Result(-TypeParam{0}));
// This checks all other specification-driven test cases, including:
// fnearest(+-inf) = +-inf
// fnearest(+-0) = +-0
for (const auto& [arg, expected_trunc] : this->positive_trunc_tests)
{
ASSERT_EQ(expected_trunc, std::trunc(expected_trunc));
const auto is_even = std::fmod(expected_trunc, TypeParam{2}) == TypeParam{0};
// Computing "expected" is expanded to individual cases to check code coverage.
TypeParam expected;
if (arg == expected_trunc)
{
expected = expected_trunc;
}
else
{
const auto diff = arg - expected_trunc;
ASSERT_LT(diff, TypeParam{1});
ASSERT_GT(diff, TypeParam{0});
if (diff < TypeParam{0.5})
expected = expected_trunc; // NOLINT(bugprone-branch-clone)
else if (diff > TypeParam{0.5})
expected = expected_trunc + TypeParam{1}; // NOLINT(bugprone-branch-clone)
else if (is_even)
expected = expected_trunc;
else
expected = expected_trunc + TypeParam{1};
}
EXPECT_THAT(exec(arg), Result(expected)) << arg << ": " << expected;
EXPECT_THAT(exec(-arg), Result(-expected)) << -arg << ": " << -expected;
}
}
ASSERT_EQ(std::fesetround(FE_TONEAREST), 0);
}
TYPED_TEST(execute_floating_point_types, sqrt)
{
using FP = FP<TypeParam>;
using Limits = typename FP::Limits;
auto instance = instantiate(parse(this->get_unop_code(Instr::f32_sqrt)));
const auto exec = [&](auto arg) { return execute(*instance, 0, {arg}); };
// fsqrt(-inf) = nan:canonical
EXPECT_THAT(exec(-Limits::infinity()), CanonicalNaN(TypeParam{}));
// fsqrt(+inf) = +inf
EXPECT_THAT(exec(Limits::infinity()), Result(Limits::infinity()));
// fsqrt(+-0) = +-0
EXPECT_THAT(exec(TypeParam{0.0}), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-TypeParam{0.0}), Result(-TypeParam{0.0}));
for (const auto p : TestValues<TypeParam>::positive_nonzero_finite())
{
// fsqrt(-p) = nan:canonical
EXPECT_THAT(exec(-p), CanonicalNaN(TypeParam{}));
}
EXPECT_THAT(exec(TypeParam{1}), Result(TypeParam{1}));
EXPECT_THAT(exec(TypeParam{4}), Result(TypeParam{2}));
EXPECT_THAT(exec(TypeParam{0x1.21p6}), Result(TypeParam{0x1.1p3}));
}
TYPED_TEST(execute_floating_point_types, add)
{
using FP = FP<TypeParam>;
using Limits = typename FP::Limits;
auto instance = instantiate(parse(this->get_binop_code(Instr::f32_add)));
const auto exec = [&](auto arg1, auto arg2) { return execute(*instance, 0, {arg1, arg2}); };
// fadd(+-inf, -+inf) = nan:canonical
EXPECT_THAT(exec(Limits::infinity(), -Limits::infinity()), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(-Limits::infinity(), Limits::infinity()), CanonicalNaN(TypeParam{}));
// fadd(+-inf, +-inf) = +-inf
EXPECT_THAT(exec(Limits::infinity(), Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), -Limits::infinity()), Result(-Limits::infinity()));
// fadd(+-0, -+0) = +0
EXPECT_THAT(exec(TypeParam{0.0}, -TypeParam{0.0}), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-TypeParam{0.0}, TypeParam{0.0}), Result(TypeParam{0.0}));
// fadd(+-0, +-0) = +-0
EXPECT_THAT(exec(TypeParam{0.0}, TypeParam{0.0}), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-TypeParam{0.0}, -TypeParam{0.0}), Result(-TypeParam{0.0}));
// fadd(z1, +-0) = z1 (for z1 = +-inf)
EXPECT_THAT(exec(Limits::infinity(), TypeParam{0.0}), Result(Limits::infinity()));
EXPECT_THAT(exec(Limits::infinity(), -TypeParam{0.0}), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), TypeParam{0.0}), Result(-Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), -TypeParam{0.0}), Result(-Limits::infinity()));
// fadd(+-0, z2) = z2 (for z2 = +-inf)
EXPECT_THAT(exec(TypeParam{0.0}, Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(TypeParam{0.0}, -Limits::infinity()), Result(-Limits::infinity()));
EXPECT_THAT(exec(-TypeParam{0.0}, Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(-TypeParam{0.0}, -Limits::infinity()), Result(-Limits::infinity()));
for (const auto q : TestValues<TypeParam>::positive_nonzero_finite())
{
// fadd(z1, +-inf) = +-inf (for z1 = +-q)
EXPECT_THAT(exec(q, Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(q, -Limits::infinity()), Result(-Limits::infinity()));
EXPECT_THAT(exec(-q, Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(-q, -Limits::infinity()), Result(-Limits::infinity()));
// fadd(+-inf, z2) = +-inf (for z2 = +-q)
EXPECT_THAT(exec(Limits::infinity(), q), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), q), Result(-Limits::infinity()));
EXPECT_THAT(exec(Limits::infinity(), -q), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), -q), Result(-Limits::infinity()));
// fadd(z1, +-0) = z1 (for z1 = +-q)
EXPECT_THAT(exec(q, TypeParam{0.0}), Result(q));
EXPECT_THAT(exec(q, -TypeParam{0.0}), Result(q));
EXPECT_THAT(exec(-q, TypeParam{0.0}), Result(-q));
EXPECT_THAT(exec(-q, -TypeParam{0.0}), Result(-q));
// fadd(+-0, z2) = z2 (for z2 = +-q)
EXPECT_THAT(exec(TypeParam{0.0}, q), Result(q));
EXPECT_THAT(exec(-TypeParam{0.0}, q), Result(q));
EXPECT_THAT(exec(TypeParam{0.0}, -q), Result(-q));
EXPECT_THAT(exec(-TypeParam{0.0}, -q), Result(-q));
// fadd(+-q, -+q) = +0
EXPECT_THAT(exec(q, -q), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-q, q), Result(TypeParam{0.0}));
}
EXPECT_THAT(exec(TypeParam{0x0.287p2}, TypeParam{0x1.FFp4}), Result(TypeParam{0x1.048Ep5}));
}
TYPED_TEST(execute_floating_point_types, sub)
{
using FP = FP<TypeParam>;
using Limits = typename FP::Limits;
auto instance = instantiate(parse(this->get_binop_code(Instr::f32_sub)));
const auto exec = [&](auto arg1, auto arg2) { return execute(*instance, 0, {arg1, arg2}); };
// fsub(+-inf, +-inf) = nan:canonical
EXPECT_THAT(exec(Limits::infinity(), Limits::infinity()), CanonicalNaN(TypeParam{}));
EXPECT_THAT(exec(-Limits::infinity(), -Limits::infinity()), CanonicalNaN(TypeParam{}));
// fsub(+-inf, -+inf) = +-inf
EXPECT_THAT(exec(Limits::infinity(), -Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), Limits::infinity()), Result(-Limits::infinity()));
// fsub(+-0, +-0) = +0
EXPECT_THAT(exec(TypeParam{0.0}, TypeParam{0.0}), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-TypeParam{0.0}, -TypeParam{0.0}), Result(TypeParam{0.0}));
// fsub(+-0, -+0) = +-0
EXPECT_THAT(exec(TypeParam{0.0}, -TypeParam{0.0}), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-TypeParam{0.0}, TypeParam{0.0}), Result(-TypeParam{0.0}));
// fsub(z1, +-0) = z1 (for z1 = +-inf)
EXPECT_THAT(exec(Limits::infinity(), TypeParam{0.0}), Result(Limits::infinity()));
EXPECT_THAT(exec(Limits::infinity(), -TypeParam{0.0}), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), TypeParam{0.0}), Result(-Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), -TypeParam{0.0}), Result(-Limits::infinity()));
for (const auto q : TestValues<TypeParam>::positive_nonzero_finite())
{
// fsub(z1, +-inf) = -+inf (for z1 = +-q)
EXPECT_THAT(exec(q, Limits::infinity()), Result(-Limits::infinity()));
EXPECT_THAT(exec(q, -Limits::infinity()), Result(Limits::infinity()));
EXPECT_THAT(exec(-q, Limits::infinity()), Result(-Limits::infinity()));
EXPECT_THAT(exec(-q, -Limits::infinity()), Result(Limits::infinity()));
// fsub(+-inf, z2) = +-inf (for z2 = +-q)
EXPECT_THAT(exec(Limits::infinity(), q), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), q), Result(-Limits::infinity()));
EXPECT_THAT(exec(Limits::infinity(), -q), Result(Limits::infinity()));
EXPECT_THAT(exec(-Limits::infinity(), -q), Result(-Limits::infinity()));
// fsub(z1, +-0) = z1 (for z1 = +-q)
EXPECT_THAT(exec(q, TypeParam{0.0}), Result(q));
EXPECT_THAT(exec(q, -TypeParam{0.0}), Result(q));
EXPECT_THAT(exec(-q, TypeParam{0.0}), Result(-q));
EXPECT_THAT(exec(-q, -TypeParam{0.0}), Result(-q));
// fsub(+-0, +-q2) = -+q2
EXPECT_THAT(exec(TypeParam{0.0}, q), Result(-q));
EXPECT_THAT(exec(-TypeParam{0.0}, -q), Result(q));
// Not part of the spec.
EXPECT_THAT(exec(TypeParam{0.0}, -q), Result(q));
EXPECT_THAT(exec(-TypeParam{0.0}, q), Result(-q));
// fsub(+-q, +-q) = +0
EXPECT_THAT(exec(q, q), Result(TypeParam{0.0}));
EXPECT_THAT(exec(-q, -q), Result(TypeParam{0.0}));
}
EXPECT_THAT(exec(TypeParam{0x1.048Ep5}, TypeParam{0x1.FFp4}), Result(TypeParam{0x0.287p2}));
}
TYPED_TEST(execute_floating_point_types, add_sub_neg_relation)
{
// Checks the note from the Wasm spec:
// > Up to the non-determinism regarding NaNs, it always holds that
// > fsub(z1, z2) = fadd(z1, fneg(z2)).
using FP = FP<TypeParam>;
/* wat2wasm
(func (param f32 f32) (result f32) (f32.sub (local.get 0) (local.get 1)))
(func (param f32 f32) (result f32) (f32.add (local.get 0) (f32.neg(local.get 1))))
(func (param f64 f64) (result f64) (f64.sub (local.get 0) (local.get 1)))
(func (param f64 f64) (result f64) (f64.add (local.get 0) (f64.neg(local.get 1))))
*/
const auto wasm = from_hex(
"0061736d01000000010d0260027d7d017d60027c7c017c030504000001010a2304070020002001930b08002000"
"20018c920b070020002001a10b0800200020019aa00b");
auto module = parse(wasm);
constexpr auto fn_offset = std::is_same_v<TypeParam, float> ? 0 : 2;
constexpr auto sub_fn_idx = 0 + fn_offset;
constexpr auto addneg_fn_idx = 1 + fn_offset;
auto instance = instantiate(std::move(module));
const auto sub = [&](auto a, auto b) { return execute(*instance, sub_fn_idx, {a, b}); };
const auto addneg = [&](auto a, auto b) { return execute(*instance, addneg_fn_idx, {a, b}); };
const auto& ordered_values = TestValues<TypeParam>::ordered_and_nans();
for (const auto z1 : ordered_values)
{
for (const auto z2 : ordered_values)