summaryrefslogtreecommitdiffstats
path: root/third_party/aom/test/simd_cmp_impl.h
blob: 28bd64a5bc022143f70a0635b87056479a679ec6 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/

#include <assert.h>
#include <string>
#include "./aom_dsp_rtcd.h"
#include "test/acm_random.h"
#include "aom_dsp/aom_simd.h"
#undef SIMD_INLINE
#define SIMD_INLINE static  // Don't enforce inlining
#include "aom_dsp/simd/v128_intrinsics_c.h"

// Machine tuned code goes into this file. This file is included from
// simd_cmp_sse2.cc, simd_cmp_ssse3.cc etc which define the macros
// ARCH (=neon, sse2, ssse3, etc), SIMD_NAMESPACE and ARCH_POSTFIX().

using libaom_test::ACMRandom;

namespace SIMD_NAMESPACE {

// Wrap templates around intrinsics using immediate values
template <int shift>
v64 imm_v64_shl_n_byte(v64 a) {
  return v64_shl_n_byte(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_byte(v64 a) {
  return v64_shr_n_byte(a, shift);
}
template <int shift>
v64 imm_v64_shl_n_8(v64 a) {
  return v64_shl_n_8(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_u8(v64 a) {
  return v64_shr_n_u8(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_s8(v64 a) {
  return v64_shr_n_s8(a, shift);
}
template <int shift>
v64 imm_v64_shl_n_16(v64 a) {
  return v64_shl_n_16(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_u16(v64 a) {
  return v64_shr_n_u16(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_s16(v64 a) {
  return v64_shr_n_s16(a, shift);
}
template <int shift>
v64 imm_v64_shl_n_32(v64 a) {
  return v64_shl_n_32(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_u32(v64 a) {
  return v64_shr_n_u32(a, shift);
}
template <int shift>
v64 imm_v64_shr_n_s32(v64 a) {
  return v64_shr_n_s32(a, shift);
}
template <int shift>
v64 imm_v64_align(v64 a, v64 b) {
  return v64_align(a, b, shift);
}

// Wrap templates around corresponding C implementations of the above
template <int shift>
c_v64 c_imm_v64_shl_n_byte(c_v64 a) {
  return c_v64_shl_n_byte(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_byte(c_v64 a) {
  return c_v64_shr_n_byte(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shl_n_8(c_v64 a) {
  return c_v64_shl_n_8(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_u8(c_v64 a) {
  return c_v64_shr_n_u8(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_s8(c_v64 a) {
  return c_v64_shr_n_s8(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shl_n_16(c_v64 a) {
  return c_v64_shl_n_16(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_u16(c_v64 a) {
  return c_v64_shr_n_u16(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_s16(c_v64 a) {
  return c_v64_shr_n_s16(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shl_n_32(c_v64 a) {
  return c_v64_shl_n_32(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_u32(c_v64 a) {
  return c_v64_shr_n_u32(a, shift);
}
template <int shift>
c_v64 c_imm_v64_shr_n_s32(c_v64 a) {
  return c_v64_shr_n_s32(a, shift);
}
template <int shift>
c_v64 c_imm_v64_align(c_v64 a, c_v64 b) {
  return c_v64_align(a, b, shift);
}

template <int shift>
v128 imm_v128_shl_n_byte(v128 a) {
  return v128_shl_n_byte(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_byte(v128 a) {
  return v128_shr_n_byte(a, shift);
}
template <int shift>
v128 imm_v128_shl_n_8(v128 a) {
  return v128_shl_n_8(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_u8(v128 a) {
  return v128_shr_n_u8(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_s8(v128 a) {
  return v128_shr_n_s8(a, shift);
}
template <int shift>
v128 imm_v128_shl_n_16(v128 a) {
  return v128_shl_n_16(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_u16(v128 a) {
  return v128_shr_n_u16(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_s16(v128 a) {
  return v128_shr_n_s16(a, shift);
}
template <int shift>
v128 imm_v128_shl_n_32(v128 a) {
  return v128_shl_n_32(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_u32(v128 a) {
  return v128_shr_n_u32(a, shift);
}
template <int shift>
v128 imm_v128_shr_n_s32(v128 a) {
  return v128_shr_n_s32(a, shift);
}
template <int shift>
v128 imm_v128_align(v128 a, v128 b) {
  return v128_align(a, b, shift);
}

template <int shift>
c_v128 c_imm_v128_shl_n_byte(c_v128 a) {
  return c_v128_shl_n_byte(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_byte(c_v128 a) {
  return c_v128_shr_n_byte(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shl_n_8(c_v128 a) {
  return c_v128_shl_n_8(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_u8(c_v128 a) {
  return c_v128_shr_n_u8(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_s8(c_v128 a) {
  return c_v128_shr_n_s8(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shl_n_16(c_v128 a) {
  return c_v128_shl_n_16(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_u16(c_v128 a) {
  return c_v128_shr_n_u16(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_s16(c_v128 a) {
  return c_v128_shr_n_s16(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shl_n_32(c_v128 a) {
  return c_v128_shl_n_32(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_u32(c_v128 a) {
  return c_v128_shr_n_u32(a, shift);
}
template <int shift>
c_v128 c_imm_v128_shr_n_s32(c_v128 a) {
  return c_v128_shr_n_s32(a, shift);
}
template <int shift>
c_v128 c_imm_v128_align(c_v128 a, c_v128 b) {
  return c_v128_align(a, b, shift);
}

// Wrappers around the the SAD and SSD functions
uint32_t v64_sad_u8(v64 a, v64 b) {
  return v64_sad_u8_sum(::v64_sad_u8(v64_sad_u8_init(), a, b));
}
uint32_t v64_ssd_u8(v64 a, v64 b) {
  return v64_ssd_u8_sum(::v64_ssd_u8(v64_ssd_u8_init(), a, b));
}

uint32_t c_v64_sad_u8(c_v64 a, c_v64 b) {
  return c_v64_sad_u8_sum(::c_v64_sad_u8(c_v64_sad_u8_init(), a, b));
}
uint32_t c_v64_ssd_u8(c_v64 a, c_v64 b) {
  return c_v64_ssd_u8_sum(::c_v64_ssd_u8(c_v64_ssd_u8_init(), a, b));
}
uint32_t v128_sad_u8(v128 a, v128 b) {
  return v128_sad_u8_sum(::v128_sad_u8(v128_sad_u8_init(), a, b));
}
uint32_t v128_ssd_u8(v128 a, v128 b) {
  return v128_ssd_u8_sum(::v128_ssd_u8(v128_ssd_u8_init(), a, b));
}
uint32_t c_v128_sad_u8(c_v128 a, c_v128 b) {
  return c_v128_sad_u8_sum(::c_v128_sad_u8(c_v128_sad_u8_init(), a, b));
}
uint32_t c_v128_ssd_u8(c_v128 a, c_v128 b) {
  return c_v128_ssd_u8_sum(::c_v128_ssd_u8(c_v128_ssd_u8_init(), a, b));
}

namespace {

typedef void (*fptr)();

typedef struct {
  const char *name;
  fptr ref;
  fptr simd;
} mapping;

#define MAP(name)                                                      \
  {                                                                    \
    #name, reinterpret_cast < fptr > (c_##name),                       \
                                      reinterpret_cast < fptr > (name) \
  }

const mapping m[] = { MAP(v64_sad_u8),
                      MAP(v64_ssd_u8),
                      MAP(v64_add_8),
                      MAP(v64_add_16),
                      MAP(v64_sadd_s16),
                      MAP(v64_add_32),
                      MAP(v64_sub_8),
                      MAP(v64_ssub_u8),
                      MAP(v64_ssub_s8),
                      MAP(v64_sub_16),
                      MAP(v64_ssub_s16),
                      MAP(v64_ssub_u16),
                      MAP(v64_sub_32),
                      MAP(v64_ziplo_8),
                      MAP(v64_ziphi_8),
                      MAP(v64_ziplo_16),
                      MAP(v64_ziphi_16),
                      MAP(v64_ziplo_32),
                      MAP(v64_ziphi_32),
                      MAP(v64_pack_s32_s16),
                      MAP(v64_pack_s16_u8),
                      MAP(v64_pack_s16_s8),
                      MAP(v64_unziphi_8),
                      MAP(v64_unziplo_8),
                      MAP(v64_unziphi_16),
                      MAP(v64_unziplo_16),
                      MAP(v64_or),
                      MAP(v64_xor),
                      MAP(v64_and),
                      MAP(v64_andn),
                      MAP(v64_mullo_s16),
                      MAP(v64_mulhi_s16),
                      MAP(v64_mullo_s32),
                      MAP(v64_madd_s16),
                      MAP(v64_madd_us8),
                      MAP(v64_avg_u8),
                      MAP(v64_rdavg_u8),
                      MAP(v64_avg_u16),
                      MAP(v64_min_u8),
                      MAP(v64_max_u8),
                      MAP(v64_min_s8),
                      MAP(v64_max_s8),
                      MAP(v64_min_s16),
                      MAP(v64_max_s16),
                      MAP(v64_cmpgt_s8),
                      MAP(v64_cmplt_s8),
                      MAP(v64_cmpeq_8),
                      MAP(v64_cmpgt_s16),
                      MAP(v64_cmplt_s16),
                      MAP(v64_cmpeq_16),
                      MAP(v64_shuffle_8),
                      MAP(imm_v64_align<1>),
                      MAP(imm_v64_align<2>),
                      MAP(imm_v64_align<3>),
                      MAP(imm_v64_align<4>),
                      MAP(imm_v64_align<5>),
                      MAP(imm_v64_align<6>),
                      MAP(imm_v64_align<7>),
                      MAP(v64_abs_s8),
                      MAP(v64_abs_s16),
                      MAP(v64_unpacklo_u8_s16),
                      MAP(v64_unpackhi_u8_s16),
                      MAP(v64_unpacklo_s8_s16),
                      MAP(v64_unpackhi_s8_s16),
                      MAP(v64_unpacklo_u16_s32),
                      MAP(v64_unpacklo_s16_s32),
                      MAP(v64_unpackhi_u16_s32),
                      MAP(v64_unpackhi_s16_s32),
                      MAP(imm_v64_shr_n_byte<1>),
                      MAP(imm_v64_shr_n_byte<2>),
                      MAP(imm_v64_shr_n_byte<3>),
                      MAP(imm_v64_shr_n_byte<4>),
                      MAP(imm_v64_shr_n_byte<5>),
                      MAP(imm_v64_shr_n_byte<6>),
                      MAP(imm_v64_shr_n_byte<7>),
                      MAP(imm_v64_shl_n_byte<1>),
                      MAP(imm_v64_shl_n_byte<2>),
                      MAP(imm_v64_shl_n_byte<3>),
                      MAP(imm_v64_shl_n_byte<4>),
                      MAP(imm_v64_shl_n_byte<5>),
                      MAP(imm_v64_shl_n_byte<6>),
                      MAP(imm_v64_shl_n_byte<7>),
                      MAP(imm_v64_shl_n_8<1>),
                      MAP(imm_v64_shl_n_8<2>),
                      MAP(imm_v64_shl_n_8<3>),
                      MAP(imm_v64_shl_n_8<4>),
                      MAP(imm_v64_shl_n_8<5>),
                      MAP(imm_v64_shl_n_8<6>),
                      MAP(imm_v64_shl_n_8<7>),
                      MAP(imm_v64_shr_n_u8<1>),
                      MAP(imm_v64_shr_n_u8<2>),
                      MAP(imm_v64_shr_n_u8<3>),
                      MAP(imm_v64_shr_n_u8<4>),
                      MAP(imm_v64_shr_n_u8<5>),
                      MAP(imm_v64_shr_n_u8<6>),
                      MAP(imm_v64_shr_n_u8<7>),
                      MAP(imm_v64_shr_n_s8<1>),
                      MAP(imm_v64_shr_n_s8<2>),
                      MAP(imm_v64_shr_n_s8<3>),
                      MAP(imm_v64_shr_n_s8<4>),
                      MAP(imm_v64_shr_n_s8<5>),
                      MAP(imm_v64_shr_n_s8<6>),
                      MAP(imm_v64_shr_n_s8<7>),
                      MAP(imm_v64_shl_n_16<1>),
                      MAP(imm_v64_shl_n_16<2>),
                      MAP(imm_v64_shl_n_16<4>),
                      MAP(imm_v64_shl_n_16<6>),
                      MAP(imm_v64_shl_n_16<8>),
                      MAP(imm_v64_shl_n_16<10>),
                      MAP(imm_v64_shl_n_16<12>),
                      MAP(imm_v64_shl_n_16<14>),
                      MAP(imm_v64_shr_n_u16<1>),
                      MAP(imm_v64_shr_n_u16<2>),
                      MAP(imm_v64_shr_n_u16<4>),
                      MAP(imm_v64_shr_n_u16<6>),
                      MAP(imm_v64_shr_n_u16<8>),
                      MAP(imm_v64_shr_n_u16<10>),
                      MAP(imm_v64_shr_n_u16<12>),
                      MAP(imm_v64_shr_n_u16<14>),
                      MAP(imm_v64_shr_n_s16<1>),
                      MAP(imm_v64_shr_n_s16<2>),
                      MAP(imm_v64_shr_n_s16<4>),
                      MAP(imm_v64_shr_n_s16<6>),
                      MAP(imm_v64_shr_n_s16<8>),
                      MAP(imm_v64_shr_n_s16<10>),
                      MAP(imm_v64_shr_n_s16<12>),
                      MAP(imm_v64_shr_n_s16<14>),
                      MAP(imm_v64_shl_n_32<1>),
                      MAP(imm_v64_shl_n_32<4>),
                      MAP(imm_v64_shl_n_32<8>),
                      MAP(imm_v64_shl_n_32<12>),
                      MAP(imm_v64_shl_n_32<16>),
                      MAP(imm_v64_shl_n_32<20>),
                      MAP(imm_v64_shl_n_32<24>),
                      MAP(imm_v64_shl_n_32<28>),
                      MAP(imm_v64_shr_n_u32<1>),
                      MAP(imm_v64_shr_n_u32<4>),
                      MAP(imm_v64_shr_n_u32<8>),
                      MAP(imm_v64_shr_n_u32<12>),
                      MAP(imm_v64_shr_n_u32<16>),
                      MAP(imm_v64_shr_n_u32<20>),
                      MAP(imm_v64_shr_n_u32<24>),
                      MAP(imm_v64_shr_n_u32<28>),
                      MAP(imm_v64_shr_n_s32<1>),
                      MAP(imm_v64_shr_n_s32<4>),
                      MAP(imm_v64_shr_n_s32<8>),
                      MAP(imm_v64_shr_n_s32<12>),
                      MAP(imm_v64_shr_n_s32<16>),
                      MAP(imm_v64_shr_n_s32<20>),
                      MAP(imm_v64_shr_n_s32<24>),
                      MAP(imm_v64_shr_n_s32<28>),
                      MAP(v64_shl_8),
                      MAP(v64_shr_u8),
                      MAP(v64_shr_s8),
                      MAP(v64_shl_16),
                      MAP(v64_shr_u16),
                      MAP(v64_shr_s16),
                      MAP(v64_shl_32),
                      MAP(v64_shr_u32),
                      MAP(v64_shr_s32),
                      MAP(v64_hadd_u8),
                      MAP(v64_hadd_s16),
                      MAP(v64_dotp_s16),
                      MAP(v64_dotp_su8),
                      MAP(v64_u64),
                      MAP(v64_low_u32),
                      MAP(v64_high_u32),
                      MAP(v64_low_s32),
                      MAP(v64_high_s32),
                      MAP(v64_dup_8),
                      MAP(v64_dup_16),
                      MAP(v64_dup_32),
                      MAP(v64_from_32),
                      MAP(v64_zero),
                      MAP(v64_from_16),
                      MAP(v128_sad_u8),
                      MAP(v128_ssd_u8),
                      MAP(v128_add_8),
                      MAP(v128_add_16),
                      MAP(v128_sadd_s16),
                      MAP(v128_add_32),
                      MAP(v128_sub_8),
                      MAP(v128_ssub_u8),
                      MAP(v128_ssub_s8),
                      MAP(v128_sub_16),
                      MAP(v128_ssub_s16),
                      MAP(v128_ssub_u16),
                      MAP(v128_sub_32),
                      MAP(v128_ziplo_8),
                      MAP(v128_ziphi_8),
                      MAP(v128_ziplo_16),
                      MAP(v128_ziphi_16),
                      MAP(v128_ziplo_32),
                      MAP(v128_ziphi_32),
                      MAP(v128_ziplo_64),
                      MAP(v128_ziphi_64),
                      MAP(v128_unziphi_8),
                      MAP(v128_unziplo_8),
                      MAP(v128_unziphi_16),
                      MAP(v128_unziplo_16),
                      MAP(v128_unziphi_32),
                      MAP(v128_unziplo_32),
                      MAP(v128_pack_s32_s16),
                      MAP(v128_pack_s16_u8),
                      MAP(v128_pack_s16_s8),
                      MAP(v128_or),
                      MAP(v128_xor),
                      MAP(v128_and),
                      MAP(v128_andn),
                      MAP(v128_mullo_s16),
                      MAP(v128_mulhi_s16),
                      MAP(v128_mullo_s32),
                      MAP(v128_madd_s16),
                      MAP(v128_madd_us8),
                      MAP(v128_avg_u8),
                      MAP(v128_rdavg_u8),
                      MAP(v128_avg_u16),
                      MAP(v128_min_u8),
                      MAP(v128_max_u8),
                      MAP(v128_min_s8),
                      MAP(v128_max_s8),
                      MAP(v128_min_s16),
                      MAP(v128_max_s16),
                      MAP(v128_cmpgt_s8),
                      MAP(v128_cmplt_s8),
                      MAP(v128_cmpeq_8),
                      MAP(v128_cmpgt_s16),
                      MAP(v128_cmpeq_16),
                      MAP(v128_cmplt_s16),
                      MAP(v128_shuffle_8),
                      MAP(imm_v128_align<1>),
                      MAP(imm_v128_align<2>),
                      MAP(imm_v128_align<3>),
                      MAP(imm_v128_align<4>),
                      MAP(imm_v128_align<5>),
                      MAP(imm_v128_align<6>),
                      MAP(imm_v128_align<7>),
                      MAP(imm_v128_align<8>),
                      MAP(imm_v128_align<9>),
                      MAP(imm_v128_align<10>),
                      MAP(imm_v128_align<11>),
                      MAP(imm_v128_align<12>),
                      MAP(imm_v128_align<13>),
                      MAP(imm_v128_align<14>),
                      MAP(imm_v128_align<15>),
                      MAP(v128_abs_s8),
                      MAP(v128_abs_s16),
                      MAP(v128_padd_s16),
                      MAP(v128_unpacklo_u16_s32),
                      MAP(v128_unpacklo_s16_s32),
                      MAP(v128_unpackhi_u16_s32),
                      MAP(v128_unpackhi_s16_s32),
                      MAP(imm_v128_shr_n_byte<1>),
                      MAP(imm_v128_shr_n_byte<2>),
                      MAP(imm_v128_shr_n_byte<3>),
                      MAP(imm_v128_shr_n_byte<4>),
                      MAP(imm_v128_shr_n_byte<5>),
                      MAP(imm_v128_shr_n_byte<6>),
                      MAP(imm_v128_shr_n_byte<7>),
                      MAP(imm_v128_shr_n_byte<8>),
                      MAP(imm_v128_shr_n_byte<9>),
                      MAP(imm_v128_shr_n_byte<10>),
                      MAP(imm_v128_shr_n_byte<11>),
                      MAP(imm_v128_shr_n_byte<12>),
                      MAP(imm_v128_shr_n_byte<13>),
                      MAP(imm_v128_shr_n_byte<14>),
                      MAP(imm_v128_shr_n_byte<15>),
                      MAP(imm_v128_shl_n_byte<1>),
                      MAP(imm_v128_shl_n_byte<2>),
                      MAP(imm_v128_shl_n_byte<3>),
                      MAP(imm_v128_shl_n_byte<4>),
                      MAP(imm_v128_shl_n_byte<5>),
                      MAP(imm_v128_shl_n_byte<6>),
                      MAP(imm_v128_shl_n_byte<7>),
                      MAP(imm_v128_shl_n_byte<8>),
                      MAP(imm_v128_shl_n_byte<9>),
                      MAP(imm_v128_shl_n_byte<10>),
                      MAP(imm_v128_shl_n_byte<11>),
                      MAP(imm_v128_shl_n_byte<12>),
                      MAP(imm_v128_shl_n_byte<13>),
                      MAP(imm_v128_shl_n_byte<14>),
                      MAP(imm_v128_shl_n_byte<15>),
                      MAP(imm_v128_shl_n_8<1>),
                      MAP(imm_v128_shl_n_8<2>),
                      MAP(imm_v128_shl_n_8<3>),
                      MAP(imm_v128_shl_n_8<4>),
                      MAP(imm_v128_shl_n_8<5>),
                      MAP(imm_v128_shl_n_8<6>),
                      MAP(imm_v128_shl_n_8<7>),
                      MAP(imm_v128_shr_n_u8<1>),
                      MAP(imm_v128_shr_n_u8<2>),
                      MAP(imm_v128_shr_n_u8<3>),
                      MAP(imm_v128_shr_n_u8<4>),
                      MAP(imm_v128_shr_n_u8<5>),
                      MAP(imm_v128_shr_n_u8<6>),
                      MAP(imm_v128_shr_n_u8<7>),
                      MAP(imm_v128_shr_n_s8<1>),
                      MAP(imm_v128_shr_n_s8<2>),
                      MAP(imm_v128_shr_n_s8<3>),
                      MAP(imm_v128_shr_n_s8<4>),
                      MAP(imm_v128_shr_n_s8<5>),
                      MAP(imm_v128_shr_n_s8<6>),
                      MAP(imm_v128_shr_n_s8<7>),
                      MAP(imm_v128_shl_n_16<1>),
                      MAP(imm_v128_shl_n_16<2>),
                      MAP(imm_v128_shl_n_16<4>),
                      MAP(imm_v128_shl_n_16<6>),
                      MAP(imm_v128_shl_n_16<8>),
                      MAP(imm_v128_shl_n_16<10>),
                      MAP(imm_v128_shl_n_16<12>),
                      MAP(imm_v128_shl_n_16<14>),
                      MAP(imm_v128_shr_n_u16<1>),
                      MAP(imm_v128_shr_n_u16<2>),
                      MAP(imm_v128_shr_n_u16<4>),
                      MAP(imm_v128_shr_n_u16<6>),
                      MAP(imm_v128_shr_n_u16<8>),
                      MAP(imm_v128_shr_n_u16<10>),
                      MAP(imm_v128_shr_n_u16<12>),
                      MAP(imm_v128_shr_n_u16<14>),
                      MAP(imm_v128_shr_n_s16<1>),
                      MAP(imm_v128_shr_n_s16<2>),
                      MAP(imm_v128_shr_n_s16<4>),
                      MAP(imm_v128_shr_n_s16<6>),
                      MAP(imm_v128_shr_n_s16<8>),
                      MAP(imm_v128_shr_n_s16<10>),
                      MAP(imm_v128_shr_n_s16<12>),
                      MAP(imm_v128_shr_n_s16<14>),
                      MAP(imm_v128_shl_n_32<1>),
                      MAP(imm_v128_shl_n_32<4>),
                      MAP(imm_v128_shl_n_32<8>),
                      MAP(imm_v128_shl_n_32<12>),
                      MAP(imm_v128_shl_n_32<16>),
                      MAP(imm_v128_shl_n_32<20>),
                      MAP(imm_v128_shl_n_32<24>),
                      MAP(imm_v128_shl_n_32<28>),
                      MAP(imm_v128_shr_n_u32<1>),
                      MAP(imm_v128_shr_n_u32<4>),
                      MAP(imm_v128_shr_n_u32<8>),
                      MAP(imm_v128_shr_n_u32<12>),
                      MAP(imm_v128_shr_n_u32<16>),
                      MAP(imm_v128_shr_n_u32<20>),
                      MAP(imm_v128_shr_n_u32<24>),
                      MAP(imm_v128_shr_n_u32<28>),
                      MAP(imm_v128_shr_n_s32<1>),
                      MAP(imm_v128_shr_n_s32<4>),
                      MAP(imm_v128_shr_n_s32<8>),
                      MAP(imm_v128_shr_n_s32<12>),
                      MAP(imm_v128_shr_n_s32<16>),
                      MAP(imm_v128_shr_n_s32<20>),
                      MAP(imm_v128_shr_n_s32<24>),
                      MAP(imm_v128_shr_n_s32<28>),
                      MAP(v128_from_v64),
                      MAP(v128_zip_8),
                      MAP(v128_zip_16),
                      MAP(v128_zip_32),
                      MAP(v128_mul_s16),
                      MAP(v128_unpack_u8_s16),
                      MAP(v128_unpack_s8_s16),
                      MAP(v128_unpack_u16_s32),
                      MAP(v128_unpack_s16_s32),
                      MAP(v128_shl_8),
                      MAP(v128_shr_u8),
                      MAP(v128_shr_s8),
                      MAP(v128_shl_16),
                      MAP(v128_shr_u16),
                      MAP(v128_shr_s16),
                      MAP(v128_shl_32),
                      MAP(v128_shr_u32),
                      MAP(v128_shr_s32),
                      MAP(v128_hadd_u8),
                      MAP(v128_dotp_s16),
                      MAP(v128_low_u32),
                      MAP(v128_low_v64),
                      MAP(v128_high_v64),
                      MAP(v128_from_64),
                      MAP(v128_from_32),
                      MAP(v128_zero),
                      MAP(v128_dup_8),
                      MAP(v128_dup_16),
                      MAP(v128_dup_32),
                      MAP(v128_unpacklo_u8_s16),
                      MAP(v128_unpackhi_u8_s16),
                      MAP(v128_unpacklo_s8_s16),
                      MAP(v128_unpackhi_s8_s16),
                      MAP(u32_load_unaligned),
                      MAP(u32_store_unaligned),
                      MAP(v64_load_unaligned),
                      MAP(v64_store_unaligned),
                      MAP(v128_load_unaligned),
                      MAP(v128_store_unaligned),
                      { NULL, NULL, NULL } };
#undef MAP

// Map reference functions to machine tuned functions. Since the
// functions depend on machine tuned types, the non-machine tuned
// instantiations of the test can't refer to these functions directly,
// so we refer to them by name and do the mapping here.
void Map(const char *name, fptr *ref, fptr *simd) {
  unsigned int i;
  for (i = 0; m[i].name && strcmp(name, m[i].name); i++) {
  }

  *ref = m[i].ref;
  *simd = m[i].simd;
}

// Used for printing errors in TestSimd1Arg and TestSimd2Args
std::string Print(const uint8_t *a, int size) {
  std::string text = "0x";
  for (int i = 0; i < size; i++) {
    const uint8_t c = a[!CONFIG_BIG_ENDIAN ? size - 1 - i : i];
    // Same as snprintf(..., ..., "%02x", c)
    text += (c >> 4) + '0' + ((c >> 4) > 9) * ('a' - '0' - 10);
    text += (c & 15) + '0' + ((c & 15) > 9) * ('a' - '0' - 10);
  }

  return text;
}

// Used in TestSimd1Arg and TestSimd2Args to restrict argument ranges
void SetMask(uint8_t *s, int size, uint32_t mask, uint32_t maskwidth) {
  switch (maskwidth) {
    case 0: {
      break;
    }
    case 8: {
      for (int i = 0; i < size; i++) s[i] &= mask;
      break;
    }
    case 16: {
      uint16_t *t = reinterpret_cast<uint16_t *>(s);
      assert(!(reinterpret_cast<uintptr_t>(s) & 1));
      for (int i = 0; i < size / 2; i++) t[i] &= mask;
      break;
    }
    case 32: {
      uint32_t *t = reinterpret_cast<uint32_t *>(s);
      assert(!(reinterpret_cast<uintptr_t>(s) & 3));
      for (int i = 0; i < size / 4; i++) t[i] &= mask;
      break;
    }
    case 64: {
      uint64_t *t = reinterpret_cast<uint64_t *>(s);
      assert(!(reinterpret_cast<uintptr_t>(s) & 7));
      for (int i = 0; i < size / 8; i++) t[i] &= mask;
      break;
    }
    default: {
      FAIL() << "Unsupported mask width";
      break;
    }
  }
}

// We need some extra load/store functions
void u64_store_aligned(void *p, uint64_t a) {
  v64_store_aligned(p, v64_from_64(a));
}
void s32_store_aligned(void *p, int32_t a) {
  u32_store_aligned(p, static_cast<uint32_t>(a));
}
void s64_store_aligned(void *p, int64_t a) {
  v64_store_aligned(p, v64_from_64(static_cast<uint64_t>(a)));
}

void c_u64_store_aligned(void *p, uint64_t a) {
  c_v64_store_aligned(p, c_v64_from_64(a));
}

void c_s32_store_aligned(void *p, int32_t a) {
  c_u32_store_aligned(p, static_cast<uint32_t>(a));
}

void c_s64_store_aligned(void *p, int64_t a) {
  c_v64_store_aligned(p, c_v64_from_64(static_cast<uint64_t>(a)));
}

uint64_t u64_load_aligned(const void *p) {
  return v64_u64(v64_load_aligned(p));
}
uint16_t u16_load_aligned(const void *p) {
  return *(reinterpret_cast<const uint16_t *>(p));
}
uint8_t u8_load_aligned(const void *p) {
  return *(reinterpret_cast<const uint8_t *>(p));
}

uint64_t c_u64_load_aligned(const void *p) {
  return c_v64_u64(c_v64_load_aligned(p));
}
uint16_t c_u16_load_aligned(const void *p) {
  return *(reinterpret_cast<const uint16_t *>(p));
}
uint8_t c_u8_load_aligned(const void *p) {
  return *(reinterpret_cast<const uint8_t *>(p));
}

// CompareSimd1Arg and CompareSimd2Args compare intrinsics taking 1 or
// 2 arguments respectively with their corresponding C reference.
// Ideally, the loads and stores should have gone into the template
// parameter list, but v64 and v128 could be typedef'ed to the same
// type (which is the case on x86) and then we can't instantiate both
// v64 and v128, so the function return and argument types, including
// the always differing types in the C equivalent are used instead.
// The function arguments must be void pointers and then go through a
// cast to avoid matching errors in the branches eliminated by the
// typeid tests in the calling function.
template <typename Ret, typename Arg, typename CRet, typename CArg>
int CompareSimd1Arg(fptr store, fptr load, fptr simd, void *d, fptr c_store,
                    fptr c_load, fptr c_simd, void *ref_d, const void *a) {
  void (*const my_store)(void *, Ret) = (void (*const)(void *, Ret))store;
  Arg (*const my_load)(const void *) = (Arg(*const)(const void *))load;
  Ret (*const my_simd)(Arg) = (Ret(*const)(Arg))simd;
  void (*const my_c_store)(void *, CRet) = (void (*const)(void *, CRet))c_store;
  CArg (*const my_c_load)(const void *) = (CArg(*const)(const void *))c_load;
  CRet (*const my_c_simd)(CArg) = (CRet(*const)(CArg))c_simd;

  // Call reference and intrinsic
  my_c_store(ref_d, my_c_simd(my_c_load(a)));
  my_store(d, my_simd(my_load(a)));

  // Compare results
  return memcmp(ref_d, d, sizeof(CRet));
}

template <typename Ret, typename Arg1, typename Arg2, typename CRet,
          typename CArg1, typename CArg2>
int CompareSimd2Args(fptr store, fptr load1, fptr load2, fptr simd, void *d,
                     fptr c_store, fptr c_load1, fptr c_load2, fptr c_simd,
                     void *ref_d, const void *a, const void *b) {
  void (*const my_store)(void *, Ret) = (void (*const)(void *, Ret))store;
  Arg1 (*const my_load1)(const void *) = (Arg1(*const)(const void *))load1;
  Arg2 (*const my_load2)(const void *) = (Arg2(*const)(const void *))load2;
  Ret (*const my_simd)(Arg1, Arg2) = (Ret(*const)(Arg1, Arg2))simd;
  void (*const my_c_store)(void *, CRet) = (void (*const)(void *, CRet))c_store;
  CArg1 (*const my_c_load1)(const void *) =
      (CArg1(*const)(const void *))c_load1;
  CArg2 (*const my_c_load2)(const void *) =
      (CArg2(*const)(const void *))c_load2;
  CRet (*const my_c_simd)(CArg1, CArg2) = (CRet(*const)(CArg1, CArg2))c_simd;

  // Call reference and intrinsic
  my_c_store(ref_d, my_c_simd(my_c_load1(a), my_c_load2(b)));
  my_store(d, my_simd(my_load1(a), my_load2(b)));

  // Compare results
  return memcmp(ref_d, d, sizeof(CRet));
}

}  // namespace

template <typename CRet, typename CArg>
void TestSimd1Arg(uint32_t iterations, uint32_t mask, uint32_t maskwidth,
                  const char *name) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  fptr ref_simd;
  fptr simd;
  int error = 0;
  DECLARE_ALIGNED(32, uint8_t, s[sizeof(CArg)]);
  DECLARE_ALIGNED(32, uint8_t, d[sizeof(CRet)]);
  DECLARE_ALIGNED(32, uint8_t, ref_d[sizeof(CRet)]);
  memset(ref_d, 0, sizeof(ref_d));
  memset(d, 0, sizeof(d));

  Map(name, &ref_simd, &simd);
  if (simd == NULL || ref_simd == NULL) {
    FAIL() << "Internal error: Unknown intrinsic function " << name;
  }
  for (unsigned int count = 0;
       count < iterations && !error && !testing::Test::HasFailure(); count++) {
    for (unsigned int c = 0; c < sizeof(CArg); c++) s[c] = rnd.Rand8();

    if (maskwidth) {
      SetMask(s, sizeof(CArg), mask, maskwidth);
    }

    if (typeid(CRet) == typeid(c_v64) && typeid(CArg) == typeid(c_v64)) {
      // V64_V64
      error = CompareSimd1Arg<v64, v64, CRet, CArg>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v64) &&
               typeid(CArg) == typeid(uint8_t)) {
      // V64_U8
      error = CompareSimd1Arg<v64, uint8_t, CRet, CArg>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(u8_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_u8_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v64) &&
               typeid(CArg) == typeid(uint16_t)) {
      // V64_U16
      error = CompareSimd1Arg<v64, uint16_t, CRet, CArg>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(u16_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_u16_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v64) &&
               typeid(CArg) == typeid(uint32_t)) {
      // V64_U32
      error = CompareSimd1Arg<v64, uint32_t, CRet, CArg>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(u32_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_u32_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(uint64_t) &&
               typeid(CArg) == typeid(c_v64)) {
      // U64_V64
      error = CompareSimd1Arg<uint64_t, v64, CRet, CArg>(
          reinterpret_cast<fptr>(u64_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_u64_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(int64_t) &&
               typeid(CArg) == typeid(c_v64)) {
      // S64_V64
      error = CompareSimd1Arg<int64_t, v64, CRet, CArg>(
          reinterpret_cast<fptr>(s64_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_s64_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(uint32_t) &&
               typeid(CArg) == typeid(c_v64)) {
      // U32_V64
      error = CompareSimd1Arg<uint32_t, v64, CRet, CArg>(
          reinterpret_cast<fptr>(u32_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_u32_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(int32_t) &&
               typeid(CArg) == typeid(c_v64)) {
      // S32_V64
      error = CompareSimd1Arg<int32_t, v64, CRet, CArg>(
          reinterpret_cast<fptr>(s32_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_s32_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(uint32_t) &&
               typeid(CArg) == typeid(c_v128)) {
      // U32_V128
      error = CompareSimd1Arg<uint32_t, v128, CRet, CArg>(
          reinterpret_cast<fptr>(u32_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_u32_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(uint64_t) &&
               typeid(CArg) == typeid(c_v128)) {
      // U64_V128
      error = CompareSimd1Arg<uint64_t, v128, CRet, CArg>(
          reinterpret_cast<fptr>(u64_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_u64_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v64) &&
               typeid(CArg) == typeid(c_v128)) {
      // V64_V128
      error = CompareSimd1Arg<v64, v128, CRet, CArg>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg) == typeid(c_v128)) {
      // V128_V128
      error = CompareSimd1Arg<v128, v128, CRet, CArg>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg) == typeid(c_v64)) {
      // V128_V64
      error = CompareSimd1Arg<v128, v64, CRet, CArg>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg) == typeid(uint8_t)) {
      // V128_U8
      error = CompareSimd1Arg<v128, uint8_t, CRet, CArg>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(u8_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_u8_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg) == typeid(uint16_t)) {
      // V128_U16
      error = CompareSimd1Arg<v128, uint16_t, CRet, CArg>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(u16_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_u16_load_aligned), ref_simd, ref_d, s);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg) == typeid(uint32_t)) {
      // V128_U32
      error = CompareSimd1Arg<v128, uint32_t, CRet, CArg>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(u32_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_u32_load_aligned), ref_simd, ref_d, s);
    } else {
      FAIL() << "Internal error: Unknown intrinsic function "
             << typeid(CRet).name() << " " << name << "(" << typeid(CArg).name()
             << ")";
    }
  }

  EXPECT_EQ(0, error) << "Error: mismatch for " << name << "("
                      << Print(s, sizeof(s)) << ") -> " << Print(d, sizeof(d))
                      << " (simd), " << Print(ref_d, sizeof(ref_d)) << " (ref)";
}

template <typename CRet, typename CArg1, typename CArg2>
void TestSimd2Args(uint32_t iterations, uint32_t mask, uint32_t maskwidth,
                   const char *name) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  fptr ref_simd;
  fptr simd;
  int error = 0;
  DECLARE_ALIGNED(32, uint8_t, s1[sizeof(CArg1)]);
  DECLARE_ALIGNED(32, uint8_t, s2[sizeof(CArg2)]);
  DECLARE_ALIGNED(32, uint8_t, d[sizeof(CRet)]);
  DECLARE_ALIGNED(32, uint8_t, ref_d[sizeof(CRet)]);
  memset(ref_d, 0, sizeof(ref_d));
  memset(d, 0, sizeof(d));

  Map(name, &ref_simd, &simd);
  if (simd == NULL || ref_simd == NULL) {
    FAIL() << "Internal error: Unknown intrinsic function " << name;
  }

  for (unsigned int count = 0;
       count < iterations && !error && !testing::Test::HasFailure(); count++) {
    for (unsigned int c = 0; c < sizeof(CArg1); c++) s1[c] = rnd.Rand8();

    for (unsigned int c = 0; c < sizeof(CArg2); c++) s2[c] = rnd.Rand8();

    if (maskwidth) SetMask(s2, sizeof(CArg2), mask, maskwidth);

    if (typeid(CRet) == typeid(c_v64) && typeid(CArg1) == typeid(c_v64) &&
        typeid(CArg2) == typeid(c_v64)) {
      // V64_V64V64
      error = CompareSimd2Args<v64, v64, v64, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(c_v64) &&
               typeid(CArg1) == typeid(uint32_t) &&
               typeid(CArg2) == typeid(uint32_t)) {
      // V64_U32U32
      error = CompareSimd2Args<v64, uint32_t, uint32_t, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(u32_load_aligned),
          reinterpret_cast<fptr>(u32_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_u32_load_aligned),
          reinterpret_cast<fptr>(c_u32_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(uint32_t) &&
               typeid(CArg1) == typeid(c_v64) &&
               typeid(CArg2) == typeid(c_v64)) {
      // U32_V64V64
      error = CompareSimd2Args<uint32_t, v64, v64, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(u32_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_u32_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(int64_t) &&
               typeid(CArg1) == typeid(c_v64) &&
               typeid(CArg2) == typeid(c_v64)) {
      // S64_V64V64
      error = CompareSimd2Args<int64_t, v64, v64, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(s64_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_s64_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(c_v64) &&
               typeid(CArg1) == typeid(c_v64) &&
               typeid(CArg2) == typeid(uint32_t)) {
      // V64_V64U32
      error = CompareSimd2Args<v64, v64, uint32_t, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v64_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned),
          reinterpret_cast<fptr>(u32_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v64_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(c_u32_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg1) == typeid(c_v128) &&
               typeid(CArg2) == typeid(c_v128)) {
      // V128_V128V128
      error = CompareSimd2Args<v128, v128, v128, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(uint32_t) &&
               typeid(CArg1) == typeid(c_v128) &&
               typeid(CArg2) == typeid(c_v128)) {
      // U32_V128V128
      error = CompareSimd2Args<uint32_t, v128, v128, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(u32_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_u32_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(int64_t) &&
               typeid(CArg1) == typeid(c_v128) &&
               typeid(CArg2) == typeid(c_v128)) {
      // S64_V128V128
      error = CompareSimd2Args<int64_t, v128, v128, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(s64_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned),
          reinterpret_cast<fptr>(v128_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_s64_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg1) == typeid(uint64_t) &&
               typeid(CArg2) == typeid(uint64_t)) {
      // V128_U64U64
      error = CompareSimd2Args<v128, uint64_t, uint64_t, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(u64_load_aligned),
          reinterpret_cast<fptr>(u64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_u64_load_aligned),
          reinterpret_cast<fptr>(c_u64_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg1) == typeid(c_v64) &&
               typeid(CArg2) == typeid(c_v64)) {
      // V128_V64V64
      error = CompareSimd2Args<v128, v64, v64, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(v64_load_aligned),
          reinterpret_cast<fptr>(v64_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(c_v64_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else if (typeid(CRet) == typeid(c_v128) &&
               typeid(CArg1) == typeid(c_v128) &&
               typeid(CArg2) == typeid(uint32_t)) {
      // V128_V128U32
      error = CompareSimd2Args<v128, v128, uint32_t, CRet, CArg1, CArg2>(
          reinterpret_cast<fptr>(v128_store_aligned),
          reinterpret_cast<fptr>(v128_load_aligned),
          reinterpret_cast<fptr>(u32_load_aligned), simd, d,
          reinterpret_cast<fptr>(c_v128_store_aligned),
          reinterpret_cast<fptr>(c_v128_load_aligned),
          reinterpret_cast<fptr>(c_u32_load_aligned),
          reinterpret_cast<fptr>(ref_simd), ref_d, s1, s2);
    } else {
      FAIL() << "Internal error: Unknown intrinsic function "
             << typeid(CRet).name() << " " << name << "("
             << typeid(CArg1).name() << ", " << typeid(CArg2).name() << ")";
    }
  }

  EXPECT_EQ(0, error) << "Error: mismatch for " << name << "("
                      << Print(s1, sizeof(s1)) << ", " << Print(s2, sizeof(s2))
                      << ") -> " << Print(d, sizeof(d)) << " (simd), "
                      << Print(ref_d, sizeof(ref_d)) << " (ref)";
}

// Instantiations to make the functions callable from another files
template void TestSimd1Arg<c_v64, uint8_t>(uint32_t, uint32_t, uint32_t,
                                           const char *);
template void TestSimd1Arg<c_v64, uint16_t>(uint32_t, uint32_t, uint32_t,
                                            const char *);
template void TestSimd1Arg<c_v64, uint32_t>(uint32_t, uint32_t, uint32_t,
                                            const char *);
template void TestSimd1Arg<c_v64, c_v64>(uint32_t, uint32_t, uint32_t,
                                         const char *);
template void TestSimd1Arg<uint32_t, c_v64>(uint32_t, uint32_t, uint32_t,
                                            const char *);
template void TestSimd1Arg<int32_t, c_v64>(uint32_t, uint32_t, uint32_t,
                                           const char *);
template void TestSimd1Arg<uint64_t, c_v64>(uint32_t, uint32_t, uint32_t,
                                            const char *);
template void TestSimd1Arg<int64_t, c_v64>(uint32_t, uint32_t, uint32_t,
                                           const char *);
template void TestSimd2Args<c_v64, uint32_t, uint32_t>(uint32_t, uint32_t,
                                                       uint32_t, const char *);
template void TestSimd2Args<c_v64, c_v64, c_v64>(uint32_t, uint32_t, uint32_t,
                                                 const char *);
template void TestSimd2Args<c_v64, c_v64, uint32_t>(uint32_t, uint32_t,
                                                    uint32_t, const char *);
template void TestSimd2Args<int64_t, c_v64, c_v64>(uint32_t, uint32_t, uint32_t,
                                                   const char *);
template void TestSimd2Args<uint32_t, c_v64, c_v64>(uint32_t, uint32_t,
                                                    uint32_t, const char *);
template void TestSimd1Arg<c_v128, c_v128>(uint32_t, uint32_t, uint32_t,
                                           const char *);
template void TestSimd1Arg<c_v128, uint8_t>(uint32_t, uint32_t, uint32_t,
                                            const char *);
template void TestSimd1Arg<c_v128, uint16_t>(uint32_t, uint32_t, uint32_t,
                                             const char *);
template void TestSimd1Arg<c_v128, uint32_t>(uint32_t, uint32_t, uint32_t,
                                             const char *);
template void TestSimd1Arg<c_v128, c_v64>(uint32_t, uint32_t, uint32_t,
                                          const char *);
template void TestSimd1Arg<uint32_t, c_v128>(uint32_t, uint32_t, uint32_t,
                                             const char *);
template void TestSimd1Arg<uint64_t, c_v128>(uint32_t, uint32_t, uint32_t,
                                             const char *);
template void TestSimd1Arg<c_v64, c_v128>(uint32_t, uint32_t, uint32_t,
                                          const char *);
template void TestSimd2Args<c_v128, c_v128, c_v128>(uint32_t, uint32_t,
                                                    uint32_t, const char *);
template void TestSimd2Args<c_v128, c_v128, uint32_t>(uint32_t, uint32_t,
                                                      uint32_t, const char *);
template void TestSimd2Args<c_v128, uint64_t, uint64_t>(uint32_t, uint32_t,
                                                        uint32_t, const char *);
template void TestSimd2Args<c_v128, c_v64, c_v64>(uint32_t, uint32_t, uint32_t,
                                                  const char *);
template void TestSimd2Args<int64_t, c_v128, c_v128>(uint32_t, uint32_t,
                                                     uint32_t, const char *);
template void TestSimd2Args<uint32_t, c_v128, c_v128>(uint32_t, uint32_t,
                                                      uint32_t, const char *);

}  // namespace SIMD_NAMESPACE