Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 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" |
Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <cmath> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 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. |
| 37 | static void OpenFile(const std::string &Filename, unsigned &Len, char* &BufPtr){ |
Chris Lattner | 531b802 | 2004-05-28 00:31:36 +0000 | [diff] [blame] | 38 | BufPtr = (char*)ReadFileIntoAddressSpace(Filename, Len); |
| 39 | if (BufPtr == 0) { |
Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 40 | std::cerr << "Error: cannot open file '" << Filename << "'\n"; |
| 41 | exit(2); |
| 42 | } |
Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static 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 Lattner | 6de6a0a | 2004-04-13 21:48:43 +0000 | [diff] [blame] | 49 | case '.': case '+': case '-': |
Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 50 | case 'e': |
| 51 | case 'E': return true; |
| 52 | default: return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static char *BackupNumber(char *Pos, char *FirstChar) { |
Chris Lattner | 6de6a0a | 2004-04-13 21:48:43 +0000 | [diff] [blame] | 57 | while (Pos > FirstChar && isNumberChar(Pos[-1])) |
Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 58 | --Pos; |
| 59 | return Pos; |
| 60 | } |
| 61 | |
| 62 | static void CompareNumbers(char *&F1P, char *&F2P, char *F1End, char *F2End) { |
| 63 | char *F1NumEnd, *F2NumEnd; |
| 64 | double V1 = strtod(F1P, &F1NumEnd); |
| 65 | double V2 = strtod(F2P, &F2NumEnd); |
| 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"; |
Brian Gaeke | 9a459ab | 2004-04-19 19:09:24 +0000 | [diff] [blame] | 85 | std::cerr << "Out of tolerance: rel/abs: " << RelTolerance |
Chris Lattner | 337481a | 2004-04-13 20:55:49 +0000 | [diff] [blame] | 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 | |
| 96 | int main(int argc, char **argv) { |
| 97 | cl::ParseCommandLineOptions(argc, argv); |
| 98 | |
| 99 | // mmap in the files. |
| 100 | unsigned File1Len, File2Len; |
| 101 | char *File1Start, *File2Start; |
| 102 | OpenFile(File1, File1Len, File1Start); |
| 103 | OpenFile(File2, File2Len, File2Start); |
| 104 | |
| 105 | // Okay, now that we opened the files, scan them for the first difference. |
| 106 | char *File1End = File1Start+File1Len; |
| 107 | char *File2End = File2Start+File2Len; |
| 108 | char *F1P = File1Start; |
| 109 | char *F2P = File2Start; |
| 110 | |
| 111 | while (1) { |
| 112 | // Scan for the end of file or first difference. |
| 113 | while (F1P < File1End && F2P < File2End && *F1P == *F2P) |
| 114 | ++F1P, ++F2P; |
| 115 | |
| 116 | if (F1P >= File1End || F2P >= File2End) break; |
| 117 | |
| 118 | // Okay, we must have found a difference. Backup to the start of the |
| 119 | // current number each stream is at so that we can compare from the |
| 120 | // beginning. |
| 121 | F1P = BackupNumber(F1P, File1Start); |
| 122 | F2P = BackupNumber(F2P, File2Start); |
| 123 | |
| 124 | // Now that we are at the start of the numbers, compare them, exiting if |
| 125 | // they don't match. |
| 126 | CompareNumbers(F1P, F2P, File1End, File2End); |
| 127 | } |
| 128 | |
| 129 | // Okay, we reached the end of file. If both files are at the end, we |
| 130 | // succeeded. |
| 131 | if (F1P >= File1End && F2P >= File2End) return 0; |
| 132 | |
| 133 | // Otherwise, we might have run off the end due to a number, backup and retry. |
| 134 | F1P = BackupNumber(F1P, File1Start); |
| 135 | F2P = BackupNumber(F2P, File2Start); |
| 136 | |
| 137 | // Now that we are at the start of the numbers, compare them, exiting if |
| 138 | // they don't match. |
| 139 | CompareNumbers(F1P, F2P, File1End, File2End); |
| 140 | |
| 141 | // If we found the end, we succeeded. |
| 142 | if (F1P >= File1End && F2P >= File2End) return 0; |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |