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

stillwater-sc / universal / 24907312588

24 Apr 2026 07:12PM UTC coverage: 84.392% (+0.02%) from 84.371%
24907312588

push

github

web-flow
feat(bfloat16): full constexpr support across all operators (#751)

* feat(bfloat16): full constexpr support across all operators

Replace std::memcpy with sw::bit_cast for the float<->uint32 punning at the
heart of bfloat16's IEEE-754 conversion. memcpy is decorated constexpr in
the existing code but is not actually constexpr until C++26; bit_cast is
constexpr today via __builtin_bit_cast on gcc/clang/MSVC modern.

Once the conversion functions are BIT_CAST_CONSTEXPR, almost everything
else falls in line because bfloat16 arithmetic is just float arithmetic
through the conversion (and IEEE-754 float operators are constexpr in
C++20).

Promoted to constexpr / BIT_CAST_CONSTEXPR:
  convert_signed, convert_unsigned, convert_ieee754, convert_to_ieee754
  bfloat16(int/long/long long/unsigned/.../float/double/long double) ctors
  operator=(int/long/.../float/double/long double)
  operator+=, -=, *=, /=
  operator++, --, prefix unary -
  operator float, double, signed/unsigned char, short, int, long, long long
  operator==, !=, <, <=, >, >= (free + literal-comparison overloads)

operator=(char) gets the same signedness dispatch we used in the posit
constexpr work (#714) -- if constexpr (std::is_signed_v<char>) selects
convert_signed, otherwise convert_unsigned, so negative chars on
signed-char platforms (the common case) sign-extend correctly.

Tests:
- static/float/bfloat16/api/api.cpp gets a #if BIT_CAST_IS_CONSTEXPR
  guarded block that exercises the constexpr contract via lambdas and
  static_asserts (constexpr +=, *=, -=, /=, conversion-out, comparison)
  plus runtime cross-checks for bit-equivalence.

Validation (gcc-13 + clang-18, -std=c++20):
  bfloat16_api, bfloat16_attributes, bfloat16_constants, bfloat16_traits,
  bfloat16_arithmetic - PASS on both compilers

Resolves #725
Relates to #723 (Epic: constexpr support across Universal number systems)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(bfloat16): align literal-c... (continued)

47 of 49 new or added lines in 1 file covered. (95.92%)

8 existing lines in 1 file now uncovered.

44952 of 53266 relevant lines covered (84.39%)

6450119.32 hits per line

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

93.53
/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 <regex>
26
#include <type_traits>
27
#include <universal/native/ieee754.hpp>
28
#include <universal/native/subnormal.hpp>
29
#include <universal/utility/find_msb.hpp>
30
#include <universal/number/shared/nan_encoding.hpp>
31
#include <universal/number/shared/infinite_encoding.hpp>
32
#include <universal/number/shared/specific_value_encoding.hpp>
33
// arithmetic tracing options
34
#include <universal/number/algorithm/trace_constants.hpp>
35
// cfloat exception structure
36
#include <universal/number/cfloat/exceptions.hpp>
37
// composition types used by cfloat
38
#include <universal/internal/blockbinary/blockbinary.hpp>
39
#include <universal/internal/blocktriple/blocktriple.hpp>
40
#include <universal/number/support/decimal.hpp>
41

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

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

50
namespace sw { namespace universal {
51

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

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

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

93
/// <summary>
94
/// convert a blocktriple to a cfloat. blocktriples come out of the arithmetic
95
/// engine in the form ii.ff...ff and a scale. The conversion must take this
96
/// denormalized form into account during conversion.
97
/// 
98
/// The blocktriple must be in this form to round correctly, as all the bits
99
/// after an arithmetic operation must be taken into account.
100
/// 
101
/// Transformation:
102
///    ii.ff...ff  transform to    s.eee.fffff
103
/// All number systems that depend on blocktriple will need to have
104
/// the rounding decision answered, so that functionality can be
105
/// reused if we locate it inside blocktriple.
106
/// 
107
/// if (srcbits > fbits) // we need to round
108
///     if (ii.00..00 > 1) 
109
///         mask is at srcbits - fbits + 1
110
///     else 
111
///                    mask is at srcbits - fbits
112
/// }
113
/// else {               // no need to round
114
/// }
115
/// </summary>
116
/// <typeparam name="bt">type of the block used for cfloat storage</typeparam>
117
/// <param name="src">the blocktriple to be converted</param>
118
/// <param name="tgt">the resulting cfloat</param>
119
template<unsigned srcbits, BlockTripleOperator op, unsigned nbits, unsigned es, typename bt,
120
        bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
121
inline /*constexpr*/ void convert(const blocktriple<srcbits, op, bt>& src, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& tgt) {
78,739,348✔
122
        using btType = blocktriple<srcbits, op, bt>;
123
        using cfloatType = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
124
        // test special cases
125
        if (src.isnan()) {
78,739,348✔
126
                tgt.setnan(src.sign() ? NAN_TYPE_SIGNALLING : NAN_TYPE_QUIET);
560✔
127
        }
128
        else        if (src.isinf()) {
78,738,788✔
129
                tgt.setinf(src.sign());
560✔
130
        }
131
        else         if (src.iszero()) {
78,738,228✔
132
                tgt.setzero();
46,448✔
133
                tgt.setsign(src.sign()); // preserve sign
46,448✔
134
        }
135
        else {
136
                int significandScale = src.significandscale();
78,691,780✔
137
                int exponent = src.scale() + significandScale;
78,691,780✔
138
                // blocktriple keeps arithmetic results intentionally unnormalized. scale() tracks the source binade,
139
                // while significandScale() reports whether the exact result spilled into the integer headroom above
140
                // the radix. cfloat conversion has to combine both before it can classify the value as normal,
141
                // subnormal, or overflowing.
142
                // special case of underflow
143
                if constexpr (hasSubnormals) {
144
//                        std::cout << "exponent = " << exponent << " bias = " << cfloatType::EXP_BIAS << " exp subnormal = " << cfloatType::MIN_EXP_SUBNORMAL << '\n';
145
                        // why must exponent be less than (minExpSubnormal - 1) to be rounded to zero? 
146
                        // because the half-way value that would round up to minpos is at exp = (minExpSubnormal - 1)
147
                        if (exponent < cfloatType::MIN_EXP_SUBNORMAL) {
55,340,000✔
148
                                tgt.setzero();
183,174✔
149
                                if (exponent == (cfloatType::MIN_EXP_SUBNORMAL - 1)) {
183,174✔
150
                                        // -exponent because we are right shifting and exponent in this range is negative
151
                                        int adjustment = -(exponent + subnormal_reciprocal_shift[es]);
39,878✔
152
                                        std::pair<bool, unsigned> alignment = src.roundingDecision(adjustment);
39,878✔
153
                                        if (alignment.first) ++tgt; // we are minpos
39,878✔
154
                                }
155
                                tgt.setsign(src.sign());
183,174✔
156
                                return;
3,108,638✔
157
                        }
158
                }
159
                else {
160
                        if (exponent + cfloatType::EXP_BIAS <= 0) {  // value is in the subnormal range, which maps to 0
23,351,780✔
161
                                tgt.setzero();
363,536✔
162
                                tgt.setsign(src.sign());
363,536✔
163
                                return;
1,241,230✔
164
                        }
165
                }
166
                // special case of overflow
167
                if constexpr (hasMaxExpValues) {
168
                        if constexpr (isSaturating) {
169
                                if (exponent > cfloatType::MAX_EXP) {
21,098,841✔
170
                                        if (src.sign()) tgt.maxneg(); else tgt.maxpos();
934,372✔
171
                                        return;
934,372✔
172
                                }
173
                        }
174
                        else {
175
                                if (exponent > cfloatType::MAX_EXP) {
30,556,166✔
176
                                        tgt.setinf(src.sign());
2,673,539✔
177
                                        return;
2,673,539✔
178
                                }
179
                        }
180
                }
181
                else {  // no max-exponent values: will saturate at a different encoding: TODO can we hide it all in maxpos?
182
                        if constexpr (isSaturating) {
183
                                if (exponent > cfloatType::MAX_EXP) {
3,704✔
184
                                        if (src.sign()) tgt.maxneg(); else tgt.maxpos();
6✔
185
                                        return;
6✔
186
                                }
187
                        }
188
                        else {
189
                                if (exponent > cfloatType::MAX_EXP) {
26,486,359✔
190
                                        tgt.setinf(src.sign());
195,241✔
191
                                        return;
195,241✔
192
                                }
193
                        }
194
                }
195

196
                // our value needs to go through rounding to be correctly interpreted
197
                // 
198
                // tgt.clear();  // no need as all bits are going to be set by the code below
199

200
                // exponent construction
201
                int adjustment{ 0 };
74,341,912✔
202
                // construct exponent
203
                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
74,341,912✔
204
//                        std::cout << "exponent         " << to_binary(biasedExponent) << '\n';        
205
                if constexpr (hasSubnormals) {
206
                        //if (exponent >= cfloatType::MIN_EXP_SUBNORMAL && exponent < cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::MIN_EXP_NORMAL) {
207
                        if (exponent < cfloatType::MIN_EXP_NORMAL) {
52,231,362✔
208
                                // the value is in the subnormal range of the cfloat
209
                                biasedExponent = 0;
35,478,893✔
210
                                // -exponent because we are right shifting and exponent in this range is negative
211
                                adjustment = -(exponent + subnormal_reciprocal_shift[es]);
35,478,893✔
212
                                // this is the right shift adjustment required for subnormal representation due 
213
                                // to the scale of the input number, i.e. the exponent of 2^-adjustment
214
                        }
215
                        else {
216
                                // the value is in the normal range of the cfloat
217
                                biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(cfloatType::EXP_BIAS)); // this is guaranteed to be positive
16,752,469✔
218
                        }
219
                }
220
                else {
221
                        if (exponent < cfloatType::MIN_EXP_NORMAL) biasedExponent = 1ull; // fixup biasedExponent if we are in the subnormal region
22,110,550✔
222
                }
223

224

225
                // get the rounding direction and the LSB right shift: 
226
                std::pair<bool, unsigned> alignment = src.roundingDecision(adjustment);
74,341,912✔
227
                unsigned rightShift = alignment.second;  // this is the shift to get the LSB of the src to the LSB of the tgt
74,341,912✔
228
                // rightShift is also the normalization step: it lines the blocktriple hidden bit up with the cfloat
229
                // hidden-bit position, while the bool tells us whether the discarded tail rounds the retained payload up.
230
                //std::cout << "rightShift       " << rightShift << '\n';
231

232
                if constexpr (btType::bfbits < 65) {
233
                        bool roundup = alignment.first;
73,810,031✔
234
                        //std::cout << "round-up?        " << (roundup ? "yes" : "no") << '\n';
235
                        // we can use a uint64_t to construct the cfloat
236
                        uint64_t raw = (src.sign() ? 1ull : 0ull); // process sign
73,810,031✔
237
                        //std::cout << "raw bits (sign)  " << to_binary(raw) << '\n';
238
                        // construct the fraction bits
239
                        uint64_t fracbits = src.significand_ull(); // get all the bits, including the integer bits
73,810,031✔
240
                        //std::cout << "fracbits         " << to_binary(fracbits) << '\n';
241
                        fracbits >>= rightShift;
73,810,031✔
242
                        //std::cout << "fracbits shifted " << to_binary(fracbits) << '\n';
243
                        fracbits &= cfloatType::ALL_ONES_FR; // remove the hidden bit
73,810,031✔
244
                        //std::cout << "fracbits masked  " << to_binary(fracbits) << '\n';
245
                        if (roundup) ++fracbits;
73,810,031✔
246
                        // Rounding can turn 1.111... into 10.000..., so the carry has to be reinterpreted as an exponent bump.
247
                        // This is the last place where the unrounded blocktriple state can still change the target binade.
248
                        if (fracbits == (1ull << cfloatType::fbits)) { // check for overflow
73,810,031✔
249
                                if (biasedExponent == cfloatType::ALL_ONES_ES) {
456,326✔
250
                                        fracbits = cfloatType::INF_ENCODING; // project to INF
6,424✔
251
                                }
252
                                else {
253
                                        ++biasedExponent;
449,902✔
254
                                        fracbits = 0;
449,902✔
255
                                }
256
                        }
257

258
                        raw <<= es; // shift sign to make room for the exponent bits
73,810,031✔
259
                        raw |= biasedExponent;
73,810,031✔
260
                        //std::cout << "raw bits (exp)   " << to_binary(raw) << '\n';
261
                        raw <<= cfloatType::fbits; // make room for the fraction bits
73,810,031✔
262
                        //std::cout << "raw bits (s+exp) " << to_binary(raw) << '\n';
263
                        raw |= fracbits;
73,810,031✔
264
                        //std::cout << "raw bits (final) " << to_binary(raw) << '\n';
265
                        tgt.setbits(raw);
73,810,031✔
266
//                        std::cout << "raw bits (all)   " << to_binary(raw) << '\n';
267
                        if constexpr (isSaturating) {
268
                                if (tgt.isnan()) {
20,168,159✔
269
                                        if (src.sign()) {
722✔
270
                                                tgt.maxneg();        // map back to maxneg
361✔
271
                                        }
272
                                        else {
273
                                                tgt.maxpos();        // map back to maxpos
361✔
274
                                        }
275
                                }
276
                        }
277
                        else {
278
                                // when you get too far, map it back to +-inf: 
279
                                // TBD: this doesn't appear to be the right algorithm to catch all overflow patterns
280
                                if (tgt.isnan()) tgt.setinf(src.sign());        // map back to +-inf
53,641,872✔
281
                        }
282
                }
283
                else {
284
                        // compose the segments
285
                        bool roundup = alignment.first;
531,881✔
286
                        auto fracbits = src.significand();
531,881✔
287
                        fracbits >>= static_cast<int>(rightShift);
531,881✔
288
                        fracbits.setbit(cfloatType::fbits, false); // remove the hidden bit (matches narrow path: fracbits &= ALL_ONES_FR)
531,881✔
289

290
                        // apply rounding (matches the bfbits < 65 path above)
291
                        if (roundup) fracbits.increment();
531,881✔
292

293
                        // check for rounding overflow: if the fraction carried into
294
                        // the hidden-bit position, clear the fraction and bump the exponent
295
                        if (fracbits.test(cfloatType::fbits)) {
531,881✔
296
                                fracbits.clear();
33✔
297
                                if (biasedExponent == cfloatType::ALL_ONES_ES) {
33✔
298
                                        // rounding overflowed beyond the maximum binade:
299
                                        // handle explicitly because the isnan() remap below
300
                                        // does not catch all configurations (e.g. hasMaxExpValues
301
                                        // with zero fraction is a valid finite encoding, not NaN)
302
                                        if constexpr (isSaturating) {
303
                                                if (src.sign()) tgt.maxneg(); else tgt.maxpos();
×
304
                                        }
305
                                        else {
306
                                                tgt.setinf(src.sign());
×
307
                                        }
308
                                        return;
×
309
                                }
310
                                else {
311
                                        ++biasedExponent;
33✔
312
                                        ++exponent;
33✔
313
                                }
314
                        }
315

316
                        // copy the blocks that contain fraction bits
317
                        tgt.clear();
531,881✔
318
                        for (unsigned b = 0; b < btType::nrBlocks; ++b) {
1,074,276✔
319
                                tgt.setblock(b, fracbits.block(b));
542,395✔
320
                        }
321
                        tgt.setsign(src.sign());
531,881✔
322
                        if (!tgt.setexponent(exponent)) {
531,881✔
323
                                std::cerr << "exponent value is out of range: " << exponent << '\n';
×
324
                        }
325

326
                        // saturation / overflow-to-inf handling (matches the bfbits < 65 path)
327
                        if constexpr (isSaturating) {
328
                                if (tgt.isnan()) {
8✔
329
                                        if (src.sign()) {
×
330
                                                tgt.maxneg();
×
331
                                        }
332
                                        else {
333
                                                tgt.maxpos();
×
334
                                        }
335
                                }
336
                        }
337
                        else {
338
                                if (tgt.isnan()) tgt.setinf(src.sign());
531,873✔
339
                        }
340
                }
341
        }
342
}
343

344

345
/// <summary>
346
/// An arbitrary, fixed-size floating-point number with configurable gradual under/overflow and saturation/non-saturation arithmetic.
347
/// Default configuration offers normal encoding and non-saturating arithmetic.
348
/// /// </summary>
349
/// <typeparam name="nbits">number of bits in the encoding</typeparam>
350
/// <typeparam name="es">number of exponent bits in the encoding</typeparam>
351
/// <typeparam name="bt">the type to use as storage class: one of [uint8_t|uint16_t|uint32_t]</typeparam>
352
/// <typeparam name="hasSubnormals">configure gradual underflow (==subnormals)</typeparam>
353
/// <typeparam name="hasMaxExpValues">reclaim max-exponent encodings as numeric values</typeparam>
354
/// <typeparam name="isSaturating">configure saturation arithmetic</typeparam>
355
template<unsigned _nbits, unsigned _es, typename bt = uint8_t,
356
        bool _hasSubnormals = false, bool _hasMaxExpValues = false, bool _isSaturating = false>
357
class cfloat {
358
public:
359
        static_assert(_nbits > _es + 1ull, "nbits is too small to accomodate the requested number of exponent bits");
360
        static_assert(_es < 21ull, "my God that is a big number, are you trying to break the Interweb?");
361
        static_assert(_es > 0, "number of exponent bits must be bigger than 0 to be a classic floating point number");
362
        // how do you assert on the condition that if es == 1 then subnormals and max-exponent values must be true?
363
        static constexpr bool     subsuper = (_hasSubnormals && _hasMaxExpValues);
364
        static constexpr bool     special = (subsuper ? true : (_es > 1));
365
        static_assert(special, "when es == 1, cfloat must have both subnormals and max-exponent values");
366
        static constexpr unsigned bitsInByte = 8u;
367
        static constexpr unsigned bitsInBlock = sizeof(bt) * bitsInByte;
368
        static_assert(bitsInBlock <= 64, "storage unit for block arithmetic needs to be <= uint64_t"); // TODO: carry propagation on uint64_t requires assembly code
369

370
        static constexpr unsigned nbits = _nbits;
371
        static constexpr unsigned es = _es;
372
        static constexpr unsigned fbits  = nbits - 1u - es;    // number of fraction bits excluding the hidden bit
373
        static constexpr unsigned fhbits = nbits - es;           // number of fraction bits including the hidden bit
374

375
        static constexpr uint64_t  storageMask = (0xFFFFFFFFFFFFFFFFull >> (64ull - bitsInBlock));
376
        static constexpr bt       BLOCK_MASK = bt(~0);
377
        static constexpr bt       ALL_ONES = bt(~0); // block type specific all 1's value
378
        static constexpr uint32_t ALL_ONES_ES = (0xFFFF'FFFFul >> (32 - es));
379
        static constexpr uint64_t topfbits = fbits % 64;
380
        static constexpr uint64_t FR_SHIFT = (topfbits > 0 ? (64 - topfbits) : 0);
381
        static constexpr uint64_t ALL_ONES_FR = (topfbits > 0 ? (0xFFFF'FFFF'FFFF'FFFFull >> FR_SHIFT) : 0ull); // special case for nbits <= 64
382
        static constexpr uint64_t INF_ENCODING = (ALL_ONES_FR & ~1ull);
383

384
        static constexpr unsigned nrBlocks = 1u + ((nbits - 1ull) / bitsInBlock);
385
        static constexpr unsigned MSU = nrBlocks - 1u; // MSU == Most Significant Unit, as MSB is already taken
386
        static constexpr bt       MSU_MASK = (ALL_ONES >> (nrBlocks * bitsInBlock - nbits));
387
        static constexpr unsigned bitsInMSU = bitsInBlock - (nrBlocks * bitsInBlock - nbits);
388
        static constexpr unsigned fBlocks = 1ull + ((fbits - 1ull) / bitsInBlock); // nr of blocks with fraction bits
389
        static constexpr unsigned FSU = fBlocks - 1u;  // FSU = Fraction Significant Unit: the index of the block that contains the most significant fraction bits
390
        static constexpr bt       FSU_MASK = (ALL_ONES >> (fBlocks * bitsInBlock - fbits));
391
        static constexpr unsigned bitsInFSU = bitsInBlock - (fBlocks * bitsInBlock - fbits);
392

393
        static constexpr bt       SIGN_BIT_MASK = bt(bt(1ull) << ((nbits - 1ull) % bitsInBlock));
394
        static constexpr bt       LSB_BIT_MASK = bt(1ull);
395
        static constexpr bool     MSU_CAPTURES_EXP = (1ull + es) <= bitsInMSU;
396
        static constexpr unsigned EXP_SHIFT = (MSU_CAPTURES_EXP ? (1 == nrBlocks ? (nbits - 1ull - es) : (bitsInMSU - 1ull - es)) : 0);
397
        static constexpr bt       MSU_EXP_MASK = ((ALL_ONES << EXP_SHIFT) & ~SIGN_BIT_MASK) & MSU_MASK;
398
        static constexpr int      EXP_BIAS = ((1 << (es - 1u)) - 1l);
399
        static constexpr int      MAX_EXP = (es == 1) ? 1 : ((1 << es) - EXP_BIAS - 1);
400
        static constexpr int      MIN_EXP_NORMAL = 1 - EXP_BIAS;
401
        static constexpr int      MIN_EXP_SUBNORMAL = 1 - EXP_BIAS - int(fbits); // the scale of smallest ULP
402

403
        static constexpr bool     hasSubnormals   = _hasSubnormals;
404
        static constexpr bool     hasMaxExpValues = _hasMaxExpValues;
405
        static constexpr bool     isSaturating    = _isSaturating;
406
        typedef bt BlockType;
407

408
        // constructors
409
        cfloat() = default;
410
        cfloat(const cfloat&) = default;
411
        cfloat& operator=(const cfloat&) = default;
412

413
        // construct a cfloat from another
414
        template<unsigned nnbits, unsigned ees, typename bbt, bool ssub, bool ssup, bool ssat>
415
        cfloat(const cfloat<nnbits, ees, bbt, ssub, ssup, ssat>& rhs) noexcept : _block{} {
10,995✔
416
                if (rhs.isnan()) {
10,995✔
417
                        setnan(rhs.sign() ? NAN_TYPE_SIGNALLING : NAN_TYPE_QUIET);
461✔
418
                }
419
                else if (rhs.isinf()) {
10,534✔
420
                        setinf(rhs.sign());
23✔
421
                }
422
                else if (rhs.iszero()) {
10,511✔
423
                        setzero();
1,125✔
424
                }
425
                else {
426
                        if constexpr (std::is_same_v<bt, bbt>) {
427
                                // Full-precision path: create a blocktriple sized for the TARGET's
428
                                // fraction width, pre-align the source fraction bits into it (with
429
                                // proper rounding-bit collection), then let convert() handle
430
                                // rounding and encoding.
431
                                constexpr unsigned srcFbits = nnbits - 1u - ees;
4,968✔
432
                                using TgtBT = blocktriple<fbits, BlockTripleOperator::ADD, bt>;
433
                                constexpr unsigned addRbits = TgtBT::rbits;
4,968✔
434
                                TgtBT value;
4,968✔
435

436
                                bool srcSign = rhs.sign();
4,968✔
437
                                int srcScale = rhs.scale();
4,968✔
438

439
                                if constexpr (srcFbits < 64 && (fbits + addRbits) < 64) {
440
                                        // Fast path: fits in uint64_t
441
                                        // Guard requires (fbits + addRbits) < 64 to prevent UB from
442
                                        // left-shift of amount (fbits + addRbits - srcFbits) >= 64.
443
                                        uint64_t srcRaw = rhs.fraction_ull();
4,968✔
444
                                        if (rhs.isnormal() || rhs.ismaxexpvalue()) {
4,968✔
445
                                                // Normal and max-exponent values both have a hidden bit
446
                                                srcRaw |= (1ull << srcFbits);
4,968✔
447
                                        }
448
                                        else {
449
                                                // Normalize subnormal: find leading 1, shift to hidden-bit position
450
                                                using SrcCfloat = cfloat<nnbits, ees, bbt, ssub, ssup, ssat>;
451
                                                srcScale = SrcCfloat::MIN_EXP_NORMAL;
×
452
                                                for (int i = static_cast<int>(srcFbits) - 1; i >= 0; --i) {
×
453
                                                        if (srcRaw & (1ull << i)) {
×
454
                                                                unsigned shift = srcFbits - static_cast<unsigned>(i);
×
455
                                                                srcScale -= static_cast<int>(shift);
×
456
                                                                srcRaw <<= shift;
×
457
                                                                break;
×
458
                                                        }
459
                                                }
460
                                        }
461
                                        // Align source hidden bit (at srcFbits) to target position (fbits + addRbits)
462
                                        uint64_t tgtRaw;
463
                                        if constexpr (srcFbits >= fbits + addRbits) {
464
                                                constexpr unsigned rshift = srcFbits - fbits - addRbits;
4,230✔
465
                                                if constexpr (rshift > 0) {
466
                                                        uint64_t stickyMask = (1ull << rshift) - 1;
646✔
467
                                                        bool sticky = (srcRaw & stickyMask) != 0;
646✔
468
                                                        tgtRaw = srcRaw >> rshift;
646✔
469
                                                        if (sticky) tgtRaw |= 1; // fold discarded bits into sticky
646✔
470
                                                }
471
                                                else {
472
                                                        tgtRaw = srcRaw;
3,584✔
473
                                                }
474
                                        }
475
                                        else {
476
                                                constexpr unsigned lshift = fbits + addRbits - srcFbits;
738✔
477
                                                tgtRaw = srcRaw << lshift;
738✔
478
                                        }
479
                                        value.setsign(srcSign);
4,968✔
480
                                        value.setscale(srcScale);
4,968✔
481
                                        value.setbits(tgtRaw);
4,968✔
482
                                }
483
                                else {
484
                                        // Wide path: bit-by-bit copy with alignment
485
                                        int offset = static_cast<int>(fbits + addRbits) - static_cast<int>(srcFbits);
486
                                        // Initialize radix via setbits, then overwrite significand
487
                                        value.setbits(0);
488
                                        bool sticky = false;
489
                                        for (unsigned i = 0; i < srcFbits; ++i) {
490
                                                bool v = rhs.at(i);
491
                                                if (!v) continue;
492
                                                int tgtBit = static_cast<int>(i) + offset;
493
                                                if (tgtBit >= 0 && tgtBit < static_cast<int>(TgtBT::bfbits)) {
494
                                                        value.setbit(static_cast<unsigned>(tgtBit), true);
495
                                                }
496
                                                else if (tgtBit < 0) {
497
                                                        sticky = true;
498
                                                }
499
                                        }
500
                                        if (sticky) value.setbit(0, true);
501
                                        if (rhs.isnormal() || rhs.ismaxexpvalue()) {
502
                                                // Normal and max-exponent values both have a hidden bit
503
                                                value.setbit(static_cast<unsigned>(fbits + addRbits), true);
504
                                        }
505
                                        else {
506
                                                // Normalize subnormal for wide path.
507
                                                // The bit-copy loop above placed source bit i at target position
508
                                                // i + offset = i + (fbits + addRbits - srcFbits).  The blocktriple
509
                                                // scale represents the exponent such that:
510
                                                //   value = 2^scale * significant * 2^(-fbits-addRbits)
511
                                                // For the subnormal with leading 1 at source bit i, the significant
512
                                                // integer has its leading 1 at position i + fbits + addRbits - srcFbits,
513
                                                // giving value = 2^scale * 2^(i - srcFbits) * 1.xxx.
514
                                                // The true subnormal value is 2^MIN_EXP_NORMAL * 2^(i - srcFbits) * 1.xxx,
515
                                                // so scale = MIN_EXP_NORMAL with no further adjustment.
516
                                                using SrcCfloat = cfloat<nnbits, ees, bbt, ssub, ssup, ssat>;
517
                                                srcScale = SrcCfloat::MIN_EXP_NORMAL;
518
                                                // (No srcScale -= shift: the offset alignment already accounts for
519
                                                //  the bit position; subtracting shift would apply it twice.)
520
                                        }
521
                                        value.setnormal(); // clear _zero flag set by setbits(0)
522
                                        value.setsign(srcSign);
523
                                        value.setscale(srcScale);
524
                                }
525
                                convert(value, *this);
4,968✔
526
                        }
527
                        else {
528
                                // Cross-block-type: marshall through double.
529
                                // Safe only when the source fraction fits in double's 52-bit significand.
530
                                constexpr unsigned srcFbitsXB = nnbits - ees - 1u;
4,418✔
531
                                static_assert(srcFbitsXB <= 52u,
532
                                        "cfloat converting constructor: source and target have different "
533
                                        "block types and source has more fraction bits than double can represent; "
534
                                        "use matching block types for full-precision cross-config conversion");
535
                                *this = double(rhs);
4,418✔
536
                        }
537
                }
538
        }
10,995✔
539

540
        // converting constructors
541
        constexpr cfloat(const std::string& stringRep) : _block{} { assign(stringRep); }
1✔
542
        // specific value constructor
543
        constexpr cfloat(const SpecificValue code) noexcept : _block{} {
309✔
544
                switch (code) {
309✔
545
                case SpecificValue::maxpos:
95✔
546
                        maxpos();
95✔
547
                        break;
95✔
548
                case SpecificValue::minpos:
111✔
549
                        minpos();
111✔
550
                        break;
111✔
551
                case SpecificValue::zero:
×
552
                default:
553
                        zero();
×
554
                        break;
×
555
                case SpecificValue::minneg:
×
556
                        minneg();
×
557
                        break;
×
558
                case SpecificValue::maxneg:
48✔
559
                        maxneg();
48✔
560
                        break;
48✔
561
                case SpecificValue::infpos:
21✔
562
                        setinf(false);
21✔
563
                        break;
21✔
564
                case SpecificValue::infneg:
×
565
                        setinf(true);
×
566
                        break;
×
567
                case SpecificValue::nar: // approximation as cfloats don't have a NaR
17✔
568
                case SpecificValue::qnan:
569
                        setnan(NAN_TYPE_QUIET);
17✔
570
                        break;
17✔
571
                case SpecificValue::snan:
17✔
572
                        setnan(NAN_TYPE_SIGNALLING);
17✔
573
                        break;
17✔
574
                }
575
        }
309✔
576

577
        constexpr cfloat(signed char iv)                    noexcept : _block{} { *this = iv; }
578
        constexpr cfloat(short iv)                          noexcept : _block{} { *this = iv; }
579
        constexpr cfloat(int iv)                            noexcept : _block{} { *this = iv; }
7,847✔
580
        constexpr cfloat(long iv)                           noexcept : _block{} { *this = iv; }
581
        constexpr cfloat(long long iv)                      noexcept : _block{} { *this = iv; }
1✔
582
        constexpr cfloat(char iv)                           noexcept : _block{} { *this = iv; }
583
        constexpr cfloat(unsigned short iv)                 noexcept : _block{} { *this = iv; }
584
        constexpr cfloat(unsigned int iv)                   noexcept : _block{} { *this = iv; }
585
        constexpr cfloat(unsigned long iv)                  noexcept : _block{} { *this = iv; }
100✔
586
        constexpr cfloat(unsigned long long iv)             noexcept : _block{} { *this = iv; }
587
        CONSTEXPRESSION cfloat(float iv)                    noexcept : _block{} { *this = iv; }
340,804✔
588
        CONSTEXPRESSION cfloat(double iv)                   noexcept : _block{} { *this = iv; }
474,587✔
589

590
        // assignment operators
591
        constexpr cfloat& operator=(signed char rhs)        noexcept { return convert_signed_integer(rhs); }
592
        constexpr cfloat& operator=(short rhs)              noexcept { return convert_signed_integer(rhs); }
593
        constexpr cfloat& operator=(int rhs)                noexcept { return convert_signed_integer(rhs); }
13,329✔
594
        constexpr cfloat& operator=(long rhs)               noexcept { return convert_signed_integer(rhs); }
595
        constexpr cfloat& operator=(long long rhs)          noexcept { return convert_signed_integer(rhs); }
1✔
596

597
        constexpr cfloat& operator=(char rhs)               noexcept { return convert_unsigned_integer(rhs); }
598
        constexpr cfloat& operator=(unsigned short rhs)     noexcept { return convert_unsigned_integer(rhs); }
599
        constexpr cfloat& operator=(unsigned int rhs)       noexcept { return convert_unsigned_integer(rhs); }
2,685✔
600
        constexpr cfloat& operator=(unsigned long rhs)      noexcept { return convert_unsigned_integer(rhs); }
110✔
601
        constexpr cfloat& operator=(unsigned long long rhs) noexcept { return convert_unsigned_integer(rhs); }
602

603
        BIT_CAST_CONSTEXPR cfloat& operator=(float rhs)     noexcept { return convert_ieee754(rhs); }
32,488,679✔
604
        BIT_CAST_CONSTEXPR cfloat& operator=(double rhs)    noexcept { return convert_ieee754(rhs); }
105,788,589✔
605

606
        // make conversions to native types explicit
607
        explicit operator int()                       const noexcept { return to_int(); }
608
        explicit operator long()                      const noexcept { return to_long(); }
609
        explicit operator long long()                 const noexcept { return to_long_long(); }
1✔
610
        explicit operator float()                     const noexcept { return to_native<float>(); }
35,177,206✔
611
        explicit operator double()                    const noexcept { return to_native<double>(); }
78,366,155✔
612

613
        // guard long double support to enable ARM and RISC-V embedded environments
614
#if LONG_DOUBLE_SUPPORT
615
        explicit operator long double()               const noexcept { return to_native<long double>(); }
54✔
616
        BIT_CAST_CONSTEXPR cfloat(long double iv)           noexcept : _block{} { *this = iv; }
2✔
617
        BIT_CAST_CONSTEXPR cfloat& operator=(long double rhs)  noexcept { return convert_ieee754(rhs); }
74✔
618
#endif
619

620
        // arithmetic operators
621
        // prefix operator
622
        inline cfloat operator-() const {
8,722,901✔
623
                cfloat tmp(*this);
8,722,901✔
624
                tmp._block[MSU] ^= SIGN_BIT_MASK;
8,722,901✔
625
                return tmp;
8,722,901✔
626
        }
627

628
        cfloat& operator+=(const cfloat& rhs) CFLOAT_EXCEPT {
19,171,153✔
629
                if constexpr (_trace_add) std::cout << "---------------------- ADD -------------------" << std::endl;
630
                // special case handling of the inputs
631
#if CFLOAT_THROW_ARITHMETIC_EXCEPTION
632
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
744✔
633
                        throw cfloat_operand_is_nan{};
×
634
                }
635
#else
636
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
19,170,409✔
637
                        setnan(NAN_TYPE_SIGNALLING);
234,874✔
638
                        return *this;
234,874✔
639
                }
640
                if (isnan(NAN_TYPE_QUIET) || rhs.isnan(NAN_TYPE_QUIET)) {
18,935,535✔
641
                        setnan(NAN_TYPE_QUIET);
215,298✔
642
                        return *this;
215,298✔
643
                }
644
#endif
645
                // normal + inf  = inf
646
                // normal + -inf = -inf
647
                // inf + normal = inf
648
                // inf + inf    = inf
649
                // inf + -inf    = ?
650
                // -inf + normal = -inf
651
                // -inf + -inf   = -inf
652
                // -inf + inf    = ?
653
                if (isinf()) {
18,720,981✔
654
                        if (rhs.isinf()) {
60,327✔
655
                                if (sign() != rhs.sign()) {
939✔
656
                                        setnan(NAN_TYPE_SIGNALLING);
514✔
657
                                }
658
                                return *this;
939✔
659
                        }
660
                        else {
661
                                return *this;
59,388✔
662
                        }
663
                }
664
                else {
665
                        if (rhs.isinf()) {
18,660,654✔
666
                                *this = rhs;
60,431✔
667
                                return *this;
60,431✔
668
                        }
669
                }
670

671
                if (iszero()) {
18,600,223✔
672
                        *this = rhs;
214,549✔
673
                        return *this;
214,549✔
674
                }
675
                if (rhs.iszero()) return *this;
18,385,674✔
676

677
                // arithmetic operation
678
                blocktriple<fbits, BlockTripleOperator::ADD, bt> a, b, sum;
18,216,903✔
679

680
                // transform the inputs into (sign,scale,significant) 
681
                normalizeAddition(a); 
18,216,903✔
682
                rhs.normalizeAddition(b);
18,216,903✔
683
                sum.add(a, b);
18,216,903✔
684

685
                convert(sum, *this);
18,216,903✔
686

687
                return *this;
18,216,903✔
688
        }
689
        cfloat& operator+=(double rhs) CFLOAT_EXCEPT {
690
                return *this += cfloat(rhs);
691
        }
692
        cfloat& operator-=(const cfloat& rhs) CFLOAT_EXCEPT {
8,805,856✔
693
                if constexpr (_trace_sub) std::cout << "---------------------- SUB -------------------" << std::endl;
694
                if (rhs.isnan()) 
8,805,856✔
695
                        return *this += rhs;
170,204✔
696
                else 
697
                        return *this += -rhs;
8,635,652✔
698
        }
699
        cfloat& operator-=(double rhs) CFLOAT_EXCEPT {
8✔
700
                return *this -= cfloat(rhs);
8✔
701
        }
702
        cfloat& operator*=(const cfloat& rhs) CFLOAT_EXCEPT {
4,202,561✔
703
                if constexpr (_trace_mul) std::cout << "---------------------- MUL -------------------\n";
322✔
704
                // special case handling of the inputs
705
#if CFLOAT_THROW_ARITHMETIC_EXCEPTION
706
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
612✔
707
                        throw cfloat_operand_is_nan{};
×
708
                }
709
#else
710
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
4,201,949✔
711
                        setnan(NAN_TYPE_SIGNALLING);
124,659✔
712
                        return *this;
124,659✔
713
                }
714
                if (isnan(NAN_TYPE_QUIET) || rhs.isnan(NAN_TYPE_QUIET)) {
4,077,290✔
715
                        setnan(NAN_TYPE_QUIET);
113,355✔
716
                        return *this;
113,355✔
717
                }
718
#endif
719
                //  inf * inf = inf
720
                //  inf * -inf = -inf
721
                // -inf * inf = -inf
722
                // -inf * -inf = inf
723
                //        0 * inf = -nan(ind)
724
                //        inf * 0 = -nan(ind)
725
                bool resultSign = sign() != rhs.sign();
3,964,547✔
726
                if (isinf()) {
3,964,547✔
727
                        if (rhs.iszero()) {
24,457✔
728
                                setnan(NAN_TYPE_QUIET);
1,591✔
729
                        }
730
                        else {
731
                                setsign(resultSign);
22,866✔
732
                        }
733
                        return *this;
24,457✔
734
                }
735
                if (rhs.isinf()) {
3,940,090✔
736
                        if (iszero()) {
25,815✔
737
                                setnan(NAN_TYPE_QUIET);
2,969✔
738
                        }
739
                        else {
740
                                setinf(resultSign);
22,846✔
741
                        }
742
                        return *this;
25,815✔
743
                }
744

745
                if (iszero() || rhs.iszero()) {                        
3,914,275✔
746
                        setzero();
1,816,893✔
747
                        setsign(resultSign); // deal with negative 0
1,816,893✔
748
                        return *this;
1,816,893✔
749
                }
750

751
                // arithmetic operation
752
                blocktriple<fbits, BlockTripleOperator::MUL, bt> a, b, product;
2,097,382✔
753

754
                // transform the inputs into (sign,scale,significant) 
755
                // triples of the correct width
756
                normalizeMultiplication(a);
2,097,382✔
757
                rhs.normalizeMultiplication(b);
2,097,382✔
758
                product.mul(a, b);
2,097,382✔
759
                convert(product, *this);
2,097,382✔
760

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

763
                return *this;
2,097,382✔
764
        }
765
        cfloat& operator*=(double rhs) CFLOAT_EXCEPT {
40✔
766
                return *this *= cfloat(rhs);
40✔
767
        }
768
        cfloat& operator/=(const cfloat& rhs) CFLOAT_EXCEPT {
5,022,569✔
769
                if constexpr (_trace_div) std::cout << "---------------------- DIV -------------------" << std::endl;
770

771
                // special case handling of the inputs
772
                // qnan / qnan = qnan
773
                // qnan / snan = qnan
774
                // snan / qnan = snan
775
                // snan / snan = snan
776
#if CFLOAT_THROW_ARITHMETIC_EXCEPTION
777
                if (rhs.iszero()) throw cfloat_divide_by_zero();
49✔
778
                if (rhs.isnan()) throw cfloat_divide_by_nan();
48✔
779
                if (isnan()) throw cfloat_operand_is_nan();
48✔
780
#else
781
                if (isnan(NAN_TYPE_SIGNALLING) || rhs.isnan(NAN_TYPE_SIGNALLING)) {
5,022,520✔
782
                        setnan(NAN_TYPE_SIGNALLING);
162,762✔
783
                        return *this;
162,762✔
784
                }
785
                if (isnan(NAN_TYPE_QUIET) || rhs.isnan(NAN_TYPE_QUIET)) {
4,859,758✔
786
                        setnan(NAN_TYPE_QUIET);
149,935✔
787
                        return *this;
149,935✔
788
                }
789
                if (rhs.iszero()) {
4,709,823✔
790
                        if (iszero()) {
169,761✔
791
                                // zero divide by zero yields quiet NaN (in MSVC it is labeled -nan(ind) for indeterminate)
792
                                setnan(NAN_TYPE_QUIET);
29,299✔
793
                        }
794
                        else {
795
                                // non-zero divide by zero yields INF
796
                                bool resultSign = sign() != rhs.sign();
140,462✔
797
                                setinf(resultSign);
140,462✔
798
                        }
799
                        return *this;
169,761✔
800
                }
801
#endif
802
                //  inf /  inf = -nan(ind)
803
                //  inf / -inf = -nan(ind)
804
                // -inf /  inf = -nan(ind)
805
                // -inf / -inf = -nan(ind)
806
                //        1.0 /  inf = 0
807
                bool resultSign = sign() != rhs.sign();
4,540,110✔
808
                if (isinf()) {
4,540,110✔
809
                        if (rhs.isinf()) {
31,087✔
810
                                // inf divide by inf yields quiet NaN (in MSVC it is labeled -nan(ind) for indeterminate)
811
                                setnan(NAN_TYPE_QUIET);
559✔
812
                                return *this;
559✔
813
                        }
814
                        else {
815
                                // we stay an infinite but may change sign
816
                                setsign(resultSign);
30,528✔
817
                                return *this;
30,528✔
818
                        }
819
                }
820
                else {
821
                        if (rhs.isinf()) {
4,509,023✔
822
                                setzero();
32,833✔
823
                                setsign(resultSign);
32,833✔
824
                                return *this;
32,833✔
825
                        }
826
                }
827

828
                if (iszero()) {
4,476,190✔
829
                        setzero();
139,452✔
830
                        setsign(resultSign); // deal with negative 0
139,452✔
831
                        return *this;
139,452✔
832
                }
833

834
                // arithmetic operation
835
                using BlockTriple = blocktriple<fbits, BlockTripleOperator::DIV, bt>;
836
                BlockTriple a, b, quotient;
4,336,738✔
837

838
                // transform the inputs into (sign,scale,significant) 
839
                // triples of the correct width
840
                normalizeDivision(a);
4,336,738✔
841
                rhs.normalizeDivision(b);
4,336,738✔
842
                quotient.div(a, b);
4,336,738✔
843
                quotient.setradix(BlockTriple::radix);
4,336,738✔
844
                convert(quotient, *this);
4,336,738✔
845

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

848
                return *this;
4,336,738✔
849
        }
850
        cfloat& operator/=(double rhs) CFLOAT_EXCEPT {
851
                return *this /= cfloat(rhs);
852
        }
853
        cfloat& reciprocal() CFLOAT_EXCEPT {
854
                cfloat c = 1.0 / *this;
855
                return *this = c;
856
        }
857
        /// <summary>
858
        /// move to the next bit encoding modulo 2^nbits
859
        /// </summary>
860
        /// <typeparam name="bt"></typeparam>
861
        cfloat& operator++() {
4,596,090✔
862
                if constexpr (0 == nrBlocks) {
863
                        return *this;
864
                }
865
                else if constexpr (1 == nrBlocks) {
866
                        if (sign()) {
861,703✔
867
                                if (_block[MSU] == (SIGN_BIT_MASK | 1ul)) { // pattern: 1.00.001 = minneg
467✔
868
                                        _block[MSU] = 0; // pattern: 0.00.000 = +0 
7✔
869
                                }
870
                                else {
871
                                        --_block[MSU];
460✔
872
                                }
873
                                if constexpr (!hasSubnormals) {
874
                                        if (isdenormal()) {
198✔
875
                                                // special case, we need to jump past all the subnormal value encodings which puts us on 0
876
                                                _block[MSU] = 0; // pattern: 0.00.000 = +0
3✔
877
                                        }
878
                                }
879
                        }
880
                        else {
881
                                if constexpr (!hasSubnormals) {
882
                                        if (_block[MSU] == 0) {
384,649✔
883
                                                // special case, we need to jump past all the subnormal value encodings minus 1
884
                                                setfraction(0xFFFF'FFFF'FFFF'FFFFull);
4✔
885
                                        }
886
                                }
887
                                if ((_block[MSU] & (MSU_MASK >> 1)) == (MSU_MASK >> 1)) { // pattern: 0.11.111 = nan
861,236✔
888
                                        _block[MSU] |= SIGN_BIT_MASK; // pattern: 1.11.111 = snan : wrap to the other side of the encoding
×
889
                                }
890
                                else {
891
                                        ++_block[MSU];
861,236✔
892
                                }
893
                        }
894
                }
895
                else {
896
                        if (sign()) {
3,734,387✔
897
                                // special case: pattern: 1.00.001 = minneg transitions to pattern: 0.00.000 = +0 
898
                                if (isminnegencoding()) {
66,947✔
899
                                        setzero();
8✔
900
                                }
901
                                else {
902
                                        //  1111 0000
903
                                        //  1110 1111
904
                                        bool borrow = true;
66,939✔
905
                                        for (unsigned i = 0; i < MSU; ++i) {
199,415✔
906
                                                if (borrow) {
132,476✔
907
                                                        if ((_block[i] & storageMask) == 0) { // block will underflow
67,197✔
908
                                                                --_block[i];
261✔
909
                                                                borrow = true;
261✔
910
                                                        }
911
                                                        else {
912
                                                                --_block[i];
66,936✔
913
                                                                borrow = false;
66,936✔
914
                                                        }
915
                                                }
916
                                        }
917
                                        if (borrow) {
66,939✔
918
                                                --_block[MSU];
3✔
919
                                        }
920
                                        if constexpr (!hasSubnormals) {
921
                                                if (isdenormal()) {
385✔
922
                                                        // special case, we need to jump past all the subnormal value encodings which puts us on 0
923
                                                        setzero(); // pattern: 0.00.000 = +0
3✔
924
                                                }
925
                                        }
926
                                }
927
                        }
928
                        else {
929
                                // special case: pattern: 0.11.111 = nan transitions to pattern: 1.11.111 = snan 
930
                                if (isnanencoding()) {
3,667,440✔
931
                                        setnan(NAN_TYPE_SIGNALLING);
×
932
                                }
933
                                else {
934
                                        if constexpr (!hasSubnormals) {
935
                                                if (iszero()) {
1,720,661✔
936
                                                        // 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
937
                                                        setfraction(0xFFFF'FFFF'FFFF'FFFFull);
3✔
938
                                                }
939
                                        }
940
                                        bool carry = true;
3,667,440✔
941
                                        for (unsigned i = 0; i < MSU; ++i) {
7,400,782✔
942
                                                if (carry) {
3,733,342✔
943
                                                        if ((_block[i] & storageMask) == storageMask) { // block will overflow
3,667,695✔
944
                                                                _block[i] = 0;
256✔
945
                                                                carry = true;
256✔
946
                                                        }
947
                                                        else {
948
                                                                ++_block[i];
3,667,439✔
949
                                                                carry = false;
3,667,439✔
950
                                                        }
951
                                                }
952
                                        }
953
                                        if (carry) {
3,667,440✔
954
                                                ++_block[MSU];
1✔
955
                                        }
956
                                }
957
                        }
958
                }
959
                return *this;
4,596,090✔
960
        }
961
        cfloat operator++(int) {
134,800✔
962
                cfloat tmp(*this);
134,800✔
963
                operator++();
134,800✔
964
                return tmp;
134,800✔
965
        }
966
        cfloat& operator--() {
5,014,100✔
967
                if constexpr (0 == nrBlocks) {
968
                        return *this;
969
                }
970
                else if constexpr (1 == nrBlocks) {
971
                        if (sign()) {
942,793✔
972
                                ++_block[MSU];
942,299✔
973
                        }
974
                        else {
975
                                // positive range
976
                                if (_block[MSU] == 0) { // pattern: 0.00.000 = 0
494✔
977
                                        if constexpr (hasSubnormals) {
978
                                                _block[MSU] |= SIGN_BIT_MASK | bt(1u); // pattern: 1.00.001 = minneg 
8✔
979
                                        }
980
                                        else {
981
                                                // special case, we need to jump past all the subnormal value encodings
982
                                                setfraction(0xFFFF'FFFF'FFFF'FFFFull); // set to 0.00.11...11
3✔
983
                                                ++_block[MSU]; // increment into 0.01.0000
3✔
984
                                                _block[MSU] |= SIGN_BIT_MASK; // set to 1.01.0000
3✔
985
                                        }
986
                                }
987
                                else {
988
                                        --_block[MSU];
483✔
989
                                }
990
                                if constexpr (!hasSubnormals) {
991
                                        if (isdenormal()) {
211✔
992
                                                // special case, we need to jump past all the subnormal value encodings which puts us on 0
993
                                                _block[MSU] = 0; // pattern: 0.00.000 = +0
3✔
994
                                        }
995
                                }
996
                        }
997
                }
998
                else {
999
                        if (sign()) {
4,071,307✔
1000
                                bool carry = true;
4,004,349✔
1001
                                for (unsigned i = 0; i < MSU; ++i) {
8,074,235✔
1002
                                        if (carry) {
4,069,886✔
1003
                                                if ((_block[i] & storageMask) == storageMask) { // block will overflow
4,004,604✔
1004
                                                        _block[i] = 0;
256✔
1005
                                                        carry = true;
256✔
1006
                                                }
1007
                                                else {
1008
                                                        ++_block[i];
4,004,348✔
1009
                                                        carry = false;
4,004,348✔
1010
                                                }
1011
                                        }
1012
                                }
1013
                                if (carry) {
4,004,349✔
1014
                                        ++_block[MSU];
1✔
1015
                                }
1016
                        }
1017
                        else {
1018
                                // special case: pattern: 0.00.000 = +0 transitions to pattern: 1.00.001 = minneg
1019
                                if (iszeroencoding()) {
66,958✔
1020
                                        if constexpr (hasSubnormals) {
1021
                                                setsign(true);
8✔
1022
                                                setbit(0, true);
8✔
1023
                                        }
1024
                                        else {
1025
                                                // special case, we need to jump past all the subnormal value encodings 1.01.0000 = minneg normal
1026
                                                setexponent(1 - EXP_BIAS);
2✔
1027
                                                setsign(true);
2✔
1028
                                        }
1029
                                }
1030
                                else {
1031
                                        bool borrow = true;
66,948✔
1032
                                        for (unsigned i = 0; i < MSU; ++i) {
199,441✔
1033
                                                if (borrow) {
132,493✔
1034
                                                        if ((_block[i] & storageMask) == 0) { // block will underflow
67,209✔
1035
                                                                --_block[i];
266✔
1036
                                                                borrow = true;
266✔
1037
                                                        }
1038
                                                        else {
1039
                                                                --_block[i];
66,943✔
1040
                                                                borrow = false;
66,943✔
1041
                                                        }
1042
                                                }
1043
                                        }
1044
                                        if (borrow) {
66,948✔
1045
                                                --_block[MSU];
5✔
1046
                                        }
1047
                                        if constexpr (!hasSubnormals) {
1048
                                                if (isdenormal()) {
384✔
1049
                                                        // special case, we need to jump past all the subnormal value encodings which puts us on 0
1050
                                                        setzero(); // pattern: 0.00.000 = +0
2✔
1051
                                                }
1052
                                        }
1053
                                }
1054
                        }
1055
                }
1056
                return *this;
5,014,100✔
1057
        }
1058
        cfloat operator--(int) {
134,812✔
1059
                cfloat tmp(*this);
134,812✔
1060
                operator--();
134,812✔
1061
                return tmp;
134,812✔
1062
        }
1063

1064
        // modifiers        
1065
        constexpr void clear() noexcept {
143,249,711✔
1066
                if constexpr (0 == nrBlocks) {
1067
                        return;
1068
                }
1069
                else if constexpr (1 == nrBlocks) {
1070
                        _block[0] = bt(0);
41,084,007✔
1071
                }
1072
                else if constexpr (2 == nrBlocks) {
1073
                        _block[0] = bt(0);
48,058,389✔
1074
                        _block[1] = bt(0);
48,058,389✔
1075
                }
1076
                else if constexpr (3 == nrBlocks) {
1077
                        _block[0] = bt(0);
53,973,317✔
1078
                        _block[1] = bt(0);
53,973,317✔
1079
                        _block[2] = bt(0);
53,973,317✔
1080
                }
1081
                else if constexpr (4 == nrBlocks) {
1082
                        _block[0] = bt(0);
43,315✔
1083
                        _block[1] = bt(0);
43,315✔
1084
                        _block[2] = bt(0);
43,315✔
1085
                        _block[3] = bt(0);
43,315✔
1086
                }
1087
                else if constexpr (5 == nrBlocks) {
1088
                        _block[0] = bt(0);
29,999✔
1089
                        _block[1] = bt(0);
29,999✔
1090
                        _block[2] = bt(0);
29,999✔
1091
                        _block[3] = bt(0);
29,999✔
1092
                        _block[4] = bt(0);
29,999✔
1093
                }
1094
                else if constexpr (6 == nrBlocks) {
1095
                        _block[0] = bt(0);
10,015✔
1096
                        _block[1] = bt(0);
10,015✔
1097
                        _block[2] = bt(0);
10,015✔
1098
                        _block[3] = bt(0);
10,015✔
1099
                        _block[4] = bt(0);
10,015✔
1100
                        _block[5] = bt(0);
10,015✔
1101
                }
1102
                else if constexpr (7 == nrBlocks) {
1103
                        _block[0] = bt(0);
9,999✔
1104
                        _block[1] = bt(0);
9,999✔
1105
                        _block[2] = bt(0);
9,999✔
1106
                        _block[3] = bt(0);
9,999✔
1107
                        _block[4] = bt(0);
9,999✔
1108
                        _block[5] = bt(0);
9,999✔
1109
                        _block[6] = bt(0);
9,999✔
1110
                }
1111
                else if constexpr (8 == nrBlocks) {
1112
                        _block[0] = bt(0);
20,323✔
1113
                        _block[1] = bt(0);
20,323✔
1114
                        _block[2] = bt(0);
20,323✔
1115
                        _block[3] = bt(0);
20,323✔
1116
                        _block[4] = bt(0);
20,323✔
1117
                        _block[5] = bt(0);
20,323✔
1118
                        _block[6] = bt(0);
20,323✔
1119
                        _block[7] = bt(0);
20,323✔
1120
                }
1121
                else {
1122
                        for (unsigned i = 0; i < nrBlocks; ++i) {
224,339✔
1123
                                _block[i] = bt(0);
203,992✔
1124
                        }
1125
                }
1126
        }
143,249,711✔
1127
        constexpr void setzero() noexcept { clear(); }
2,617,200✔
1128
        constexpr void setinf(bool sign = true) noexcept {
6,548,847✔
1129
                // the Inf encoding is the pattern 0b0'11...11'11...10 for a +inf, and 0b1'11...11'11...110 for a -inf
1130
                if constexpr (0 == nrBlocks) {
1131
                        return;
1132
                }
1133
                else if constexpr (1 == nrBlocks) {
1134
                        _block[MSU] = sign ? bt(MSU_MASK ^ LSB_BIT_MASK) : bt(~SIGN_BIT_MASK & (MSU_MASK ^ LSB_BIT_MASK));
2,192,480✔
1135
                }
1136
                else if constexpr (2 == nrBlocks) {
1137
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
1,859,414✔
1138
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
1,859,414✔
1139
                }
1140
                else if constexpr (3 == nrBlocks) {
1141
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
2,493,885✔
1142
                        _block[1] = BLOCK_MASK;
2,493,885✔
1143
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
2,493,885✔
1144
                }
1145
                else if constexpr (4 == nrBlocks) {
1146
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
2,808✔
1147
                        _block[1] = BLOCK_MASK;
2,808✔
1148
                        _block[2] = BLOCK_MASK;
2,808✔
1149
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
2,808✔
1150
                }
1151
                else if constexpr (5 == nrBlocks) {
1152
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
80✔
1153
                        _block[1] = BLOCK_MASK;
80✔
1154
                        _block[2] = BLOCK_MASK;
80✔
1155
                        _block[3] = BLOCK_MASK;
80✔
1156
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
80✔
1157
                }
1158
                else if constexpr (6 == nrBlocks) {
1159
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
37✔
1160
                        _block[1] = BLOCK_MASK;
37✔
1161
                        _block[2] = BLOCK_MASK;
37✔
1162
                        _block[3] = BLOCK_MASK;
37✔
1163
                        _block[4] = BLOCK_MASK;
37✔
1164
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
37✔
1165
                }
1166
                else if constexpr (7 == nrBlocks) {
1167
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
37✔
1168
                        _block[1] = BLOCK_MASK;
37✔
1169
                        _block[2] = BLOCK_MASK;
37✔
1170
                        _block[3] = BLOCK_MASK;
37✔
1171
                        _block[4] = BLOCK_MASK;
37✔
1172
                        _block[5] = BLOCK_MASK;
37✔
1173
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
37✔
1174
                }
1175
                else if constexpr (8 == nrBlocks) {
1176
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
78✔
1177
                        _block[1] = BLOCK_MASK;
78✔
1178
                        _block[2] = BLOCK_MASK;
78✔
1179
                        _block[3] = BLOCK_MASK;
78✔
1180
                        _block[4] = BLOCK_MASK;
78✔
1181
                        _block[5] = BLOCK_MASK;
78✔
1182
                        _block[6] = BLOCK_MASK;
78✔
1183
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
78✔
1184
                }
1185
                else {
1186
                        _block[0] = BLOCK_MASK ^ LSB_BIT_MASK;
28✔
1187
                        for (unsigned i = 1; i < nrBlocks - 1; ++i) {
252✔
1188
                                _block[i] = BLOCK_MASK;
224✔
1189
                        }
1190
                        _block[MSU] = sign ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
28✔
1191
                }        
1192
        }
6,548,847✔
1193
        constexpr void setnan(int NaNType = NAN_TYPE_SIGNALLING) noexcept {
7,118,308✔
1194
                // 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
1195
                if constexpr (0 == nrBlocks) {
1196
                        return;
1197
                }
1198
                else if constexpr (1 == nrBlocks) {
1199
                        // fall through
1200
                }
1201
                else if constexpr (2 == nrBlocks) {
1202
                        _block[0] = BLOCK_MASK;
1,994,929✔
1203
                }
1204
                else if constexpr (3 == nrBlocks) {
1205
                        _block[0] = BLOCK_MASK;
1,048,705✔
1206
                        _block[1] = BLOCK_MASK;
1,048,705✔
1207
                }
1208
                else if constexpr (4 == nrBlocks) {
1209
                        _block[0] = BLOCK_MASK;
30✔
1210
                        _block[1] = BLOCK_MASK;
30✔
1211
                        _block[2] = BLOCK_MASK;
30✔
1212
                }
1213
                else if constexpr (5 == nrBlocks) {
1214
                        _block[0] = BLOCK_MASK;
9✔
1215
                        _block[1] = BLOCK_MASK;
9✔
1216
                        _block[2] = BLOCK_MASK;
9✔
1217
                        _block[3] = BLOCK_MASK;
9✔
1218
                }
1219
                else if constexpr (6 == nrBlocks) {
1220
                        _block[0] = BLOCK_MASK;
4✔
1221
                        _block[1] = BLOCK_MASK;
4✔
1222
                        _block[2] = BLOCK_MASK;
4✔
1223
                        _block[3] = BLOCK_MASK;
4✔
1224
                        _block[4] = BLOCK_MASK;
4✔
1225
                }
1226
                else if constexpr (7 == nrBlocks) {
1227
                        _block[0] = BLOCK_MASK;
4✔
1228
                        _block[1] = BLOCK_MASK;
4✔
1229
                        _block[2] = BLOCK_MASK;
4✔
1230
                        _block[3] = BLOCK_MASK;
4✔
1231
                        _block[4] = BLOCK_MASK;
4✔
1232
                        _block[5] = BLOCK_MASK;
4✔
1233
                }
1234
                else if constexpr (8 == nrBlocks) {
1235
                        _block[0] = BLOCK_MASK;
4✔
1236
                        _block[1] = BLOCK_MASK;
4✔
1237
                        _block[2] = BLOCK_MASK;
4✔
1238
                        _block[3] = BLOCK_MASK;
4✔
1239
                        _block[4] = BLOCK_MASK;
4✔
1240
                        _block[5] = BLOCK_MASK;
4✔
1241
                        _block[6] = BLOCK_MASK;
4✔
1242
                }
1243
                else {
1244
                        for (unsigned i = 0; i < nrBlocks - 1; ++i) {
60✔
1245
                                _block[i] = BLOCK_MASK;
54✔
1246
                        }
1247
                }
1248
                _block[MSU] = NaNType == NAN_TYPE_SIGNALLING ? MSU_MASK : bt(~SIGN_BIT_MASK & MSU_MASK);
7,118,308✔
1249
        }
7,118,308✔
1250
        constexpr void setsign(bool sign = true) {
3,402,901✔
1251
                if (sign) {
3,402,901✔
1252
                        _block[MSU] |= SIGN_BIT_MASK;
614,687✔
1253
                }
1254
                else {
1255
                        _block[MSU] &= ~SIGN_BIT_MASK;
2,788,214✔
1256
                }
1257
        }
3,402,901✔
1258
        constexpr bool setexponent(int scale) {
533,850✔
1259
                if (scale < MIN_EXP_SUBNORMAL || scale > MAX_EXP) return false; // this scale cannot be represented
533,850✔
1260
                if constexpr (nbits < 65) {
1261
                        uint32_t exponentBits = static_cast<uint32_t>(scale + EXP_BIAS);
532,309✔
1262
                        if (scale >= MIN_EXP_SUBNORMAL && scale < MIN_EXP_NORMAL) {
532,309✔
1263
                                // setexponent() only writes the encoding field. In the subnormal interval the true scale is carried
1264
                                // by a zero exponent field plus a left-shifted fraction, so callers must have already denormalized
1265
                                // the significand before asking for this exponent pattern.
1266
                                // we are a subnormal number: all exponent bits are 0
1267
                                exponentBits = 0;
45✔
1268
                        }
1269
                        // TODO: optimize
1270
                        uint32_t mask = (1ul << (es - 1));
532,309✔
1271
                        for (unsigned i = nbits - 2; i > nbits - 2 - es; --i) {
4,800,643✔
1272
                                setbit(i, (mask & exponentBits));
4,268,334✔
1273
                                mask >>= 1;
4,268,334✔
1274
                        }
1275
                }
1276
                else {
1277
                        uint32_t exponentBits = static_cast<uint32_t>(scale + EXP_BIAS);
1,541✔
1278
                        uint32_t mask = (1ul << (es - 1));
1,541✔
1279
                        for (unsigned i = nbits - 2; i > nbits - 2 - es; --i) {
25,156✔
1280
                                setbit(i, (mask & exponentBits));
23,615✔
1281
                                mask >>= 1;
23,615✔
1282
                        }
1283
                }
1284
                return true;
533,850✔
1285
        }
1286
        constexpr void setfraction(uint64_t raw_bits) {
610✔
1287
                // unoptimized as it is not meant to be an end-user API, it is a test API
1288
                // raw_bits is uint64_t so can have at most 64 bits of fraction data
1289
                constexpr unsigned bitsToSet = (fbits < 64) ? fbits : 64;
610✔
1290
                uint64_t mask{ 1ull };
610✔
1291
                for (unsigned i = 0; i < bitsToSet; ++i) {
24,744✔
1292
                        setbit(i, (mask & raw_bits));
24,134✔
1293
                        mask <<= 1;
24,134✔
1294
                }
1295
        }
610✔
1296
        constexpr void setbit(unsigned i, bool v = true) noexcept {
8,137,427✔
1297
                unsigned blockIndex = i / bitsInBlock;
8,137,427✔
1298
                if (blockIndex < nrBlocks) {
8,137,427✔
1299
                        bt block = _block[blockIndex];
8,137,427✔
1300
                        bt null = ~(1ull << (i % bitsInBlock));
8,137,427✔
1301
                        bt bit = bt(v ? 1 : 0);
8,137,427✔
1302
                        bt mask = bt(bit << (i % bitsInBlock));
8,137,427✔
1303
                        _block[blockIndex] = bt((block & null) | mask);
8,137,427✔
1304
                }
1305
        }
8,137,427✔
1306
        constexpr cfloat& setbits(uint64_t raw_bits) noexcept {
307,364,040✔
1307
                if constexpr (0 == nrBlocks) {
1308
                        return *this;
1309
                }
1310
                else if constexpr (1 == nrBlocks) {
1311
                        _block[0] = raw_bits & storageMask;
89,990,063✔
1312
                }
1313
                else if constexpr (2 == nrBlocks) {
1314
                        if constexpr (bitsInBlock < 64) {
1315
                                _block[0] = raw_bits & storageMask;
103,256,461✔
1316
                                raw_bits >>= bitsInBlock;
103,256,461✔
1317
                                _block[1] = raw_bits & storageMask;
103,256,461✔
1318
                        }
1319
                        else {
1320
                                _block[0] = raw_bits & storageMask;
1321
                                _block[1] = 0;
1322
                        }
1323
                }
1324
                else if constexpr (3 == nrBlocks) {
1325
                        if constexpr (bitsInBlock < 64) {
1326
                                _block[0] = raw_bits & storageMask;
113,957,367✔
1327
                                raw_bits >>= bitsInBlock;
113,957,367✔
1328
                                _block[1] = raw_bits & storageMask;
113,957,367✔
1329
                                raw_bits >>= bitsInBlock;
113,957,367✔
1330
                                _block[2] = raw_bits & storageMask;
113,957,367✔
1331
                        }
1332
                        else {
1333
                                _block[0] = raw_bits & storageMask;
1334
                                _block[1] = 0;
1335
                                _block[2] = 0;
1336
                        }
1337
                }
1338
                else if constexpr (4 == nrBlocks) {
1339
                        if constexpr (bitsInBlock < 64) {
1340
                                _block[0] = raw_bits & storageMask;
70,122✔
1341
                                raw_bits >>= bitsInBlock;
70,122✔
1342
                                _block[1] = raw_bits & storageMask;
70,122✔
1343
                                raw_bits >>= bitsInBlock;
70,122✔
1344
                                _block[2] = raw_bits & storageMask;
70,122✔
1345
                                raw_bits >>= bitsInBlock;
70,122✔
1346
                                _block[3] = raw_bits & storageMask;
70,122✔
1347
                        }
1348
                        else {
1349
                                _block[0] = raw_bits & storageMask;
1350
                                _block[1] = 0;
1351
                                _block[2] = 0;
1352
                                _block[3] = 0;
1353
                        }
1354
                }
1355
                else {
1356
                        if constexpr (bitsInBlock < 64) {
1357
                                for (unsigned i = 0; i < nrBlocks; ++i) {
730,864✔
1358
                                        _block[i] = raw_bits & storageMask;
640,837✔
1359
                                        raw_bits >>= bitsInBlock;
640,837✔
1360
                                }
1361
                        }
1362
                        else {
1363
                                _block[0] = raw_bits & storageMask;
1364
                                for (unsigned i = 1; i < nrBlocks; ++i) {
1365
                                        _block[i] = 0;
1366
                                }
1367
                        }
1368
                }
1369
                _block[MSU] &= MSU_MASK; // enforce precondition for fast comparison by properly nulling bits that are outside of nbits
307,364,040✔
1370
                return *this;
307,364,040✔
1371
        }
1372
        constexpr void setblock(unsigned b, bt data) noexcept {
542,395✔
1373
                if (b < nrBlocks) _block[b] = data;
542,395✔
1374
        }
542,395✔
1375
        
1376
        // create specific number system values of interest
1377
        constexpr cfloat& maxpos() noexcept {
936,039✔
1378
                if constexpr (isSaturating) {
1379
                        // in a saturating encoding with max-exponent values we are removing the Inf encoding pattern 0b0'11...11'11...10 for a +inf, 
1380
                        // and 0b1'11...11'11...110 for a -inf and using it as a value
1381
                        if constexpr (hasMaxExpValues) {
1382
                                // maximum positive value has this bit pattern: 0-1...1-111...110, that is, sign = 0, e = 11..11, f = 111...110
1383
                                clear();
935,334✔
1384
                                flip();
935,334✔
1385
                                setbit(nbits - 1ull, false); // sign = 0
935,334✔
1386
                                setbit(0ull, false); // bit0 = 0
935,334✔
1387
                        }
1388
                        else {
1389
                                // maximum positive value has this bit pattern: 0-11...10-111...111, that is, sign = 0, e = 11..10, f = 111...111
1390
                                clear();
420✔
1391
                                flip();
420✔
1392
                                setbit(fbits, false); // set least significant exponent bit to 0
420✔
1393
                                setbit(nbits - 1ull, false); // set sign to 0
420✔
1394
                        }
1395
                }
1396
                else {
1397
                        // the Inf encoding is the pattern 0b0'11...11'11...10 for a +inf, and 0b1'11...11'11...110 for a -inf
1398
                        // the maxpos is the encoding before that
1399
                        if constexpr (hasMaxExpValues) {
1400
                                // maximum positive value has this bit pattern: 0-1...1-111...101, that is, sign = 0, e = 11..11, f = 111...101
1401
                                clear();
113✔
1402
                                flip();
113✔
1403
                                setbit(nbits - 1ull, false); // sign = 0
113✔
1404
                                setbit(1ull, false); // bit1 = 0
113✔
1405
                        }
1406
                        else {
1407
                                // maximum positive value has this bit pattern: 0-1...0-111...111, that is, sign = 0, e = 11..10, f = 111...111
1408
                                clear();
172✔
1409
                                flip();
172✔
1410
                                setbit(fbits, false); // set least significant exponent bit to 0
172✔
1411
                                setbit(nbits - 1ull, false); // set sign to 0
172✔
1412
                        }
1413
                }
1414
                return *this;
936,039✔
1415
        }
1416
        constexpr cfloat& minpos() noexcept {
13,290✔
1417
                // minpos encoding is not impacted by saturating encodings, which only affects maxpos and inf
1418
                if constexpr (hasSubnormals) {
1419
                        // minimum positive value has this bit pattern: 0-000-00...01, that is, sign = 0, e = 000, f = 00001
1420
                        clear();
327✔
1421
                        setbit(0);
327✔
1422
                }
1423
                else {
1424
                        // minimum positive value has this bit pattern: 0-001-00...0, that is, sign = 0, e = 001, f = 0000
1425
                        clear();
12,963✔
1426
                        setbit(fbits);
12,963✔
1427
                }
1428
                return *this;
13,290✔
1429
        }
1430
        constexpr cfloat& zero() noexcept {
1✔
1431
                // the zero value
1432
                clear();
1✔
1433
                return *this;
1✔
1434
        }
1435
        constexpr cfloat& minneg() noexcept {
14✔
1436
                // minneg encoding is not impacted by saturating encodings, which only affects maxpos and inf
1437
                if constexpr (hasSubnormals) {
1438
                        // minimum negative value has this bit pattern: 1-000-00...01, that is, sign = 1, e = 00, f = 00001
1439
                        clear();
8✔
1440
                        setbit(nbits - 1ull);
8✔
1441
                        setbit(0);
8✔
1442
                }
1443
                else {
1444
                        // minimum negative value has this bit pattern: 1-001-00...0, that is, sign = 1, e = 001, f = 0000
1445
                        clear();
6✔
1446
                        setbit(fbits);
6✔
1447
                        setbit(nbits - 1ull);
6✔
1448
                }
1449
                return *this;
14✔
1450
        }
1451
        constexpr cfloat& maxneg() noexcept {
935,702✔
1452
                if constexpr (isSaturating) {
1453
                        // in a saturating encoding with max-exponent values we are removing the Inf encoding pattern 0b0'11...11'11...10 for a +inf, 
1454
                        // and 0b1'11...11'11...110 for a -inf and using it as a value
1455
                        if constexpr (hasMaxExpValues) {
1456
                                // maximum negative value has this bit pattern: 1-1...1-111...110, that is, sign = 1, e = 1..1, f = 111...110
1457
                                clear();
935,240✔
1458
                                flip();
935,240✔
1459
                                setbit(0ull, false);
935,240✔
1460
                        }
1461
                        else {
1462
                                // maximum negative value has this bit pattern: 1-1...0-111...111, that is, sign = 1, e = 11..10, f = 111...111
1463
                                clear();
401✔
1464
                                flip();
401✔
1465
                                setbit(fbits, false);
401✔
1466
                        }
1467
                }
1468
                else {
1469
                        if constexpr (hasMaxExpValues) {
1470
                                // maximum negative value has this bit pattern: 1-1...1-111...101, that is, sign = 1, e = 1..1, f = 111...101
1471
                                clear();
12✔
1472
                                flip();
12✔
1473
                                setbit(1ull, false);
12✔
1474
                        }
1475
                        else {
1476
                                // maximum negative value has this bit pattern: 1-1...0-111...111, that is, sign = 1, e = 11..10, f = 111...111
1477
                                clear();
49✔
1478
                                flip();
49✔
1479
                                setbit(fbits, false);
49✔
1480
                        }
1481
                }
1482
                return *this;
935,702✔
1483
        }
1484

1485

1486
        /// <summary>
1487
        /// assign the value of the string representation to the cfloat
1488
        /// </summary>
1489
        /// <param name="stringRep">decimal scientific notation of a real number to be assigned</param>
1490
        /// <returns>reference to this cfloat</returns>
1491
        /// Clang doesn't support constexpr yet on string manipulations, so we need to make it conditional
1492
        CONSTEXPRESSION cfloat& assign(const std::string& str) noexcept {
7✔
1493
                clear();
7✔
1494
                unsigned nrChars = static_cast<unsigned>(str.size());
7✔
1495
                unsigned nrBits = 0;
7✔
1496
                unsigned nrDots = 0;
7✔
1497
                std::string bits;
7✔
1498
                if (nrChars > 2) {
7✔
1499
                        if (str[0] == '0' && str[1] == 'b') {
7✔
1500
                                for (unsigned i = 2; i < nrChars; ++i) {
211✔
1501
                                        char c = str[i];
204✔
1502
                                        switch (c) {
204✔
1503
                                        case '0':
186✔
1504
                                        case '1':
1505
                                                ++nrBits;
186✔
1506
                                                bits += c;
186✔
1507
                                                break;
186✔
1508
                                        case '.':
14✔
1509
                                                ++nrDots;
14✔
1510
                                                bits += c;
14✔
1511
                                                break;
14✔
1512
                                        case '\'':
4✔
1513
                                                // consume this delimiting character
1514
                                                break;
4✔
1515
                                        default:
×
1516
                                                std::cerr << "string contained a non-standard character: " << c << '\n';
×
1517
                                                return *this;
×
1518
                                        }
1519
                                }
1520
                        }
1521
                        else {
1522
                                std::cerr << "string must start with 0b: instead input pattern was " << str << '\n';
×
1523
                                return *this;
×
1524
                        }
1525
                }
1526
                else {
1527
                        std::cerr << "string is too short\n";
×
1528
                        return *this;
×
1529
                }
1530

1531
                if (nrBits != nbits) {
7✔
1532
                        std::cerr << "number of bits in the string is " << nrBits << " and needs to be " << nbits << '\n';
×
1533
                        return *this;
×
1534
                }
1535
                if (nrDots != 2) {
7✔
1536
                        std::cerr << "number of segment delimiters in string is " << nrDots << " and needs to be 2 for a cfloat<>\n";
×
1537
                        return *this;
×
1538
                }
1539

1540
                // assign the bits
1541
                int field{ 0 };  // three fields: sign, exponent, mantissa: fields are separated by a '.'
7✔
1542
                int nrExponentBits{ -1 };
7✔
1543
                unsigned bit = nrBits;
7✔
1544
                for (unsigned i = 0; i < bits.size(); ++i) {
207✔
1545
                        char c = bits[i];
200✔
1546
                        if (c == '.') {
200✔
1547
                                ++field;
14✔
1548
                                if (field == 2) { // just finished parsing exponent field: we can now check the number of exponent bits
14✔
1549
                                        if (nrExponentBits != es) {
7✔
1550
                                                std::cerr << "provided binary string representation does not contain " << es << " exponent bits. Found " << nrExponentBits << ". Reset to 0\n";
×
1551
                                                clear();
×
1552
                                                return *this;
×
1553
                                        }
1554
                                }
1555
                        }
1556
                        else {
1557
                                setbit(--bit, c == '1');
186✔
1558
                        }
1559
                        if (field == 1) { // exponent field
200✔
1560
                                ++nrExponentBits;
51✔
1561
                        }
1562
                }
1563
                if (field != 2) {
7✔
1564
                        std::cerr << "provided binary string did not contain three fields separated by '.': Reset to 0\n";
×
1565
                        clear();
×
1566
                        return *this;
×
1567
                }
1568
                return *this;
7✔
1569
        }
7✔
1570

1571
        // selectors
1572
        constexpr bool sign() const noexcept { return (_block[MSU] & SIGN_BIT_MASK) == SIGN_BIT_MASK; }
259,301,101✔
1573
        constexpr int  scale() const noexcept {
62,208,318✔
1574
                int e{ 0 };
62,208,318✔
1575
                if constexpr (MSU_CAPTURES_EXP) {
1576
                        e = static_cast<int>((_block[MSU] & ~SIGN_BIT_MASK) >> EXP_SHIFT);
50,556,735✔
1577
                        if (e == 0) {
50,556,735✔
1578
                                // subnormal scale is determined by fraction
1579
                                // subnormals: (-1)^s * 2^(2-2^(es-1)) * (f/2^fbits))
1580
                                e = (2l - (1l << (es - 1ull))) - 1;
6,564,147✔
1581
                                if constexpr (nbits > 2 + es) {
1582
                                        for (unsigned i = nbits - 2ull - es; i > 0; --i) {
12,633,897✔
1583
                                                if (test(i)) break;
12,528,015✔
1584
                                                --e;
6,092,286✔
1585
                                        }
1586
                                }
1587
                        }
1588
                        else {
1589
                                e -= EXP_BIAS;
43,992,588✔
1590
                        }
1591
                }
1592
                else {
1593
                        blockbinary<es, bt> ebits{};
11,651,934✔
1594
                        exponent(ebits);
11,651,583✔
1595
                        if (ebits.iszero()) {
11,651,583✔
1596
                                // subnormal scale is determined by fraction
1597
                                // subnormals: (-1)^s * 2^(2-2^(es-1)) * (f/2^fbits))
1598
                                e = (2l - (1l << (es - 1ull))) - 1;
1,599,668✔
1599
                                if constexpr (nbits > 2 + es) {
1600
                                        for (unsigned i = nbits - 2ull - es; i > 0; --i) {
3,160,457✔
1601
                                                if (test(i)) break;
3,153,335✔
1602
                                                --e;
1,560,800✔
1603
                                        }
1604
                                }
1605
                        }
1606
                        else {
1607
                                e = static_cast<int>(unsigned(ebits) - EXP_BIAS);
10,051,915✔
1608
                        }
1609
                }
1610
                return e;
62,208,318✔
1611
        }
1612
        constexpr bool isneg() const noexcept {
558✔
1613
                if (isnan()) return false;
558✔
1614
                return sign(); 
541✔
1615
        }
1616
        constexpr bool ispos() const noexcept { 
34,473✔
1617
                if (isnan()) return false;
34,473✔
1618
                return !sign(); 
34,473✔
1619
        }
1620
        constexpr bool iszero() const noexcept {
430,819,813✔
1621
                // NOTE: this is a very specific design that makes the decsion that
1622
                // for subnormal encodings found in a configuration that doesn't
1623
                // support them, we assume that these values map to 0.
1624
                if constexpr (hasSubnormals) {
1625
                        return iszeroencoding();
251,710,229✔
1626
                }
1627
                else { // all subnormals round to 0
1628
                        blockbinary<es, bt> ebits{};
179,110,026✔
1629
                        exponent(ebits);
179,109,584✔
1630
                        if (ebits.iszero()) return true; else return false;
179,109,584✔
1631
                }
1632
        }
1633
        constexpr bool isone() const noexcept {
1634
                // unbiased exponent = scale = 0, fraction = 0
1635
                int s = scale();
1636
                if (s == 0) {
1637
                        blockbinary<fbits, bt> f{};
1638
                        fraction(f);
1639
                        return f.iszero();
1640
                }
1641
                return false;
1642
        }
1643
        constexpr bool isinf(int InfType = INF_TYPE_EITHER) const noexcept {
383,818,332✔
1644
                // the bit pattern encoding of Inf is independent of the max-exponent value configuration
1645
                bool isNegInf = false;
383,818,332✔
1646
                bool isPosInf = false;
383,818,332✔
1647
                if constexpr (0 == nrBlocks) {
1648
                        return false;
1649
                }
1650
                else if constexpr (1 == nrBlocks) {
1651
                        isNegInf = (_block[MSU] & MSU_MASK) == (MSU_MASK ^ LSB_BIT_MASK);
171,940,701✔
1652
                        isPosInf = (_block[MSU] & MSU_MASK) == ((MSU_MASK ^ SIGN_BIT_MASK) ^ LSB_BIT_MASK);
171,940,701✔
1653
                }
1654
                else if constexpr (2 == nrBlocks) {
1655
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK));
145,285,211✔
1656
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
145,285,211✔
1657
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
145,285,211✔
1658
                }
1659
                else if constexpr (3 == nrBlocks) {
1660
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK)) && (_block[1] == BLOCK_MASK);
66,400,021✔
1661
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
66,400,021✔
1662
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
66,400,021✔
1663
                }
1664
                else if constexpr (4 == nrBlocks) {
1665
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK)) && (_block[1] == BLOCK_MASK) && (_block[2] == BLOCK_MASK);
100,284✔
1666
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
100,284✔
1667
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
100,284✔
1668
                }
1669
                else {
1670
                        bool isInf = (_block[0] == (BLOCK_MASK ^ LSB_BIT_MASK));
92,115✔
1671
                        for (unsigned i = 1; i < nrBlocks - 1; ++i) {
93,629✔
1672
                                if (_block[i] != BLOCK_MASK) {
93,335✔
1673
                                        isInf = false;
91,821✔
1674
                                        break;
91,821✔
1675
                                }
1676
                        }
1677
                        isNegInf = isInf && ((_block[MSU] & MSU_MASK) == MSU_MASK);
92,115✔
1678
                        isPosInf = isInf && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
92,115✔
1679
                }
1680

1681
                return (InfType == INF_TYPE_EITHER ? (isNegInf || isPosInf) :
442,104,478✔
1682
                        (InfType == INF_TYPE_NEGATIVE ? isNegInf :
87,430,759✔
1683
                                (InfType == INF_TYPE_POSITIVE ? isPosInf : false)));
442,107,558✔
1684
        }
58,286,146✔
1685
        constexpr bool isnan(int NaNType = NAN_TYPE_EITHER) const noexcept {
1,002,430,816✔
1686
                if constexpr (hasMaxExpValues) {
1687
                        return isnanencoding(NaNType);
596,765,572✔
1688
                }
1689
                else {
1690
                        if (ismaxexpvalue()) {
405,665,244✔
1691
                                // all these max-exponent encodings are NANs, except for the encoding representing INF
1692
                                bool isNaN = isinf() ? false : true;
24,520,168✔
1693
                                bool isNegNaN = isNaN && sign();
24,520,168✔
1694
                                bool isPosNaN = isNaN && !sign();
24,520,168✔
1695
                                return (NaNType == NAN_TYPE_EITHER ? (isNaN) :
27,937,410✔
1696
                                        (NaNType == NAN_TYPE_SIGNALLING ? isNegNaN :
4,551,916✔
1697
                                                (NaNType == NAN_TYPE_QUIET ? isPosNaN : false)));
26,789,516✔
1698
                        }
1699
                        else {
1700
                                return false;
381,145,076✔
1701
                        }
1702
                }
1703
        }
24,520,168✔
1704
        // iszeroencoding returns true if it finds a pure -0 or +0 pattern and returns false otherwise
1705
        constexpr bool iszeroencoding() const noexcept {
359,054,770✔
1706
                if constexpr (0 == nrBlocks) {
1707
                        return true;
1708
                }
1709
                else if constexpr (1 == nrBlocks) {
1710
                        return (_block[MSU] & ~SIGN_BIT_MASK) == 0;
138,294,780✔
1711
                }
1712
                else if constexpr (2 == nrBlocks) {
1713
                        return (_block[0] == 0) && (_block[MSU] & ~SIGN_BIT_MASK) == 0;
146,613,519✔
1714
                }
1715
                else if constexpr (3 == nrBlocks) {
1716
                        return (_block[0] == 0) && _block[1] == 0 && (_block[MSU] & ~SIGN_BIT_MASK) == 0;
73,918,368✔
1717
                }
1718
                else if constexpr (4 == nrBlocks) {
1719
                        return (_block[0] == 0) && _block[1] == 0 && _block[2] == 0 && (_block[MSU] & ~SIGN_BIT_MASK) == 0;
135,371✔
1720
                }
1721
                else {
1722
                        for (unsigned i = 0; i < nrBlocks - 1; ++i) if (_block[i] != 0) return false;
338,426✔
1723
                        return (_block[MSU] & ~SIGN_BIT_MASK) == 0;
555✔
1724
                }
1725
        }
