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

stillwater-sc / universal / 23093938441

14 Mar 2026 06:19PM UTC coverage: 83.989%. First build
23093938441

Pull #558

github

web-flow
Merge 2dc8dfd7d into 303a244f6
Pull Request #558: fix(quire): generalized quire with normalize(), exact sizing, and CI coverage

356 of 382 new or added lines in 24 files covered. (93.19%)

42899 of 51077 relevant lines covered (83.99%)

6158849.39 hits per line

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

95.38
/include/sw/universal/number/cfloat/cfloat_impl.hpp
1
#pragma once
2
// cfloat_impl.hpp: implementation of an arbitrary configuration fixed-size 'classic' floating-point representation
3
// cfloat<> can emulate IEEE-754 floats and the new Deep Learning types, such as 
4
// IEEE-754 half-precision floats
5
// Google bfloat16
6
// NVIDIA TensorFloat 
7
// AMD FP16 and FP32
8
// Microsoft FP8 and FP9
9
// Tesla CFP8, CFP16
10
// 
11
// cfloat<> can also emulate more precise configurations, such as
12
// 80bit IEEE-754 extended precision floats
13
// true 128bit quad precision floats
14
//
15
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
16
// SPDX-License-Identifier: MIT
17
//
18
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
19

20
// compiler specific environment has been delegated to be handled by the
21
// number system include file <universal/number/cfloat/cfloat.hpp>
22
// 
23
// supporting types and functions
24
#include <limits>
25
#include <type_traits>
26
#include <universal/native/ieee754.hpp>
27
#include <universal/native/subnormal.hpp>
28
#include <universal/utility/find_msb.hpp>
29
#include <universal/number/shared/nan_encoding.hpp>
30
#include <universal/number/shared/infinite_encoding.hpp>
31
#include <universal/number/shared/specific_value_encoding.hpp>
32
// arithmetic tracing options
33
#include <universal/number/algorithm/trace_constants.hpp>
34
// cfloat exception structure
35
#include <universal/number/cfloat/exceptions.hpp>
36
// composition types used by cfloat
37
#include <universal/internal/blockbinary/blockbinary.hpp>
38
#include <universal/internal/blocktriple/blocktriple.hpp>
39
#include <universal/number/support/decimal.hpp>
40

41
#ifndef CFLOAT_THROW_ARITHMETIC_EXCEPTION
42
#define CFLOAT_THROW_ARITHMETIC_EXCEPTION 0
43
#endif
44

45
#ifndef TRACE_CONVERSION
46
#define TRACE_CONVERSION 0
47
#endif
48

49
namespace sw { namespace universal {
50

51
/*
52
 * classic floating-point cfloat offers subnormals, max-exponent values, and
53
 * saturation. The default configuration turns off subnormals, max-exponent
54
 * values, and projects values outside of their dynamic range to +-inf
55
 *
56
 * Behavior flags
57
 *   hasSubnormals   : gradual underflow: use all fraction encodings when exponent is all 0's
58
 *   hasMaxExpValues  : reclaim max-exponent encodings as numeric values instead of
59
 *                      reserving the entire all-1s exponent binade for inf/NaN;
60
 *                      only 4 encodings are reserved for +-inf and quiet/signalling NaN
61
 *   isSaturating     : saturate to maxneg or maxpos when value is out of dynamic range
62
 */
63

64
/// <summary>
65
/// decode an cfloat value into its constituent parts
66
/// </summary>
67
/// <typeparam name="bt">block type</typeparam>
68
/// <param name="v">cfloat value to decode (input: const ref)</param>
69
/// <param name="s">sign (output: bool ref)</param>
70
/// <param name="e">exponent (output: blockbinary ref)</param>
71
/// <param name="f">fraction (output: blockbinary ref)</param>
72
template<unsigned nbits, unsigned es, unsigned fbitsPlus1, typename bt,
73
        bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
74
void decode(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& v, bool& s, blockbinary<es, bt>& e, blockbinary<fbitsPlus1, bt>& f) {
800✔
75
        v.sign(s);
800✔
76
        v.exponent(e);
800✔
77
        v.fraction(f);
800✔
78
}
800✔
79

80
/// <summary>
81
/// return the binary scale of the given number
82
/// </summary>
83
/// <typeparam name="bt">Block type used for storage: derived through ADL</typeparam>
84
/// <param name="v">the cfloat number for which we seek to know the binary scale</param>
85
/// <returns>binary scale, i.e. 2^scale, of the value of the cfloat</returns>
86
template<unsigned nbits, unsigned es, typename bt,
87
        bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
88
int scale(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& v) {
329✔
89
        return v.scale();
329✔
90
}
91

92
/// <summary>
93
/// convert a blocktriple to a cfloat. blocktriples come out of the arithmetic
94
/// engine in the form ii.ff...ff and a scale. The conversion must take this
95
/// denormalized form into account during conversion.
96
/// 
97
/// The blocktriple must be in this form to round correctly, as all the bits
98
/// after an arithmetic operation must be taken into account.
99
/// 
100
/// Transformation:
101
///    ii.ff...ff  transform to    s.eee.fffff
102
/// All number systems that depend on blocktriple will need to have
103
/// the rounding decision answered, so that functionality can be
104
/// reused if we locate it inside blocktriple.
105
/// 
106
/// if (srcbits > fbits) // we need to round
107
///     if (ii.00..00 > 1) 
108
///         mask is at srcbits - fbits + 1
109
///     else 
110
///                    mask is at srcbits - fbits
111
/// }
112
/// else {               // no need to round
113
/// }
114
/// </summary>
115
/// <typeparam name="bt">type of the block used for cfloat storage</typeparam>
116
/// <param name="src">the blocktriple to be converted</param>
117
/// <param name="tgt">the resulting cfloat</param>
118
template<unsigned srcbits, BlockTripleOperator op, unsigned nbits, unsigned es, typename bt,
119
        bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
120
inline /*constexpr*/ void convert(const blocktriple<srcbits, op, bt>& src, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& tgt) {
71,332,934✔
121
        using btType = blocktriple<srcbits, op, bt>;
122
        using cfloatType = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
123
        // test special cases
124
        if (src.isnan()) {
71,332,934✔
125
                tgt.setnan(src.sign() ? NAN_TYPE_SIGNALLING : NAN_TYPE_QUIET);
388✔
126
        }
127
        else        if (src.isinf()) {
71,332,546✔
128
                tgt.setinf(src.sign());
388✔
129
        }
130
        else         if (src.iszero()) {
71,332,158✔
131
                tgt.setzero();
57,149✔
132
                tgt.setsign(src.sign()); // preserve sign
57,149✔
133
        }
134
        else {
135
                int significandScale = src.significandscale();
71,275,009✔
136
                int exponent = src.scale() + significandScale;
71,275,009✔
137
                // special case of underflow
138
                if constexpr (hasSubnormals) {
139
//                        std::cout << "exponent = " << exponent << " bias = " << cfloatType::EXP_BIAS << " exp subnormal = " << cfloatType::MIN_EXP_SUBNORMAL << '\n';
140
                        // why must exponent be less than (minExpSubnormal - 1) to be rounded to zero? 
141
                        // because the half-way value that would round up to minpos is at exp = (minExpSubnormal - 1)
142
                        if (exponent < cfloatType::MIN_EXP_SUBNORMAL) {
54,638,534✔
143
                                tgt.setzero();
192,358✔
144
                                if (exponent == (cfloatType::MIN_EXP_SUBNORMAL - 1)) {
192,358✔
145
                                        // -exponent because we are right shifting and exponent in this range is negative
146
                                        int adjustment = -(exponent + subnormal_reciprocal_shift[es]);
42,800✔
147
                                        std::pair<bool, unsigned> alignment = src.roundingDecision(adjustment);
42,800✔
148
                                        if (alignment.first) ++tgt; // we are minpos
42,800✔
149
                                }
150
                                tgt.setsign(src.sign());
192,358✔
151
                                return;
3,127,107✔
152
                        }
153
                }
154
                else {
155
                        if (exponent + cfloatType::EXP_BIAS <= 0) {  // value is in the subnormal range, which maps to 0
16,636,475✔
156
                                tgt.setzero();
362,510✔
157
                                tgt.setsign(src.sign());
362,510✔
158
                                return;
638,776✔
159
                        }
160
                }
161
                // special case of overflow
162
                if constexpr (hasMaxExpValues) {
163
                        if constexpr (isSaturating) {
164
                                if (exponent > cfloatType::MAX_EXP) {
17,743,113✔
165
                                        if (src.sign()) tgt.maxneg(); else tgt.maxpos();
634,020✔
166
                                        return;
634,020✔
167
                                }
168
                        }
169
                        else {
170
                                if (exponent > cfloatType::MAX_EXP) {
27,200,475✔
171
                                        tgt.setinf(src.sign());
2,373,142✔
172
                                        return;
2,373,142✔
173
                                }
174
                        }
175
                }
176
                else {  // no max-exponent values: will saturate at a different encoding: TODO can we hide it all in maxpos?
177
                        if constexpr (isSaturating) {
178
                                if (exponent > cfloatType::MAX_EXP) {
3,707✔
179
                                        if (src.sign()) tgt.maxneg(); else tgt.maxpos();
6✔
180
                                        return;
6✔
181
                                }
182
                        }
183
                        else {
184
                                if (exponent > cfloatType::MAX_EXP) {
25,772,846✔
185
                                        tgt.setinf(src.sign());
203,847✔
186
                                        return;
203,847✔
187
                                }
188
                        }
189
                }
190

191
                // our value needs to go through rounding to be correctly interpreted
192
                // 
193
                // tgt.clear();  // no need as all bits are going to be set by the code below
194

195
                // exponent construction
196
                int adjustment{ 0 };
67,509,126✔
197
                // construct exponent
198
                uint64_t biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(cfloatType::EXP_BIAS)); // this is guaranteed to be positive if exponent in encoding range
67,509,126✔
199
//                        std::cout << "exponent         " << to_binary(biasedExponent) << '\n';        
200
                if constexpr (hasSubnormals) {
201
                        //if (exponent >= cfloatType::MIN_EXP_SUBNORMAL && exponent < cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::MIN_EXP_NORMAL) {
202
                        if (exponent < cfloatType::MIN_EXP_NORMAL) {
51,511,427✔
203
                                // the value is in the subnormal range of the cfloat
204
                                biasedExponent = 0;
35,471,778✔
205
                                // -exponent because we are right shifting and exponent in this range is negative
206
                                adjustment = -(exponent + subnormal_reciprocal_shift[es]);
35,471,778✔
207
                                // this is the right shift adjustment required for subnormal representation due 
208
                                // to the scale of the input number, i.e. the exponent of 2^-adjustment
209
                        }
210
                        else {
211
                                // the value is in the normal range of the cfloat
212
                                biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(cfloatType::EXP_BIAS)); // this is guaranteed to be positive
16,039,649✔
213
                        }
214
                }
215
                else {
216
                        if (exponent < cfloatType::MIN_EXP_NORMAL) biasedExponent = 1ull; // fixup biasedExponent if we are in the subnormal region
15,997,699✔
217
                }
218

219

220
                // get the rounding direction and the LSB right shift: 
221
                std::pair<bool, unsigned> alignment = src.roundingDecision(adjustment);
67,509,126✔
222
                unsigned rightShift = alignment.second;  // this is the shift to get the LSB of the src to the LSB of the tgt
67,509,126✔
223
                //std::cout << "rightShift       " << rightShift << '\n';
224

225
                if constexpr (btType::bfbits < 65) {
226
                        bool roundup = alignment.first;
66,977,226✔
227
                        //std::cout << "round-up?        " << (roundup ? "yes" : "no") << '\n';
228
                        // we can use a uint64_t to construct the cfloat
229
                        uint64_t raw = (src.sign() ? 1ull : 0ull); // process sign
66,977,226✔
230
                        //std::cout << "raw bits (sign)  " << to_binary(raw) << '\n';
231
                        // construct the fraction bits
232
                        uint64_t fracbits = src.significand_ull(); // get all the bits, including the integer bits
66,977,226✔
233
                        //std::cout << "fracbits         " << to_binary(fracbits) << '\n';
234
                        fracbits >>= rightShift;
66,977,226✔
235
                        //std::cout << "fracbits shifted " << to_binary(fracbits) << '\n';
236
                        fracbits &= cfloatType::ALL_ONES_FR; // remove the hidden bit
66,977,226✔
237
                        //std::cout << "fracbits masked  " << to_binary(fracbits) << '\n';
238
                        if (roundup) ++fracbits;
66,977,226✔
239
                        if (fracbits == (1ull << cfloatType::fbits)) { // check for overflow
66,977,226✔
240
                                if (biasedExponent == cfloatType::ALL_ONES_ES) {
425,455✔
241
                                        fracbits = cfloatType::INF_ENCODING; // project to INF
6,244✔
242
                                }
243
                                else {
244
                                        ++biasedExponent;
419,211✔
245
                                        fracbits = 0;
419,211✔
246
                                }
247
                        }
248

249
                        raw <<= es; // shift sign to make room for the exponent bits
66,977,226✔
250
                        raw |= biasedExponent;
66,977,226✔
251
                        //std::cout << "raw bits (exp)   " << to_binary(raw) << '\n';
252
                        raw <<= cfloatType::fbits; // make room for the fraction bits
66,977,226✔
253
                        //std::cout << "raw bits (s+exp) " << to_binary(raw) << '\n';
254
                        raw |= fracbits;
66,977,226✔
255
                        //std::cout << "raw bits (final) " << to_binary(raw) << '\n';
256
                        tgt.setbits(raw);
66,977,226✔
257
//                        std::cout << "raw bits (all)   " << to_binary(raw) << '\n';
258
                        if constexpr (isSaturating) {
259
                                if (tgt.isnan()) {
17,112,786✔
260
                                        if (src.sign()) {
550✔
261
                                                tgt.maxneg();        // map back to maxneg
275✔
262
                                        }
263
                                        else {
264
                                                tgt.maxpos();        // map back to maxpos
275✔
265
                                        }
266
                                }
267
                        }
268
                        else {
269
                                // when you get too far, map it back to +-inf: 
270
                                // TBD: this doesn't appear to be the right algorithm to catch all overflow patterns
271
                                if (tgt.isnan()) tgt.setinf(src.sign());        // map back to +-inf
49,864,440✔
272
                        }
273
                }
274
                else {
275
                        // compose the segments
276
                        auto fracbits = src.significand();  // why significand? cheesy optimization: we are going to overwrite the hidden bit position anyway when we write the exponent below, so no need to pay the overhead of generating the fraction here.
531,900✔
277
                        //std::cout << "fraction      : " << to_binary(fracbits, true) << '\n';
278
                        fracbits >>= static_cast<int>(rightShift);
531,900✔
279
                        //std::cout << "aligned fbits : " << to_binary(fracbits, true) << '\n';
280

281
                        // copy the blocks that contain fraction bits
282
                        // significand blocks are organized like this:
283
                        //   ADD        iii.ffffrrrrrrrrr          3 integer bits, f fraction bits, and 2*fhbits rounding bits
284
                        //   MUL         ii.ffff'ffff              2 integer bits, 2*f fraction bits
285
                        //   DIV         ii.ffff'ffff'ffff'rrrr    2 integer bits, 3*f fraction bits, and r rounding bits
286
                        //std::cout << "fraction bits : " << to_binary(fracbits, true) << '\n';
287
                        tgt.clear();
531,900✔
288
                        //std::cout << "initial state : " << to_binary(tgt) << " : " << tgt << '\n';
289
                        for (unsigned b = 0; b < btType::nrBlocks; ++b) {
1,074,316✔
290
                                tgt.setblock(b, fracbits.block(b));
542,416✔
291
                        }
292
                        //std::cout << "fraction bits : " << to_binary(tgt, true) << '\n';
293
                        tgt.setsign(src.sign());
531,900✔
294
                        //std::cout << "adding sign   : " << to_binary(tgt) << '\n';
295
                        if (!tgt.setexponent(exponent)) {
531,900✔
296
                                std::cerr << "exponent value is out of range: " << exponent << '\n';
×
297
                        }
298
                        //std::cout << "add exponent  : " << to_binary(tgt) << '\n';
299
                }
300
        }
301
}
302

303

304
/// <summary>
305
/// An arbitrary, fixed-size floating-point number with configurable gradual under/overflow and saturation/non-saturation arithmetic.
306
/// Default configuration offers normal encoding and non-saturating arithmetic.
307
/// /// </summary>
308
/// <typeparam name="nbits">number of bits in the encoding</typeparam>
309
/// <typeparam name="es">number of exponent bits in the encoding</typeparam>
310
/// <typeparam name="bt">the type to use as storage class: one of [uint8_t|uint16_t|uint32_t]</typeparam>
311
/// <typeparam name="hasSubnormals">configure gradual underflow (==subnormals)</typeparam>
312
/// <typeparam name="hasMaxExpValues">reclaim max-exponent encodings as numeric values</typeparam>
313
/// <typeparam name="isSaturating">configure saturation arithmetic</typeparam>
314
template<unsigned _nbits, unsigned _es, typename bt = uint8_t,
315
        bool _hasSubnormals = false, bool _hasMaxExpValues = false, bool _isSaturating = false>
