blob: 3b9fc6d3645e7ea14f81ff78f8cd3ddd6781c778 [file] [log] [blame]
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -07001/* ==========================================================================
Laurence Lundbladebf8e1cf2024-01-17 11:58:24 -07002 * ieee754.c -- floating-point conversion for half, double & single-precision
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -07003 *
4 * Copyright (c) 2018-2024, Laurence Lundblade. All rights reserved.
5 * Copyright (c) 2021, Arm Limited. All rights reserved.
6 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 * See BSD-3-Clause license in README.md
10 *
11 * Created on 7/23/18
12 * ========================================================================== */
Laurence Lundbladecc2ed342018-09-22 17:29:55 -070013
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +020014#include "qcbor/qcbor_common.h"
15
Laurence Lundblade9682a532020-06-06 18:33:04 -070016
Laurence Lundblade12d32c52018-09-19 11:25:27 -070017#include "ieee754.h"
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070018#include <string.h> /* For memcpy() */
Laurence Lundblade12d32c52018-09-19 11:25:27 -070019
Laurence Lundblade8db3d3e2018-09-29 11:46:37 -070020
Laurence Lundblade12d32c52018-09-19 11:25:27 -070021/*
Laurence Lundbladee026f4f2024-01-18 13:48:34 -070022 * This has long lines and is easier to read because of
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070023 * them. Some coding guidelines prefer 80 column lines (can they not
24 * afford big displays?).
25 *
26 * This code works solely using shifts and masks and thus has no
27 * dependency on any math libraries. It can even work if the CPU
28 * doesn't have any floating-point support, though that isn't the most
29 * useful thing to do.
30 *
31 * The memcpy() dependency is only for CopyFloatToUint32() and friends
32 * which only is needed to avoid type punning when converting the
33 * actual float bits to an unsigned value so the bit shifts and masks
34 * can work.
35 *
36 * The references used to write this code:
37 *
38 * IEEE 754-2008, particularly section 3.6 and 6.2.1
39 *
40 * https://en.wikipedia.org/wiki/IEEE_754 and subordinate pages
41 *
42 * https://stackoverflow.com/questions/19800415/why-does-ieee-754-reserve-so-many-nan-values
43 *
44 * https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules
45 *
46 * https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be
47 *
48 * IEEE754_FloatToDouble(uint32_t uFloat) was created but is not
49 * needed. It can be retrieved from github history if needed.
Laurence Lundblade12d32c52018-09-19 11:25:27 -070050 */
51
52
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070053
54
55/* ----- Half Precsion ----------- */
Laurence Lundblade12d32c52018-09-19 11:25:27 -070056#define HALF_NUM_SIGNIFICAND_BITS (10)
57#define HALF_NUM_EXPONENT_BITS (5)
58#define HALF_NUM_SIGN_BITS (1)
59
60#define HALF_SIGNIFICAND_SHIFT (0)
61#define HALF_EXPONENT_SHIFT (HALF_NUM_SIGNIFICAND_BITS)
62#define HALF_SIGN_SHIFT (HALF_NUM_SIGNIFICAND_BITS + HALF_NUM_EXPONENT_BITS)
63
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070064#define HALF_SIGNIFICAND_MASK (0x3ffU) // The lower 10 bits
Laurence Lundblade06350ea2020-01-27 19:32:40 -080065#define HALF_EXPONENT_MASK (0x1fU << HALF_EXPONENT_SHIFT) // 0x7c00 5 bits of exponent
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070066#define HALF_SIGN_MASK (0x01U << HALF_SIGN_SHIFT) // 0x8000 1 bit of sign
Laurence Lundblade06350ea2020-01-27 19:32:40 -080067#define HALF_QUIET_NAN_BIT (0x01U << (HALF_NUM_SIGNIFICAND_BITS-1)) // 0x0200
Laurence Lundblade12d32c52018-09-19 11:25:27 -070068
69/* Biased Biased Unbiased Use
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070070 * 0x00 0 -15 0 and subnormal
71 * 0x01 1 -14 Smallest normal exponent
72 * 0x1e 30 15 Largest normal exponent
73 * 0x1F 31 16 NaN and Infinity */
Laurence Lundblade12d32c52018-09-19 11:25:27 -070074#define HALF_EXPONENT_BIAS (15)
75#define HALF_EXPONENT_MAX (HALF_EXPONENT_BIAS) // 15 Unbiased
76#define HALF_EXPONENT_MIN (-HALF_EXPONENT_BIAS+1) // -14 Unbiased
77#define HALF_EXPONENT_ZERO (-HALF_EXPONENT_BIAS) // -15 Unbiased
78#define HALF_EXPONENT_INF_OR_NAN (HALF_EXPONENT_BIAS+1) // 16 Unbiased
79
80
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070081/* ------ Single-Precision -------- */
Laurence Lundblade12d32c52018-09-19 11:25:27 -070082#define SINGLE_NUM_SIGNIFICAND_BITS (23)
83#define SINGLE_NUM_EXPONENT_BITS (8)
84#define SINGLE_NUM_SIGN_BITS (1)
85
86#define SINGLE_SIGNIFICAND_SHIFT (0)
87#define SINGLE_EXPONENT_SHIFT (SINGLE_NUM_SIGNIFICAND_BITS)
88#define SINGLE_SIGN_SHIFT (SINGLE_NUM_SIGNIFICAND_BITS + SINGLE_NUM_EXPONENT_BITS)
89
Laurence Lundblade06350ea2020-01-27 19:32:40 -080090#define SINGLE_SIGNIFICAND_MASK (0x7fffffU) // The lower 23 bits
91#define SINGLE_EXPONENT_MASK (0xffU << SINGLE_EXPONENT_SHIFT) // 8 bits of exponent
92#define SINGLE_SIGN_MASK (0x01U << SINGLE_SIGN_SHIFT) // 1 bit of sign
93#define SINGLE_QUIET_NAN_BIT (0x01U << (SINGLE_NUM_SIGNIFICAND_BITS-1))
Laurence Lundblade12d32c52018-09-19 11:25:27 -070094
95/* Biased Biased Unbiased Use
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -070096 * 0x0000 0 -127 0 and subnormal
97 * 0x0001 1 -126 Smallest normal exponent
98 * 0x7f 127 0 1
99 * 0xfe 254 127 Largest normal exponent
100 * 0xff 255 128 NaN and Infinity */
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700101#define SINGLE_EXPONENT_BIAS (127)
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700102#define SINGLE_EXPONENT_MAX (SINGLE_EXPONENT_BIAS)
103#define SINGLE_EXPONENT_MIN (-SINGLE_EXPONENT_BIAS+1)
104#define SINGLE_EXPONENT_ZERO (-SINGLE_EXPONENT_BIAS)
105#define SINGLE_EXPONENT_INF_OR_NAN (SINGLE_EXPONENT_BIAS+1)
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700106
107
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700108/* --------- Double-Precision ---------- */
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700109#define DOUBLE_NUM_SIGNIFICAND_BITS (52)
110#define DOUBLE_NUM_EXPONENT_BITS (11)
111#define DOUBLE_NUM_SIGN_BITS (1)
112
113#define DOUBLE_SIGNIFICAND_SHIFT (0)
114#define DOUBLE_EXPONENT_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS)
115#define DOUBLE_SIGN_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS + DOUBLE_NUM_EXPONENT_BITS)
116
Laurence Lundblade8db3d3e2018-09-29 11:46:37 -0700117#define DOUBLE_SIGNIFICAND_MASK (0xfffffffffffffULL) // The lower 52 bits
118#define DOUBLE_EXPONENT_MASK (0x7ffULL << DOUBLE_EXPONENT_SHIFT) // 11 bits of exponent
119#define DOUBLE_SIGN_MASK (0x01ULL << DOUBLE_SIGN_SHIFT) // 1 bit of sign
120#define DOUBLE_QUIET_NAN_BIT (0x01ULL << (DOUBLE_NUM_SIGNIFICAND_BITS-1))
121
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700122
123/* Biased Biased Unbiased Use
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700124 * 0x00000000 0 -1023 0 and subnormal
125 * 0x00000001 1 -1022 Smallest normal exponent
126 * 0x000007fe 2046 1023 Largest normal exponent
127 * 0x000007ff 2047 1024 NaN and Infinity */
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700128#define DOUBLE_EXPONENT_BIAS (1023)
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700129#define DOUBLE_EXPONENT_MAX (DOUBLE_EXPONENT_BIAS)
130#define DOUBLE_EXPONENT_MIN (-DOUBLE_EXPONENT_BIAS+1)
131#define DOUBLE_EXPONENT_ZERO (-DOUBLE_EXPONENT_BIAS)
132#define DOUBLE_EXPONENT_INF_OR_NAN (DOUBLE_EXPONENT_BIAS+1)
133
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700134
135
136
137/*
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700138 * Convenient functions to avoid type punning, compiler warnings and
139 * such. The optimizer reduces them to a simple assignment. This is a
140 * crusty corner of C. It shouldn't be this hard.
141 *
142 * These are also in UsefulBuf.h under a different name. They are copied
143 * here to avoid a dependency on UsefulBuf.h. There is no object code
144 * size impact because these always optimze down to a simple assignment.
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700145 */
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700146static inline uint32_t
147CopyFloatToUint32(float f)
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700148{
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700149 uint32_t u32;
150 memcpy(&u32, &f, sizeof(uint32_t));
151 return u32;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700152}
153
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700154static inline uint64_t
155CopyDoubleToUint64(double d)
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700156{
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700157 uint64_t u64;
158 memcpy(&u64, &d, sizeof(uint64_t));
159 return u64;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700160}
161
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800162
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800163#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
164
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700165static inline double
166CopyUint64ToDouble(uint64_t u64)
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700167{
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700168 double d;
169 memcpy(&d, &u64, sizeof(uint64_t));
170 return d;
171}
172
173static inline float
174CopyUint32ToSingle(uint32_t u32)
175{
176 float f;
177 memcpy(&f, &u32, sizeof(uint32_t));
178 return f;
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700179}
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700180
181
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
183
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700184/**
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700185 * @brief Assemble sign, significand and exponent into double precision float.
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700186 *
187 * @param[in] uDoubleSign 0 if positive, 1 if negative
188 * @pararm[in] uDoubleSignificand Bits of the significand
189 * @param[in] nDoubleUnBiasedExponent Exponent
190 *
191 * This returns the bits for a single-precision float, a binary64
192 * as specified in IEEE754.
Laurence Lundbladefe09bbf2020-07-16 12:14:51 -0700193 */
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700194// TODO: make the sign and exponent type int?
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700195static double
196IEEE754_AssembleDouble(uint64_t uDoubleSign,
197 uint64_t uDoubleSignificand,
198 int64_t nDoubleUnBiasedExponent)
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700199{
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700200 uint64_t uDoubleBiasedExponent;
201
202 uDoubleBiasedExponent = (uint64_t)(nDoubleUnBiasedExponent + DOUBLE_EXPONENT_BIAS);
203
204 return CopyUint64ToDouble(uDoubleSignificand |
205 (uDoubleBiasedExponent << DOUBLE_EXPONENT_SHIFT) |
206 (uDoubleSign << DOUBLE_SIGN_SHIFT));
207}
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
209
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700210/* Public function; see ieee754.h */
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700211double
212IEEE754_HalfToDouble(uint16_t uHalfPrecision)
213{
214 uint64_t uDoubleSignificand;
215 int64_t nDoubleUnBiasedExponent;
216 double dResult;
217
218 /* Pull out the three parts of the half-precision float. Do all
219 * the work in 64 bits because that is what the end result is. It
220 * may give smaller code size and will keep static analyzers
221 * happier.
222 */
223 const uint64_t uHalfSignificand = uHalfPrecision & HALF_SIGNIFICAND_MASK;
224 const uint64_t uHalfBiasedExponent = (uHalfPrecision & HALF_EXPONENT_MASK) >> HALF_EXPONENT_SHIFT;
225 const int64_t nHalfUnBiasedExponent = (int64_t)uHalfBiasedExponent - HALF_EXPONENT_BIAS;
226 const uint64_t uHalfSign = (uHalfPrecision & HALF_SIGN_MASK) >> HALF_SIGN_SHIFT;
227
228 if(nHalfUnBiasedExponent == HALF_EXPONENT_ZERO) {
229 /* 0 or subnormal */
230 if(uHalfSignificand) {
231 /* --- SUBNORMAL --- */
232 /* A half-precision subnormal can always be converted to a
233 * normal double-precision float because the ranges line up.
234 * The exponent of a subnormal starts out at the min exponent
235 * for a normal. As the sub normal significand bits are
236 * shifted, left to normalize, the exponent is
237 * decremented. Shifting continues until fully normalized.
238 */
239 nDoubleUnBiasedExponent = HALF_EXPONENT_MIN;
240 uDoubleSignificand = uHalfSignificand;
241 do {
242 uDoubleSignificand <<= 1;
243 nDoubleUnBiasedExponent--;
244 } while ((uDoubleSignificand & (1ULL << HALF_NUM_SIGNIFICAND_BITS)) == 0);
245 /* A normal has an implied 1 in the most significant
246 * position that a subnormal doesn't. */
247 uDoubleSignificand -= 1ULL << HALF_NUM_SIGNIFICAND_BITS;
248 /* Must shift into place for a double significand */
249 uDoubleSignificand <<= DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS;
250
251 dResult = IEEE754_AssembleDouble(uHalfSign,
252 uDoubleSignificand,
253 nDoubleUnBiasedExponent);
254 } else {
255 /* --- ZERO --- */
256 dResult = IEEE754_AssembleDouble(uHalfSign,
257 0,
258 DOUBLE_EXPONENT_ZERO);
259 }
260 } else if(nHalfUnBiasedExponent == HALF_EXPONENT_INF_OR_NAN) {
261 /* NaN or Inifinity */
262 if(uHalfSignificand) {
263 /* --- NaN --- */
264 /* Half-precision payloads always fit into double precision
265 * payloads. They are shifted left the same as a normal
266 * number significand.
267 */
268 uDoubleSignificand = uHalfSignificand << (DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS);
269 dResult = IEEE754_AssembleDouble(uHalfSign,
270 uDoubleSignificand,
271 DOUBLE_EXPONENT_INF_OR_NAN);
272 } else {
273 /* --- INFINITY --- */
274 dResult = IEEE754_AssembleDouble(uHalfSign,
275 0,
276 DOUBLE_EXPONENT_INF_OR_NAN);
277 }
278 } else {
279 /* --- NORMAL NUMBER --- */
280 uDoubleSignificand = uHalfSignificand << (DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS);
281 dResult = IEEE754_AssembleDouble(uHalfSign,
282 uDoubleSignificand,
283 nHalfUnBiasedExponent);
284 }
285
286 return dResult;
287}
288
289
290/**
291 * @brief Assemble sign, significand and exponent into single precision float.
292 *
293 * @param[in] uHalfSign 0 if positive, 1 if negative
294 * @pararm[in] uHalfSignificand Bits of the significand
295 * @param[in] nHalfUnBiasedExponent Exponent
296 *
297 * This returns the bits for a single-precision float, a binary32 as
298 * specified in IEEE754. It is returned as a uint64_t rather than a
299 * uint32_t or a float for convenience of usage.
300 */
301static uint32_t
302IEEE754_AssembleHalf(uint32_t uHalfSign,
303 uint32_t uHalfSignificand,
304 int32_t nHalfUnBiasedExponent)
305{
306 uint32_t uHalfUnbiasedExponent;
307
308 uHalfUnbiasedExponent = (uint32_t)(nHalfUnBiasedExponent + HALF_EXPONENT_BIAS);
309
310 return uHalfSignificand |
311 (uHalfUnbiasedExponent << HALF_EXPONENT_SHIFT) |
312 (uHalfSign << HALF_SIGN_SHIFT);
313}
314
315
316/* Public function; see ieee754.h */
317IEEE754_union
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700318IEEE754_SingleToHalf(const float f, const int bNoNaNPayload)
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700319{
320 IEEE754_union result;
321 uint32_t uDroppedBits;
322 int32_t nExponentDifference;
323 int32_t nShiftAmount;
324 uint32_t uHalfSignificand;
325
326 /* Pull the three parts out of the double-precision float Most work
327 * is done with uint32_t which helps avoid integer promotions and
328 * static analyzer complaints.
329 */
330 const uint32_t uSingle = CopyFloatToUint32(f);
331 const uint32_t uSingleBiasedExponent = (uSingle & SINGLE_EXPONENT_MASK) >> SINGLE_EXPONENT_SHIFT;
332 const int32_t nSingleUnbiasedExponent = (int32_t)uSingleBiasedExponent - SINGLE_EXPONENT_BIAS;
333 const uint32_t uSingleSignificand = uSingle & SINGLE_SIGNIFICAND_MASK;
334 const uint32_t uSingleSign = (uSingle & SINGLE_SIGN_MASK) >> SINGLE_SIGN_SHIFT;
335
336 if(nSingleUnbiasedExponent == SINGLE_EXPONENT_ZERO) {
337 if(uSingleSignificand == 0) {
338 /* --- IS ZERO --- */
339 result.uSize = IEEE754_UNION_IS_HALF;
340 result.uValue = IEEE754_AssembleHalf(uSingleSign,
341 0,
342 HALF_EXPONENT_ZERO);
343 } else {
344 /* --- IS SINGLE SUBNORMAL --- */
345 /* The largest single subnormal is slightly less than the
346 * largest single normal which is 2^-149 or
347 * 2.2040517676619426e-38. The smallest half subnormal is
348 * 2^-14 or 5.9604644775390625E-8. There is no overlap so
349 * single subnormals can't be converted to halfs of any sort.
350 */
351 result.uSize = IEEE754_UNION_IS_SINGLE;
352 result.uValue = uSingle;
353 }
354 } else if(nSingleUnbiasedExponent == SINGLE_EXPONENT_INF_OR_NAN) {
355 if(uSingleSignificand == 0) {
356 /* ---- IS INFINITY ---- */
357 result.uSize = IEEE754_UNION_IS_HALF;
358 result.uValue = IEEE754_AssembleHalf(uSingleSign, 0, HALF_EXPONENT_INF_OR_NAN);
359 } else {
Laurence Lundblade240ca822024-01-16 11:11:00 -0700360 if(bNoNaNPayload) {
361 /* --- REQUIRE CANNONICAL NAN --- */
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700362 result.uSize = IEEE754_UNION_IS_HALF;
363 result.uValue = IEEE754_AssembleHalf(uSingleSign,
Laurence Lundblade240ca822024-01-16 11:11:00 -0700364 HALF_QUIET_NAN_BIT,
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700365 HALF_EXPONENT_INF_OR_NAN);
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700366 } else {
Laurence Lundblade240ca822024-01-16 11:11:00 -0700367 /* The NaN can only be converted if no payload bits are lost
368 * per RFC 8949 section 4.1 that defines Preferred
369 * Serializaton. Note that Deterministically Encode CBOR in
370 * section 4.2 allows for some variation of this rule, but at
371 * the moment this implementation is of Preferred
372 * Serialization, not CDE. As of December 2023, we are also
373 * expecting an update to CDE. This code may need to be
374 * updated for CDE.
375 */
376 uDroppedBits = uSingleSignificand & (SINGLE_SIGNIFICAND_MASK >> HALF_NUM_SIGNIFICAND_BITS);
377 if(uDroppedBits == 0) {
378 /* --- IS CONVERTABLE NAN --- */
379 uHalfSignificand = uSingleSignificand >> (SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS);
380 result.uSize = IEEE754_UNION_IS_HALF;
381 result.uValue = IEEE754_AssembleHalf(uSingleSign,
382 uHalfSignificand,
383 HALF_EXPONENT_INF_OR_NAN);
384
385 } else {
386 /* --- IS UNCONVERTABLE NAN --- */
387 result.uSize = IEEE754_UNION_IS_SINGLE;
388 result.uValue = uSingle;
389 }
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700390 }
391 }
392 } else {
393 /* ---- REGULAR NUMBER ---- */
394 /* A regular single can be converted to a regular half if the
395 * single's exponent is in the smaller range of a half and if no
396 * precision is lost in the significand.
397 */
398 if(nSingleUnbiasedExponent >= HALF_EXPONENT_MIN &&
399 nSingleUnbiasedExponent <= HALF_EXPONENT_MAX &&
400 (uSingleSignificand & (SINGLE_SIGNIFICAND_MASK >> HALF_NUM_SIGNIFICAND_BITS)) == 0) {
401 uHalfSignificand = uSingleSignificand >> (SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS);
402
403 /* --- CONVERT TO HALF NORMAL --- */
404 result.uSize = IEEE754_UNION_IS_HALF;
405 result.uValue = IEEE754_AssembleHalf(uSingleSign,
406 uHalfSignificand,
407 nSingleUnbiasedExponent);
408 } else {
409 /* Unable to convert to a half normal. See if it can be
410 * converted to a half subnormal. To do that, the exponent
411 * must be in range and no precision can be lost in the
412 * signficand.
413 *
414 * This is more complicated because the number is not
415 * normalized. The signficand must be shifted proprotionally
416 * to the exponent and 1 must be added in. See
417 * https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Exponent_encoding
418 *
419 * Exponents -14 to -24 map to a shift of 0 to 10 of the
420 * significand. The largest value of a half subnormal has an
421 * exponent of -14. Subnormals are not normalized like
422 * normals meaning they lose precision as the numbers get
423 * smaller. Normals don't lose precision because the exponent
424 * allows all the bits of the significand to be significant.
425 */
426 /* The exponent of the largest possible half-precision
427 * subnormal is HALF_EXPONENT_MIN (-14). Exponents larger
428 * than this are normal and handled above. We're going to
429 * shift the significand right by at least this amount.
430 */
431 nExponentDifference = -(nSingleUnbiasedExponent - HALF_EXPONENT_MIN);
432
433 /* In addition to the shift based on the exponent's value,
434 * the single significand has to be shifted right to fit into
435 * a half-precision significand */
436 nShiftAmount = nExponentDifference + (SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS);
437
438 /* Must add 1 in to the possible significand because there is
439 * an implied 1 for normal values and not for subnormal
440 * values. See equations here:
441 * https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Exponent_encoding
442 */
443 uHalfSignificand = (uSingleSignificand + (1 << SINGLE_NUM_SIGNIFICAND_BITS)) >> nShiftAmount;
444
445 /* If only zero bits get shifted out, this can be converted
446 * to subnormal */
447 if(nSingleUnbiasedExponent < HALF_EXPONENT_MIN &&
448 nSingleUnbiasedExponent >= HALF_EXPONENT_MIN - HALF_NUM_SIGNIFICAND_BITS &&
449 uHalfSignificand << nShiftAmount == uSingleSignificand + (1 << SINGLE_NUM_SIGNIFICAND_BITS)) {
450 /* --- CONVERTABLE TO HALF SUBNORMAL --- */
451 result.uSize = IEEE754_UNION_IS_HALF;
452 result.uValue = IEEE754_AssembleHalf(uSingleSign,
453 uHalfSignificand,
454 HALF_EXPONENT_ZERO);
455 } else {
456 /* --- DO NOT CONVERT --- */
457 result.uSize = IEEE754_UNION_IS_SINGLE;
458 result.uValue = uSingle;
459 }
460 }
461 }
462
463 return result;
464}
465
466
467/**
468 * @brief Assemble sign, significand and exponent into single precision float.
469 *
470 * @param[in] uSingleSign 0 if positive, 1 if negative
471 * @pararm[in] uSingleSignificand Bits of the significand
472 * @param[in] nSingleUnBiasedExponent Exponent
473 *
474 * This returns the bits for a single-precision float, a binary32 as
475 * specified in IEEE754. It is returned as a uint64_t rather than a
476 * uint32_t or a float for convenience of usage.
477 */
478static uint64_t
479IEEE754_AssembleSingle(uint64_t uSingleSign,
480 uint64_t uSingleSignificand,
481 int64_t nSingleUnBiasedExponent)
482{
483 uint64_t uSingleBiasedExponent;
484
485 uSingleBiasedExponent = (uint64_t)(nSingleUnBiasedExponent + SINGLE_EXPONENT_BIAS);
486
487 return uSingleSignificand |
488 (uSingleBiasedExponent << SINGLE_EXPONENT_SHIFT) |
489 (uSingleSign << SINGLE_SIGN_SHIFT);
490}
491
492
493/**
494 * @brief Convert a double-precision float to single-precision.
495 *
496 * @param[in] d The value to convert.
497 *
498 * @returns Either unconverted value or value converted to single-precision.
499 *
500 * This always succeeds. If the value cannot be converted without the
501 * loss of precision, it is not converted.
502 *
503 * This handles all subnormals and NaN payloads.
504 */
505static IEEE754_union
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700506IEEE754_DoubleToSingle(const double d)
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700507{
508 IEEE754_union Result;
509 int64_t nExponentDifference;
510 int64_t nShiftAmount;
511 uint64_t uSingleSignificand;
512 uint64_t uDroppedBits;
513
514
515 /* Pull the three parts out of the double-precision float. Most
516 * work is done with uint64_t which helps avoid integer promotions
517 * and static analyzer complaints.
518 */
519 const uint64_t uDouble = CopyDoubleToUint64(d);
520 const uint64_t uDoubleBiasedExponent = (uDouble & DOUBLE_EXPONENT_MASK) >> DOUBLE_EXPONENT_SHIFT;
521 const int64_t nDoubleUnbiasedExponent = (int64_t)uDoubleBiasedExponent - DOUBLE_EXPONENT_BIAS;
522 const uint64_t uDoubleSign = (uDouble & DOUBLE_SIGN_MASK) >> DOUBLE_SIGN_SHIFT;
523 const uint64_t uDoubleSignificand = uDouble & DOUBLE_SIGNIFICAND_MASK;
524
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700525 if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_ZERO) {
526 if(uDoubleSignificand == 0) {
527 /* --- IS ZERO --- */
528 Result.uSize = IEEE754_UNION_IS_SINGLE;
529 Result.uValue = IEEE754_AssembleSingle(uDoubleSign,
530 0,
531 SINGLE_EXPONENT_ZERO);
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700532 } else {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700533 /* --- IS DOUBLE SUBNORMAL --- */
534 /* The largest double subnormal is slightly less than the
535 * largest double normal which is 2^-1022 or
536 * 2.2250738585072014e-308. The smallest single subnormal
537 * is 2^-149 or 1.401298464324817e-45. There is no
538 * overlap so double subnormals can't be converted to
539 * singles of any sort.
540 */
541 Result.uSize = IEEE754_UNION_IS_DOUBLE;
542 Result.uValue = uDouble;
543 }
544 } else if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_INF_OR_NAN) {
545 if(uDoubleSignificand == 0) {
546 /* ---- IS INFINITY ---- */
547 Result.uSize = IEEE754_UNION_IS_SINGLE;
548 Result.uValue = IEEE754_AssembleSingle(uDoubleSign,
549 0,
550 SINGLE_EXPONENT_INF_OR_NAN);
551 } else {
552 /* The NaN can only be converted if no payload bits are
553 * lost per RFC 8949 section 4.1 that defines Preferred
554 * Serializaton. Note that Deterministically Encode CBOR
555 * in section 4.2 allows for some variation of this rule,
556 * but at the moment this implementation is of Preferred
557 * Serialization, not CDE. As of December 2023, we are
558 * also expecting an update to CDE. This code may need to
559 * be updated for CDE.
560 */
561 uDroppedBits = uDoubleSignificand & (DOUBLE_SIGNIFICAND_MASK >> SINGLE_NUM_SIGNIFICAND_BITS);
562 if(uDroppedBits == 0) {
563 /* --- IS CONVERTABLE NAN --- */
564 uSingleSignificand = uDoubleSignificand >> (DOUBLE_NUM_SIGNIFICAND_BITS - SINGLE_NUM_SIGNIFICAND_BITS);
565 Result.uSize = IEEE754_UNION_IS_SINGLE;
566 Result.uValue = IEEE754_AssembleSingle(uDoubleSign,
567 uSingleSignificand,
568 SINGLE_EXPONENT_INF_OR_NAN);
569 } else {
570 /* --- IS UNCONVERTABLE NAN --- */
571 Result.uSize = IEEE754_UNION_IS_DOUBLE;
572 Result.uValue = uDouble;
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700573 }
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700574 }
575 } else {
576 /* ---- REGULAR NUMBER ---- */
577 /* A regular double can be converted to a regular single if
578 * the double's exponent is in the smaller range of a single
579 * and if no precision is lost in the significand.
580 */
581 uDroppedBits = uDoubleSignificand & (DOUBLE_SIGNIFICAND_MASK >> SINGLE_NUM_SIGNIFICAND_BITS);
582 if(nDoubleUnbiasedExponent >= SINGLE_EXPONENT_MIN &&
583 nDoubleUnbiasedExponent <= SINGLE_EXPONENT_MAX &&
584 uDroppedBits == 0) {
585 /* --- IS CONVERTABLE TO SINGLE --- */
586 uSingleSignificand = uDoubleSignificand >> (DOUBLE_NUM_SIGNIFICAND_BITS - SINGLE_NUM_SIGNIFICAND_BITS);
587 Result.uSize = IEEE754_UNION_IS_SINGLE;
588 Result.uValue = IEEE754_AssembleSingle(uDoubleSign,
589 uSingleSignificand,
590 nDoubleUnbiasedExponent);
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700591 } else {
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700592 /* Unable to convert to a single normal. See if it can be
593 * converted to a single subnormal. To do that, the
594 * exponent must be in range and no precision can be lost
595 * in the signficand.
596 *
597 * This is more complicated because the number is not
598 * normalized. The signficand must be shifted
599 * proprotionally to the exponent and 1 must be added
600 * in. See
601 * https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Exponent_encoding
602 */
603 nExponentDifference = -(nDoubleUnbiasedExponent - SINGLE_EXPONENT_MIN);
604 nShiftAmount = nExponentDifference + (DOUBLE_NUM_SIGNIFICAND_BITS - SINGLE_NUM_SIGNIFICAND_BITS);
605 uSingleSignificand = (uDoubleSignificand + (1ULL << DOUBLE_NUM_SIGNIFICAND_BITS)) >> nShiftAmount;
606
607 if(nDoubleUnbiasedExponent < SINGLE_EXPONENT_MIN &&
608 nDoubleUnbiasedExponent >= SINGLE_EXPONENT_MIN - SINGLE_NUM_SIGNIFICAND_BITS &&
609 uSingleSignificand << nShiftAmount == uDoubleSignificand + (1ULL << DOUBLE_NUM_SIGNIFICAND_BITS)) {
610 /* --- IS CONVERTABLE TO SINGLE SUBNORMAL --- */
611 Result.uSize = IEEE754_UNION_IS_SINGLE;
612 Result.uValue = IEEE754_AssembleSingle(uDoubleSign,
613 uSingleSignificand,
614 SINGLE_EXPONENT_ZERO);
615 } else {
616 /* --- CAN NOT BE CONVERTED --- */
617 Result.uSize = IEEE754_UNION_IS_DOUBLE;
618 Result.uValue = uDouble;
619 }
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700620 }
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700621 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800622
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700623 return Result;
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700624}
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700625
626
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700627/* Public function; see ieee754.h */
628IEEE754_union
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700629IEEE754_DoubleToSmaller(const double d,
630 const int bAllowHalfPrecision,
631 const int bNoNanPayload)
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700632{
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700633 IEEE754_union result;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800634
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700635 result = IEEE754_DoubleToSingle(d);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800636
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700637 if(result.uSize == IEEE754_UNION_IS_SINGLE && bAllowHalfPrecision) {
638 /* Cast to uint32_t is OK, because value was just successfully
639 * converted to single. */
640 float uSingle = CopyUint32ToSingle((uint32_t)result.uValue);
Laurence Lundblade240ca822024-01-16 11:11:00 -0700641 result = IEEE754_SingleToHalf(uSingle, bNoNanPayload);
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700642 }
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700643
Laurence Lundblade83dbf5c2024-01-07 19:17:52 -0700644 return result;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700645}
646
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800647
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700648/* This returns 64 minus the number of zero bits on the right. It is
649 * is the amount of precision in the 64-bit significand passed in.
650 * When used for 52 and 23-bit significands, subtract 12 and 41
651 * to get their precision.
652 *
653 * The value returned is for a *normalized* number like the
654 * significand of a double. When used for precision for a non-normalized
655 * number like a uint64_t, further computation is required.
656 *
657 * If the significand is 0, then 0 is returned as the precision.*/
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700658static int
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700659IEEE754_Private_CountPrecisionBits(uint64_t uSignigicand)
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700660{
661 int nNonZeroBitsCount;
662 uint64_t uMask;
663
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700664 for(nNonZeroBitsCount = 64; nNonZeroBitsCount > 0; nNonZeroBitsCount--) {
665 uMask = 0x01UL << (64 - nNonZeroBitsCount);
666 if(uMask & uSignigicand) {
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700667 break;
668 }
669 }
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700670
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700671 return nNonZeroBitsCount;
672}
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700673
674
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700675
Laurence Lundblade240ca822024-01-16 11:11:00 -0700676/* Public function; see ieee754.h */
677struct IEEE754_ToInt
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700678IEEE754_DoubleToInt(const double d)
Laurence Lundblade240ca822024-01-16 11:11:00 -0700679{
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700680 int64_t nPrecisionBits;
Laurence Lundblade240ca822024-01-16 11:11:00 -0700681 struct IEEE754_ToInt Result;
682 uint64_t uInteger;
683
684 /* Pull the three parts out of the double-precision float. Most
685 * work is done with uint64_t which helps avoid integer promotions
686 * and static analyzer complaints.
687 */
688 const uint64_t uDouble = CopyDoubleToUint64(d);
689 const uint64_t uDoubleBiasedExponent = (uDouble & DOUBLE_EXPONENT_MASK) >> DOUBLE_EXPONENT_SHIFT;
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700690 /* Cast safe because of mask above; exponents < DOUBLE_EXPONENT_MAX */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700691 const int64_t nDoubleUnbiasedExponent = (int64_t)uDoubleBiasedExponent - DOUBLE_EXPONENT_BIAS;
692 const uint64_t uDoubleSignificand = uDouble & DOUBLE_SIGNIFICAND_MASK;
693
Laurence Lundblade240ca822024-01-16 11:11:00 -0700694 if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_ZERO) {
695 if(uDoubleSignificand == 0) {
696 /* --- POSITIVE AND NEGATIVE ZERO --- */
697 Result.integer.un_signed = 0;
698 Result.type = IEEE754_ToInt_IS_UINT;
699 } else {
700 /* --- SUBNORMAL --- */
701 Result.type = IEEE754_ToInt_NO_CONVERSION;
702 }
703 } else if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_INF_OR_NAN) {
704 if(uDoubleSignificand != 0) {
Laurence Lundbladee75f5962024-02-15 17:51:32 -0800705 /* --- NAN --- */
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800706 Result.type = IEEE754_ToInt_NaN; /* dCBOR doesn't care about payload */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700707 } else {
Laurence Lundbladee75f5962024-02-15 17:51:32 -0800708 /* --- INIFINITY --- */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700709 Result.type = IEEE754_ToInt_NO_CONVERSION;
710 }
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700711 } else if(nDoubleUnbiasedExponent < 0 ||
712 (nDoubleUnbiasedExponent >= ((uDouble & DOUBLE_SIGN_MASK) ? 63 : 64))) {
Laurence Lundblade240ca822024-01-16 11:11:00 -0700713 /* --- Exponent out of range --- */
714 Result.type = IEEE754_ToInt_NO_CONVERSION;
715 } else {
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700716 /* Conversion only fails when the input is too large or is not a
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700717 * whole number, never because of lack of precision because
Laurence Lundbladebf8e1cf2024-01-17 11:58:24 -0700718 * 64-bit integers always have more precision than the 52-bits
719 * of a double.
Laurence Lundblade240ca822024-01-16 11:11:00 -0700720 */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700721
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700722 nPrecisionBits = IEEE754_Private_CountPrecisionBits(uDoubleSignificand) -
723 (64-DOUBLE_NUM_SIGNIFICAND_BITS);
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800724
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700725 if(nPrecisionBits && nPrecisionBits > nDoubleUnbiasedExponent) {
Laurence Lundblade240ca822024-01-16 11:11:00 -0700726 /* --- Not a whole number --- */
727 Result.type = IEEE754_ToInt_NO_CONVERSION;
728 } else {
729 /* --- CONVERTABLE WHOLE NUMBER --- */
730 /* Add in the one that is implied in normal floats */
731 uInteger = uDoubleSignificand + (1ULL << DOUBLE_NUM_SIGNIFICAND_BITS);
732 /* Factor in the exponent */
733 if(nDoubleUnbiasedExponent < DOUBLE_NUM_SIGNIFICAND_BITS) {
734 /* Numbers less than 2^52 with up to 52 significant bits */
735 uInteger >>= DOUBLE_NUM_SIGNIFICAND_BITS - nDoubleUnbiasedExponent;
736 } else {
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700737 /* Numbers greater than 2^52 with at most 52 significant bits */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700738 uInteger <<= nDoubleUnbiasedExponent - DOUBLE_NUM_SIGNIFICAND_BITS;
739 }
740 if(uDouble & DOUBLE_SIGN_MASK) {
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700741 /* Cast safe because exponent range check above */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700742 Result.integer.is_signed = -((int64_t)uInteger);
743 Result.type = IEEE754_ToInt_IS_INT;
744 } else {
745 Result.integer.un_signed = uInteger;
746 Result.type = IEEE754_ToInt_IS_UINT;
747 }
748 }
749 }
750
751 return Result;
752}
753
754
755/* Public function; see ieee754.h */
756struct IEEE754_ToInt
Laurence Lundbladeff8106f2024-02-05 20:02:30 -0700757IEEE754_SingleToInt(const float f)
Laurence Lundblade240ca822024-01-16 11:11:00 -0700758{
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700759 int32_t nPrecisionBits;
Laurence Lundblade240ca822024-01-16 11:11:00 -0700760 struct IEEE754_ToInt Result;
761 uint64_t uInteger;
762
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700763 /* Pull the three parts out of the single-precision float. Most
Laurence Lundblade240ca822024-01-16 11:11:00 -0700764 * work is done with uint32_t which helps avoid integer promotions
765 * and static analyzer complaints.
766 */
767 const uint32_t uSingle = CopyFloatToUint32(f);
768 const uint32_t uSingleBiasedExponent = (uSingle & SINGLE_EXPONENT_MASK) >> SINGLE_EXPONENT_SHIFT;
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700769 /* Cast safe because of mask above; exponents < SINGLE_EXPONENT_MAX */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700770 const int32_t nSingleUnbiasedExponent = (int32_t)uSingleBiasedExponent - SINGLE_EXPONENT_BIAS;
771 const uint32_t uSingleleSignificand = uSingle & SINGLE_SIGNIFICAND_MASK;
772
773 if(nSingleUnbiasedExponent == SINGLE_EXPONENT_ZERO) {
774 if(uSingleleSignificand == 0 && !(uSingle & SINGLE_SIGN_MASK)) {
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700775 /* --- POSITIVE AND NEGATIVE ZERO --- */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700776 Result.integer.un_signed = 0;
777 Result.type = IEEE754_ToInt_IS_UINT;
778 } else {
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700779 /* --- Subnormal --- */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700780 Result.type = IEEE754_ToInt_NO_CONVERSION;
781 }
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700782 } else if(nSingleUnbiasedExponent == SINGLE_EXPONENT_INF_OR_NAN) {
783 /* --- NAN or INFINITY --- */
784 if(uSingleleSignificand != 0) {
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800785 Result.type = IEEE754_ToInt_NaN; /* dCBOR doesn't care about payload */
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700786 } else {
787 Result.type = IEEE754_ToInt_NO_CONVERSION;
788 }
789 } else if(nSingleUnbiasedExponent < 0 ||
790 (nSingleUnbiasedExponent >= ((uSingle & SINGLE_SIGN_MASK) ? 63 : 64))) {
791 /* --- Exponent out of range --- */
Laurence Lundblade240ca822024-01-16 11:11:00 -0700792 Result.type = IEEE754_ToInt_NO_CONVERSION;
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700793 } else {
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700794 /* Conversion only fails when the input is too large or is not a
Laurence Lundbladee026f4f2024-01-18 13:48:34 -0700795 * whole number, never because of lack of precision because
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700796 * 64-bit integers always have more precision than the 23 bits
797 * of a single.
Laurence Lundblade240ca822024-01-16 11:11:00 -0700798 */
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700799 nPrecisionBits = IEEE754_Private_CountPrecisionBits(uSingleleSignificand) -
800 (64 - SINGLE_NUM_SIGNIFICAND_BITS);
Laurence Lundblade240ca822024-01-16 11:11:00 -0700801
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700802 if(nPrecisionBits && nPrecisionBits > nSingleUnbiasedExponent) {
Laurence Lundblade240ca822024-01-16 11:11:00 -0700803 /* --- Not a whole number --- */
804 Result.type = IEEE754_ToInt_NO_CONVERSION;
805 } else {
806 /* --- CONVERTABLE WHOLE NUMBER --- */
807 /* Add in the one that is implied in normal floats */
808 uInteger = uSingleleSignificand + (1ULL << SINGLE_NUM_SIGNIFICAND_BITS);
Laurence Lundbladee75f5962024-02-15 17:51:32 -0800809 /* Factor in the exponent */
Laurence Lundbladebf8e1cf2024-01-17 11:58:24 -0700810 if(nSingleUnbiasedExponent < SINGLE_NUM_SIGNIFICAND_BITS) {
811 /* Numbers less than 2^23 with up to 23 significant bits */
812 uInteger >>= SINGLE_NUM_SIGNIFICAND_BITS - nSingleUnbiasedExponent;
813 } else {
814 /* Numbers greater than 2^23 with at most 23 significant bits*/
815 uInteger <<= nSingleUnbiasedExponent - SINGLE_NUM_SIGNIFICAND_BITS;
816 }
Laurence Lundblade240ca822024-01-16 11:11:00 -0700817 if(uSingle & SINGLE_SIGN_MASK) {
818 Result.integer.is_signed = -((int64_t)uInteger);
819 Result.type = IEEE754_ToInt_IS_INT;
820 } else {
821 Result.integer.un_signed = uInteger;
822 Result.type = IEEE754_ToInt_IS_UINT;
823 }
824 }
825 }
826
827 return Result;
828}
829
Laurence Lundbladed883ad32024-03-23 22:37:37 -0700830
831
832/* Public function; see ieee754.h */
833double
834IEEE754_UintToDouble(const uint64_t uInt, const int uIsNegative)
835{
836 int nDoubleUnbiasedExponent;
837 uint64_t uDoubleSignificand;
838 int nPrecisionBits;
839
840 /* Figure out the exponent and normalize the significand. This is
841 * done by shifting out all leading zero bits and counting them. If
842 * none are shifted out, the exponent is 63. */
843 uDoubleSignificand = uInt;
844 nDoubleUnbiasedExponent = 63;
845 while(1) {
846 if(uDoubleSignificand & 0x8000000000000000UL) {
847 break;
848 }
849 uDoubleSignificand <<= 1;
850 nDoubleUnbiasedExponent--;
851 };
852
853 /* Position significand correctly for a double. Only shift 63 bits
854 * because of the 1 that is present by implication in IEEE 754.*/
855 uDoubleSignificand >>= 63 - DOUBLE_NUM_SIGNIFICAND_BITS;
856
857 /* Subtract 1 which is present by implication in IEEE 754 */
858 uDoubleSignificand -= 1ULL << (DOUBLE_NUM_SIGNIFICAND_BITS);
859
860 nPrecisionBits = IEEE754_Private_CountPrecisionBits(uInt) - (64 - nDoubleUnbiasedExponent);
861
862 if(nPrecisionBits > DOUBLE_NUM_SIGNIFICAND_BITS) {
863 /* Will lose precision if converted */
864 return IEEE754_UINT_TO_DOUBLE_OOB;
865 }
866
867 return IEEE754_AssembleDouble((uint64_t)uIsNegative,
868 uDoubleSignificand,
869 nDoubleUnbiasedExponent);
870}
871
Laurence Lundblade6bef4a62024-02-16 14:59:23 -0800872#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
873
874
Laurence Lundblade240ca822024-01-16 11:11:00 -0700875
Laurence Lundblade4aa8aad2024-02-15 17:47:41 -0800876/* Public function; see ieee754.h */
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800877int
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800878IEEE754_IsNotStandardDoubleNaN(const double d)
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800879{
880 const uint64_t uDouble = CopyDoubleToUint64(d);
881 const uint64_t uDoubleBiasedExponent = (uDouble & DOUBLE_EXPONENT_MASK) >> DOUBLE_EXPONENT_SHIFT;
882 /* Cast safe because of mask above; exponents < DOUBLE_EXPONENT_MAX */
883 const int64_t nDoubleUnbiasedExponent = (int64_t)uDoubleBiasedExponent - DOUBLE_EXPONENT_BIAS;
884 const uint64_t uDoubleSignificand = uDouble & DOUBLE_SIGNIFICAND_MASK;
885
886 if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_INF_OR_NAN &&
887 uDoubleSignificand != 0 &&
888 uDoubleSignificand != DOUBLE_QUIET_NAN_BIT) {
889 return 1;
890 } else {
891 return 0;
892 }
893}
894
895
Laurence Lundblade4aa8aad2024-02-15 17:47:41 -0800896/* Public function; see ieee754.h */
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800897int
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800898IEEE754_IsNotStandardSingleNaN(const float f)
Laurence Lundblade8e5f42d2024-02-11 14:25:32 -0800899{
900 const uint32_t uSingle = CopyFloatToUint32(f);
901 const uint32_t uSingleBiasedExponent = (uSingle & SINGLE_EXPONENT_MASK) >> SINGLE_EXPONENT_SHIFT;
902 /* Cast safe because of mask above; exponents < SINGLE_EXPONENT_MAX */
903 const int32_t nSingleUnbiasedExponent = (int32_t)uSingleBiasedExponent - SINGLE_EXPONENT_BIAS;
904 const uint32_t uSingleleSignificand = uSingle & SINGLE_SIGNIFICAND_MASK;
905
906 if(nSingleUnbiasedExponent == SINGLE_EXPONENT_INF_OR_NAN &&
907 uSingleleSignificand != 0 &&
908 uSingleleSignificand != SINGLE_QUIET_NAN_BIT) {
909 return 1;
910 } else {
911 return 0;
912 }
913}
914