1726
        // isminnegencoding returns true if it find the pattern 1.00.00001 and returns false otherwise
1727
        constexpr bool isminnegencoding() const noexcept {
66,947✔
1728
                if constexpr (0 == nrBlocks) {
1729
                        return false;
1730
                }
1731
                else if constexpr (1 == nrBlocks) {
1732
                        return (_block[MSU] & (SIGN_BIT_MASK | 1ul));
1733
                }
1734
                else if constexpr (2 == nrBlocks) {
1735
                        return ((_block[0] == 1ul) && (_block[1] == SIGN_BIT_MASK));
1,408✔
1736
                }
1737
                else if constexpr (3 == nrBlocks) {
1738
                        return ((_block[0] == 1ul) && (_block[1] == 0) && (_block[2] == SIGN_BIT_MASK));
65,536✔
1739
                }
1740
                else if constexpr (4 == nrBlocks) {
1741
                        return ((_block[0] == 1ul) && (_block[1] == 0) && (_block[2] == 0) && (_block[3] == SIGN_BIT_MASK));
3✔
1742
                }
1743
                else {
1744
                        if (_block[0] != 1ul) return false;
×
1745
                        for (unsigned i = 1; i < nrBlocks - 2; ++i) if (_block[i] != 0) return false;
×
1746
                        return (_block[MSU] == SIGN_BIT_MASK);
×
1747
                }
1748
        }
1749
        constexpr bool isnanencoding(int NaNType = NAN_TYPE_EITHER) const noexcept {
600,433,012✔
1750
                // the bit encoding of NaN is independent of the gradual overflow configuration
1751
                bool isNaN = true;
600,433,012✔
1752
                bool isNegNaN = false;
600,433,012✔
1753
                bool isPosNaN = false;
600,433,012✔
1754

1755
                if constexpr (0 == nrBlocks) {
1756
                        return false;
1757
                }
1758
                else if constexpr (1 == nrBlocks) {
1759
                }
1760
                else if constexpr (2 == nrBlocks) {
1761
                        isNaN = (_block[0] == BLOCK_MASK);
223,999,990✔
1762
                }
1763
                else if constexpr (3 == nrBlocks) {
1764
                        isNaN = (_block[0] == BLOCK_MASK) && (_block[1] == BLOCK_MASK);
201,348,421✔
1765
                }
1766
                else if constexpr (4 == nrBlocks) {
1767
                        isNaN = (_block[0] == BLOCK_MASK) && (_block[1] == BLOCK_MASK) && (_block[2] == BLOCK_MASK);
226,740✔
1768
                }
1769
                else {
1770
                        for (unsigned i = 0; i < nrBlocks - 1; ++i) {
270,095✔
1771
                                if (_block[i] != BLOCK_MASK) {
270,052✔
1772
                                        isNaN = false;
269,802✔
1773
                                        break;
269,802✔
1774
                                }
1775
                        }
1776
                }
1777
                isNegNaN = isNaN && ((_block[MSU] & MSU_MASK) == MSU_MASK);
600,433,012✔
1778
                isPosNaN = isNaN && (_block[MSU] & MSU_MASK) == (MSU_MASK ^ SIGN_BIT_MASK);
600,433,012✔
1779

1780
                return (NaNType == NAN_TYPE_EITHER ? (isNegNaN || isPosNaN) :
816,798,078✔
1781
                        (NaNType == NAN_TYPE_SIGNALLING ? isNegNaN :
324,399,979✔
1782
                                (NaNType == NAN_TYPE_QUIET ? isPosNaN : false)));
816,502,838✔
1783
        }
216,365,066✔
1784
        // isnormal returns true if 0 or exponent bits are not all zero or one, false otherwise
1785
        constexpr bool isnormal() const noexcept {
62,052,393✔
1786
                if (iszeroencoding()) return true; // filter out the one special case
62,052,393✔
1787
                blockbinary<es, bt> e{};
62,052,743✔
1788
                exponent(e);
62,052,392✔
1789
                return !e.iszero() && !e.all();
62,052,392✔
1790
        }
1791
        // isdenormal returns true if exponent bits are all zero, false otherwise
1792
        constexpr bool isdenormal() const noexcept {
45,225,190✔
1793
                if (iszeroencoding()) return false; // filter out the one special case
45,225,190✔
1794
                blockbinary<es, bt> e{};
45,216,411✔
1795
                exponent(e);
45,216,411✔
1796
                return e.iszero(); 
45,216,411✔
1797
        }
1798
        // ismaxexpvalue returns true if exponent bits are all one, false otherwise
1799
        constexpr bool ismaxexpvalue() const noexcept {
408,894,280✔
1800
                blockbinary<es, bt> e{};
408,895,336✔
1801
                exponent(e);
408,894,280✔
1802
                return e.all();
408,894,280✔
1803
        }
1804
        // isinteger is TBD
1805
        constexpr bool isinteger() const noexcept { return false; } // return (floor(*this) == *this) ? true : false; }
1806
        
1807
        template<typename NativeReal>
1808
        constexpr bool inrange(NativeReal v) const noexcept {
9,306,296✔
1809
                // the valid range for this cfloat includes the interval between 
1810
                // maxpos and the value that would round down to maxpos
1811
                bool bIsInRange = true;                
9,306,296✔
1812
                if (v > 0) {
9,306,296✔
1813
                        cfloat c(SpecificValue::maxpos);
4,427,047✔
1814
                        cfloat<nbits + 1, es, BlockType, hasSubnormals, hasMaxExpValues, isSaturating> d{};
8,027,370✔
1815
                        d = NativeReal(c);
4,427,047✔
1816
                        ++d;
4,427,047✔
1817
                        if (v >= NativeReal(d)) bIsInRange = false;
4,427,047✔
1818
                }
1819
                else {
1820
                        cfloat c(SpecificValue::maxneg);
4,879,249✔
1821
                        cfloat<nbits + 1, es, BlockType, hasSubnormals, hasMaxExpValues, isSaturating> d{};
8,816,662✔
1822
                        d = NativeReal(c);
4,879,249✔
1823
                        --d;
4,879,249✔
1824
                        if (v <= NativeReal(d)) bIsInRange = false;
4,879,249✔
1825
                }
1826

1827
                return bIsInRange;
9,306,296✔
1828
        }
1829
        constexpr bool test(unsigned bitIndex) const noexcept {
15,697,013✔
1830
                return at(bitIndex);
15,697,013✔
1831
        }
1832
        constexpr bool at(unsigned bitIndex) const noexcept {
1,878,999,243✔
1833
                if (bitIndex < nbits) {
1,878,999,243✔
1834
                        bt word = _block[bitIndex / bitsInBlock];
1,878,999,243✔
1835
                        bt mask = bt(1ull << (bitIndex % bitsInBlock));
1,878,999,243✔
1836
                        return (word & mask);
1,878,999,243✔
1837
                }
1838
                return false;
×
1839
        }
1840
        constexpr uint8_t nibble(unsigned n) const noexcept {
640✔
1841
                if (n < (1 + ((nbits - 1) >> 2))) {
640✔
1842
                        bt word = _block[(n * 4) / bitsInBlock];
640✔
1843
                        int nibbleIndexInWord = int(n % (bitsInBlock >> 2ull));
640✔
1844
                        bt mask = bt(0xF << (nibbleIndexInWord * 4));
640✔
1845
                        bt nibblebits = bt(mask & word);
640✔
1846
                        return uint8_t(nibblebits >> (nibbleIndexInWord * 4));
640✔
1847
                }
1848
                return 0;
×
1849
        }
1850
        constexpr bt block(unsigned b) const noexcept {
1851
                if (b < nrBlocks) {
1852
                        return _block[b];
1853
                }
1854
                return 0;
1855
        }
1856

1857
        constexpr void sign(bool& s) const {
800✔
1858
                s = sign();
800✔
1859
        }
800✔
1860
        constexpr void exponent(blockbinary<es, bt>& e) const {
813,276,196✔
1861
                e.clear();
813,276,196✔
1862
                if constexpr (0 == nrBlocks) return;
1863
                else if constexpr (1 == nrBlocks) {
1864
                        bt ebits = bt(_block[MSU] & ~SIGN_BIT_MASK);
376,737,585✔
1865
                        e.setbits(uint64_t(ebits >> EXP_SHIFT));
376,737,585✔
1866
                }
1867
                else if constexpr (nrBlocks > 1) {
1868
                        if (MSU_CAPTURES_EXP) {
1869
                                bt ebits = bt(_block[MSU] & ~SIGN_BIT_MASK);
250,091,154✔
1870
                                e.setbits(uint64_t(ebits >> ((nbits - 1ull - es) % bitsInBlock)));
250,091,154✔
1871
                        }
1872
                        else {
1873
                                for (unsigned i = 0; i < es; ++i) { e.setbit(i, at(nbits - 1ull - es + i)); }
841,378,349✔
1874
                        }
1875
                }
1876
        }
813,276,196✔
1877
        template<unsigned targetFractionBits>
1878
        constexpr blockbinary<targetFractionBits, bt>& fraction(blockbinary<targetFractionBits, bt>& f) const {
34,084✔
1879
                static_assert(targetFractionBits >= fbits, "target blockbinary is too small and can't receive all fraction bits");
1880
                f.clear();
34,084✔
1881
                if constexpr (0 == nrBlocks) return f;
1882
                else if constexpr (1 == nrBlocks) {
1883
                        bt fraction = bt(_block[MSU] & ~MSU_EXP_MASK);
1,001✔
1884
                        f.setbits(fraction);
1,001✔
1885
                }
1886
                else if constexpr (nrBlocks > 1) {
1887
                        for (unsigned i = 0; i < fbits; ++i) { f.setbit(i, at(i)); }
242,396✔
1888
                }
1889
                return f;
34,084✔
1890
        }
1891
        constexpr uint64_t fraction_ull() const {
60,996,741✔
1892
                uint64_t raw{ 0 };
60,996,741✔
1893
                if constexpr (nbits - es - 1ull < 65ull) { // no-op if precondition doesn't hold
1894
                        if constexpr (1 == nrBlocks) {
1895
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
27,941,915✔
1896
                                raw = fbitMask & uint64_t(_block[0]);
27,941,915✔
1897
                        }
1898
                        else if constexpr (2 == nrBlocks) {
1899
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
22,782,373✔
1900
                                raw = fbitMask & ((uint64_t(_block[1]) << bitsInBlock) | uint64_t(_block[0]));
22,782,373✔
1901
                        }
1902
                        else if constexpr (3 == nrBlocks) {
1903
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
10,249,633✔
1904
                                raw = fbitMask & ((uint64_t(_block[2]) << (2 * bitsInBlock)) | (uint64_t(_block[1]) << bitsInBlock) | uint64_t(_block[0]));
10,249,633✔
1905
                        }
1906
                        else if constexpr (4 == nrBlocks) {
1907
                                uint64_t fbitMask = 0xFFFF'FFFF'FFFF'FFFF >> (64 - fbits);
22,453✔
1908
                                raw = fbitMask & ((uint64_t(_block[3]) << (3 * bitsInBlock)) | (uint64_t(_block[2]) << (2 * bitsInBlock)) | (uint64_t(_block[1]) << bitsInBlock) | uint64_t(_block[0]));
22,453✔
1909
                        }
1910
                        else {
1911
                                uint64_t mask{ 1 };
367✔
1912
                                for (unsigned i = 0; i < fbits; ++i) { 
16,030✔
1913
                                        if (test(i)) {
15,663✔
1914
                                                raw |= mask;
2,280✔
1915
                                        }
1916
                                        mask <<= 1;
15,663✔
1917
                                }
1918
                        }
1919
                }
1920
                return raw;
60,996,741✔
1921
        }
1922
        // construct the significant from the encoding, returns normalization offset
1923
        constexpr unsigned significant(blockbinary<fhbits, bt>& s, bool isNormal = true) const {
23✔
1924
                unsigned shift = 0;
23✔
1925
                if (iszero()) return 0;
23✔
1926
                if constexpr (0 == nrBlocks) return 0;
1927
                else if constexpr (1 == nrBlocks) {
1928
                        bt significant = bt(_block[MSU] & ~MSU_EXP_MASK & ~SIGN_BIT_MASK);
23✔
1929
                        if (isNormal) {
23✔
1930
                                significant |= (bt(0x1ul) << fbits);
×
1931
                        }
1932
                        else {
1933
                                unsigned msb = find_msb(significant);
23✔
1934
//                                std::cout << "msb : " << msb << " : fhbits : " << fhbits << " : " << to_binary(significant, true) << std::endl;
1935
                                shift = fhbits - msb;
23✔
1936
                                significant <<= shift;
23✔
1937
                        }
1938
                        s.setbits(significant);
23✔
1939
                }
1940
                else if constexpr (nrBlocks > 1) {
1941
                        s.clear();
1942
                        // TODO: design and implement a block-oriented algorithm, this sequential algorithm is super slow
1943
                        if (isNormal) {
1944
                                s.setbit(fbits);
1945
                                for (unsigned i = 0; i < fbits; ++i) { s.setbit(i, at(i)); }
1946
                        }
1947
                        else {
1948
                                // Find the MSB of the subnormal: 
1949
                                unsigned msb = 0;
1950
                                for (unsigned i = 0; i < fbits; ++i) { // msb protected from not being assigned through iszero test at prelude of function
1951
                                        msb = fbits - 1ull - i;
1952
                                        if (test(msb)) break;
1953
                                }
1954
                                //      m-----lsb
1955
                                // h00001010101
1956
                                // 101010100000
1957
                                for (unsigned i = 0; i <= msb; ++i) {
1958
                                        s.setbit(fbits - msb + i, at(i));
1959
                                }
1960
                                shift = fhbits - msb;
1961
                        }
1962
                }
1963
                return shift;
23✔
1964
        }
1965
        template<unsigned targetbits>
1966
        constexpr void bits(blockbinary<targetbits, bt>& b) const {
640✔
1967
                unsigned upperbound = (nbits > targetbits ? targetbits : nbits);
640✔
1968
                b.clear();
640✔
1969
                for (unsigned i = 0; i < upperbound; ++i) { b.setbit(i, at(i)); }
3,840✔
1970
        }
640✔
1971

1972
        // casts to native types
1973
        int to_int() const {
1974
                if (isnan()) return 0;
1975
                if (isinf()) return sign() ? std::numeric_limits<int>::min() : std::numeric_limits<int>::max();
1976
                return int(to_native<float>());
1977
        }
1978
        long to_long() const {
1979
                if (isnan()) return 0;
1980
                if (isinf()) return sign() ? std::numeric_limits<long>::min() : std::numeric_limits<long>::max();
1981
                return long(to_native<double>());
1982
        }
1983
        long long to_long_long() const {
1✔
1984
                if (isnan()) return 0;
1✔
1985
                if (isinf()) return sign() ? std::numeric_limits<long long>::min() : std::numeric_limits<long long>::max();
1✔
1986
                return (long long)(to_native<double>());
1✔
1987
        }
1988

1989
        // transform an cfloat to a native C++ floating-point. We are using the native
1990
        // precision to compute, which means that all sub-values need to be representable 
1991
        // by the native precision.
1992
        // A more accurate approximation would require an adaptive precision algorithm
1993
        // with a final rounding step.
1994
        template<typename TargetFloat>
1995
        TargetFloat to_native() const { 
113,543,416✔
1996
                TargetFloat v{ 0.0 };
113,543,416✔
1997
                if (iszero()) {
113,543,416✔
1998
                        // the optimizer might destroy the sign
1999
                        return sign() ? -TargetFloat(0) : TargetFloat(0);
904,819✔
2000
                }
2001
                else if (isnan()) {
112,638,597✔
2002
                        v = sign() ? std::numeric_limits<TargetFloat>::signaling_NaN() : std::numeric_limits<TargetFloat>::quiet_NaN();
6,143,273✔
2003
                }
2004
                else if (isinf()) {
106,495,324✔
2005
                        v = sign() ? -std::numeric_limits<TargetFloat>::infinity() : std::numeric_limits<TargetFloat>::infinity();
144,178✔
2006
                }
2007
                else { // TODO: this approach has catastrophic cancellation when nbits is large and native target float is too small
2008
                        TargetFloat f{ 0 };
106,351,146✔
2009
                        TargetFloat fbit{ 0.5 };
106,351,146✔
2010
                        for (int i = static_cast<int>(nbits - 2ull - es); i >= 0; --i) {
1,314,469,066✔
2011
                                f += at(static_cast<unsigned>(i)) ? fbit : TargetFloat(0);
1,208,117,920✔
2012
                                fbit *= TargetFloat(0.5);
1,208,117,920✔
2013
                        }
2014
                        blockbinary<es, bt> ebits;
2015
                        exponent(ebits);
106,351,146✔
2016
                        if constexpr (hasSubnormals) {
2017
                                if (ebits.iszero()) {
66,577,539✔
2018
                                        // subnormals: (-1)^s * 2^(2-2^(es-1)) * (f/2^fbits))
2019
                                        TargetFloat exponentiation = TargetFloat(subnormal_exponent[es]); // precomputed values for 2^(2-2^(es-1))
14,668,808✔
2020
                                        v = exponentiation * f;  // f is already f/2^fbits
14,668,808✔
2021
                                        return sign() ? -v : v;
14,668,808✔
2022
                                }
2023
                        }
2024
                        else {
2025
                                if (ebits.iszero()) { // underflow to 0
39,773,607✔
2026
                                        // compiler fast float optimization might destroy the sign
2027
                                        return sign() ? -TargetFloat(0) : TargetFloat(0);
×
2028
                                }
2029
                        }
2030
                        if constexpr (hasMaxExpValues) {
2031
                                // regular: (-1)^s * 2^(e+1-2^(es-1)) * (1 + f/2^fbits))
2032
                                int exponent = static_cast<int>(unsigned(ebits) - EXP_BIAS);
52,360,713✔
2033
                                if (-64 < exponent && exponent < 64) {
52,360,713✔
2034
                                        TargetFloat exponentiation = (exponent >= 0 ? TargetFloat(1ull << exponent) : (1.0f / TargetFloat(1ull << -exponent)));
52,037,993✔
2035
                                        v = exponentiation * (TargetFloat(1.0) + f);
52,037,993✔
2036
                                }
52,037,993✔
2037
                                else {
2038
                                        double exponentiation = ipow(exponent);
322,720✔
2039
                                        v = TargetFloat(exponentiation * (1.0 + f));
322,720✔
2040
                                }
2041
                        }
2042
                        else {
2043
                                if (ebits.all()) {
39,321,625✔
2044
                                        // max-exponent values are mapped to quiet NaNs
2045
                                        v = std::numeric_limits<TargetFloat>::quiet_NaN();
×
2046
                                        return v;
×
2047
                                }
2048
                                else {
2049
                                        // regular: (-1)^s * 2^(e+1-2^(es-1)) * (1 + f/2^fbits))
2050
                                        int exponent = static_cast<int>(unsigned(ebits) - EXP_BIAS);
39,321,625✔
2051
                                        if (-64 < exponent && exponent < 64) {
39,321,625✔
2052
                                                TargetFloat exponentiation = (exponent >= 0 ? TargetFloat(1ull << exponent) : (1.0f / TargetFloat(1ull << -exponent)));
39,120,476✔
2053
                                                v = exponentiation * (TargetFloat(1.0) + f);
39,120,476✔
2054
                                        }
39,120,476✔
2055
                                        else {
2056
                                                double exponentiation = ipow(exponent);
201,149✔
2057
                                                v = TargetFloat(exponentiation * (1.0 + f));
201,149✔
2058
                                        }
2059
                                }
2060
                        }
2061
                        v = sign() ? -v : v;
91,682,338✔
2062
                }
2063
                return v;
97,969,789✔
2064
        }