316
class cfloat {
317
public:
318
        static_assert(_nbits > _es + 1ull, "nbits is too small to accomodate the requested number of exponent bits");
319
        static_assert(_es < 21ull, "my God that is a big number, are you trying to break the Interweb?");
320
        static_assert(_es > 0, "number of exponent bits must be bigger than 0 to be a classic floating point number");
321
        // how do you assert on the condition that if es == 1 then subnormals and max-exponent values must be true?
322
        static constexpr bool     subsuper = (_hasSubnormals && _hasMaxExpValues);
323
        static constexpr bool     special = (subsuper ? true : (_es > 1));
324
        static_assert(special, "when es == 1, cfloat must have both subnormals and max-exponent values");
325
        static constexpr unsigned bitsInByte = 8u;
326
        static constexpr unsigned bitsInBlock = sizeof(bt) * bitsInByte;
327
        static_assert(bitsInBlock <= 64, "storage unit for block arithmetic needs to be <= uint64_t"); // TODO: carry propagation on uint64_t requires assembly code
328

329
        static constexpr unsigned nbits = _nbits;
330
        static constexpr unsigned es = _es;
331
        static constexpr unsigned fbits  = nbits - 1u - es;    // number of fraction bits excluding the hidden bit
332
        static constexpr unsigned fhbits = nbits - es;           // number of fraction bits including the hidden bit
333

334
        static constexpr uint64_t  storageMask = (0xFFFFFFFFFFFFFFFFull >> (64ull - bitsInBlock));
335
        static constexpr bt       BLOCK_MASK = bt(~0);
336
        static constexpr bt       ALL_ONES = bt(~0); // block type specific all 1's value
337
        static constexpr uint32_t ALL_ONES_ES = (0xFFFF'FFFFul >> (32 - es));
338
        static constexpr uint64_t topfbits = fbits % 64;
339
        static constexpr uint64_t FR_SHIFT = (topfbits > 0 ? (64 - topfbits) : 0);
340
        static constexpr uint64_t ALL_ONES_FR = (topfbits > 0 ? (0xFFFF'FFFF'FFFF'FFFFull >> FR_SHIFT) : 0ull); // special case for nbits <= 64
341
        static constexpr uint64_t INF_ENCODING = (ALL_ONES_FR & ~1ull);
342

343
        static constexpr unsigned nrBlocks = 1u + ((nbits - 1ull) / bitsInBlock);
344
        static constexpr unsigned MSU = nrBlocks - 1u; // MSU == Most Significant Unit, as MSB is already taken
345
        static constexpr bt       MSU_MASK = (ALL_ONES >> (nrBlocks * bitsInBlock - nbits));
346
        static constexpr unsigned bitsInMSU = bitsInBlock - (nrBlocks * bitsInBlock - nbits);
347
        static constexpr unsigned fBlocks = 1ull + ((fbits - 1ull) / bitsInBlock); // nr of blocks with fraction bits
348
        static constexpr unsigned FSU = fBlocks - 1u;  // FSU = Fraction Significant Unit: the index of the block that contains the most significant fraction bits
349
        static constexpr bt       FSU_MASK = (ALL_ONES >> (fBlocks * bitsInBlock - fbits));
350
        static constexpr unsigned bitsInFSU = bitsInBlock - (fBlocks * bitsInBlock - fbits);
351

352
        static constexpr bt       SIGN_BIT_MASK = bt(bt(1ull) << ((nbits - 1ull) % bitsInBlock));
353
        static constexpr bt       LSB_BIT_MASK = bt(1ull);
354
        static constexpr bool     MSU_CAPTURES_EXP = (1ull + es) <= bitsInMSU;
355
        static constexpr unsigned EXP_SHIFT = (MSU_CAPTURES_EXP ? (1 == nrBlocks ? (nbits - 1ull - es) : (bitsInMSU - 1ull - es)) : 0);
356
        static constexpr bt       MSU_EXP_MASK = ((ALL_ONES << EXP_SHIFT) & ~SIGN_BIT_MASK) & MSU_MASK;
357
        static constexpr int      EXP_BIAS = ((1 << (es - 1u)) - 1l);
358
        static constexpr int      MAX_EXP = (es == 1) ? 1 : ((1 << es) - EXP_BIAS - 1);
359
        static constexpr int      MIN_EXP_NORMAL = 1 - EXP_BIAS;
360
        static constexpr int      MIN_EXP_SUBNORMAL = 1 - EXP_BIAS - int(fbits); // the scale of smallest ULP
361

362
        static constexpr bool     hasSubnormals   = _hasSubnormals;
363
        static constexpr bool     hasMaxExpValues = _hasMaxExpValues;
364
        static constexpr bool     isSaturating    = _isSaturating;
365
        typedef bt BlockType;
366

367
        // constructors
368
        cfloat() = default;
369
        cfloat(const cfloat&) = default;
370
        cfloat& operator=(const cfloat&) = default;
371

372
        // construct a cfloat from another
373
        template<unsigned nnbits, unsigned ees, typename bbt, bool ssub, bool ssup, bool ssat>
374
        cfloat(const cfloat<nnbits, ees, bbt, ssub, ssup, ssat>& rhs) noexcept : _block{} {
6,104✔
375
                if (rhs.isnan()) {
6,104✔
376
                        setnan(rhs.sign() ? NAN_TYPE_SIGNALLING : NAN_TYPE_QUIET);
79✔
377
                }
378
                else if (rhs.isinf()) {
6,025✔
379
                        setinf(rhs.sign());
9✔
380
                }
381
                else if (rhs.iszero()) {
6,016✔
382
                        setzero();
738✔
383
                }
384
                else {
385
                        // TODO: cfloat from another cfloat: marshall through a proper blocktriple
386
                        /*
387
                        if constexpr (std::is_same_v<bt, bbt>) {
388
                                blocktriple<fbits, BlockTripleOperator::REP, bt> value;
389
                                value.setnormal();
390
                                value.setsign(rhs.sign());
391
                                value.setscale(rhs.scale());
392
                                //constexpr unsigned rhsFbits = nnbits - 1ul - ees;
393
                                //blockbinary<rhsFbits, bbt, BinaryNumberType::Signed> fraction;
394
                                //rhs.fraction<rhsFbits>(fraction);
395
                                //std::cout << "fraction : " << to_binary(fraction) << '\n';
396
                                //value.setfraction(fraction);
397
                                convert(value, *this);
398
                        }
399
                        else {
400
                                static_assert(nnbits < 64, "converting constructor marshalls values through native double precision, and rhs has more bits");
401
                                *this = double(rhs); 
402
                        }
403
                        */
404
                        *this = double(rhs);
5,278✔
405
                }
406
        }
6,104✔
407

408
        // converting constructors
409
        constexpr cfloat(const std::string& stringRep) : _block{} { assign(stringRep); }
1✔
410
        // specific value constructor
411
        constexpr cfloat(const SpecificValue code) noexcept : _block{} {
309✔
412
                switch (code) {
309✔
413
                case SpecificValue::maxpos:
95✔
414
                        maxpos();
95✔
415
                        break;
95✔
416
                case SpecificValue::minpos:
111✔
417
                        minpos();
111✔
418
                        break;
111✔
419
                case SpecificValue::zero:
×
420
                default:
421
                        zero();
×
422
                        break;
×
423
                case SpecificValue::minneg:
×
424
                        minneg();
×
425
                        break;
×
426
                case SpecificValue::maxneg:
48✔
427
                        maxneg();
48✔
428
                        break;
48✔
429
                case SpecificValue::infpos:
21✔
430
                        setinf(false);
21✔
431
                        break;
21✔
432
                case SpecificValue::infneg:
×
433
                        setinf(true);
×
434
                        break;
×
435
                case SpecificValue::nar: // approximation as cfloats don't have a NaR
17✔
436
                case SpecificValue::qnan:
437
                        setnan(NAN_TYPE_QUIET);
17✔
438
                        break;
17✔
439
                case SpecificValue::snan:
17✔
440
                        setnan(NAN_TYPE_SIGNALLING);
17✔
441
                        break;
17✔
442
                }
443
        }
309✔
444

445
        constexpr cfloat(signed char iv)                    noexcept : _block{} { *this = iv; }
446
        constexpr cfloat(short iv)                          noexcept : _block{} { *this = iv; }
447
        constexpr cfloat(int iv)                            noexcept : _block{} { *this = iv; }
114,803✔
448
        constexpr cfloat(long iv)                           noexcept : _block{} { *this = iv; }
449
        constexpr cfloat(long long iv)                      noexcept : _block{} { *this = iv; }
1✔
450
        constexpr cfloat(char iv)                           noexcept : _block{} { *this = iv; }
451
        constexpr cfloat(unsigned short iv)                 noexcept : _block{} { *this = iv; }
452
        constexpr cfloat(unsigned int iv)                   noexcept : _block{} { *this = iv; }
453
        constexpr cfloat(unsigned long iv)                  noexcept : _block{} { *this = iv; }
100✔
454
        constexpr cfloat(unsigned long long iv)             noexcept : _block{} { *this = iv; }
455
        CONSTEXPRESSION cfloat(float iv)                    noexcept : _block{} { *this = iv; }
340,794✔
456
        CONSTEXPRESSION cfloat(double iv)                   noexcept : _block{} { *this = iv; }
506,973✔
457

458
        // assignment operators
459
        constexpr cfloat& operator=(signed char rhs)        noexcept { return convert_signed_integer(rhs); }
460
        constexpr cfloat& operator=(short rhs)              noexcept { return convert_signed_integer(rhs); }
461
        constexpr cfloat& operator=(int rhs)                noexcept { return convert_signed_integer(rhs); }
174,971✔
462
        constexpr cfloat& operator=(long rhs)               noexcept { return convert_signed_integer(rhs); }
463
        constexpr cfloat& operator=(long long rhs)          noexcept { return convert_signed_integer(rhs); }
1✔
464

465
        constexpr cfloat& operator=(char rhs)               noexcept { return convert_unsigned_integer(rhs); }
466
        constexpr cfloat& operator=(unsigned short rhs)     noexcept { return convert_unsigned_integer(rhs); }
467
        constexpr cfloat& operator=(unsigned int rhs)       noexcept { return convert_unsigned_integer(rhs); }
30✔
468
        constexpr cfloat& operator=(unsigned long rhs)      noexcept { return convert_unsigned_integer(rhs); }
110✔
469
        constexpr cfloat& operator=(unsigned long long rhs) noexcept { return convert_unsigned_integer(rhs); }
470

471
        BIT_CAST_CONSTEXPR cfloat& operator=(float rhs)     noexcept { return convert_ieee754(rhs); }
32,488,669✔
472
        BIT_CAST_CONSTEXPR cfloat& operator=(double rhs)    noexcept { return convert_ieee754(rhs); }
99,040,150✔
473

474
        // make conversions to native types explicit
475
        explicit operator int()                       const noexcept { return to_int(); }
59,976✔
476
        explicit operator long()                      const noexcept { return to_long(); }
477
        explicit operator long long()                 const noexcept { return to_long_long(); }
1✔
478
        explicit operator float()                     const noexcept { return to_native<float>(); }
35,177,206✔
479
        explicit operator double()                    const noexcept { return to_native<double>(); }
78,233,383✔
480

481
        // guard long double support to enable ARM and RISC-V embedded environments
482
#if LONG_DOUBLE_SUPPORT
483
        explicit operator long double()               const noexcept { return to_native<long double>(); }
54✔
484
        BIT_CAST_CONSTEXPR cfloat(long double iv)           noexcept : _block{} { *this = iv; }
2✔
485
        BIT_CAST_CONSTEXPR cfloat& operator=(long double rhs)  noexcept { return convert_ieee754(rhs); }
74✔
486
#endif
487

488
        // arithmetic operators
489
        // prefix operator
490
        inline cfloat operator-() const {
8,346,918✔
491
                cfloat tmp(*this);
8,346,918✔
492
                tmp._block[MSU] ^= SIGN_BIT_MASK;
8,346,918✔
493
                return tmp;
8,346,918✔
494
        }
495

496
        cfloat& operator+=(const cfloat& rhs) CFLOAT_EXCEPT {
18,470,966✔
497
                if constexpr (_trace_add) std::cout << "---------------------- ADD -------------------" << std::endl;
498
                // special case handling of the inputs
499
#if CFLOAT_THROW_ARITHMETIC_EXCEPTION
500
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
744✔
501
                        throw cfloat_operand_is_nan{};
×
502
                }
503
#else
504
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
18,470,222✔
505
                        setnan(NAN_TYPE_SIGNALLING);
233,112✔
506
                        return *this;
233,112✔
507
                }
508
                if (isnan(NAN_TYPE_QUIET) || rhs.isnan(NAN_TYPE_QUIET)) {
18,237,110✔
509
                        setnan(NAN_TYPE_QUIET);
213,837✔
510
                        return *this;
213,837✔
511
                }
512
#endif
513
                // normal + inf  = inf
514
                // normal + -inf = -inf
515
                // inf + normal = inf
516
                // inf + inf    = inf
517
                // inf + -inf    = ?
518
                // -inf + normal = -inf
519
                // -inf + -inf   = -inf
520
                // -inf + inf    = ?
521
                if (isinf()) {
18,024,017✔
522
                        if (rhs.isinf()) {
58,855✔
523
                                if (sign() != rhs.sign()) {
783✔
524
                                        setnan(NAN_TYPE_SIGNALLING);
358✔
525
                                }
526
                                return *this;
783✔
527
                        }
528
                        else {
529
                                return *this;
58,072✔
530
                        }
531
                }
532
                else {
533
                        if (rhs.isinf()) {
17,965,162✔
534
                                *this = rhs;
58,075✔
535
                                return *this;
58,075✔
536
                        }
537
                }
538

539
                if (iszero()) {
17,907,087✔
540
                        *this = rhs;
205,858✔
541
                        return *this;
205,858✔
542
                }
543
                if (rhs.iszero()) return *this;
17,701,229✔
544

545
                // arithmetic operation
546
                blocktriple<fbits, BlockTripleOperator::ADD, bt> a, b, sum;
17,370,069✔
547

548
                // transform the inputs into (sign,scale,significant) 
549
                normalizeAddition(a); 
17,370,069✔
550
                rhs.normalizeAddition(b);
17,370,069✔
551
                sum.add(a, b);
17,370,069✔
552

553
                convert(sum, *this);
17,370,069✔
554

555
                return *this;
17,370,069✔
556
        }
557
        cfloat& operator+=(double rhs) CFLOAT_EXCEPT {
558
                return *this += cfloat(rhs);
559
        }
560
        cfloat& operator-=(const cfloat& rhs) CFLOAT_EXCEPT {
8,454,808✔
561
                if constexpr (_trace_sub) std::cout << "---------------------- SUB -------------------" << std::endl;
562
                if (rhs.isnan()) 
8,454,808✔
563
                        return *this += rhs;
163,649✔
564
                else 
565
                        return *this += -rhs;
8,291,159✔
566
        }
567
        cfloat& operator-=(double rhs) CFLOAT_EXCEPT {
8✔
568
                return *this -= cfloat(rhs);
8✔
569
        }
570
        cfloat& operator*=(const cfloat& rhs) CFLOAT_EXCEPT {
4,298,336✔
571
                if constexpr (_trace_mul) std::cout << "---------------------- MUL -------------------\n";
322✔
572
                // special case handling of the inputs
573
#if CFLOAT_THROW_ARITHMETIC_EXCEPTION
574
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
612✔
575
                        throw cfloat_operand_is_nan{};
×
576
                }
577
#else
578
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
4,297,724✔
579
                        setnan(NAN_TYPE_SIGNALLING);
121,230✔
580
                        return *this;
121,230✔
581
                }
582
                if (isnan(NAN_TYPE_QUIET) || rhs.isnan(NAN_TYPE_QUIET)) {
4,176,494✔
583
                        setnan(NAN_TYPE_QUIET);
111,602✔
584
                        return *this;
111,602✔
585
                }
586
#endif
587
                //  inf * inf = inf
588
                //  inf * -inf = -inf
589
                // -inf * inf = -inf
590
                // -inf * -inf = inf
591
                //        0 * inf = -nan(ind)
592
                //        inf * 0 = -nan(ind)
593
                bool resultSign = sign() != rhs.sign();
4,065,504✔
594
                if (isinf()) {
4,065,504✔
595
                        if (rhs.iszero()) {
24,458✔
596
                                setnan(NAN_TYPE_QUIET);
1,591✔
597
                        }
598
                        else {
599
                                setsign(resultSign);
22,867✔
600
                        }
601
                        return *this;
24,458✔
602
                }
603
                if (rhs.isinf()) {
4,041,046✔
604
                        if (iszero()) {
24,067✔
605
                                setnan(NAN_TYPE_QUIET);
1,595✔
606
                        }
607
                        else {
608
                                setinf(resultSign);
22,472✔
609
                        }
610
                        return *this;
24,067✔
611
                }
612

613
                if (iszero() || rhs.iszero()) {                        
4,016,979✔
614
                        setzero();
1,873,982✔
615
                        setsign(resultSign); // deal with negative 0
1,873,982✔
616
                        return *this;
1,873,982✔
617
                }
618

619
                // arithmetic operation
620
                blocktriple<fbits, BlockTripleOperator::MUL, bt> a, b, product;
2,142,997✔
621

622
                // transform the inputs into (sign,scale,significant) 
623
                // triples of the correct width
624
                normalizeMultiplication(a);
2,142,997✔
625
                rhs.normalizeMultiplication(b);
2,142,997✔
626
                product.mul(a, b);
2,142,997✔
627
                convert(product, *this);
2,142,997✔
628

629
                if constexpr (_trace_mul) std::cout << to_binary(a) << " : " << a << " *\n" << to_binary(b) << " : " << b << " =\n" << to_binary(product) << " : " << product << '\n';
106✔
630

631
                return *this;
2,142,997✔
632
        }
633
        cfloat& operator*=(double rhs) CFLOAT_EXCEPT {
40✔
634
                return *this *= cfloat(rhs);
40✔
635
        }
636
        cfloat& operator/=(const cfloat& rhs) CFLOAT_EXCEPT {
5,135,689✔
637
                if constexpr (_trace_div) std::cout << "---------------------- DIV -------------------" << std::endl;
638

639
                // special case handling of the inputs
640
                // qnan / qnan = qnan
641
                // qnan / snan = qnan
642
                // snan / qnan = snan
643
                // snan / snan = snan
644
#if CFLOAT_THROW_ARITHMETIC_EXCEPTION
645
                if (rhs.iszero()) throw cfloat_divide_by_zero();
49✔
646
                if (rhs.isnan()) throw cfloat_divide_by_nan();
48✔
647
                if (isnan()) throw cfloat_operand_is_nan();
48✔
648
#else
649
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
5,135,640✔
650
                        setnan(NAN_TYPE_SIGNALLING);
163,870✔
651
                        return *this;
163,870✔
652
                }
653
                if (isnan(NAN_TYPE_QUIET) || rhs.isnan(NAN_TYPE_QUIET)) {
4,971,770✔
654
                        setnan(NAN_TYPE_QUIET);
150,950✔
655
                        return *this;
150,950✔
656
                }
657
                if (rhs.iszero()) {
4,820,820✔
658
                        if (iszero()) {
169,760✔
659
                                // zero divide by zero yields quiet NaN (in MSVC it is labeled -nan(ind) for indeterminate)
660
                                setnan(NAN_TYPE_QUIET);
29,299✔
661
                        }
662
                        else {
663
                                // non-zero divide by zero yields INF
664
                                bool resultSign = sign() != rhs.sign();
140,461✔
665
                                setinf(resultSign);
140,461✔
666
                        }
667
                        return *this;
169,760✔
668
                }
669
#endif
670
                //  inf /  inf = -nan(ind)
671
                //  inf / -inf = -nan(ind)
672
                // -inf /  inf = -nan(ind)
673
                // -inf / -inf = -nan(ind)
674
                //        1.0 /  inf = 0
675
                bool resultSign = sign() != rhs.sign();
4,651,108✔
676
                if (isinf()) {
4,651,108✔
677
                        if (rhs.isinf()) {
31,056✔
678
                                // inf divide by inf yields quiet NaN (in MSVC it is labeled -nan(ind) for indeterminate)
679
                                setnan(NAN_TYPE_QUIET);
528✔
680
                                return *this;
528✔
681
                        }
682
                        else {
683
                                // we stay an infinite but may change sign
684
                                setsign(resultSign);
30,528✔
685
                                return *this;
30,528✔
686
                        }
687
                }
688
                else {
689
                        if (rhs.isinf()) {
4,620,052✔
690
                                setzero();
32,647✔
691
                                setsign(resultSign);
32,647✔
692
                                return *this;
32,647✔
693
                        }
694
                }
695

696
                if (iszero()) {
4,587,405✔
697
                        setzero();
139,004✔
698
                        setsign(resultSign); // deal with negative 0
139,004✔
699
                        return *this;
139,004✔
700
                }
701

702
                // arithmetic operation
703
                using BlockTriple = blocktriple<fbits, BlockTripleOperator::DIV, bt>;
704
                BlockTriple a, b, quotient;
4,448,401✔
705

706
                // transform the inputs into (sign,scale,significant) 
707
                // triples of the correct width
708
                normalizeDivision(a);
4,448,401✔
709
                rhs.normalizeDivision(b);
4,448,401✔
710
                quotient.div(a, b);
4,448,401✔
711
                quotient.setradix(BlockTriple::radix);
4,448,401✔
712
                convert(quotient, *this);
4,448,401✔
713

714
                if constexpr (_trace_div) std::cout << to_binary(a) << " : " << a << " /\n" << to_binary(b) << " : " << b << " =\n" << to_binary(quotient) << " : " << quotient << '\n';
715

716
                return *this;
4,448,401✔
717
        }
718
        cfloat& operator/=(double rhs) CFLOAT_EXCEPT {
719
                return *this /= cfloat(rhs);
720
        }
721
        cfloat& reciprocal() CFLOAT_EXCEPT {
722
                cfloat c = 1.0 / *this;
723
                return *this = c;
724
        }
725
        /// <summary>
726
        /// move to the next bit encoding modulo 2^nbits
727
        /// </summary>
728
        /// <typeparam name="bt"></typeparam>
