blob: 7e472d474f67fcdaf0daf0cd3e382caf067e6a3a [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
15#include "Support/CommandLine.h"
16#include "Support/FileUtilities.h"
17#include "Config/fcntl.h"
18#include "Config/sys/mman.h"
19#include <iostream>
20#include <cmath>
21
22using namespace llvm;
23
24namespace {
25 cl::opt<std::string>
26 File1(cl::Positional, cl::desc("<input file #1>"), cl::Required);
27 cl::opt<std::string>
28 File2(cl::Positional, cl::desc("<input file #2>"), cl::Required);
29
30 cl::opt<double>
31 RelTolerance("r", cl::desc("Relative error tolerated"), cl::init(0));
32 cl::opt<double>
33 AbsTolerance("a", cl::desc("Absolute error tolerated"), cl::init(0));
34}
35
36
37/// OpenFile - mmap the specified file into the address space for reading, and
38/// return the length and address of the buffer.
39static void OpenFile(const std::string &Filename, unsigned &Len, char* &BufPtr){
40 int FD = open(Filename.c_str(), O_RDONLY);
41 if (FD == -1 || (Len = getFileSize(Filename)) == ~0U) {
42 std::cerr << "Error: cannot open file '" << Filename << "'\n";
43 exit(2);
44 }
45
46 // mmap in the file all at once...
47 BufPtr = (char*)mmap(0, Len, PROT_READ, MAP_PRIVATE, FD, 0);
48
49 if (BufPtr == (char*)MAP_FAILED) {
50 std::cerr << "Error: cannot open file '" << Filename << "'\n";
51 exit(2);
52 }
Brian Gaeke6893ead2004-04-23 17:38:17 +000053
54 // If mmap decided that the files were empty, it might have returned a
55 // null pointer. If so, make a new, fake pointer -- it shouldn't matter
56 // what it contains, because Len is 0, and it should never be read.
57 if (BufPtr == 0 && Len == 0)
58 BufPtr = new char[1];
Chris Lattner337481a2004-04-13 20:55:49 +000059}
60
61static bool isNumberChar(char C) {
62 switch (C) {
63 case '0': case '1': case '2': case '3': case '4':
64 case '5': case '6': case '7': case '8': case '9':
Chris Lattner6de6a0a2004-04-13 21:48:43 +000065 case '.': case '+': case '-':
Chris Lattner337481a2004-04-13 20:55:49 +000066 case 'e':
67 case 'E': return true;
68 default: return false;
69 }
70}
71
72static char *BackupNumber(char *Pos, char *FirstChar) {
Chris Lattner6de6a0a2004-04-13 21:48:43 +000073 while (Pos > FirstChar && isNumberChar(Pos[-1]))
Chris Lattner337481a2004-04-13 20:55:49 +000074 --Pos;
75 return Pos;
76}
77
78static void CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End) {
79 char *F1NumEnd, *F2NumEnd;
80 double V1 = strtod(F1P, &F1NumEnd);
81 double V2 = strtod(F2P, &F2NumEnd);
82
83 if (F1NumEnd == F1P || F2NumEnd == F2P) {
84 std::cerr << "Comparison failed, not a numeric difference.\n";
85 exit(1);
86 }
87
88 // Check to see if these are inside the absolute tolerance
89 if (AbsTolerance < std::abs(V1-V2)) {
90 // Nope, check the relative tolerance...
91 double Diff;
92 if (V2)
93 Diff = std::abs(V1/V2 - 1.0);
94 else if (V1)
95 Diff = std::abs(V2/V1 - 1.0);
96 else
97 Diff = 0; // Both zero.
98 if (Diff > RelTolerance) {
99 std::cerr << "Compared: " << V1 << " and " << V2 << ": diff = "
100 << Diff << "\n";
Brian Gaeke9a459ab2004-04-19 19:09:24 +0000101 std::cerr << "Out of tolerance: rel/abs: " << RelTolerance
Chris Lattner337481a2004-04-13 20:55:49 +0000102 << "/" << AbsTolerance << "\n";
103 exit(1);
104 }
105 }
106
107 // Otherwise, advance our read pointers to the end of the numbers.
108 F1P = F1NumEnd; F2P = F2NumEnd;
109}
110
111
112int main(int argc, char **argv) {
113 cl::ParseCommandLineOptions(argc, argv);
114
115 // mmap in the files.
116 unsigned File1Len, File2Len;
117 char *File1Start, *File2Start;
118 OpenFile(File1, File1Len, File1Start);
119 OpenFile(File2, File2Len, File2Start);
120
121 // Okay, now that we opened the files, scan them for the first difference.
122 char *File1End = File1Start+File1Len;
123 char *File2End = File2Start+File2Len;
124 char *F1P = File1Start;
125 char *F2P = File2Start;
126
127 while (1) {
128 // Scan for the end of file or first difference.
129 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
130 ++F1P, ++F2P;
131
132 if (F1P >= File1End || F2P >= File2End) break;
133
134 // Okay, we must have found a difference. Backup to the start of the
135 // current number each stream is at so that we can compare from the
136 // beginning.
137 F1P = BackupNumber(F1P, File1Start);
138 F2P = BackupNumber(F2P, File2Start);
139
140 // Now that we are at the start of the numbers, compare them, exiting if
141 // they don't match.
142 CompareNumbers(F1P, F2P, File1End, File2End);
143 }
144
145 // Okay, we reached the end of file. If both files are at the end, we
146 // succeeded.
147 if (F1P >= File1End && F2P >= File2End) return 0;
148
149 // Otherwise, we might have run off the end due to a number, backup and retry.
150 F1P = BackupNumber(F1P, File1Start);
151 F2P = BackupNumber(F2P, File2Start);
152
153 // Now that we are at the start of the numbers, compare them, exiting if
154 // they don't match.
155 CompareNumbers(F1P, F2P, File1End, File2End);
156
157 // If we found the end, we succeeded.
158 if (F1P >= File1End && F2P >= File2End) return 0;
159
160 return 1;
161}
162