e7e318efd3ddd5b7aaf43a70338e525f5f1b65b1
[oota-llvm.git] / utils / fpcmp / fpcmp.cpp
1 //===- 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
22 using namespace llvm;
23
24 namespace {
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.
39 static void OpenFile(const std::string &Filename, unsigned &Len, char* &BufPtr){
40   BufPtr = (char*)ReadFileIntoAddressSpace(Filename, Len);
41   if (BufPtr == 0) {
42     std::cerr << "Error: cannot open file '" << Filename << "'\n";
43     exit(2);
44   }
45 }
46
47 static bool isNumberChar(char C) {
48   switch (C) {
49   case '0': case '1': case '2': case '3': case '4':
50   case '5': case '6': case '7': case '8': case '9': 
51   case '.': case '+': case '-':
52   case 'e':
53   case 'E': return true;
54   default: return false;
55   }
56 }
57
58 static char *BackupNumber(char *Pos, char *FirstChar) {
59   while (Pos > FirstChar && isNumberChar(Pos[-1]))
60     --Pos;
61   return Pos;
62 }
63
64 static void CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End) {
65   char *F1NumEnd, *F2NumEnd;
66   double V1 = strtod(F1P, &F1NumEnd);
67   double V2 = strtod(F2P, &F2NumEnd);
68
69   if (F1NumEnd == F1P || F2NumEnd == F2P) {
70     std::cerr << "Comparison failed, not a numeric difference.\n";
71     exit(1);
72   }
73
74   // Check to see if these are inside the absolute tolerance
75   if (AbsTolerance < std::abs(V1-V2)) {
76     // Nope, check the relative tolerance...
77     double Diff;
78     if (V2)
79       Diff = std::abs(V1/V2 - 1.0);
80     else if (V1)
81       Diff = std::abs(V2/V1 - 1.0);
82     else
83       Diff = 0;  // Both zero.
84     if (Diff > RelTolerance) {
85       std::cerr << "Compared: " << V1 << " and " << V2 << ": diff = "
86                 << Diff << "\n";
87       std::cerr << "Out of tolerance: rel/abs: " << RelTolerance
88                 << "/" << AbsTolerance << "\n";
89       exit(1);
90     }
91   }
92
93   // Otherwise, advance our read pointers to the end of the numbers.
94   F1P = F1NumEnd;  F2P = F2NumEnd;
95 }
96
97
98 int main(int argc, char **argv) {
99   cl::ParseCommandLineOptions(argc, argv);
100
101   // mmap in the files.
102   unsigned File1Len, File2Len;
103   char *File1Start, *File2Start;
104   OpenFile(File1, File1Len, File1Start);
105   OpenFile(File2, File2Len, File2Start);
106
107   // Okay, now that we opened the files, scan them for the first difference.
108   char *File1End = File1Start+File1Len;
109   char *File2End = File2Start+File2Len;
110   char *F1P = File1Start;
111   char *F2P = File2Start;
112   
113   while (1) {
114     // Scan for the end of file or first difference.
115     while (F1P < File1End && F2P < File2End && *F1P == *F2P)
116       ++F1P, ++F2P;
117
118     if (F1P >= File1End || F2P >= File2End) break;
119
120     // Okay, we must have found a difference.  Backup to the start of the
121     // current number each stream is at so that we can compare from the
122     // beginning.
123     F1P = BackupNumber(F1P, File1Start);
124     F2P = BackupNumber(F2P, File2Start);
125
126     // Now that we are at the start of the numbers, compare them, exiting if
127     // they don't match.
128     CompareNumbers(F1P, F2P, File1End, File2End);
129   }
130
131   // Okay, we reached the end of file.  If both files are at the end, we
132   // succeeded.
133   if (F1P >= File1End && F2P >= File2End) return 0;
134
135   // Otherwise, we might have run off the end due to a number, backup and retry.
136   F1P = BackupNumber(F1P, File1Start);
137   F2P = BackupNumber(F2P, File2Start);
138
139   // Now that we are at the start of the numbers, compare them, exiting if
140   // they don't match.
141   CompareNumbers(F1P, F2P, File1End, File2End);
142
143   // If we found the end, we succeeded.
144   if (F1P >= File1End && F2P >= File2End) return 0;
145
146   return 1;
147 }
148