729
        cfloat& operator++() {
4,598,619✔
730
                if constexpr (0 == nrBlocks) {
731
                        return *this;
732
                }
733
                else if constexpr (1 == nrBlocks) {
734
                        if (sign()) {
864,239✔
735
                                if (_block[MSU] == (SIGN_BIT_MASK | 1ul)) { // pattern: 1.00.001 = minneg
467✔
736
                                        _block[MSU] = 0; // pattern: 0.00.000 = +0 
7✔
737
                                }
738
                                else {
739
                                        --_block[MSU];
460✔
740
                                }
741
                                if constexpr (!hasSubnormals) {
742
                                        if (isdenormal()) {
198✔
743
                                                // special case, we need to jump past all the subnormal value encodings which puts us on 0
744
                                                _block[MSU] = 0; // pattern: 0.00.000 = +0
3✔
745
                                        }
746
                                }
747
                        }
748
                        else {
749
                                if constexpr (!hasSubnormals) {
750
                                        if (_block[MSU] == 0) {
384,649✔
751
                                                // special case, we need to jump past all the subnormal value encodings minus 1
752
                                                setfraction(0xFFFF'FFFF'FFFF'FFFFull);
4✔
753
                                        }
754
                                }
755
                                if ((_block[MSU] & (MSU_MASK >> 1)) == (MSU_MASK >> 1)) { // pattern: 0.11.111 = nan
863,772✔
756
                                        _block[MSU] |= SIGN_BIT_MASK; // pattern: 1.11.111 = snan : wrap to the other side of the encoding
×
757
                                }
758
                                else {
759
                                        ++_block[MSU];
863,772✔
760
                                }
761
                        }
762
                }
763
                else {
764
                        if (sign()) {
3,734,380✔
765
                                // special case: pattern: 1.00.001 = minneg transitions to pattern: 0.00.000 = +0 
766
                                if (isminnegencoding()) {
66,947✔
767
                                        setzero();
8✔
768
                                }
769
                                else {
770
                                        //  1111 0000
771
                                        //  1110 1111
772
                                        bool borrow = true;
66,939✔
773
                                        for (unsigned i = 0; i < MSU; ++i) {
199,415✔
774
                                                if (borrow) {
132,476✔
775
                                                        if ((_block[i] & storageMask) == 0) { // block will underflow
67,197✔
776
                                                                --_block[i];
261✔
777
                                                                borrow = true;
261✔
778
                                                        }
779
                                                        else {
780
                                                                --_block[i];
66,936✔
781
                                                                borrow = false;
66,936✔
782
                                                        }
783
                                                }
784
                                        }
785
                                        if (borrow) {
66,939✔
786
                                                --_block[MSU];
3✔
787
                                        }
788
                                        if constexpr (!hasSubnormals) {
789
                                                if (isdenormal()) {
385✔
790
                                                        // special case, we need to jump past all the subnormal value encodings which puts us on 0
791
                                                        setzero(); // pattern: 0.00.000 = +0
3✔
792
                                                }
793
                                        }
794
                                }
795
                        }
796
                        else {
797
                                // special case: pattern: 0.11.111 = nan transitions to pattern: 1.11.111 = snan 
798
                                if (isnanencoding()) {
3,667,433✔
799
                                        setnan(NAN_TYPE_SIGNALLING);
×
800
                                }
801
                                else {
802
                                        if constexpr (!hasSubnormals) {
803
                                                if (iszero()) {
1,720,661✔
804
                                                        // special case, we need to jump past all the subnormal value encodings minus 1 so that the increment code below ends up on normal minpos
805
                                                        setfraction(0xFFFF'FFFF'FFFF'FFFFull);
3✔
806
                                                }
807
                                        }
808
                                        bool carry = true;
3,667,433✔
809
                                        for (unsigned i = 0; i < MSU; ++i) {
7,400,766✔
810
                                                if (carry) {
3,733,333✔
811
                                                        if ((_block[i] & storageMask) == storageMask) { // block will overflow
3,667,688✔
812
                                                                _block[i] = 0;
256✔
813
                                                                carry = true;
256✔
814
                                                        }
815
                                                        else {
816
                                                                ++_block[i];
3,667,432✔
817
                                                                carry = false;
3,667,432✔
818
                                                        }
819
                                                }
820
                                        }
821
                                        if (carry) {
3,667,433✔
822
                                                ++_block[MSU];
1✔
823
                                        }
824
                                }
825
                        }
826
                }
827
                return *this;
4,598,619✔
828
        }
829
        cfloat operator++(int) {
134,800✔
830
                cfloat tmp(*this);
134,800✔
831
                operator++();
134,800✔
832
                return tmp;
134,800✔
833
        }
834
        cfloat& operator--() {
5,014,100✔
835
                if constexpr (0 == nrBlocks) {
836
                        return *this;
837
                }
838
                else if constexpr (1 == nrBlocks) {
839
                        if (sign()) {
942,793✔
840
                                ++_block[MSU];
942,299✔
841
                        }
842
                        else {
843
                                // positive range
844
                                if (_block[MSU] == 0) { // pattern: 0.00.000 = 0
494✔
845
                                        if constexpr (hasSubnormals) {
846
                                                _block[MSU] |= SIGN_BIT_MASK | bt(1u); // pattern: 1.00.001 = minneg 
8✔
847
                                        }
848
                                        else {
849
                                                // special case, we need to jump past all the subnormal value encodings
850
                                                setfraction(0xFFFF'FFFF'FFFF'FFFFull); // set to 0.00.11...11
3✔
851
                                                ++_block[MSU]; // increment into 0.01.0000
3✔
852
                                                _block[MSU] |= SIGN_BIT_MASK; // set to 1.01.0000
3✔
853
                                        }
854
                                }
855
                                else {
856
                                        --_block[MSU];
483✔
857
                                }
858
                                if constexpr (!hasSubnormals) {
859
                                        if (isdenormal()) {
211✔
860
                                                // special case, we need to jump past all the subnormal value encodings which puts us on 0
861
                                                _block[MSU] = 0; // pattern: 0.00.000 = +0
3✔
862
                                        }
863
                                }
864
                        }
865
                }
866
                else {
867
                        if (sign()) {
4,071,307✔
868
                                bool carry = true;
4,004,349✔
869
                                for (unsigned i = 0; i < MSU; ++i) {
8,074,235✔
870
                                        if (carry) {
4,069,886✔
871
                                                if ((_block[i] & storageMask) == storageMask) { // block will overflow
4,004,604✔
872
                                                        _block[i] = 0;
256✔
873
                                                        carry = true;
256✔
874
                                                }
875
                                                else {
876
                                                        ++_block[i];
4,004,348✔
877
                                                        carry = false;
4,004,348✔
878
                                                }
879
                                        }
880
                                }
881
                                if (carry) {
4,004,349✔
882
                                        ++_block[MSU];
1✔
883
                                }
884
                        }
885
                        else {
886
                                // special case: pattern: 0.00.000 = +0 transitions to pattern: 1.00.001 = minneg
887
                                if (iszeroencoding()) {
66,958✔
888
                                        if constexpr (hasSubnormals) {
889
                                                setsign(true);
8✔
890
                                                setbit(0, true);
8✔
891
                                        }
892
                                        else {
893
                                                // special case, we need to jump past all the subnormal value encodings 1.01.0000 = minneg normal
894
                                                setexponent(1 - EXP_BIAS);
2✔
895
                                                setsign(true);
2✔
896
                                        }
897
                                }
898
                                else {
899
                                        bool borrow = true;
66,948✔
900
                                        for (unsigned i = 0; i < MSU; ++i) {
199,441✔
901
                                                if (borrow) {
132,493✔
902
                                                        if ((_block[i] & storageMask) == 0) { // block will underflow
67,209✔
903
                                                                --_block[i];
266✔
904
                                                                borrow = true;
266✔
905
                                                        }
906
                                                        else {
907
                                                                --_block[i];
66,943✔
908
                                                                borrow = false;
66,943✔
909
                                                        }
910
                                                }
911
                                        }
912
                                        if (borrow) {
66,948✔
913
                                                --_block[MSU];
5✔
914
                                        }
915
                                        if constexpr (!hasSubnormals) {
916
                                                if (isdenormal()) {
384✔
917
                                                        // special case, we need to jump past all the subnormal value encodings which puts us on 0
918
                                                        setzero(); // pattern: 0.00.000 = +0
2✔
919
                                                }
920
                                        }
921
                                }
922
                        }
923
                }
924
                return *this;
5,014,100✔
925
        }
926
        cfloat operator--(int) {
134,812✔
927
                cfloat tmp(*this);
134,812✔
928
                operator--();
134,812✔
929
                return tmp;
134,812✔
930
        }
931

932
        // modifiers        
933
        constexpr void clear() noexcept {
136,133,106✔
934
                if constexpr (0 == nrBlocks) {
935
                        return;
936
                }
937
                else if constexpr (1 == nrBlocks) {
938
                        _block[0] = bt(0);
41,270,135✔
939
                }
940
                else if constexpr (2 == nrBlocks) {
941
                        _block[0] = bt(0);
46,801,470✔
942
                        _block[1] = bt(0);
46,801,470✔
943
                }
944
                else if constexpr (3 == nrBlocks) {
945
                        _block[0] = bt(0);
47,927,465✔
946
                        _block[1] = bt(0);
47,927,465✔
947
                        _block[2] = bt(0);
47,927,465✔
948
                }
949
                else if constexpr (4 == nrBlocks) {
950
                        _block[0] = bt(0);
43,351✔
951
                        _block[1] = bt(0);
43,351✔
952
                        _block[2] = bt(0);
43,351✔
953
                        _block[3] = bt(0);
43,351✔
954
                }
955
                else if constexpr (5 == nrBlocks) {
956
                        _block[0] = bt(0);
29,999✔
957
                        _block[1] = bt(0);
29,999✔
958
                        _block[2] = bt(0);
29,999✔
959
                        _block[3] = bt(0);
29,999✔
960
                        _block[4] = bt(0);
29,999✔
961
                }
962
                else if constexpr (6 == nrBlocks) {
963
                        _block[0] = bt(0);
10,015✔
964
                        _block[1] = bt(0);
10,015✔
965
                        _block[2] = bt(0);
10,015✔
966
                        _block[3] = bt(0);
10,015✔
967
                        _block[4] = bt(0);
10,015✔
968
                        _block[5] = bt(0);
10,015✔
969
                }
970
                else if constexpr (7 == nrBlocks) {
971
                        _block[0] = bt(0);
9,999✔
972
                        _block[1] = bt(0);
9,999✔
973
                        _block[2] = bt(0);
9,999✔
974
                        _block[3] = bt(0);
9,999✔
975
                        _block[4] = bt(0);
9,999✔
976
                        _block[5] = bt(0);
9,999✔
977
                        _block[6] = bt(0);
9,999✔
978
                }
979
                else if constexpr (8 == nrBlocks) {
980
                        _block[0] = bt(0);
20,325✔
981
                        _block[1] = bt(0);
20,325✔
982
                        _block[2] = bt(0);
20,325✔
983
                        _block[3] = bt(0);
20,325✔
984
                        _block[4] = bt(0);
20,325✔
985
                        _block[5] = bt(0);
20,325✔
986
                        _block[6] = bt(0);
20,325✔
987
                        _block[7] = bt(0);
20,325✔
988
                }
989
                else {
990
                        for (unsigned i = 0; i < nrBlocks; ++i) {
224,339✔
991
                                _block[i] = bt(0);
203,992✔
992
                        }
993
                }
994
        }
136,133,106✔
995
        constexpr void setzero() noexcept { clear(); }
2,692,114✔
996
        constexpr void setinf(bool sign = true) noexcept {
5,951,212✔
997
                // the Inf encoding is the pattern 0b0'11...11'11...10 for a +inf, and 0b1'11...11'11...110 for a -inf
998
                if constexpr (0 == nrBlocks) {
999
                        return;
1000
                }
1001
                else if constexpr (1 == nrBlocks) {
1002
                        _block[MSU] = sign ? bt(MSU_MASK ^ LSB_BIT_MASK) : bt(~SIGN_BIT_MASK & (MSU_MASK ^ LSB_BIT_MASK));
2,195,200✔
1003
                }
1004
                else if constexpr (2 == nrBlocks) {
1005
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
1,750,719✔
1006
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
1,750,719✔
1007
                }
1008
                else if constexpr (3 == nrBlocks) {
1009
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
2,002,271✔
1010
                        _block[1] = BLOCK_MASK;
2,002,271✔
1011
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
2,002,271✔
1012
                }
1013
                else if constexpr (4 == nrBlocks) {
1014
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
2,720✔
1015
                        _block[1] = BLOCK_MASK;
2,720✔
1016
                        _block[2] = BLOCK_MASK;
2,720✔
1017
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
2,720✔
1018
                }
1019
                else if constexpr (5 == nrBlocks) {
1020
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
90✔
1021
                        _block[1] = BLOCK_MASK;
90✔
1022
                        _block[2] = BLOCK_MASK;
90✔
1023
                        _block[3] = BLOCK_MASK;
90✔
1024
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
90✔
1025
                }
1026
                else if constexpr (6 == nrBlocks) {
1027
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
46✔
1028
                        _block[1] = BLOCK_MASK;
46✔
1029
                        _block[2] = BLOCK_MASK;
46✔
1030
                        _block[3] = BLOCK_MASK;
46✔
1031
                        _block[4] = BLOCK_MASK;
46✔
1032
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
46✔
1033
                }
1034
                else if constexpr (7 == nrBlocks) {
1035
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
46✔
1036
                        _block[1] = BLOCK_MASK;
46✔
1037
                        _block[2] = BLOCK_MASK;
46✔
1038
                        _block[3] = BLOCK_MASK;
46✔
1039
                        _block[4] = BLOCK_MASK;
46✔
1040
                        _block[5] = BLOCK_MASK;
46✔
1041
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
46✔
1042
                }
1043
                else if constexpr (8 == nrBlocks) {
1044
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
81✔
1045
                        _block[1] = BLOCK_MASK;
81✔
1046
                        _block[2] = BLOCK_MASK;
81✔
1047
                        _block[3] = BLOCK_MASK;
81✔
1048
                        _block[4] = BLOCK_MASK;
81✔
1049
                        _block[5] = BLOCK_MASK;
81✔
1050
                        _block[6] = BLOCK_MASK;
81✔
1051
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
81✔
1052
                }
1053
                else {
1054
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
39✔
1055
                        for (unsigned i = 1; i < nrBlocks - 1; ++i) {
351✔
1056
                                _block[i] = BLOCK_MASK;
312✔
1057
                        }
1058
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
39✔
1059
                }        
1060
        }
5,951,212✔
1061
        constexpr void setnan(int NaNType = NAN_TYPE_SIGNALLING) noexcept {
7,105,167✔
1062
                // the NaN encoding is the pattern 0b0'11...11'11...11 for a quiet Nan, and 0b1'11...11'11...111 for a signalling NaN
1063
                if constexpr (0 == nrBlocks) {
1064
                        return;
1065
                }
1066
                else if constexpr (1 == nrBlocks) {
1067
                        // fall through
1068
                }
1069
                else if constexpr (2 == nrBlocks) {
1070
                        _block[0] = BLOCK_MASK;
1,994,454✔
1071
                }
1072
                else if constexpr (3 == nrBlocks) {
1073
                        _block[0] = BLOCK_MASK;
1,048,674✔
1074
                        _block[1] = BLOCK_MASK;
1,048,674✔
1075
                }
1076
                else if constexpr (4 == nrBlocks) {
1077
                        _block[0] = BLOCK_MASK;
25✔
1078
                        _block[1] = BLOCK_MASK;
25✔
1079
                        _block[2] = BLOCK_MASK;
25✔
1080
                }
1081
                else if constexpr (5 == nrBlocks) {
1082
                        _block[0] = BLOCK_MASK;
8✔
1083
                        _block[1] = BLOCK_MASK;
8✔
1084
                        _block[2] = BLOCK_MASK;
8✔
1085
                        _block[3] = BLOCK_MASK;
8✔
1086
                }
1087
                else if constexpr (6 == nrBlocks) {
1088
                        _block[0] = BLOCK_MASK;
4✔
1089
                        _block[1] = BLOCK_MASK;
4✔
1090
                        _block[2] = BLOCK_MASK;
4✔
1091
                        _block[3] = BLOCK_MASK;
4✔
1092
                        _block[4] = BLOCK_MASK;
4✔
1093
                }
1094
                else if constexpr (7 == nrBlocks) {
1095
                        _block[0] = BLOCK_MASK;
4✔
1096
                        _block[1] = BLOCK_MASK;
4✔
1097
                        _block[2] = BLOCK_MASK;
4✔
1098
                        _block[3] = BLOCK_MASK;
4✔
1099
                        _block[4] = BLOCK_MASK;
4✔
1100
                        _block[5] = BLOCK_MASK;
4✔
1101
                }
1102
                else if constexpr (8 == nrBlocks) {
1103
                        _block[0] = BLOCK_MASK;
4✔
1104
                        _block[1] = BLOCK_MASK;
4✔
1105
                        _block[2] = BLOCK_MASK;
4✔
1106
                        _block[3] = BLOCK_MASK;
4✔
1107
                        _block[4] = BLOCK_MASK;
4✔
1108
                        _block[5] = BLOCK_MASK;
4✔
1109
                        _block[6] = BLOCK_MASK;
4✔
1110
                }
1111
                else {
1112
                        for (unsigned i = 0; i < nrBlocks - 1; ++i) {
30✔
1113
                                _block[i] = BLOCK_MASK;
27✔
1114
                        }
1115
                }
1116
                _block[MSU] = NaNType == NAN_TYPE_SIGNALLING ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
7,105,167✔
1117
        }
7,105,167✔
1118
        constexpr void setsign(bool sign = true) {
3,533,987✔
1119
                if (sign) {
3,533,987✔
1120
                        _block[MSU] |= SIGN_BIT_MASK;
631,209✔
1121
                }
1122
                else {
1123
                        _block[MSU] &= ~SIGN_BIT_MASK;
2,902,778✔
1124
                }
1125
        }
3,533,987✔
1126
        constexpr bool setexponent(int scale) {
590,516✔
1127
                if (scale < MIN_EXP_SUBNORMAL || scale > MAX_EXP) return false; // this scale cannot be represented
590,516✔
1128
                if constexpr (nbits < 65) {
1129
                        uint32_t exponentBits = static_cast<uint32_t>(scale + EXP_BIAS);
588,975✔
1130
                        if (scale >= MIN_EXP_SUBNORMAL && scale < MIN_EXP_NORMAL) {
588,975✔
1131
                                // we are a subnormal number: all exponent bits are 0
1132
                                exponentBits = 0;
45✔
1133
                        }
1134
                        // TODO: optimize
1135
                        uint32_t mask = (1ul << (es - 1));
588,975✔
1136
                        for (unsigned i = nbits - 2; i > nbits - 2 - es; --i) {
5,084,089✔
1137
                                setbit(i, (mask & exponentBits));
4,495,114✔
1138
                                mask >>= 1;
4,495,114✔
1139
                        }
1140
                }
1141
                else {
1142
                        uint32_t exponentBits = static_cast<uint32_t>(scale + EXP_BIAS);
1,541✔
1143
                        uint32_t mask = (1ul << (es - 1));
1,541✔
1144
                        for (unsigned i = nbits - 2; i > nbits - 2 - es; --i) {
25,156✔
1145
                                setbit(i, (mask & exponentBits));
23,615✔
1146
                                mask >>= 1;
23,615✔
1147
                        }
1148
                }
1149
                return true;
590,516✔
1150
        }
1151
        constexpr void setfraction(uint64_t raw_bits) {
610✔
1152
                // unoptimized as it is not meant to be an end-user API, it is a test API
1153
                // raw_bits is uint64_t so can have at most 64 bits of fraction data
1154
                constexpr unsigned bitsToSet = (fbits < 64) ? fbits : 64;
610✔
1155
                uint64_t mask{ 1ull };
610✔
1156
                for (unsigned i = 0; i < bitsToSet; ++i) {
24,744✔
1157
                        setbit(i, (mask & raw_bits));
24,134✔
1158
                        mask <<= 1;
24,134✔
1159
                }
1160
        }
610✔
1161
        constexpr void setbit(unsigned i, bool v = true) noexcept {
7,476,519✔
1162
                unsigned blockIndex = i / bitsInBlock;
7,476,519✔
1163
                if (blockIndex < nrBlocks) {
7,476,519✔
1164
                        bt block = _block[blockIndex];
7,476,519✔
1165
                        bt null = ~(1ull << (i % bitsInBlock));
7,476,519✔
1166
                        bt bit = bt(v ? 1 : 0);
7,476,519✔
1167
                        bt mask = bt(bit << (i % bitsInBlock));
7,476,519✔
1168
                        _block[blockIndex] = bt((block & null) | mask);
7,476,519✔
1169
                }
1170
        }
7,476,519✔
1171
        constexpr cfloat& setbits(uint64_t raw_bits) noexcept {
294,354,307✔
1172
                if constexpr (0 == nrBlocks) {
1173
                        return *this;
1174
                }
1175
                else if constexpr (1 == nrBlocks) {
1176
                        _block[0] = raw_bits & storageMask;
89,194,753✔
1177
                }
1178
                else if constexpr (2 == nrBlocks) {
1179
                        if constexpr (bitsInBlock < 64) {
1180
                                _block[0] = raw_bits & storageMask;
101,167,838✔
1181
                                raw_bits >>= bitsInBlock;
101,167,838✔
1182
                                _block[1] = raw_bits & storageMask;
101,167,838✔
1183
                        }
1184
                        else {
1185
                                _block[0] = raw_bits & storageMask;
1186
                                _block[1] = 0;
1187
                        }
1188
                }
1189
                else if constexpr (3 == nrBlocks) {
1190
                        if constexpr (bitsInBlock < 64) {
1191
                                _block[0] = raw_bits & storageMask;
103,831,991✔
1192
                                raw_bits >>= bitsInBlock;
103,831,991✔
1193
                                _block[1] = raw_bits & storageMask;
103,831,991✔
1194
                                raw_bits >>= bitsInBlock;
103,831,991✔
1195
                                _block[2] = raw_bits & storageMask;
103,831,991✔
1196
                        }
1197
                        else {
1198
                                _block[0] = raw_bits & storageMask;
1199
                                _block[1] = 0;
1200
                                _block[2] = 0;
1201
                        }
1202
                }
1203
                else if constexpr (4 == nrBlocks) {
1204
                        if constexpr (bitsInBlock < 64) {
1205
                                _block[0] = raw_bits & storageMask;
69,991✔
1206
                                raw_bits >>= bitsInBlock;
69,991✔
1207
                                _block[1] = raw_bits & storageMask;
69,991✔
1208
                                raw_bits >>= bitsInBlock;
69,991✔
1209
                                _block[2] = raw_bits & storageMask;
69,991✔
1210
                                raw_bits >>= bitsInBlock;
69,991✔
1211
                                _block[3] = raw_bits & storageMask;
69,991✔
1212
                        }
1213
                        else {
1214
                                _block[0] = raw_bits & storageMask;
1215
                                _block[1] = 0;
1216
                                _block[2] = 0;
1217
                                _block[3] = 0;
1218
                        }
1219
                }
1220
                else {
1221
                        if constexpr (bitsInBlock < 64) {
1222
                                for (unsigned i = 0; i < nrBlocks; ++i) {
728,502✔
1223
                                        _block[i] = raw_bits & storageMask;
638,768✔
1224
                                        raw_bits >>= bitsInBlock;
638,768✔
1225
                                }
1226
                        }
1227
                        else {
1228
                                _block[0] = raw_bits & storageMask;
1229
                                for (unsigned i = 1; i < nrBlocks; ++i) {
1230
                                        _block[i] = 0;
1231
                                }
1232
                        }
1233
                }
1234
                _block[MSU] &= MSU_MASK; // enforce precondition for fast comparison by properly nulling bits that are outside of nbits
294,354,307✔
1235
                return *this;
294,354,307✔
1236
        }
1237
        constexpr void setblock(unsigned b, bt data) noexcept {
542,416✔
1238
                if (b < nrBlocks) _block[b] = data;
542,416✔
1239
        }
542,416✔
1240
        
1241
        // create specific number system values of interest
1242
        constexpr cfloat& maxpos() noexcept {
635,206✔
1243
                if constexpr (isSaturating) {
1244
                        // in a saturating encoding with max-exponent values we are removing the Inf encoding pattern 0b0'11...11'11...10 for a +inf, 
1245
                        // and 0b1'11...11'11...110 for a -inf and using it as a value
1246
                        if constexpr (hasMaxExpValues) {
1247
                                // maximum positive value has this bit pattern: 0-1...1-111...110, that is, sign = 0, e = 11..11, f = 111...110
1248
                                clear();
634,552✔
1249
                                flip();
634,552✔
1250
                                setbit(nbits - 1ull, false); // sign = 0
634,552✔
1251
                                setbit(0ull, false); // bit0 = 0
634,552✔
1252
                        }
1253
                        else {
1254
                                // maximum positive value has this bit pattern: 0-11...10-111...111, that is, sign = 0, e = 11..10, f = 111...111
1255
                                clear();
415✔
1256
                                flip();
415✔
1257
                                setbit(fbits, false); // set least significant exponent bit to 0
415✔
1258
                                setbit(nbits - 1ull, false); // set sign to 0
415✔
1259
                        }
1260
                }
1261
                else {
1262
                        // the Inf encoding is the pattern 0b0'11...11'11...10 for a +inf, and 0b1'11...11'11...110 for a -inf
1263
                        // the maxpos is the encoding before that
1264
                        if constexpr (hasMaxExpValues) {
1265
                                // maximum positive value has this bit pattern: 0-1...1-111...101, that is, sign = 0, e = 11..11, f = 111...101
1266
                                clear();
70✔
1267
                                flip();
70✔
1268
                                setbit(nbits - 1ull, false); // sign = 0
70✔
1269
                                setbit(1ull, false); // bit1 = 0
70✔
1270
                        }
1271
                        else {
1272
                                // maximum positive value has this bit pattern: 0-1...0-111...111, that is, sign = 0, e = 11..10, f = 111...111
1273
                                clear();
169✔
1274
                                flip();
169✔
1275
                                setbit(fbits, false); // set least significant exponent bit to 0
169✔
1276
                                setbit(nbits - 1ull, false); // set sign to 0
169✔
1277
                        }
1278
                }
1279
                return *this;
635,206✔
1280
        }
1281
        constexpr cfloat& minpos() noexcept {
13,201✔
1282
                // minpos encoding is not impacted by saturating encodings, which only affects maxpos and inf
1283
                if constexpr (hasSubnormals) {
1284
                        // minimum positive value has this bit pattern: 0-000-00...01, that is, sign = 0, e = 000, f = 00001
1285
                        clear();
327✔
1286
                        setbit(0);
327✔
1287
                }
1288
                else {
1289
                        // minimum positive value has this bit pattern: 0-001-00...0, that is, sign = 0, e = 001, f = 0000
1290
                        clear();
12,874✔
1291
                        setbit(fbits);
12,874✔
1292
                }
1293
                return *this;
13,201✔
1294
        }
1295
        constexpr cfloat& zero() noexcept {
1✔
1296
                // the zero value
1297
                clear();
1✔
1298
                return *this;
1✔
1299
        }
1300
        constexpr cfloat& minneg() noexcept {
11✔
1301
                // minneg encoding is not impacted by saturating encodings, which only affects maxpos and inf
1302
                if constexpr (hasSubnormals) {
1303
                        // minimum negative value has this bit pattern: 1-000-00...01, that is, sign = 1, e = 00, f = 00001
1304
                        clear();
8✔
1305
                        setbit(nbits - 1ull);
8✔
1306
                        setbit(0);
8✔
1307
                }
1308
                else {
1309
                        // minimum negative value has this bit pattern: 1-001-00...0, that is, sign = 1, e = 001, f = 0000
1310
                        clear();
3✔
1311
                        setbit(fbits);
3✔
1312
                        setbit(nbits - 1ull);
3✔
1313
                }
1314
                return *this;
11✔
1315
        }
1316
        constexpr cfloat& maxneg() noexcept {
634,956✔
1317
                if constexpr (isSaturating) {
1318
                        // in a saturating encoding with max-exponent values we are removing the Inf encoding pattern 0b0'11...11'11...10 for a +inf, 
1319
                        // and 0b1'11...11'11...110 for a -inf and using it as a value
1320
                        if constexpr (hasMaxExpValues) {
1321
                                // maximum negative value has this bit pattern: 1-1...1-111...110, that is, sign = 1, e = 1..1, f = 111...110
1322
                                clear();
634,501✔
1323
                                flip();
634,501✔
1324
                                setbit(0ull, false);
634,501✔
1325
                        }
1326
                        else {
1327
                                // maximum negative value has this bit pattern: 1-1...0-111...111, that is, sign = 1, e = 11..10, f = 111...111
1328
                                clear();
397✔
1329
                                flip();
397✔
1330
                                setbit(fbits, false);
397✔
1331
                        }
1332
                }
1333
                else {
1334
                        if constexpr (hasMaxExpValues) {
1335
                                // maximum negative value has this bit pattern: 1-1...1-111...101, that is, sign = 1, e = 1..1, f = 111...101
1336
                                clear();
12✔
1337
                                flip();
12✔
1338
                                setbit(1ull, false);
12✔
1339
                        }
1340
                        else {
1341
                                // maximum negative value has this bit pattern: 1-1...0-111...111, that is, sign = 1, e = 11..10, f = 111...111
1342
                                clear();
46✔
1343
                                flip();
46✔
1344
                                setbit(fbits, false);
46✔
1345
                        }
1346
                }
1347
                return *this;
634,956✔
1348
        }
1349

1350

1351
        /// <summary>
1352
        /// assign the value of the string representation to the cfloat
1353
        /// </summary>
1354
        /// <param name="stringRep">decimal scientific notation of a real number to be assigned</param>
1355
        /// <returns>reference to this cfloat</returns>
1356
        /// Clang doesn't support constexpr yet on string manipulations, so we need to make it conditional
1357
        CONSTEXPRESSION cfloat& assign(const std::string& str) noexcept {
7✔
1358
                clear();
7✔
1359
                unsigned nrChars = static_cast<unsigned>(str.size());
7✔
1360
                unsigned nrBits = 0;
7✔
1361
                unsigned nrDots = 0;
7✔
1362
                std::string bits;
7✔
1363
                if (nrChars > 2) {
7✔
1364
                        if (str[0] == '0' && str[1] == 'b') {
7✔
1365
                                for (unsigned i = 2; i < nrChars; ++i) {
211✔
1366
                                        char c = str[i];
204✔
1367
                                        switch (c) {
204✔
1368
                                        case '0':
186✔
1369
                                        case '1':
1370
                                                ++nrBits;
186✔
1371
                                                bits += c;
186✔
1372
                                                break;
186✔
1373
                                        case '.':
14✔
1374
                                                ++nrDots;
14✔
1375
                                                bits += c;
14✔
1376
                                                break;
14✔
1377
                                        case '\'':
4✔
1378
                                                // consume this delimiting character
1379
                                                break;
4✔
1380
                                        default:
×
1381
                                                std::cerr << "string contained a non-standard character: " << c << '\n';
×
1382
                                                return *this;
×
1383
                                        }
1384
                                }
1385
                        }
1386
                        else {
1387
                                std::cerr << "string must start with 0b: instead input pattern was " << str << '\n';
×
1388
                                return *this;
×
1389
                        }
1390
                }
1391
                else {
1392
                        std::cerr << "string is too short\n";
×
1393
                        return *this;
×
1394
                }
1395

1396
                if (nrBits != nbits) {
7✔
1397
                        std::cerr << "number of bits in the string is " << nrBits << " and needs to be " << nbits << '\n';
×
1398
                        return *this;
×
1399
                }
1400
                if (nrDots != 2) {
7✔
1401
                        std::cerr << "number of segment delimiters in string is " << nrDots << " and needs to be 2 for a cfloat<>\n";
×
1402
                        return *this;
×
1403
                }
1404

1405
                // assign the bits
1406
                int field{ 0 };  // three fields: sign, exponent, mantissa: fields are separated by a '.'
7✔
1407
                int nrExponentBits{ -1 };
7✔
1408
                unsigned bit = nrBits;
7✔
1409
                for (unsigned i = 0; i < bits.size(); ++i) {
207✔
1410
                        char c = bits[i];
200✔
1411
                        if (c == '.') {
200✔
1412
                                ++field;
14✔
1413
                                if (field == 2) { // just finished parsing exponent field: we can now check the number of exponent bits
14✔
1414
                                        if (nrExponentBits != es) {
7✔
1415
                                                std::cerr << "provided binary string representation does not contain " << es << " exponent bits. Found " << nrExponentBits << ". Reset to 0\n";
×
1416
                                                clear();
×
1417
                                                return *this;
×
1418
                                        }
1419
                                }
1420
                        }
1421
                        else {
1422
                                setbit(--bit, c == '1');
186✔
1423
                        }
1424
                        if (field == 1) { // exponent field
200✔
1425
                                ++nrExponentBits;
51✔
1426
                        }
1427
                }
1428
                if (field != 2) {
7✔
1429
                        std::cerr << "provided binary string did not contain three fields separated by '.': Reset to 0\n";
×
1430
                        clear();
×
1431
                        return *this;
×
1432
                }
1433
                return *this;
7✔
1434
        }
7✔
1435

1436
        // selectors
1437
        constexpr bool sign() const noexcept { return (_block[MSU] & SIGN_BIT_MASK) == SIGN_BIT_MASK; }
257,737,566✔
1438
        constexpr int  scale() const noexcept {
60,878,485✔
1439
                int e{ 0 };
60,878,485✔
1440
                if constexpr (MSU_CAPTURES_EXP) {
1441
                        e = static_cast<int>((_block[MSU] & ~SIGN_BIT_MASK) >> EXP_SHIFT);
49,230,865✔
1442
                        if (e == 0) {
49,230,865✔
1443
                                // subnormal scale is determined by fraction
1444
                                // subnormals: (-1)^s * 2^(2-2^(es-1)) * (f/2^fbits))
1445
                                e = (2l - (1l << (es - 1ull))) - 1;
6,529,798✔
1446
                                if constexpr (nbits > 2 + es) {
1447
                                        for (unsigned i = nbits - 2ull - es; i > 0; --i) {
12,571,064✔
1448
                                                if (test(i)) break;
12,466,666✔
1449
                                                --e;
6,063,791✔
1450
                                        }
1451
                                }
1452
                        }
1453
                        else {
1454
                                e -= EXP_BIAS;
42,701,067✔
1455
                        }
1456
                }
1457
                else {
1458
                        blockbinary<es, bt> ebits{};
11,647,794✔
1459
                        exponent(ebits);
11,647,620✔
1460
                        if (ebits.iszero()) {
11,647,620✔
1461
                                // subnormal scale is determined by fraction
1462
                                // subnormals: (-1)^s * 2^(2-2^(es-1)) * (f/2^fbits))
1463
                                e = (2l - (1l << (es - 1ull))) - 1;
1,599,549✔
1464
                                if constexpr (nbits > 2 + es) {
1465
                                        for (unsigned i = nbits - 2ull - es; i > 0; --i) {
3,158,541✔
1466
                                                if (test(i)) break;
3,151,425✔
1467
                                                --e;
1,559,003✔
1468
                                        }
1469
                                }
1470
                        }
1471
                        else {
1472
                                e = static_cast<int>(unsigned(ebits) - EXP_BIAS);
10,048,071✔
1473
                        }
1474
                }
1475
                return e;
60,878,485✔
1476
        }
1477
        constexpr bool isneg() const noexcept {
558✔
1478
                if (isnan()) return false;
558✔
1479
                return sign(); 
541✔
1480
        }
1481
        constexpr bool ispos() const noexcept { 
34,472✔
1482
                if (isnan()) return false;
34,472✔
1483
                return !sign(); 
34,472✔
1484
        }
1485
        constexpr bool iszero() const noexcept {
298,036,307✔
1486
                // NOTE: this is a very specific design that makes the decsion that
1487
                // for subnormal encodings found in a configuration that doesn't
1488
                // support them, we assume that these values map to 0.
1489
                if constexpr (hasSubnormals) {
1490
                        return iszeroencoding();
160,900,124✔
1491
                }
1492
                else { // all subnormals round to 0
1493
                        blockbinary<es, bt> ebits{};
137,136,621✔
1494
                        exponent(ebits);
137,136,183✔
1495
                        if (ebits.iszero()) return true; else return false;
137,136,183✔
1496
                }
1497
        }
1498
        constexpr bool isone() const noexcept {
1499
                // unbiased exponent = scale = 0, fraction = 0
1500
                int s = scale();
1501
                if (s == 0) {
1502
                        blockbinary<fbits, bt> f{};
1503
                        fraction(f);
1504
                        return f.iszero();
1505
                }
1506
                return false;
1507
        }
1508
        constexpr bool isinf(int InfType = INF_TYPE_EITHER) const noexcept {
373,577,690✔
1509
                // the bit pattern encoding of Inf is independent of the max-exponent value configuration
1510
                bool isNegInf = false;
373,577,690✔
1511
                bool isPosInf = false;
373,577,690✔
1512
                if constexpr (0 == nrBlocks) {
1513
                        return false;
1514
                }
1515
                else if constexpr (1 == nrBlocks) {
1516
                        isNegInf = (_block[MSU] & MSU_MASK) == (MSU_MASK ^ LSB_BIT_MASK);
167,822,458✔
1517
                        isPosInf = (_block[MSU] & MSU_MASK) == ((MSU_MASK ^ SIGN_BIT_MASK) ^ LSB_BIT_MASK);
167,822,458✔
1518
                }
1519
                else if constexpr (2 == nrBlocks) {
1520
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK));
144,225,855✔
1521
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
144,225,855✔
1522
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
144,225,855✔
1523
                }
1524
                else if constexpr (3 == nrBlocks) {
1525
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK)) && (_block[1] == BLOCK_MASK);
61,337,293✔
1526
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
61,337,293✔
1527
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
61,337,293✔
1528
                }
1529
                else if constexpr (4 == nrBlocks) {
1530
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK)) && (_block[1] == BLOCK_MASK) && (_block[2] == BLOCK_MASK);
100,135✔
1531
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
100,135✔
1532
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
100,135✔
1533
                }
1534
                else {
1535
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK));
91,949✔
1536
                        for (unsigned i = 1; i < nrBlocks - 1; ++i) {
93,621✔
1537
                                if (_block[i] != BLOCK_MASK) {
93,292✔
1538
                                        isInf = false;
91,620✔
1539
                                        break;
91,620✔
1540
                                }
1541
                        }
1542
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
91,949✔
1543
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
91,949✔
1544
                }
1545

1546
                return (InfType == INF_TYPE_EITHER ? (isNegInf || isPosInf) :
424,306,509✔
1547
                        (InfType == INF_TYPE_NEGATIVE ? isNegInf :
76,093,529✔
1548
                                (InfType == INF_TYPE_POSITIVE ? isPosInf : false)));
424,307,110✔
1549
        }
50,728,819✔
1550
        constexpr bool isnan(int NaNType = NAN_TYPE_EITHER) const noexcept {
963,155,634✔
1551
                if constexpr (hasMaxExpValues) {
1552
                        return isnanencoding(NaNType);
564,912,677✔
1553
                }
1554
                else {
1555
                        if (ismaxexpvalue()) {
398,242,957✔
1556
                                // all these max-exponent encodings are NANs, except for the encoding representing INF
1557
                                bool isNaN = isinf() ? false : true;
24,472,379✔
1558
                                bool isNegNaN = isNaN && sign();
24,472,379✔
1559
                                bool isPosNaN = isNaN && !sign();
24,472,379✔
1560
                                return (NaNType == NAN_TYPE_EITHER ? (isNaN) :
27,860,996✔
1561
                                        (NaNType == NAN_TYPE_SIGNALLING ? isNegNaN :
4,513,407✔
1562
                                                (NaNType == NAN_TYPE_QUIET ? isPosNaN : false)));
26,721,959✔
1563
                        }
1564
                        else {
1565
                                return false;
373,770,578✔
1566
                        }
1567
                }
1568
        }
24,472,379✔
1569
        // iszeroencoding returns true if it finds a pure -0 or +0 pattern and returns false otherwise
1570
        constexpr bool iszeroencoding() const noexcept {
266,803,888✔
1571
                if constexpr (0 == nrBlocks) {
1572
                        return true;
1573
                }
1574
                else if constexpr (1 == nrBlocks) {
1575
                        return (_block[MSU] & ~SIGN_BIT_MASK) == 0;
113,224,470✔
1576
                }
1577
                else if constexpr (2 == nrBlocks) {
1578
                        return (_block[0] == 0) && (_block[MSU] & ~SIGN_BIT_MASK) == 0;
120,500,381✔
1579
                }
1580
                else if constexpr (3 == nrBlocks) {
1581
                        return (_block[0] == 0) && _block[1] == 0 && (_block[MSU] & ~SIGN_BIT_MASK) == 0;
32,863,274✔
1582
                }
1583
                else if constexpr (4 == nrBlocks) {
1584
                        return (_block[0] == 0) && _block[1] == 0 && _block[2] == 0 && (_block[MSU] & ~SIGN_BIT_MASK) == 0;
123,252✔
1585
                }
1586
                else {
1587
                        for (unsigned i = 0; i < nrBlocks - 1; ++i) if (_block[i] != 0) return false;
337,876✔
1588
                        return (_block[MSU] & ~SIGN_BIT_MASK) == 0;
820✔
1589
                }
1590
        }
1591
        // isminnegencoding returns true if it find the pattern 1.00.00001 and returns false otherwise
1592
        constexpr bool isminnegencoding() const noexcept {
66,947✔
1593
                if constexpr (0 == nrBlocks) {
1594
                        return false;
1595
                }
1596
                else if constexpr (1 == nrBlocks) {
1597
                        return (_block[MSU] & (SIGN_BIT_MASK | 1ul));
1598
                }
1599
                else if constexpr (2 == nrBlocks) {
1600
                        return ((_block[0] == 1ul) && (_block[1] == SIGN_BIT_MASK));
1,408✔
1601
                }
1602
                else if constexpr (3 == nrBlocks) {
1603
                        return ((_block[0] == 1ul) && (_block[1] == 0) && (_block[2] == SIGN_BIT_MASK));
65,536✔
1604
                }
1605
                else if constexpr (4 == nrBlocks) {
1606
                        return ((_block[0] == 1ul) && (_block[1] == 0) && (_block[2] == 0) && (_block[3] == SIGN_BIT_MASK));
3✔
1607
                }
1608
                else {
1609
                        if (_block[0] != 1ul) return false;
×
1610
                        for (unsigned i = 1; i < nrBlocks - 2; ++i) if (_block[i] != 0) return false;
×
1611
                        return (_block[MSU] == SIGN_BIT_MASK);
×
1612
                }
1613
        }
1614
        constexpr bool isnanencoding(int NaNType = NAN_TYPE_EITHER) const noexcept {
568,580,110✔
1615
                // the bit encoding of NaN is independent of the gradual overflow configuration
1616
                bool isNaN = true;
568,580,110✔
1617
                bool isNegNaN = false;
568,580,110✔
1618
                bool isPosNaN = false;
568,580,110✔
1619

1620
                if constexpr (0 == nrBlocks) {
1621
                        return false;
1622
                }
1623
                else if constexpr (1 == nrBlocks) {
1624
                }
1625
                else if constexpr (2 == nrBlocks) {
1626
                        isNaN = (_block[0] == BLOCK_MASK);
218,589,078✔
1627
                }
1628
                else if constexpr (3 == nrBlocks) {
1629
                        isNaN = (_block[0] == BLOCK_MASK) && (_block[1] == BLOCK_MASK);
175,052,007✔
1630
                }
1631
                else if constexpr (4 == nrBlocks) {
1632
                        isNaN = (_block[0] == BLOCK_MASK) && (_block[1] == BLOCK_MASK) && (_block[2] == BLOCK_MASK);
226,605✔
1633
                }
1634
                else {
1635
                        for (unsigned i = 0; i < nrBlocks - 1; ++i) {
269,723✔
1636
                                if (_block[i] != BLOCK_MASK) {
269,684✔
1637
                                        isNaN = false;
269,465✔
1638
                                        break;
269,465✔
1639
                                }
1640
                        }
1641
                }
1642
                isNegNaN = isNaN && ((_block[MSU] & MSU_MASK) == MSU_MASK);
568,580,110✔
1643
                isPosNaN = isNaN && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
568,580,110✔
1644

1645
                return (NaNType == NAN_TYPE_EITHER ? (isNegNaN || isPosNaN) :
772,714,750✔
1646
                        (NaNType == NAN_TYPE_SIGNALLING ? isNegNaN :
306,054,150✔
1647
                                (NaNType == NAN_TYPE_QUIET ? isPosNaN : false)));
772,419,130✔
1648
        }
204,134,640✔
1649
        // isnormal returns true if 0 or exponent bits are not all zero or one, false otherwise
1650
        constexpr bool isnormal() const noexcept {
60,666,010✔
1651
                if (iszeroencoding()) return true; // filter out the one special case
60,666,010✔
1652
                blockbinary<es, bt> e{};
60,666,183✔
1653
                exponent(e);
60,666,009✔
1654
                return !e.iszero() && !e.all();
60,666,009✔
1655
        }
1656
        // isdenormal returns true if exponent bits are all zero, false otherwise
1657
        constexpr bool isdenormal() const noexcept {
45,170,796✔
1658
                if (iszeroencoding()) return false; // filter out the one special case
45,170,796✔
1659
                blockbinary<es, bt> e{};
45,162,017✔
1660
                exponent(e);
45,162,017✔
1661
                return e.iszero(); 
45,162,017✔
1662
        }
1663
        // ismaxexpvalue returns true if exponent bits are all one, false otherwise
1664
        constexpr bool ismaxexpvalue() const noexcept {
401,488,510✔
1665
                blockbinary<es, bt> e{};
401,489,515✔
1666
                exponent(e);
401,488,510✔
1667
                return e.all();
401,488,510✔
1668
        }
1669
        // isinteger is TBD
1670
        constexpr bool isinteger() const noexcept { return false; } // return (floor(*this) == *this) ? true : false; }
1671
        
1672
        template<typename NativeReal>
1673
        constexpr bool inrange(NativeReal v) const noexcept {
9,306,296✔
1674
                // the valid range for this cfloat includes the interval between 
1675
                // maxpos and the value that would round down to maxpos
1676
                bool bIsInRange = true;                
9,306,296✔
1677
                if (v > 0) {
9,306,296✔
1678
                        cfloat c(SpecificValue::maxpos);
4,427,047✔
1679
                        cfloat<nbits + 1, es, BlockType, hasSubnormals, hasMaxExpValues, isSaturating> d{};
8,027,370✔
1680
                        d = NativeReal(c);
4,427,047✔
1681
                        ++d;
4,427,047✔
1682
                        if (v >= NativeReal(d)) bIsInRange = false;
4,427,047✔
1683
                }
1684
                else {
1685
                        cfloat c(SpecificValue::maxneg);
4,879,249✔
1686
                        cfloat<nbits + 1, es, BlockType, hasSubnormals, hasMaxExpValues, isSaturating> d{};
8,816,662✔
1687
                        d = NativeReal(c);
4,879,249✔
1688
                        --d;
4,879,249✔
1689
                        if (v <= NativeReal(d)) bIsInRange = false;
4,879,249✔
1690
                }
1691

1692
                return bIsInRange;
9,306,296✔
1693
        }
1694
        constexpr bool test(unsigned bitIndex) const noexcept {
15,628,987✔
1695
                return at(bitIndex);
15,628,987✔
1696
        }
1697
        constexpr bool at(unsigned bitIndex) const noexcept {
1,824,572,213✔
1698
                if (bitIndex < nbits) {
1,824,572,213✔
1699
                        bt word = _block[bitIndex / bitsInBlock];
1,824,572,213✔
1700
                        bt mask = bt(1ull << (bitIndex % bitsInBlock));
1,824,572,213✔
1701
                        return (word & mask);
1,824,572,213✔
1702
                }
1703
                return false;
×
1704
        }
1705
        constexpr uint8_t nibble(unsigned n) const noexcept {
640✔
1706
                if (n < (1 + ((nbits - 1) >> 2))) {
640✔
1707
                        bt word = _block[(n * 4) / bitsInBlock];
640✔
1708
                        int nibbleIndexInWord = int(n % (bitsInBlock >> 2ull));
640✔
1709
                        bt mask = bt(0xF << (nibbleIndexInWord * 4));
640✔
1710
                        bt nibblebits = bt(mask & word);
640✔
1711
                        return uint8_t(nibblebits >> (nibbleIndexInWord * 4));
640✔
1712
                }
1713
                return 0;
×
1714
        }
1715
        constexpr bt block(unsigned b) const noexcept {
1716
                if (b < nrBlocks) {
1717
                        return _block[b];
1718
                }
1719
                return 0;
1720
        }
1721

1722
        constexpr void sign(bool& s) const {
800✔
1723
                s = sign();
800✔
1724
        }
800✔
1725
        constexpr void exponent(blockbinary<es, bt>& e) const {
762,370,626✔
1726
                e.clear();
762,370,626✔
1727
                if constexpr (0 == nrBlocks) return;
1728
                else if constexpr (1 == nrBlocks) {
1729
                        bt ebits = bt(_block[MSU] & ~SIGN_BIT_MASK);
351,596,649✔
1730
                        e.setbits(uint64_t(ebits >> EXP_SHIFT));
351,596,649✔
1731
                }
1732
                else if constexpr (nrBlocks > 1) {
1733
                        if (MSU_CAPTURES_EXP) {
1734
                                bt ebits = bt(_block[MSU] & ~SIGN_BIT_MASK);
239,453,620✔
1735
                                e.setbits(uint64_t(ebits >> ((nbits - 1ull - es) % bitsInBlock)));
239,453,620✔
1736
                        }
1737
                        else {
1738
                                for (unsigned i = 0; i < es; ++i) { e.setbit(i, at(nbits - 1ull - es + i)); }
772,324,230✔
1739
                        }
1740
                }
1741
        }
762,370,626✔
1742
        template<unsigned targetFractionBits>
1743
        constexpr blockbinary<targetFractionBits, bt>& fraction(blockbinary<targetFractionBits, bt>& f) const {
34,084✔
1744
                static_assert(targetFractionBits >= fbits, "target blockbinary is too small and can't receive all fraction bits");
1745
                f.clear();
34,084✔
1746
                if constexpr (0 == nrBlocks) return f;
1747
                else if constexpr (1 == nrBlocks) {
1748
                        bt fraction = bt(_block[MSU] & ~MSU_EXP_MASK);
1,001✔
1749
                        f.setbits(fraction);
1,001✔
1750
                }
1751
                else if constexpr (nrBlocks > 1) {
1752
                        for (unsigned i = 0; i < fbits; ++i) { f.setbit(i, at(i)); }
242,396✔
1753
                }
1754
                return f;
34,084✔
1755
        }
1756
        constexpr uint64_t fraction_ull() const {
59,610,458✔
1757
                uint64_t raw{ 0 };
59,610,458✔
1758
                if constexpr (nbits - es - 1ull < 65ull) { // no-op if precondition doesn't hold
1759
                        if constexpr (1 == nrBlocks) {
1760
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
26,560,351✔
1761
                                raw = fbitMask & uint64_t(_block[0]);
26,560,351✔
1762
                        }
1763
                        else if constexpr (2 == nrBlocks) {
1764
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
22,777,998✔
1765
                                raw = fbitMask & ((uint64_t(_block[1]) << bitsInBlock) | uint64_t(_block[0]));
22,777,998✔
1766
                        }
1767
                        else if constexpr (3 == nrBlocks) {
1768
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
10,249,549✔
1769
                                raw = fbitMask & ((uint64_t(_block[2]) << (2 * bitsInBlock)) | (uint64_t(_block[1]) << bitsInBlock) | uint64_t(_block[0]));
10,249,549✔
1770
                        }
1771
                        else if constexpr (4 == nrBlocks) {
1772
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
22,316✔
1773
                                raw = fbitMask & ((uint64_t(_block[3]) << (3 * bitsInBlock)) | (uint64_t(_block[2]) << (2 * bitsInBlock)) | (uint64_t(_block[1]) << bitsInBlock) | uint64_t(_block[0]));
22,316✔
1774
                        }
1775
                        else {
1776
                                uint64_t mask{ 1 };
244✔
1777
                                for (unsigned i = 0; i < fbits; ++i) { 
11,140✔
1778
                                        if (test(i)) {
10,896✔
1779
                                                raw |= mask;
2,000✔
1780
                                        }
1781
                                        mask <<= 1;
10,896✔
1782
                                }
1783
                        }
1784
                }
1785
                return raw;
59,610,458✔
1786
        }
1787
        // construct the significant from the encoding, returns normalization offset
1788
        constexpr unsigned significant(blockbinary<fhbits, bt>& s, bool isNormal = true) const {
23✔
1789
                unsigned shift = 0;
23✔
1790
                if (iszero()) return 0;
23✔
1791
                if constexpr (0 == nrBlocks) return 0;
1792
                else if constexpr (1 == nrBlocks) {
1793
                        bt significant = bt(_block[MSU] & ~MSU_EXP_MASK & ~SIGN_BIT_MASK);
23✔
1794
                        if (isNormal) {
23✔
1795
                                significant |= (bt(0x1ul) << fbits);
×
1796
                        }
1797
                        else {
1798
                                unsigned msb = find_msb(significant);
23✔
1799
//                                std::cout << "msb : " << msb << " : fhbits : " << fhbits << " : " << to_binary(significant, true) << std::endl;
1800
                                shift = fhbits - msb;
23✔
1801
                                significant <<= shift;
23✔
1802
                        }
1803
                        s.setbits(significant);
23✔
1804
                }
1805
                else if constexpr (nrBlocks > 1) {
1806
                        s.clear();
1807
                        // TODO: design and implement a block-oriented algorithm, this sequential algorithm is super slow
1808
                        if (isNormal) {
1809
                                s.setbit(fbits);
1810
                                for (unsigned i = 0; i < fbits; ++i) { s.setbit(i, at(i)); }
1811
                        }
1812
                        else {
1813
                                // Find the MSB of the subnormal: 
1814
                                unsigned msb = 0;
1815
                                for (unsigned i = 0; i < fbits; ++i) { // msb protected from not being assigned through iszero test at prelude of function
1816
                                        msb = fbits - 1ull - i;
1817
                                        if (test(msb)) break;
1818
                                }
1819
                                //      m-----lsb
1820
                                // h00001010101
1821
                                // 101010100000
1822
                                for (unsigned i = 0; i <= msb; ++i) {
1823
                                        s.setbit(fbits - msb + i, at(i));
1824
                                }
1825
                                shift = fhbits - msb;
1826
                        }
1827
                }
1828
                return shift;
23✔
1829
        }
1830
        template<unsigned targetbits>
1831
        constexpr void bits(blockbinary<targetbits, bt>& b) const {
640✔
1832
                unsigned upperbound = (nbits > targetbits ? targetbits : nbits);
640✔
1833
                b.clear();
640✔
1834
                for (unsigned i = 0; i < upperbound; ++i) { b.setbit(i, at(i)); }
3,840✔
1835
        }
640✔
1836

1837
        // casts to native types
1838
        int to_int() const {
59,976✔
1839
                if (isnan()) return 0;
59,976✔
1840
                if (isinf()) return sign() ? std::numeric_limits<int>::min() : std::numeric_limits<int>::max();
56,644✔
1841
                return int(to_native<float>());
50,308✔
1842
        }
1843
        long to_long() const {
1844
                if (isnan()) return 0;
1845
                if (isinf()) return sign() ? std::numeric_limits<long>::min() : std::numeric_limits<long>::max();
1846
                return long(to_native<double>());
1847
        }
1848
        long long to_long_long() const {
1✔
1849
                if (isnan()) return 0;
1✔
1850
                if (isinf()) return sign() ? std::numeric_limits<long long>::min() : std::numeric_limits<long long>::max();
1✔
1851
                return (long long)(to_native<double>());
1✔
1852
        }
1853

1854
        // transform an cfloat to a native C++ floating-point. We are using the native
1855
        // precision to compute, which means that all sub-values need to be representable 
1856
        // by the native precision.
1857
        // A more accurate approximation would require an adaptive precision algorithm
1858
        // with a final rounding step.
1859
        template<typename TargetFloat>
1860
        TargetFloat to_native() const { 
113,460,952✔
1861
                TargetFloat v{ 0.0 };
113,460,952✔
1862
                if (iszero()) {
113,460,952✔
1863
                        // the optimizer might destroy the sign
1864
                        return sign() ? -TargetFloat(0) : TargetFloat(0);
910,535✔
1865
                }
1866
                else if (isnan()) {
112,550,417✔
1867
                        v = sign() ? std::numeric_limits<TargetFloat>::signaling_NaN() : std::numeric_limits<TargetFloat>::quiet_NaN();
6,137,550✔
1868
                }
1869
                else if (isinf()) {
106,412,867✔
1870
                        v = sign() ? -std::numeric_limits<TargetFloat>::infinity() : std::numeric_limits<TargetFloat>::infinity();
143,380✔
1871
                }
1872
                else { // TODO: this approach has catastrophic cancellation when nbits is large and native target float is too small
1873
                        TargetFloat f{ 0 };
106,269,487✔
1874
                        TargetFloat fbit{ 0.5 };
106,269,487✔
1875
                        for (int i = static_cast<int>(nbits - 2ull - es); i >= 0; --i) {
1,313,951,273✔
1876
                                f += at(static_cast<unsigned>(i)) ? fbit : TargetFloat(0);
1,207,681,786✔
1877
                                fbit *= TargetFloat(0.5);
1,207,681,786✔
1878
                        }
1879
                        blockbinary<es, bt> ebits;
1880
                        exponent(ebits);
106,269,487✔
1881
                        if constexpr (hasSubnormals) {
1882
                                if (ebits.iszero()) {
66,507,121✔
1883
                                        // subnormals: (-1)^s * 2^(2-2^(es-1)) * (f/2^fbits))
1884
                                        TargetFloat exponentiation = TargetFloat(subnormal_exponent[es]); // precomputed values for 2^(2-2^(es-1))
14,672,428✔
1885
                                        v = exponentiation * f;  // f is already f/2^fbits
14,672,428✔
1886
                                        return sign() ? -v : v;
14,672,428✔
1887
                                }
1888
                        }
1889
                        else {
1890
                                if (ebits.iszero()) { // underflow to 0
39,762,366✔
1891
                                        // compiler fast float optimization might destroy the sign
1892
                                        return sign() ? -TargetFloat(0) : TargetFloat(0);
×
1893
                                }
1894
                        }
1895
                        if constexpr (hasMaxExpValues) {
1896
                                // regular: (-1)^s * 2^(e+1-2^(es-1)) * (1 + f/2^fbits))
1897
                                int exponent = static_cast<int>(unsigned(ebits) - EXP_BIAS);
52,318,609✔
1898
                                if (-64 < exponent && exponent < 64) {
52,318,609✔
1899
                                        TargetFloat exponentiation = (exponent >= 0 ? TargetFloat(1ull << exponent) : (1.0f / TargetFloat(1ull << -exponent)));
51,995,143✔
1900
                                        v = exponentiation * (TargetFloat(1.0) + f);
51,995,143✔
1901
                                }
51,995,143✔
1902
                                else {
1903
                                        double exponentiation = ipow(exponent);
323,466✔
1904
                                        v = TargetFloat(exponentiation * (1.0 + f));
323,466✔
1905
                                }
1906
                        }
1907
                        else {
1908
                                if (ebits.all()) {
39,278,450✔
1909
                                        // max-exponent values are mapped to quiet NaNs
1910
                                        v = std::numeric_limits<TargetFloat>::quiet_NaN();
×
1911
                                        return v;
×
1912
                                }
1913
                                else {
1914
                                        // regular: (-1)^s * 2^(e+1-2^(es-1)) * (1 + f/2^fbits))
1915
                                        int exponent = static_cast<int>(unsigned(ebits) - EXP_BIAS);
39,278,450✔
1916
                                        if (-64 < exponent && exponent < 64) {
39,278,450✔
1917
                                                TargetFloat exponentiation = (exponent >= 0 ? TargetFloat(1ull << exponent) : (1.0f / TargetFloat(1ull << -exponent)));
39,077,127✔
1918
                                                v = exponentiation * (TargetFloat(1.0) + f);
39,077,127✔
1919
                                        }
39,077,127✔
1920
                                        else {
1921
                                                double exponentiation = ipow(exponent);
201,323✔
1922
                                                v = TargetFloat(exponentiation * (1.0 + f));
201,323✔
1923
                                        }
1924
                                }
1925
                        }
1926
                        v = sign() ? -v : v;
91,597,059✔
1927
                }
1928
                return v;
97,877,989✔
1929
        }
1930

1931
        // convert a cfloat to a blocktriple with the fraction format 1.ffff
1932
        // we are using the same block type so that we can use block copies to move bits around.
1933
        // Since we tend to have at least two exponent bits, this will lead to
1934
        // most cfloat<->blocktriple cases being efficient as the block types are aligned.
1935
        // The relationship between the source cfloat and target blocktriple is not
1936
        // arbitrary, enforce it by setting the blocktriple fbits to the cfloat's (nbits - es - 1)
1937
        template<typename TargetBlockType = bt>
1938
        constexpr void normalize(blocktriple<fbits, BlockTripleOperator::REP, TargetBlockType>& tgt) const {
37✔
1939
                // test special cases
1940
                if (isnan()) {
37✔
1941
                        tgt.setnan();
×
1942
                }
1943
                else if (isinf()) {
37✔
1944
                        tgt.setinf();
×
1945
                }
1946
                else if (iszero()) {
37✔
1947
                        tgt.setzero();
×
1948
                }
1949
                else {
1950
                        tgt.setnormal(); // a blocktriple is always normalized
37✔
1951
                        int scale = this->scale();
37✔
1952
                        tgt.setsign(sign());
37✔
1953
                        tgt.setscale(scale);
37✔
1954
                        // set significant
1955
                        // we are going to unify to the format 01.ffffeeee
1956
                        // where 'f' is a fraction bit, and 'e' is an extension bit
1957
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
1958
                        if (isnormal()) {
37✔
1959
                                if constexpr (fbits < 64) { // max 63 bits of fraction to yield 64bit of raw significant bits
1960
                                        uint64_t raw = fraction_ull();
37✔
1961
                                        raw |= (1ull << fbits);
37✔
1962
                                        tgt.setbits(raw);
37✔
1963
                                }
1964
                                else {
1965
                                        blockcopy(tgt);
1966
                                        tgt.setbit(fbits);
1967
                                }
1968
                        }
1969
                        else { // it is a subnormal encoding in this target cfloat
1970
                                int shift = MIN_EXP_NORMAL - scale;
×
1971
                                if constexpr (fbits < 64) {
1972
                                        uint64_t raw = fraction_ull();
×
1973
                                        raw <<= shift;
×
1974
                                        raw |= (1ull << fbits);
×
1975
                                        tgt.setbits(raw);
×
1976
                                }
1977
                                else {
1978
                                        blockcopy(tgt);
1979
                                        tgt <<= shift;
1980
                                        tgt.setbit(fbits);
1981
                                }
1982
                        }
1983
                }
1984
        }
37✔
1985

1986
        // normalize a cfloat to a blocktriple used in add/sub, which has the form 00h.fffff
1987
        // that is 3 + fbits, the 3 extra bits are required to be able to use 2's complement 
1988
        // and capture the largest value of an addition/subtraction.
1989
        // TODO: currently abits = 2*fhbits as the worst case input argument size to
1990
        // capture the smallest normal value in aligned form. There is a faster/smaller
1991
        // implementation where the input is constrainted to just the round, guard, and sticky bits.
1992
        constexpr void normalizeAddition(blocktriple<fbits, BlockTripleOperator::ADD, bt>& tgt) const {
39,479,357✔
1993
                using BlockTripleConfiguration = blocktriple<fbits, BlockTripleOperator::ADD, bt>;
1994
                // test special cases
1995
                if (isnan()) {
39,479,357✔
1996
                        tgt.setnan();
396,494✔
1997
                }
1998
                else if (isinf()) {
39,082,863✔
1999
                        tgt.setinf();
326✔
2000
                }
2001
                else if (iszero()) {
39,082,537✔
2002
                        tgt.setzero();
325,672✔
2003
                }
2004
                else {
2005
                        tgt.setnormal(); // a blocktriple is always normalized
38,756,865✔
2006
                        int scale = this->scale();
38,756,865✔
2007
                        tgt.setsign(sign());
38,756,865✔
2008
                        tgt.setscale(scale);
38,756,865✔
2009
                        // set significant
2010
                        // we are going to unify to the format 001.ffffeeee
2011
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2012
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2013
                        if (isnormal()) {
38,756,865✔
2014
                                if constexpr (fbits < 64 && BlockTripleConfiguration::rbits < (64 - fbits)) {
2015
                                        uint64_t raw = fraction_ull();
26,002,190✔
2016
                                        raw |= (1ull << fbits); // add the hidden bit
26,002,190✔
2017
                                        //std::cout << "normalize      : " << *this << '\n';
2018
                                        //std::cout << "significant    : " << to_binary(raw, fbits + 2) << '\n';
2019
                                        raw <<= BlockTripleConfiguration::rbits;  // rounding bits required for correct rounding
26,002,190✔
2020
                                        //std::cout << "rounding shift : " << to_binary(raw, fbits + 2 + BlockTripleConfiguration::rbits) << '\n';
2021
                                        tgt.setbits(raw);
26,002,190✔
2022
                                }
2023
                                else {
2024
                                        blockcopy(tgt);
962✔
2025
                                        tgt.setradix();
962✔
2026
                                        tgt.setbit(fbits); // add the hidden bit
962✔
2027
                                        tgt.bitShift(BlockTripleConfiguration::rbits);  // rounding bits required for correct rounding
962✔
2028
                                }
2029
                        }
2030
                        else {
2031
                                if (isdenormal()) { // it is a subnormal encoding in this target cfloat
12,753,713✔
2032
                                        if constexpr (hasSubnormals) {
2033
                                                if constexpr (BlockTripleConfiguration::rbits < (64 - fbits)) {
2034
                                                        uint64_t raw = fraction_ull();
6,462,058✔
2035
                                                        int shift = MIN_EXP_NORMAL - scale;
6,462,058✔
2036
                                                        raw <<= shift; // shift but do NOT add a hidden bit as the MSB of the subnormal is shifted in the hidden bit position
6,462,058✔
2037
                                                        raw <<= BlockTripleConfiguration::rbits;  // rounding bits required for correct rounding
6,462,058✔
2038
                                                        tgt.setbits(raw);
6,462,058✔
2039
                                                }
2040
                                                else {
2041
                                                        blockcopy(tgt);
2042
                                                        tgt.setradix();
2043
                                                        int shift = MIN_EXP_NORMAL - scale;
2044
                                                        tgt.bitShift(shift + BlockTripleConfiguration::rbits);  // rounding bits required for correct rounding
2045
                                                }
2046
                                        }
2047
                                        else {  // this cfloat has no subnormals
2048
                                                tgt.setzero(tgt.sign()); // preserve the sign
×
2049
                                        }
2050
                                }
2051
                                else {
2052
                                        // by design, a cfloat is either normal, subnormal, or max-exponent value, so this else clause is by deduction covering a max-exponent value
2053
                                        if constexpr (hasMaxExpValues) {
2054
                                                if constexpr (fbits < 64 && BlockTripleConfiguration::rbits < (64 - fbits)) {
2055
                                                        uint64_t raw = fraction_ull();
6,291,655✔
2056
                                                        raw |= (1ull << fbits); // add the hidden bit
6,291,655✔
2057
                                                        raw <<= BlockTripleConfiguration::rbits;  // rounding bits required for correct rounding
6,291,655✔
2058
                                                        tgt.setbits(raw);
6,291,655✔
2059
                                                }
2060
                                                else {
2061
                                                        blockcopy(tgt);
×
2062
                                                        tgt.setradix();
×
2063
                                                        tgt.setbit(fbits); // add the hidden bit
×
2064
                                                        tgt.bitShift(BlockTripleConfiguration::rbits);  // rounding bits required for correct rounding
×
2065
                                                }
2066
                                        }
2067
                                        else {  // this cfloat has no max-exponent values and thus this represents a nan, signalling or quiet determined by the sign
2068
                                                tgt.setnan(tgt.sign());
×
2069
                                        }                        
2070
                                }
2071
                        }
2072
                }
2073
                // tgt.setradix(radix);
2074
        }
39,479,357✔
2075

2076
        // Normalize a cfloat to a blocktriple used in mul, which has the form 0'00001.fffff
2077
        // that is 2*fbits, plus 1 overflow bit, and the radix set at <fbits>.
2078
        // The result radix will go to 2*fbits after multiplication.
2079
        constexpr void normalizeMultiplication(blocktriple<fbits, BlockTripleOperator::MUL, bt>& tgt) const {
13,914,390✔
2080
                // test special cases
2081
                if (isnan()) {
13,914,390✔
2082
                        tgt.setnan();
450,696✔
2083
                }
2084
                else if (isinf()) {
13,463,694✔
2085
                        tgt.setinf();
652✔
2086
                }
2087
                else if (iszero()) {
13,463,042✔
2088
                        tgt.setzero();
450,960✔
2089
                }
2090
                else {
2091
                        tgt.setnormal(); // a blocktriple is always normalized
13,012,082✔
2092
                        int scale = this->scale();
13,012,082✔
2093
                        tgt.setsign(sign());
13,012,082✔
2094
                        tgt.setscale(scale);
13,012,082✔
2095

2096
                        // set significant
2097
                        // we are going to unify to the format 01.ffffeeee
2098
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2099
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2100
                        if (isnormal() || ismaxexpvalue()) {
13,012,082✔
2101
                                if constexpr (fbits < 64) { // max 63 bits of fraction to yield 64bit of raw significant bits
2102
                                        uint64_t raw = fraction_ull();
11,793,933✔
2103
                                        raw |= (1ull << fbits);
11,793,933✔
2104
                                        tgt.setbits(raw);
11,793,933✔
2105
                                }
2106
                                else {
2107
                                        blockcopy(tgt);
832✔
2108
                                        tgt.setradix();
832✔
2109
                                        tgt.setbit(fbits); // add the hidden bit
832✔
2110
                                }
2111
                        }
2112
                        else { 
2113
                                // it is a subnormal encoding in this target cfloat
2114
                                if constexpr (hasSubnormals) {
2115
                                        if constexpr (fbits < 64) {
2116
                                                uint64_t raw = fraction_ull();
1,217,317✔
2117
                                                int shift = MIN_EXP_NORMAL - scale;
1,217,317✔
2118
                                                raw <<= shift;
1,217,317✔
2119
                                                raw |= (1ull << fbits);
1,217,317✔
2120
                                                tgt.setbits(raw);
1,217,317✔
2121
                                        }
2122
                                        else {
2123
                                                blockcopy(tgt);
×
2124
                                                int shift = MIN_EXP_NORMAL - scale;
×
2125
                                                tgt.bitShift(shift);
×
2126
                                                tgt.setbit(fbits);
×
2127
                                        }
2128
                                }
2129
                                else { // this cfloat has no subnormals
2130
                                        tgt.setzero(tgt.sign()); // preserve the sign
×
2131
                                }
2132
                        }
2133
                }
2134
                tgt.setradix(fbits); // override the radix with the input scale for accurate value printing
13,914,390✔
2135
        }
13,914,390✔
2136

2137
        // normalize a cfloat to a blocktriple used in div, which has the form 0'00000'00001.fffff
2138
        // that is 3*fbits, plus 1 overflow bit, and the radix set at <fbits>.
2139
        // the result radix will go to 2*fbits after multiplication.
2140
        // TODO: needs implementation
2141
        constexpr void normalizeDivision(blocktriple<fbits, BlockTripleOperator::DIV, bt>& tgt) const {
8,897,108✔
2142
                constexpr unsigned divshift = blocktriple<fbits, BlockTripleOperator::DIV, bt>::divshift;
8,897,108✔
2143
                // test special cases
2144
                if (isnan()) {
8,897,108✔
2145
                        tgt.setnan();
38✔
2146
                }
2147
                else if (isinf()) {
8,897,070✔
2148
                        tgt.setinf();
6✔
2149
                }
2150
                else if (iszero()) {
8,897,064✔
2151
                        tgt.setzero();
44✔
2152
                }
2153
                else {
2154
                        tgt.setnormal(); // a blocktriple is always normalized
8,897,020✔
2155
                        int scale = this->scale();
8,897,020✔
2156
                        tgt.setsign(sign());
8,897,020✔
2157
                        tgt.setscale(scale);
8,897,020✔
2158
                        // set significant
2159
                        // we are going to unify to the format 01.ffffeeee
2160
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2161
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2162
                        if (isnormal() || ismaxexpvalue()) {
8,897,020✔
2163
                                if constexpr (fbits < 64 && divshift < (64 - fbits)) {
2164
                                        uint64_t raw = fraction_ull();
7,397,221✔
2165
                                        raw |= (1ull << fbits);
7,397,221✔
2166
                                        raw <<= divshift; // shift the input value to the output radix
7,397,221✔
2167
                                        tgt.setbits(raw);
7,397,221✔
2168
                                }
2169
                                else {
2170
                                        // brute force copy of blocks
2171
                                        blockcopy(tgt);
1,054,350✔
2172
                                        tgt.setbit(fbits);
1,054,350✔
2173
                                        tgt.bitShift(divshift); // shift the input value to the output radix
1,054,350✔
2174
                                }
2175
                        }
2176
                        else { // it is a subnormal encoding in this target cfloat
2177
                                if constexpr (fbits < 64 && divshift < (64 - fbits)) {
2178
                                        uint64_t raw = fraction_ull();
445,447✔
2179
                                        int shift = MIN_EXP_NORMAL - scale;
445,447✔
2180
                                        raw <<= shift;
445,447✔
2181
                                        raw |= (1ull << fbits);
445,447✔
2182
                                        raw <<= divshift; // shift the input value to the output radix
445,447✔
2183
                                        tgt.setbits(raw);
445,447✔
2184
                                }
2185
                                else {
2186
                                        blockcopy(tgt);
2✔
2187
                                        int shift = MIN_EXP_NORMAL - scale;
2✔
2188
                                        tgt.bitShift(shift);
2✔
2189
                                        tgt.setbit(fbits);
2✔
2190
                                        tgt.bitShift(divshift); // shift the input value to the output radix
2✔
2191
                                }
2192
                        }
2193
                }
2194
                tgt.setradix(blocktriple<fbits, BlockTripleOperator::DIV, bt>::radix);
8,897,108✔
2195
        }
8,897,108✔
2196

2197
        // helper debug function
2198
        void constexprClassParameters() const noexcept {
8✔
2199
                std::cout << "-------------------------------------------------------------\n";
8✔
2200
                std::cout << "type              : " << typeid(*this).name() << '\n';
8✔
2201
                std::cout << "nbits             : " << nbits << '\n';
8✔
2202
                std::cout << "es                : " << es << std::endl;
8✔
2203
                std::cout << "hasSubnormals     : " << (hasSubnormals ? "true" : "false") << '\n';
8✔
2204
                std::cout << "hasMaxExpValues   : " << (hasMaxExpValues ? "true" : "false") << '\n';
8✔
2205
                std::cout << "isSaturating      : " << (isSaturating ? "true" : "false") << '\n';
8✔
2206
                std::cout << "ALL_ONES          : " << to_binary(ALL_ONES, 0, true) << '\n';
8✔
2207
                std::cout << "BLOCK_MASK        : " << to_binary(BLOCK_MASK, 0, true) << '\n';
8✔
2208
                std::cout << "nrBlocks          : " << nrBlocks << '\n';
8✔
2209
                std::cout << "bits in MSU       : " << bitsInMSU << '\n';
8✔
2210
                std::cout << "MSU               : " << MSU << '\n';
8✔
2211
                std::cout << "MSU MASK          : " << to_binary(MSU_MASK, 0, true) << '\n';
8✔
2212
                std::cout << "SIGN_BIT_MASK     : " << to_binary(SIGN_BIT_MASK, 0, true) << '\n';
8✔
2213
                std::cout << "LSB_BIT_MASK      : " << to_binary(LSB_BIT_MASK, 0, true) << '\n';
8✔
2214
                std::cout << "MSU CAPTURES_EXP  : " << (MSU_CAPTURES_EXP ? "yes\n" : "no\n");
8✔
2215
                std::cout << "EXP_SHIFT         : " << EXP_SHIFT << '\n';
8✔
2216
                std::cout << "MSU EXP MASK      : " << to_binary(MSU_EXP_MASK, 0, true) << '\n';
8✔
2217
                std::cout << "ALL_ONE_MASK_ES   : " << to_binary(ALL_ONES_ES) << '\n';
8✔
2218
                std::cout << "EXP_BIAS          : " << EXP_BIAS << '\n';
8✔
2219
                std::cout << "MAX_EXP           : " << MAX_EXP << '\n';
8✔
2220
                std::cout << "MIN_EXP_NORMAL    : " << MIN_EXP_NORMAL << '\n';
8✔
2221
                std::cout << "MIN_EXP_SUBNORMAL : " << MIN_EXP_SUBNORMAL << '\n';
8✔
2222
                std::cout << "fraction Blocks   : " << fBlocks << '\n';
8✔
2223
                std::cout << "bits in FSU       : " << bitsInFSU << '\n';
8✔
2224
                std::cout << "FSU               : " << FSU << '\n';
8✔
2225
                std::cout << "FSU MASK          : " << to_binary(FSU_MASK, 0, true) << '\n';
8✔
2226
                std::cout << "topfbits          : " << topfbits << '\n';
8✔
2227
                std::cout << "ALL_ONE_MASK_FR   : " << to_binary(ALL_ONES_FR) << '\n';
8✔
2228
        }
8✔
2229
        void showLimbs() const {
2230
                for (unsigned b = 0; b < nrBlocks; ++b) {
2231
                        std::cout << to_binary(_block[nrBlocks - b - 1], sizeof(bt) * 8) << ' ';
2232
                }
2233
                std::cout << '\n';
2234
        }
2235

2236
protected:
2237
        // HELPER methods
2238

2239
        /// <summary>
2240
        /// 1's complement of the encoding used to set up specific encoding patterns.
2241
        /// This is not an arithmetic operator that makes sense for floating-point numbers.
2242
        /// </summary>
2243
        /// <returns>reference to this cfloat object</returns>
2244
        constexpr cfloat& flip() noexcept { // in-place one's complement
1,270,162✔
2245
                for (unsigned i = 0; i < nrBlocks; ++i) {
4,823,069✔
2246
                        _block[i] = bt(~_block[i]);
3,552,907✔
2247
                }
2248
                _block[MSU] &= MSU_MASK; // assert precondition of properly nulled leading non-bits
1,270,162✔
2249
                return *this;
1,270,162✔
2250
        }
2251

2252
        /// <summary>
2253
        /// shift left is a bit level encoding helper for fast limb-based conversions between different cfloats
2254
        /// </summary>
2255
        /// <param name="bitsToShift"></param>
2256
        void shiftLeft(unsigned bitsToShift) {
60,129✔
2257
                if (bitsToShift == 0) return;
60,129✔
2258
                if (bitsToShift > nbits) {
60,129✔
2259
                        setzero();
×
2260
                }
2261
                if (bitsToShift >= bitsInBlock) {
60,129✔
2262
                        int blockShift = static_cast<int>(bitsToShift / bitsInBlock);
60,129✔
2263
                        for (int i = static_cast<int>(MSU); i >= blockShift; --i) {
140,250✔
2264
                                _block[i] = _block[i - blockShift];
80,121✔
2265
                        }
2266
                        for (int i = blockShift - 1; i >= 0; --i) {
340,788✔
2267
                                _block[i] = bt(0);
280,659✔
2268
                        }
2269
                        // adjust the shift
2270
                        bitsToShift -= blockShift * bitsInBlock;
60,129✔
2271
                        if (bitsToShift == 0) return;
60,129✔
2272
                }
2273
                if constexpr (MSU > 0) {
2274
                        // construct the mask for the upper bits in the block that need to move to the higher word
2275
                        bt mask = 0xFFFFFFFFFFFFFFFF << (bitsInBlock - bitsToShift);
60,083✔
2276
                        for (unsigned i = MSU; i > 0; --i) {
360,152✔
2277
                                _block[i] <<= bitsToShift;
300,069✔
2278
                                // mix in the bits from the right
2279
                                bt bits = bt(mask & _block[i - 1]);
300,069✔
2280
                                _block[i] |= (bits >> (bitsInBlock - bitsToShift));
300,069✔
2281
                        }
2282
                }
2283
                _block[0] <<= bitsToShift;
60,083✔
2284
        }
2285

2286
        // convert an unsigned integer into a cfloat
2287
        // TODO: this method does not protect against being called with a signed integer
2288
        template<typename Ty>
2289
        constexpr cfloat& convert_unsigned_integer(const Ty& rhs) noexcept {
140✔
2290
                clear();
140✔
2291
                if (0 == rhs) return *this;
140✔
2292

2293
                uint64_t raw = static_cast<uint64_t>(rhs);
137✔
2294
                int msb = static_cast<int>(find_msb(raw)) - 1; // msb > 0 due to zero test above 
137✔
2295
                int exponent = msb;
137✔
2296
                // remove the MSB as it represents the hidden bit in the cfloat representation
2297
                uint64_t hmask = ~(1ull << msb);
137✔
2298
                raw &= hmask;
137✔
2299

2300
                constexpr uint32_t sizeInBits = 8 * sizeof(Ty);
137✔
2301
                uint32_t shift = sizeInBits - exponent - 1;
137✔
2302
                raw <<= shift;
137✔
2303
                raw = round<sizeInBits, uint64_t>(raw, exponent);
137✔
2304

2305
                // construct the target cfloat
2306
                if constexpr (fbits < (64 - es)) {
2307
                        uint64_t biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(EXP_BIAS));
107✔
2308
                        uint64_t bits = 0;
107✔
2309
                        bits <<= es;
107✔
2310
                        bits |= biasedExponent;
107✔
2311
                        bits <<= fbits;
107✔
2312
                        bits |= raw;
107✔
2313
                        setbits(bits);
107✔
2314
                }
2315
                else {
2316
                        setsign(false);
30✔
2317
                        setexponent(exponent);
30✔
2318
                        // For large types, place fraction bits at the TOP of the fraction field
2319
                        // After shift, raw has fraction bits at positions (sizeInBits-2) down to (sizeInBits-1-exponent)
2320
                        // We need to place them at positions (fbits-1) down to (fbits-exponent)
2321
                        for (int i = 0; i < exponent; ++i) {
291✔
2322
                                bool bit = (raw >> (sizeInBits - 2 - i)) & 1;
261✔
2323
                                setbit(static_cast<unsigned>(fbits - 1 - i), bit);
261✔
2324
                        }
2325
                }
2326
                return *this;
137✔
2327
        }
2328
        // convert a signed integer into a cfloat
2329
        // TODO: this method does not protect against being called with a signed integer
2330
        template<typename Ty>
2331
        constexpr cfloat& convert_signed_integer(const Ty& rhs) noexcept {
174,972✔
2332
                clear();
174,972✔
2333
                if (0 == rhs) return *this;
174,972✔
2334
                bool s = (rhs < 0);
33,377✔
2335
                using UnsignedTy = std::make_unsigned_t<Ty>;
2336
                UnsignedTy urhs = static_cast<UnsignedTy>(rhs);
33,377✔
2337
                uint64_t raw = static_cast<uint64_t>(s ? (UnsignedTy(0) - urhs) : urhs);
33,377✔
2338

2339
                int msb = static_cast<int>(find_msb(raw)) - 1; // msb > 0 due to zero test above 
33,377✔
2340
                int exponent = msb;
33,377✔
2341
                // remove the MSB as it represents the hidden bit in the cfloat representation
2342
                uint64_t hmask = ~(1ull << msb);
33,377✔
2343
                raw &= hmask;
33,377✔
2344

2345
                // shift the msb to the msb of the fraction
2346
                constexpr uint32_t sizeInBits = 8 * sizeof(Ty);
33,377✔
2347
                uint32_t shift = sizeInBits - exponent - 1;
33,377✔
2348
                raw <<= shift;
33,377✔
2349
                raw = round<sizeInBits, uint64_t>(raw, exponent);
33,377✔
2350

2351
                // construct the target cfloat
2352
                if constexpr (fbits < (64 - es)) {
2353
                        uint64_t biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(EXP_BIAS));
33,006✔
2354
                        uint64_t bits = (s ? 1ull : 0ull);
33,006✔
2355
                        bits <<= es;
33,006✔
2356
                        bits |= biasedExponent;
33,006✔
2357
                        bits <<= fbits;
33,006✔
2358
                        bits |= raw;
33,006✔
2359
                        setbits(bits);
33,006✔
2360
                }
2361
                else {
2362
                        setsign(s);
371✔
2363
                        setexponent(exponent);
371✔
2364
                        // For large types, place fraction bits at the TOP of the fraction field
2365
                        // After shift, raw has fraction bits at positions (sizeInBits-2) down to (sizeInBits-1-exponent)
2366
                        // We need to place them at positions (fbits-1) down to (fbits-exponent)
2367
                        for (int i = 0; i < exponent; ++i) {
3,120✔
2368
                                bool bit = (raw >> (sizeInBits - 2 - i)) & 1;
2,749✔
2369
                                setbit(static_cast<unsigned>(fbits - 1 - i), bit);
2,749✔
2370
                        }
2371
                }
2372
                return *this;
33,377✔
2373
        }
2374

2375
public:
2376
        template<typename Real>
2377
        CONSTEXPRESSION cfloat& convert_ieee754(Real rhs) noexcept {
131,528,893✔
2378
                if constexpr (nbits == 32 && es == 8 && sizeof(Real) == 4) {
2379
                        // we CANNOT use the native conversion to float as cfloats have max-exponent values
2380
                        // which IEEE-754 does not have and thus a native conversion would destroy
2381
                        // only if the Real type is a float can we use the direct conversion
2382

2383
                        // when our cfloat is a perfect match to single precision IEEE-754
2384
                        bool s{ false };
65,749✔
2385
                        uint64_t rawExponent{ 0 };
65,749✔
2386
                        uint64_t rawFraction{ 0 };
65,749✔
2387
                        uint64_t bits{ 0 };
65,749✔
2388
                        extractFields(rhs, s, rawExponent, rawFraction, bits);
65,749✔
2389
                        if (rawExponent == ieee754_parameter<Real>::eallset) { // nan and inf need to be remapped
65,749✔
2390
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::snanmask) ||
24✔
2391
                                        rawFraction == (ieee754_parameter<Real>::fmask & (ieee754_parameter<Real>::qnanmask | ieee754_parameter<Real>::snanmask))) {
18✔
2392
                                        // 1.11111111.00000000.......00000001 signalling nan
2393
                                        // 0.11111111.00000000000000000000001 signalling nan
2394
                                        // MSVC
2395
                                        // 1.11111111.10000000.......00000001 signalling nan
2396
                                        // 0.11111111.10000000.......00000001 signalling nan
2397
                                        setnan(NAN_TYPE_SIGNALLING);
6✔
2398
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2399
                                        return *this;
6✔
2400
                                }
2401
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::qnanmask)) {
18✔
2402
                                        // 1.11111111.10000000.......00000000 quiet nan
2403
                                        // 0.11111111.10000000.......00000000 quiet nan
2404
                                        setnan(NAN_TYPE_QUIET);
5✔
2405
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2406
                                        return *this;
5✔
2407
                                }
2408
                                if (rawFraction == 0ull) {
13✔
2409
                                        // 1.11111111.0000000.......000000000 -inf
2410
                                        // 0.11111111.0000000.......000000000 +inf
2411
                                        setinf(s);
13✔
2412
                                        return *this;
13✔
2413
                                }
2414
                        }
2415
                        uint64_t raw{ s ? 1ull : 0ull };
65,725✔
2416
                        raw <<= 31;
65,725✔
2417
                        raw |= (rawExponent << fbits);
65,725✔
2418
                        raw |= rawFraction;
65,725✔
2419
                        setbits(raw);
65,725✔
2420
                        return *this;
65,725✔
2421
                }
2422
                else if constexpr (nbits == 64 && es == 11 && sizeof(Real) == 8) {
2423
                        // when our cfloat is a perfect match to double precision IEEE-754
2424
                        bool s{ false };
12,582✔
2425
                        uint64_t rawExponent{ 0 };
12,582✔
2426
                        uint64_t rawFraction{ 0 };
12,582✔
2427
                        uint64_t bits{ 0 };
12,582✔
2428
                        extractFields(rhs, s, rawExponent, rawFraction, bits);
12,582✔
2429
                        if (rawExponent == ieee754_parameter<Real>::eallset) { // nan and inf need to be remapped
12,582✔
2430
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::snanmask) ||
24✔
2431
                                        rawFraction == (ieee754_parameter<Real>::fmask & (ieee754_parameter<Real>::qnanmask | ieee754_parameter<Real>::snanmask))) {
17✔
2432
                                        // 1.11111111.00000000.......00000001 signalling nan
2433
                                        // 0.11111111.00000000000000000000001 signalling nan
2434
                                        // MSVC
2435
                                        // 1.11111111.10000000.......00000001 signalling nan
2436
                                        // 0.11111111.10000000.......00000001 signalling nan
2437
                                        setnan(NAN_TYPE_SIGNALLING);
7✔
2438
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2439
                                        return *this;
7✔
2440
                                }
2441
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::qnanmask)) {
17✔
2442
                                        // 1.11111111.10000000.......00000000 quiet nan
2443
                                        // 0.11111111.10000000.......00000000 quiet nan
2444
                                        setnan(NAN_TYPE_QUIET);
8✔
2445
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2446
                                        return *this;
8✔
2447
                                }
2448
                                if (rawFraction == 0ull) {
9✔
2449
                                        // 1.11111111.0000000.......000000000 -inf
2450
                                        // 0.11111111.0000000.......000000000 +inf
2451
                                        setinf(s);
9✔
2452
                                        return *this;
9✔
2453
                                }
2454
                        }
2455
                        // normal and subnormal handling
2456
                        uint64_t raw{ s ? 1ull : 0ull };
12,558✔
2457
                        raw <<= 63;
12,558✔
2458
                        raw |= (rawExponent << fbits);
12,558✔
2459
                        raw |= rawFraction;
12,558✔
2460
                        setbits(raw);
12,558✔
2461
                        return *this;
12,558✔
2462
                }
2463
                else {
2464
                        clear();
131,450,562✔
2465
                        // extract raw IEEE-754 bits
2466
                        bool s{ false };
131,450,562✔
2467
                        uint64_t rawExponent{ 0 };
131,450,562✔
2468
                        uint64_t rawFraction{ 0 };
131,450,562✔
2469
                        uint64_t bits{ 0 };
131,450,562✔
2470
                        extractFields(rhs, s, rawExponent, rawFraction, bits);
131,450,562✔
2471
                        // special case handling
2472
                        if (rawExponent == ieee754_parameter<Real>::eallset) { // nan and inf
131,450,562✔
2473
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::snanmask) ||
5,100,784✔
2474
                                        rawFraction == (ieee754_parameter<Real>::fmask & (ieee754_parameter<Real>::qnanmask | ieee754_parameter<Real>::snanmask))) {
2,585,179✔
2475
                                        // 1.11111111.00000000.......00000001 signalling nan
2476
                                        // 0.11111111.00000000000000000000001 signalling nan
2477
                                        // MSVC
2478
                                        // 1.11111111.10000000.......00000001 signalling nan
2479
                                        // 0.11111111.10000000.......00000001 signalling nan
2480
                                        setnan(NAN_TYPE_SIGNALLING);
2,520,908✔
2481
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2482
                                        return *this;
2,520,908✔
2483
                                }
2484
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::qnanmask)) {
2,579,876✔
2485
                                        // 1.11111111.10000000.......00000000 quiet nan
2486
                                        // 0.11111111.10000000.......00000000 quiet nan
2487
                                        setnan(NAN_TYPE_QUIET);
2,551,123✔
2488
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2489
                                        return *this;
2,551,123✔
2490
                                }
2491
                                if (rawFraction == 0ull) {
28,753✔
2492
                                        // 1.11111111.0000000.......000000000 -inf
2493
                                        // 0.11111111.0000000.......000000000 +inf
2494
                                        setinf(s);
28,729✔
2495
                                        return *this;
28,729✔
2496
                                }
2497
                        }
2498
                        if (rhs == 0.0) { // IEEE rule: this is valid for + and - 0.0
126,349,802✔
2499
                                setbit(nbits - 1ull, s);
597,328✔
2500
                                return *this;
597,328✔
2501
                        }
2502
        
2503
                        // normal number consists of fbits fraction bits and one hidden bit
2504
                        // subnormal number has no hidden bit
2505
                        int exponent = static_cast<int>(rawExponent) - ieee754_parameter<Real>::bias;  // unbias the exponent
125,752,474✔
2506

2507
                        // check special case of 
2508
                        //  1- saturating to maxpos/maxneg, or 
2509
                        //  2- projecting to +-inf 
2510
                        // if the value is out of range.
2511
                        // 
2512
                        // One problem here is that at the rounding cusps of maxpos <-> inf <-> nan
2513
                        // you need to go through the rounding logic to know which encoding you end up
2514
                        // with. 
2515
                        // For each specific cfloat configuration, you can work out these rounding cusps
2516
                        // but they need to go through the value transformation to map them back to native
2517
                        // IEEE-754. That is a complex computation to do as a static constexpr as you need
2518
                        // to construct the value, then evaluate it, and store it.
2519
                        // 
2520
                        // The algorithm used here is to check for the obvious out of range values by
2521
                        // comparing their scale to the max scale this cfloat encoding can represent.
2522
                        // For the rounding cusps, we go through the rounding logic, and then clean up
2523
                        // after rounding using the observation that no conversion from a value can ever
2524
                        // yield the NaN encoding.
2525
                        //
2526
                        // The rounding logic will correctly sort between maxpos and inf, and we clean
2527
                        // up any NaN encodings by projecting back to the configuration's saturation rule.
2528
                        //
2529
                        // We could improve on this by creating the database of rounding cusps and
2530
                        // referencing them with a direct value comparison with the input. That would be
2531
                        // the most performant implementation.
2532
                        if (exponent > MAX_EXP) {
125,752,474✔
2533
                                if constexpr (isSaturating) {
2534
                                        if (s) this->maxneg(); else this->maxpos(); // saturate to maxpos or maxneg
634,021✔
2535
                                }
2536
                                else {
2537
                                        setinf(s);
666,894✔
2538
                                }
2539
                                return *this;
1,300,915✔
2540
                        }
2541
                        if constexpr (hasSubnormals) {
2542
                                if (exponent < MIN_EXP_SUBNORMAL - 1) { 
88,953,613✔
2543
                                        // map to +-0 any values that have a scale less than (MIN_EXP_SUBMORNAL - 1)
2544
                                        this->setbit(nbits - 1, s);
143,656✔
2545
                                        return *this;
143,656✔
2546
                                }
2547
                        }
2548
                        else {
2549
                                if (exponent < MIN_EXP_NORMAL - 1) {
35,497,946✔
2550
                                        // map to +-0 any values that have a scale less than (MIN_EXP_MORNAL - 1)
2551
                                        this->setbit(nbits - 1, s);
270,877✔
2552
                                        return *this;
270,877✔
2553
                                }
2554
                        }
2555

2556
                        /////////////////  
2557
                        /// end of special case processing, move on to value sampling and rounding
2558

2559
#if TRACE_CONVERSION
2560
                        std::cout << '\n';
2561
                        std::cout << "value             : " << rhs << '\n';
2562
                        std::cout << "segments          : " << to_binary(rhs) << '\n';
2563
                        std::cout << "sign     bit      : " << (s ? '1' : '0') << '\n';
2564
                        std::cout << "exponent bits     : " << to_binary(rawExponent, ieee754_parameter<Real>::ebits, true) << '\n';
2565
                        std::cout << "fraction bits     : " << to_binary(rawFraction, ieee754_parameter<Real>::fbits, true) << '\n';
2566
                        std::cout << "exponent value    : " << exponent << '\n';
2567
#endif
2568

2569
                        // do the following scenarios have different rounding bits?
2570
                        // input is normal, cfloat is normal           <-- rounding can happen with native ieee-754 bits
2571
                        // input is normal, cfloat is subnormal
2572
                        // input is subnormal, cfloat is normal
2573
                        // input is subnormal, cfloat is subnormal
2574

2575
                        // The first condition is the relationship between the number 
2576
                        // of fraction bits from the source and the number of fraction bits 
2577
                        // in the target cfloat: these are constexpressions and guard the shifts
2578
                        // input fbits >= cfloat fbits                 <-- need to round
2579
                        // input fbits < cfloat fbits                  <-- no need to round
2580

2581
                        // quick check if we are truncating to 0 for all subnormal values
2582
                        if constexpr (!hasSubnormals) {
2583
                                if (exponent < MIN_EXP_NORMAL) {
35,227,069✔
2584
                                        setsign(s); // rest of the bits, exponent and fraction, are already set correctly
123,584✔
2585
                                        return *this;
123,584✔
2586
                                }
2587
                        }
2588
                        if constexpr (fbits < ieee754_parameter<Real>::fbits) {
2589
                                // this is the common case for cfloats that are smaller in size compared to single and double precision IEEE-754
2590
                                constexpr int rightShift = ieee754_parameter<Real>::fbits - fbits; // this is the bit shift to get the MSB of the src to the MSB of the tgt
123,654,030✔
2591
                                uint32_t biasedExponent{ 0 };
123,654,030✔
2592
                                int adjustment{ 0 }; // right shift adjustment for subnormal representation
123,654,030✔
2593
                                uint64_t mask;
2594
                                if (rawExponent != 0) {
123,654,030✔
2595
                                        // the source real is a normal number, 
2596
//                                        if (exponent >= (MIN_EXP_SUBNORMAL - 1) && exponent < MIN_EXP_NORMAL) {
2597
                                        if (exponent < MIN_EXP_NORMAL) {
123,654,011✔
2598
//                                                exponent = (exponent < MIN_EXP_SUBNORMAL ? MIN_EXP_SUBNORMAL : exponent); // clip to the smallest subnormal exponent, otherwise the adjustment is off
2599
                                                // the value is a subnormal number in this representation: biasedExponent = 0
2600
                                                // add the hidden bit to the fraction bits so the denormalization has the correct MSB
2601
                                                rawFraction |= ieee754_parameter<Real>::hmask;
41,271,466✔
2602

2603
                                                // fraction processing: we have 1 hidden + 23 explicit fraction bits 
2604
                                                // f = 1.ffff 2^exponent * 2^fbits * 2^-(2-2^(es-1)) = 1.ff...ff >> (23 - (-exponent + fbits - (2 -2^(es-1))))
2605
                                                // -exponent because we are right shifting and exponent in this range is negative
2606
                                                adjustment = -(exponent + subnormal_reciprocal_shift[es]);
41,271,466✔
2607
                                                // this is the right shift adjustment required for subnormal representation due 
2608
                                                // to the scale of the input number, i.e. the exponent of 2^-adjustment
2609
                                        }
2610
                                        else {
2611
                                                // the value is a normal number in this representation: common case
2612
                                                biasedExponent = static_cast<uint32_t>(exponent + EXP_BIAS); // project the exponent into the target 
82,382,545✔
2613
                                                // fraction processing
2614
                                                // float structure is: seee'eeee'efff'ffff'ffff'ffff'ffff'ffff, s = sign, e - exponent bit, f = fraction bit
2615
                                                // target structure is for example cfloat<8,2>: seef'ffff
2616
                                                // since both are normals, we can shift the incoming fraction to the target structure bits, and round
2617
                                                // MSB of source = 23 - 1, MSB of target = fbits - 1: shift = MSB of src - MSB of tgt => 23 - fbits
2618
                                                adjustment = 0;
82,382,545✔
2619
                                        }
2620
                                        if constexpr (rightShift > 0) {
2621
                                                // if true we need to round
2622
                                                // round-to-even logic
2623
                                                //  ... lsb | guard  round sticky   round
2624
                                                //       x     0       x     x       down
2625
                                                //       0     1       0     0       down  round to even
2626
                                                //       1     1       0     0        up   round to even
2627
                                                //       x     1       0     1        up
2628
                                                //       x     1       1     0        up
2629
                                                //       x     1       1     1        up
2630
                                                // collect lsb, guard, round, and sticky bits
2631

2632

2633
#if TRACE_CONVERSION
2634
                                                std::cout << "fraction bits     : " << to_binary(rawFraction, ieee754_parameter<Real>::nbits, true) << '\n';
2635
                                                std::cout << "lsb mask bits     : " << to_binary(mask, ieee754_parameter<Real>::nbits, true) << '\n';
2636
#endif
2637
                                                mask = (1ull << (rightShift + adjustment)); // bit mask for the lsb bit
123,654,011✔
2638
                                                bool lsb = (mask & rawFraction);
123,654,011✔
2639
                                                mask >>= 1;
123,654,011✔
2640
                                                bool guard = (mask & rawFraction);
123,654,011✔
2641
                                                mask >>= 1;
123,654,011✔
2642
                                                bool round = (mask & rawFraction);
123,654,011✔
2643
                                                if ((rightShift + adjustment) > 1) {
123,654,011✔
2644
                                                        mask = (0xFFFF'FFFF'FFFF'FFFFull << (rightShift + adjustment - 2));
123,654,011✔
2645
                                                        mask = ~mask;
123,654,011✔
2646
                                                }
2647
                                                else {
2648
                                                        mask = 0;
×
2649
                                                }
2650
#if TRACE_CONVERSION
2651
                                                std::cout << "right shift       : " << rightShift << '\n';
2652
                                                std::cout << "adjustment        : " << adjustment << '\n';
2653
                                                std::cout << "shift to LSB      : " << (rightShift + adjustment) << '\n';
2654
                                                std::cout << "fraction bits     : " << to_binary(rawFraction, ieee754_parameter<Real>::nbits, true) << '\n';
2655
                                                std::cout << "sticky mask bits  : " << to_binary(mask, ieee754_parameter<Real>::nbits, true) << '\n';
2656
#endif
2657
                                                bool sticky = (mask & rawFraction);
123,654,011✔
2658
                                                rawFraction >>= (static_cast<int64_t>(rightShift) + static_cast<int64_t>(adjustment));
123,654,011✔
2659

2660
                                                // execute rounding operation
2661
                                                if (guard) {
123,654,011✔
2662
                                                        if (lsb && (!round && !sticky)) ++rawFraction; // round to even
24,517,909✔
2663
                                                        if (round || sticky) ++rawFraction;
24,517,909✔
2664
                                                        if (rawFraction == (1ull << fbits)) { // overflow
24,517,909✔
2665
                                                                if (biasedExponent == ALL_ONES_ES) { // overflow to INF == .111..01
443,449✔
2666
                                                                        rawFraction = INF_ENCODING;
398✔
2667
                                                                }
2668
                                                                else {
2669
                                                                        ++biasedExponent;
443,051✔
2670
                                                                        rawFraction = 0;
443,051✔
2671
                                                                }
2672
                                                        }
2673
                                                }
2674
#if TRACE_CONVERSION
2675
                                                std::cout << "lsb               : " << (lsb ? "1\n" : "0\n");
2676
                                                std::cout << "guard             : " << (guard ? "1\n" : "0\n");
2677
                                                std::cout << "round             : " << (round ? "1\n" : "0\n");
2678
                                                std::cout << "sticky            : " << (sticky ? "1\n" : "0\n");
2679
                                                std::cout << "rounding decision : " << (lsb && (!round && !sticky) ? "round to even\n" : "-\n");
2680
                                                std::cout << "rounding direction: " << (round || sticky ? "round up\n" : "round down\n");
2681
#endif
2682
                                        }
2683
                                        else { // all bits of the float go into this representation and need to be shifted up, no rounding necessary
2684
                                                int shiftLeft = fbits - ieee754_parameter<Real>::fbits;
2685
                                                rawFraction <<= shiftLeft;
2686
                                        }
2687
#if TRACE_CONVERSION
2688
                                        std::cout << "biased exponent   : " << biasedExponent << " : 0x" << std::hex << biasedExponent << std::dec << '\n';
2689
                                        std::cout << "right shift       : " << rightShift << '\n';
2690
                                        std::cout << "adjustment shift  : " << adjustment << '\n';
2691
                                        std::cout << "sticky bit mask   : " << to_binary(mask, 32, true) << '\n';
2692
                                        std::cout << "fraction bits     : " << to_binary(rawFraction, 32, true) << '\n';
2693
#endif
2694
                                        // construct the target cfloat
2695
                                        bits = (s ? 1ull : 0ull);
123,654,011✔
2696
                                        bits <<= es;
123,654,011✔
2697
                                        bits |= biasedExponent;
123,654,011✔
2698
                                        bits <<= fbits;
123,654,011✔
2699
                                        bits |= rawFraction;
123,654,011✔
2700
#if TRACE_CONVERSION
2701
                                        std::cout << "sign bit          : " << (s ? '1' : '0') << '\n';
2702
                                        std::cout << "biased exponent   : " << biasedExponent << " : 0x" << std::hex << biasedExponent << std::dec << '\n';
2703
                                        std::cout << "fraction bits     : " << to_binary(rawFraction, 32, true) << '\n';
2704
                                        std::cout << "cfloat bits       : " << to_binary(bits, nbits, true) << '\n';
2705
#endif
2706
                                        setbits(bits);
123,654,011✔
2707
                                }
2708
                                else {
2709
                                        // the source real is a subnormal number                                
2710
                                        mask = 0x00FF'FFFFu >> (fbits + exponent + subnormal_reciprocal_shift[es] + 1); // mask for sticky bit 
19✔
2711

2712
                                        // fraction processing: we have fbits+1 bits = 1 hidden + fbits explicit fraction bits 
2713
                                        // f = 1.ffff  2^exponent * 2^fbits * 2^-(2-2^(es-1)) = 1.ff...ff >> (23 - (-exponent + fbits - (2 -2^(es-1))))
2714
                                        // -exponent because we are right shifting and exponent in this range is negative
2715
                                        adjustment = -(exponent + subnormal_reciprocal_shift[es]); // this is the right shift adjustment due to the scale of the input number, i.e. the exponent of 2^-adjustment
19✔
2716
#if TRACE_CONVERSION                                        
2717
                                        std::cout << "source is subnormal: TBD\n";
2718
                                        std::cout << "shift to LSB    " << (rightShift + adjustment) << '\n';
2719
                                        std::cout << "adjustment      " << adjustment << '\n';
2720
                                        std::cout << "exponent        " << exponent << '\n';
2721
                                        std::cout << "subnormal shift " << subnormal_reciprocal_shift[es] << '\n';
2722
#endif
2723
                                        if (exponent >= (MIN_EXP_SUBNORMAL - 1) && exponent < MIN_EXP_NORMAL) {
2724
                                                // the value is a subnormal number in this representation
2725
                                        }
2726
                                        else {
2727
                                                // the value is a normal number in this representation
2728
                                        }
2729
                                }
2730
                        }
2731
                        else {
2732
                                // no need to round, but we need to shift left to deliver the bits
2733
                                // cfloat<40,  8> = float
2734
                                // cfloat<48,  9> = float
2735
                                // cfloat<56, 10> = float
2736
                                // cfloat<64, 11> = float
2737
                                // cfloat<64, 10> = double 
2738
                                // can we go from an input subnormal to a cfloat normal? 
2739
                                // yes, for example a cfloat<64,11> assigned to a subnormal float
2740

2741
                                // map exponent into target cfloat encoding
2742
                                uint64_t biasedExponent = static_cast<uint64_t>(static_cast<int64_t>(exponent) + EXP_BIAS);
259,412✔
2743
                                constexpr int upshift = fbits - ieee754_parameter<Real>::fbits;
259,412✔
2744
                                // output processing
2745
                                if constexpr (nbits < 65) {
2746
                                        // we can compose the bits in a native 64-bit unsigned integer
2747
                                        // common case: normal to normal
2748
                                        // reference example: nbits = 40, es = 8, fbits = 31: rhs = float fbits = 23; shift left by (31 - 23) = 8
2749

2750
                                        if (rawExponent != 0) {
199,166✔
2751
                                                // rhs is a normal encoding
2752
                                                uint64_t raw{ s ? 1ull : 0ull };
198,421✔
2753
                                                raw <<= es;
198,421✔
2754
                                                raw |= biasedExponent;
198,421✔
2755
                                                raw <<= fbits;
198,421✔
2756
                                                rawFraction <<= upshift;
198,421✔
2757
                                                raw |= rawFraction;
198,421✔
2758
                                                setbits(raw);
198,421✔
2759
                                        }
2760
                                        else {
2761
                                                // rhs is a subnormal
2762
        //                                        std::cerr << "rhs is a subnormal : " << to_binary(rhs) << " : " << rhs << '\n';
2763
                                                // we need to calculate the effective scale to see 
2764
                                                // if this value becomes a normal, or maps to a subnormal encoding
2765
                                                // in this target format
2766
                                        }
2767
                                }
2768
                                else {
2769
                                        // we need to write and shift bits into place
2770
                                        // use cases are cfloats like cfloat<80, 11, bt>
2771
                                        // even though the bits that come in are single or double precision
2772
                                        // we need to write the fields and then shifting them in place
2773
                                        // 
2774
                                        // common case: normal to normal
2775
                                        if constexpr (bitsInBlock < 64) {
2776
                                                if (rawExponent != 0) {
60,246✔
2777
                                                        // reference example: nbits = 128, es = 15, fbits = 112: rhs = float: shift left by (112 - 23) = 89
2778
                                                        setbits(biasedExponent);
60,129✔
2779
                                                        shiftLeft(fbits);
60,129✔
2780
                                                        bt fractionBlock[nrBlocks]{ 0 };
60,129✔
2781
                                                        // copy fraction bits
2782
                                                        unsigned blocksRequired = (8 * sizeof(rawFraction) + 1) / bitsInBlock;
60,129✔
2783
                                                        unsigned maxBlockNr = (blocksRequired < nrBlocks ? blocksRequired : nrBlocks);
60,129✔
2784
                                                        uint64_t mask = static_cast<uint64_t>(ALL_ONES); // set up the block mask
60,129✔
2785
                                                        unsigned shift = 0;
60,129✔
2786
                                                        for (unsigned i = 0; i < maxBlockNr; ++i) {
340,163✔
2787
                                                                fractionBlock[i] = bt((mask & rawFraction) >> shift);
280,034✔
2788
                                                                mask <<= bitsInBlock;
280,034✔
2789
                                                                shift += bitsInBlock;
280,034✔
2790
                                                        }
2791
                                                        // shift fraction bits
2792
                                                        int bitsToShift = upshift;
60,129✔
2793
                                                        if (bitsToShift >= static_cast<int>(bitsInBlock)) {
60,129✔
2794
                                                                int blockShift = static_cast<int>(bitsToShift / bitsInBlock);
50,113✔
2795
                                                                for (int i = MSU; i >= blockShift; --i) {
270,568✔
2796
                                                                        fractionBlock[i] = fractionBlock[i - blockShift];
220,455✔
2797
                                                                }
2798
                                                                for (int i = blockShift - 1; i >= 0; --i) {
160,390✔
2799
                                                                        fractionBlock[i] = bt(0);
110,277✔
2800
                                                                }
2801
                                                                // adjust the shift
2802
                                                                bitsToShift -= blockShift * bitsInBlock;
50,113✔
2803
                                                        }
2804
                                                        if (bitsToShift > 0) {
60,129✔
2805
                                                                // construct the mask for the upper bits in the block that need to move to the higher word
2806
                                                                bt bitsToMoveMask = bt(ALL_ONES << (bitsInBlock - bitsToShift));
40,099✔
2807
                                                                for (unsigned i = MSU; i > 0; --i) {
210,465✔
2808
                                                                        fractionBlock[i] <<= bitsToShift;
170,366✔
2809
                                                                        // mix in the bits from the right
2810
                                                                        bt fracbits = static_cast<bt>(bitsToMoveMask & fractionBlock[i - 1]); // operator & yields an int
170,366✔
2811
                                                                        fractionBlock[i] |= (fracbits >> (bitsInBlock - bitsToShift));
170,366✔
2812
                                                                }
2813
                                                                fractionBlock[0] <<= bitsToShift;
40,099✔
2814
                                                        }
2815
                                                        // OR the bits in
2816
                                                        for (unsigned i = 0; i <= MSU; ++i) {
420,909✔
2817
                                                                _block[i] |= fractionBlock[i];
360,780✔
2818
                                                        }
2819
                                                        // enforce precondition for fast comparison by properly nulling bits that are outside of nbits
2820
                                                        _block[MSU] &= MSU_MASK;
60,129✔
2821
                                                        // finally, set the sign bit
2822
                                                        setsign(s);
60,129✔
2823
                                                }
2824
                                                else {
2825
                                                        // rhs is a subnormal
2826
                //                                        std::cerr << "rhs is a subnormal : " << to_binary(rhs) << " : " << rhs << '\n';
2827
                                                }
2828
                                        }
2829
                                        else {
2830
                                                // BlockType is incorrect
2831
                                        }
2832
                                }
2833
                        }
2834
                        // post-processing results to implement saturation and projection after rounding logic
2835
                        if constexpr (isSaturating) {
2836
                                if (isinf(INF_TYPE_POSITIVE) || isnan(NAN_TYPE_QUIET)) {
17,112,997✔
2837
                                        maxpos();
587✔
2838
                                }
2839
                                else if (isinf(INF_TYPE_NEGATIVE) || isnan(NAN_TYPE_SIGNALLING)) {
17,112,410✔
2840
                                        maxneg();
587✔
2841
                                }
2842
                        }
2843
                        else {
2844
                                if (isnan(NAN_TYPE_QUIET)) {
106,800,445✔
2845
                                        setinf(false);
301,221✔
2846
                                }
2847
                                else if (isnan(NAN_TYPE_SIGNALLING)) {
106,499,224✔
2848
                                        setinf(true);
300,173✔
2849
                                }
2850
                        }
2851
                        return *this;  // TODO: unreachable in some configurations        
123,913,442✔
2852
                }
