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

stillwater-sc / universal / 43483250202

13 Nov 2026 06:30PM UTC coverage: 89.011% (-0.1%) from 89.108%
43483250202

push

github

web-flow
fix(internal): blocktriple, blockbinary, blockfraction and blocksignificand conversion, comparison, division and shift defects (#2055)

* fix(internal): blocktriple, blockbinary, blockfraction and blocksignificand conversion, comparison, division and shift defects

blocktriple
- round() dropped a bit when fbits == srcbits - 1 and placed the significand one bit
  low for fbits >= srcbits (1.5f read back 0.75 for every fbits >= 23; fbits >= 32
  from float was not shifted at all). Rewritten as one round-to-nearest-even of
  srcbits - fbits - 1 bits; wider targets are placed with a significand shift, so
  fbits >= 64 works too. The carry out of 1.11..1 is detected for fbits + 1 == 63,
  where UINT64_MAX used to round into bit 63 and read back -0.
- Subnormal float and double values converted to zero; they are now normalized.
- Integer conversion negated INT_MIN (UB), sent plain char through the unsigned path
  (char(-1) read back -2.2e12) and mis-scaled narrow integers into wide targets.
  The magnitude is taken in the unsigned domain and unsigned char gets its own
  overload.
- A MUL or DIV blocktriple built from a native value now has the operand layout
  mul() and div() consume (radix fbits for MUL; the divshift-aligned radix for DIV,
  as cfloat builds them), and integers are aligned like native values.
- to_native() read the overflow bit of a MUL/DIV/SQRT result as a sign
  (1.5 * 1.5 read back -1.19): only the ADD layout is 2's complement.
- ==, <, <=, >, >= follow IEEE-754: NaN unordered, +0 == -0, -inf < x < +inf
  (+inf < 1 was true, nan == nan was true).

blockbinary
- /= and %= at nbits == limb width cast through the signed native type whatever
  the encoding (Unsigned 200 / 2 = 228, 200 % 7 = 0) and trapped on maxneg / -1.
  Signed maxneg / -1 now wraps to maxneg and maxneg % -1 is 0; longdivision accepts
  Unsigned.
- New BLOCKBINARY_THROW_ARITHMETIC_EXCEPTION guard (default 0) and
  blockbinary_divide_by_zero: x / 0 and x % 0 return 0, or r... (continued)

608 of 721 new or added lines in 12 files covered. (84.33%)

88 existing lines in 8 files now uncovered.

62162 of 69836 relevant lines covered (89.01%)

11234904.44 hits per line

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

81.05
/internal/blockbinary/api/rounding.cpp
1
// rounding.cpp: functional tests for rounding using blockbinary numbers
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
#include <universal/utility/directives.hpp>
8
#include <iostream>
9
#include <sstream>
10

11
// Configure the blockbinary template environment
12
// first: enable general or specialized array configurations
13
#define BLOCKBIN_FAST_SPECIALIZATION
14
// second: enable/disable arithmetic exceptions
15
#define BLOCKBIN_THROW_ARITHMETIC_EXCEPTION 0
16

17
#include <universal/internal/blockbinary/blockbinary.hpp>
18
#include <universal/verification/test_suite.hpp>
19
#include <universal/verification/blockbinary_test_status.hpp>
20

21
// report helper to interpret rounding decision encoding
22
std::string roundingDecision(int roundingDirection) {
×
23
        std::stringstream ss;
×
24
        ss << (roundingDirection == 0 ? "tie" : (roundingDirection > 0 ? "up" : "down"));
×
25
        return ss.str();
×
26
}
×
27

UNCOV
28
int ValidateAny(bool reportTestCases) {
×
UNCOV
29
        int nrFailures = 0;
×
30
        sw::universal::blockbinary<18> a;
31

UNCOV
32
        a.setbits(0x32000); // 11'0010'0000'0000'0000
×
UNCOV
33
        if (a.any(8)) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
34
        if (a.any(9)) {        ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
35
        if (a.any(10)) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
36
        if (a.any(11)) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
37
        if (a.any(12)) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
38
        if (a.any(13) != true) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
39
        if (a.any(14) != true) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
40
        if (a.any(16) != true) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
41

UNCOV
42
        a.setbits(0x3244); // 00'0011'0010'0100'0100
×
UNCOV
43
        if (a.any(1)) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
44
        if (a.any(4) != true) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
45
        a.setbits(0x3240); // 00'0011'0010'0100'0000
×
UNCOV
46
        if (a.any(5)) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
47
        if (a.any(6) != true) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
UNCOV
48
        if (a.any(7) != true) { ++nrFailures; if (reportTestCases) std::cout << "fail\n"; }
×
49

UNCOV
50
        return nrFailures;
×
51
}
52

53
int ValidateSpecialRoundingCases(bool reportTestCases) {
1 ✔
54
        using namespace sw::universal;
55

56
        blockbinary<8> a, b, roundedResult;
57
        blockbinary<16> c;
58
        bool roundUp;
59

60
        int nrOfFailedTestCases = 0;
1 ✔
61

62
        // test cases at the boundary
63
//                   | lsb
64
//               '0001     round down                         1 -> 1
65
//                '0010    round down                         1 -> 1
66
//                 '0010   tie with lsb == 0, round down      0 -> 0
67
//                 '0100   round down                         1 -> 1
68
//                 '0101   round down                         1 -> 1
69
//                 '0110   tie with lsb == 1, round up        1 -> 2
70
        a = 1;
1 ✔
71
        roundUp = a.roundingMode(0);
1 ✔
72
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
73
        a = 2;
1 ✔
74
        roundUp = a.roundingMode(1);
1 ✔
75
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
76
        a = 2;
1 ✔
77
        roundUp = a.roundingMode(2);
1 ✔
78
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
79
        a = 4;
1 ✔
80
        roundUp = a.roundingMode(2);
1 ✔
81
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
82
        a = 5;
1 ✔
83
        roundUp = a.roundingMode(2);
1 ✔
84
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
85
        a = 6;
1 ✔
86
        roundUp = a.roundingMode(2);
1 ✔
87
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
88

89
        // test cases at the boundary
90
        //                  | lsb
91
        //                 '0001   round down                         0 -> 0
92
        //                 '0010   round down                         0 -> 0
93
        //                 '0011   round down                         0 -> 0
94
        //                 '0100   tie with lsb == 0, round down      0 -> 0
95
        //                 '0101   round up                           0 -> 0
96
        //                 '0110   round up                           0 -> 1
97
        //                 '0111   round up                           0 -> 1
98
        //                 '1100   tie with lsb == 1, round up        1 -> 2
99
        a = 1;
1 ✔
100
        roundUp = a.roundingMode(3);
1 ✔
101
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
102
        a = 2;
1 ✔
103
        roundUp = a.roundingMode(3);
1 ✔
104
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
105
        a = 3;
1 ✔
106
        roundUp = a.roundingMode(3);
1 ✔
107
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
108
        a = 4;
1 ✔
109
        roundUp = a.roundingMode(3);
1 ✔
110
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
111
        a = 5;
1 ✔
112
        roundUp = a.roundingMode(3);
1 ✔
113
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
114
        a = 6;
1 ✔
115
        roundUp = a.roundingMode(3);
1 ✔
116
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
117
        a = 7;
1 ✔
118
        roundUp = a.roundingMode(3);
1 ✔
119
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
120
        a = 12;
1 ✔
121
        roundUp = a.roundingMode(3);
1 ✔
122
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
123

124
        std::cout << "First Nibble: " << (nrOfFailedTestCases > 0 ? "FAIL" : "PASS") << '\n';
1 ✔
125

126
        // test cases at the boundary
127
        //             | lsb
128
        //             1000'0000   round down                         1 -> 1
129
        //             0100'0000   tie with lsb == 0, round down      0 -> 0
130
        //             1100'0000   tie with lsb == 1, round up        1 -> 2
131
        //             0100'0001   round up                           0 -> 1
132
        //             0110'0000   round up                           0 -> 1
133
        //             0110'0001   round up                           0 -> 1
134

135
        a = 0x80;
1 ✔
136
        roundUp = a.roundingMode(7);
1 ✔
137
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
138
        a = 0x40;
1 ✔
139
        roundUp = a.roundingMode(7);
1 ✔
140
        if (roundUp) ++nrOfFailedTestCases;
1 ✔
141
        a = 0xC0;
1 ✔
142
        roundUp = a.roundingMode(7);
1 ✔
143
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
144
        a = 0x41;
1 ✔
145
        roundUp = a.roundingMode(7);
1 ✔
146
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
147
        a = 0x60;
1 ✔
148
        roundUp = a.roundingMode(7);
1 ✔
149
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
150
        a = 0x61;
1 ✔
151
        roundUp = a.roundingMode(7);
1 ✔
152
        if (!roundUp) ++nrOfFailedTestCases;
1 ✔
153

154
        std::cout << "Second Nibble: " << (nrOfFailedTestCases > 0 ? "FAIL" : "PASS") << '\n';
1 ✔
155

156

157
        // test case  16 bits with lsb at 9
158
        //           | lsb
159
        //   0000'0000'0000'0000   round down                         0 -> 0
160
        //   0000'0000'1000'0000   tie with lsb == 0, round down    128 -> 0
161
        //   0000'0001'1000'0000   tie with lsb == 1, round up      384 -> 2
162
        //   0000'0000'1000'0001   round up                         129 -> 1
163
        //   0000'0000'1100'0000   round up                         192 -> 1
164
        //   0000'0000'1100'0001   round up                         193 -> 1
165
        //   0000'0000'0100'0001   round down                        65 -> 0
166

167
        // result is 128 -> rounds to 0
168
        a = 64;
1 ✔
169
        b = 2;
1 ✔
170
        c = urmul(a, b);
1 ✔
171
        if (reportTestCases) std::cout << "unrounded result is " << to_hex(c, true) << '\n';
1 ✔
172
        roundUp = c.roundingMode(8);
1 ✔
173
        if (reportTestCases) std::cout << (roundUp ? "round up" : "round down") << '\n';
1 ✔
174
        c >>= 8;
1 ✔
175
        roundedResult = c;
1 ✔
176
        if (reportTestCases) std::cout << "shifted unrounded result: " << to_hex(c) << " result in orginal system: " << to_hex(roundedResult) << '\n';
1 ✔
177
        if (roundUp) roundedResult += 1;
1 ✔
178
        if (reportTestCases) std::cout << "final rounded result: " << to_hex(roundedResult) << '\n';
1 ✔
179
        if (reportTestCases) std::cout << std::endl;
1 ✔
180
        if (roundedResult != blockbinary<8>(0ll)) ++nrOfFailedTestCases;
1 ✔
181

182
        // result is 384 -> rounds to 2
183
        a = 96;
1 ✔
184
        b = 4;
1 ✔
185
        c = urmul(a, b);
1 ✔
186
        if (reportTestCases) std::cout << "unrounded result is " << to_hex(c, true) << '\n';
1 ✔
187
        roundUp = c.roundingMode(8);
1 ✔
188
        if (reportTestCases) std::cout << (roundUp ? "round up" : "round down") << '\n';
1 ✔
189
        c >>= 8;
1 ✔
190
        roundedResult = c;
1 ✔
191
        if (reportTestCases) std::cout << "shifted unrounded result: " << to_hex(c) << " result in orginal system: " << to_hex(roundedResult) << '\n';
1 ✔
192
        if (roundUp) roundedResult += 1;
1 ✔
193
        if (reportTestCases) std::cout << "final rounded result: " << to_hex(roundedResult) << '\n';
1 ✔
194
        if (reportTestCases) std::cout << std::endl;
1 ✔
195
        if (roundedResult != blockbinary<8>(2ll)) ++nrOfFailedTestCases;
1 ✔
196

197
        // result is 129 -> rounds to 1
198
        a = 64;
1 ✔
199
        b = 2;
1 ✔
200
        c = urmul(a, b); c += 1;
1 ✔
201
        if (reportTestCases) std::cout << "unrounded result is " << to_hex(c, true) << '\n';
1 ✔
202
        roundUp = c.roundingMode(8);
1 ✔
203
        if (reportTestCases) std::cout << (roundUp ? "round up" : "round down") << '\n';
1 ✔
204
        c >>= 8;
1 ✔
205
        roundedResult = c;
1 ✔
206
        if (reportTestCases) std::cout << "shifted unrounded result: " << to_hex(c) << " result in orginal system: " << to_hex(roundedResult) << '\n';
1 ✔
207
        if (roundUp) roundedResult += 1;
1 ✔
208
        if (reportTestCases) std::cout << "final rounded result: " << to_hex(roundedResult) << '\n';
1 ✔
209
        if (reportTestCases) std::cout << std::endl;
1 ✔
210
        if (roundedResult != blockbinary<8>(1ll)) ++nrOfFailedTestCases;
1 ✔
211

212
        // result is 192 -> rounds to 1
213
        a = 96;
1 ✔
214
        b = 2;
1 ✔
215
        c = urmul(a, b);
1 ✔
216
        if (reportTestCases) std::cout << "unrounded result is " << to_hex(c, true) << '\n';
1 ✔
217
        roundUp = c.roundingMode(8);
1 ✔
218
        if (reportTestCases) std::cout << (roundUp ? "round up" : "round down") << '\n';
1 ✔
219
        c >>= 8;
1 ✔
220
        roundedResult = c;
1 ✔
221
        if (reportTestCases) std::cout << "shifted unrounded result: " << to_hex(c) << " result in orginal system: " << to_hex(roundedResult) << '\n';
1 ✔
222
        if (roundUp) roundedResult += 1;
1 ✔
223
        if (reportTestCases) std::cout << "final rounded result: " << to_hex(roundedResult) << '\n';
1 ✔
224
        if (reportTestCases) std::cout << std::endl;
1 ✔
225
        if (roundedResult != blockbinary<8>(1ll)) ++nrOfFailedTestCases;
1 ✔
226

227
        // result is 193 -> rounds to 1
228
        a = 96;
1 ✔
229
        b = 2;
1 ✔
230
        c = urmul(a, b); c += 1;
1 ✔
231
        if (reportTestCases) std::cout << "unrounded result is " << to_hex(c, true) << '\n';
1 ✔
232
        roundUp = c.roundingMode(8);
1 ✔
233
        if (reportTestCases) std::cout << (roundUp ? "round up" : "round down") << '\n';
1 ✔
234
        c >>= 8;
1 ✔
235
        roundedResult = c;
1 ✔
236
        if (reportTestCases) std::cout << "shifted unrounded result: " << to_hex(c) << " result in orginal system: " << to_hex(roundedResult) << '\n';
1 ✔
237
        if (roundUp) roundedResult += 1;
1 ✔
238
        if (reportTestCases) std::cout << "final rounded result: " << to_hex(roundedResult) << '\n';
1 ✔
239
        if (reportTestCases) std::cout << std::endl;
1 ✔
240
        if (roundedResult != blockbinary<8>(1ll)) ++nrOfFailedTestCases;
1 ✔
241

242
        // result is 65 -> rounds to 0
243
        a = 32;
1 ✔
244
        b = 2;
1 ✔
245
        c = urmul(a, b); c += 1;
1 ✔
246
        if (reportTestCases) std::cout << "unrounded result is " << to_hex(c, true) << '\n';
1 ✔
247
        roundUp = c.roundingMode(8);
1 ✔
248
        if (reportTestCases) std::cout << (roundUp ? "round up" : "round down") << '\n';
1 ✔
249
        c >>= 8;
1 ✔
250
        roundedResult = c;
1 ✔
251
        if (reportTestCases) std::cout << "shifted unrounded result: " << to_hex(c) << " result in orginal system: " << to_hex(roundedResult) << '\n';
1 ✔
252
        if (roundUp) roundedResult += 1;
1 ✔
253
        if (reportTestCases) std::cout << "final rounded result: " << to_hex(roundedResult) << '\n';
1 ✔
254
        if (reportTestCases) std::cout << std::endl;
1 ✔
255
        if (roundedResult != blockbinary<8>(0ll)) ++nrOfFailedTestCases;
1 ✔
256

257
        std::cout << "Second Byte: " << (nrOfFailedTestCases > 0 ? "FAIL" : "PASS") << '\n';
1 ✔
258

259
        return nrOfFailedTestCases;
1 ✔
260
}
261

262
template<size_t nbits>
263
int ValidateRounding(bool reportTestCases) {
264
        int nrTestFailures = 0;
265

266
        return nrTestFailures;
267
}
268

269
// Regression testing guards: typically set by the cmake configuration, but MANUAL_TESTING is an override
270
#define MANUAL_TESTING 0
271
// REGRESSION_LEVEL_OVERRIDE is set by the cmake file to drive a specific regression intensity
272
// It is the responsibility of the regression test to organize the tests in a quartile progression.
273
//#undef REGRESSION_LEVEL_OVERRIDE
274
#ifndef REGRESSION_LEVEL_OVERRIDE
275
#undef REGRESSION_LEVEL_1
276
#undef REGRESSION_LEVEL_2
277
#undef REGRESSION_LEVEL_3
278
#undef REGRESSION_LEVEL_4
279
#define REGRESSION_LEVEL_1 1
280
#define REGRESSION_LEVEL_2 1
281
#define REGRESSION_LEVEL_3 1
282
#define REGRESSION_LEVEL_4 1
283
#endif
284

285
int main()
1 ✔
286
try {
287
        using namespace sw::universal;
288
        
289
        std::string test_suite  = "blockbinary rounding validation";
2 ✔
290
        std::string test_tag    = "rounding";
1 ✔
291
        bool reportTestCases    = true;
1 ✔
292
        int nrOfFailedTestCases = 0;
1 ✔
293

294
        ReportTestSuiteHeader(test_suite, reportTestCases);
1 ✔
295

296
#if MANUAL_TESTING
297

298
        nrOfFailedTestCases += ValidateAny(reportTestCases);
299

300
        // basic algorithm
301
        //  010101...010101010101
302
        //               |  the source arithmetic needs to round at this point
303
        //                | guard bit
304
        //                 | rounding bit
305
        //                  ----- OR'ed to generate the sticky bit
306
        // rounding logic table
307
        //   lsb    guard   round   sticky   rounding decision
308
        //    0       0       x       x      round down
309
        //    0       1       0       0      tie, round to even -> LSB = 0, thus round down
310
        //    1       1       0       0      tie, round to even -> LSB = 1, thus round up
311
        //    x       1       0       1      round up
312
        //    x       1       1       0      round up
313
        //    x       1       1       1      round up
314

315
        //  111111...110101010101
316
        //               |  the source arithmetic needs to round at this point
317
        //                | guard bit
318
        //                 | rounding bit
319
        //                  ----- OR'ed to generate the sticky bit
320
        // rounding logic table
321
        //   lsb    guard   round   sticky   rounding decision
322
        //    0       0       x       x      round down
323
        //    0       1       0       0      tie, round to even -> LSB = 0, thus round down
324
        //    1       1       0       0      tie, round to even -> LSB = 1, thus round up
325
        //    x       1       0       1      round up
326
        //    x       1       1       0      round up
327
        //    x       1       1       1      round up
328

329
        nrOfFailedTestCases = ReportTestResult(ValidateSpecialRoundingCases(reportTestCases), test_tag, "special rounding cases");
330

331
        //nrOfFailedTestCases = ReportTestResult(ValidateRounding<4>(reportTestCases), tag, "urmul");
332
        
333

334
        ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
335
        return EXIT_SUCCESS;
336
#else
337

338
#if REGRESSION_LEVEL_1
339
        nrOfFailedTestCases = ReportTestResult(ValidateSpecialRoundingCases(reportTestCases), test_tag, "special rounding cases");
1 ✔
340

341
#endif
342

343
#if REGRESSION_LEVEL_2
344
#endif
345

346
#if REGRESSION_LEVEL_3
347
#endif
348

349
#if REGRESSION_LEVEL_4
350
#endif
351

352
        ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
1 ✔
353
        return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
1 ✔
354

355
#endif  // MANUAL_TESTING
356
}
1 ✔
357
catch (char const* msg) {
×
358
        std::cerr << msg << std::endl;
×
359
        return EXIT_FAILURE;
×
360
}
×
361
catch (const std::runtime_error& err) {
×
362
        std::cerr << "Uncaught runtime exception: " << err.what() << std::endl;
×
363
        return EXIT_FAILURE;
×
364
}
×
365
catch (...) {
×
366
        std::cerr << "Caught unknown exception" << std::endl;
×
367
        return EXIT_FAILURE;
×
368
}
×
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