Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2 | ieee754.c -- floating-point conversion between half, double & single-precision |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -0800 | [diff] [blame] | 3 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 4 | Copyright (c) 2018-2020, Laurence Lundblade. All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | a3fd49f | 2019-01-21 10:16:22 -0800 | [diff] [blame] | 6 | SPDX-License-Identifier: BSD-3-Clause |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -0800 | [diff] [blame] | 7 | |
Laurence Lundblade | a3fd49f | 2019-01-21 10:16:22 -0800 | [diff] [blame] | 8 | See BSD-3-Clause license in README.md |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -0800 | [diff] [blame] | 9 | |
Laurence Lundblade | a3fd49f | 2019-01-21 10:16:22 -0800 | [diff] [blame] | 10 | Created on 7/23/18 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 11 | =============================================================================*/ |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 12 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 13 | #ifndef QCBOR_CONFIG_DISABLE_ENCODE_IEEE754 |
| 14 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 15 | #include "ieee754.h" |
| 16 | #include <string.h> // For memcpy() |
| 17 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 18 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 19 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 20 | This code is written for clarity and verifiability, not for size, on |
| 21 | the assumption that the optimizer will do a good job. The LLVM |
| 22 | optimizer, -Os, does seem to do the job and the resulting object code |
| 23 | is smaller from combining code for the many different cases (normal, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 24 | subnormal, infinity, zero...) for the conversions. GCC is no where near |
| 25 | as good. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 26 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 27 | This code has really long lines and is much easier to read because of |
| 28 | them. Some coding guidelines prefer 80 column lines (can they not afford |
| 29 | big displays?). It would make this code much worse even to wrap at 120 |
| 30 | columns. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 31 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | Dead stripping is also really helpful to get code size down when |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 33 | floating-point encoding is not needed. (If this is put in a library |
| 34 | and linking is against the library, then dead stripping is automatic). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 35 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 36 | This code works solely using shifts and masks and thus has no |
| 37 | dependency on any math libraries. It can even work if the CPU doesn't |
| 38 | have any floating-point support, though that isn't the most useful |
| 39 | thing to do. |
| 40 | |
| 41 | The memcpy() dependency is only for CopyFloatToUint32() and friends |
| 42 | which only is needed to avoid type punning when converting the actual |
| 43 | float bits to an unsigned value so the bit shifts and masks can work. |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | /* |
| 47 | The references used to write this code: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 48 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 49 | - IEEE 754-2008, particularly section 3.6 and 6.2.1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 50 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 51 | - https://en.wikipedia.org/wiki/IEEE_754 and subordinate pages |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 52 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 53 | - https://stackoverflow.com/questions/19800415/why-does-ieee-754-reserve-so-many-nan-values |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 54 | |
| 55 | - https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules |
| 56 | |
| 57 | - https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 58 | */ |
| 59 | |
| 60 | |
| 61 | // ----- Half Precsion ----------- |
| 62 | #define HALF_NUM_SIGNIFICAND_BITS (10) |
| 63 | #define HALF_NUM_EXPONENT_BITS (5) |
| 64 | #define HALF_NUM_SIGN_BITS (1) |
| 65 | |
| 66 | #define HALF_SIGNIFICAND_SHIFT (0) |
| 67 | #define HALF_EXPONENT_SHIFT (HALF_NUM_SIGNIFICAND_BITS) |
| 68 | #define HALF_SIGN_SHIFT (HALF_NUM_SIGNIFICAND_BITS + HALF_NUM_EXPONENT_BITS) |
| 69 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 70 | #define HALF_SIGNIFICAND_MASK (0x3ffU) // The lower 10 bits // 0x03ff |
| 71 | #define HALF_EXPONENT_MASK (0x1fU << HALF_EXPONENT_SHIFT) // 0x7c00 5 bits of exponent |
| 72 | #define HALF_SIGN_MASK (0x01U << HALF_SIGN_SHIFT) // // 0x8000 1 bit of sign |
| 73 | #define HALF_QUIET_NAN_BIT (0x01U << (HALF_NUM_SIGNIFICAND_BITS-1)) // 0x0200 |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 74 | |
| 75 | /* Biased Biased Unbiased Use |
| 76 | 0x00 0 -15 0 and subnormal |
| 77 | 0x01 1 -14 Smallest normal exponent |
| 78 | 0x1e 30 15 Largest normal exponent |
| 79 | 0x1F 31 16 NaN and Infinity */ |
| 80 | #define HALF_EXPONENT_BIAS (15) |
| 81 | #define HALF_EXPONENT_MAX (HALF_EXPONENT_BIAS) // 15 Unbiased |
| 82 | #define HALF_EXPONENT_MIN (-HALF_EXPONENT_BIAS+1) // -14 Unbiased |
| 83 | #define HALF_EXPONENT_ZERO (-HALF_EXPONENT_BIAS) // -15 Unbiased |
| 84 | #define HALF_EXPONENT_INF_OR_NAN (HALF_EXPONENT_BIAS+1) // 16 Unbiased |
| 85 | |
| 86 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 87 | // ------ Single-Precision -------- |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 88 | #define SINGLE_NUM_SIGNIFICAND_BITS (23) |
| 89 | #define SINGLE_NUM_EXPONENT_BITS (8) |
| 90 | #define SINGLE_NUM_SIGN_BITS (1) |
| 91 | |
| 92 | #define SINGLE_SIGNIFICAND_SHIFT (0) |
| 93 | #define SINGLE_EXPONENT_SHIFT (SINGLE_NUM_SIGNIFICAND_BITS) |
| 94 | #define SINGLE_SIGN_SHIFT (SINGLE_NUM_SIGNIFICAND_BITS + SINGLE_NUM_EXPONENT_BITS) |
| 95 | |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 96 | #define SINGLE_SIGNIFICAND_MASK (0x7fffffU) // The lower 23 bits |
| 97 | #define SINGLE_EXPONENT_MASK (0xffU << SINGLE_EXPONENT_SHIFT) // 8 bits of exponent |
| 98 | #define SINGLE_SIGN_MASK (0x01U << SINGLE_SIGN_SHIFT) // 1 bit of sign |
| 99 | #define SINGLE_QUIET_NAN_BIT (0x01U << (SINGLE_NUM_SIGNIFICAND_BITS-1)) |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 100 | |
| 101 | /* Biased Biased Unbiased Use |
| 102 | 0x0000 0 -127 0 and subnormal |
| 103 | 0x0001 1 -126 Smallest normal exponent |
| 104 | 0x7f 127 0 1 |
| 105 | 0xfe 254 127 Largest normal exponent |
| 106 | 0xff 255 128 NaN and Infinity */ |
| 107 | #define SINGLE_EXPONENT_BIAS (127) |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 108 | #define SINGLE_EXPONENT_MAX (SINGLE_EXPONENT_BIAS) // 127 unbiased |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 109 | #define SINGLE_EXPONENT_MIN (-SINGLE_EXPONENT_BIAS+1) // -126 unbiased |
| 110 | #define SINGLE_EXPONENT_ZERO (-SINGLE_EXPONENT_BIAS) // -127 unbiased |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 111 | #define SINGLE_EXPONENT_INF_OR_NAN (SINGLE_EXPONENT_BIAS+1) // 128 unbiased |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 112 | |
| 113 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 114 | // --------- Double-Precision ---------- |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 115 | #define DOUBLE_NUM_SIGNIFICAND_BITS (52) |
| 116 | #define DOUBLE_NUM_EXPONENT_BITS (11) |
| 117 | #define DOUBLE_NUM_SIGN_BITS (1) |
| 118 | |
| 119 | #define DOUBLE_SIGNIFICAND_SHIFT (0) |
| 120 | #define DOUBLE_EXPONENT_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS) |
| 121 | #define DOUBLE_SIGN_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS + DOUBLE_NUM_EXPONENT_BITS) |
| 122 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 123 | #define DOUBLE_SIGNIFICAND_MASK (0xfffffffffffffULL) // The lower 52 bits |
| 124 | #define DOUBLE_EXPONENT_MASK (0x7ffULL << DOUBLE_EXPONENT_SHIFT) // 11 bits of exponent |
| 125 | #define DOUBLE_SIGN_MASK (0x01ULL << DOUBLE_SIGN_SHIFT) // 1 bit of sign |
| 126 | #define DOUBLE_QUIET_NAN_BIT (0x01ULL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) |
| 127 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 128 | |
| 129 | /* Biased Biased Unbiased Use |
| 130 | 0x00000000 0 -1023 0 and subnormal |
| 131 | 0x00000001 1 -1022 Smallest normal exponent |
| 132 | 0x000007fe 2046 1023 Largest normal exponent |
| 133 | 0x000007ff 2047 1024 NaN and Infinity */ |
| 134 | #define DOUBLE_EXPONENT_BIAS (1023) |
| 135 | #define DOUBLE_EXPONENT_MAX (DOUBLE_EXPONENT_BIAS) // unbiased |
| 136 | #define DOUBLE_EXPONENT_MIN (-DOUBLE_EXPONENT_BIAS+1) // unbiased |
| 137 | #define DOUBLE_EXPONENT_ZERO (-DOUBLE_EXPONENT_BIAS) // unbiased |
| 138 | #define DOUBLE_EXPONENT_INF_OR_NAN (DOUBLE_EXPONENT_BIAS+1) // unbiased |
| 139 | |
| 140 | |
| 141 | |
| 142 | /* |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 143 | Convenient functions to avoid type punning, compiler warnings and |
| 144 | such. The optimizer reduces them to a simple assignment. This is a |
| 145 | crusty corner of C. It shouldn't be this hard. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 146 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 147 | These are also in UsefulBuf.h under a different name. They are copied |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 148 | here to avoid a dependency on UsefulBuf.h. There is no object code |
| 149 | size impact because these always optimze down to a simple assignment. |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 150 | */ |
| 151 | static inline uint32_t CopyFloatToUint32(float f) |
| 152 | { |
| 153 | uint32_t u32; |
| 154 | memcpy(&u32, &f, sizeof(uint32_t)); |
| 155 | return u32; |
| 156 | } |
| 157 | |
| 158 | static inline uint64_t CopyDoubleToUint64(double d) |
| 159 | { |
| 160 | uint64_t u64; |
| 161 | memcpy(&u64, &d, sizeof(uint64_t)); |
| 162 | return u64; |
| 163 | } |
| 164 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 165 | static inline float CopyUint32ToFloat(uint32_t u32) |
| 166 | { |
| 167 | float f; |
| 168 | memcpy(&f, &u32, sizeof(uint32_t)); |
| 169 | return f; |
| 170 | } |
| 171 | |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 172 | static inline double CopyUint64ToDouble(uint64_t u64) |
| 173 | { |
| 174 | double d; |
| 175 | memcpy(&d, &u64, sizeof(uint64_t)); |
| 176 | return d; |
| 177 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 178 | |
| 179 | |
| 180 | // Public function; see ieee754.h |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 181 | uint16_t IEEE754_FloatToHalf(float f) |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 182 | { |
| 183 | // Pull the three parts out of the single-precision float |
| 184 | const uint32_t uSingle = CopyFloatToUint32(f); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 185 | const int32_t nSingleUnbiasedExponent = (int32_t)((uSingle & SINGLE_EXPONENT_MASK) >> SINGLE_EXPONENT_SHIFT) - SINGLE_EXPONENT_BIAS; |
| 186 | const uint32_t uSingleSign = (uSingle & SINGLE_SIGN_MASK) >> SINGLE_SIGN_SHIFT; |
| 187 | const uint32_t uSingleSignificand = uSingle & SINGLE_SIGNIFICAND_MASK; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 188 | |
| 189 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 190 | // Now convert the three parts to half-precision. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 191 | |
| 192 | // All works is done on uint32_t with conversion to uint16_t at the end. |
| 193 | // This avoids integer promotions that static analyzers complain about and |
| 194 | // reduces code size. |
| 195 | uint32_t uHalfSign, uHalfSignificand, uHalfBiasedExponent; |
| 196 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 197 | if(nSingleUnbiasedExponent == SINGLE_EXPONENT_INF_OR_NAN) { |
| 198 | // +/- Infinity and NaNs -- single biased exponent is 0xff |
| 199 | uHalfBiasedExponent = HALF_EXPONENT_INF_OR_NAN + HALF_EXPONENT_BIAS; |
| 200 | if(!uSingleSignificand) { |
| 201 | // Infinity |
| 202 | uHalfSignificand = 0; |
| 203 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 204 | // Copy the LSBs of the NaN payload that will fit from the single to the half |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 205 | uHalfSignificand = uSingleSignificand & (HALF_SIGNIFICAND_MASK & ~HALF_QUIET_NAN_BIT); |
| 206 | if(uSingleSignificand & SINGLE_QUIET_NAN_BIT) { |
| 207 | // It's a qNaN; copy the qNaN bit |
| 208 | uHalfSignificand |= HALF_QUIET_NAN_BIT; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 209 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 210 | // It's an sNaN; make sure the significand is not zero so it stays a NaN |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 211 | // This is needed because not all significand bits are copied from single |
| 212 | if(!uHalfSignificand) { |
| 213 | // Set the LSB. This is what wikipedia shows for sNAN. |
| 214 | uHalfSignificand |= 0x01; |
| 215 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } else if(nSingleUnbiasedExponent == SINGLE_EXPONENT_ZERO) { |
| 219 | // 0 or a subnormal number -- singled biased exponent is 0 |
| 220 | uHalfBiasedExponent = 0; |
| 221 | uHalfSignificand = 0; // Any subnormal single will be too small to express as a half precision |
| 222 | } else if(nSingleUnbiasedExponent > HALF_EXPONENT_MAX) { |
| 223 | // Exponent is too large to express in half-precision; round up to infinity |
| 224 | uHalfBiasedExponent = HALF_EXPONENT_INF_OR_NAN + HALF_EXPONENT_BIAS; |
| 225 | uHalfSignificand = 0; |
| 226 | } else if(nSingleUnbiasedExponent < HALF_EXPONENT_MIN) { |
| 227 | // Exponent is too small to express in half-precision normal; make it a half-precision subnormal |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 228 | uHalfBiasedExponent = HALF_EXPONENT_ZERO + HALF_EXPONENT_BIAS; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 229 | // Difference between single normal exponent and the base exponent of a half subnormal |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 230 | const uint32_t uExpDiff = (uint32_t)-(nSingleUnbiasedExponent - HALF_EXPONENT_MIN); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 231 | // Also have to shift the significand by the difference in number of bits between a single and a half significand |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 232 | const uint32_t uSignificandBitsDiff = SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 233 | // Add in the 1 that is implied in the significand of a normal number; it needs to be present in a subnormal |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 234 | const uint32_t uSingleSignificandSubnormal = uSingleSignificand + (0x01U << SINGLE_NUM_SIGNIFICAND_BITS); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 235 | uHalfSignificand = uSingleSignificandSubnormal >> (uExpDiff + uSignificandBitsDiff); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 236 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 237 | // The normal case, exponent is in range for half-precision |
| 238 | uHalfBiasedExponent = (uint32_t)(nSingleUnbiasedExponent + HALF_EXPONENT_BIAS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 239 | uHalfSignificand = uSingleSignificand >> (SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS); |
| 240 | } |
| 241 | uHalfSign = uSingleSign; |
| 242 | |
| 243 | // Put the 3 values in the right place for a half precision |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 244 | const uint32_t uHalfPrecision = uHalfSignificand | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 245 | (uHalfBiasedExponent << HALF_EXPONENT_SHIFT) | |
| 246 | (uHalfSign << HALF_SIGN_SHIFT); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 247 | // Cast is safe because all the masks and shifts above work to make |
| 248 | // a half precision value which is only 16 bits. |
| 249 | return (uint16_t)uHalfPrecision; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | // Public function; see ieee754.h |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 254 | uint16_t IEEE754_DoubleToHalf(double d) |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 255 | { |
| 256 | // Pull the three parts out of the double-precision float |
| 257 | const uint64_t uDouble = CopyDoubleToUint64(d); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 258 | const int64_t nDoubleUnbiasedExponent = (int64_t)((uDouble & DOUBLE_EXPONENT_MASK) >> DOUBLE_EXPONENT_SHIFT) - DOUBLE_EXPONENT_BIAS; |
| 259 | const uint64_t uDoubleSign = (uDouble & DOUBLE_SIGN_MASK) >> DOUBLE_SIGN_SHIFT; |
| 260 | const uint64_t uDoubleSignificand = uDouble & DOUBLE_SIGNIFICAND_MASK; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 261 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 262 | // Now convert the three parts to half-precision. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 263 | |
| 264 | // All works is done on uint64_t with conversion to uint16_t at the end. |
| 265 | // This avoids integer promotions that static analyzers complain about. |
| 266 | // Other options are for these to be unsigned int or fast_int16_t. Code |
| 267 | // size doesn't vary much between all these options for 64-bit LLVM, |
| 268 | // 64-bit GCC and 32-bit Armv7 LLVM. |
| 269 | uint64_t uHalfSign, uHalfSignificand, uHalfBiasedExponent; |
| 270 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 271 | if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_INF_OR_NAN) { |
| 272 | // +/- Infinity and NaNs -- single biased exponent is 0xff |
| 273 | uHalfBiasedExponent = HALF_EXPONENT_INF_OR_NAN + HALF_EXPONENT_BIAS; |
| 274 | if(!uDoubleSignificand) { |
| 275 | // Infinity |
| 276 | uHalfSignificand = 0; |
| 277 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 278 | // Copy the LSBs of the NaN payload that will fit from the double to the half |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 279 | uHalfSignificand = uDoubleSignificand & (HALF_SIGNIFICAND_MASK & ~HALF_QUIET_NAN_BIT); |
| 280 | if(uDoubleSignificand & DOUBLE_QUIET_NAN_BIT) { |
| 281 | // It's a qNaN; copy the qNaN bit |
| 282 | uHalfSignificand |= HALF_QUIET_NAN_BIT; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 283 | } else { |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 284 | // It's an sNaN; make sure the significand is not zero so it stays a NaN |
| 285 | // This is needed because not all significand bits are copied from single |
| 286 | if(!uHalfSignificand) { |
| 287 | // Set the LSB. This is what wikipedia shows for sNAN. |
| 288 | uHalfSignificand |= 0x01; |
| 289 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } else if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_ZERO) { |
| 293 | // 0 or a subnormal number -- double biased exponent is 0 |
| 294 | uHalfBiasedExponent = 0; |
| 295 | uHalfSignificand = 0; // Any subnormal single will be too small to express as a half precision; TODO, is this really true? |
| 296 | } else if(nDoubleUnbiasedExponent > HALF_EXPONENT_MAX) { |
| 297 | // Exponent is too large to express in half-precision; round up to infinity; TODO, is this really true? |
| 298 | uHalfBiasedExponent = HALF_EXPONENT_INF_OR_NAN + HALF_EXPONENT_BIAS; |
| 299 | uHalfSignificand = 0; |
| 300 | } else if(nDoubleUnbiasedExponent < HALF_EXPONENT_MIN) { |
| 301 | // Exponent is too small to express in half-precision; round down to zero |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 302 | uHalfBiasedExponent = HALF_EXPONENT_ZERO + HALF_EXPONENT_BIAS; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 303 | // Difference between double normal exponent and the base exponent of a half subnormal |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 304 | const uint64_t uExpDiff = (uint64_t)-(nDoubleUnbiasedExponent - HALF_EXPONENT_MIN); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 305 | // Also have to shift the significand by the difference in number of bits between a double and a half significand |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 306 | const uint64_t uSignificandBitsDiff = DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 307 | // Add in the 1 that is implied in the significand of a normal number; it needs to be present in a subnormal |
Laurence Lundblade | f280195 | 2018-12-17 10:40:29 -0800 | [diff] [blame] | 308 | const uint64_t uDoubleSignificandSubnormal = uDoubleSignificand + (0x01ULL << DOUBLE_NUM_SIGNIFICAND_BITS); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 309 | uHalfSignificand = uDoubleSignificandSubnormal >> (uExpDiff + uSignificandBitsDiff); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 310 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 311 | // The normal case, exponent is in range for half-precision |
| 312 | uHalfBiasedExponent = (uint32_t)(nDoubleUnbiasedExponent + HALF_EXPONENT_BIAS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 313 | uHalfSignificand = uDoubleSignificand >> (DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS); |
| 314 | } |
| 315 | uHalfSign = uDoubleSign; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 316 | |
| 317 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 318 | // Put the 3 values in the right place for a half precision |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 319 | const uint64_t uHalfPrecision = uHalfSignificand | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 320 | (uHalfBiasedExponent << HALF_EXPONENT_SHIFT) | |
| 321 | (uHalfSign << HALF_SIGN_SHIFT); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 322 | // Cast is safe because all the masks and shifts above work to make |
| 323 | // a half precision value which is only 16 bits. |
| 324 | return (uint16_t)uHalfPrecision; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 328 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 329 | // Public function; see ieee754.h |
| 330 | float IEEE754_HalfToFloat(uint16_t uHalfPrecision) |
| 331 | { |
| 332 | // Pull out the three parts of the half-precision float |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 333 | // Do all the work in 32 bits because that is what the end result is |
| 334 | // may give smaller code size and will keep static analyzers happier. |
| 335 | const uint32_t uHalfSignificand = uHalfPrecision & HALF_SIGNIFICAND_MASK; |
| 336 | const int32_t nHalfUnBiasedExponent = (int32_t)((uHalfPrecision & HALF_EXPONENT_MASK) >> HALF_EXPONENT_SHIFT) - HALF_EXPONENT_BIAS; |
| 337 | const uint32_t uHalfSign = (uHalfPrecision & HALF_SIGN_MASK) >> HALF_SIGN_SHIFT; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 338 | |
| 339 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 340 | // Make the three parts of the single-precision number |
| 341 | uint32_t uSingleSignificand, uSingleSign, uSingleBiasedExponent; |
| 342 | if(nHalfUnBiasedExponent == HALF_EXPONENT_ZERO) { |
| 343 | // 0 or subnormal |
| 344 | if(uHalfSignificand) { |
| 345 | // Subnormal case |
| 346 | uSingleBiasedExponent = -HALF_EXPONENT_BIAS + SINGLE_EXPONENT_BIAS +1; |
| 347 | // A half-precision subnormal can always be converted to a normal single-precision float because the ranges line up |
| 348 | uSingleSignificand = uHalfSignificand; |
| 349 | // Shift bits from right of the decimal to left, reducing the exponent by 1 each time |
| 350 | do { |
| 351 | uSingleSignificand <<= 1; |
| 352 | uSingleBiasedExponent--; |
| 353 | } while ((uSingleSignificand & 0x400) == 0); |
| 354 | uSingleSignificand &= HALF_SIGNIFICAND_MASK; |
| 355 | uSingleSignificand <<= (SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS); |
| 356 | } else { |
| 357 | // Just zero |
| 358 | uSingleBiasedExponent = SINGLE_EXPONENT_ZERO + SINGLE_EXPONENT_BIAS; |
| 359 | uSingleSignificand = 0; |
| 360 | } |
| 361 | } else if(nHalfUnBiasedExponent == HALF_EXPONENT_INF_OR_NAN) { |
| 362 | // NaN or Inifinity |
| 363 | uSingleBiasedExponent = SINGLE_EXPONENT_INF_OR_NAN + SINGLE_EXPONENT_BIAS; |
| 364 | if(uHalfSignificand) { |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 365 | // NaN |
| 366 | // First preserve the NaN payload from half to single |
| 367 | uSingleSignificand = uHalfSignificand & ~HALF_QUIET_NAN_BIT; |
| 368 | if(uHalfSignificand & HALF_QUIET_NAN_BIT) { |
| 369 | // Next, set qNaN if needed since half qNaN bit is not copied above |
| 370 | uSingleSignificand |= SINGLE_QUIET_NAN_BIT; |
| 371 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 372 | } else { |
| 373 | // Infinity |
| 374 | uSingleSignificand = 0; |
| 375 | } |
| 376 | } else { |
| 377 | // Normal number |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 378 | uSingleBiasedExponent = (uint32_t)(nHalfUnBiasedExponent + SINGLE_EXPONENT_BIAS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 379 | uSingleSignificand = uHalfSignificand << (SINGLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS); |
| 380 | } |
| 381 | uSingleSign = uHalfSign; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 382 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 383 | // Shift the three parts of the single-precision into place |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 384 | const uint32_t uSinglePrecision = uSingleSignificand | |
| 385 | (uSingleBiasedExponent << SINGLE_EXPONENT_SHIFT) | |
| 386 | (uSingleSign << SINGLE_SIGN_SHIFT); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 387 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 388 | return CopyUint32ToFloat(uSinglePrecision); |
| 389 | } |
| 390 | |
| 391 | |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 392 | // Public function; see ieee754.h |
| 393 | double IEEE754_HalfToDouble(uint16_t uHalfPrecision) |
| 394 | { |
| 395 | // Pull out the three parts of the half-precision float |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 396 | // Do all the work in 64 bits because that is what the end result is. |
| 397 | // It may give smaller code size and will keep static analyzers happier. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 398 | const uint64_t uHalfSignificand = uHalfPrecision & HALF_SIGNIFICAND_MASK; |
| 399 | const int64_t nHalfUnBiasedExponent = (int64_t)((uHalfPrecision & HALF_EXPONENT_MASK) >> HALF_EXPONENT_SHIFT) - HALF_EXPONENT_BIAS; |
| 400 | const uint64_t uHalfSign = (uHalfPrecision & HALF_SIGN_MASK) >> HALF_SIGN_SHIFT; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 401 | |
| 402 | |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 403 | // Make the three parts of hte single-precision number |
| 404 | uint64_t uDoubleSignificand, uDoubleSign, uDoubleBiasedExponent; |
| 405 | if(nHalfUnBiasedExponent == HALF_EXPONENT_ZERO) { |
| 406 | // 0 or subnormal |
| 407 | uDoubleBiasedExponent = DOUBLE_EXPONENT_ZERO + DOUBLE_EXPONENT_BIAS; |
| 408 | if(uHalfSignificand) { |
| 409 | // Subnormal case |
| 410 | uDoubleBiasedExponent = -HALF_EXPONENT_BIAS + DOUBLE_EXPONENT_BIAS +1; |
| 411 | // A half-precision subnormal can always be converted to a normal double-precision float because the ranges line up |
| 412 | uDoubleSignificand = uHalfSignificand; |
| 413 | // Shift bits from right of the decimal to left, reducing the exponent by 1 each time |
| 414 | do { |
| 415 | uDoubleSignificand <<= 1; |
| 416 | uDoubleBiasedExponent--; |
| 417 | } while ((uDoubleSignificand & 0x400) == 0); |
| 418 | uDoubleSignificand &= HALF_SIGNIFICAND_MASK; |
| 419 | uDoubleSignificand <<= (DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS); |
| 420 | } else { |
| 421 | // Just zero |
| 422 | uDoubleSignificand = 0; |
| 423 | } |
| 424 | } else if(nHalfUnBiasedExponent == HALF_EXPONENT_INF_OR_NAN) { |
| 425 | // NaN or Inifinity |
| 426 | uDoubleBiasedExponent = DOUBLE_EXPONENT_INF_OR_NAN + DOUBLE_EXPONENT_BIAS; |
| 427 | if(uHalfSignificand) { |
| 428 | // NaN |
| 429 | // First preserve the NaN payload from half to single |
| 430 | uDoubleSignificand = uHalfSignificand & ~HALF_QUIET_NAN_BIT; |
| 431 | if(uHalfSignificand & HALF_QUIET_NAN_BIT) { |
| 432 | // Next, set qNaN if needed since half qNaN bit is not copied above |
| 433 | uDoubleSignificand |= DOUBLE_QUIET_NAN_BIT; |
| 434 | } |
| 435 | } else { |
| 436 | // Infinity |
| 437 | uDoubleSignificand = 0; |
| 438 | } |
| 439 | } else { |
| 440 | // Normal number |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 441 | uDoubleBiasedExponent = (uint64_t)(nHalfUnBiasedExponent + DOUBLE_EXPONENT_BIAS); |
| 442 | uDoubleSignificand = uHalfSignificand << (DOUBLE_NUM_SIGNIFICAND_BITS - HALF_NUM_SIGNIFICAND_BITS); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 443 | } |
| 444 | uDoubleSign = uHalfSign; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 445 | |
| 446 | |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 447 | // Shift the 3 parts into place as a double-precision |
| 448 | const uint64_t uDouble = uDoubleSignificand | |
| 449 | (uDoubleBiasedExponent << DOUBLE_EXPONENT_SHIFT) | |
| 450 | (uDoubleSign << DOUBLE_SIGN_SHIFT); |
| 451 | return CopyUint64ToDouble(uDouble); |
| 452 | } |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 453 | |
| 454 | |
| 455 | // Public function; see ieee754.h |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 456 | double IEEE754_FloatToDouble(float f) |
| 457 | { |
| 458 | const uint32_t uFloat = CopyFloatToUint32(f); |
| 459 | // Pull out the three parts of the single-precision float |
| 460 | // Do all the work in 64 bits because that is what the end result is. |
| 461 | // It may give smaller code size and will keep static analyzers happier. |
| 462 | const uint64_t uSingleSignificand = uFloat & SINGLE_SIGNIFICAND_MASK; |
| 463 | const int64_t nSingleUnBiasedExponent = (int64_t)((uFloat & SINGLE_EXPONENT_MASK) >> SINGLE_EXPONENT_SHIFT) - SINGLE_EXPONENT_BIAS; |
| 464 | const uint64_t uSingleSign = (uFloat & SINGLE_SIGN_MASK) >> SINGLE_SIGN_SHIFT; |
| 465 | |
| 466 | |
| 467 | // Make the three parts of hte single-precision number |
| 468 | uint64_t uDoubleSignificand, uDoubleSign, uDoubleBiasedExponent; |
| 469 | if(nSingleUnBiasedExponent == SINGLE_EXPONENT_ZERO) { |
| 470 | // 0 or subnormal |
| 471 | uDoubleBiasedExponent = DOUBLE_EXPONENT_ZERO + DOUBLE_EXPONENT_BIAS; |
| 472 | if(uSingleSignificand) { |
| 473 | // Subnormal case |
| 474 | uDoubleBiasedExponent = -SINGLE_EXPONENT_BIAS + DOUBLE_EXPONENT_BIAS + 1; |
| 475 | // A single-precision subnormal can always be converted to a normal double-precision float because the ranges line up |
| 476 | uDoubleSignificand = uSingleSignificand; |
| 477 | // Shift bits from right of the decimal to left, reducing the exponent by 1 each time |
| 478 | do { |
| 479 | uDoubleSignificand <<= 1; |
| 480 | uDoubleBiasedExponent--; |
| 481 | // TODO: is this right? Where does 0x400 come from? |
| 482 | } while ((uDoubleSignificand & 0x400) == 0); |
| 483 | uDoubleSignificand &= SINGLE_SIGNIFICAND_MASK; |
| 484 | uDoubleSignificand <<= (DOUBLE_NUM_SIGNIFICAND_BITS - SINGLE_NUM_SIGNIFICAND_BITS); |
| 485 | } else { |
| 486 | // Just zero |
| 487 | uDoubleSignificand = 0; |
| 488 | } |
| 489 | } else if(nSingleUnBiasedExponent == SINGLE_EXPONENT_INF_OR_NAN) { |
| 490 | // NaN or Inifinity |
| 491 | uDoubleBiasedExponent = DOUBLE_EXPONENT_INF_OR_NAN + DOUBLE_EXPONENT_BIAS; |
| 492 | if(uSingleSignificand) { |
| 493 | // NaN |
| 494 | // First preserve the NaN payload from half to single |
| 495 | // TODO: check this |
| 496 | uDoubleSignificand = uSingleSignificand & ~SINGLE_QUIET_NAN_BIT; |
| 497 | if(uSingleSignificand & SINGLE_QUIET_NAN_BIT) { |
| 498 | // Next, set qNaN if needed since half qNaN bit is not copied above |
| 499 | uDoubleSignificand |= DOUBLE_QUIET_NAN_BIT; |
| 500 | } |
| 501 | } else { |
| 502 | // Infinity |
| 503 | uDoubleSignificand = 0; |
| 504 | } |
| 505 | } else { |
| 506 | // Normal number |
| 507 | uDoubleBiasedExponent = (uint64_t)(nSingleUnBiasedExponent + DOUBLE_EXPONENT_BIAS); |
| 508 | uDoubleSignificand = uSingleSignificand << (DOUBLE_NUM_SIGNIFICAND_BITS - SINGLE_NUM_SIGNIFICAND_BITS); |
| 509 | } |
| 510 | uDoubleSign = uSingleSign; |
| 511 | |
| 512 | |
| 513 | // Shift the 3 parts into place as a double-precision |
| 514 | const uint64_t uDouble = uDoubleSignificand | |
| 515 | (uDoubleBiasedExponent << DOUBLE_EXPONENT_SHIFT) | |
| 516 | (uDoubleSign << DOUBLE_SIGN_SHIFT); |
| 517 | return CopyUint64ToDouble(uDouble); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | // Public function; see ieee754.h |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 524 | IEEE754_union IEEE754_FloatToSmallest(float f) |
| 525 | { |
| 526 | IEEE754_union result; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 527 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 528 | // Pull the neeed two parts out of the single-precision float |
| 529 | const uint32_t uSingle = CopyFloatToUint32(f); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 530 | const int32_t nSingleExponent = (int32_t)((uSingle & SINGLE_EXPONENT_MASK) >> SINGLE_EXPONENT_SHIFT) - SINGLE_EXPONENT_BIAS; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 531 | const uint32_t uSingleSignificand = uSingle & SINGLE_SIGNIFICAND_MASK; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 532 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 533 | // Bit mask that is the significand bits that would be lost when converting |
| 534 | // from single-precision to half-precision |
| 535 | const uint64_t uDroppedSingleBits = SINGLE_SIGNIFICAND_MASK >> HALF_NUM_SIGNIFICAND_BITS; |
| 536 | |
| 537 | // Optimizer will re organize so there is only one call to IEEE754_FloatToHalf() |
| 538 | if(uSingle == 0) { |
| 539 | // Value is 0.0000, not a a subnormal |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 540 | result.uSize = IEEE754_UNION_IS_HALF; |
| 541 | result.uValue = IEEE754_FloatToHalf(f); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 542 | } else if(nSingleExponent == SINGLE_EXPONENT_INF_OR_NAN) { |
| 543 | // NaN, +/- infinity |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 544 | result.uSize = IEEE754_UNION_IS_HALF; |
| 545 | result.uValue = IEEE754_FloatToHalf(f); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 546 | } else if((nSingleExponent >= HALF_EXPONENT_MIN) && nSingleExponent <= HALF_EXPONENT_MAX && (!(uSingleSignificand & uDroppedSingleBits))) { |
| 547 | // Normal number in exponent range and precision won't be lost |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 548 | result.uSize = IEEE754_UNION_IS_HALF; |
| 549 | result.uValue = IEEE754_FloatToHalf(f); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 550 | } else { |
| 551 | // Subnormal, exponent out of range, or precision will be lost |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 552 | result.uSize = IEEE754_UNION_IS_SINGLE; |
| 553 | result.uValue = uSingle; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 554 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 555 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 556 | return result; |
| 557 | } |
| 558 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 559 | // Public function; see ieee754.h |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 560 | IEEE754_union IEEE754_DoubleToSmallestInternal(double d, int bAllowHalfPrecision) |
| 561 | { |
| 562 | IEEE754_union result; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 563 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 564 | // Pull the needed two parts out of the double-precision float |
| 565 | const uint64_t uDouble = CopyDoubleToUint64(d); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 566 | const int64_t nDoubleExponent = (int64_t)((uDouble & DOUBLE_EXPONENT_MASK) >> DOUBLE_EXPONENT_SHIFT) - DOUBLE_EXPONENT_BIAS; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 567 | const uint64_t uDoubleSignificand = uDouble & DOUBLE_SIGNIFICAND_MASK; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 568 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 569 | // Masks to check whether dropped significand bits are zero or not |
| 570 | const uint64_t uDroppedDoubleBits = DOUBLE_SIGNIFICAND_MASK >> HALF_NUM_SIGNIFICAND_BITS; |
| 571 | const uint64_t uDroppedSingleBits = DOUBLE_SIGNIFICAND_MASK >> SINGLE_NUM_SIGNIFICAND_BITS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 572 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 573 | // The various cases |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 574 | if(d == 0.0) { // Take care of positive and negative zero |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 575 | // Value is 0.0000, not a a subnormal |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 576 | result.uSize = IEEE754_UNION_IS_HALF; |
| 577 | result.uValue = IEEE754_DoubleToHalf(d); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 578 | } else if(nDoubleExponent == DOUBLE_EXPONENT_INF_OR_NAN) { |
| 579 | // NaN, +/- infinity |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 580 | result.uSize = IEEE754_UNION_IS_HALF; |
| 581 | result.uValue = IEEE754_DoubleToHalf(d); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 582 | } else if(bAllowHalfPrecision && (nDoubleExponent >= HALF_EXPONENT_MIN) && nDoubleExponent <= HALF_EXPONENT_MAX && (!(uDoubleSignificand & uDroppedDoubleBits))) { |
| 583 | // Can convert to half without precision loss |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 584 | result.uSize = IEEE754_UNION_IS_HALF; |
| 585 | result.uValue = IEEE754_DoubleToHalf(d); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 586 | } else if((nDoubleExponent >= SINGLE_EXPONENT_MIN) && nDoubleExponent <= SINGLE_EXPONENT_MAX && (!(uDoubleSignificand & uDroppedSingleBits))) { |
| 587 | // Can convert to single without precision loss |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 588 | result.uSize = IEEE754_UNION_IS_SINGLE; |
| 589 | result.uValue = CopyFloatToUint32((float)d); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 590 | } else { |
| 591 | // Can't convert without precision loss |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 592 | result.uSize = IEEE754_UNION_IS_DOUBLE; |
| 593 | result.uValue = uDouble; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 594 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 595 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 596 | return result; |
| 597 | } |
| 598 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 599 | #endif /* QCBOR_CONFIG_DISABLE_ENCODE_IEEE754 */ |