2065

2066
        // convert a cfloat to a blocktriple with the fraction format 1.ffff
2067
        // we are using the same block type so that we can use block copies to move bits around.
2068
        // Since we tend to have at least two exponent bits, this will lead to
2069
        // most cfloat<->blocktriple cases being efficient as the block types are aligned.
2070
        // The relationship between the source cfloat and target blocktriple is not
2071
        // arbitrary, enforce it by setting the blocktriple fbits to the cfloat's (nbits - es - 1)
2072
        template<typename TargetBlockType = bt>
2073
        constexpr void normalize(blocktriple<fbits, BlockTripleOperator::REP, TargetBlockType>& tgt) const {
3,154✔
2074
                // test special cases
2075
                if (isnan()) {
3,154✔
2076
                        tgt.setnan();
253✔
2077
                }
2078
                else if (isinf()) {
2,901✔
2079
                        tgt.setinf();
246✔
2080
                }
2081
                else if (iszero()) {
2,655✔
2082
                        tgt.setzero();
313✔
2083
                }
2084
                else {
2085
                        tgt.setnormal(); // a blocktriple is always normalized
2,342✔
2086
                        int scale = this->scale();
2,342✔
2087
                        tgt.setsign(sign());
2,342✔
2088
                        tgt.setscale(scale);
2,342✔
2089
                        // set significant
2090
                        // we are going to unify to the format 01.ffffeeee
2091
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2092
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2093
                        if (isnormal() || ismaxexpvalue()) {
2,342✔
2094
                                // normal encoding or max-exponent encoding (hasMaxExpValues=true):
2095
                                // significand has a hidden bit at position fbits
2096
                                if constexpr (fbits < 64) { // max 63 bits of fraction to yield 64bit of raw significant bits
2097
                                        uint64_t raw = fraction_ull();
1,673✔
2098
                                        raw |= (1ull << fbits);
1,673✔
2099
                                        tgt.setbits(raw);
1,673✔
2100
                                }
2101
                                else {
2102
                                        blockcopy(tgt);
111✔
2103
                                        tgt.setbit(fbits);
111✔
2104
                                }
2105
                        }
2106
                        else { // it is a subnormal encoding in this target cfloat
2107
                                int shift = MIN_EXP_NORMAL - scale;
558✔
2108
                                if (shift < 0) shift = 0; // guard against negative shifts
558✔
2109
                                // Stored subnormals have no hidden bit, but blocktriple REP expects a normalized 1.ffff payload.
2110
                                // Shift the encoded fraction up until the leading 1 reaches the hidden-bit position, while leaving
2111
                                // scale at the value returned by cfloat::scale(); that preserves the original real value in canonical form.
2112
                                if constexpr (fbits < 64) {
2113
                                        uint64_t raw = fraction_ull();
541✔
2114
                                        raw <<= shift;
541✔
2115
                                        raw |= (1ull << fbits);
541✔
2116
                                        tgt.setbits(raw);
541✔
2117
                                }
2118
                                else {
2119
                                        blockcopy(tgt);
17✔
2120
                                        tgt <<= shift;
17✔
2121
                                        tgt.setbit(fbits);
17✔
2122
                                }
2123
                        }
2124
                }
2125
        }
3,154✔
2126

2127
        // normalize a cfloat to a blocktriple used in add/sub, which has the form 00h.fffff
2128
        // that is 3 + fbits, the 3 extra bits are required to be able to use 2's complement 
2129
        // and capture the largest value of an addition/subtraction.
2130
        // TODO: currently abits = 2*fhbits as the worst case input argument size to
2131
        // capture the smallest normal value in aligned form. There is a faster/smaller
2132
        // implementation where the input is constrainted to just the round, guard, and sticky bits.
2133
        constexpr void normalizeAddition(blocktriple<fbits, BlockTripleOperator::ADD, bt>& tgt) const {
41,173,025✔
2134
                using BlockTripleConfiguration = blocktriple<fbits, BlockTripleOperator::ADD, bt>;
2135
                // test special cases
2136
                if (isnan()) {
41,173,025✔
2137
                        tgt.setnan();
396,494✔
2138
                }
2139
                else if (isinf()) {
40,776,531✔
2140
                        tgt.setinf();
326✔
2141
                }
2142
                else if (iszero()) {
40,776,205✔
2143
                        tgt.setzero();
325,672✔
2144
                }
2145
                else {
2146
                        tgt.setnormal(); // a blocktriple is always normalized
40,450,533✔
2147
                        int scale = this->scale();
40,450,533✔
2148
                        tgt.setsign(sign());
40,450,533✔
2149
                        tgt.setscale(scale);
40,450,533✔
2150
                        // set significant
2151
                        // we are going to unify to the format 001.ffffeeee
2152
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2153
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2154
                        if (isnormal()) {
40,450,533✔
2155
                                if constexpr (fbits < 64 && BlockTripleConfiguration::rbits < (64 - fbits)) {
2156
                                        uint64_t raw = fraction_ull();
27,641,379✔
2157
                                        raw |= (1ull << fbits); // add the hidden bit
27,641,379✔
2158
                                        //std::cout << "normalize      : " << *this << '\n';
2159
                                        //std::cout << "significant    : " << to_binary(raw, fbits + 2) << '\n';
2160
                                        raw <<= BlockTripleConfiguration::rbits;  // rounding bits required for correct rounding
27,641,379✔
2161
                                        //std::cout << "rounding shift : " << to_binary(raw, fbits + 2 + BlockTripleConfiguration::rbits) << '\n';
2162
                                        tgt.setbits(raw);
27,641,379✔
2163
                                }
2164
                                else {
2165
                                        blockcopy(tgt);
962✔
2166
                                        tgt.setradix();
962✔
2167
                                        tgt.setbit(fbits); // add the hidden bit
962✔
2168
                                        tgt.bitShift(BlockTripleConfiguration::rbits);  // rounding bits required for correct rounding
962✔
2169
                                }
2170
                        }
2171
                        else {
2172
                                if (isdenormal()) { // it is a subnormal encoding in this target cfloat
12,808,192✔
2173
                                        if constexpr (hasSubnormals) {
2174
                                                if constexpr (BlockTripleConfiguration::rbits < (64 - fbits)) {
2175
                                                        uint64_t raw = fraction_ull();
6,516,517✔
2176
                                                        int shift = MIN_EXP_NORMAL - scale;
6,516,517✔
2177
                                                        raw <<= shift; // shift but do NOT add a hidden bit as the MSB of the subnormal is shifted in the hidden bit position
6,516,517✔
2178
                                                        raw <<= BlockTripleConfiguration::rbits;  // rounding bits required for correct rounding
6,516,517✔
2179
                                                        tgt.setbits(raw);
6,516,517✔
2180
                                                }
2181
                                                else {
2182
                                                        blockcopy(tgt);
2183
                                                        tgt.setradix();
2184
                                                        int shift = MIN_EXP_NORMAL - scale;
2185
                                                        tgt.bitShift(shift + BlockTripleConfiguration::rbits);  // rounding bits required for correct rounding
2186
                                                }
2187
                                        }
2188
                                        else {  // this cfloat has no subnormals
2189
                                                tgt.setzero(tgt.sign()); // preserve the sign
×
2190
                                        }
2191
                                }
2192
                                else {
2193
                                        // by design, a cfloat is either normal, subnormal, or max-exponent value, so this else clause is by deduction covering a max-exponent value
2194
                                        if constexpr (hasMaxExpValues) {
2195
                                                if constexpr (fbits < 64 && BlockTripleConfiguration::rbits < (64 - fbits)) {
2196
                                                        uint64_t raw = fraction_ull();
6,291,675✔
2197
                                                        raw |= (1ull << fbits); // add the hidden bit
6,291,675✔
2198
                                                        raw <<= BlockTripleConfiguration::rbits;  // rounding bits required for correct rounding
6,291,675✔
2199
                                                        tgt.setbits(raw);
6,291,675✔
2200
                                                }
2201
                                                else {
2202
                                                        blockcopy(tgt);
×
2203
                                                        tgt.setradix();
×
2204
                                                        tgt.setbit(fbits); // add the hidden bit
×
2205
                                                        tgt.bitShift(BlockTripleConfiguration::rbits);  // rounding bits required for correct rounding
×
2206
                                                }
2207
                                        }
2208
                                        else {  // this cfloat has no max-exponent values and thus this represents a nan, signalling or quiet determined by the sign
2209
                                                tgt.setnan(tgt.sign());
×
2210
                                        }                        
2211
                                }
2212
                        }
2213
                }
2214
                // tgt.setradix(radix);
2215
        }
41,173,025✔
2216

2217
        // Normalize a cfloat to a blocktriple used in mul, which has the form 0'00001.fffff
2218
        // that is 2*fbits, plus 1 overflow bit, and the radix set at <fbits>.
2219
        // The result radix will go to 2*fbits after multiplication.
2220
        constexpr void normalizeMultiplication(blocktriple<fbits, BlockTripleOperator::MUL, bt>& tgt) const {
13,823,158✔
2221
                // test special cases
2222
                if (isnan()) {
13,823,158✔
2223
                        tgt.setnan();
450,696✔
2224
                }
2225
                else if (isinf()) {
13,372,462✔
2226
                        tgt.setinf();
652✔
2227
                }
2228
                else if (iszero()) {
13,371,810✔
2229
                        tgt.setzero();
450,960✔
2230
                }
2231
                else {
2232
                        tgt.setnormal(); // a blocktriple is always normalized
12,920,850✔
2233
                        int scale = this->scale();
12,920,850✔
2234
                        tgt.setsign(sign());
12,920,850✔
2235
                        tgt.setscale(scale);
12,920,850✔
2236

2237
                        // set significant
2238
                        // we are going to unify to the format 01.ffffeeee
2239
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2240
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2241
                        if (isnormal() || ismaxexpvalue()) {
12,920,850✔
2242
                                if constexpr (fbits < 64) { // max 63 bits of fraction to yield 64bit of raw significant bits
2243
                                        uint64_t raw = fraction_ull();
11,706,786✔
2244
                                        raw |= (1ull << fbits);
11,706,786✔
2245
                                        tgt.setbits(raw);
11,706,786✔
2246
                                }
2247
                                else {
2248
                                        blockcopy(tgt);
832✔
2249
                                        tgt.setradix();
832✔
2250
                                        tgt.setbit(fbits); // add the hidden bit
832✔
2251
                                }
2252
                        }
2253
                        else { 
2254
                                // it is a subnormal encoding in this target cfloat
2255
                                if constexpr (hasSubnormals) {
2256
                                        if constexpr (fbits < 64) {
2257
                                                uint64_t raw = fraction_ull();
1,213,232✔
2258
                                                int shift = MIN_EXP_NORMAL - scale;
1,213,232✔
2259
                                                raw <<= shift;
1,213,232✔
2260
                                                raw |= (1ull << fbits);
1,213,232✔
2261
                                                tgt.setbits(raw);
1,213,232✔
2262
                                        }
2263
                                        else {
2264
                                                blockcopy(tgt);
×
2265
                                                int shift = MIN_EXP_NORMAL - scale;
×
2266
                                                tgt.bitShift(shift);
×
2267
                                                tgt.setbit(fbits);
×
2268
                                        }
2269
                                }
2270
                                else { // this cfloat has no subnormals
2271
                                        tgt.setzero(tgt.sign()); // preserve the sign
×
2272
                                }
2273
                        }
2274
                }
2275
                tgt.setradix(fbits); // override the radix with the input scale for accurate value printing
13,823,158✔
2276
        }
13,823,158✔
2277

2278
        // normalize a cfloat to a blocktriple used in div, which has the form 0'00000'00001.fffff
2279
        // that is 3*fbits, plus 1 overflow bit, and the radix set at <fbits>.
2280
        // the result radix will go to 2*fbits after multiplication.
2281
        // TODO: needs implementation
2282
        constexpr void normalizeDivision(blocktriple<fbits, BlockTripleOperator::DIV, bt>& tgt) const {
8,673,782✔
2283
                constexpr unsigned divshift = blocktriple<fbits, BlockTripleOperator::DIV, bt>::divshift;
8,673,782✔
2284
                // test special cases
2285
                if (isnan()) {
8,673,782✔
2286
                        tgt.setnan();
38✔
2287
                }
2288
                else if (isinf()) {
8,673,744✔
2289
                        tgt.setinf();
6✔
2290
                }
2291
                else if (iszero()) {
8,673,738✔
2292
                        tgt.setzero();
44✔
2293
                }
2294
                else {
2295
                        tgt.setnormal(); // a blocktriple is always normalized
8,673,694✔
2296
                        int scale = this->scale();
8,673,694✔
2297
                        tgt.setsign(sign());
8,673,694✔
2298
                        tgt.setscale(scale);
8,673,694✔
2299
                        // set significant
2300
                        // we are going to unify to the format 01.ffffeeee
2301
                        // where 'f' is a fraction bit, and 'e' is an extension bit
2302
                        // so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
2303
                        if (isnormal() || ismaxexpvalue()) {
8,673,694✔
2304
                                if constexpr (fbits < 64 && divshift < (64 - fbits)) {
2305
                                        uint64_t raw = fraction_ull();
7,187,050✔
2306
                                        raw |= (1ull << fbits);
7,187,050✔
2307
                                        raw <<= divshift; // shift the input value to the output radix
7,187,050✔
2308
                                        tgt.setbits(raw);
7,187,050✔
2309
                                }
2310
                                else {
2311
                                        // brute force copy of blocks
2312
                                        blockcopy(tgt);
1,054,322✔
2313
                                        tgt.setbit(fbits);
1,054,322✔
2314
                                        tgt.bitShift(divshift); // shift the input value to the output radix
1,054,322✔
2315
                                }
2316
                        }
2317
                        else { // it is a subnormal encoding in this target cfloat
2318
                                if constexpr (fbits < 64 && divshift < (64 - fbits)) {
2319
                                        uint64_t raw = fraction_ull();
432,320✔
2320
                                        int shift = MIN_EXP_NORMAL - scale;
432,320✔
2321
                                        raw <<= shift;
432,320✔
2322
                                        raw |= (1ull << fbits);
432,320✔
2323
                                        raw <<= divshift; // shift the input value to the output radix
432,320✔
2324
                                        tgt.setbits(raw);
432,320✔
2325
                                }
2326
                                else {
2327
                                        blockcopy(tgt);
2✔
2328
                                        int shift = MIN_EXP_NORMAL - scale;
2✔
2329
                                        tgt.bitShift(shift);
2✔
2330
                                        tgt.setbit(fbits);
2✔
2331
                                        tgt.bitShift(divshift); // shift the input value to the output radix
2✔
2332
                                }
2333
                        }
2334
                }
2335
                tgt.setradix(blocktriple<fbits, BlockTripleOperator::DIV, bt>::radix);
8,673,782✔
2336
        }
8,673,782✔
2337

2338
        // helper debug function
2339
        void constexprClassParameters() const noexcept {
8✔
2340
                std::cout << "-------------------------------------------------------------\n";
8✔
2341
                std::cout << "type              : " << typeid(*this).name() << '\n';
8✔
2342
                std::cout << "nbits             : " << nbits << '\n';
8✔
2343
                std::cout << "es                : " << es << std::endl;
8✔
2344
                std::cout << "hasSubnormals     : " << (hasSubnormals ? "true" : "false") << '\n';
8✔
2345
                std::cout << "hasMaxExpValues   : " << (hasMaxExpValues ? "true" : "false") << '\n';
8✔
2346
                std::cout << "isSaturating      : " << (isSaturating ? "true" : "false") << '\n';
8✔
2347
                std::cout << "ALL_ONES          : " << to_binary(ALL_ONES, 0, true) << '\n';
8✔
2348
                std::cout << "BLOCK_MASK        : " << to_binary(BLOCK_MASK, 0, true) << '\n';
8✔
2349
                std::cout << "nrBlocks          : " << nrBlocks << '\n';
8✔
2350
                std::cout << "bits in MSU       : " << bitsInMSU << '\n';
8✔
2351
                std::cout << "MSU               : " << MSU << '\n';
8✔
2352
                std::cout << "MSU MASK          : " << to_binary(MSU_MASK, 0, true) << '\n';
8✔
2353
                std::cout << "SIGN_BIT_MASK     : " << to_binary(SIGN_BIT_MASK, 0, true) << '\n';
8✔
2354
                std::cout << "LSB_BIT_MASK      : " << to_binary(LSB_BIT_MASK, 0, true) << '\n';
8✔
2355
                std::cout << "MSU CAPTURES_EXP  : " << (MSU_CAPTURES_EXP ? "yes\n" : "no\n");
8✔
2356
                std::cout << "EXP_SHIFT         : " << EXP_SHIFT << '\n';
8✔
2357
                std::cout << "MSU EXP MASK      : " << to_binary(MSU_EXP_MASK, 0, true) << '\n';
8✔
2358
                std::cout << "ALL_ONE_MASK_ES   : " << to_binary(ALL_ONES_ES) << '\n';
8✔
2359
                std::cout << "EXP_BIAS          : " << EXP_BIAS << '\n';
8✔
2360
                std::cout << "MAX_EXP           : " << MAX_EXP << '\n';
8✔
2361
                std::cout << "MIN_EXP_NORMAL    : " << MIN_EXP_NORMAL << '\n';
8✔
2362
                std::cout << "MIN_EXP_SUBNORMAL : " << MIN_EXP_SUBNORMAL << '\n';
8✔
2363
                std::cout << "fraction Blocks   : " << fBlocks << '\n';
8✔
2364
                std::cout << "bits in FSU       : " << bitsInFSU << '\n';
8✔
2365
                std::cout << "FSU               : " << FSU << '\n';
8✔
2366
                std::cout << "FSU MASK          : " << to_binary(FSU_MASK, 0, true) << '\n';
8✔
2367
                std::cout << "topfbits          : " << topfbits << '\n';
8✔
2368
                std::cout << "ALL_ONE_MASK_FR   : " << to_binary(ALL_ONES_FR) << '\n';
8✔
2369
        }
8✔
2370
        void showLimbs() const {
2371
                for (unsigned b = 0; b < nrBlocks; ++b) {
2372
                        std::cout << to_binary(_block[nrBlocks - b - 1], sizeof(bt) * 8) << ' ';
2373
                }
2374
                std::cout << '\n';
2375
        }
2376

2377
protected:
2378
        // HELPER methods
2379

2380
        /// <summary>
2381
        /// 1's complement of the encoding used to set up specific encoding patterns.
2382
        /// This is not an arithmetic operator that makes sense for floating-point numbers.
2383
        /// </summary>
2384
        /// <returns>reference to this cfloat object</returns>
2385
        constexpr cfloat& flip() noexcept { // in-place one's complement
1,871,741✔
2386
                for (unsigned i = 0; i < nrBlocks; ++i) {
7,118,188✔
2387
                        _block[i] = bt(~_block[i]);
5,246,447✔
2388
                }
2389
                _block[MSU] &= MSU_MASK; // assert precondition of properly nulled leading non-bits
1,871,741✔
2390
                return *this;
1,871,741✔
2391
        }
2392

2393
        /// <summary>
2394
        /// shift left is a bit level encoding helper for fast limb-based conversions between different cfloats
2395
        /// </summary>
2396
        /// <param name="bitsToShift"></param>
2397
        void shiftLeft(unsigned bitsToShift) {
60,269✔
2398
                if (bitsToShift == 0) return;
60,269✔
2399
                if (bitsToShift > nbits) {
60,269✔
2400
                        setzero();
×
2401
                }
2402
                if (bitsToShift >= bitsInBlock) {
60,269✔
2403
                        int blockShift = static_cast<int>(bitsToShift / bitsInBlock);
60,269✔
2404
                        for (int i = static_cast<int>(MSU); i >= blockShift; --i) {
140,578✔
2405
                                _block[i] = _block[i - blockShift];
80,309✔
2406
                        }
2407
                        for (int i = blockShift - 1; i >= 0; --i) {
341,584✔
2408
                                _block[i] = bt(0);
281,315✔
2409
                        }
2410
                        // adjust the shift
2411
                        bitsToShift -= blockShift * bitsInBlock;
60,269✔
2412
                        if (bitsToShift == 0) return;
60,269✔
2413
                }
2414
                if constexpr (MSU > 0) {
2415
                        // construct the mask for the upper bits in the block that need to move to the higher word
2416
                        bt mask = 0xFFFFFFFFFFFFFFFF << (bitsInBlock - bitsToShift);
60,223✔
2417
                        for (unsigned i = MSU; i > 0; --i) {
360,996✔
2418
                                _block[i] <<= bitsToShift;
300,773✔
2419
                                // mix in the bits from the right
2420
                                bt bits = bt(mask & _block[i - 1]);
300,773✔
2421
                                _block[i] |= (bits >> (bitsInBlock - bitsToShift));
300,773✔
2422
                        }
2423
                }
2424
                _block[0] <<= bitsToShift;
60,223✔
2425
        }
2426

2427
        // convert an unsigned integer into a cfloat
2428
        // TODO: this method does not protect against being called with a signed integer
2429
        template<typename Ty>
2430
        constexpr cfloat& convert_unsigned_integer(const Ty& rhs) noexcept {
2,795✔
2431
                clear();
2,795✔
2432
                if (0 == rhs) return *this;
2,795✔
2433

2434
                uint64_t raw = static_cast<uint64_t>(rhs);
2,772✔
2435
                int msb = static_cast<int>(find_msb(raw)) - 1; // msb > 0 due to zero test above
2,772✔
2436
                int exponent = msb;
2,772✔
2437
                // remove the MSB as it represents the hidden bit in the cfloat representation
2438
                uint64_t hmask = ~(1ull << msb);
2,772✔
2439
                raw &= hmask;
2,772✔
2440

2441
                constexpr uint32_t sizeInBits = 8 * sizeof(Ty);
2,772✔
2442
                uint32_t shift = sizeInBits - exponent - 1;
2,772✔
2443
                raw <<= shift;
2,772✔
2444
                raw = round<sizeInBits, uint64_t>(raw, exponent);
2,772✔
2445

2446
                // check for exponent overflow after rounding
2447
                if (exponent > MAX_EXP) {
2,772✔
2448
                        if constexpr (isSaturating) {
2449
                                this->maxpos();
×
2450
                        }
2451
                        else {
2452
                                setinf(false);
×
2453
                        }
2454
                        return *this;
×
2455
                }
2456

2457
                // construct the target cfloat
2458
                if constexpr (fbits < (64 - es)) {
2459
                        uint64_t biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(EXP_BIAS));
2,742✔
2460
                        uint64_t bits = 0;
2,742✔
2461
                        bits <<= es;
2,742✔
2462
                        bits |= biasedExponent;
2,742✔
2463
                        bits <<= fbits;
2,742✔
2464
                        bits |= raw;
2,742✔
2465
                        setbits(bits);
2,742✔
2466
                }
2467
                else {
2468
                        setsign(false);
30✔
2469
                        setexponent(exponent);
30✔
2470
                        for (int i = 0; i < exponent; ++i) {
291✔
2471
                                bool bit = (raw >> (sizeInBits - 2 - i)) & 1;
261✔
2472
                                setbit(static_cast<unsigned>(fbits - 1 - i), bit);
261✔
2473
                        }
2474
                }
2475
                // post-rounding cleanup: rounding at the maxpos boundary can
2476
                // produce a NaN encoding; project back to inf or maxpos
2477
                if (isnan()) {
2,772✔
2478
                        if constexpr (isSaturating) {
2479
                                this->maxpos();
2✔
2480
                        }
2481
                        else {
2482
                                setinf(false);
×
2483
                        }
2484
                }
2485
                return *this;
2,772✔
2486
        }
2487
        // convert a signed integer into a cfloat
2488
        // TODO: this method does not protect against being called with a signed integer
2489
        template<typename Ty>
2490
        constexpr cfloat& convert_signed_integer(const Ty& rhs) noexcept {
13,330✔
2491
                clear();
13,330✔
2492
                if (0 == rhs) return *this;
13,330✔
2493
                bool s = (rhs < 0);
10,087✔
2494
                using UnsignedTy = std::make_unsigned_t<Ty>;
2495
                UnsignedTy urhs = static_cast<UnsignedTy>(rhs);
10,087✔
2496
                uint64_t raw = static_cast<uint64_t>(s ? (UnsignedTy(0) - urhs) : urhs);
10,087✔
2497

2498
                int msb = static_cast<int>(find_msb(raw)) - 1; // msb > 0 due to zero test above 
10,087✔
2499
                int exponent = msb;
10,087✔
2500
                // remove the MSB as it represents the hidden bit in the cfloat representation
2501
                uint64_t hmask = ~(1ull << msb);
10,087✔
2502
                raw &= hmask;
10,087✔
2503

2504
                // shift the msb to the msb of the fraction
2505
                constexpr uint32_t sizeInBits = 8 * sizeof(Ty);
10,087✔
2506
                uint32_t shift = sizeInBits - exponent - 1;
10,087✔
2507
                raw <<= shift;
10,087✔
2508
                raw = round<sizeInBits, uint64_t>(raw, exponent);
10,087✔
2509

2510
                // check for exponent overflow after rounding
2511
                if (exponent > MAX_EXP) {
10,087✔
2512
                        if constexpr (isSaturating) {
2513
                                if (s) this->maxneg(); else this->maxpos();
×
2514
                        }
2515
                        else {
2516
                                setinf(s);
×
2517
                        }
2518
                        return *this;
×
2519
                }
2520

2521
                // construct the target cfloat
2522
                if constexpr (fbits < (64 - es)) {
2523
                        uint64_t biasedExponent = static_cast<uint64_t>(static_cast<long long>(exponent) + static_cast<long long>(EXP_BIAS));
9,716✔
2524
                        uint64_t bits = (s ? 1ull : 0ull);
9,716✔
2525
                        bits <<= es;
9,716✔
2526
                        bits |= biasedExponent;
9,716✔
2527
                        bits <<= fbits;
9,716✔
2528
                        bits |= raw;
9,716✔
2529
                        setbits(bits);
9,716✔
2530
                }
2531
                else {
2532
                        setsign(s);
371✔
2533
                        setexponent(exponent);
371✔
2534
                        for (int i = 0; i < exponent; ++i) {
3,120✔
2535
                                bool bit = (raw >> (sizeInBits - 2 - i)) & 1;
2,749✔
2536
                                setbit(static_cast<unsigned>(fbits - 1 - i), bit);
2,749✔
2537
                        }
2538
                }
2539
                // post-rounding cleanup: rounding at the maxpos boundary can
2540
                // produce a NaN encoding; project back to inf or maxpos/maxneg
2541
                if (isnan()) {
10,087✔
2542
                        if constexpr (isSaturating) {
2543
                                if (s) this->maxneg(); else this->maxpos();
4✔
2544
                        }
2545
                        else {
2546
                                setinf(s);
×
2547
                        }
2548
                }
2549
                return *this;
10,087✔
2550
        }
2551

2552
public:
2553
        template<typename Real>
2554
        CONSTEXPRESSION cfloat& convert_ieee754(Real rhs) noexcept {
138,277,342✔
2555
                if constexpr (nbits == 32 && es == 8 && sizeof(Real) == 4) {
2556
                        // we CANNOT use the native conversion to float as cfloats have max-exponent values
2557
                        // which IEEE-754 does not have and thus a native conversion would destroy
2558
                        // only if the Real type is a float can we use the direct conversion
2559

2560
                        // when our cfloat is a perfect match to single precision IEEE-754
2561
                        bool s{ false };
65,749✔
2562
                        uint64_t rawExponent{ 0 };
65,749✔
2563
                        uint64_t rawFraction{ 0 };
65,749✔
2564
                        uint64_t bits{ 0 };
65,749✔
2565
                        extractFields(rhs, s, rawExponent, rawFraction, bits);
65,749✔
2566
                        if (rawExponent == ieee754_parameter<Real>::eallset) { // nan and inf need to be remapped
65,749✔
2567
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::snanmask) ||
24✔
2568
                                        rawFraction == (ieee754_parameter<Real>::fmask & (ieee754_parameter<Real>::qnanmask | ieee754_parameter<Real>::snanmask))) {
18✔
2569
                                        // 1.11111111.00000000.......00000001 signalling nan
2570
                                        // 0.11111111.00000000000000000000001 signalling nan
2571
                                        // MSVC
2572
                                        // 1.11111111.10000000.......00000001 signalling nan
2573
                                        // 0.11111111.10000000.......00000001 signalling nan
2574
                                        setnan(NAN_TYPE_SIGNALLING);
6✔
2575
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2576
                                        return *this;
6✔
2577
                                }
2578
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::qnanmask)) {
18✔
2579
                                        // 1.11111111.10000000.......00000000 quiet nan
2580
                                        // 0.11111111.10000000.......00000000 quiet nan
2581
                                        setnan(NAN_TYPE_QUIET);
5✔
2582
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2583
                                        return *this;
5✔
2584
                                }
2585
                                if (rawFraction == 0ull) {
13✔
2586
                                        // 1.11111111.0000000.......000000000 -inf
2587
                                        // 0.11111111.0000000.......000000000 +inf
2588
                                        setinf(s);
13✔
2589
                                        return *this;
13✔
2590
                                }
2591
                        }
2592
                        uint64_t raw{ s ? 1ull : 0ull };
65,725✔
2593
                        raw <<= 31;
65,725✔
2594
                        raw |= (rawExponent << fbits);
65,725✔
2595
                        raw |= rawFraction;
65,725✔
2596
                        setbits(raw);
65,725✔
2597
                        return *this;
65,725✔
2598
                }
2599
                else if constexpr (nbits == 64 && es == 11 && sizeof(Real) == 8) {
2600
                        // when our cfloat is a perfect match to double precision IEEE-754
2601
                        bool s{ false };
12,177✔
2602
                        uint64_t rawExponent{ 0 };
12,177✔
2603
                        uint64_t rawFraction{ 0 };
12,177✔
2604
                        uint64_t bits{ 0 };
12,177✔
2605
                        extractFields(rhs, s, rawExponent, rawFraction, bits);
12,177✔
2606
                        if (rawExponent == ieee754_parameter<Real>::eallset) { // nan and inf need to be remapped
12,177✔
2607
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::snanmask) ||
20✔
2608
                                        rawFraction == (ieee754_parameter<Real>::fmask & (ieee754_parameter<Real>::qnanmask | ieee754_parameter<Real>::snanmask))) {
14✔
2609
                                        // 1.11111111.00000000.......00000001 signalling nan
2610
                                        // 0.11111111.00000000000000000000001 signalling nan
2611
                                        // MSVC
2612
                                        // 1.11111111.10000000.......00000001 signalling nan
2613
                                        // 0.11111111.10000000.......00000001 signalling nan
2614
                                        setnan(NAN_TYPE_SIGNALLING);
6✔
2615
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2616
                                        return *this;
6✔
2617
                                }
2618
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::qnanmask)) {
14✔
2619
                                        // 1.11111111.10000000.......00000000 quiet nan
2620
                                        // 0.11111111.10000000.......00000000 quiet nan
2621
                                        setnan(NAN_TYPE_QUIET);
5✔
2622
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2623
                                        return *this;
5✔
2624
                                }
2625
                                if (rawFraction == 0ull) {
9✔
2626
                                        // 1.11111111.0000000.......000000000 -inf
2627
                                        // 0.11111111.0000000.......000000000 +inf
2628
                                        setinf(s);
9✔
2629
                                        return *this;
9✔
2630
                                }
2631
                        }
2632
                        // normal and subnormal handling
2633
                        uint64_t raw{ s ? 1ull : 0ull };
12,157✔
2634
                        raw <<= 63;
12,157✔
2635
                        raw |= (rawExponent << fbits);
12,157✔
2636
                        raw |= rawFraction;
12,157✔
2637
                        setbits(raw);
12,157✔
2638
                        return *this;
12,157✔
2639
                }
2640
                else {
2641
                        clear();
138,199,416✔
2642
                        // extract raw IEEE-754 bits
2643
                        bool s{ false };
138,199,416✔
2644
                        uint64_t rawExponent{ 0 };
138,199,416✔
2645
                        uint64_t rawFraction{ 0 };
138,199,416✔
2646
                        uint64_t bits{ 0 };
138,199,416✔
2647
                        extractFields(rhs, s, rawExponent, rawFraction, bits);
138,199,416✔
2648
                        // special case handling
2649
                        if (rawExponent == ieee754_parameter<Real>::eallset) { // nan and inf
138,199,416✔
2650
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::snanmask) ||
5,105,673✔
2651
                                        rawFraction == (ieee754_parameter<Real>::fmask & (ieee754_parameter<Real>::qnanmask | ieee754_parameter<Real>::snanmask))) {
2,589,634✔
2652
                                        // 1.11111111.00000000.......00000001 signalling nan
2653
                                        // 0.11111111.00000000000000000000001 signalling nan
2654
                                        // MSVC
2655
                                        // 1.11111111.10000000.......00000001 signalling nan
2656
                                        // 0.11111111.10000000.......00000001 signalling nan
2657
                                        setnan(NAN_TYPE_SIGNALLING);
2,523,249✔
2658
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2659
                                        return *this;
2,523,249✔
2660
                                }
2661
                                if (rawFraction == (ieee754_parameter<Real>::fmask & ieee754_parameter<Real>::qnanmask)) {
2,582,424✔
2662
                                        // 1.11111111.10000000.......00000000 quiet nan
2663
                                        // 0.11111111.10000000.......00000000 quiet nan
2664
                                        setnan(NAN_TYPE_QUIET);
2,553,524✔
2665
                                        //setsign(s);  a cfloat encodes a signalling nan with sign = 1, and a quiet nan with sign = 0
2666
                                        return *this;
2,553,524✔
2667
                                }
2668
                                if (rawFraction == 0ull) {
28,900✔
2669
                                        // 1.11111111.0000000.......000000000 -inf
2670
                                        // 0.11111111.0000000.......000000000 +inf
2671
                                        setinf(s);
28,876✔
2672
                                        return *this;
28,876✔
2673
                                }
2674
                        }
2675
                        if (rhs == 0.0) { // IEEE rule: this is valid for + and - 0.0
133,093,767✔
2676
                                setbit(nbits - 1ull, s);
578,424✔
2677
                                return *this;
578,424✔
2678
                        }
2679
        
2680
                        // normal number consists of fbits fraction bits and one hidden bit
2681
                        // subnormal number has no hidden bit
2682
                        int exponent = static_cast<int>(rawExponent) - ieee754_parameter<Real>::bias;  // unbias the exponent
132,515,343✔
2683

2684
                        // normalize subnormal source values so downstream code can treat them as normal
2685
                        // A subnormal has rawExponent == 0 and value = 0.fraction * 2^(1-bias).
2686
                        // We find the MSB of the fraction, shift it into the hidden bit position,
2687
                        // mask it off (like a normal's implicit 1), and adjust the exponent.
2688
                        // Setting rawExponent = 1 causes all downstream if(rawExponent != 0) branches
2689
                        // to take the normal-number path, which already handles normal-to-normal and
2690
                        // normal-to-subnormal target conversions correctly.
2691
                        if (rawExponent == 0 && rawFraction != 0) {
132,515,343✔
2692
                                exponent = 1 - ieee754_parameter<Real>::bias; // effective exponent for subnormals
1,171✔
2693
                                unsigned msb_pos = find_msb(rawFraction); // 1-indexed position of MSB
1,171✔
2694
                                unsigned normalizeShift = static_cast<unsigned>(ieee754_parameter<Real>::fbits) + 1u - msb_pos;
1,171✔
2695
                                rawFraction = (rawFraction << normalizeShift) & static_cast<uint64_t>(ieee754_parameter<Real>::fmask);
1,171✔
2696
                                exponent -= static_cast<int>(normalizeShift);
1,171✔
2697
                                rawExponent = 1; // mark as normalized so downstream paths treat this as a normal number
1,171✔
2698
                        }
2699

2700
                        // check special case of
2701
                        //  1- saturating to maxpos/maxneg, or 
2702
                        //  2- projecting to +-inf 
2703
                        // if the value is out of range.
2704
                        // 
2705
                        // One problem here is that at the rounding cusps of maxpos <-> inf <-> nan
2706
                        // you need to go through the rounding logic to know which encoding you end up
2707
                        // with. 
2708
                        // For each specific cfloat configuration, you can work out these rounding cusps
2709
                        // but they need to go through the value transformation to map them back to native
2710
                        // IEEE-754. That is a complex computation to do as a static constexpr as you need
2711
                        // to construct the value, then evaluate it, and store it.
2712
                        // 
2713
                        // The algorithm used here is to check for the obvious out of range values by
2714
                        // comparing their scale to the max scale this cfloat encoding can represent.
2715
                        // For the rounding cusps, we go through the rounding logic, and then clean up
2716
                        // after rounding using the observation that no conversion from a value can ever
2717
                        // yield the NaN encoding.
2718
                        //
2719
                        // The rounding logic will correctly sort between maxpos and inf, and we clean
2720
                        // up any NaN encodings by projecting back to the configuration's saturation rule.
2721
                        //
2722
                        // We could improve on this by creating the database of rounding cusps and
2723
                        // referencing them with a direct value comparison with the input. That would be
2724
                        // the most performant implementation.
2725
                        if (exponent > MAX_EXP) {
132,515,343✔
2726
                                if constexpr (isSaturating) {
2727
                                        if (s) this->maxneg(); else this->maxpos(); // saturate to maxpos or maxneg
934,372✔
2728
                                }
2729
                                else {
2730
                                        setinf(s);
971,746✔
2731
                                }
2732
                                return *this;
1,906,118✔
2733
                        }
2734
                        if constexpr (hasSubnormals) {
2735
                                if (exponent < MIN_EXP_SUBNORMAL - 1) { 
88,990,711✔
2736
                                        // map to +-0 any values that have a scale less than (MIN_EXP_SUBMORNAL - 1)
2737
                                        this->setbit(nbits - 1, s);
145,434✔
2738
                                        return *this;
145,434✔
2739
                                }
2740
                        }
2741
                        else {
2742
                                if (exponent < MIN_EXP_NORMAL - 1) {
41,618,514✔
2743
                                        // map to +-0 any values that have a scale less than (MIN_EXP_MORNAL - 1)
2744
                                        this->setbit(nbits - 1, s);
273,184✔
2745
                                        return *this;
273,184✔
2746
                                }
2747
                        }
2748

2749
                        /////////////////  
2750
                        /// end of special case processing, move on to value sampling and rounding
2751

2752
#if TRACE_CONVERSION
2753
                        std::cout << '\n';
2754
                        std::cout << "value             : " << rhs << '\n';
2755
                        std::cout << "segments          : " << to_binary(rhs) << '\n';
2756
                        std::cout << "sign     bit      : " << (s ? '1' : '0') << '\n';
2757
                        std::cout << "exponent bits     : " << to_binary(rawExponent, ieee754_parameter<Real>::ebits, true) << '\n';
2758
                        std::cout << "fraction bits     : " << to_binary(rawFraction, ieee754_parameter<Real>::fbits, true) << '\n';
2759
                        std::cout << "exponent value    : " << exponent << '\n';
2760
#endif
2761

2762
                        // do the following scenarios have different rounding bits?
2763
                        // input is normal, cfloat is normal           <-- rounding can happen with native ieee-754 bits
2764
                        // input is normal, cfloat is subnormal
2765
                        // input is subnormal, cfloat is normal
2766
                        // input is subnormal, cfloat is subnormal
2767

2768
                        // The first condition is the relationship between the number 
2769
                        // of fraction bits from the source and the number of fraction bits 
2770
                        // in the target cfloat: these are constexpressions and guard the shifts
2771
                        // input fbits >= cfloat fbits                 <-- need to round
2772
                        // input fbits < cfloat fbits                  <-- no need to round
2773

2774
                        // quick check if we are truncating to 0 for all subnormal values
2775
                        if constexpr (!hasSubnormals) {
2776
                                if (exponent < MIN_EXP_NORMAL) {
41,345,330✔
2777
                                        setsign(s); // rest of the bits, exponent and fraction, are already set correctly
124,352✔
2778
                                        return *this;
124,352✔
2779
                                }
2780
                        }
2781
                        if constexpr (fbits < ieee754_parameter<Real>::fbits) {
2782
                                // this is the common case for cfloats that are smaller in size compared to single and double precision IEEE-754
2783
                                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
129,806,790✔
2784
                                uint32_t biasedExponent{ 0 };
129,806,790✔
2785
                                int adjustment{ 0 }; // right shift adjustment for subnormal representation
129,806,790✔
2786
                                uint64_t mask;
2787
                                if (rawExponent != 0) {
129,806,790✔
2788
                                        // the source real is a normal number, 
2789
//                                        if (exponent >= (MIN_EXP_SUBNORMAL - 1) && exponent < MIN_EXP_NORMAL) {
2790
                                        if (exponent < MIN_EXP_NORMAL) {
129,806,790✔
2791
//                                                exponent = (exponent < MIN_EXP_SUBNORMAL ? MIN_EXP_SUBNORMAL : exponent); // clip to the smallest subnormal exponent, otherwise the adjustment is off
2792
                                                // the value is a subnormal number in this representation: biasedExponent = 0
2793
                                                // add the hidden bit to the fraction bits so the denormalization has the correct MSB
2794
                                                rawFraction |= ieee754_parameter<Real>::hmask;
41,278,974✔
2795

2796
                                                // fraction processing: we have 1 hidden + 23 explicit fraction bits 
2797
                                                // f = 1.ffff 2^exponent * 2^fbits * 2^-(2-2^(es-1)) = 1.ff...ff >> (23 - (-exponent + fbits - (2 -2^(es-1))))
2798
                                                // -exponent because we are right shifting and exponent in this range is negative
2799
                                                adjustment = -(exponent + subnormal_reciprocal_shift[es]);
41,278,974✔
2800
                                                // this is the right shift adjustment required for subnormal representation due 
2801
                                                // to the scale of the input number, i.e. the exponent of 2^-adjustment
2802
                                        }
2803
                                        else {
2804
                                                // the value is a normal number in this representation: common case
2805
                                                biasedExponent = static_cast<uint32_t>(exponent + EXP_BIAS); // project the exponent into the target 
88,527,816✔
2806
                                                // fraction processing
2807
                                                // float structure is: seee'eeee'efff'ffff'ffff'ffff'ffff'ffff, s = sign, e - exponent bit, f = fraction bit
2808
                                                // target structure is for example cfloat<8,2>: seef'ffff
2809
                                                // since both are normals, we can shift the incoming fraction to the target structure bits, and round
2810
                                                // MSB of source = 23 - 1, MSB of target = fbits - 1: shift = MSB of src - MSB of tgt => 23 - fbits
2811
                                                adjustment = 0;
88,527,816✔
2812
                                        }
2813
                                        if constexpr (rightShift > 0) {
2814
                                                // if true we need to round
2815
                                                // round-to-even logic
2816
                                                //  ... lsb | guard  round sticky   round
2817
                                                //       x     0       x     x       down
2818
                                                //       0     1       0     0       down  round to even
2819
                                                //       1     1       0     0        up   round to even
2820
                                                //       x     1       0     1        up
2821
                                                //       x     1       1     0        up
2822
                                                //       x     1       1     1        up
2823
                                                // collect lsb, guard, round, and sticky bits
2824

2825

2826
#if TRACE_CONVERSION
2827
                                                std::cout << "fraction bits     : " << to_binary(rawFraction, ieee754_parameter<Real>::nbits, true) << '\n';
2828
                                                std::cout << "lsb mask bits     : " << to_binary(mask, ieee754_parameter<Real>::nbits, true) << '\n';
2829
#endif
2830
                                                mask = (1ull << (rightShift + adjustment)); // bit mask for the lsb bit
129,806,790✔
2831
                                                bool lsb = (mask & rawFraction);
129,806,790✔
2832
                                                mask >>= 1;
129,806,790✔
2833
                                                bool guard = (mask & rawFraction);
129,806,790✔
2834
                                                mask >>= 1;
129,806,790✔
2835
                                                bool round = (mask & rawFraction);
129,806,790✔
2836
                                                if ((rightShift + adjustment) > 1) {
129,806,790✔
2837
                                                        mask = (0xFFFF'FFFF'FFFF'FFFFull << (rightShift + adjustment - 2));
129,806,790✔
2838
                                                        mask = ~mask;
129,806,790✔
2839
                                                }
2840
                                                else {
2841
                                                        mask = 0;
×
2842
                                                }
2843
#if TRACE_CONVERSION
2844
                                                std::cout << "right shift       : " << rightShift << '\n';
2845
                                                std::cout << "adjustment        : " << adjustment << '\n';
2846
                                                std::cout << "shift to LSB      : " << (rightShift + adjustment) << '\n';
2847
                                                std::cout << "fraction bits     : " << to_binary(rawFraction, ieee754_parameter<Real>::nbits, true) << '\n';
2848
                                                std::cout << "sticky mask bits  : " << to_binary(mask, ieee754_parameter<Real>::nbits, true) << '\n';
2849
#endif
2850
                                                bool sticky = (mask & rawFraction);
129,806,790✔
2851
                                                rawFraction >>= (static_cast<int64_t>(rightShift) + static_cast<int64_t>(adjustment));
129,806,790✔
2852

2853
                                                // execute rounding operation
2854
                                                if (guard) {
129,806,790✔
2855
                                                        if (lsb && (!round && !sticky)) ++rawFraction; // round to even
26,459,681✔
2856
                                                        if (round || sticky) ++rawFraction;
26,459,681✔
2857
                                                        if (rawFraction == (1ull << fbits)) { // overflow
26,459,681✔
2858
                                                                if (biasedExponent == ALL_ONES_ES) { // overflow to INF == .111..01
451,393✔
2859
                                                                        rawFraction = INF_ENCODING;
594✔
2860
                                                                }
2861
                                                                else {
2862
                                                                        ++biasedExponent;
450,799✔
2863
                                                                        rawFraction = 0;
450,799✔
2864
                                                                }
2865
                                                        }
2866
                                                }
2867
#if TRACE_CONVERSION
2868
                                                std::cout << "lsb               : " << (lsb ? "1\n" : "0\n");
2869
                                                std::cout << "guard             : " << (guard ? "1\n" : "0\n");
2870
                                                std::cout << "round             : " << (round ? "1\n" : "0\n");
2871
                                                std::cout << "sticky            : " << (sticky ? "1\n" : "0\n");
2872
                                                std::cout << "rounding decision : " << (lsb && (!round && !sticky) ? "round to even\n" : "-\n");
2873
                                                std::cout << "rounding direction: " << (round || sticky ? "round up\n" : "round down\n");
2874
#endif
2875
                                        }
2876
                                        else { // all bits of the float go into this representation and need to be shifted up, no rounding necessary
2877
                                                int shiftLeft = fbits - ieee754_parameter<Real>::fbits;
2878
                                                rawFraction <<= shiftLeft;
2879
                                        }
2880
#if TRACE_CONVERSION
2881
                                        std::cout << "biased exponent   : " << biasedExponent << " : 0x" << std::hex << biasedExponent << std::dec << '\n';
2882
                                        std::cout << "right shift       : " << rightShift << '\n';
2883
                                        std::cout << "adjustment shift  : " << adjustment << '\n';
2884
                                        std::cout << "sticky bit mask   : " << to_binary(mask, 32, true) << '\n';
2885
                                        std::cout << "fraction bits     : " << to_binary(rawFraction, 32, true) << '\n';
2886
#endif
2887
                                        // construct the target cfloat
2888
                                        bits = (s ? 1ull : 0ull);
129,806,790✔
2889
                                        bits <<= es;
129,806,790✔
2890
                                        bits |= biasedExponent;
129,806,790✔
2891
                                        bits <<= fbits;
129,806,790✔
2892
                                        bits |= rawFraction;
129,806,790✔
2893
#if TRACE_CONVERSION
2894
                                        std::cout << "sign bit          : " << (s ? '1' : '0') << '\n';
2895
                                        std::cout << "biased exponent   : " << biasedExponent << " : 0x" << std::hex << biasedExponent << std::dec << '\n';
2896
                                        std::cout << "fraction bits     : " << to_binary(rawFraction, 32, true) << '\n';
2897
                                        std::cout << "cfloat bits       : " << to_binary(bits, nbits, true) << '\n';
2898
#endif
2899
                                        setbits(bits);
129,806,790✔
2900
                                }
2901
                                else {
2902
                                        // the source real is a subnormal number                                
2903
                                        mask = 0x00FF'FFFFu >> (fbits + exponent + subnormal_reciprocal_shift[es] + 1); // mask for sticky bit 
×
2904

2905
                                        // fraction processing: we have fbits+1 bits = 1 hidden + fbits explicit fraction bits 
2906
                                        // f = 1.ffff  2^exponent * 2^fbits * 2^-(2-2^(es-1)) = 1.ff...ff >> (23 - (-exponent + fbits - (2 -2^(es-1))))
2907
                                        // -exponent because we are right shifting and exponent in this range is negative
2908
                                        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
×
2909
#if TRACE_CONVERSION                                        
2910
                                        std::cout << "source is subnormal: TBD\n";
2911
                                        std::cout << "shift to LSB    " << (rightShift + adjustment) << '\n';
2912
                                        std::cout << "adjustment      " << adjustment << '\n';
2913
                                        std::cout << "exponent        " << exponent << '\n';
2914
                                        std::cout << "subnormal shift " << subnormal_reciprocal_shift[es] << '\n';
2915
#endif
2916
                                        if (exponent >= (MIN_EXP_SUBNORMAL - 1) && exponent < MIN_EXP_NORMAL) {
2917
                                                // the value is a subnormal number in this representation
2918
                                        }
2919
                                        else {
2920
                                                // the value is a normal number in this representation
2921
                                        }
2922
                                }
2923
                        }
2924
                        else {
2925
                                // no need to round, but we need to shift left to deliver the bits
2926
                                // cfloat<40,  8> = float
2927
                                // cfloat<48,  9> = float
2928
                                // cfloat<56, 10> = float
2929
                                // cfloat<64, 11> = float
2930
                                // cfloat<64, 10> = double 
2931
                                // can we go from an input subnormal to a cfloat normal? 
2932
                                // yes, for example a cfloat<64,11> assigned to a subnormal float
2933

2934
                                // map exponent into target cfloat encoding
2935
                                constexpr int upshift = fbits - ieee754_parameter<Real>::fbits;
259,465✔
2936
                                uint64_t biasedExponent{ 0 };
259,465✔
2937
                                bool subnormalInTarget = (exponent < MIN_EXP_NORMAL);
259,465✔
2938
                                int denormShift = 0;
259,465✔
2939
                                if (subnormalInTarget) {
259,465✔
2940
                                        biasedExponent = 0;
643✔
2941
                                        denormShift = MIN_EXP_NORMAL - exponent;
643✔
2942
                                }
2943
                                else {
2944
                                        biasedExponent = static_cast<uint64_t>(static_cast<int64_t>(exponent) + EXP_BIAS);
258,822✔
2945
                                }
2946
                                // output processing
2947
                                if constexpr (nbits < 65) {
2948
                                        // we can compose the bits in a native 64-bit unsigned integer
2949
                                        // common case: normal to normal
2950
                                        // reference example: nbits = 40, es = 8, fbits = 31: rhs = float fbits = 23; shift left by (31 - 23) = 8
2951

2952
                                        if (rawExponent != 0) {
199,196✔
2953
                                                // rhs is a normal encoding (or a normalized former-subnormal)
2954
                                                uint64_t raw{ s ? 1ull : 0ull };
199,196✔
2955
                                                raw <<= es;
199,196✔
2956
                                                raw |= biasedExponent;
199,196✔
2957
                                                raw <<= fbits;
199,196✔
2958
                                                if (subnormalInTarget) {
199,196✔
2959
                                                        // add the hidden bit and denormalize the fraction
2960
                                                        uint64_t significand = rawFraction | ieee754_parameter<Real>::hmask;
623✔
2961
                                                        int netShift = upshift - denormShift;
623✔
2962
                                                        if (netShift >= 0) {
623✔
2963
                                                                rawFraction = significand << netShift;
623✔
2964
                                                        }
2965
                                                        else {
2966
                                                                // right shift with round-to-nearest-even
UNCOV
2967
                                                                int rshift = -netShift;
×
UNCOV
2968
                                                                uint64_t lsbMask = (1ull << rshift);
×
UNCOV
2969
                                                                bool lsb    = (significand & lsbMask) != 0;
×
UNCOV
2970
                                                                bool guard  = (rshift >= 1) && ((significand & (lsbMask >> 1)) != 0);
×
UNCOV
2971
                                                                bool round  = (rshift >= 2) && ((significand & (lsbMask >> 2)) != 0);
×
UNCOV
2972
                                                                bool sticky = (rshift >= 3) && ((significand & ((lsbMask >> 2) - 1)) != 0);
×
UNCOV
2973
                                                                rawFraction = significand >> rshift;
×
UNCOV
2974
                                                                if (guard) {
×
2975
                                                                        if (lsb && (!round && !sticky)) ++rawFraction; // round to even
×
2976
                                                                        if (round || sticky) ++rawFraction;
×
2977
                                                                }
2978
                                                        }
2979
                                                }
2980
                                                else {
2981
                                                        rawFraction <<= upshift;
198,573✔
2982
                                                }
2983
                                                raw |= rawFraction;
199,196✔
2984
                                                setbits(raw);
199,196✔
2985
                                        }
2986
                                        else {
2987
                                                // rhs is a subnormal (unreachable after normalization above, kept for safety)
2988
                                        }
2989
                                }
2990
                                else {
2991
                                        // we need to write and shift bits into place
2992
                                        // use cases are cfloats like cfloat<80, 11, bt>
2993
                                        // even though the bits that come in are single or double precision
2994
                                        // we need to write the fields and then shifting them in place
2995
                                        // 
2996
                                        // common case: normal to normal
2997
                                        if constexpr (bitsInBlock < 64) {
2998
                                                if (rawExponent != 0) {
60,269✔
2999
                                                        // reference example: nbits = 128, es = 15, fbits = 112: rhs = float: shift left by (112 - 23) = 89
3000
                                                        setbits(biasedExponent);
60,269✔
3001
                                                        shiftLeft(fbits);
60,269✔
3002
                                                        // if subnormal in target, add hidden bit before copying
3003
                                                        uint64_t fractionToCopy = subnormalInTarget
60,269✔
3004
                                                                ? (rawFraction | ieee754_parameter<Real>::hmask)
60,269✔
3005
                                                                : rawFraction;
3006
                                                        // shift fraction bits
3007
                                                        int bitsToShift = subnormalInTarget ? (upshift - denormShift) : upshift;
60,269✔
3008
                                                        // for subnormal targets near minpos, bitsToShift can be negative (right shift)
3009
                                                        // apply rounding on the 64-bit significand before splitting into blocks
3010
                                                        if (bitsToShift < 0) {
60,269✔
3011
                                                                int rshift = -bitsToShift;
×
3012
                                                                uint64_t lsbMask = (1ull << rshift);
×
3013
                                                                bool lsb    = (fractionToCopy & lsbMask) != 0;
×
3014
                                                                bool guard  = (rshift >= 1) && ((fractionToCopy & (lsbMask >> 1)) != 0);
×
3015
                                                                bool round  = (rshift >= 2) && ((fractionToCopy & (lsbMask >> 2)) != 0);
×
3016
                                                                bool sticky = (rshift >= 3) && ((fractionToCopy & ((lsbMask >> 2) - 1)) != 0);
×
3017
                                                                fractionToCopy >>= rshift;
×
3018
                                                                if (guard) {
×
3019
                                                                        if (lsb && (!round && !sticky)) ++fractionToCopy; // round to even
×
3020
                                                                        if (round || sticky) ++fractionToCopy;
×
3021
                                                                }
3022
                                                                bitsToShift = 0;
×
3023
                                                        }
3024
                                                        bt fractionBlock[nrBlocks]{ 0 };
60,269✔
3025
                                                        // copy fraction bits
3026
                                                        unsigned blocksRequired = (8 * sizeof(fractionToCopy) + 1) / bitsInBlock;
60,269✔
3027
                                                        unsigned maxBlockNr = (blocksRequired < nrBlocks ? blocksRequired : nrBlocks);
60,269✔
3028
                                                        uint64_t mask = static_cast<uint64_t>(ALL_ONES); // set up the block mask
60,269✔
3029
                                                        unsigned shift = 0;
60,269✔
3030
                                                        for (unsigned i = 0; i < maxBlockNr; ++i) {
340,959✔
3031
                                                                fractionBlock[i] = bt((mask & fractionToCopy) >> shift);
280,690✔
3032
                                                                mask <<= bitsInBlock;
280,690✔
3033
                                                                shift += bitsInBlock;
280,690✔
3034
                                                        }
3035
                                                        if (bitsToShift >= static_cast<int>(bitsInBlock)) {
60,269✔
3036
                                                                int blockShift = static_cast<int>(bitsToShift / bitsInBlock);
50,238✔
3037
                                                                for (int i = MSU; i >= blockShift; --i) {
271,123✔
3038
                                                                        fractionBlock[i] = fractionBlock[i - blockShift];
220,885✔
3039
                                                                }
3040
                                                                for (int i = blockShift - 1; i >= 0; --i) {
160,866✔
3041
                                                                        fractionBlock[i] = bt(0);
110,628✔
3042
                                                                }
3043
                                                                // adjust the shift
3044
                                                                bitsToShift -= blockShift * bitsInBlock;
50,238✔
3045
                                                        }
3046
                                                        if (bitsToShift > 0) {
60,269✔
3047
                                                                // construct the mask for the upper bits in the block that need to move to the higher word
3048
                                                                bt bitsToMoveMask = bt(ALL_ONES << (bitsInBlock - bitsToShift));
40,248✔
3049
                                                                for (unsigned i = MSU; i > 0; --i) {
211,369✔
3050
                                                                        fractionBlock[i] <<= bitsToShift;
171,121✔
3051
                                                                        // mix in the bits from the right
3052
                                                                        bt fracbits = static_cast<bt>(bitsToMoveMask & fractionBlock[i - 1]); // operator & yields an int
171,121✔
3053
                                                                        fractionBlock[i] |= (fracbits >> (bitsInBlock - bitsToShift));
171,121✔
3054
                                                                }
3055
                                                                fractionBlock[0] <<= bitsToShift;
40,248✔
3056
                                                        }
3057
                                                        // OR the bits in
3058
                                                        for (unsigned i = 0; i <= MSU; ++i) {
421,893✔
3059
                                                                _block[i] |= fractionBlock[i];
361,624✔
3060
                                                        }
3061
                                                        // enforce precondition for fast comparison by properly nulling bits that are outside of nbits
3062
                                                        _block[MSU] &= MSU_MASK;
60,269✔
3063
                                                        // finally, set the sign bit
3064
                                                        setsign(s);
60,269✔
3065
                                                }
3066
                                                else {
3067
                                                        // rhs is a subnormal
3068
                //                                        std::cerr << "rhs is a subnormal : " << to_binary(rhs) << " : " << rhs << '\n';
3069
                                                }
3070
                                        }
3071
                                        else {
3072
                                                // BlockType is incorrect
3073
                                        }
3074
                                }
3075
                        }
3076
                        // post-processing results to implement saturation and projection after rounding logic
3077
                        if constexpr (isSaturating) {
3078
                                if (isinf(INF_TYPE_POSITIVE) || isnan(NAN_TYPE_QUIET)) {
20,168,752✔
3079
                                        maxpos();
890✔
3080
                                }
3081
                                else if (isinf(INF_TYPE_NEGATIVE) || isnan(NAN_TYPE_SIGNALLING)) {
20,167,862✔
3082
                                        maxneg();
890✔
3083
                                }
3084
                        }
3085
                        else {
3086
                                if (isnan(NAN_TYPE_QUIET)) {
109,897,503✔
3087
                                        setinf(false);
301,658✔
3088
                                }
3089
                                else if (isnan(NAN_TYPE_SIGNALLING)) {
109,595,845✔
3090
                                        setinf(true);
300,610✔
3091
                                }
3092
                        }
3093
                        return *this;  // TODO: unreachable in some configurations        
130,066,255✔
3094
                }
