Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2 | |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 3 | Copyright (c) 2018, Laurence Lundblade. |
| 4 | All rights reserved. |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 5 | |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * The name "Laurence Lundblade" may not be used to |
| 16 | endorse or promote products derived from this software without |
| 17 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 18 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 19 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 20 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 23 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 26 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 28 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 29 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 30 | ==============================================================================*/ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 31 | // |
| 32 | // ieee754.h |
| 33 | // Indefinite |
| 34 | // |
| 35 | // Created by Laurence Lundblade on 7/23/18. |
| 36 | // Copyright © 2018 Laurence Lundblade. All rights reserved. |
| 37 | // |
| 38 | |
| 39 | #ifndef ieee754_h |
| 40 | #define ieee754_h |
| 41 | |
| 42 | #include <stdint.h> |
| 43 | |
| 44 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | General comments |
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 | This is a complete in that it handles all conversion cases |
| 50 | including +/- infinity, +/- zero, subnormal numbers, qNaN, sNaN |
| 51 | and NaN payloads. |
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 | This confirms to IEEE 754-2008, but note that this doesn't |
| 54 | specify conversions, just the encodings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 55 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 56 | NaN payloads are preserved with alignment on the LSB. The |
| 57 | qNaN bit is handled differently and explicity copied. It |
| 58 | is always the MSB of the significand. The NaN payload MSBs |
| 59 | (except the qNaN bit) are truncated when going from |
| 60 | double or single to half. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 61 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 62 | TODO: what does the C cast do with NaN payloads from |
| 63 | double to single? |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 64 | |
| 65 | |
| 66 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 67 | */ |
| 68 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 69 | /* |
| 70 | Most simply just explicilty encode the type you want, single or double. |
| 71 | This works easily everywhere since standard C supports both |
| 72 | these types and so does qcbor. This encoder also supports |
| 73 | half precision and there's a few ways to use it to encode |
| 74 | floating point numbers in less space. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 75 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 76 | Without losing precision, you can encode a single or double |
| 77 | such that the special values of 0, NaN and Infinity encode |
| 78 | as half-precision. This CBOR decodoer and most others |
| 79 | should handle this properly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 80 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 81 | If you don't mind losing precision, then you can use half-precision. |
| 82 | One way to do this is to set up your environment to use |
| 83 | ___fp_16. Some compilers and CPUs support it even though it is not |
| 84 | standard C. What is nice about this is that your program |
| 85 | will use less memory and floating point operations like |
| 86 | multiplying, adding and such will be faster. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 87 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 88 | Another way to make use of half-precision is to represent |
| 89 | the values in your program as single or double, but encode |
| 90 | them in CBOR as half-precision. This cuts the size |
| 91 | of the encoded messages by 2 or 4, but doesn't reduce |
| 92 | memory needs or speed because you are still using |
| 93 | single or double in your code. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 94 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 95 | |
| 96 | encode: |
| 97 | - float as float |
| 98 | - double as double |
| 99 | - half as half |
| 100 | - float as half_precision, for environments that don't support a half-precision type |
| 101 | - double as half_precision, for environments that don't support a half-precision type |
| 102 | - float with NaN, Infinity and 0 as half |
| 103 | - double with NaN, Infinity and 0 as half |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 104 | |
| 105 | |
| 106 | |
| 107 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 108 | */ |
| 109 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /* |
| 113 | Convert single precision float to half-precision float. |
| 114 | Precision and NaN payload bits will be lost. Too large |
| 115 | values will round up to infinity and too small to zero. |
| 116 | */ |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 117 | uint16_t IEEE754_FloatToHalf(float f); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 118 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | Convert half precision float to single precision float. |
| 122 | This is a loss-less conversion. |
| 123 | */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 124 | float IEEE754_HalfToFloat(uint16_t uHalfPrecision); |
| 125 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | Convert double precision float to half-precision float. |
| 129 | Precision and NaN payload bits will be lost. Too large |
| 130 | values will round up to infinity and too small to zero. |
| 131 | */ |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 132 | uint16_t IEEE754_DoubleToHalf(double d); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 133 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | Convert half precision float to double precision float. |
| 137 | This is a loss-less conversion. |
| 138 | */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 139 | double IEEE754_HalfToDouble(uint16_t uHalfPrecision); |
| 140 | |
| 141 | |
| 142 | |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 143 | // Both tags the value and gives the size |
| 144 | #define IEEE754_UNION_IS_HALF 2 |
| 145 | #define IEEE754_UNION_IS_SINGLE 4 |
| 146 | #define IEEE754_UNION_IS_DOUBLE 8 |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 147 | |
| 148 | typedef struct { |
Laurence Lundblade | 577d821 | 2018-11-01 14:04:08 +0700 | [diff] [blame] | 149 | uint8_t uSize; // One of IEEE754_IS_xxxx |
| 150 | uint64_t uValue; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 151 | } IEEE754_union; |
| 152 | |
| 153 | |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 154 | /* |
| 155 | Converts double-precision to single-precision or half-precision if possible without |
| 156 | loss of precisions. If not, leaves it as a double. Only converts to single-precision |
| 157 | unless bAllowHalfPrecision is set. |
| 158 | */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 159 | IEEE754_union IEEE754_DoubleToSmallestInternal(double d, int bAllowHalfPrecision); |
| 160 | |
| 161 | /* |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 162 | Converts double-precision to single-precision if possible without |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 163 | loss of precision. If not, leaves it as a double. |
| 164 | */ |
| 165 | static inline IEEE754_union IEEE754_DoubleToSmall(double d) |
| 166 | { |
| 167 | return IEEE754_DoubleToSmallestInternal(d, 0); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* |
Laurence Lundblade | 8db3d3e | 2018-09-29 11:46:37 -0700 | [diff] [blame] | 172 | Converts double-precision to single-precision or half-precision if possible without |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 173 | loss of precisions. If not, leaves it as a double. |
| 174 | */ |
| 175 | static inline IEEE754_union IEEE754_DoubleToSmallest(double d) |
| 176 | { |
| 177 | return IEEE754_DoubleToSmallestInternal(d, 1); |
| 178 | } |
| 179 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 180 | /* |
| 181 | Converts single-precision to half-precision if possible without |
| 182 | loss of precision. If not leaves as single-precision. |
| 183 | */ |
| 184 | IEEE754_union IEEE754_FloatToSmallest(float f); |
| 185 | |
| 186 | |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 187 | #endif /* ieee754_h */ |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |