llvm.org GIT mirror llvm / 537a49a
The meat of this utility has been moved to FileUtilities, where it can be used by other tools. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19767 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
1 changed file(s) with 8 addition(s) and 153 deletion(s). Raw diff Collapse all Expand all
77 //===----------------------------------------------------------------------===//
88 //
99 // fpcmp is a tool that basically works like the 'cmp' tool, except that it can
10 // tolerate errors due to floating point noise, with the -r option.
10 // tolerate errors due to floating point noise, with the -r and -a options.
1111 //
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/Support/CommandLine.h"
15 #include "llvm/System/MappedFile.h"
15 #include "llvm/Support/FileUtilities.h"
1616 #include
1717 #include
1818
3030 AbsTolerance("a", cl::desc("Absolute error tolerated"), cl::init(0));
3131 }
3232
33 static bool isNumberChar(char C) {
34 switch (C) {
35 case '0': case '1': case '2': case '3': case '4':
36 case '5': case '6': case '7': case '8': case '9':
37 case '.': case '+': case '-':
38 case 'e':
39 case 'E': return true;
40 default: return false;
41 }
42 }
43
44 static char *BackupNumber(char *Pos, char *FirstChar) {
45 // If we didn't stop in the middle of a number, don't backup.
46 if (!isNumberChar(*Pos)) return Pos;
47
48 // Otherwise, return to the start of the number.
49 while (Pos > FirstChar && isNumberChar(Pos[-1]))
50 --Pos;
51 return Pos;
52 }
53
54 static void CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End) {
55 char *F1NumEnd, *F2NumEnd;
56 double V1 = 0.0, V2 = 0.0;
57 // If we stop on numbers, compare their difference.
58 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
59 V1 = strtod(F1P, &F1NumEnd);
60 V2 = strtod(F2P, &F2NumEnd);
61 } else {
62 // Otherwise, the diff failed.
63 F1NumEnd = F1P;
64 F2NumEnd = F2P;
65 }
66
67 if (F1NumEnd == F1P || F2NumEnd == F2P) {
68 std::cerr << "Comparison failed, not a numeric difference.\n";
69 exit(1);
70 }
71
72 // Check to see if these are inside the absolute tolerance
73 if (AbsTolerance < std::abs(V1-V2)) {
74 // Nope, check the relative tolerance...
75 double Diff;
76 if (V2)
77 Diff = std::abs(V1/V2 - 1.0);
78 else if (V1)
79 Diff = std::abs(V2/V1 - 1.0);
80 else
81 Diff = 0; // Both zero.
82 if (Diff > RelTolerance) {
83 std::cerr << "Compared: " << V1 << " and " << V2 << ": diff = "
84 << Diff << "\n";
85 std::cerr << "Out of tolerance: rel/abs: " << RelTolerance
86 << "/" << AbsTolerance << "\n";
87 exit(1);
88 }
89 }
90
91 // Otherwise, advance our read pointers to the end of the numbers.
92 F1P = F1NumEnd; F2P = F2NumEnd;
93 }
94
95 // PadFileIfNeeded - If the files are not identical, we will have to be doing
96 // numeric comparisons in here. There are bad cases involved where we (i.e.,
97 // strtod) might run off the beginning or end of the file if it starts or ends
98 // with a number. Because of this, if needed, we pad the file so that it starts
99 // and ends with a null character.
100 static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
101 if (isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
102 unsigned FileLen = FileEnd-FileStart;
103 char *NewFile = new char[FileLen+2];
104 NewFile[0] = 0; // Add null padding
105 NewFile[FileLen+1] = 0; // Add null padding
106 memcpy(NewFile+1, FileStart, FileLen);
107 FP = NewFile+(FP-FileStart)+1;
108 FileStart = NewFile+1;
109 FileEnd = FileStart+FileLen;
110 }
111 }
112
11333 int main(int argc, char **argv) {
11434 cl::ParseCommandLineOptions(argc, argv);
11535
116 try {
117 // map in the files into memory
118 sys::Path F1Path(File1);
119 sys::Path F2Path(File2);
120 sys::MappedFile F1 ( F1Path );
121 sys::MappedFile F2 ( F2Path );
122 F1.map();
123 F2.map();
124
125 // Okay, now that we opened the files, scan them for the first difference.
126 char *File1Start = F1.charBase();
127 char *File2Start = F2.charBase();
128 char *File1End = File1Start+F1.size();
129 char *File2End = File2Start+F2.size();
130 char *F1P = File1Start;
131 char *F2P = File2Start;
132
133 // Scan for the end of file or first difference.
134 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
135 ++F1P, ++F2P;
136
137 // Common case: identifical files.
138 if (F1P == File1End && F2P == File2End) return 0;
139
140 // If the files need padding, do so now.
141 PadFileIfNeeded(File1Start, File1End, F1P);
142 PadFileIfNeeded(File2Start, File2End, F2P);
143
144 while (1) {
145 // Scan for the end of file or next difference.
146 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
147 ++F1P, ++F2P;
148
149 if (F1P >= File1End || F2P >= File2End) break;
150
151 // Okay, we must have found a difference. Backup to the start of the
152 // current number each stream is at so that we can compare from the
153 // beginning.
154 F1P = BackupNumber(F1P, File1Start);
155 F2P = BackupNumber(F2P, File2Start);
156
157 // Now that we are at the start of the numbers, compare them, exiting if
158 // they don't match.
159 CompareNumbers(F1P, F2P, File1End, File2End);
160 }
161
162 // Okay, we reached the end of file. If both files are at the end, we
163 // succeeded.
164 bool F1AtEnd = F1P >= File1End;
165 bool F2AtEnd = F2P >= File2End;
166 if (F1AtEnd & F2AtEnd) return 0;
167
168 // Else, we might have run off the end due to a number: backup and retry.
169 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
170 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
171 F1P = BackupNumber(F1P, File1Start);
172 F2P = BackupNumber(F2P, File2Start);
173
174 // Now that we are at the start of the numbers, compare them, exiting if
175 // they don't match.
176 CompareNumbers(F1P, F2P, File1End, File2End);
177
178 // If we found the end, we succeeded.
179 if (F1P >= File1End && F2P >= File2End) return 0;
180
181 } catch (const std::string& msg) {
182 std::cerr << argv[0] << ": error: " << msg << "\n";
183 return 2;
184 }
185
186 return 1;
36 std::string ErrorMsg;
37 int DF = DiffFilesWithTolerance(File1, File2, AbsTolerance, RelTolerance,
38 &ErrorMsg);
39 if (!ErrorMsg.empty())
40 std::cerr << argv[0] << ": " << ErrorMsg << "\n";
41 return DF;
18742 }
18843