Skip to content

Commit fdd7d1a

Browse files
author
Kavitha Madhu
committed
Warnings fixed
1 parent 86ca84f commit fdd7d1a

File tree

1 file changed

+64
-37
lines changed

1 file changed

+64
-37
lines changed

tools/fpcmp/fpcmp.c

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,35 @@ const char *g_program;
2222
/* *** */
2323

2424
typedef int bool;
25-
#define true ((bool) 1)
26-
#define false ((bool) 0)
25+
#define true ((bool)1)
26+
#define false ((bool)0)
2727

28-
static bool isSignedChar(char C) {
29-
return (C == '+' || C == '-');
30-
}
28+
static bool isSignedChar(char C) { return (C == '+' || C == '-'); }
3129

3230
static bool isExponentChar(char C) {
3331
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.
3634
case 'e':
37-
case 'E': return true;
38-
default: return false;
35+
case 'E':
36+
return true;
37+
default:
38+
return false;
3939
}
4040
}
4141

4242
static bool isDigitChar(char C) {
4343
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':
4654
return true;
4755
default:
4856
return false;
@@ -119,78 +127,97 @@ static bool skip_whitespace(const char **FP, const char *FEnd) {
119127
static bool CompareNumbers(const char *F1P, const char *F2P, const char *F1End,
120128
const char *F2End, double AbsTolerance,
121129
double RelTolerance) {
122-
const char *F1NumEnd, *F2NumEnd;
130+
char *F1NumEnd, *F2NumEnd;
123131
const ptrdiff_t F1Len = F1End - F1P;
124132
const ptrdiff_t F2Len = F2End - F2P;
125133
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);
126138

127139
// 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);
129143
return false;
144+
}
130145

131146
// Note that some ugliness is built into this to permit support for numbers
132147
// that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
133148
// 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));
136151

137152
if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
138153
// Copy string into tmp buffer to replace the 'D' with an 'e'.
139154
char *StrTmp = malloc(F1Len + 1);
140-
memcpy(StrTmp, F1P, F1Len + 1);
155+
memcpy(StrTmp, f1P, F1Len + 1);
141156

142157
// Strange exponential notation!
143-
StrTmp[(unsigned)(F1NumEnd - F1P)] = 'e';
158+
StrTmp[(unsigned)(F1NumEnd - f1P)] = 'e';
144159

145160
V1 = strtod(&StrTmp[0], (char **)(&F1NumEnd));
146-
F1NumEnd = F1P + (F1NumEnd - &StrTmp[0]);
161+
F1NumEnd = f1P + (F1NumEnd - &StrTmp[0]);
147162

148163
free(StrTmp);
149164
}
150165

151166
if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
152167
// Copy string into tmp buffer to replace the 'D' with an 'e'.
153168
char *StrTmp = malloc(F2Len + 1);
154-
memcpy(StrTmp, F2P, F2Len + 1);
169+
memcpy(StrTmp, f2P, F2Len + 1);
155170

156171
// Strange exponential notation!
157-
StrTmp[(unsigned)(F2NumEnd - F2P)] = 'e';
172+
StrTmp[(unsigned)(F2NumEnd - f2P)] = 'e';
158173

159174
V2 = strtod(&StrTmp[0], (char **)(&F2NumEnd));
160-
F2NumEnd = F2P + (F2NumEnd - &StrTmp[0]);
175+
F2NumEnd = f2P + (F2NumEnd - &StrTmp[0]);
161176

162177
free(StrTmp);
163178
}
164179

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);
168187
return true;
169188
}
170189

171190
// Quick check for identical values
172-
if (V1 == V2)
191+
if (V1 == V2) {
192+
free(f1P);
193+
free(f2P);
173194
return false;
195+
}
174196

175197
// Check to see if these are inside the absolute tolerance
176198
if (AbsTolerance == 0.0 || AbsTolerance < fabs(V1 - V2)) {
177199
// Nope, check the relative tolerance...
178200
double Diff;
179201
if (V2)
180-
Diff = fabs(V1/V2 - 1.0);
202+
Diff = fabs(V1 / V2 - 1.0);
181203
else if (V1)
182-
Diff = fabs(V2/V1 - 1.0);
204+
Diff = fabs(V2 / V1 - 1.0);
183205
else
184-
Diff = 0; // Both zero.
206+
Diff = 0; // Both zero.
185207
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);
190216
return true;
191217
}
192218
}
193-
219+
free(f1P);
220+
free(f2P);
194221
return false;
195222
}
196223

@@ -236,8 +263,8 @@ char *load_file(const char *path, long *size_out) {
236263
/* Read in the file contents. */
237264
data[size] = 0;
238265
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);
241268
exit(2);
242269
}
243270

@@ -386,7 +413,7 @@ void usage() {
386413
exit(2);
387414
}
388415

389-
int main(int argc, char * const argv[]) {
416+
int main(int argc, char *const argv[]) {
390417
double relative_tolerance = 0.0;
391418
double absolute_tolerance = 0.0;
392419
bool parse_fp = false;
@@ -397,7 +424,7 @@ int main(int argc, char * const argv[]) {
397424
for (i = 1; i != argc; ++i) {
398425
const char *arg = argv[i];
399426
if (arg[0] != '-')
400-
break;
427+
break;
401428

402429
if (strlen(arg) != 2) {
403430
fprintf(stderr, "error: invalid argument '%s'\n\n", arg);

0 commit comments

Comments
 (0)