3095
        }
3096

3097
        // post-processing results to implement saturation and projection after rounding logic
3098
        // arithmetic bit operations can't produce NaN encodings, so we need to re-interpret
3099
        // these encodings and 'project' them to the proper values.
3100
        void constexpr post_process() noexcept {
3101
                if constexpr (isSaturating) {
3102
                        if (isinf(INF_TYPE_POSITIVE) || isnan(NAN_TYPE_QUIET)) {
3103
                                maxpos();
3104
                        }
3105
                        else if (isinf(INF_TYPE_NEGATIVE) || isnan(NAN_TYPE_SIGNALLING)) {
3106
                                maxneg();
3107
                        }
3108
                }
3109
                else {
3110
                        if (isnan(NAN_TYPE_QUIET)) {
3111
                                setinf(false);
3112
                        }
3113
                        else if (isnan(NAN_TYPE_SIGNALLING)) {
3114
                                setinf(true);
3115
                        }
3116
                }
3117
        }
3118

3119
protected:
3120

3121
        /// <summary>
3122
        /// round a set of source bits to the present representation.
3123
        /// srcbits is the number of bits of significant in the source representation
3124
        /// </summary>
3125
        /// <typeparam name="StorageType"></typeparam>
3126
        /// <param name="raw"></param>
3127
        /// <returns></returns>
3128
        template<unsigned srcbits, typename StorageType>
3129
        constexpr uint64_t round(StorageType raw, int& exponent) noexcept {
12,859✔
3130
                if constexpr (fhbits < srcbits) {
3131
                        // round to even: lsb guard round sticky
3132
                    // collect guard, round, and sticky bits
3133
                    // this same logic will work for the case where
3134
                    // we only have a guard bit and no round and sticky bits
3135
                    // because the mask logic will make round and sticky both 0
3136
                        constexpr uint32_t shift = srcbits - fhbits - 1ull;
9,875✔
3137
                        StorageType mask = (StorageType(1ull) << shift);
9,875✔
3138
                        bool guard = (mask & raw);
9,875✔
3139
                        mask >>= 1;
9,875✔
3140
                        bool round = (mask & raw);
9,875✔
3141
                        if constexpr (shift > 1u) { // protect against a negative shift
3142
                                StorageType allones(StorageType(~0));
9,875✔
3143
                                mask = StorageType(allones << (shift - 1));
9,875✔
3144
                                mask = ~mask;
9,875✔
3145
                        }
3146
                        else {
3147
                                mask = 0;
3148
                        }
3149
                        bool sticky = (mask & raw);
9,875✔
3150

3151
                        raw >>= (shift + 1);  // shift out the bits we are rounding away
9,875✔
3152
                        bool lsb = (raw & 0x1u);
9,875✔
3153
                        //  ... lsb | guard  round sticky   round
3154
                        //       x     0       x     x       down
3155
                        //       0     1       0     0       down  round to even
3156
                        //       1     1       0     0        up   round to even
3157
                        //       x     1       0     1        up
3158
                        //       x     1       1     0        up
3159
                        //       x     1       1     1        up
3160
                        if (guard) {
9,875✔
3161
                                if (lsb && (!round && !sticky)) ++raw; // round to even
3,117✔
3162
                                if (round || sticky) ++raw;
3,117✔
3163
                                if (raw == (1ull << fbits)) { // overflow into next power of two
3,117✔
3164
                                        ++exponent;
135✔
3165
                                        raw = 0; // fraction is zero after carry into hidden bit
135✔
3166
                                }
3167
                        }
3168
                }
3169
                else {
3170
                        // Target has more precision than source - need to left-shift to align
3171
                        // For large types where fhbits > 64, the fraction bits cannot fit in
3172
                        // a 64-bit raw after shifting. In this case, skip the shift and let
3173
                        // convert_signed/unsigned_integer place bits using setbit().
3174
                        // The caller positions fraction bits at the top of srcbits, and we
3175
                        // need to ensure they don't overflow 64 bits after our shift.
3176
                        constexpr unsigned shift = fhbits - srcbits;
2,984✔
3177
                        if constexpr (fhbits <= 64 && shift < 64) {
3178
                                raw <<= shift;
2,583✔
3179
                        }
3180
                        // else: raw stays as-is; caller will extract bits and place them
3181
                }
3182
                uint64_t significant = raw;
12,859✔
3183
                return significant;
12,859✔
3184
        }
3185

3186
        template<typename ArgumentBlockType>
3187
        constexpr void copyBits(ArgumentBlockType v) {
3188
                unsigned blocksRequired = (8 * sizeof(v) + 1 ) / bitsInBlock;
3189
                unsigned maxBlockNr = (blocksRequired < nrBlocks ? blocksRequired : nrBlocks);
3190
                bt b{ 0ul }; b = bt(~b);
3191
                ArgumentBlockType mask = ArgumentBlockType(b);
3192
                unsigned shift = 0;
3193
                for (unsigned i = 0; i < maxBlockNr; ++i) {
3194
                        _block[i] = bt((mask & v) >> shift);
3195
                        mask <<= bitsInBlock;
3196
                        shift += bitsInBlock;
3197
                }
3198
        }
3199
        void shiftLeft(int leftShift) {
3200
                if (leftShift == 0) return;
3201
                if (leftShift < 0) return shiftRight(-leftShift);
3202
                if (leftShift > long(nbits)) leftShift = nbits; // clip to max
3203
                if (leftShift >= long(bitsInBlock)) {
3204
                        int blockShift = leftShift / static_cast<int>(bitsInBlock);
3205
                        for (signed i = signed(MSU); i >= blockShift; --i) {
3206
                                _block[i] = _block[i - blockShift];
3207
                        }
3208
                        for (signed i = blockShift - 1; i >= 0; --i) {
3209
                                _block[i] = bt(0);
3210
                        }
3211
                        // adjust the shift
3212
                        leftShift -= (long)(blockShift * bitsInBlock);
3213
                        if (leftShift == 0) return;
3214
                }
3215
                // construct the mask for the upper bits in the block that need to move to the higher word
3216
//                bt mask = static_cast<bt>(0xFFFFFFFFFFFFFFFFull << (bitsInBlock - leftShift));
3217
                bt mask = ALL_ONES;
3218
                mask <<= (bitsInBlock - leftShift);
3219
                for (unsigned i = MSU; i > 0; --i) {
3220
                        _block[i] <<= leftShift;
3221
                        // mix in the bits from the right
3222
                        bt bits = static_cast<bt>(mask & _block[i - 1]);
3223
                        _block[i] |= (bits >> (bitsInBlock - leftShift));
3224
                }
3225
                _block[0] <<= leftShift;
3226
        }
3227
        void shiftRight(int rightShift) {
3228
                if (rightShift == 0) return;
3229
                if (rightShift < 0) return shiftLeft(-rightShift);
3230
                if (rightShift >= long(nbits)) {
3231
                        setzero();
3232
                        return;
3233
                }
3234
                bool signext = sign();
3235
                unsigned blockShift = 0;
3236
                if (rightShift >= long(bitsInBlock)) {
3237
                        blockShift = rightShift / bitsInBlock;
3238
                        if (MSU >= blockShift) {
3239
                                // shift by blocks
3240
                                for (unsigned i = 0; i <= MSU - blockShift; ++i) {
3241
                                        _block[i] = _block[i + blockShift];
3242
                                }
3243
                        }
3244
                        // adjust the shift
3245
                        rightShift -= (long)(blockShift * bitsInBlock);
3246
                        if (rightShift == 0) {
3247
                                // fix up the leading zeros if we have a negative number
3248
                                if (signext) {
3249
                                        // rightShift is guaranteed to be less than nbits
3250
                                        rightShift += (long)(blockShift * bitsInBlock);
3251
                                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3252
                                                this->setbit(i);
3253
                                        }
3254
                                }
3255
                                else {
3256
                                        // clean up the blocks we have shifted clean
3257
                                        rightShift += (long)(blockShift * bitsInBlock);
3258
                                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3259
                                                this->setbit(i, false);
3260
                                        }
3261
                                }
3262
                                return;  // shift was aligned to block boundary, no per-bit shift needed
3263
                        }
3264
                }
3265

3266
                bt mask = ALL_ONES;
3267
                mask >>= (bitsInBlock - rightShift); // this is a mask for the lower bits in the block that need to move to the lower word
3268
                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
3269
                        _block[i] >>= rightShift;
3270
                        // mix in the bits from the left
3271
                        bt bits = static_cast<bt>(mask & _block[i + 1]); // & operator returns an int
3272
                        _block[i] |= (bits << (bitsInBlock - rightShift));
3273
                }
3274
                _block[MSU] >>= rightShift;
3275

3276
                // fix up the leading zeros if we have a negative number
3277
                if (signext) {
3278
                        // bitsToShift is guaranteed to be less than nbits
3279
                        rightShift += (long)(blockShift * bitsInBlock);
3280
                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3281
                                this->setbit(i);
3282
                        }
3283
                }
3284
                else {
3285
                        // clean up the blocks we have shifted clean
3286
                        rightShift += (long)(blockShift * bitsInBlock);
3287
                        for (unsigned i = nbits - rightShift; i < nbits; ++i) {
3288
                                this->setbit(i, false);
3289
                        }
3290
                }
3291

3292
                // enforce precondition for fast comparison by properly nulling bits that are outside of nbits
3293
                _block[MSU] &= MSU_MASK;
3294
        }
3295

3296
        // calculate the integer power 2 ^ b using exponentiation by squaring
3297
        double ipow(int exponent) const {
523,869✔
3298
                bool negative = (exponent < 0);
523,869✔
3299
                exponent = negative ? -exponent : exponent;
523,869✔
3300
                double result(1.0);
523,869✔
3301
                double base = 2.0;
523,869✔
3302
                for (;;) {
3303
                        if (exponent % 2) result *= base;
3,838,137✔
3304
                        exponent >>= 1;
3,838,137✔
3305
                        if (exponent == 0) break;
3,838,137✔
3306
                        base *= base;
3,314,268✔
3307
                }
3308
                return (negative ? (1.0 / result) : result);
523,869✔
3309
        }
3310

3311
        template<BlockTripleOperator btop, typename TargetBlockType = bt>
3312
        constexpr void blockcopy(blocktriple<fbits, btop, TargetBlockType>& tgt) const {
1,056,246✔
3313
                // brute force copy of blocks
3314
                if constexpr (1 == fBlocks) {
3315
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0] & FSU_MASK));
1,050,734✔
3316
                }
3317
                else if constexpr (2 == fBlocks) {
3318
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
3,028✔
3319
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1] & FSU_MASK));
3,028✔
3320
                }
3321
                else if constexpr (3 == fBlocks) {
3322
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
204✔
3323
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
204✔
3324
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2] & FSU_MASK));
204✔
3325
                }
3326
                else if constexpr (4 == fBlocks) {
3327
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
1,500✔
3328
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
1,500✔
3329
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
1,500✔
3330
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3] & FSU_MASK));
1,500✔
3331
                }
3332
                else if constexpr (5 == fBlocks) {
3333
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
×
3334
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
×
3335
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
×
3336
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
×
3337
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4] & FSU_MASK));
×
3338
                }
3339
                else if constexpr (6 == fBlocks) {
3340
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
3341
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
3342
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
3343
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
3344
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4]));
3345
                        tgt.setblock(5, static_cast<TargetBlockType>(_block[5] & FSU_MASK));
3346
                }
3347
                else if constexpr (7 == fBlocks) {
3348
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
8✔
3349
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
8✔
3350
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
8✔
3351
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
8✔
3352
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4]));
8✔
3353
                        tgt.setblock(5, static_cast<TargetBlockType>(_block[5]));
8✔
3354
                        tgt.setblock(6, static_cast<TargetBlockType>(_block[6] & FSU_MASK));
8✔
3355
                }
3356
                else if constexpr (8 == fBlocks) {
3357
                        tgt.setblock(0, static_cast<TargetBlockType>(_block[0]));
370✔
3358
                        tgt.setblock(1, static_cast<TargetBlockType>(_block[1]));
370✔
3359
                        tgt.setblock(2, static_cast<TargetBlockType>(_block[2]));
370✔
3360
                        tgt.setblock(3, static_cast<TargetBlockType>(_block[3]));
370✔
3361
                        tgt.setblock(4, static_cast<TargetBlockType>(_block[4]));
370✔
3362
                        tgt.setblock(5, static_cast<TargetBlockType>(_block[5]));
370✔
3363
                        tgt.setblock(6, static_cast<TargetBlockType>(_block[6]));
370✔
3364
                        tgt.setblock(7, static_cast<TargetBlockType>(_block[7] & FSU_MASK));
370✔
3365
                }
3366
                else {
3367
                        for (unsigned i = 0; i < FSU; ++i) {
4,127✔
3368
                                tgt.setblock(i, static_cast<TargetBlockType>(_block[i]));
3,725✔
3369
                        }
3370
                        tgt.setblock(FSU, static_cast<TargetBlockType>(_block[FSU] & FSU_MASK));
402✔
3371
                }
3372
        }
1,056,246✔
3373

3374
private:
3375
        bt _block[nrBlocks];
3376

3377
        //////////////////////////////////////////////////////////////////////////////
3378
        // friend functions
3379

3380
        // template parameters need names different from class template parameters (for gcc and clang)
3381
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3382
        friend std::ostream& operator<< (std::ostream& ostr, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& r);
3383
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3384
        friend std::istream& operator>> (std::istream& istr, cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& r);
3385

