• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stillwater-sc / universal / 43514747065

13 Nov 2026 09:54PM UTC coverage: 89.046% (-0.004%) from 89.05%
43514747065

push

github

web-flow
fix(internal): value arithmetic and special values, bitblock helpers, abstract triple, blockdecimal/blockdigit edges; archive gfp/f2s (#2057)

* fix(internal): value arithmetic and special values, bitblock helpers, abstract triple, blockdecimal/blockdigit edges; archive gfp/f2s

The legacy and digit-container findings of QA sweep #2029.

gfp, f2s (USER decision: archive)
- include/sw/universal/internal/{gfp,f2s} and internal/{gfp,f2s} move to ./archive
  (git mv, history kept), out of the build. Their Grisu paths were stubs, the two
  headers could not be included together, and their tests asserted nothing.

value<fbits>
- + and - used abits = fbits + 5 against module_add's fbits + 4 layout: every sum
  was wrong (1 + 0.5 = 1.75). A zero operand was read as 1.0 * 2^0.
- IEEE special values throughout: x / 0 is +-inf (was 0), 0 / 0, inf - inf and
  0 * inf are NaN, NaN propagates through + - * /; an infinity keeps its sign
  (every inf was negative, +inf printed as -inf); -0.0 keeps its sign; negation,
  abs and round_to keep NaN; to_double() of an infinity or NaN (was 1.0 * 2^scale);
  ==, <, <=, >= treat NaN as unordered and +0 == -0.
- round_to OR-ed the dropped bits into the last kept bit (jamming); it now rounds
  to nearest, ties to even, with the carry into the scale, and round_to<0> rounds a
  tie up (the hidden bit is odd).
- the quotient had no sticky bit: divide_with_fraction now reports a remainder and
  module_divide jams it below the guard bits, so x / y is correctly rounded.
- integer assignment: negative values through their unsigned magnitude (LLONG_MIN
  was UB), unsigned types through unsigned long long, plain char by its value;
  to_int/to_long/to_long_long are exact from the bits (they went through float).

bitblock
- round<>() did not compile (no unsigned long long constructor) and went through
  to_ullong(), which throws past 64 bits: rewritten bit-wise.
- to_hex() emitted one hexit too many for a multiple of 4 bits, one short ot... (continued)

647 of 769 new or added lines in 12 files covered. (84.14%)

13 existing lines in 3 files now uncovered.

62855 of 70587 relevant lines covered (89.05%)

10983199.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.28
/internal/blockdecimal/api/api.cpp
1
// api.cpp: blockdecimal construction, arithmetic, shifts and conversion across the encodings
2
//
3
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
4
// SPDX-License-Identifier: MIT
5
//
6
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
7
//
8
// This suite was an empty shell. The QA sweep of #2029 (tracking issue #2053) found that the BID
9
// encoding kept a value modulo 2^nbits rather than modulo 10^ndigits (12345 into 3 digits read
10
// back 57, where BCD and DPD keep 345), that <<= INT_MIN and >>= INT_MIN called each other without
11
// end, and that to_double() of more than 308 digits was NaN. Arithmetic beyond division had no
12
// test at all.
13
#include <universal/utility/directives.hpp>
14
#include <climits>
15
#include <cmath>
16
#include <iostream>
17
#include <limits>
18
#include <string>
19
#include <universal/internal/blockdecimal/blockdecimal.hpp>
20
#include <universal/verification/test_suite.hpp>
21

22
namespace {
23

24
using namespace sw::universal;
25

26
// + - * / % of 4-digit decimals against native arithmetic, modulo 10^4 as a 4-digit register wraps
27
template<DecimalEncoding E>
28
int VerifyArithmetic(bool reportTestCases) {
3 ✔
29
        using D = blockdecimal<4, E>;
30
        constexpr long long modulus = 10000;
3 ✔
31
        auto wrap = [](long long v) {   // keep the low 4 decimal digits of the magnitude, and the sign
1,521 ✔
32
                const long long m = (v < 0 ? -v : v) % modulus;
1,521 ✔
33
                return v < 0 ? -m : m;
1,521 ✔
34
        };
35
        int nrOfFailedTests = 0;
3 ✔
36
        const long long values[] = { 0, 1, -1, 7, -9, 42, 99, -100, 999, 1234, -4321, 9999, -9999 };
3 ✔
37
        for (long long x : values) {
42 ✔
38
                for (long long y : values) {
546 ✔
39
                        const D a(x), b(y);
507 ✔
40
                        long long got[5], expected[5];
41
                        got[0] = (a + b).to_long_long();  expected[0] = wrap(x + y);
507 ✔
42
                        got[1] = (a - b).to_long_long();  expected[1] = wrap(x - y);
507 ✔
43
                        got[2] = (a * b).to_long_long();  expected[2] = wrap(x * y);
507 ✔
44
                        const int n = (y != 0) ? 5 : 3;
507 ✔
45
                        if (y != 0) {
507 ✔
46
                                got[3] = (a / b).to_long_long();  expected[3] = x / y;
468 ✔
47
                                got[4] = (a % b).to_long_long();  expected[4] = x % y;
468 ✔
48
                        }
49
                        const char* ops = "+-*/%";
507 ✔
50
                        for (int k = 0; k < n; ++k) {
2,964 ✔
51
                                if (got[k] != expected[k]) {
2,457 ✔
NEW
52
                                        ++nrOfFailedTests;
×
NEW
53
                                        if (reportTestCases && nrOfFailedTests < 10) std::cerr << "FAIL: blockdecimal<4> " << x << ' ' << ops[k] << ' ' << y << " = " << got[k] << " expected " << expected[k] << '\n';
×
54
                                }
55
                        }
56
                }
57
        }
58
        return nrOfFailedTests;
3 ✔
59
}
60

61
// a value past ndigits keeps its low digits, in every encoding
62
int VerifyWrapAcrossEncodings(bool reportTestCases) {
1 ✔
63
        int nrOfFailedTests = 0;
1 ✔
64
        const long long bcd = blockdecimal<3, DecimalEncoding::BCD>(12345).to_long_long();
1 ✔
65
        const long long bid = blockdecimal<3, DecimalEncoding::BID>(12345).to_long_long();
1 ✔
66
        const long long dpd = blockdecimal<3, DecimalEncoding::DPD>(12345).to_long_long();
1 ✔
67
        if (bcd != 345 || bid != 345 || dpd != 345) {
68
                ++nrOfFailedTests;
69
                if (reportTestCases) std::cerr << "FAIL: 12345 into 3 digits: BCD " << bcd << " BID " << bid << " DPD " << dpd << " expected 345\n";
70
        }
71
        const long long bid_neg = blockdecimal<3, DecimalEncoding::BID>(-12345).to_long_long();
1 ✔
72
        if (bid_neg != -345) {
73
                ++nrOfFailedTests;
74
                if (reportTestCases) std::cerr << "FAIL: -12345 into 3 BID digits = " << bid_neg << '\n';
75
        }
76
        return nrOfFailedTests;
1 ✔
77
}
78

79
template<DecimalEncoding E>
80
int VerifyShifts(bool reportTestCases) {
2 ✔
81
        using D = blockdecimal<8, E>;
82
        int nrOfFailedTests = 0;
2 ✔
83
        auto check = [&](long long got, long long expected, const char* label) {
30 ✔
84
                if (got != expected) {
14 ✔
NEW
85
                        ++nrOfFailedTests;
×
NEW
86
                        if (reportTestCases) std::cerr << "FAIL: blockdecimal<8> " << label << " = " << got << " expected " << expected << '\n';
×
87
                }
88
        };
89
        D a(1234);
2 ✔
90
        a <<= 2;  check(a.to_long_long(), 123400, "1234 << 2");
2 ✔
91
        a >>= 3;  check(a.to_long_long(), 123, "123400 >> 3");
2 ✔
92
        a <<= -1; check(a.to_long_long(), 12, "123 << -1");
2 ✔
93
        D b(1234); b <<= 8;  check(b.to_long_long(), 0, "1234 << 8");
2 ✔
94
        D c(1234); c <<= INT_MIN; check(c.to_long_long(), 0, "1234 << INT_MIN");
2 ✔
95
        D d(-1234); d >>= INT_MIN; check(d.to_long_long(), 0, "-1234 >> INT_MIN");
2 ✔
96
        D e(1234); e <<= INT_MAX; check(e.to_long_long(), 0, "1234 << INT_MAX");
2 ✔
97
        return nrOfFailedTests;
2 ✔
98
}
99

100
int VerifyToDouble(bool reportTestCases) {
1 ✔
101
        int nrOfFailedTests = 0;
1 ✔
102
        blockdecimal<400> big;
103
        big.maxval();   // 400 nines, past double's range
1 ✔
104
        const double d = big.to_double();
1 ✔
105
        if (!std::isinf(d) || d < 0) {
1 ✔
NEW
106
                ++nrOfFailedTests;
×
NEW
107
                if (reportTestCases) std::cerr << "FAIL: to_double of 400 nines = " << d << " expected +inf\n";
×
108
        }
109
        blockdecimal<400> small(123456789);   // leading zero digits past 10^308
1 ✔
110
        if (small.to_double() != 123456789.0) {
1 ✔
NEW
111
                ++nrOfFailedTests;
×
NEW
112
                if (reportTestCases) std::cerr << "FAIL: to_double of a 400-digit 123456789 = " << small.to_double() << '\n';
×
113
        }
114
        blockdecimal<20> n(-987654321);
1 ✔
115
        if (n.to_double() != -987654321.0) {
1 ✔
NEW
116
                ++nrOfFailedTests;
×
NEW
117
                if (reportTestCases) std::cerr << "FAIL: to_double(-987654321) = " << n.to_double() << '\n';
×
118
        }
119
        return nrOfFailedTests;
1 ✔
120
}
121

122
} // namespace
123

124
// Regression testing guards: typically set by the cmake configuration, but MANUAL_TESTING is an override
125
#define MANUAL_TESTING 0
126
// REGRESSION_LEVEL_OVERRIDE is set by the cmake file to drive a specific regression intensity
127
// It is the responsibility of the regression test to organize the tests in a quartile progression.
128
//#undef REGRESSION_LEVEL_OVERRIDE
129
#ifndef REGRESSION_LEVEL_OVERRIDE
130
#undef REGRESSION_LEVEL_1
131
#undef REGRESSION_LEVEL_2
132
#undef REGRESSION_LEVEL_3
133
#undef REGRESSION_LEVEL_4
134
#define REGRESSION_LEVEL_1 1
135
#define REGRESSION_LEVEL_2 0
136
#define REGRESSION_LEVEL_3 0
137
#define REGRESSION_LEVEL_4 0
138
#endif
139

140
int main()
1 ✔
141
try {
142
        using namespace sw::universal;
143

144
        std::string test_suite  = "blockdecimal API validation";
2 ✔
145
        std::string test_tag    = "API";
1 ✔
146
        bool reportTestCases    = false;
1 ✔
147
        int nrOfFailedTestCases = 0;
1 ✔
148

149
        ReportTestSuiteHeader(test_suite, reportTestCases);
1 ✔
150

151
#if MANUAL_TESTING
152
        nrOfFailedTestCases += VerifyArithmetic<DecimalEncoding::BCD>(true);
153

154
        ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
155
        return EXIT_SUCCESS;   // ignore failures
156
#else
157

158
#if REGRESSION_LEVEL_1
159
        nrOfFailedTestCases += ReportTestResult(VerifyArithmetic<DecimalEncoding::BCD>(reportTestCases), "blockdecimal<4,BCD>", "+ - * / %");
4 ✔
160
        nrOfFailedTestCases += ReportTestResult(VerifyArithmetic<DecimalEncoding::BID>(reportTestCases), "blockdecimal<4,BID>", "+ - * / %");
4 ✔
161
        nrOfFailedTestCases += ReportTestResult(VerifyArithmetic<DecimalEncoding::DPD>(reportTestCases), "blockdecimal<4,DPD>", "+ - * / %");
4 ✔
162
        nrOfFailedTestCases += ReportTestResult(VerifyWrapAcrossEncodings(reportTestCases), "blockdecimal<3>", "wrap past ndigits");
4 ✔
163
        nrOfFailedTestCases += ReportTestResult(VerifyShifts<DecimalEncoding::BCD>(reportTestCases), "blockdecimal<8,BCD>", "shifts");
4 ✔
164
        nrOfFailedTestCases += ReportTestResult(VerifyShifts<DecimalEncoding::BID>(reportTestCases), "blockdecimal<8,BID>", "shifts");
4 ✔
165
        nrOfFailedTestCases += ReportTestResult(VerifyToDouble(reportTestCases), "blockdecimal", "to_double");
3 ✔
166
#endif
167

168
#if REGRESSION_LEVEL_2
169
#endif
170

171
#if REGRESSION_LEVEL_3
172
#endif
173

174
#if REGRESSION_LEVEL_4
175
#endif
176

177
        ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
1 ✔
178
        return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
1 ✔
179
#endif  // MANUAL_TESTING
180
}
1 ✔
181
catch (char const* msg) {
×
182
        std::cerr << msg << '\n';
×
183
        return EXIT_FAILURE;
×
184
}
×
185
catch (const std::runtime_error& err) {
×
186
        std::cerr << "Uncaught runtime exception: " << err.what() << std::endl;
×
187
        return EXIT_FAILURE;
×
188
}
×
189
catch (...) {
×
190
        std::cerr << "Caught unknown exception" << '\n';
×
191
        return EXIT_FAILURE;
×
192
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc