34
34
// ./libc_str_to_float_comparison_test <path/to/dataset/repo>/data/*
35
35
// It will take a few seconds to run.
36
36
37
+ struct ParseResult {
38
+ uint32_t totalFails;
39
+ uint32_t totalBitDiffs;
40
+ uint32_t detailedBitDiffs[4 ];
41
+ uint32_t total;
42
+ };
43
+
37
44
static inline uint32_t hexCharToU32 (char in) {
38
45
return in > ' 9' ? in + 10 - ' A' : in - ' 0' ;
39
46
}
@@ -56,13 +63,13 @@ static inline uint64_t fastHexToU64(const char *inStr) {
56
63
return result;
57
64
}
58
65
59
- static void parseLine (char *line, int *total, int *detailedBitDiffs ,
60
- int32_t &curFails, int32_t & curBitDiffs) {
66
+ static void parseLine (char *line, ParseResult &parseResult, int32_t &curFails ,
67
+ int32_t &curBitDiffs) {
61
68
62
69
if (line[0 ] == ' #' ) {
63
70
return ;
64
71
}
65
- * total = *total + 1 ;
72
+ parseResult. total += 1 ;
66
73
uint32_t expectedFloatRaw;
67
74
uint64_t expectedDoubleRaw;
68
75
@@ -83,9 +90,11 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
83
90
if (expectedFloatRaw == floatRaw + 1 || expectedFloatRaw == floatRaw - 1 ) {
84
91
curBitDiffs++;
85
92
if (expectedFloatRaw == floatRaw + 1 ) {
86
- detailedBitDiffs[0 ] = detailedBitDiffs[0 ] + 1 ; // float low
93
+ parseResult.detailedBitDiffs [0 ] =
94
+ parseResult.detailedBitDiffs [0 ] + 1 ; // float low
87
95
} else {
88
- detailedBitDiffs[1 ] = detailedBitDiffs[1 ] + 1 ; // float high
96
+ parseResult.detailedBitDiffs [1 ] =
97
+ parseResult.detailedBitDiffs [1 ] + 1 ; // float high
89
98
}
90
99
} else {
91
100
curFails++;
@@ -101,9 +110,11 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
101
110
expectedDoubleRaw == doubleRaw - 1 ) {
102
111
curBitDiffs++;
103
112
if (expectedDoubleRaw == doubleRaw + 1 ) {
104
- detailedBitDiffs[2 ] = detailedBitDiffs[2 ] + 1 ; // double low
113
+ parseResult.detailedBitDiffs [2 ] =
114
+ parseResult.detailedBitDiffs [2 ] + 1 ; // double low
105
115
} else {
106
- detailedBitDiffs[3 ] = detailedBitDiffs[3 ] + 1 ; // double high
116
+ parseResult.detailedBitDiffs [3 ] =
117
+ parseResult.detailedBitDiffs [3 ] + 1 ; // double high
107
118
}
108
119
} else {
109
120
curFails++;
@@ -115,8 +126,7 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
115
126
}
116
127
}
117
128
118
- int checkBuffer (int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
119
- int *total) {
129
+ int checkBuffer (ParseResult &parseResult) {
120
130
const char *lines[6 ] = {" 3C00 3F800000 3FF0000000000000 1" ,
121
131
" 3D00 3FA00000 3FF4000000000000 1.25" ,
122
132
" 3D9A 3FB33333 3FF6666666666666 1.4" ,
@@ -130,20 +140,19 @@ int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
130
140
131
141
for (uint8_t i = 0 ; i < 6 ; i++) {
132
142
auto line = const_cast <char *>(lines[i]);
133
- parseLine (line, total, detailedBitDiffs , curFails, curBitDiffs);
143
+ parseLine (line, parseResult , curFails, curBitDiffs);
134
144
}
135
145
136
- * totalBitDiffs += curBitDiffs;
137
- * totalFails += curFails;
146
+ parseResult. totalBitDiffs += curBitDiffs;
147
+ parseResult. totalFails += curFails;
138
148
139
149
if (curFails > 1 || curBitDiffs > 1 ) {
140
150
return 2 ;
141
151
}
142
152
return 0 ;
143
153
}
144
154
145
- int checkFile (char *inputFileName, int *totalFails, int *totalBitDiffs,
146
- int *detailedBitDiffs, int *total) {
155
+ int checkFile (char *inputFileName, ParseResult &parseResult) {
147
156
int32_t curFails = 0 ; // Only counts actual failures, not bitdiffs.
148
157
int32_t curBitDiffs = 0 ; // A bitdiff is when the expected result and actual
149
158
// result are off by +/- 1 bit.
@@ -158,13 +167,13 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
158
167
}
159
168
160
169
while (LIBC_NAMESPACE::fgets (line, sizeof (line), fileHandle)) {
161
- parseLine (line, total, detailedBitDiffs , curFails, curBitDiffs);
170
+ parseLine (line, parseResult , curFails, curBitDiffs);
162
171
}
163
172
164
173
LIBC_NAMESPACE::fclose (fileHandle);
165
174
166
- * totalBitDiffs += curBitDiffs;
167
- * totalFails += curFails;
175
+ parseResult. totalBitDiffs += curBitDiffs;
176
+ parseResult. totalFails += curFails;
168
177
169
178
if (curFails > 1 || curBitDiffs > 1 ) {
170
179
return 2 ;
@@ -183,38 +192,39 @@ int updateResult(int result, int curResult) {
183
192
184
193
TEST (LlvmLibcStrToFloatComparisonTest, CheckFloats) {
185
194
int result = 0 ;
186
- int fails = 0 ;
187
195
188
196
// Bitdiffs are cases where the expected result and actual result only differ
189
197
// by +/- the least significant bit. They are tracked separately from larger
190
198
// failures since a bitdiff is most likely the result of a rounding error, and
191
199
// splitting them off makes them easier to track down.
192
- int bitdiffs = 0 ;
193
- int detailedBitDiffs[4 ] = {0 , 0 , 0 , 0 };
194
200
195
- int total = 0 ;
201
+ ParseResult parseResult = {
202
+ .totalFails = 0 ,
203
+ .totalBitDiffs = 0 ,
204
+ .detailedBitDiffs = {0 , 0 , 0 , 0 },
205
+ .total = 0 ,
206
+ };
196
207
197
208
char *files = LIBC_NAMESPACE::getenv (" FILES" );
198
209
199
210
if (files == nullptr ) {
200
- int curResult = checkBuffer (&fails, &bitdiffs, detailedBitDiffs, &total );
211
+ int curResult = checkBuffer (parseResult );
201
212
result = updateResult (result, curResult);
202
213
} else {
203
214
files = LIBC_NAMESPACE::strdup (files);
204
215
for (char *file = LIBC_NAMESPACE::strtok (files, " ," ); file != nullptr ;
205
216
file = LIBC_NAMESPACE::strtok (nullptr , " ," )) {
206
- int curResult =
207
- checkFile (file, &fails, &bitdiffs, detailedBitDiffs, &total);
217
+ int curResult = checkFile (file, parseResult);
208
218
result = updateResult (result, curResult);
209
219
}
210
220
}
211
221
212
222
EXPECT_EQ (result, 0 );
213
- EXPECT_EQ (fails, 0 );
214
- EXPECT_EQ (bitdiffs, 0 );
215
- EXPECT_EQ (detailedBitDiffs[0 ], 0 ); // float low
216
- EXPECT_EQ (detailedBitDiffs[1 ], 0 ); // float high
217
- EXPECT_EQ (detailedBitDiffs[2 ], 0 ); // double low
218
- EXPECT_EQ (detailedBitDiffs[3 ], 0 ); // double high
219
- EXPECT_EQ (total, 6 );
223
+ EXPECT_EQ (parseResult. totalFails , 0u );
224
+ EXPECT_EQ (parseResult. totalBitDiffs , 0u );
225
+ EXPECT_EQ (parseResult. detailedBitDiffs [0 ], 0u ); // float low
226
+ EXPECT_EQ (parseResult. detailedBitDiffs [1 ], 0u ); // float high
227
+ EXPECT_EQ (parseResult. detailedBitDiffs [2 ], 0u ); // double low
228
+ EXPECT_EQ (parseResult. detailedBitDiffs [3 ], 0u ); // double high
229
+ EXPECT_EQ (parseResult. total , 6u );
220
230
}
0 commit comments