blob: dbd93aa61471e9f8ede34c46d58080ee80592b6f [file] [log] [blame]
Jim Stichnoth7da431b2014-08-05 11:22:37 -07001//===- subzero/crosstest/test_cast_main.cpp - Driver for tests ------------===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Driver for crosstesting cast operations.
11//
12//===----------------------------------------------------------------------===//
13
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070014/* crosstest.py --test=test_cast.cpp --test=test_cast_to_u1.ll \
Jan Voung109fa152014-10-07 17:22:51 -070015 --test=test_cast_vectors.ll \
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070016 --driver=test_cast_main.cpp --prefix=Subzero_ --output=test_cast */
17
Jan Voung109fa152014-10-07 17:22:51 -070018#include <cfloat>
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070019#include <cstring>
20#include <iostream>
21#include <stdint.h>
22
Jan Voung109fa152014-10-07 17:22:51 -070023#include "test_arith.def"
24#include "vectors.h"
25
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070026// Include test_cast.h twice - once normally, and once within the
27// Subzero_ namespace, corresponding to the llc and Subzero translated
28// object files, respectively.
29#include "test_cast.h"
30namespace Subzero_ {
31#include "test_cast.h"
32}
33
34#define XSTR(s) STR(s)
35#define STR(s) #s
Jim Stichnothb63cd882014-09-08 10:47:23 -070036#define COMPARE(Func, FromCName, ToCName, Input, FromString) \
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070037 do { \
38 ToCName ResultSz, ResultLlc; \
39 ResultLlc = Func<FromCName, ToCName>(Input); \
40 ResultSz = Subzero_::Func<FromCName, ToCName>(Input); \
41 ++TotalTests; \
42 if (!memcmp(&ResultLlc, &ResultSz, sizeof(ToCName))) { \
43 ++Passes; \
44 } else { \
45 ++Failures; \
Jim Stichnothb63cd882014-09-08 10:47:23 -070046 std::cout << std::fixed << XSTR(Func) << "<" << FromString \
47 << ", " XSTR(ToCName) ">(" << Input << "): "; \
48 if (sizeof(ToCName) == 1) \
49 std::cout << "sz=" << (int)ResultSz << " llc=" << (int)ResultLlc; \
50 else \
51 std::cout << "sz=" << ResultSz << " llc=" << ResultLlc; \
52 std::cout << "\n"; \
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070053 } \
54 } while (0)
55
Jan Voung109fa152014-10-07 17:22:51 -070056#define COMPARE_VEC(Func, FromCName, ToCName, Input, FromString, ToString) \
57 do { \
58 ToCName ResultSz, ResultLlc; \
59 ResultLlc = Func<FromCName, ToCName>(Input); \
60 ResultSz = Subzero_::Func<FromCName, ToCName>(Input); \
61 ++TotalTests; \
62 if (!memcmp(&ResultLlc, &ResultSz, sizeof(ToCName))) { \
63 ++Passes; \
64 } else { \
65 ++Failures; \
66 std::cout << std::fixed << XSTR(Func) << "<" << FromString << ", " \
67 << ToString << ">(" << vectAsString<FromCName>(Input) \
68 << "): "; \
69 std::cout << "sz=" << vectAsString<ToCName>(ResultSz) \
70 << " llc=" << vectAsString<ToCName>(ResultLlc); \
71 std::cout << "\n"; \
72 } \
73 } while (0)
74
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070075template <typename FromType>
76void testValue(FromType Val, size_t &TotalTests, size_t &Passes,
Jim Stichnothb63cd882014-09-08 10:47:23 -070077 size_t &Failures, const char *FromTypeString) {
78 COMPARE(cast, FromType, bool, Val, FromTypeString);
79 COMPARE(cast, FromType, uint8_t, Val, FromTypeString);
80 COMPARE(cast, FromType, myint8_t, Val, FromTypeString);
81 COMPARE(cast, FromType, uint16_t, Val, FromTypeString);
82 COMPARE(cast, FromType, int16_t, Val, FromTypeString);
83 COMPARE(cast, FromType, uint32_t, Val, FromTypeString);
84 COMPARE(cast, FromType, int32_t, Val, FromTypeString);
85 COMPARE(cast, FromType, uint64_t, Val, FromTypeString);
86 COMPARE(cast, FromType, int64_t, Val, FromTypeString);
87 COMPARE(cast, FromType, float, Val, FromTypeString);
88 COMPARE(cast, FromType, double, Val, FromTypeString);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070089}
90
Jan Voung109fa152014-10-07 17:22:51 -070091template <typename FromType, typename ToType>
92void testVector(size_t &TotalTests, size_t &Passes, size_t &Failures,
93 const char *FromTypeString, const char *ToTypeString) {
94 const static size_t NumElementsInType = Vectors<FromType>::NumElements;
95 PRNG Index;
96 static const float NegInf = -1.0 / 0.0;
97 static const float PosInf = 1.0 / 0.0;
98 static const float Nan = 0.0 / 0.0;
99 static const float NegNan = -0.0 / 0.0;
100 volatile float Values[] = FP_VALUE_ARRAY(NegInf, PosInf, NegNan, Nan);
101 static const size_t NumValues = sizeof(Values) / sizeof(*Values);
102 const size_t MaxTestsPerFunc = 20000;
103 for (size_t i = 0; i < MaxTestsPerFunc; ++i) {
104 // Initialize the test vectors.
105 FromType Value;
106 for (size_t j = 0; j < NumElementsInType; ++j) {
107 Value[j] = Values[Index() % NumValues];
108 }
109 COMPARE_VEC(cast, FromType, ToType, Value, FromTypeString, ToTypeString);
110 }
111}
112
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700113int main(int argc, char **argv) {
114 size_t TotalTests = 0;
115 size_t Passes = 0;
116 size_t Failures = 0;
117
Jim Stichnothdd842db2015-01-27 12:53:53 -0800118 volatile bool ValsUi1[] = {false, true};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700119 static const size_t NumValsUi1 = sizeof(ValsUi1) / sizeof(*ValsUi1);
Jim Stichnothdd842db2015-01-27 12:53:53 -0800120 volatile uint8_t ValsUi8[] = {0, 1, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700121 static const size_t NumValsUi8 = sizeof(ValsUi8) / sizeof(*ValsUi8);
122
Jim Stichnothdd842db2015-01-27 12:53:53 -0800123 volatile myint8_t ValsSi8[] = {0, 1, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700124 static const size_t NumValsSi8 = sizeof(ValsSi8) / sizeof(*ValsSi8);
125
Jim Stichnothdd842db2015-01-27 12:53:53 -0800126 volatile uint16_t ValsUi16[] = {0, 1, 0x7e, 0x7f, 0x80,
127 0x81, 0xfe, 0xff, 0x7ffe, 0x7fff,
128 0x8000, 0x8001, 0xfffe, 0xffff};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700129 static const size_t NumValsUi16 = sizeof(ValsUi16) / sizeof(*ValsUi16);
130
Jim Stichnothdd842db2015-01-27 12:53:53 -0800131 volatile int16_t ValsSi16[] = {0, 1, 0x7e, 0x7f, 0x80,
132 0x81, 0xfe, 0xff, 0x7ffe, 0x7fff,
133 0x8000, 0x8001, 0xfffe, 0xffff};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700134 static const size_t NumValsSi16 = sizeof(ValsSi16) / sizeof(*ValsSi16);
135
Jim Stichnothdd842db2015-01-27 12:53:53 -0800136 volatile size_t ValsUi32[] = {0, 1, 0x7e, 0x7f,
137 0x80, 0x81, 0xfe, 0xff,
138 0x7ffe, 0x7fff, 0x8000, 0x8001,
139 0xfffe, 0xffff, 0x7ffffffe, 0x7fffffff,
140 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700141 static const size_t NumValsUi32 = sizeof(ValsUi32) / sizeof(*ValsUi32);
142
Jim Stichnothdd842db2015-01-27 12:53:53 -0800143 volatile size_t ValsSi32[] = {0, 1, 0x7e, 0x7f,
144 0x80, 0x81, 0xfe, 0xff,
145 0x7ffe, 0x7fff, 0x8000, 0x8001,
146 0xfffe, 0xffff, 0x7ffffffe, 0x7fffffff,
147 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700148 static const size_t NumValsSi32 = sizeof(ValsSi32) / sizeof(*ValsSi32);
149
150 volatile uint64_t ValsUi64[] = {
Jim Stichnothdd842db2015-01-27 12:53:53 -0800151 0, 1, 0x7e,
152 0x7f, 0x80, 0x81,
153 0xfe, 0xff, 0x7ffe,
154 0x7fff, 0x8000, 0x8001,
155 0xfffe, 0xffff, 0x7ffffffe,
156 0x7fffffff, 0x80000000, 0x80000001,
157 0xfffffffe, 0xffffffff, 0x100000000ull,
158 0x100000001ull, 0x7ffffffffffffffeull, 0x7fffffffffffffffull,
159 0x8000000000000000ull, 0x8000000000000001ull, 0xfffffffffffffffeull,
160 0xffffffffffffffffull};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700161 static const size_t NumValsUi64 = sizeof(ValsUi64) / sizeof(*ValsUi64);
162
163 volatile int64_t ValsSi64[] = {
Jim Stichnothdd842db2015-01-27 12:53:53 -0800164 0, 1, 0x7e,
165 0x7f, 0x80, 0x81,
166 0xfe, 0xff, 0x7ffe,
167 0x7fff, 0x8000, 0x8001,
168 0xfffe, 0xffff, 0x7ffffffe,
169 0x7fffffff, 0x80000000, 0x80000001,
170 0xfffffffe, 0xffffffff, 0x100000000ll,
171 0x100000001ll, 0x7ffffffffffffffell, 0x7fffffffffffffffll,
172 0x8000000000000000ll, 0x8000000000000001ll, 0xfffffffffffffffell,
173 0xffffffffffffffffll};
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700174 static const size_t NumValsSi64 = sizeof(ValsSi64) / sizeof(*ValsSi64);
175
Jan Voung109fa152014-10-07 17:22:51 -0700176 static const double NegInf = -1.0 / 0.0;
177 static const double PosInf = 1.0 / 0.0;
178 static const double Nan = 0.0 / 0.0;
179 static const double NegNan = -0.0 / 0.0;
180 volatile float ValsF32[] = FP_VALUE_ARRAY(NegInf, PosInf, NegNan, Nan);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700181 static const size_t NumValsF32 = sizeof(ValsF32) / sizeof(*ValsF32);
182
Jan Voung109fa152014-10-07 17:22:51 -0700183 volatile double ValsF64[] = FP_VALUE_ARRAY(NegInf, PosInf, NegNan, Nan);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700184 static const size_t NumValsF64 = sizeof(ValsF64) / sizeof(*ValsF64);
185
186 for (size_t i = 0; i < NumValsUi1; ++i) {
187 bool Val = ValsUi1[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700188 testValue<bool>(Val, TotalTests, Passes, Failures, "bool");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700189 }
190 for (size_t i = 0; i < NumValsUi8; ++i) {
191 uint8_t Val = ValsUi8[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700192 testValue<uint8_t>(Val, TotalTests, Passes, Failures, "uint8_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700193 }
194 for (size_t i = 0; i < NumValsSi8; ++i) {
Jim Stichnoth7da431b2014-08-05 11:22:37 -0700195 myint8_t Val = ValsSi8[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700196 testValue<myint8_t>(Val, TotalTests, Passes, Failures, "int8_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700197 }
198 for (size_t i = 0; i < NumValsUi16; ++i) {
199 uint16_t Val = ValsUi16[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700200 testValue<uint16_t>(Val, TotalTests, Passes, Failures, "uint16_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700201 }
202 for (size_t i = 0; i < NumValsSi16; ++i) {
203 int16_t Val = ValsSi16[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700204 testValue<int16_t>(Val, TotalTests, Passes, Failures, "int16_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700205 }
206 for (size_t i = 0; i < NumValsUi32; ++i) {
207 uint32_t Val = ValsUi32[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700208 testValue<uint32_t>(Val, TotalTests, Passes, Failures, "uint32_t");
209 COMPARE(castBits, uint32_t, float, Val, "uint32_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700210 }
211 for (size_t i = 0; i < NumValsSi32; ++i) {
212 int32_t Val = ValsSi32[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700213 testValue<int32_t>(Val, TotalTests, Passes, Failures, "int32_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700214 }
215 for (size_t i = 0; i < NumValsUi64; ++i) {
216 uint64_t Val = ValsUi64[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700217 testValue<uint64_t>(Val, TotalTests, Passes, Failures, "uint64_t");
218 COMPARE(castBits, uint64_t, double, Val, "uint64_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700219 }
220 for (size_t i = 0; i < NumValsSi64; ++i) {
221 int64_t Val = ValsSi64[i];
Jim Stichnothb63cd882014-09-08 10:47:23 -0700222 testValue<int64_t>(Val, TotalTests, Passes, Failures, "int64_t");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700223 }
224 for (size_t i = 0; i < NumValsF32; ++i) {
225 for (unsigned j = 0; j < 2; ++j) {
226 float Val = ValsF32[i];
227 if (j > 0)
228 Val = -Val;
Jim Stichnothb63cd882014-09-08 10:47:23 -0700229 testValue<float>(Val, TotalTests, Passes, Failures, "float");
230 COMPARE(castBits, float, uint32_t, Val, "float");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700231 }
232 }
233 for (size_t i = 0; i < NumValsF64; ++i) {
234 for (unsigned j = 0; j < 2; ++j) {
235 double Val = ValsF64[i];
236 if (j > 0)
237 Val = -Val;
Jim Stichnothb63cd882014-09-08 10:47:23 -0700238 testValue<double>(Val, TotalTests, Passes, Failures, "double");
239 COMPARE(castBits, double, uint64_t, Val, "double");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700240 }
241 }
Jan Voung109fa152014-10-07 17:22:51 -0700242 testVector<v4ui32, v4f32>(TotalTests, Passes, Failures, "v4ui32", "v4f32");
243 testVector<v4si32, v4f32>(TotalTests, Passes, Failures, "v4si32", "v4f32");
244 testVector<v4f32, v4si32>(TotalTests, Passes, Failures, "v4f32", "v4si32");
245 testVector<v4f32, v4ui32>(TotalTests, Passes, Failures, "v4f32", "v4ui32");
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700246
247 std::cout << "TotalTests=" << TotalTests << " Passes=" << Passes
248 << " Failures=" << Failures << "\n";
249 return Failures;
250}