|
| 1 | +// division.cpp: regression tests for efloat division. |
| 2 | +// |
| 3 | +// REGRESSION_LEVEL convention (intensity progression): |
| 4 | +// LEVEL 1 -- all foundational hand-curated tests: algebraic invariants, |
| 5 | +// hostile arithmetic corner cases (round-to-even, massive |
| 6 | +// exponent gaps, complete overlap), subnormal boundaries, |
| 7 | +// IEEE 754 special values. |
| 8 | +// LEVEL 2 -- property-based fuzzer over random multi-component expansions |
| 9 | +// (~1,000 iterations per invariant). |
| 10 | +// LEVEL 3 -- same fuzzer at higher intensity (~100,000 iterations). |
| 11 | +// LEVEL 4 -- exhaustive fuzzer (~10,000,000 iterations). |
| 12 | +// |
| 13 | +// Copyright (C) 2017 Stillwater Supercomputing, Inc. |
| 14 | +// SPDX-License-Identifier: MIT |
| 15 | +// |
| 16 | +// This file is part of the universal numbers project, which is released under an MIT Open Source license. |
| 17 | +#include <universal/utility/directives.hpp> |
| 18 | +#include <cmath> |
| 19 | +#include <limits> |
| 20 | +#include <random> |
| 21 | +#include <universal/number/efloat/efloat.hpp> |
| 22 | +#include <universal/verification/test_suite.hpp> |
| 23 | +#include <universal/verification/efloat_test_support.hpp> |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | + // ========================================================================= |
| 28 | + // LEVEL 1: foundational hand-curated tests |
| 29 | + // ========================================================================= |
| 30 | + int VerifyEfloatDivision(bool reportTestCases) { |
| 31 | + using namespace sw::universal; |
| 32 | + int nrOfFailedTestCases = 0; |
| 33 | + |
| 34 | + // --- Algebraic invariants --- |
| 35 | + |
| 36 | + // Identity: a / 1.0 == a |
| 37 | + if (reportTestCases) std::cout << " Identity...\n"; |
| 38 | + { |
| 39 | + efloat<16> a(1.234567e+15); |
| 40 | + efloat<16> one(1.0); |
| 41 | + efloat<16> result = a / one; |
| 42 | + if (result != a) { |
| 43 | + if (reportTestCases) std::cout << " FAIL a / 1 != a\n"; |
| 44 | + ++nrOfFailedTestCases; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Self divisor: a / a == 1.0 |
| 49 | + if (reportTestCases) std::cout << " Self divisor...\n"; |
| 50 | + { |
| 51 | + efloat<16> a(1.234567e+15); |
| 52 | + efloat<16> result = a / a; |
| 53 | + efloat<16> expected(1.0); |
| 54 | + if (result != expected) { |
| 55 | + if (reportTestCases) std::cout << " FAIL a / a != 1\n"; |
| 56 | + ++nrOfFailedTestCases; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Zero dividend: 0.0 / a == 0.0 |
| 61 | + if (reportTestCases) std::cout << " Zero dividend...\n"; |
| 62 | + { |
| 63 | + efloat<16> a(1.234567e+15); |
| 64 | + efloat<16> zero(0.0); |
| 65 | + efloat<16> result = zero / a; |
| 66 | + if (!result.iszero()) { |
| 67 | + if (reportTestCases) std::cout << " FAIL 0 / a != 0\n"; |
| 68 | + ++nrOfFailedTestCases; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // --- Hostile arithmetic --- |
| 73 | + |
| 74 | + // Exact division: 2^40 / 2^20 = 2^20 |
| 75 | + if (reportTestCases) std::cout << " Limb contraction (exact powers of 2)...\n"; |
| 76 | + { |
| 77 | + efloat<16> a(1099511627776.0); // 2^40 |
| 78 | + efloat<16> b(1048576.0); // 2^20 |
| 79 | + efloat<16> result = a / b; |
| 80 | + efloat<16> expected(1048576.0); // 2^20 |
| 81 | + if (result != expected) { |
| 82 | + if (reportTestCases) { |
| 83 | + std::cout << " FAIL limb contraction value mismatch. Result: " << result << " Expected: " << expected << "\n"; |
| 84 | + } |
| 85 | + ++nrOfFailedTestCases; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // --- IEEE 754 special values --- |
| 90 | + double qnan = std::numeric_limits<double>::quiet_NaN(); |
| 91 | + double pinf = std::numeric_limits<double>::infinity(); |
| 92 | + double ninf = -pinf; |
| 93 | + |
| 94 | + if (reportTestCases) std::cout << " NaN / finite...\n"; |
| 95 | + { |
| 96 | + efloat<16> a(1.0), n(qnan); |
| 97 | + if (!(n / a).isnan()) { if (reportTestCases) std::cout << " FAIL\n"; ++nrOfFailedTestCases; } |
| 98 | + } |
| 99 | + |
| 100 | + if (reportTestCases) std::cout << " finite / +Inf...\n"; |
| 101 | + { |
| 102 | + efloat<16> a(1.0), inf(pinf); |
| 103 | + if (!(a / inf).iszero()) { if (reportTestCases) std::cout << " FAIL\n"; ++nrOfFailedTestCases; } |
| 104 | + } |
| 105 | + |
| 106 | + if (reportTestCases) std::cout << " finite / Zero (Divide by Zero)...\n"; |
| 107 | + { |
| 108 | + efloat<16> a(1.0), zero(0.0); |
| 109 | + if (!(a / zero).isinf()) { if (reportTestCases) std::cout << " FAIL\n"; ++nrOfFailedTestCases; } |
| 110 | + } |
| 111 | + |
| 112 | + if (reportTestCases) std::cout << " Zero / Zero...\n"; |
| 113 | + { |
| 114 | + efloat<16> zero1(0.0), zero2(0.0); |
| 115 | + if (!(zero1 / zero2).isnan()) { if (reportTestCases) std::cout << " FAIL\n"; ++nrOfFailedTestCases; } |
| 116 | + } |
| 117 | + |
| 118 | + return nrOfFailedTestCases; |
| 119 | + } |
| 120 | + |
| 121 | + // ========================================================================= |
| 122 | + // Fuzzer infrastructure |
| 123 | + // ========================================================================= |
| 124 | + int VerifyEfloatDivision_Fuzz(bool reportTestCases, unsigned nrIterations) { |
| 125 | + using namespace sw::universal; |
| 126 | + const uint64_t seed = 0xC0FFEE'A11CEULL; |
| 127 | + std::mt19937_64 rng(seed); |
| 128 | + int nrOfFailedTestCases = 0; |
| 129 | + efloat<16> one(1.0); |
| 130 | + efloat<16> zero(0.0); |
| 131 | + |
| 132 | + for (unsigned i = 0; i < nrIterations; ++i) { |
| 133 | + efloat<16> a = random_efloat<16>(rng); |
| 134 | + |
| 135 | + // Identity: a / 1 == a |
| 136 | + if (a / one != a) { |
| 137 | + if (reportTestCases) std::cout << " FAIL identity (seed=0x" << std::hex << seed << " iter=" << std::dec << i << ")\n"; |
| 138 | + ++nrOfFailedTestCases; |
| 139 | + } |
| 140 | + // Self divisor: a / a == 1 |
| 141 | + if (a / a != one) { |
| 142 | + if (reportTestCases) std::cout << " FAIL self divisor (seed=0x" << std::hex << seed << " iter=" << std::dec << i << ")\n"; |
| 143 | + ++nrOfFailedTestCases; |
| 144 | + } |
| 145 | + // Zero: 0 / a == 0 |
| 146 | + if (zero / a != zero) { |
| 147 | + if (reportTestCases) std::cout << " FAIL zero (seed=0x" << std::hex << seed << " iter=" << std::dec << i << ")\n"; |
| 148 | + ++nrOfFailedTestCases; |
| 149 | + } |
| 150 | + } |
| 151 | + return nrOfFailedTestCases; |
| 152 | + } |
| 153 | + |
| 154 | +} // anonymous namespace |
| 155 | + |
| 156 | +// Regression testing guards |
| 157 | +#define MANUAL_TESTING 0 |
| 158 | +#ifndef REGRESSION_LEVEL_OVERRIDE |
| 159 | +# undef REGRESSION_LEVEL_1 |
| 160 | +# undef REGRESSION_LEVEL_2 |
| 161 | +# undef REGRESSION_LEVEL_3 |
| 162 | +# undef REGRESSION_LEVEL_4 |
| 163 | +# define REGRESSION_LEVEL_1 1 |
| 164 | +# define REGRESSION_LEVEL_2 0 |
| 165 | +# define REGRESSION_LEVEL_3 0 |
| 166 | +# define REGRESSION_LEVEL_4 0 |
| 167 | +#endif |
| 168 | + |
| 169 | +int main() try { |
| 170 | + using namespace sw::universal; |
| 171 | + |
| 172 | + std::string test_suite = "efloat division"; |
| 173 | + std::string test_tag = "division"; |
| 174 | + bool reportTestCases = true; |
| 175 | + int nrOfFailedTestCases = 0; |
| 176 | + |
| 177 | + ReportTestSuiteHeader(test_suite, reportTestCases); |
| 178 | + |
| 179 | +#if MANUAL_TESTING |
| 180 | + |
| 181 | + nrOfFailedTestCases += ReportTestResult(VerifyEfloatDivision(reportTestCases), "efloat", "division manual"); |
| 182 | + ReportTestSuiteResults(test_suite, nrOfFailedTestCases); |
| 183 | + return EXIT_SUCCESS; // ignore errors in manual mode |
| 184 | + |
| 185 | +#else |
| 186 | + |
| 187 | +# if REGRESSION_LEVEL_1 |
| 188 | + nrOfFailedTestCases += ReportTestResult(VerifyEfloatDivision(reportTestCases), "efloat", "division foundational"); |
| 189 | + nrOfFailedTestCases += ReportTestResult(VerifyEfloatDivision_Fuzz(reportTestCases, 100), "efloat", "division fuzz x100"); |
| 190 | +# endif |
| 191 | + |
| 192 | +# if REGRESSION_LEVEL_2 |
| 193 | + nrOfFailedTestCases += ReportTestResult(VerifyEfloatDivision_Fuzz(reportTestCases, 1000), "efloat", "division fuzz x1k"); |
| 194 | +# endif |
| 195 | + |
| 196 | +# if REGRESSION_LEVEL_3 |
| 197 | + nrOfFailedTestCases += ReportTestResult(VerifyEfloatDivision_Fuzz(reportTestCases, 10000), "efloat", "division fuzz x10k"); |
| 198 | +# endif |
| 199 | + |
| 200 | +# if REGRESSION_LEVEL_4 |
| 201 | + nrOfFailedTestCases += ReportTestResult(VerifyEfloatDivision_Fuzz(reportTestCases, 100000), "efloat", "division fuzz x100k"); |
| 202 | +# endif |
| 203 | + |
| 204 | + ReportTestSuiteResults(test_suite, nrOfFailedTestCases); |
| 205 | + return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
| 206 | + |
| 207 | +#endif // MANUAL_TESTING |
| 208 | +} |
| 209 | +catch (const std::exception& e) { |
| 210 | + std::cerr << "Caught exception: " << e.what() << std::endl; |
| 211 | + return EXIT_FAILURE; |
| 212 | +} |
| 213 | +catch (...) { |
| 214 | + std::cerr << "Caught unknown exception" << std::endl; |
| 215 | + return EXIT_FAILURE; |
| 216 | +} |
0 commit comments