@@ -22,27 +22,35 @@ const char *g_program;
22
22
/* *** */
23
23
24
24
typedef int bool ;
25
- #define true ((bool) 1)
26
- #define false ((bool) 0)
25
+ #define true ((bool)1)
26
+ #define false ((bool)0)
27
27
28
- static bool isSignedChar (char C ) {
29
- return (C == '+' || C == '-' );
30
- }
28
+ static bool isSignedChar (char C ) { return (C == '+' || C == '-' ); }
31
29
32
30
static bool isExponentChar (char C ) {
33
31
switch (C ) {
34
- case 'D' : // Strange exponential notation.
35
- case 'd' : // Strange exponential notation.
32
+ case 'D' : // Strange exponential notation.
33
+ case 'd' : // Strange exponential notation.
36
34
case 'e' :
37
- case 'E' : return true;
38
- default : return false;
35
+ case 'E' :
36
+ return true;
37
+ default :
38
+ return false;
39
39
}
40
40
}
41
41
42
42
static bool isDigitChar (char C ) {
43
43
switch (C ) {
44
- case '0' : case '1' : case '2' : case '3' : case '4' :
45
- case '5' : case '6' : case '7' : case '8' : case '9' :
44
+ case '0' :
45
+ case '1' :
46
+ case '2' :
47
+ case '3' :
48
+ case '4' :
49
+ case '5' :
50
+ case '6' :
51
+ case '7' :
52
+ case '8' :
53
+ case '9' :
46
54
return true;
47
55
default :
48
56
return false;
@@ -119,78 +127,97 @@ static bool skip_whitespace(const char **FP, const char *FEnd) {
119
127
static bool CompareNumbers (const char * F1P , const char * F2P , const char * F1End ,
120
128
const char * F2End , double AbsTolerance ,
121
129
double RelTolerance ) {
122
- const char * F1NumEnd , * F2NumEnd ;
130
+ char * F1NumEnd , * F2NumEnd ;
123
131
const ptrdiff_t F1Len = F1End - F1P ;
124
132
const ptrdiff_t F2Len = F2End - F2P ;
125
133
double V1 = 0.0 , V2 = 0.0 ;
134
+ char * f1P = (char * )malloc (F1Len );
135
+ memcpy ((void * )f1P , (const void * )F1P , F1Len );
136
+ char * f2P = (char * )malloc (F2Len );
137
+ memcpy ((void * )f2P , (const void * )F2P , F2Len );
126
138
127
139
// Fast character-by-character comparison of the numbers.
128
- if (F1Len == F2Len && memcmp (F1P , F2P , F1Len ) == 0 )
140
+ if (F1Len == F2Len && memcmp (f1P , f2P , F1Len ) == 0 ) {
141
+ free (f1P );
142
+ free (f2P );
129
143
return false;
144
+ }
130
145
131
146
// Note that some ugliness is built into this to permit support for numbers
132
147
// that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
133
148
// occurs in 200.sixtrack in spec2k.
134
- V1 = strtod (F1P , (char * * )(& F1NumEnd ));
135
- V2 = strtod (F2P , (char * * )(& F2NumEnd ));
149
+ V1 = strtod (f1P , (char * * )(& F1NumEnd ));
150
+ V2 = strtod (f2P , (char * * )(& F2NumEnd ));
136
151
137
152
if (* F1NumEnd == 'D' || * F1NumEnd == 'd' ) {
138
153
// Copy string into tmp buffer to replace the 'D' with an 'e'.
139
154
char * StrTmp = malloc (F1Len + 1 );
140
- memcpy (StrTmp , F1P , F1Len + 1 );
155
+ memcpy (StrTmp , f1P , F1Len + 1 );
141
156
142
157
// Strange exponential notation!
143
- StrTmp [(unsigned )(F1NumEnd - F1P )] = 'e' ;
158
+ StrTmp [(unsigned )(F1NumEnd - f1P )] = 'e' ;
144
159
145
160
V1 = strtod (& StrTmp [0 ], (char * * )(& F1NumEnd ));
146
- F1NumEnd = F1P + (F1NumEnd - & StrTmp [0 ]);
161
+ F1NumEnd = f1P + (F1NumEnd - & StrTmp [0 ]);
147
162
148
163
free (StrTmp );
149
164
}
150
165
151
166
if (* F2NumEnd == 'D' || * F2NumEnd == 'd' ) {
152
167
// Copy string into tmp buffer to replace the 'D' with an 'e'.
153
168
char * StrTmp = malloc (F2Len + 1 );
154
- memcpy (StrTmp , F2P , F2Len + 1 );
169
+ memcpy (StrTmp , f2P , F2Len + 1 );
155
170
156
171
// Strange exponential notation!
157
- StrTmp [(unsigned )(F2NumEnd - F2P )] = 'e' ;
172
+ StrTmp [(unsigned )(F2NumEnd - f2P )] = 'e' ;
158
173
159
174
V2 = strtod (& StrTmp [0 ], (char * * )(& F2NumEnd ));
160
- F2NumEnd = F2P + (F2NumEnd - & StrTmp [0 ]);
175
+ F2NumEnd = f2P + (F2NumEnd - & StrTmp [0 ]);
161
176
162
177
free (StrTmp );
163
178
}
164
179
165
- if (F1NumEnd == F1P || F2NumEnd == F2P ) {
166
- fprintf (stderr , ("%s: FP Comparison failed, not a numeric difference "
167
- "between '%c' and '%c'\n" ), g_program , F1P [0 ], F2P [0 ]);
180
+ if (F1NumEnd == f1P || F2NumEnd == f2P ) {
181
+ fprintf (stderr ,
182
+ ("%s: FP Comparison failed, not a numeric difference "
183
+ "between '%c' and '%c'\n" ),
184
+ g_program , f1P [0 ], f2P [0 ]);
185
+ free (f1P );
186
+ free (f2P );
168
187
return true;
169
188
}
170
189
171
190
// Quick check for identical values
172
- if (V1 == V2 )
191
+ if (V1 == V2 ) {
192
+ free (f1P );
193
+ free (f2P );
173
194
return false;
195
+ }
174
196
175
197
// Check to see if these are inside the absolute tolerance
176
198
if (AbsTolerance == 0.0 || AbsTolerance < fabs (V1 - V2 )) {
177
199
// Nope, check the relative tolerance...
178
200
double Diff ;
179
201
if (V2 )
180
- Diff = fabs (V1 / V2 - 1.0 );
202
+ Diff = fabs (V1 / V2 - 1.0 );
181
203
else if (V1 )
182
- Diff = fabs (V2 / V1 - 1.0 );
204
+ Diff = fabs (V2 / V1 - 1.0 );
183
205
else
184
- Diff = 0 ; // Both zero.
206
+ Diff = 0 ; // Both zero.
185
207
if (RelTolerance == 0.0 || Diff > RelTolerance ) {
186
- fprintf (stderr , ("%s: Compared: %e and %e\n"
187
- "abs. diff = %e rel.diff = %e\n"
188
- "Out of tolerance: rel/abs: %e/%e\n" ),
189
- g_program , V1 , V2 , fabs (V1 - V2 ), Diff , RelTolerance , AbsTolerance );
208
+ fprintf (stderr ,
209
+ ("%s: Compared: %e and %e\n"
210
+ "abs. diff = %e rel.diff = %e\n"
211
+ "Out of tolerance: rel/abs: %e/%e\n" ),
212
+ g_program , V1 , V2 , fabs (V1 - V2 ), Diff , RelTolerance ,
213
+ AbsTolerance );
214
+ free (f1P );
215
+ free (f2P );
190
216
return true;
191
217
}
192
218
}
193
-
219
+ free (f1P );
220
+ free (f2P );
194
221
return false;
195
222
}
196
223
@@ -236,8 +263,8 @@ char *load_file(const char *path, long *size_out) {
236
263
/* Read in the file contents. */
237
264
data [size ] = 0 ;
238
265
if (fread (data , size , 1 , fp ) != 1 ) {
239
- fprintf (stderr , "%s: error: unable to read data for '%s'\n" ,
240
- g_program , path );
266
+ fprintf (stderr , "%s: error: unable to read data for '%s'\n" , g_program ,
267
+ path );
241
268
exit (2 );
242
269
}
243
270
@@ -386,7 +413,7 @@ void usage() {
386
413
exit (2 );
387
414
}
388
415
389
- int main (int argc , char * const argv []) {
416
+ int main (int argc , char * const argv []) {
390
417
double relative_tolerance = 0.0 ;
391
418
double absolute_tolerance = 0.0 ;
392
419
bool parse_fp = false;
@@ -397,7 +424,7 @@ int main(int argc, char * const argv[]) {
397
424
for (i = 1 ; i != argc ; ++ i ) {
398
425
const char * arg = argv [i ];
399
426
if (arg [0 ] != '-' )
400
- break ;
427
+ break ;
401
428
402
429
if (strlen (arg ) != 2 ) {
403
430
fprintf (stderr , "error: invalid argument '%s'\n\n" , arg );
0 commit comments