3386
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3387
        friend bool operator==(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3388
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3389
        friend bool operator!=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3390
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3391
        friend bool operator< (const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3392
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3393
        friend bool operator> (const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3394
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3395
        friend bool operator<=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3396
        template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3397
        friend bool operator>=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs);
3398
};
3399

3400
///////////////////////////// IOSTREAM operators ///////////////////////////////////////////////
3401

3402
// convert cfloat to decimal fixpnt string, i.e. "-1234.5678"
3403
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3404
std::string to_decimal_fixpnt_string(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& value, long long precision) {
3405
        constexpr unsigned fbits = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits;
3406
        constexpr unsigned bias = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::EXP_BIAS;
3407
        std::stringstream str;
3408
        if (value.iszero()) {
3409
                str << '0';
3410
                return str.str();
3411
        }
3412
        if (value.sign()) str << '-';
3413

3414
        // construct the discretization levels of the fraction part
3415
        support::decimal range, discretizationLevels, step;
3416
        // create the decimal range we are discretizing
3417
        range.setdigit(1);
3418
        range.shiftLeft(fbits); // the decimal range of the fraction
3419
        discretizationLevels.powerOf2(fbits); // calculate the discretization levels of this range
3420
        step = div(range, discretizationLevels);
3421
        // now construct the value of this range by adding the fraction samples
3422
        support::decimal partial, multiplier;
3423
        partial.setzero();  // if you just want the fraction
3424
        multiplier.setdigit(1);
3425
        // convert the fraction part
3426
        for (unsigned i = 0; i < fbits; ++i) {
3427
                if (value.at(i)) {
3428
                        support::add(partial, multiplier);
3429
                }
3430
                support::add(multiplier, multiplier);
3431
        }
3432
        if (value.isdenormal()) {
3433
                support::mul(partial, step);
3434
                support::decimal scale;
3435
                scale.powerOf2(bias - 1ull);
3436
                partial = support::div(partial, scale);
3437
        } 
3438
        else {
3439
                support::add(partial, multiplier); // add the hidden bit
3440
                support::mul(partial, step);
3441
                support::decimal scale;
3442
                int exponent = value.scale();
3443
                if (exponent < 0) {
3444
                        scale.powerOf2(static_cast<unsigned>(-exponent));
3445
                        partial = support::div(partial, scale);
3446
                }
3447
                else {
3448
                        scale.powerOf2(static_cast<unsigned>(exponent));
3449
                        support::mul(partial, scale);
3450
                }
3451
        }
3452

3453
        // the radix is at fbits
3454
        // The partial represents the parts in the range, so we can deduce
3455
        // the number of leading zeros by comparing to the length of range
3456
        int nrLeadingZeros = static_cast<int>(range.size()) - static_cast<int>(partial.size()) - 1;
3457
        if (nrLeadingZeros >= 0) str << "0.";
3458
        for (int i = 0; i < nrLeadingZeros; ++i) str << '0';
3459
        int digitsWritten = (nrLeadingZeros > 0) ? nrLeadingZeros : 0;
3460
        int position = static_cast<int>(partial.size()) - 1;
3461
        for (support::decimal::const_reverse_iterator rit = partial.rbegin(); rit != partial.rend(); ++rit) {
3462
                str << (int)*rit;
3463
                ++digitsWritten;
3464
                if (position == fbits) str << '.';
3465
                --position;
3466
        }
3467
        if (digitsWritten < precision) { // deal with trailing 0s
3468
                for (unsigned i = static_cast<unsigned>(digitsWritten); i < fbits; ++i) {
3469
                        str << '0';
3470
                }
3471
        }
3472

3473
        return str.str();
3474
}
3475

3476
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3477
std::string to_string(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& value, long long precision) {
3478
        constexpr unsigned fbits = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits;
3479
        std::stringstream str;
3480
        if (value.iszero()) {
3481
                str << '0';
3482
                return str.str();
3483
        }
3484
        if (value.sign()) str << '-';
3485

3486
        // denormalize the number to gain access to the most sigificant digits
3487
        // 1.ffff^e
3488
        // scale is e
3489
        // lsbScale is e - fbits
3490
        // shift to get lsb to position 2^0 = (e - fbits)
3491
        std::int64_t scale = value.scale();
3492
//        std::int64_t shift = scale + fbits; // we want the lsb at 2^0
3493
        std::int64_t lsbScale = scale - fbits;  // scale of the lsb
3494
        support::decimal partial, multiplier;
3495
        partial.setzero();
3496

3497
        multiplier.powerOf2(lsbScale);
3498

3499
        // convert the fraction bits 
3500
        for (unsigned i = 0; i < fbits; ++i) {
3501
                if (value.at(i)) {
3502
                        support::add(partial, multiplier);
3503
                }
3504
                support::add(multiplier, multiplier);
3505
        }
3506
        if (!value.isdenormal()) {
3507
                support::add(partial, multiplier); // add the hidden bit
3508
        }
3509
        str << partial;
3510
        return str.str();
3511
}
3512

3513
//////////////////////////////////////////////////////////////////////////////////////////////
3514
/// stream operators
3515

3516
// ostream output generates an ASCII format for the floating-point argument
3517
// Uses native binary-to-decimal conversion via blocktriple::to_string()
3518
// to produce exact output for all cfloat sizes without double conversion.
3519
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3520
inline std::ostream& operator<<(std::ostream& ostr, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& v) {
3,117✔
3521
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3522
        constexpr unsigned cfbits = Cfloat::fbits;
3,117✔
3523

3524
        std::streamsize prec  = ostr.precision();
3,117✔
3525
        std::streamsize width = ostr.width();
3,117✔
3526
        std::ios_base::fmtflags ff = ostr.flags();
3,117✔
3527
        bool bFixed      = (ff & std::ios_base::fixed) == std::ios_base::fixed;
3,117✔
3528
        bool bScientific = (ff & std::ios_base::scientific) == std::ios_base::scientific;
3,117✔
3529
        bool bShowpos    = (ff & std::ios_base::showpos) != 0;
3,117✔
3530
        bool bUppercase  = (ff & std::ios_base::uppercase) != 0;
3,117✔
3531
        bool bInternal   = (ff & std::ios_base::internal) != 0;
3,117✔
3532
        bool bLeft       = (ff & std::ios_base::left) != 0;
3,117✔
3533
        char fillChar    = ostr.fill();
3,117✔
3534

3535
        if constexpr (cfbits == 0) {
3536
                // degenerate cfloat with no fraction bits: fall back to double
3537
                std::ostringstream oss;
3538
                oss.precision(prec);
3539
                if (bFixed) oss << std::fixed;
3540
                if (bScientific) oss << std::scientific;
3541
                if (bUppercase) oss << std::uppercase;
3542
                if (bShowpos) oss << std::showpos;
3543
                oss << static_cast<double>(v);
3544
                std::string s = oss.str();
3545
                if (width > 0 && s.length() < static_cast<size_t>(width)) {
3546
                        size_t pad = static_cast<size_t>(width) - s.length();
3547
                        if (bLeft) { s.append(pad, fillChar); }
3548
                        else { s.insert(0u, pad, fillChar); }
3549
                }
3550
                return ostr << s;
3551
        } else {
3552
                blocktriple<cfbits, BlockTripleOperator::REP, bt> a;
3,117✔
3553
                v.normalize(a);
3,117✔
3554
                return ostr << a.to_string(prec, width, bFixed, bScientific,
6,234✔
3555
                                           bInternal, bLeft, bShowpos, bUppercase, fillChar);
6,234✔
3556
        }
3557
}
3558

3559
// parse a cfloat from a string in either cfloat hex format (nbits.esxHEXVALUEc)
3560
// or a decimal floating-point representation
3561
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3562
bool parse(const std::string& txt, cfloat<nbits,es,bt,hasSubnormals,hasMaxExpValues,isSaturating>& v) {
24✔
3563
        // check if the txt is of the native cfloat form: nbits.esX[0x]hexvaluec
3564
        std::regex cfloat_regex(R"(^[0-9]+\.[0-9]+[xX](0[xX])?[0-9A-Fa-f]+c?$)");
24✔
3565
        if (std::regex_match(txt, cfloat_regex)) {
24✔
3566
                // found a cfloat representation: parse nbits.esxHEXVALUEc
3567
                std::string nbitsStr, esStr, bitStr;
3✔
3568
                auto it = txt.begin();
3✔
3569
                for (; it != txt.end(); ++it) {
9✔
3570
                        if (*it == '.') break;
9✔
3571
                        nbitsStr.append(1, *it);
6✔
3572
                }
3573
                for (++it; it != txt.end(); ++it) {
6✔
3574
                        if (*it == 'x' || *it == 'X') break;
6✔
3575
                        esStr.append(1, *it);
3✔
3576
                }
3577
                for (++it; it != txt.end(); ++it) {
21✔
3578
                        if (*it == 'c') break;
21✔
3579
                        bitStr.append(1, *it);
18✔
3580
                }
3581
                unsigned nbits_in = 0;
3✔
3582
                unsigned es_in = 0;
3✔
3583
                {
3584
                        std::istringstream ss(nbitsStr);
3✔
3585
                        ss >> nbits_in;
3✔
3586
                        if (ss.fail()) return false;
3✔
3587
                }
3✔
3588
                {
3589
                        std::istringstream ss(esStr);
3✔
3590
                        ss >> es_in;
3✔
3591
                        if (ss.fail()) return false;
3✔
3592
                }
3✔
3593
                // native cfloat form must match target configuration
3594
                if (nbits_in != nbits || es_in != es) return false;
3✔
3595
                uint64_t raw = 0;
2✔
3596
                std::istringstream ss(bitStr);
2✔
3597
                ss >> std::hex >> raw;
2✔
3598
                if (ss.fail()) return false;
2✔
3599
                ss >> std::ws;
2✔
3600
                if (!ss.eof()) return false;
2✔
3601
                v.setbits(raw);
2✔
3602
                return true;
2✔
3603
        }
3✔
3604
        else {
3605
                // assume it is a float/double/long double representation
3606
                std::istringstream ss(txt);
21✔
3607
                double d;
3608
                ss >> d;
21✔
3609
                if (ss.fail()) return false;
21✔
3610
                ss >> std::ws;
21✔
3611
                if (!ss.eof()) return false;
21✔
3612
                v = d;
20✔
3613
                return true;
20✔
3614
        }
21✔
3615
}
24✔
3616

3617
// read an ASCII float or cfloat format: nbits.esxNN...NNc, for example: 16.5x7C00c
3618
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3619
inline std::istream& operator>>(std::istream& istr, cfloat<nbits,es,bt,hasSubnormals,hasMaxExpValues,isSaturating>& v) {
17✔
3620
        std::string txt;
17✔
3621
        istr >> txt;
17✔
3622
        if (!parse(txt, v)) {
17✔
3623
                std::cerr << "unable to parse -" << txt << "- into a cfloat value\n";
×
3624
        }
3625
        return istr;
17✔
3626
}
17✔
3627

3628
// encoding helpers
3629

3630
// return the Unit in the Last Position
3631
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3632
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ulp(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& a) {
49✔
3633
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> b(a);
49✔
3634
        return ++b - a;
86✔
3635
}
3636

3637
// transform cfloat to a binary representation
3638
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3639
inline std::string to_binary(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& number, bool nibbleMarker = false) {
1,496✔
3640
        std::stringstream s;
1,496✔
3641
        s << "0b";
1,496✔
3642
        unsigned index = nbits;
1,496✔
3643
        s << (number.at(--index) ? '1' : '0') << '.';
1,496✔
3644

3645
        for (int i = int(es) - 1; i >= 0; --i) {
10,375✔
3646
                s << (number.at(--index) ? '1' : '0');
8,879✔
3647
                if (i > 0 && (i % 4) == 0 && nibbleMarker) s << '\'';
8,879✔
3648
        }
3649

3650
        s << '.';
1,496✔
3651

3652
        constexpr int fbits = nbits - 1ull - es;
1,496✔
3653
        for (int i = fbits - 1; i >= 0; --i) {
32,026✔
3654
                s << (number.at(--index) ? '1' : '0');
30,530✔
3655
                if (i > 0 && (i % 4) == 0 && nibbleMarker) s << '\'';
30,530✔
3656
        }
3657

3658
        return s.str();
2,992✔
3659
}
1,496✔
3660

3661
// native semantic representation: radix-2, delegates to to_binary
3662
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3663
inline std::string to_native(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& number, bool nibbleMarker = false) {
3664
        return to_binary(number, nibbleMarker);
3665
}
3666

3667
// transform a cfloat into a triple representation
3668
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3669
inline std::string to_triple(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& number, bool nibbleMarker = true) {
3✔
3670
        std::stringstream s;
3✔
3671
        blocktriple<cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits, BlockTripleOperator::REP, bt> triple;
3✔
3672
        number.normalize(triple);
3✔
3673
        s << to_triple(triple, nibbleMarker);
3✔
3674
        return s.str();
6✔
3675
}
3✔
3676

3677
// Magnitude of a cfloat (equivalent to turning the sign bit off).
3678
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3679
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>
3680
abs(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& v) {
16,939✔
3681
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> a(v);
16,939✔
3682
        a.setsign(false);
16,939✔
3683
        return a;
16,939✔
3684
}
3685

3686
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3687
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>
3688
fabs(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> v) {
40✔
3689
        return abs(v);
40✔
3690
}
3691

3692
////////////////////// debug helpers
3693

3694
// convenience method to gain access to the values of the constexpr variables that govern the cfloat behavior
3695
template<unsigned nbits, unsigned es, typename bt = uint8_t, bool hasSubnormals = false, bool hasMaxExpValues = false, bool isSaturating = false>
3696
void ReportCfloatClassParameters() {
3697
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> a;
3698
        a.constexprClassParameters();
3699
}
3700

3701
//////////////////////////////////////////////////////
3702
/// cfloat - cfloat binary logic operators
3703

3704
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3705
inline bool operator==(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) {
127,146,791✔
3706
        if (lhs.isnan() || rhs.isnan()) return false;
127,146,791✔
3707
        if (lhs.iszero() && rhs.iszero()) return true; // +0 == -0 per IEEE-754 §5.11
125,491,702✔
3708
        for (unsigned i = 0; i < lhs.nrBlocks; ++i) {
385,823,599✔
3709
                if (lhs._block[i] != rhs._block[i]) {
263,585,366✔
3710
                        return false;
2,104,957✔
3711
                }
3712
        }
3713
        return true;
122,238,233✔
3714
}
3715
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3716
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); }
125,744,361✔
3717
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3718
inline bool operator< (const cfloat<nnbits, nes, nbt, nsub, nsup, nsat>& lhs, const cfloat<nnbits, nes, nbt, nsub, nsup, nsat>& rhs) {
5,941,730✔
3719
        if (lhs.isnan() || rhs.isnan()) return false;
5,941,730✔
3720
        // need this as arithmetic difference is defined as snan(indeterminate)
3721
        if (lhs.isinf(INF_TYPE_NEGATIVE) && rhs.isinf(INF_TYPE_NEGATIVE)) return false;
5,826,222✔
3722
        if (lhs.isinf(INF_TYPE_POSITIVE) && rhs.isinf(INF_TYPE_POSITIVE)) return false;
5,826,208✔
3723
        if constexpr (nsub) {
3724
                cfloat<nnbits, nes, nbt, nsub, nsup, nsat> diff = (lhs - rhs);
5,757,248✔
3725
                return (!diff.iszero() && diff.sign()) ? true : false;  // got to guard against -0
5,757,248✔
3726
        }
3727
        else {
3728
                if (lhs.iszero() && rhs.iszero()) return false;  // we need to 'collapse' all zero encodings
68,927✔
3729
                if (lhs.sign() && !rhs.sign()) return true;
68,919✔
3730
                if (!lhs.sign() && rhs.sign()) return false;
51,696✔
3731
                bool positive = lhs.ispos();
34,473✔
3732
                if (positive) {
34,473✔
3733
                        if (lhs.scale() < rhs.scale()) return true;
17,253✔
3734
                        if (lhs.scale() > rhs.scale()) return false;
12,785✔
3735
                }
3736
                else {
3737
                        if (lhs.scale() > rhs.scale()) return true;
17,220✔
3738
                        if (lhs.scale() < rhs.scale()) return false;
12,770✔
3739
                }
3740
                // sign and scale are the same
3741
                if (lhs.scale() == rhs.scale()) {
16,642✔
3742
                        // compare fractions: we do not have subnormals, so we can ignore the hidden bit
3743
                        blockbinary<nnbits - 1ull - nes, nbt> l, r;
3744
                        lhs.fraction(l);
16,642✔
3745
                        rhs.fraction(r);
16,642✔
3746
                        blockbinary<nnbits - nes, nbt> ll, rr; // fbits + 1 so we can 0 extend to honor 2's complement encoding of blockbinary
3747
                        ll.assignWithoutSignExtend(l);
16,642✔
3748
                        rr.assignWithoutSignExtend(r);
16,642✔
3749
                        return (positive ? (ll < rr) : (ll > rr));
16,642✔
3750
                }
3751
                return false;
×
3752
        }
3753
}
3754
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3755
inline bool operator> (const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) { 
3,143,728✔
3756
        if (lhs.isnan() || rhs.isnan()) return false;
3,143,728✔
3757
        // need this as arithmetic difference is defined as snan(indeterminate)
3758
        if (lhs.isinf(INF_TYPE_NEGATIVE) && rhs.isinf(INF_TYPE_NEGATIVE)) return false;
3,135,614✔
3759
        if (lhs.isinf(INF_TYPE_POSITIVE) && rhs.isinf(INF_TYPE_POSITIVE)) return false;
3,135,600✔
3760
        return  operator< (rhs, lhs); 
3,135,584✔
3761
}
3762
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3763
inline bool operator<=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) { 
1,747,156✔
3764
        if (lhs.isnan() || rhs.isnan()) return false;
1,747,156✔
3765
        return !operator> (lhs, rhs); 
1,739,056✔
3766
}
3767
template<unsigned nnbits, unsigned nes, typename nbt, bool nsub, bool nsup, bool nsat>
3768
inline bool operator>=(const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& lhs, const cfloat<nnbits,nes,nbt,nsub,nsup,nsat>& rhs) {
1,530,050✔
3769
        if (lhs.isnan() || rhs.isnan()) return false;
1,530,050✔
3770
        return !operator< (lhs, rhs); 
1,521,944✔
3771
}
3772

3773
//////////////////////////////////////////////////////
3774
/// cfloat - cfloat binary arithmetic operators
3775

3776
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3777
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) {
10,323,668✔
3778
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
10,323,668✔
3779
        sum += rhs;
10,323,668✔
3780
        return sum;
10,323,668✔
3781
}
3782
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3783
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,797,151✔
3784
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
8,797,151✔
3785
        diff -= rhs;
8,797,151✔
3786
        return diff;
8,797,151✔
3787
}
3788
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3789
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,170,182✔
3790
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
4,170,182✔
3791
        mul *= rhs;
4,170,182✔
3792
        return mul;
4,170,182✔
3793
}
3794
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3795
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,022,193✔
3796
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
5,022,193✔
3797
        ratio /= rhs;
5,022,193✔
3798
        return ratio;
5,022,192✔
3799
}
3800

3801
/// binary cfloat - literal arithmetic operators
3802

3803
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3804
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3805
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3806
        sum += rhs;
3807
        return sum;
3808
}
3809
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3810
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3811
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3812
        diff -= rhs;
3813
        return diff;
3814
}
3815
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3816
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3817
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3818
        mul *= rhs;
3819
        return mul;
3820
}
3821
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3822
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(float lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
2✔
3823
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
2✔
3824
        ratio /= rhs;
2✔
3825
        return ratio;
2✔
3826
}
3827

3828
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3829
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
4✔
3830
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
4✔
3831
        sum += rhs;
4✔
3832
        return sum;
4✔
3833
}
3834
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3835
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3836
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3837
        diff -= rhs;
3838
        return diff;
3839
}
3840
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3841
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
16✔
3842
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
16✔
3843
        mul *= rhs;
16✔
3844
        return mul;
16✔
3845
}
3846
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3847
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(double lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3848
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3849
        ratio /= rhs;
3850
        return ratio;
3851
}
3852

3853
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3854
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
×
3855
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
×
3856
        sum += rhs;
×
3857
        return sum;
×
3858
}
3859
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3860
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3861
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3862
        diff -= rhs;
3863
        return diff;
3864
}
3865
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3866
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
38✔
3867
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
38✔
3868
        mul *= rhs;
38✔
3869
        return mul;
38✔
3870
}
3871
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3872
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
374✔
3873
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
374✔
3874
        ratio /= rhs;
374✔
3875
        return ratio;
374✔
3876
}
3877

3878
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3879
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3880
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3881
        sum += rhs;
3882
        return sum;
3883
}
3884
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3885
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3886
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3887
        diff -= rhs;
3888
        return diff;
3889
}
3890
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3891
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3892
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3893
        mul *= rhs;
3894
        return mul;
3895
}
3896
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3897
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(unsigned int lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3898
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3899
        ratio /= rhs;
3900
        return ratio;
3901
}
3902

3903
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3904
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3905
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3906
        sum += rhs;
3907
        return sum;
3908
}
3909
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3910
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3911
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3912
        diff -= rhs;
3913
        return diff;
3914
}
3915
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3916
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3917
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3918
        mul *= rhs;
3919
        return mul;
3920
}
3921
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3922
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3923
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3924
        ratio /= rhs;
3925
        return ratio;
3926
}
3927

3928
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3929
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3930
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> sum(lhs);
3931
        sum += rhs;
3932
        return sum;
3933
}
3934
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3935
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3936
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> diff(lhs);
3937
        diff -= rhs;
3938
        return diff;
3939
}
3940
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3941
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3942
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> mul(lhs);
3943
        mul *= rhs;
3944
        return mul;
3945
}
3946
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3947
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(unsigned long long lhs, const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& rhs) {
3948
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ratio(lhs);
3949
        ratio /= rhs;
3950
        return ratio;
3951
}
3952

3953
///  binary cfloat - literal arithmetic operators
3954

3955
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3956
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3957
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3958
        Cfloat sum(lhs);
3959
        sum += Cfloat(rhs);
3960
        return sum;
3961
}
3962
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3963
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3964
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3965
        Cfloat diff(lhs);
3966
        diff -= rhs;
3967
        return diff;
3968
}
3969
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3970
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3971
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3972
        Cfloat mul(lhs);
3973
        mul *= Cfloat(rhs);
3974
        return mul;
3975
}
3976
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3977
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
3978
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3979
        Cfloat ratio(lhs);
3980
        ratio /= Cfloat(rhs);
3981
        return ratio;
3982
}
3983

3984
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3985
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
3986
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3987
        Cfloat sum(lhs);
3988
        sum += Cfloat(rhs);
3989
        return sum;
3990
}
3991
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3992
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
4✔
3993
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
3994
        Cfloat diff(lhs);
4✔
3995
        diff -= rhs;
4✔
3996
        return diff;
4✔
3997
}
3998
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
3999
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
4000
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4001
        Cfloat mul(lhs);
4002
        mul *= Cfloat(rhs);
4003
        return mul;
4004
}
4005
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4006
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
4007
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4008
        Cfloat ratio(lhs);
4009
        ratio /= Cfloat(rhs);
4010
        return ratio;
4011
}
4012

4013
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4014
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
4015
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4016
        Cfloat sum(lhs);
4017
        sum += Cfloat(rhs);
4018
        return sum;
4019
}
4020
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4021
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
4✔
4022
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4023
        Cfloat diff(lhs);
4✔
4024
        diff -= rhs;
4✔
4025
        return diff;
4✔
4026
}
4027
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4028
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
4029
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4030
        Cfloat mul(lhs);
4031
        mul *= Cfloat(rhs);
4032
        return mul;
4033
}
4034
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4035
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
4036
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4037
        Cfloat ratio(lhs);
4038
        ratio /= Cfloat(rhs);
4039
        return ratio;
4040
}
4041

4042
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4043
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
4044
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4045
        Cfloat sum(lhs);
4046
        sum += Cfloat(rhs);
4047
        return sum;
4048
}
4049
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4050
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
4051
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4052
        Cfloat diff(lhs);
4053
        diff -= rhs;
4054
        return diff;
4055
}
4056
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4057
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
4058
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4059
        Cfloat mul(lhs);
4060
        mul *= Cfloat(rhs);
4061
        return mul;
4062
}
4063
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4064
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned int rhs) {
4065
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4066
        Cfloat ratio(lhs);
4067
        ratio /= Cfloat(rhs);
4068
        return ratio;
4069
}
4070

4071
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4072
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4073
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4074
        Cfloat sum(lhs);
4075
        sum += Cfloat(rhs);
4076
        return sum;
4077
}
4078
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4079
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4080
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4081
        Cfloat diff(lhs);
4082
        diff -= rhs;
4083
        return diff;
4084
}
4085
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4086
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4087
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4088
        Cfloat mul(lhs);
4089
        mul *= Cfloat(rhs);
4090
        return mul;
4091
}
4092
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4093
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4094
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4095
        Cfloat ratio(lhs);
4096
        ratio /= Cfloat(rhs);
4097
        return ratio;
4098
}
4099

4100
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4101
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator+(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
4102
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4103
        Cfloat sum(lhs);
4104
        sum += Cfloat(rhs);
4105
        return sum;
4106
}
4107
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4108
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator-(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
4109
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4110
        Cfloat diff(lhs);
4111
        diff -= rhs;
4112
        return diff;
4113
}
4114
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4115
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator*(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
4116
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4117
        Cfloat mul(lhs);
4118
        mul *= Cfloat(rhs);
4119
        return mul;
4120
}
4121
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4122
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> operator/(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, unsigned long long rhs) {
4123
        using Cfloat = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>;
4124
        Cfloat ratio(lhs);
4125
        ratio /= Cfloat(rhs);
4126
        return ratio;
4127
}
4128

4129
///////////////////////////////////////////////////////////////////////
4130
///   binary logic literal comparisons
4131

4132
// cfloat - literal float logic operators
4133
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4134
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
4135
        return float(lhs) == rhs;
1✔
4136
}
4137
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4138
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
4139
        return float(lhs) != rhs;
1✔
4140
}
4141
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4142
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
2✔
4143
        return float(lhs) < rhs;
2✔
4144
}
4145
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4146
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
4147
        return float(lhs) > rhs;
1✔
4148
}
4149
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4150
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
4151
        return float(lhs) <= rhs;
1✔
4152
}
4153
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4154
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, float rhs) {
1✔
4155
        return float(lhs) >= rhs;
1✔
4156
}
4157
// cfloat - literal double logic operators
4158
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4159
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
4160
        return double(lhs) == rhs;
1✔
4161
}
4162
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4163
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
4164
        return double(lhs) != rhs;
1✔
4165
}
4166
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4167
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
336✔
4168
        return double(lhs) < rhs;
336✔
4169
}
4170
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4171
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
892✔
4172
        return double(lhs) > rhs;
892✔
4173
}
4174
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4175
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
4176
        return double(lhs) <= rhs;
1✔
4177
}
4178
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4179
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, double rhs) {
1✔
4180
        return double(lhs) >= rhs;
1✔
4181
}
4182

4183
#if LONG_DOUBLE_SUPPORT
4184
// cfloat - literal long double logic operators
4185
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4186
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
4187
        return (long double)(lhs) == rhs;
1✔
4188
}
4189
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4190
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
4191
        return (long double)(lhs) != rhs;
1✔
4192
}
4193
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4194
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
4195
        return (long double)(lhs) < rhs;
1✔
4196
}
4197
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4198
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
4199
        return (long double)(lhs) > rhs;
1✔
4200
}
4201
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4202
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
4203
        return (long double)(lhs) <= rhs;
1✔
4204
}
4205
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4206
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long double rhs) {
1✔
4207
        return (long double)(lhs) >= rhs;
1✔
4208
}
4209
#endif
4210

4211
// cfloat - literal int logic operators
4212
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4213
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
11✔
4214
        return operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
11✔
4215
}
4216
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4217
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
1✔
4218
        return operator!=(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
1✔
4219
}
4220
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4221
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
240✔
4222
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
240✔
4223
}
4224
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4225
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
666✔
4226
        return operator<(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs), lhs);
666✔
4227
}
4228
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4229
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
1✔
4230
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs)) || operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
1✔
4231
}
4232
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4233
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, int rhs) {
1✔
4234
        return !operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
1✔
4235
}
4236

4237
// cfloat - long long logic operators
4238
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4239
inline bool operator==(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4240
        return operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
4241
}
4242
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4243
inline bool operator!=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4244
        return operator!=(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
4245
}
4246
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4247
inline bool operator< (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4248
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
4249
}
4250
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4251
inline bool operator> (const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4252
        return operator<(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs), lhs);
4253
}
4254
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4255
inline bool operator<=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4256
        return operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs)) || operator==(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
4257
}
4258
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4259
inline bool operator>=(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& lhs, long long rhs) {
4260
        return !operator<(lhs, cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>(rhs));
4261
}
4262

4263
// standard library functions for floating point
4264

4265
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4266
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> frexp(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& x, int* exp) {
765✔
4267
        *exp = x.scale();
765✔
4268
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> fraction(x);
765✔
4269
        fraction.setexponent(0);
765✔
4270
        return fraction;
765✔
4271
}
4272

4273
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4274
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> ldexp(const cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& x, int exp) {
765✔
4275
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> result(x);
765✔
4276
        int xexp = x.scale();
765✔
4277
        result.setexponent(xexp + exp);  // TODO: this does not work for subnormals
765✔
4278
        return result;
765✔
4279
}
4280

4281
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4282
inline cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> 
4283
fma(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> x,
3✔
4284
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> y,
4285
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> z) {
4286
        cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> fused{ 0 };
3✔
4287
        constexpr unsigned FBITS = cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>::fbits;
3✔
4288
        constexpr unsigned EXTRA_FBITS = FBITS+2;
3✔
4289
        constexpr unsigned EXTENDED_PRECISION = nbits + EXTRA_FBITS;
3✔
4290
        // the C++ fma spec indicates that the x*y+z is evaluated in 'infinite' precision
4291
        // with only a single rounding event. The minimum finite precision that would behave like this
4292
        // is the precision where the product x*y does not need to be rounded, which will
4293
        // need at least 2*(fbits+1) mantissa bits to capture all bits that can be
4294
        // generated by the product.
4295
        cfloat<EXTENDED_PRECISION, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> preciseX(x), preciseY(y), preciseZ(z);
3✔
4296
//        ReportValue(preciseX, "extended precision x");
4297
//        ReportValue(preciseY, "extended precision y");
4298
//        ReportValue(preciseZ, "extended precision z");
4299
        cfloat<EXTENDED_PRECISION, es, bt, hasSubnormals, hasMaxExpValues, isSaturating> product = preciseX * preciseY;
3✔
4300
//        ReportValue(product, "extended precision p");
4301
        fused = product + preciseZ;
3✔
4302
        return fused;
3✔
4303
}
4304

4305
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4306
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
4307
minpos(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
4308
        return c.minpos();
4309
}
4310
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4311
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
4312
maxpos(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
4313
        return c.maxpos();
4314
}
4315
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4316
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
4317
minneg(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
4318
        return c.minneg();
4319
}
4320
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormals, bool hasMaxExpValues, bool isSaturating>
4321
cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>&
4322
maxneg(cfloat<nbits, es, bt, hasSubnormals, hasMaxExpValues, isSaturating>& c) {
4323
        return c.maxneg();
4324
}
4325

4326
}} // namespace sw::universal
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc