blob: ff93b9bc49e4d495a9c20553fbdfcc5f2bdb4e61 [file] [log] [blame]
Chris Lattner337481a2004-04-13 20:55:49 +00001//===- fpcmp.cpp - A fuzzy "cmp" that permits floating point noise --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// fpcmp is a tool that basically works like the 'cmp' tool, except that it can
11// tolerate errors due to floating point noise, with the -r option.
12//
13//===----------------------------------------------------------------------===//
14
Reid Spencer551ccae2004-09-01 22:55:40 +000015#include "llvm/Support/CommandLine.h"
16#include "llvm/Support/FileUtilities.h"
Chris Lattner337481a2004-04-13 20:55:49 +000017#include <iostream>
18#include <cmath>
19
20using namespace llvm;
21
22namespace {
23 cl::opt<std::string>
24 File1(cl::Positional, cl::desc("<input file #1>"), cl::Required);
25 cl::opt<std::string>
26 File2(cl::Positional, cl::desc("<input file #2>"), cl::Required);
27
28 cl::opt<double>
29 RelTolerance("r", cl::desc("Relative error tolerated"), cl::init(0));
30 cl::opt<double>
31 AbsTolerance("a", cl::desc("Absolute error tolerated"), cl::init(0));
32}
33
34
35/// OpenFile - mmap the specified file into the address space for reading, and
36/// return the length and address of the buffer.
37static void OpenFile(const std::string &Filename, unsigned &Len, char* &BufPtr){
Chris Lattner531b8022004-05-28 00:31:36 +000038 BufPtr = (char*)ReadFileIntoAddressSpace(Filename, Len);
39 if (BufPtr == 0) {
Chris Lattner337481a2004-04-13 20:55:49 +000040 std::cerr << "Error: cannot open file '" << Filename << "'\n";
41 exit(2);
42 }
Chris Lattner337481a2004-04-13 20:55:49 +000043}
44
45static bool isNumberChar(char C) {
46 switch (C) {
47 case '0': case '1': case '2': case '3': case '4':
48 case '5': case '6': case '7': case '8': case '9':
Chris Lattner6de6a0a2004-04-13 21:48:43 +000049 case '.': case '+': case '-':
Chris Lattner337481a2004-04-13 20:55:49 +000050 case 'e':
51 case 'E': return true;
52 default: return false;
53 }
54}
55
56static char *BackupNumber(char *Pos, char *FirstChar) {
Chris Lattnerff2c64c2004-06-09 18:28:53 +000057 // If we didn't stop in the middle of a number, don't backup.
58 if (!isNumberChar(*Pos)) return Pos;
59
60 // Otherwise, return to the start of the number.
Chris Lattner6de6a0a2004-04-13 21:48:43 +000061 while (Pos > FirstChar && isNumberChar(Pos[-1]))
Chris Lattner337481a2004-04-13 20:55:49 +000062 --Pos;
63 return Pos;
64}
65
66static void CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End) {
67 char *F1NumEnd, *F2NumEnd;
Reid Spencer4a4bfd82004-06-13 19:17:49 +000068 double V1 = 0.0, V2 = 0.0;
Chris Lattnerff2c64c2004-06-09 18:28:53 +000069 // If we stop on numbers, compare their difference.
70 if (isNumberChar(*F1P) && isNumberChar(*F2P)) {
71 V1 = strtod(F1P, &F1NumEnd);
72 V2 = strtod(F2P, &F2NumEnd);
73 } else {
74 // Otherwise, the diff failed.
75 F1NumEnd = F1P;
76 F2NumEnd = F2P;
77 }
Chris Lattner337481a2004-04-13 20:55:49 +000078
79 if (F1NumEnd == F1P || F2NumEnd == F2P) {
80 std::cerr << "Comparison failed, not a numeric difference.\n";
81 exit(1);
82 }
83
84 // Check to see if these are inside the absolute tolerance
85 if (AbsTolerance < std::abs(V1-V2)) {
86 // Nope, check the relative tolerance...
87 double Diff;
88 if (V2)
89 Diff = std::abs(V1/V2 - 1.0);
90 else if (V1)
91 Diff = std::abs(V2/V1 - 1.0);
92 else
93 Diff = 0; // Both zero.
94 if (Diff > RelTolerance) {
95 std::cerr << "Compared: " << V1 << " and " << V2 << ": diff = "
96 << Diff << "\n";
Brian Gaeke9a459ab2004-04-19 19:09:24 +000097 std::cerr << "Out of tolerance: rel/abs: " << RelTolerance
Chris Lattner337481a2004-04-13 20:55:49 +000098 << "/" << AbsTolerance << "\n";
99 exit(1);
100 }
101 }
102
103 // Otherwise, advance our read pointers to the end of the numbers.
104 F1P = F1NumEnd; F2P = F2NumEnd;
105}
106
Chris Lattner8e6d2bb2004-06-20 03:12:18 +0000107// PadFileIfNeeded - If the files are not identical, we will have to be doing
108// numeric comparisons in here. There are bad cases involved where we (i.e.,
109// strtod) might run off the beginning or end of the file if it starts or ends
110// with a number. Because of this, if needed, we pad the file so that it starts
111// and ends with a null character.
112static void PadFileIfNeeded(char *&FileStart, char *&FileEnd, char *&FP) {
113 if (isNumberChar(FileStart[0]) || isNumberChar(FileEnd[-1])) {
114 unsigned FileLen = FileEnd-FileStart;
115 char *NewFile = new char[FileLen+2];
116 NewFile[0] = 0; // Add null padding
117 NewFile[FileLen+1] = 0; // Add null padding
118 memcpy(NewFile+1, FileStart, FileLen);
119 FP = NewFile+(FP-FileStart)+1;
120 FileStart = NewFile+1;
121 FileEnd = FileStart+FileLen;
122 }
123}
Chris Lattner337481a2004-04-13 20:55:49 +0000124
125int main(int argc, char **argv) {
126 cl::ParseCommandLineOptions(argc, argv);
127
128 // mmap in the files.
129 unsigned File1Len, File2Len;
130 char *File1Start, *File2Start;
131 OpenFile(File1, File1Len, File1Start);
132 OpenFile(File2, File2Len, File2Start);
133
134 // Okay, now that we opened the files, scan them for the first difference.
135 char *File1End = File1Start+File1Len;
136 char *File2End = File2Start+File2Len;
137 char *F1P = File1Start;
138 char *F2P = File2Start;
Chris Lattner8e6d2bb2004-06-20 03:12:18 +0000139
140 // Scan for the end of file or first difference.
141 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
142 ++F1P, ++F2P;
143
144 // Common case: identifical files.
145 if (F1P == File1End && F2P == File2End) return 0;
146
147 // If the files need padding, do so now.
148 PadFileIfNeeded(File1Start, File1End, F1P);
149 PadFileIfNeeded(File2Start, File2End, F2P);
Chris Lattner337481a2004-04-13 20:55:49 +0000150
151 while (1) {
Chris Lattner8e6d2bb2004-06-20 03:12:18 +0000152 // Scan for the end of file or next difference.
Chris Lattner337481a2004-04-13 20:55:49 +0000153 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
154 ++F1P, ++F2P;
155
156 if (F1P >= File1End || F2P >= File2End) break;
157
158 // Okay, we must have found a difference. Backup to the start of the
159 // current number each stream is at so that we can compare from the
160 // beginning.
161 F1P = BackupNumber(F1P, File1Start);
162 F2P = BackupNumber(F2P, File2Start);
163
164 // Now that we are at the start of the numbers, compare them, exiting if
165 // they don't match.
166 CompareNumbers(F1P, F2P, File1End, File2End);
167 }
168
169 // Okay, we reached the end of file. If both files are at the end, we
170 // succeeded.
Chris Lattner8e6d2bb2004-06-20 03:12:18 +0000171 bool F1AtEnd = F1P >= File1End;
172 bool F2AtEnd = F2P >= File2End;
173 if (F1AtEnd & F2AtEnd) return 0;
Chris Lattner337481a2004-04-13 20:55:49 +0000174
Chris Lattner8e6d2bb2004-06-20 03:12:18 +0000175 // Otherwise, we might have run off the end due to a number: backup and retry.
176 if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
177 if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
Chris Lattner337481a2004-04-13 20:55:49 +0000178 F1P = BackupNumber(F1P, File1Start);
179 F2P = BackupNumber(F2P, File2Start);
180
181 // Now that we are at the start of the numbers, compare them, exiting if
182 // they don't match.
183 CompareNumbers(F1P, F2P, File1End, File2End);
184
185 // If we found the end, we succeeded.
186 if (F1P >= File1End && F2P >= File2End) return 0;
187
188 return 1;
189}
190