Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 2 | float_tests.c -- tests for float and conversion to/from half-precision |
Laurence Lundblade | 781fd82 | 2018-10-01 09:37:52 -0700 | [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. |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5 | Copyright (c) 2021, Arm Limited. All rights reserved. |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -0800 | [diff] [blame] | 6 | |
Laurence Lundblade | a3fd49f | 2019-01-21 10:16:22 -0800 | [diff] [blame] | 7 | SPDX-License-Identifier: BSD-3-Clause |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -0800 | [diff] [blame] | 8 | |
Laurence Lundblade | a3fd49f | 2019-01-21 10:16:22 -0800 | [diff] [blame] | 9 | See BSD-3-Clause license in README.md |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -0800 | [diff] [blame] | 10 | |
Laurence Lundblade | a3fd49f | 2019-01-21 10:16:22 -0800 | [diff] [blame] | 11 | Created on 9/19/18 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 12 | =============================================================================*/ |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 13 | |
Laurence Lundblade | 2aa0b57 | 2020-07-16 19:48:42 -0700 | [diff] [blame] | 14 | |
| 15 | #include "float_tests.h" |
Laurence Lundblade | 585127a | 2020-07-15 03:25:24 -0700 | [diff] [blame] | 16 | #include "qcbor/qcbor_encode.h" |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 17 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 18 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | 585127a | 2020-07-15 03:25:24 -0700 | [diff] [blame] | 19 | #include <math.h> // For INFINITY and NAN and isnan() |
| 20 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 21 | |
| 22 | /* Make a test results code that includes three components |
| 23 | * Return code is |
| 24 | * xxxyyyzzz where zz is the error code, yy is the test number and zz is |
| 25 | * check being performed |
| 26 | */ |
| 27 | static inline int32_t MakeTestResultCode(uint32_t uTestCase, |
| 28 | uint32_t uTestNumber, |
| 29 | QCBORError uErrorCode) |
| 30 | { |
| 31 | uint32_t uCode = (uTestCase * 1000000) + |
| 32 | (uTestNumber * 1000) + |
| 33 | (uint32_t)uErrorCode; |
| 34 | return (int32_t)uCode; |
| 35 | } |
| 36 | |
| 37 | |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 38 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 39 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 40 | #include "half_to_double_from_rfc7049.h" |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 41 | |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 42 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 43 | /* |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 44 | Half-precision values that are input to test half-precision decoding |
| 45 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 46 | As decoded by http://cbor.me |
| 47 | {"zero": 0.0, |
| 48 | "infinitity": Infinity, |
| 49 | "negative infinitity": -Infinity, |
| 50 | "NaN": NaN, |
| 51 | "one": 1.0, |
| 52 | "one third": 0.333251953125, |
| 53 | "largest half-precision": 65504.0, |
| 54 | "too-large half-precision": Infinity, |
| 55 | "smallest subnormal": 5.960464477539063e-8, |
| 56 | "smallest normal": 0.00006097555160522461, |
| 57 | "biggest subnormal": 0.00006103515625, |
| 58 | "subnormal single": 0.0, |
| 59 | 3: -2.0, |
| 60 | 4: NaN, |
| 61 | 5: NaN, |
| 62 | 6: NaN, |
| 63 | 7: NaN} |
| 64 | */ |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 65 | static const uint8_t spExpectedHalf[] = { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 66 | 0xB1, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 67 | 0x64, |
| 68 | 0x7A, 0x65, 0x72, 0x6F, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 69 | 0xF9, 0x00, 0x00, // half-precision 0.000 |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 70 | 0x6A, |
| 71 | 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 72 | 0xF9, 0x7C, 0x00, // Infinity |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 73 | 0x73, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 74 | 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, 0x6E, |
| 75 | 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 76 | 0xF9, 0xFC, 0x00, // -Inifinity |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 77 | 0x63, |
| 78 | 0x4E, 0x61, 0x4E, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 79 | 0xF9, 0x7E, 0x00, // NaN |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 80 | 0x63, |
| 81 | 0x6F, 0x6E, 0x65, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 82 | 0xF9, 0x3C, 0x00, // 1.0 |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 83 | 0x69, |
| 84 | 0x6F, 0x6E, 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 85 | 0xF9, 0x35, 0x55, // half-precsion one third 0.333251953125 |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 86 | 0x76, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 87 | 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C, |
| 88 | 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 89 | 0xF9, 0x7B, 0xFF, // largest half-precision 65504.0 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 90 | 0x78, 0x18, |
| 91 | 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x68, |
| 92 | 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, |
| 93 | 0x6F, 0x6E, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 94 | 0xF9, 0x7C, 0x00, // Infinity |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 95 | 0x72, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 96 | 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, |
| 97 | 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 98 | 0xF9, 0x00, 0x01, // Smallest half-precision subnormal 0.000000059604645 |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 99 | 0x71, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 100 | 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62, |
| 101 | 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 102 | 0xF9, 0x03, 0xFF, // Largest half-precision subnormal 0.0000609755516 |
| 103 | 0x6F, |
| 104 | 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F, |
| 105 | 0x72, 0x6D, 0x61, 0x6C, |
| 106 | 0xF9, 0x04, 0x00, // Smallest half-precision normal 0.000061988 |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 107 | 0x70, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 108 | 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x73, |
| 109 | 0x69, 0x6E, 0x67, 0x6C, 0x65, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 110 | 0xF9, 0x00, 0x00, |
| 111 | 0x03, |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 112 | 0xF9, 0xC0, 0x00, // -2 |
| 113 | 0x04, |
| 114 | 0xF9, 0x7E, 0x00, // qNaN |
| 115 | 0x05, |
| 116 | 0xF9, 0x7C, 0x01, // sNaN |
| 117 | 0x06, |
| 118 | 0xF9, 0x7E, 0x0F, // qNaN with payload 0x0f |
| 119 | 0x07, |
| 120 | 0xF9, 0x7C, 0x0F, // sNaN with payload 0x0f |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 124 | inline static bool CheckDouble(double d, uint64_t u) |
| 125 | { |
| 126 | return UsefulBufUtil_CopyDoubleToUint64(d) != u; |
| 127 | } |
| 128 | |
| 129 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 130 | int32_t HalfPrecisionDecodeBasicTests() |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 131 | { |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 132 | UsefulBufC HalfPrecision = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedHalf); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 133 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 134 | QCBORDecodeContext DC; |
| 135 | QCBORDecode_Init(&DC, HalfPrecision, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 136 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 137 | QCBORItem Item; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 138 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 139 | QCBORDecode_GetNext(&DC, &Item); |
| 140 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 141 | return -1; |
| 142 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 143 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 144 | QCBORDecode_GetNext(&DC, &Item); |
| 145 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0) { |
| 146 | return -2; |
| 147 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 148 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 149 | QCBORDecode_GetNext(&DC, &Item); |
| 150 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) { |
| 151 | return -3; |
| 152 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 153 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 154 | QCBORDecode_GetNext(&DC, &Item); |
| 155 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -INFINITY) { |
| 156 | return -4; |
| 157 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 158 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 159 | // TODO: NAN-related is this really converting right? It is carrying |
| 160 | // payload, but this confuses things. |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 161 | QCBORDecode_GetNext(&DC, &Item); |
| 162 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || !isnan(Item.val.dfnum)) { |
| 163 | return -5; |
| 164 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 165 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 166 | QCBORDecode_GetNext(&DC, &Item); |
| 167 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 1.0) { |
| 168 | return -6; |
| 169 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 170 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 171 | // Approximately 1/3 |
| 172 | QCBORDecode_GetNext(&DC, &Item); |
| 173 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.333251953125) { |
| 174 | return -7; |
| 175 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 176 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 177 | // Largest half-precision |
| 178 | QCBORDecode_GetNext(&DC, &Item); |
| 179 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 65504.0) { |
| 180 | return -8; |
| 181 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 182 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 183 | QCBORDecode_GetNext(&DC, &Item); |
| 184 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) { |
| 185 | return -9; |
| 186 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 187 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 188 | // Smallest half-precision subnormal |
| 189 | QCBORDecode_GetNext(&DC, &Item); |
| 190 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.00000005960464477539063) { |
| 191 | return -10; |
| 192 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 193 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 194 | // Largest half-precision subnormal |
| 195 | QCBORDecode_GetNext(&DC, &Item); |
| 196 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.00006097555160522461) { |
| 197 | return -11; |
| 198 | } |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 199 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 200 | // Smallest half-precision normal |
| 201 | QCBORDecode_GetNext(&DC, &Item); |
| 202 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.00006103515625) { |
| 203 | return -12; |
| 204 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 205 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 206 | // half-precision zero |
| 207 | QCBORDecode_GetNext(&DC, &Item); |
| 208 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0) { |
| 209 | return -13; |
| 210 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 211 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 212 | // negative 2 |
| 213 | QCBORDecode_GetNext(&DC, &Item); |
| 214 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -2.0) { |
| 215 | return -14; |
| 216 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 217 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 218 | // TODO: NAN-related double check these four tests |
| 219 | QCBORDecode_GetNext(&DC, &Item); // qNaN |
| 220 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 221 | CheckDouble(Item.val.dfnum, 0x7ff8000000000000ULL)) { |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 222 | return -15; |
| 223 | } |
| 224 | QCBORDecode_GetNext(&DC, &Item); // sNaN |
| 225 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 226 | CheckDouble(Item.val.dfnum, 0x7ff0000000000001ULL)) { |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 227 | return -16; |
| 228 | } |
| 229 | QCBORDecode_GetNext(&DC, &Item); // qNaN with payload 0x0f |
| 230 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 231 | CheckDouble(Item.val.dfnum, 0x7ff800000000000fULL)) { |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 232 | return -17; |
| 233 | } |
| 234 | QCBORDecode_GetNext(&DC, &Item); // sNaN with payload 0x0f |
| 235 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 236 | CheckDouble(Item.val.dfnum, 0x7ff000000000000fULL)) { |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 237 | return -18; |
| 238 | } |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 239 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 240 | if(QCBORDecode_Finish(&DC)) { |
| 241 | return -19; |
| 242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 243 | |
Laurence Lundblade | 6d3f6ec | 2020-07-18 17:28:26 -0700 | [diff] [blame] | 244 | return 0; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 245 | } |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 246 | |
| 247 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 248 | |
| 249 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 250 | int32_t HalfPrecisionAgainstRFCCodeTest() |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 251 | { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 252 | for(uint32_t uHalfP = 0; uHalfP < 0xffff; uHalfP += 60) { |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 253 | unsigned char x[2]; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 254 | x[1] = (uint8_t)(uHalfP & 0xff); |
| 255 | x[0] = (uint8_t)(uHalfP >> 8); // uHalfP is always less than 0xffff |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 256 | double d = decode_half(x); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 257 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 258 | // Contruct the CBOR for the half-precision float by hand |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 259 | UsefulBuf_MAKE_STACK_UB(__xx, 3); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 260 | UsefulOutBuf UOB; |
| 261 | UsefulOutBuf_Init(&UOB, __xx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 262 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 263 | const uint8_t uHalfPrecInitialByte = (uint8_t)(HALF_PREC_FLOAT + (CBOR_MAJOR_TYPE_SIMPLE << 5)); // 0xf9 |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 264 | UsefulOutBuf_AppendByte(&UOB, uHalfPrecInitialByte); // The initial byte for a half-precision float |
| 265 | UsefulOutBuf_AppendUint16(&UOB, (uint16_t)uHalfP); |
| 266 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 267 | // Now parse the hand-constructed CBOR. This will invoke the |
| 268 | // conversion to a float |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 269 | QCBORDecodeContext DC; |
| 270 | QCBORDecode_Init(&DC, UsefulOutBuf_OutUBuf(&UOB), 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 271 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 272 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 273 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 274 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 275 | if(Item.uDataType != QCBOR_TYPE_DOUBLE) { |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 276 | return -1; |
| 277 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 278 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 279 | //printf("%04x QCBOR:%15.15f RFC: %15.15f (%8x)\n", |
| 280 | // uHalfP, Item.val.fnum, d , UsefulBufUtil_CopyFloatToUint32(d)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 281 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 282 | if(isnan(d)) { |
| 283 | // The RFC code uses the native instructions which may or may not |
| 284 | // handle sNaN, qNaN and NaN payloads correctly. This test just |
| 285 | // makes sure it is a NaN and doesn't worry about the type of NaN |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 286 | if(!isnan(Item.val.dfnum)) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 287 | return -3; |
| 288 | } |
| 289 | } else { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 290 | if(Item.val.dfnum != d) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 291 | return -2; |
| 292 | } |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /* |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 300 | Expected output from preferred serialization of some of floating-point numbers |
| 301 | {"zero": 0.0, |
| 302 | "negative zero": -0.0, |
| 303 | "infinitity": Infinity, |
| 304 | "negative infinitity": -Infinity, |
| 305 | "NaN": NaN, |
| 306 | "one": 1.0, |
| 307 | "one third": 0.333251953125, |
| 308 | "largest half-precision": 65504.0, |
| 309 | "largest half-precision point one": 65504.1, |
| 310 | "too-large half-precision": 65536.0, |
| 311 | "smallest half subnormal": 5.960464477539063e-8, |
| 312 | "smallest half normal": 0.00006103515625, |
| 313 | "smallest half normal plus": 0.00006103515625000001, |
| 314 | "smallest normal minus": 0.000030517578125, |
| 315 | "largest single": 3.4028234663852886e+38, |
| 316 | "largest single plus": 6.805646932770577e+38, |
| 317 | "smallest single": 1.1754943508222875e-38, |
| 318 | "smallest single plus": 1.1754943508222878e-38, |
| 319 | "smallest single minus": 1.1754943508222874e-38, |
| 320 | "smallest single minus more": 5.877471754111438e-39, |
| 321 | 3: -2.0, "single precision": 16777216.0, |
| 322 | "single with precision loss": 16777217.0, |
| 323 | 1: "fin"} |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 324 | */ |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 325 | static const uint8_t spExpectedSmallest[] = { |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 326 | 0xB8, 0x1A, |
| 327 | 0x64, 0x7A, 0x65, 0x72, 0x6F, |
| 328 | 0xF9, 0x00, 0x00, |
| 329 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 330 | 0x6D, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x7A, |
| 331 | 0x65, 0x72, 0x6F, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 332 | 0xF9, 0x80, 0x00, |
| 333 | |
| 334 | 0x6A, 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
| 335 | 0xF9, 0x7C, 0x00, |
| 336 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 337 | 0x73, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, |
| 338 | 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 339 | 0xF9, 0xFC, 0x00, |
| 340 | |
| 341 | 0x63, 0x4E, 0x61, 0x4E, |
| 342 | 0xF9, 0x7E, 0x00, |
| 343 | |
| 344 | 0x63, 0x6F, 0x6E, 0x65, |
| 345 | 0xF9, 0x3C, 0x00, |
| 346 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 347 | 0x69, 0x6F, 0x6E, 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 348 | 0xF9, 0x35, 0x55, |
| 349 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 350 | 0x76, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, |
| 351 | 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, |
| 352 | 0x6F, 0x6E, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 353 | 0xF9, 0x7B, 0xFF, |
| 354 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 355 | 0x78, 0x20, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, |
| 356 | 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, |
| 357 | 0x69, 0x6F, 0x6E, 0x20, 0x70, 0x6F, 0x69, 0x6E, 0x74, 0x20, |
| 358 | 0x6F, 0x6E, 0x65, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 359 | 0xFB, 0x40, 0xEF, 0xFC, 0x03, 0x33, 0x33, 0x33, 0x33, |
| 360 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 361 | 0x78, 0x18, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, |
| 362 | 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, |
| 363 | 0x69, 0x73, 0x69, 0x6F, 0x6E, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 364 | 0xFA, 0x47, 0x80, 0x00, 0x00, |
| 365 | |
| 366 | 0x77, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, |
| 367 | 0x20, 0x68, 0x61, 0x6C, 0x66, 0x20, 0x73, 0x75, 0x62, 0x6E, |
| 368 | 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
| 369 | 0xFA, 0x33, 0x80, 0x00, 0x00, |
| 370 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 371 | 0x74, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x68, |
| 372 | 0x61, 0x6C, 0x66, 0x20, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 373 | 0xF9, 0x04, 0x00, |
| 374 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 375 | 0x78, 0x19, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, |
| 376 | 0x68, 0x61, 0x6C, 0x66, 0x20, 0x6E, 0x6F, 0x72, 0x6D, 0x61, |
| 377 | 0x6C, 0x20, 0x70, 0x6C, 0x75, 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 378 | 0xFB, 0x3F, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 379 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 380 | 0x75, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, |
| 381 | 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x6D, 0x69, 0x6E, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 382 | 0x75, 0x73, |
| 383 | 0xFB, 0x3F, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 384 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 385 | 0x75, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, |
| 386 | 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x6D, 0x69, 0x6E, 0x75, |
| 387 | 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 388 | 0xFA, 0x38, 0x00, 0x00, 0x00, |
| 389 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 390 | 0x6E, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x69, |
| 391 | 0x6E, 0x67, 0x6C, 0x65, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 392 | 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, |
| 393 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 394 | 0x73, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x69, |
| 395 | 0x6E,0x67, 0x6C, 0x65, 0x20, 0x70, 0x6C, 0x75, 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 396 | 0xFB, 0x47, 0xEF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x01, |
| 397 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 398 | 0x73, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x69, |
| 399 | 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x70, 0x6C, 0x75, 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 400 | 0xFB, 0x47, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, |
| 401 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 402 | 0x6F, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, |
| 403 | 0x69, 0x6E, 0x67, 0x6C, 0x65, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 404 | 0xFA, 0x00, 0x80, 0x00, 0x00, |
| 405 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 406 | 0x74, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, |
| 407 | 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x70, 0x6C, 0x75, 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 408 | 0xFB, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 409 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 410 | 0x75, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, |
| 411 | 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x6D, 0x69, 0x6E, 0x75, |
| 412 | 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 413 | 0xFB, 0x38, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 414 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 415 | 0x78, 0x1A, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, |
| 416 | 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x6D, 0x69, 0x6E, |
| 417 | 0x75, 0x73, 0x20, 0x6D, 0x6F, 0x72, 0x65, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 418 | 0xFB, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 419 | |
| 420 | 0x03, |
| 421 | 0xF9, 0xC0, 0x00, |
| 422 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 423 | 0x70, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x70, 0x72, 0x65, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 424 | 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E, |
| 425 | 0xFA, 0x4B, 0x80, 0x00, 0x00, |
| 426 | |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 427 | 0x78, 0x1A, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x77, 0x69, |
| 428 | 0x74, 0x68, 0x20, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, |
| 429 | 0x6F, 0x6E, 0x20, 0x6C, 0x6F, 0x73, 0x73, |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 430 | 0xFB, 0x41, 0x70, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, |
| 431 | |
| 432 | 0x01, |
| 433 | 0x63, 0x66, 0x69, 0x6E |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 437 | /* |
| 438 | Makes a double from a uint64_t by copying the bits, not |
| 439 | by converting the value. |
| 440 | */ |
| 441 | #define MAKE_DOUBLE(x) UsefulBufUtil_CopyUint64ToDouble(x) |
| 442 | |
| 443 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 444 | int32_t DoubleAsSmallestTest() |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 445 | { |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 446 | UsefulBuf_MAKE_STACK_UB(EncodedHalfsMem, sizeof(spExpectedSmallest)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 447 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 448 | QCBOREncodeContext EC; |
| 449 | QCBOREncode_Init(&EC, EncodedHalfsMem); |
| 450 | QCBOREncode_OpenMap(&EC); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 451 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 452 | // Many of these are from |
| 453 | // https://en.wikipedia.org/wiki/Half-precision_floating-point_format |
| 454 | // and |
| 455 | // https://en.wikipedia.org/wiki/Single-precision_floating-point_format |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 456 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 457 | // F9 0000 # primitive(0) |
| 458 | QCBOREncode_AddDoubleToMap(&EC, "zero", 0.00); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 459 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 460 | // F9 8000 # primitive(0) |
| 461 | QCBOREncode_AddDoubleToMap(&EC, "negative zero", -0.00); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 462 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 463 | // F9 7C00 # primitive(31744) |
| 464 | QCBOREncode_AddDoubleToMap(&EC, "infinitity", INFINITY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 465 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 466 | // F9 FC00 # primitive(64512) |
| 467 | QCBOREncode_AddDoubleToMap(&EC, "negative infinitity", -INFINITY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 468 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 469 | // F9 7E00 # primitive(32256) |
| 470 | QCBOREncode_AddDoubleToMap(&EC, "NaN", NAN); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 471 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 472 | // TODO: test a few NaN variants |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 473 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 474 | // F9 3C00 # primitive(15360) |
| 475 | QCBOREncode_AddDoubleToMap(&EC, "one", 1.0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 476 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 477 | // F9 3555 # primitive(13653) |
| 478 | QCBOREncode_AddDoubleToMap(&EC, "one third", 0.333251953125); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 479 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 480 | // 65504.0, converts to the large possible half-precision. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 481 | // 0xF9, 0x7B, 0xFF, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 482 | QCBOREncode_AddDoubleToMap(&EC, "largest half-precision", 65504.0); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 483 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 484 | // 65504.1, the double that has both to large an exponent and too |
| 485 | // much precision, so no conversion. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 486 | // 0xFB, 0x40, 0xEF, 0xFC, 0x03, 0x33, 0x33, 0x33, 0x33, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 487 | QCBOREncode_AddDoubleToMap(&EC, "largest half-precision point one", 65504.1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 488 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 489 | // 65536.0 has an exponent of 16, which is larger than 15, the |
| 490 | // largest half-precision exponent. It is the exponent, not |
| 491 | // precision loss that prevents conversion to half. It does convert |
| 492 | // to single precision. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 493 | // 0xFA, 0x47, 0x80, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 494 | QCBOREncode_AddDoubleToMap(&EC, "too-large half-precision", 65536.0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 495 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 496 | // 5.9604644775390625E-8, the smallest possible half-precision |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 497 | // subnormal, digitis are lost converting to half, but not |
| 498 | // when converting to a single |
| 499 | // 0xFA, 0x33, 0x80, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 500 | QCBOREncode_AddDoubleToMap(&EC, |
| 501 | "smallest half subnormal", |
| 502 | MAKE_DOUBLE(0x3e70000000000000)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 503 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 504 | // 0.00006103515625, the double value that converts to the smallest |
| 505 | // possible half-precision normal. which is what should appear in |
| 506 | // the output. |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 507 | // 0xF9, 0x04, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 508 | QCBOREncode_AddDoubleToMap(&EC, |
| 509 | "smallest half normal", |
| 510 | MAKE_DOUBLE(0x3f10000000000000)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 511 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 512 | // 0.000061035156250000014 ,the double value that is a tiny bit |
| 513 | // greater than smallest possible half-precision normal. It will be |
| 514 | // output as a double because converting it will reduce precision. |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 515 | // 0xFB, 0x3F, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 516 | QCBOREncode_AddDoubleToMap(&EC, |
| 517 | "smallest half normal plus", |
| 518 | MAKE_DOUBLE(0x3f10000000000001)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 519 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 520 | // 0.000061035156249999993, the double value that is a tiny bit |
| 521 | // smaller than the smallest half-precision normal. This will fail |
| 522 | // to convert to a half-precision because both the exponent is too |
| 523 | // small and the precision is too large for a half-precision. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 524 | // 0xFB, 0x3F, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 525 | QCBOREncode_AddDoubleToMap(&EC, |
| 526 | "smallest normal minus", |
| 527 | MAKE_DOUBLE(0x3f0fffffffffffff)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 528 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 529 | // 0.000030517578125, the double value that is too small to fit |
| 530 | // into a half-precision because the exponent won't fit, not |
| 531 | // because precision would be lost. (This would fit into a |
| 532 | // half-precision subnormal, but there is no converstion to |
| 533 | // that). This ends up encoded as a single-precision. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 534 | // 0xFA, 0x38, 0x00, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 535 | QCBOREncode_AddDoubleToMap(&EC, |
| 536 | "smallest normal minus", |
| 537 | MAKE_DOUBLE(0x3f00000000000000)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 538 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 539 | // 3.4028234664e38, the value that converts to the largest possible |
| 540 | // single-precision. |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 541 | // 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 542 | QCBOREncode_AddDoubleToMap(&EC, |
| 543 | "largest single", |
| 544 | MAKE_DOUBLE(0x47efffffe0000000)); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 545 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 546 | // 3.402823466385289E38, sightly larger than the largest possible |
| 547 | // possible precision. Conversion fails because precision would be |
| 548 | // lost. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 549 | // 0xFB, 0x47, 0xEF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x01, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 550 | QCBOREncode_AddDoubleToMap(&EC, |
| 551 | "largest single plus", |
| 552 | MAKE_DOUBLE(0x47efffffe0000001)); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 553 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 554 | // 6.8056469327705772E38, slightly more larger than the largers |
| 555 | // possible single precision. Conversion fails because exponent is |
| 556 | // too large. |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 557 | // 0xFB, 0x47, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 558 | QCBOREncode_AddDoubleToMap(&EC, |
| 559 | "largest single plus", |
| 560 | MAKE_DOUBLE(0x47ffffffe0000000)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 561 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 562 | // 1.1754943508222875E-38, The double value that converts to the |
| 563 | // smallest possible single-precision normal |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 564 | // 0xFA, 0x00, 0x80, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 565 | QCBOREncode_AddDoubleToMap(&EC, |
| 566 | "smallest single", |
| 567 | MAKE_DOUBLE(0x3810000000000000)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 568 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 569 | // 1.1754943508222878E-38, double value that is slightly larger |
| 570 | // than the smallest single-precision normal. Conversion fails |
| 571 | // because of precision |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 572 | // 0xFB, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 573 | QCBOREncode_AddDoubleToMap(&EC, |
| 574 | "smallest single plus", |
| 575 | MAKE_DOUBLE(0x3810000000000001)); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 576 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 577 | // 1.1754943508222874E-38, slightly smaller than the smallest |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 578 | // single-precision normal. Conversion fails because of precision |
Laurence Lundblade | 29ec464 | 2020-07-21 21:11:45 -0700 | [diff] [blame] | 579 | // 0xFB, 0x38, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 580 | QCBOREncode_AddDoubleToMap(&EC, |
| 581 | "smallest single minus", |
| 582 | MAKE_DOUBLE(0x380fffffffffffff)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 583 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 584 | // 5.8774717541114375E-39, slightly smaller than the smallest |
| 585 | // single-precision normal. Conversion fails because the exponent |
| 586 | // is too small. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 587 | // 0xFB, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 588 | QCBOREncode_AddDoubleToMap(&EC, |
| 589 | "smallest single minus more", |
| 590 | MAKE_DOUBLE(0x3800000000000000)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 591 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 592 | // Just -2, which converts to a negative half-precision |
| 593 | // F9 C000 # primitive(49152) |
| 594 | QCBOREncode_AddDoubleToMapN(&EC, 3, -2.0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 595 | |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 596 | // 16777216, No precision loss converting to single |
| 597 | // FA 4B800000 # primitive(1266679808) |
| 598 | QCBOREncode_AddDoubleToMap(&EC, "single precision", 16777216); |
| 599 | |
| 600 | // 16777217, One more than above. Too much precision for a single |
| 601 | // so no conversion. |
Laurence Lundblade | 60f30e7 | 2020-07-21 11:16:01 -0700 | [diff] [blame] | 602 | // 0xFB, 0x41, 0x70, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 603 | QCBOREncode_AddDoubleToMap(&EC, "single with precision loss", 16777217); |
| 604 | |
| 605 | // Just a convenient marker when cutting and pasting encoded CBOR |
| 606 | QCBOREncode_AddSZStringToMapN(&EC, 1, "fin"); |
| 607 | |
| 608 | QCBOREncode_CloseMap(&EC); |
| 609 | |
| 610 | UsefulBufC EncodedHalfs; |
| 611 | QCBORError uErr = QCBOREncode_Finish(&EC, &EncodedHalfs); |
| 612 | if(uErr) { |
| 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | if(UsefulBuf_Compare(EncodedHalfs, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedSmallest))) { |
| 617 | return -3; |
| 618 | } |
| 619 | |
| 620 | return 0; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 621 | } |
Laurence Lundblade | 585127a | 2020-07-15 03:25:24 -0700 | [diff] [blame] | 622 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 623 | |
| 624 | |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 625 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 626 | * Some encoded floating point numbers that are used for both |
| 627 | * encode and decode tests. |
| 628 | * |
| 629 | * [0.0, // Half |
| 630 | * 3.14, // Double |
| 631 | * 0.0, // Double |
| 632 | * NaN, // Double |
| 633 | * Infinity, // Double |
| 634 | * 0.0, // Half (Duplicate because of use in encode tests) |
| 635 | * 3.140000104904175, // Single |
| 636 | * 0.0, // Single |
| 637 | * NaN, // Single |
| 638 | * Infinity, // Single |
| 639 | * {100: 0.0, 101: 3.1415926, "euler": 2.718281828459045, 105: 0.0, |
| 640 | * 102: 0.0, 103: 3.141592502593994, "euler2": 2.7182817459106445, 106: 0.0}] |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 641 | */ |
| 642 | static const uint8_t spExpectedFloats[] = { |
| 643 | 0x8B, |
| 644 | 0xF9, 0x00, 0x00, |
| 645 | 0xFB, 0x40, 0x09, 0x1E, 0xB8, 0x51, 0xEB, 0x85, 0x1F, |
| 646 | 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 647 | 0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 648 | 0xFB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 649 | 0xF9, 0x00, 0x00, |
| 650 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 651 | 0xFA, 0x00, 0x00, 0x00, 0x00, |
| 652 | 0xFA, 0x7F, 0xC0, 0x00, 0x00, |
| 653 | 0xFA, 0x7F, 0x80, 0x00, 0x00, |
| 654 | 0xA8, |
| 655 | 0x18, 0x64, |
| 656 | 0xF9, 0x00, 0x00, |
| 657 | 0x18, 0x65, |
| 658 | 0xFB, 0x40, 0x09, 0x21, 0xFB, 0x4D, 0x12, 0xD8, 0x4A, |
| 659 | 0x65, 0x65, 0x75, 0x6C, 0x65, 0x72, |
| 660 | 0xFB, 0x40, 0x05, 0xBF, 0x0A, 0x8B, 0x14, 0x57, 0x69, |
| 661 | 0x18, 0x69, |
| 662 | 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 663 | 0x18, 0x66, |
| 664 | 0xF9, 0x00, 0x00, |
| 665 | 0x18, 0x67, |
| 666 | 0xFA, 0x40, 0x49, 0x0F, 0xDA, |
| 667 | 0x66, 0x65, 0x75, 0x6C, 0x65, 0x72, 0x32, |
| 668 | 0xFA, 0x40, 0x2D, 0xF8, 0x54, |
| 669 | 0x18, 0x6A, |
| 670 | 0xFA, 0x00, 0x00, 0x00, 0x00}; |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 671 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 672 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 673 | static const uint8_t spExpectedFloatsNoHalf[] = { |
Laurence Lundblade | 585127a | 2020-07-15 03:25:24 -0700 | [diff] [blame] | 674 | 0x8B, |
| 675 | 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 676 | 0xFB, 0x40, 0x09, 0x1E, 0xB8, 0x51, 0xEB, 0x85, 0x1F, |
| 677 | 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 678 | 0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 679 | 0xFB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 680 | 0xFA, 0x00, 0x00, 0x00, 0x00, |
| 681 | 0xFA, 0x40, 0x48, 0xF5, 0xC3, |
| 682 | 0xFA, 0x00, 0x00, 0x00, 0x00, |
| 683 | 0xFA, 0x7F, 0xC0, 0x00, 0x00, |
| 684 | 0xFA, 0x7F, 0x80, 0x00, 0x00, |
| 685 | 0xA8, |
| 686 | 0x18, 0x64, |
| 687 | 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 688 | 0x18, 0x65, |
| 689 | 0xFB, 0x40, 0x09, 0x21, 0xFB, 0x4D, 0x12, 0xD8, 0x4A, |
| 690 | 0x65, 0x65, 0x75, 0x6C, 0x65, 0x72, |
| 691 | 0xFB, 0x40, 0x05, 0xBF, 0x0A, 0x8B, 0x14, 0x57, 0x69, |
| 692 | 0x18, 0x69, |
| 693 | 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 694 | 0x18, 0x66, |
| 695 | 0xFA, 0x00, 0x00, 0x00, 0x00, |
| 696 | 0x18, 0x67, |
| 697 | 0xFA, 0x40, 0x49, 0x0F, 0xDA, |
| 698 | 0x66, 0x65, 0x75, 0x6C, 0x65, 0x72, 0x32, |
| 699 | 0xFA, 0x40, 0x2D, 0xF8, 0x54, |
| 700 | 0x18, 0x6A, |
| 701 | 0xFA, 0x00, 0x00, 0x00, 0x00}; |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 702 | |
| 703 | int32_t GeneralFloatEncodeTests() |
| 704 | { |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 705 | UsefulBufC ExpectedFloats; |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 706 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 707 | UsefulBuf_MAKE_STACK_UB(OutBuffer, sizeof(spExpectedFloats)); |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 708 | ExpectedFloats = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedFloats); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 709 | (void)spExpectedFloatsNoHalf; // Avoid unused variable error |
| 710 | #else |
| 711 | UsefulBuf_MAKE_STACK_UB(OutBuffer, sizeof(spExpectedFloatsNoHalf)); |
Laurence Lundblade | b992fdb | 2020-07-20 22:44:11 -0700 | [diff] [blame] | 712 | ExpectedFloats = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedFloatsNoHalf); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 713 | (void)spExpectedFloats; // Avoid unused variable error |
| 714 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 715 | |
| 716 | QCBOREncodeContext EC; |
| 717 | QCBOREncode_Init(&EC, OutBuffer); |
| 718 | QCBOREncode_OpenArray(&EC); |
| 719 | |
| 720 | QCBOREncode_AddDouble(&EC, 0.0); |
| 721 | QCBOREncode_AddDouble(&EC, 3.14); |
| 722 | QCBOREncode_AddDoubleNoPreferred(&EC, 0.0); |
| 723 | QCBOREncode_AddDoubleNoPreferred(&EC, NAN); |
| 724 | QCBOREncode_AddDoubleNoPreferred(&EC, INFINITY); |
| 725 | |
| 726 | QCBOREncode_AddFloat(&EC, 0.0); |
| 727 | QCBOREncode_AddFloat(&EC, 3.14f); |
| 728 | QCBOREncode_AddFloatNoPreferred(&EC, 0.0f); |
| 729 | QCBOREncode_AddFloatNoPreferred(&EC, NAN); |
| 730 | QCBOREncode_AddFloatNoPreferred(&EC, INFINITY); |
| 731 | |
| 732 | QCBOREncode_OpenMap(&EC); |
| 733 | |
| 734 | QCBOREncode_AddDoubleToMapN(&EC, 100, 0.0); |
| 735 | QCBOREncode_AddDoubleToMapN(&EC, 101, 3.1415926); |
| 736 | QCBOREncode_AddDoubleToMap(&EC, "euler", 2.71828182845904523536); |
| 737 | QCBOREncode_AddDoubleNoPreferredToMapN(&EC, 105, 0.0); |
| 738 | |
| 739 | QCBOREncode_AddFloatToMapN(&EC, 102, 0.0f); |
| 740 | QCBOREncode_AddFloatToMapN(&EC, 103, 3.1415926f); |
| 741 | QCBOREncode_AddFloatToMap(&EC, "euler2", 2.71828182845904523536f); |
| 742 | QCBOREncode_AddFloatNoPreferredToMapN(&EC, 106, 0.0f); |
| 743 | |
| 744 | QCBOREncode_CloseMap(&EC); |
| 745 | QCBOREncode_CloseArray(&EC); |
| 746 | |
| 747 | UsefulBufC Encoded; |
| 748 | QCBORError uErr = QCBOREncode_Finish(&EC, &Encoded); |
| 749 | if(uErr) { |
Laurence Lundblade | 585127a | 2020-07-15 03:25:24 -0700 | [diff] [blame] | 750 | return -1; |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 753 | if(UsefulBuf_Compare(Encoded, ExpectedFloats)) { |
Laurence Lundblade | 585127a | 2020-07-15 03:25:24 -0700 | [diff] [blame] | 754 | return -3; |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 761 | /* returns 0 if equivalent, non-zero if not equivalent */ |
| 762 | static int CHECK_EXPECTED_DOUBLE(double val, double expected) |
| 763 | { |
| 764 | double diff = val - expected; |
| 765 | |
| 766 | diff = fabs(diff); |
| 767 | |
| 768 | if(diff > 0.000001) { |
| 769 | return 1; |
| 770 | } else { |
| 771 | return 0; |
| 772 | } |
| 773 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 774 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 775 | |
| 776 | |
| 777 | int32_t GeneralFloatDecodeTests() |
| 778 | { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 779 | QCBORItem Item; |
| 780 | QCBORError uErr; |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 781 | QCBORDecodeContext DC; |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 782 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 783 | UsefulBufC TestData = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedFloats); |
| 784 | QCBORDecode_Init(&DC, TestData, 0); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 785 | |
| 786 | QCBORDecode_GetNext(&DC, &Item); |
| 787 | if(Item.uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 788 | return MakeTestResultCode(0, 1, 0); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 791 | /* 0.0 half-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 792 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 793 | if(uErr != FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 794 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 795 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 796 | || Item.val.dfnum != 0.0 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 797 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 798 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 799 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 800 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 801 | return MakeTestResultCode(0, 2, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 802 | } |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 803 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 804 | /* 3.14 double-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 805 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 806 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 807 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
| 808 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 809 | || Item.val.dfnum != 3.14 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 810 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 811 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 812 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 813 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 814 | return MakeTestResultCode(0, 3, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 817 | /* 0.0 double-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 818 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 819 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 820 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
| 821 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 822 | || Item.val.dfnum != 0.0 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 823 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 824 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 825 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 826 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 827 | return MakeTestResultCode(0, 4, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 830 | /* NaN double-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 831 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 832 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 833 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
| 834 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 835 | || !isnan(Item.val.dfnum) |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 836 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 837 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 838 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 839 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 840 | return MakeTestResultCode(0, 5, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 843 | /* Infinity double-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 844 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 845 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 846 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
| 847 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 848 | || Item.val.dfnum != INFINITY |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 849 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 850 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 851 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 852 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 853 | return MakeTestResultCode(0, 6, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 856 | /* 0.0 half-precision (again) */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 857 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 858 | if(uErr != FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 859 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 860 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 861 | || Item.val.dfnum != 0.0 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 862 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 863 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 864 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 865 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 866 | return MakeTestResultCode(0, 7, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 869 | /* 3.140000104904175 single-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 870 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 871 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 872 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 873 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 874 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 875 | || CHECK_EXPECTED_DOUBLE(3.14, Item.val.dfnum) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 876 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 877 | || Item.uDataType != QCBOR_TYPE_FLOAT |
| 878 | || CHECK_EXPECTED_DOUBLE(3.14, Item.val.fnum) |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 879 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 880 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 881 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 882 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 883 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 884 | return MakeTestResultCode(0, 8, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 887 | /* 0.0 single-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 888 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 889 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 890 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 891 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 892 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 893 | || Item.val.dfnum != 0.0 |
| 894 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 895 | || Item.uDataType != QCBOR_TYPE_FLOAT |
| 896 | || Item.val.fnum != 0.0 |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 897 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 898 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 899 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 900 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 901 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 902 | return MakeTestResultCode(0, 9, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 905 | /* NaN single-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 906 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 907 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 908 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 909 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 910 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 911 | || !isnan(Item.val.dfnum) |
| 912 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 913 | || Item.uDataType != QCBOR_TYPE_FLOAT |
| 914 | || !isnan(Item.val.fnum) |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 915 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 916 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 917 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 918 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 919 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 920 | return MakeTestResultCode(0, 10, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 923 | /* Infinity single-precision */ |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 924 | uErr = QCBORDecode_GetNext(&DC, &Item); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 925 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS) |
| 926 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 927 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 928 | || Item.uDataType != QCBOR_TYPE_DOUBLE |
| 929 | || Item.val.dfnum != INFINITY |
| 930 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 931 | || Item.uDataType != QCBOR_TYPE_FLOAT |
| 932 | || Item.val.fnum != INFINITY |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 933 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 934 | #else /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 935 | || Item.uDataType != QCBOR_TYPE_NONE |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 936 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 937 | ) { |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 938 | return MakeTestResultCode(0, 11, uErr); |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 939 | } |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 940 | /* Sufficent test coverage. Don't need to decode the rest. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 941 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 942 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 943 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 944 | /* Now tests for spiffy decode main function */ |
| 945 | TestData = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedFloats); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 946 | double d; |
| 947 | QCBORDecode_Init(&DC, TestData, 0); |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 948 | QCBORDecode_EnterArray(&DC, NULL); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 949 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 950 | /* 0.0 half-precision */ |
| 951 | QCBORDecode_GetDouble(&DC, &d); |
| 952 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 953 | if(uErr != FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 954 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 955 | || d != 0.0 |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 956 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame^] | 957 | ) { |
| 958 | return MakeTestResultCode(1, 1, uErr); |
| 959 | } |
| 960 | |
| 961 | /* 3.14 double-precision */ |
| 962 | QCBORDecode_GetDouble(&DC, &d); |
| 963 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 964 | if(uErr != QCBOR_SUCCESS || d != 3.14) { |
| 965 | return MakeTestResultCode(1, 2, uErr); |
| 966 | } |
| 967 | |
| 968 | /* 0.0 double-precision */ |
| 969 | QCBORDecode_GetDouble(&DC, &d); |
| 970 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 971 | if(uErr != QCBOR_SUCCESS || d != 0.0) { |
| 972 | return MakeTestResultCode(1, 3, uErr); |
| 973 | } |
| 974 | |
| 975 | /* NaN double-precision */ |
| 976 | QCBORDecode_GetDouble(&DC, &d); |
| 977 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 978 | if(uErr != QCBOR_SUCCESS || !isnan(d)) { |
| 979 | return MakeTestResultCode(1, 4, uErr); |
| 980 | } |
| 981 | |
| 982 | /* Infinity double-precision */ |
| 983 | QCBORDecode_GetDouble(&DC, &d); |
| 984 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 985 | if(uErr != QCBOR_SUCCESS || d != INFINITY) { |
| 986 | return MakeTestResultCode(1, 5, uErr); |
| 987 | } |
| 988 | |
| 989 | /* 0.0 half-precision */ |
| 990 | QCBORDecode_GetDouble(&DC, &d); |
| 991 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 992 | if(uErr != FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS) |
| 993 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 994 | || d != 0.0 |
| 995 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 996 | ) { |
| 997 | return MakeTestResultCode(1, 6, uErr); |
| 998 | } |
| 999 | |
| 1000 | /* 3.140000104904175 single-precision */ |
| 1001 | QCBORDecode_GetDouble(&DC, &d); |
| 1002 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 1003 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 1004 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 1005 | || d != 3.140000104904175 |
| 1006 | #endif |
| 1007 | ) { |
| 1008 | return MakeTestResultCode(1, 7, uErr); |
| 1009 | } |
| 1010 | |
| 1011 | /* 0.0 single-precision */ |
| 1012 | QCBORDecode_GetDouble(&DC, &d); |
| 1013 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 1014 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 1015 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 1016 | || d != 0.0 |
| 1017 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 1018 | ) { |
| 1019 | return MakeTestResultCode(1, 8, uErr); |
| 1020 | } |
| 1021 | |
| 1022 | /* NaN single-precision */ |
| 1023 | QCBORDecode_GetDouble(&DC, &d); |
| 1024 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 1025 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 1026 | || !isnan(d) |
| 1027 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 1028 | ) { |
| 1029 | return MakeTestResultCode(1, 9, uErr); |
| 1030 | } |
| 1031 | |
| 1032 | /* Infinity single-precision */ |
| 1033 | QCBORDecode_GetDouble(&DC, &d); |
| 1034 | uErr = QCBORDecode_GetAndResetError(&DC); |
| 1035 | if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS) |
| 1036 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 1037 | || d != INFINITY |
| 1038 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 1039 | ) { |
| 1040 | return MakeTestResultCode(1, 10, uErr); |
| 1041 | } |
| 1042 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1043 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1044 | |
Laurence Lundblade | 02fcf31 | 2020-07-17 02:49:46 -0700 | [diff] [blame] | 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 1049 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 1050 | #ifdef NAN_EXPERIMENT |
| 1051 | /* |
| 1052 | Code for checking what the double to float cast does with |
| 1053 | NaNs. Not run as part of tests. Keep it around to |
| 1054 | be able to check various platforms and CPUs. |
| 1055 | */ |
| 1056 | |
| 1057 | #define DOUBLE_NUM_SIGNIFICAND_BITS (52) |
| 1058 | #define DOUBLE_NUM_EXPONENT_BITS (11) |
| 1059 | #define DOUBLE_NUM_SIGN_BITS (1) |
| 1060 | |
| 1061 | #define DOUBLE_SIGNIFICAND_SHIFT (0) |
| 1062 | #define DOUBLE_EXPONENT_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS) |
| 1063 | #define DOUBLE_SIGN_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS + DOUBLE_NUM_EXPONENT_BITS) |
| 1064 | |
| 1065 | #define DOUBLE_SIGNIFICAND_MASK (0xfffffffffffffULL) // The lower 52 bits |
| 1066 | #define DOUBLE_EXPONENT_MASK (0x7ffULL << DOUBLE_EXPONENT_SHIFT) // 11 bits of exponent |
| 1067 | #define DOUBLE_SIGN_MASK (0x01ULL << DOUBLE_SIGN_SHIFT) // 1 bit of sign |
| 1068 | #define DOUBLE_QUIET_NAN_BIT (0x01ULL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) |
| 1069 | |
| 1070 | |
| 1071 | static int NaNExperiments() { |
| 1072 | double dqNaN = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | DOUBLE_QUIET_NAN_BIT); |
| 1073 | double dsNaN = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | 0x01); |
| 1074 | double dqNaNPayload = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | DOUBLE_QUIET_NAN_BIT | 0xf00f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1075 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 1076 | float f1 = (float)dqNaN; |
| 1077 | float f2 = (float)dsNaN; |
| 1078 | float f3 = (float)dqNaNPayload; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1079 | |
| 1080 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 1081 | uint32_t uqNaN = UsefulBufUtil_CopyFloatToUint32((float)dqNaN); |
| 1082 | uint32_t usNaN = UsefulBufUtil_CopyFloatToUint32((float)dsNaN); |
| 1083 | uint32_t uqNaNPayload = UsefulBufUtil_CopyFloatToUint32((float)dqNaNPayload); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1084 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 1085 | // Result of this on x86 is that every NaN is a qNaN. The intel |
| 1086 | // CVTSD2SS instruction ignores the NaN payload and even converts |
| 1087 | // a sNaN to a qNaN. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1088 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 1089 | return 0; |
| 1090 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1091 | #endif /* NAN_EXPERIMENT */ |