2853
        }
2854

2855
        // post-processing results to implement saturation and projection after rounding logic
2856
        // arithmetic bit operations can't produce NaN encodings, so we need to re-interpret
2857
        // these encodings and 'project' them to the proper values.
2858
        void constexpr post_process() noexcept {
2859
                if constexpr (isSaturating) {
2860
                        if (isinf(INF_TYPE_POSITIVE) || isnan(NAN_TYPE_QUIET)) {
2861
                                maxpos();
2862
                        }
2863
                        else if (isinf(INF_TYPE_NEGATIVE) || isnan(NAN_TYPE_SIGNALLING)) {
2864
                                maxneg();
2865
                        }
2866
                }
2867
                else {
2868
                        if (isnan(NAN_TYPE_QUIET)) {
2869
                                setinf(false);
2870
                        }
2871
                        else if (isnan(NAN_TYPE_SIGNALLING)) {
2872
                                setinf(true);
2873
                        }
2874
                }
2875
        }
2876

2877
protected:
2878

2879
        /// <summary>
2880
        /// round a set of source bits to the present representation.
2881
        /// srcbits is the number of bits of significant in the source representation
2882
        /// </summary>
2883
        /// <typeparam name="StorageType"></typeparam>
2884
        /// <param name="raw"></param>
2885
        /// <returns></returns>
2886
        template<unsigned srcbits, typename StorageType>
2887
        constexpr uint64_t round(StorageType raw, int& exponent) noexcept {
33,514✔
2888
                if constexpr (fhbits < srcbits) {
2889
                        // round to even: lsb guard round sticky
2890
                    // collect guard, round, and sticky bits
2891
                    // this same logic will work for the case where
2892
                    // we only have a guard bit and no round and sticky bits
2893
                    // because the mask logic will make round and sticky both 0
2894
                        constexpr uint32_t shift = srcbits - fhbits - 1ull;
30,530✔
2895
                        StorageType mask = (StorageType(1ull) << shift);
30,530✔
2896
                        bool guard = (mask & raw);
30,530✔
2897
                        mask >>= 1;
30,530✔
2898
                        bool round = (mask & raw);
30,530✔
2899
                        if constexpr (shift > 1u) { // protect against a negative shift
2900
                                StorageType allones(StorageType(~0));
30,530✔
2901
                                mask = StorageType(allones << (shift - 2));
30,530✔
2902
                                mask = ~mask;
30,530✔
2903
                        }
2904
                        else {
2905
                                mask = 0;
2906
                        }
2907
                        bool sticky = (mask & raw);
30,530✔
2908

2909
                        raw >>= (shift + 1);  // shift out the bits we are rounding away
30,530✔
2910
                        bool lsb = (raw & 0x1u);
30,530✔
2911
                        //  ... lsb | guard  round sticky   round
2912
                        //       x     0       x     x       down
2913
                        //       0     1       0     0       down  round to even
2914
                        //       1     1       0     0        up   round to even
2915
                        //       x     1       0     1        up
2916
                        //       x     1       1     0        up
2917
                        //       x     1       1     1        up
2918
                        if (guard) {
30,530✔
2919
                                if (lsb && (!round && !sticky)) ++raw; // round to even
3,168✔
2920
                                if (round || sticky) ++raw;
3,168✔
2921
                                if (raw == (1ull << fbits)) { // overflow
3,168✔
2922
                                        ++exponent;
3,168✔
2923
                                        raw >>= 1u;
3,168✔
2924
                                }
2925
                        }
2926
                }
2927
                else {
2928
                        // Target has more precision than source - need to left-shift to align
2929
                        // For large types where fhbits > 64, the fraction bits cannot fit in
2930
                        // a 64-bit raw after shifting. In this case, skip the shift and let
2931
                        // convert_signed/unsigned_integer place bits using setbit().
2932
                        // The caller positions fraction bits at the top of srcbits, and we
2933
                        // need to ensure they don't overflow 64 bits after our shift.
2934
                        constexpr unsigned shift = fhbits - srcbits;
2,984✔
2935
                        if constexpr (fhbits <= 64 && shift < 64) {
2936
                                raw <<= shift;
2,583✔
2937
                        }
2938
                        // else: raw stays as-is; caller will extract bits and place them
2939
                }
2940
                uint64_t significant = raw;
33,514✔
2941
                return significant;
33,514✔
2942
        }
2943

2944
        template<typename ArgumentBlockType>
2945
        constexpr void copyBits(ArgumentBlockType v) {
2946
                unsigned blocksRequired = (8 * sizeof(v) + 1 ) / bitsInBlock;
2947
                unsigned maxBlockNr = (blocksRequired < nrBlocks ? blocksRequired : nrBlocks);
2948
                bt b{ 0ul }; b = bt(~b);
2949
                ArgumentBlockType mask = ArgumentBlockType(b);
2950
                unsigned shift = 0;
2951
                for (unsigned i = 0; i < maxBlockNr; ++i) {
2952
                        _block[i] = bt((mask & v) >> shift);
2953
                        mask <<= bitsInBlock;
2954
                        shift += bitsInBlock;
2955
                }
2956
        }
2957
        void shiftLeft(int leftShift) {
2958
                if (leftShift == 0) return;
2959
                if (leftShift < 0) return shiftRight(-leftShift);
2960
                if (leftShift > long(nbits)) leftShift = nbits; // clip to max
2961
                if (leftShift >= long(bitsInBlock)) {
2962
                        int blockShift = leftShift / static_cast<int>(bitsInBlock);
2963
                        for (signed i = signed(MSU); i >= blockShift; --i) {
2964
                                _block[i] = _block[i - blockShift];
2965
                        }
2966
                        for (signed i = blockShift - 1; i >= 0; --i) {
2967
                                _block[i] = bt(0);
2968
                        }
2969
                        // adjust the shift
2970
                        leftShift -= (long)(blockShift * bitsInBlock);
2971
                        if (leftShift == 0) return;
2972
                }
2973
                // construct the mask for the upper bits in the block that need to move to the higher word
2974
//                bt mask = static_cast<bt>(0xFFFFFFFFFFFFFFFFull << (bitsInBlock - leftShift));
2975
                bt mask = ALL_ONES;
2976
                mask <<= (bitsInBlock - leftShift);
2977
                for (unsigned i = MSU; i > 0; --i) {
2978
                        _block[i] <<= leftShift;
2979
                        // mix in the bits from the right
2980
                        bt bits = static_cast<bt>(mask & _block[i - 1]);
2981
                        _block[i] |= (bits >> (bitsInBlock - leftShift));
2982
                }
2983
                _block[0] <<= leftShift;
2984
        }
2985
        void shiftRight(int rightShift) {
2986
                if (rightShift == 0) return;
2987
                if (rightShift < 0) return shiftLeft(-rightShift);
2988
                if (rightShift >= long(nbits)) {
2989
                        setzero();
2990
                        return;
2991
                }
2992
                bool signext = sign();
2993
                unsigned blockShift = 0;
2994
                if (rightShift >= long(bitsInBlock)) {
2995
                        blockShift = rightShift / bitsInBlock;
2996
                        if (MSU >= blockShift) {
2997
                                // shift by blocks
2998
                                for (unsigned i = 0; i <= MSU - blockShift; ++i) {
2999
                                        _block[i] = _block[i + blockShift];
3000
                                }
3001
                        }
3002
                        // adjust the shift
3003
                        rightShift -= (long)(blockShift * bitsInBlock);
3004
                        if (rightShift == 0) {
3005
                                // fix up the leading zeros if we have a negative number
3006
                                if (signext) {
3007
                                        // rightShift is guaranteed to be less than nbits
3008
                                        rightShift += (long)(blockShift * bitsInBlock);
3009
                                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3010
                                                this->setbit(i);
3011
                                        }
3012
                                }
3013
                                else {
3014
                                        // clean up the blocks we have shifted clean
3015
                                        rightShift += (long)(blockShift * bitsInBlock);
3016
                                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3017
                                                this->setbit(i, false);
3018
                                        }
3019
                                }
3020
                                return;  // shift was aligned to block boundary, no per-bit shift needed
3021
                        }
3022
                }
3023

3024
                bt mask = ALL_ONES;
3025
                mask >>= (bitsInBlock - rightShift); // this is a mask for the lower bits in the block that need to move to the lower word
3026
                for (unsigned i = 0; i < MSU; ++i) {  // TODO: can this be improved? we should not have to work on the upper blocks in case we block shifted
3027
                        _block[i] >>= rightShift;
3028
                        // mix in the bits from the left
3029
                        bt bits = static_cast<bt>(mask & _block[i + 1]); // & operator returns an int
3030
                        _block[i] |= (bits << (bitsInBlock - rightShift));
3031
                }
3032
                _block[MSU] >>= rightShift;
3033

3034
                // fix up the leading zeros if we have a negative number
3035
                if (signext) {
3036
                        // bitsToShift is guaranteed to be less than nbits
3037
                        rightShift += (long)(blockShift * bitsInBlock);
3038
                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3039
                                this->setbit(i);
3040
                        }
3041
                }
3042
                else {
3043
                        // clean up the blocks we have shifted clean
3044
                        rightShift += (long)(blockShift * bitsInBlock);
3045
                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3046
                                this->setbit(i, false);
3047
                        }
3048
                }
3049

3050
                // enforce precondition for fast comparison by properly nulling bits that are outside of nbits
3051
                _block[MSU] &= MSU_MASK;
3052
        }
3053

3054
        // calculate the integer power 2 ^ b using exponentiation by squaring
3055
        double ipow(int exponent) const {
524,789✔
3056
                bool negative = (exponent < 0);
524,789✔
3057
                exponent = negative ? -exponent : exponent;
524,789✔
3058
                double result(1.0);
524,789✔
3059
                double base = 2.0;
524,789✔
3060
                for (;;) {
3061
                        if (exponent % 2) result *= base;
3,845,172✔
3062
                        exponent >>= 1;
3,845,172✔
3063
                        if (exponent == 0) break;
3,845,172✔
3064
                        base *= base;
3,320,383✔
3065
                }
3066
                return (negative ? (1.0 / result) : result);
524,789✔
3067
        }
3068

3069
        template<BlockTripleOperator btop, typename TargetBlockType = bt>
3070
        constexpr void blockcopy(blocktriple<fbits, btop, TargetBlockType>& tgt) const {
1,056,146✔
3071
                // brute force copy of blocks
3072
                if constexpr (1 == fBlocks) {
3073
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0] & FSU_MASK));
1,050,746✔
3074
                }
3075
                else if constexpr (2 == fBlocks) {
3076
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
3,044✔
3077
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1] & FSU_MASK));
3,044✔
3078
                }
3079
                else if constexpr (3 == fBlocks) {
3080
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
202✔
3081
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
202✔
3082
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2] & FSU_MASK));
202✔
3083
                }
3084
                else if constexpr (4 == fBlocks) {
3085
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
1,438✔
3086
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
1,438✔
3087
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
1,438✔
3088
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3] & FSU_MASK));
1,438✔
3089
                }
3090
                else if constexpr (5 == fBlocks) {
NEW
3091
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
×
NEW
3092
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
×
NEW
3093
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
×
NEW
3094
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
×
NEW
3095
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4] & FSU_MASK));
×
3096
                }
3097
                else if constexpr (6 == fBlocks) {
3098
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
3099
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
3100
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
3101
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
3102
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4]));
3103
                        tgt.setblock(5, static_cast<TargetBlockType>(_block[5] & FSU_MASK));
3104
                }
3105
                else if constexpr (7 == fBlocks) {
3106
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
8✔
3107
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
8✔
3108
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
8✔
3109
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
8✔
3110
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4]));
8✔
3111
                        tgt.setblock(5, static_cast<TargetBlockType>(_block[5]));
8✔
3112
                        tgt.setblock(6, static_cast<TargetBlockType>(_block[6] & FSU_MASK));
8✔
3113
                }
3114
                else if constexpr (8 == fBlocks) {
3115
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
356✔
3116
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
356✔
3117
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
356✔
3118
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
356✔
3119
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4]));
356✔
3120
                        tgt.setblock(5, static_cast<TargetBlockType>(_block[5]));
356✔
3121
                        tgt.setblock(6, static_cast<TargetBlockType>(_block[6]));
356✔
3122
                        tgt.setblock(7, static_cast<TargetBlockType>(_block[7] & FSU_MASK));
356✔
3123
                }
3124
                else {
3125
                        for (unsigned i = 0; i < FSU; ++i) {
3,528✔
3126
                                tgt.setblock(i, static_cast<TargetBlockType>(_block[i]));
3,176✔
3127
                        }
3128
                        tgt.setblock(FSU, static_cast<TargetBlockType>(_block[FSU] & FSU_MASK));
352✔
3129
                }
3130
        }
1,056,146✔
3131

3132
private:
3133
        bt _block[nrBlocks];
3134

3135
        //////////////////////////////////////////////////////////////////////////////
3136
        // friend functions
3137

3138
        // template parameters need names different from class template parameters (for gcc and clang)
3139
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3140
        friend std::ostream& operator<< (std::ostream& ostr, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& r);
3141
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3142
        friend std::istream& operator>> (std::istream& istr, cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& r);
3143

