1 //===- fpcmp.cpp - A fuzzy "cmp" that permits floating point noise --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "Support/CommandLine.h"
16 #include "Support/FileUtilities.h"
17 #include "Config/fcntl.h"
18 #include "Config/sys/mman.h"
26 File1(cl::Positional, cl::desc("<input file #1>"), cl::Required);
28 File2(cl::Positional, cl::desc("<input file #2>"), cl::Required);
31 RelTolerance("r", cl::desc("Relative error tolerated"), cl::init(0));
33 AbsTolerance("a", cl::desc("Absolute error tolerated"), cl::init(0));
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 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";
46 // mmap in the file all at once...
47 BufPtr = (char*)mmap(0, Len, PROT_READ, MAP_PRIVATE, FD, 0);
49 if (BufPtr == (char*)MAP_FAILED) {
50 std::cerr << "Error: cannot open file '" << Filename << "'\n";
55 static bool isNumberChar(char C) {
57 case '0': case '1': case '2': case '3': case '4':
58 case '5': case '6': case '7': case '8': case '9':
59 case '.': case '+': case '-':
61 case 'E': return true;
62 default: return false;
66 static char *BackupNumber(char *Pos, char *FirstChar) {
67 while (Pos > FirstChar && isNumberChar(Pos[-1]))
72 static void CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End) {
73 char *F1NumEnd, *F2NumEnd;
74 double V1 = strtod(F1P, &F1NumEnd);
75 double V2 = strtod(F2P, &F2NumEnd);
77 if (F1NumEnd == F1P || F2NumEnd == F2P) {
78 std::cerr << "Comparison failed, not a numeric difference.\n";
82 // Check to see if these are inside the absolute tolerance
83 if (AbsTolerance < std::abs(V1-V2)) {
84 // Nope, check the relative tolerance...
87 Diff = std::abs(V1/V2 - 1.0);
89 Diff = std::abs(V2/V1 - 1.0);
91 Diff = 0; // Both zero.
92 if (Diff > RelTolerance) {
93 std::cerr << "Compared: " << V1 << " and " << V2 << ": diff = "
95 std::cerr << "Out of tolerance: rel/abs: " << RelTolerance
96 << "/" << AbsTolerance << "\n";
101 // Otherwise, advance our read pointers to the end of the numbers.
102 F1P = F1NumEnd; F2P = F2NumEnd;
106 int main(int argc, char **argv) {
107 cl::ParseCommandLineOptions(argc, argv);
109 // mmap in the files.
110 unsigned File1Len, File2Len;
111 char *File1Start, *File2Start;
112 OpenFile(File1, File1Len, File1Start);
113 OpenFile(File2, File2Len, File2Start);
115 // Okay, now that we opened the files, scan them for the first difference.
116 char *File1End = File1Start+File1Len;
117 char *File2End = File2Start+File2Len;
118 char *F1P = File1Start;
119 char *F2P = File2Start;
122 // Scan for the end of file or first difference.
123 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
126 if (F1P >= File1End || F2P >= File2End) break;
128 // Okay, we must have found a difference. Backup to the start of the
129 // current number each stream is at so that we can compare from the
131 F1P = BackupNumber(F1P, File1Start);
132 F2P = BackupNumber(F2P, File2Start);
134 // Now that we are at the start of the numbers, compare them, exiting if
136 CompareNumbers(F1P, F2P, File1End, File2End);
139 // Okay, we reached the end of file. If both files are at the end, we
141 if (F1P >= File1End && F2P >= File2End) return 0;
143 // Otherwise, we might have run off the end due to a number, backup and retry.
144 F1P = BackupNumber(F1P, File1Start);
145 F2P = BackupNumber(F2P, File2Start);
147 // Now that we are at the start of the numbers, compare them, exiting if
149 CompareNumbers(F1P, F2P, File1End, File2End);
151 // If we found the end, we succeeded.
152 if (F1P >= File1End && F2P >= File2End) return 0;