3144
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3145
        friend bool operator==(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3146
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3147
        friend bool operator!=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3148
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3149
        friend bool operator< (const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3150
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3151
        friend bool operator> (const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3152
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3153
        friend bool operator<=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3154
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3155
        friend bool operator>=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3156
};
3157

3158
///////////////////////////// IOSTREAM operators ///////////////////////////////////////////////
3159

3160
// convert cfloat to decimal fixpnt string, i.e. "-1234.5678"
3161
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3162
std::string to_decimal_fixpnt_string(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& value, long long precision) {
86✔
3163
        constexpr unsigned fbits = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits;
86✔
3164
        constexpr unsigned bias = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::EXP_BIAS;
86✔
3165
        std::stringstream str;
86✔
3166
        if (value.iszero()) {
86✔
3167
                str << '0';
1✔
3168
                return str.str();
1✔
3169
        }
3170
        if (value.sign()) str << '-';
85✔
3171

3172
        // construct the discretization levels of the fraction part
3173
        support::decimal range, discretizationLevels, step;
85✔
3174
        // create the decimal range we are discretizing
3175
        range.setdigit(1);
85✔
3176
        range.shiftLeft(fbits); // the decimal range of the fraction
85✔
3177
        discretizationLevels.powerOf2(fbits); // calculate the discretization levels of this range
85✔
3178
        step = div(range, discretizationLevels);
85✔
3179
        // now construct the value of this range by adding the fraction samples
3180
        support::decimal partial, multiplier;
85✔
3181
        partial.setzero();  // if you just want the fraction
85✔
3182
        multiplier.setdigit(1);
85✔
3183
        // convert the fraction part
3184
        for (unsigned i = 0; i < fbits; ++i) {
1,130✔
3185
                if (value.at(i)) {
1,045✔
3186
                        support::add(partial, multiplier);
50✔
3187
                }
3188
                support::add(multiplier, multiplier);
1,045✔
3189
        }
3190
        if (value.isdenormal()) {
85✔
3191
                support::mul(partial, step);
10✔
3192
                support::decimal scale;
10✔
3193
                scale.powerOf2(bias - 1ull);
10✔
3194
                partial = support::div(partial, scale);
10✔
3195
        } 
10✔
3196
        else {
3197
                support::add(partial, multiplier); // add the hidden bit
75✔
3198
                support::mul(partial, step);
75✔
3199
                support::decimal scale;
75✔
3200
                int exponent = value.scale();
75✔
3201
                if (exponent < 0) {
75✔
3202
                        scale.powerOf2(static_cast<unsigned>(-exponent));
28✔
3203
                        partial = support::div(partial, scale);
28✔
3204
                }
3205
                else {
3206
                        scale.powerOf2(static_cast<unsigned>(exponent));
47✔
3207
                        support::mul(partial, scale);
47✔
3208
                }
3209
        }
75✔
3210

3211
        // the radix is at fbits
3212
        // The partial represents the parts in the range, so we can deduce
3213
        // the number of leading zeros by comparing to the length of range
3214
        int nrLeadingZeros = static_cast<int>(range.size()) - static_cast<int>(partial.size()) - 1;
85✔
3215
        if (nrLeadingZeros >= 0) str << "0.";
85✔
3216
        for (int i = 0; i < nrLeadingZeros; ++i) str << '0';
189✔
3217
        int digitsWritten = (nrLeadingZeros > 0) ? nrLeadingZeros : 0;
85✔
3218
        int position = static_cast<int>(partial.size()) - 1;
85✔
3219
        for (support::decimal::const_reverse_iterator rit = partial.rbegin(); rit != partial.rend(); ++rit) {
1,131✔
3220
                str << (int)*rit;
1,046✔
3221
                ++digitsWritten;
1,046✔
3222
                if (position == fbits) str << '.';
1,046✔
3223
                --position;
1,046✔
3224
        }
3225
        if (digitsWritten < precision) { // deal with trailing 0s
85✔
3226
                for (unsigned i = static_cast<unsigned>(digitsWritten); i < fbits; ++i) {
×
3227
                        str << '0';
×
3228
                }
3229
        }
3230

3231
        return str.str();
85✔
3232
}
86✔
3233

3234
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3235
std::string to_string(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& value, long long precision) {
3236
        constexpr unsigned fbits = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits;
3237
        std::stringstream str;
3238
        if (value.iszero()) {
3239
                str << '0';
3240
                return str.str();
3241
        }
3242
        if (value.sign()) str << '-';
3243

3244
        // denormalize the number to gain access to the most sigificant digits
3245
        // 1.ffff^e
3246
        // scale is e
3247
        // lsbScale is e - fbits
3248
        // shift to get lsb to position 2^0 = (e - fbits)
3249
        std::int64_t scale = value.scale();
3250
//        std::int64_t shift = scale + fbits; // we want the lsb at 2^0
3251
        std::int64_t lsbScale = scale - fbits;  // scale of the lsb
3252
        support::decimal partial, multiplier;
3253
        partial.setzero();
3254

3255
        multiplier.powerOf2(lsbScale);
3256

3257
        // convert the fraction bits 
3258
        for (unsigned i = 0; i < fbits; ++i) {
3259
                if (value.at(i)) {
3260
                        support::add(partial, multiplier);
3261
                }
3262
                support::add(multiplier, multiplier);
3263
        }
3264
        if (!value.isdenormal()) {
3265
                support::add(partial, multiplier); // add the hidden bit
3266
        }
3267
        str << partial;
3268
        return str.str();
3269
}
3270

3271
//////////////////////////////////////////////////////////////////////////////////////////////
3272
/// stream operators
3273

3274
// ostream output generates an ASCII format for the floating-point argument
3275
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3276
inline std::ostream& operator<<(std::ostream& ostr, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& v) {
3,147✔
3277
        std::streamsize precision = ostr.precision();
3,147✔
3278
        std::streamsize width = ostr.width();
3,147✔
3279

3280
        std::ios_base::fmtflags ff = ostr.flags();
3,147✔
3281
        // extract the format flags that change the representation
3282
        bool scientific = (ff & std::ios_base::scientific) == std::ios_base::scientific;
3,147✔
3283
        bool fixed      = !scientific && (ff & std::ios_base::fixed);
3,147✔
3284

3285
        std::string representation;
3,147✔
3286
        if (fixed) {
3,147✔
3287
                representation = to_decimal_fixpnt_string(v, precision);
86✔
3288
        }
3289
        else {
3290
                std::stringstream ss;
3,061✔
3291
                ss << std::setprecision(precision) << double(v);  // TODO: make this native
3,061✔
3292
                representation = ss.str();
3,061✔
3293
//                representation = to_string(v, precision);
3294
        }
3,061✔
3295

3296
        // implement setw and left/right operators
3297
        std::streamsize repWidth = static_cast<std::streamsize>(representation.size());
3,147✔
3298
        if (width > repWidth) {
3,147✔
3299
                std::streamsize diff = width - static_cast<std::streamsize>(representation.size());
1,701✔
3300
                char fill = ostr.fill();
1,701✔
3301
                if ((ff & std::ios_base::left) == std::ios_base::left) {
1,701✔
3302
                        representation.append(static_cast<unsigned>(diff), fill);
95✔
3303
                }
3304
                else {
3305
                        representation.insert(0ull, static_cast<unsigned>(diff), fill);
1,606✔
3306
                }
3307
        }
3308

3309
        return ostr << representation;
6,294✔
3310
}
3,147✔
3311

3312
// istream input: currently marshalling through native double
3313
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3314
inline std::istream& operator>>(std::istream& istr, cfloat<nbits,es,bt,hasSubnormals,hasMaxExpValues,isSaturating>& v) {
10✔
3315
        double d(0.0);
10✔
3316
        istr >> d;
10✔
3317
        v = d;
10✔
3318
        return istr;
10✔
3319
}
3320

3321
// encoding helpers
3322

3323
// return the Unit in the Last Position
3324
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3325
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ulp(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& a) {
49✔
3326
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> b(a);
49✔
3327
        return ++b - a;
86✔
3328
}
3329

3330
// transform cfloat to a binary representation
3331
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3332
inline std::string to_binary(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& number, bool nibbleMarker = false) {
1,551✔
3333
        std::stringstream s;
1,551✔
3334
        s << "0b";
1,551✔
3335
        unsigned index = nbits;
1,551✔
3336
        s << (number.at(--index) ? '1' : '0') << '.';
1,551✔
3337

3338
        for (int i = int(es) - 1; i >= 0; --i) {
10,975✔
3339
                s << (number.at(--index) ? '1' : '0');
9,424✔
3340
                if (i > 0 && (i % 4) == 0 && nibbleMarker) s << '\'';
9,424✔
3341
        }
3342

3343
        s << '.';
1,551✔
3344

3345
        constexpr int fbits = nbits - 1ull - es;
1,551✔
3346
        for (int i = fbits - 1; i >= 0; --i) {
34,585✔
3347
                s << (number.at(--index) ? '1' : '0');
33,034✔
3348
                if (i > 0 && (i % 4) == 0 && nibbleMarker) s << '\'';
33,034✔
3349
        }
3350

3351
        return s.str();
3,102✔
3352
}
1,551✔
3353

3354
// transform a cfloat into a triple representation
3355
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3356
inline std::string to_triple(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& number, bool nibbleMarker = true) {
3✔
3357
        std::stringstream s;
3✔
3358
        blocktriple<cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits, BlockTripleOperator::REP, bt> triple;
3✔
3359
        number.normalize(triple);
3✔
3360
        s << to_triple(triple, nibbleMarker);
3✔
3361
        return s.str();
6✔
3362
}
3✔
3363

3364
// Magnitude of a cfloat (equivalent to turning the sign bit off).
3365
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3366
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>
3367
abs(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& v) {
16,952✔
3368
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> a(v);
16,952✔
3369
        a.setsign(false);
16,952✔
3370
        return a;
16,952✔
3371
}
3372

3373
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3374
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>
3375
fabs(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> v) {
40✔
3376
        return abs(v);
40✔
3377
}
3378

3379
////////////////////// debug helpers
3380

3381
// convenience method to gain access to the values of the constexpr variables that govern the cfloat behavior
3382
template<unsigned nbits, unsigned es, typename bt = uint8_t, bool hasSubnormals = false, bool hasMaxExpValues = false, bool isSaturating = false>
3383
void ReportCfloatClassParameters() {
3384
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> a;
3385
        a.constexprClassParameters();
3386
}
3387

3388
//////////////////////////////////////////////////////
3389
/// cfloat - cfloat binary logic operators
3390

3391
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3392
inline bool operator==(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) {
120,418,158✔
3393
        if (lhs.isnan() || rhs.isnan()) return false;
120,418,158✔
3394
        for (unsigned i = 0; i < lhs.nrBlocks; ++i) {
362,308,084✔
3395
                if (lhs._block[i] != rhs._block[i]) {
245,771,293✔
3396
                        return false;
2,226,453✔
3397
                }
3398
        }
3399
        return true;
116,536,791✔
3400
}
3401
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3402
inline bool operator!=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) { return !operator==(lhs, rhs); }
119,020,020✔
3403
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3404
inline bool operator< (const cfloat<nnbits, nes, nbt, nsub, nsup, nsat>& lhs, const cfloat<nnbits, nes, nbt, nsub, nsup, nsat>& rhs) {
5,568,897✔
3405
        if (lhs.isnan() || rhs.isnan()) return false;
5,568,897✔
3406
        // need this as arithmetic difference is defined as snan(indeterminate)
3407
        if (lhs.isinf(INF_TYPE_NEGATIVE) && rhs.isinf(INF_TYPE_NEGATIVE)) return false;
5,453,389✔
3408
        if (lhs.isinf(INF_TYPE_POSITIVE) && rhs.isinf(INF_TYPE_POSITIVE)) return false;
5,453,375✔
3409
        if constexpr (nsub) {
3410
                cfloat<nnbits, nes, nbt, nsub, nsup, nsat> diff = (lhs - rhs);
5,384,416✔
3411
                return (!diff.iszero() && diff.sign()) ? true : false;  // got to guard against -0
5,384,416✔
3412
        }
3413
        else {
3414
                if (lhs.iszero() && rhs.iszero()) return false;  // we need to 'collapse' all zero encodings
68,926✔
3415
                if (lhs.sign() && !rhs.sign()) return true;
68,918✔
3416
                if (!lhs.sign() && rhs.sign()) return false;
51,695✔
3417
                bool positive = lhs.ispos();
34,472✔
3418
                if (positive) {
34,472✔
3419
                        if (lhs.scale() < rhs.scale()) return true;
17,252✔
3420
                        if (lhs.scale() > rhs.scale()) return false;
12,786✔
3421
                }
3422
                else {
3423
                        if (lhs.scale() > rhs.scale()) return true;
17,220✔
3424
                        if (lhs.scale() < rhs.scale()) return false;
12,770✔
3425
                }
3426
                // sign and scale are the same
3427
                if (lhs.scale() == rhs.scale()) {
16,642✔
3428
                        // compare fractions: we do not have subnormals, so we can ignore the hidden bit
3429
                        blockbinary<nnbits - 1ull - nes, nbt> l, r;
3430
                        lhs.fraction(l);
16,642✔
3431
                        rhs.fraction(r);
16,642✔
3432
                        blockbinary<nnbits - nes, nbt> ll, rr; // fbits + 1 so we can 0 extend to honor 2's complement encoding of blockbinary
3433
                        ll.assignWithoutSignExtend(l);
16,642✔
3434
                        rr.assignWithoutSignExtend(r);
16,642✔
3435
                        return (positive ? (ll < rr) : (ll > rr));
16,642✔
3436
                }
3437
                return false;
×
3438
        }
3439
}
3440
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3441
inline bool operator> (const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) { 
2,794,589✔
3442
        if (lhs.isnan() || rhs.isnan()) return false;
2,794,589✔
3443
        // need this as arithmetic difference is defined as snan(indeterminate)
3444
        if (lhs.isinf(INF_TYPE_NEGATIVE) && rhs.isinf(INF_TYPE_NEGATIVE)) return false;
2,786,475✔
3445
        if (lhs.isinf(INF_TYPE_POSITIVE) && rhs.isinf(INF_TYPE_POSITIVE)) return false;
2,786,461✔
3446
        return  operator< (rhs, lhs); 
2,786,445✔
3447
}
3448
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3449
inline bool operator<=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) { 
1,398,017✔
3450
        if (lhs.isnan() || rhs.isnan()) return false;
1,398,017✔
3451
        return !operator> (lhs, rhs); 
1,389,917✔
3452
}
3453
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3454
inline bool operator>=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) {
1,399,400✔
3455
        if (lhs.isnan() || rhs.isnan()) return false;
1,399,400✔
3456
        return !operator< (lhs, rhs); 
1,391,294✔
3457
}
3458

3459
//////////////////////////////////////////////////////
3460
/// cfloat - cfloat binary arithmetic operators
3461

3462
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3463
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
9,974,529✔
3464
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
9,974,529✔
3465
        sum += rhs;
9,974,529✔
3466
        return sum;
9,974,529✔
3467
}
3468
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3469
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
8,446,103✔
3470
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
8,446,103✔
3471
        diff -= rhs;
8,446,103✔
3472
        return diff;
8,446,103✔
3473
}
3474
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3475
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
4,265,958✔
3476
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
4,265,958✔
3477
        mul *= rhs;
4,265,958✔
3478
        return mul;
4,265,958✔
3479
}
3480
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3481
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
5,135,313✔
3482
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
5,135,313✔
3483
        ratio /= rhs;
5,135,313✔
3484
        return ratio;
5,135,312✔
3485
}
3486

3487
/// binary cfloat - literal arithmetic operators
3488

3489
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3490
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3491
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3492
        sum += rhs;
3493
        return sum;
3494
}
3495
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3496
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3497
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3498
        diff -= rhs;
3499
        return diff;
3500
}
3501
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3502
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3503
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3504
        mul *= rhs;
3505
        return mul;
3506
}
3507
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3508
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
2✔
3509
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
2✔
3510
        ratio /= rhs;
2✔
3511
        return ratio;
2✔
3512
}
3513

3514
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3515
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
4✔
3516
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
4✔
3517
        sum += rhs;
4✔
3518
        return sum;
4✔
3519
}
3520
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3521
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3522
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3523
        diff -= rhs;
3524
        return diff;
3525
}
3526
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3527
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
16✔
3528
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
16✔
3529
        mul *= rhs;
16✔
3530
        return mul;
16✔
3531
}
3532
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3533
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3534
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3535
        ratio /= rhs;
3536
        return ratio;
3537
}
3538

3539
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3540
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
×
3541
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
×
3542
        sum += rhs;
×
3543
        return sum;
×
3544
}
3545
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3546
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3547
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3548
        diff -= rhs;
3549
        return diff;
3550
}
3551
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3552
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
38✔
3553
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
38✔
3554
        mul *= rhs;
38✔
3555
        return mul;
38✔
3556
}
3557
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3558
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
374✔
3559
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
374✔
3560
        ratio /= rhs;
374✔
3561
        return ratio;
374✔
3562
}
3563

3564
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3565
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3566
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3567
        sum += rhs;
3568
        return sum;
3569
}
3570
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3571
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3572
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3573
        diff -= rhs;
3574
        return diff;
3575
}
3576
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3577
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3578
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3579
        mul *= rhs;
3580
        return mul;
3581
}
3582
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3583
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3584
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3585
        ratio /= rhs;
3586
        return ratio;
3587
}
3588

3589
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3590
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3591
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3592
        sum += rhs;
3593
        return sum;
3594
}
3595
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3596
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3597
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3598
        diff -= rhs;
3599
        return diff;
3600
}
3601
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3602
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3603
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3604
        mul *= rhs;
3605
        return mul;
3606
}
3607
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3608
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3609
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3610
        ratio /= rhs;
3611
        return ratio;
3612
}
3613

3614
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3615
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3616
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3617
        sum += rhs;
3618
        return sum;
3619
}
3620
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3621
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3622
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3623
        diff -= rhs;
3624
        return diff;
3625
}
3626
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3627
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3628
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3629
        mul *= rhs;
3630
        return mul;
3631
}
3632
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3633
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3634
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3635
        ratio /= rhs;
3636
        return ratio;
3637
}
3638

3639
///  binary cfloat - literal arithmetic operators
3640

3641
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3642
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3643
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3644
        Cfloat sum(lhs);
3645
        sum += Cfloat(rhs);
3646
        return sum;
3647
}
3648
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3649
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3650
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3651
        Cfloat diff(lhs);
3652
        diff -= rhs;
3653
        return diff;
3654
}
3655
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3656
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3657
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3658
        Cfloat mul(lhs);
3659
        mul *= Cfloat(rhs);
3660
        return mul;
3661
}
3662
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3663
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3664
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3665
        Cfloat ratio(lhs);
3666
        ratio /= Cfloat(rhs);
3667
        return ratio;
3668
}
3669

3670
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3671
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
3672
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3673
        Cfloat sum(lhs);
3674
        sum += Cfloat(rhs);
3675
        return sum;
3676
}
3677
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3678
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
4✔
3679
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3680
        Cfloat diff(lhs);
4✔
3681
        diff -= rhs;
4✔
3682
        return diff;
4✔
3683
}
3684
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3685
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
3686
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3687
        Cfloat mul(lhs);
3688
        mul *= Cfloat(rhs);
3689
        return mul;
3690
}
3691
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3692
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
3693
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3694
        Cfloat ratio(lhs);
3695
        ratio /= Cfloat(rhs);
3696
        return ratio;
3697
}
3698

3699
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3700
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
3701
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3702
        Cfloat sum(lhs);
3703
        sum += Cfloat(rhs);
3704
        return sum;
3705
}
3706
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3707
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
4✔
3708
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3709
        Cfloat diff(lhs);
4✔
3710
        diff -= rhs;
4✔
3711
        return diff;
4✔
3712
}
3713
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3714
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
3715
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3716
        Cfloat mul(lhs);
3717
        mul *= Cfloat(rhs);
3718
        return mul;
3719
}
3720
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3721
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
3722
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3723
        Cfloat ratio(lhs);
3724
        ratio /= Cfloat(rhs);
3725
        return ratio;
3726
}
3727

3728
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3729
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
3730
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3731
        Cfloat sum(lhs);
3732
        sum += Cfloat(rhs);
3733
        return sum;
3734
}
3735
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3736
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
3737
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3738
        Cfloat diff(lhs);
3739
        diff -= rhs;
3740
        return diff;
3741
}
3742
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3743
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
3744
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3745
        Cfloat mul(lhs);
3746
        mul *= Cfloat(rhs);
3747
        return mul;
3748
}
3749
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3750
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
3751
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3752
        Cfloat ratio(lhs);
3753
        ratio /= Cfloat(rhs);
3754
        return ratio;
3755
}
3756

3757
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3758
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3759
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3760
        Cfloat sum(lhs);
3761
        sum += Cfloat(rhs);
3762
        return sum;
3763
}
3764
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3765
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3766
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3767
        Cfloat diff(lhs);
3768
        diff -= rhs;
3769
        return diff;
3770
}
3771
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3772
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3773
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3774
        Cfloat mul(lhs);
3775
        mul *= Cfloat(rhs);
3776
        return mul;
3777
}
3778
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3779
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3780
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3781
        Cfloat ratio(lhs);
3782
        ratio /= Cfloat(rhs);
3783
        return ratio;
3784
}
3785

3786
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3787
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
3788
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3789
        Cfloat sum(lhs);
3790
        sum += Cfloat(rhs);
3791
        return sum;
3792
}
3793
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3794
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
3795
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3796
        Cfloat diff(lhs);
3797
        diff -= rhs;
3798
        return diff;
3799
}
3800
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3801
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
3802
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3803
        Cfloat mul(lhs);
3804
        mul *= Cfloat(rhs);
3805
        return mul;
3806
}
3807
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3808
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
3809
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3810
        Cfloat ratio(lhs);
3811
        ratio /= Cfloat(rhs);
3812
        return ratio;
3813
}
3814

3815
///////////////////////////////////////////////////////////////////////
3816
///   binary logic literal comparisons
3817

3818
// cfloat - literal float logic operators
3819
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3820
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
3821
        return float(lhs) == rhs;
1✔
3822
}
3823
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3824
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
3825
        return float(lhs) != rhs;
1✔
3826
}
3827
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3828
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
2✔
3829
        return float(lhs) < rhs;
2✔
3830
}
3831
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3832
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
3833
        return float(lhs) > rhs;
1✔
3834
}
3835
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3836
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
3837
        return float(lhs) <= rhs;
1✔
3838
}
3839
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3840
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
3841
        return float(lhs) >= rhs;
1✔
3842
}
3843
// cfloat - literal double logic operators
3844
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3845
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
3846
        return double(lhs) == rhs;
1✔
3847
}
3848
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3849
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
3850
        return double(lhs) != rhs;
1✔
3851
}
3852
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3853
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
336✔
3854
        return double(lhs) < rhs;
336✔
3855
}
3856
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3857
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
892✔
3858
        return double(lhs) > rhs;
892✔
3859
}
3860
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3861
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
3862
        return double(lhs) <= rhs;
1✔
3863
}
3864
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3865
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
3866
        return double(lhs) >= rhs;
1✔
3867
}
3868

3869
#if LONG_DOUBLE_SUPPORT
3870
// cfloat - literal long double logic operators
3871
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3872
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
3873
        return (long double)(lhs) == rhs;
1✔
3874
}
3875
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3876
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
3877
        return (long double)(lhs) != rhs;
1✔
3878
}
3879
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3880
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
3881
        return (long double)(lhs) < rhs;
1✔
3882
}
3883
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3884
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
3885
        return (long double)(lhs) > rhs;
1✔
3886
}
3887
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3888
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
3889
        return (long double)(lhs) <= rhs;
1✔
3890
}
3891
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3892
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
3893
        return (long double)(lhs) >= rhs;
1✔
3894
}
3895
#endif
3896

3897
// cfloat - literal int logic operators
3898
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3899
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
11✔
3900
        return operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
11✔
3901
}
3902
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3903
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
1✔
3904
        return operator!=(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
1✔
3905
}
3906
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3907
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
107,196✔
3908
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
107,196✔
3909
}
3910
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3911
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
666✔
3912
        return operator<(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs), lhs);
666✔
3913
}
3914
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3915
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
1✔
3916
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs)) || operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
1✔
3917
}
3918
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3919
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
1✔
3920
        return !operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
1✔
3921
}
3922

3923
// cfloat - long long logic operators
3924
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3925
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3926
        return operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
3927
}
3928
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3929
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3930
        return operator!=(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
3931
}
3932
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3933
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3934
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
3935
}
3936
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3937
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3938
        return operator<(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs), lhs);
3939
}
3940
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3941
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3942
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs)) || operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
3943
}
3944
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3945
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
3946
        return !operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
3947
}
3948

3949
// standard library functions for floating point
3950

3951
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3952
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> frexp(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& x, int* exp) {
57,412✔
3953
        *exp = x.scale();
57,412✔
3954
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> fraction(x);
57,412✔
3955
        fraction.setexponent(0);
57,412✔
3956
        return fraction;
57,412✔
3957
}
3958

3959
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3960
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ldexp(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& x, int exp) {
765✔
3961
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> result(x);
765✔
3962
        int xexp = x.scale();
765✔
3963
        result.setexponent(xexp + exp);  // TODO: this does not work for subnormals
765✔
3964
        return result;
765✔
3965
}
3966

3967
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3968
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> 
3969
fma(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> x,
3✔
3970
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> y,
3971
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> z) {
3972
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> fused{ 0 };
3✔
3973
        constexpr unsigned FBITS = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits;
3✔
3974
        constexpr unsigned EXTRA_FBITS = FBITS+2;
3✔
3975
        constexpr unsigned EXTENDED_PRECISION = nbits + EXTRA_FBITS;
3✔
3976
        // the C++ fma spec indicates that the x*y+z is evaluated in 'infinite' precision
3977
        // with only a single rounding event. The minimum finite precision that would behave like this
3978
        // is the precision where the product x*y does not need to be rounded, which will
3979
        // need at least 2*(fbits+1) mantissa bits to capture all bits that can be
3980
        // generated by the product.
3981
        cfloat<EXTENDED_PRECISION, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> preciseX(x), preciseY(y), preciseZ(z);
3✔
3982
//        ReportValue(preciseX, "extended precision x");
3983
//        ReportValue(preciseY, "extended precision y");
3984
//        ReportValue(preciseZ, "extended precision z");
3985
        cfloat<EXTENDED_PRECISION, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> product = preciseX * preciseY;
3✔
3986
//        ReportValue(product, "extended precision p");
3987
        fused = product + preciseZ;
3✔
3988
        return fused;
3✔
3989
}
3990

3991
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3992
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
3993
minpos(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
3994
        return c.minpos();
3995
}
3996
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3997
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
3998
maxpos(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
3999
        return c.maxpos();
4000
}
4001
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4002
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
4003
minneg(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
4004
        return c.minneg();
4005
}
4006
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4007
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
4008
maxneg(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
4009
        return c.maxneg();
4010
}
4011

4012
}} // namespace sw::universal
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