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. |
Laurence Lundblade | 035bd78 | 2019-01-21 17:01:31 -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 9/19/18 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 11 | =============================================================================*/ |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 12 | |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame^] | 13 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 14 | |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 15 | #include "float_tests.h" |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 16 | #include "qcbor/qcbor_encode.h" |
| 17 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 18 | #include "half_to_double_from_rfc7049.h" |
| 19 | #include <math.h> // For INFINITY and NAN and isnan() |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 20 | |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 21 | |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 22 | |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 23 | static const uint8_t spExpectedHalf[] = { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 24 | 0xB1, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 25 | 0x64, |
| 26 | 0x7A, 0x65, 0x72, 0x6F, |
| 27 | 0xF9, 0x00, 0x00, // 0.000 |
| 28 | 0x6A, |
| 29 | 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
| 30 | 0xF9, 0x7C, 0x00, // Infinity |
| 31 | 0x73, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, 0x6E, |
| 33 | 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 34 | 0xF9, 0xFC, 0x00, // -Inifinity |
| 35 | 0x63, |
| 36 | 0x4E, 0x61, 0x4E, |
| 37 | 0xF9, 0x7E, 0x00, // NaN |
| 38 | 0x63, |
| 39 | 0x6F, 0x6E, 0x65, |
| 40 | 0xF9, 0x3C, 0x00, // 1.0 |
| 41 | 0x69, |
| 42 | 0x6F, 0x6E, 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64, |
| 43 | 0xF9, 0x35, 0x55, // 0.333251953125 |
| 44 | 0x76, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 45 | 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C, |
| 46 | 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 47 | 0xF9, 0x7B, 0xFF, // 65504.0 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 48 | 0x78, 0x18, |
| 49 | 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x68, |
| 50 | 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, |
| 51 | 0x6F, 0x6E, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 52 | 0xF9, 0x7C, 0x00, // Infinity |
| 53 | 0x72, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 54 | 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, |
| 55 | 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 56 | 0xF9, 0x00, 0x01, // 0.000000059604 |
| 57 | 0x6F, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 58 | 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F, |
| 59 | 0x72, 0x6D, 0x61, 0x6C, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 60 | 0xF9, 0x03, 0xFF, // 0.0000609755516 |
| 61 | 0x71, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 62 | 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62, |
| 63 | 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 64 | 0xF9, 0x04, 0x00, // 0.000061988 |
| 65 | 0x70, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 66 | 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x73, |
| 67 | 0x69, 0x6E, 0x67, 0x6C, 0x65, |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 68 | 0xF9, 0x00, 0x00, |
| 69 | 0x03, |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 70 | 0xF9, 0xC0, 0x00, // -2 |
| 71 | 0x04, |
| 72 | 0xF9, 0x7E, 0x00, // qNaN |
| 73 | 0x05, |
| 74 | 0xF9, 0x7C, 0x01, // sNaN |
| 75 | 0x06, |
| 76 | 0xF9, 0x7E, 0x0F, // qNaN with payload 0x0f |
| 77 | 0x07, |
| 78 | 0xF9, 0x7C, 0x0F, // sNaN with payload 0x0f |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 79 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 83 | int32_t HalfPrecisionDecodeBasicTests() |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 84 | { |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 85 | UsefulBufC HalfPrecision = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedHalf); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 86 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 87 | QCBORDecodeContext DC; |
| 88 | QCBORDecode_Init(&DC, HalfPrecision, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 89 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 90 | QCBORItem Item; |
| 91 | |
| 92 | QCBORDecode_GetNext(&DC, &Item); |
| 93 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 98 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 99 | return -2; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 100 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 101 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 102 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 103 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 104 | return -3; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 108 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -INFINITY) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 109 | return -4; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 112 | // TODO, is this really converting right? It is carrying payload, but |
| 113 | // this confuses things. |
| 114 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 115 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || !isnan(Item.val.dfnum)) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 116 | return -5; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 120 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 1.0F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 121 | return -6; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 122 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 123 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 124 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 125 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.333251953125F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 126 | return -7; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 130 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 65504.0F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 131 | return -8; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 135 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 136 | return -9; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 137 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 138 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 139 | QCBORDecode_GetNext(&DC, &Item); // TODO: check this |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 140 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000000596046448F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 141 | return -10; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | QCBORDecode_GetNext(&DC, &Item); // TODO: check this |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 145 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000609755516F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 146 | return -11; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | QCBORDecode_GetNext(&DC, &Item); // TODO check this |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 150 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000610351563F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 151 | return -12; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 152 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 153 | |
| 154 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 155 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 156 | return -13; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 157 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 158 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 159 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 160 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -2.0F) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 161 | return -14; |
| 162 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 163 | |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 164 | // TODO: double check these four tests |
| 165 | QCBORDecode_GetNext(&DC, &Item); // qNaN |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 166 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 167 | UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff8000000000000ULL) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 168 | return -15; |
| 169 | } |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 170 | QCBORDecode_GetNext(&DC, &Item); // sNaN |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 171 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 172 | UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff0000000000001ULL) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 173 | return -16; |
| 174 | } |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 175 | QCBORDecode_GetNext(&DC, &Item); // qNaN with payload 0x0f |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 176 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 177 | UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff800000000000fULL) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 178 | return -17; |
| 179 | } |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 180 | QCBORDecode_GetNext(&DC, &Item); // sNaN with payload 0x0f |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 181 | if(Item.uDataType != QCBOR_TYPE_DOUBLE || |
| 182 | UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff000000000000fULL) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 183 | return -18; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 184 | } |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 185 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 186 | if(QCBORDecode_Finish(&DC)) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 187 | return -19; |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 188 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 189 | |
Laurence Lundblade | 68a1335 | 2018-09-23 02:19:54 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 192 | |
| 193 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 194 | |
| 195 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 196 | int32_t HalfPrecisionAgainstRFCCodeTest() |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 197 | { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 198 | for(uint32_t uHalfP = 0; uHalfP < 0xffff; uHalfP += 60) { |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 199 | unsigned char x[2]; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 200 | x[1] = (uint8_t)(uHalfP & 0xff); |
| 201 | x[0] = (uint8_t)(uHalfP >> 8); // uHalfP is always less than 0xffff |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 202 | double d = decode_half(x); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 203 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 204 | // Contruct the CBOR for the half-precision float by hand |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 205 | UsefulBuf_MAKE_STACK_UB(__xx, 3); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 206 | UsefulOutBuf UOB; |
| 207 | UsefulOutBuf_Init(&UOB, __xx); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 208 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 209 | 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] | 210 | UsefulOutBuf_AppendByte(&UOB, uHalfPrecInitialByte); // The initial byte for a half-precision float |
| 211 | UsefulOutBuf_AppendUint16(&UOB, (uint16_t)uHalfP); |
| 212 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 213 | // Now parse the hand-constructed CBOR. This will invoke the |
| 214 | // conversion to a float |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 215 | QCBORDecodeContext DC; |
| 216 | QCBORDecode_Init(&DC, UsefulOutBuf_OutUBuf(&UOB), 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 217 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 218 | QCBORItem Item; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 219 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 220 | QCBORDecode_GetNext(&DC, &Item); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 221 | if(Item.uDataType != QCBOR_TYPE_DOUBLE) { |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 222 | return -1; |
| 223 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 224 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 225 | //printf("%04x QCBOR:%15.15f RFC: %15.15f (%8x)\n", |
| 226 | // uHalfP, Item.val.fnum, d , UsefulBufUtil_CopyFloatToUint32(d)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 227 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 228 | if(isnan(d)) { |
| 229 | // The RFC code uses the native instructions which may or may not |
| 230 | // handle sNaN, qNaN and NaN payloads correctly. This test just |
| 231 | // 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] | 232 | if(!isnan(Item.val.dfnum)) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 233 | return -3; |
| 234 | } |
| 235 | } else { |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 236 | if(Item.val.dfnum != d) { |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 237 | return -2; |
| 238 | } |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /* |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 246 | {"zero": 0.0, |
| 247 | "negative zero": -0.0, |
| 248 | "infinitity": Infinity, |
| 249 | "negative infinitity": -Infinity, |
| 250 | "NaN": NaN, |
| 251 | "one": 1.0, |
| 252 | "one third": 0.333251953125, |
| 253 | "largest half-precision": 65504.0, |
| 254 | "largest half-precision point one": 65504.1, |
| 255 | "too-large half-precision": 65536.0, |
| 256 | "smallest subnormal": 5.96046448e-8, |
| 257 | "smallest normal": 0.00006103515261202119, |
| 258 | "biggest subnormal": 0.00006103515625, |
| 259 | "subnormal single": 4.00000646641519e-40, |
| 260 | 3: -2.0, |
| 261 | "large single exp": 2.5521177519070385e+38, |
| 262 | "too-large single exp": 5.104235503814077e+38, |
| 263 | "biggest single with prec": 16777216.0, |
| 264 | "first single with prec loss": 16777217.0, |
| 265 | 1: "fin"} |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 266 | */ |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 267 | static const uint8_t spExpectedSmallest[] = { |
| 268 | 0xB4, 0x64, 0x7A, 0x65, 0x72, 0x6F, 0xF9, 0x00, 0x00, 0x6D, |
| 269 | 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x7A, |
| 270 | 0x65, 0x72, 0x6F, 0xF9, 0x80, 0x00, 0x6A, 0x69, 0x6E, 0x66, |
| 271 | 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, 0xF9, 0x7C, 0x00, |
| 272 | 0x73, 0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, |
| 273 | 0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79, |
| 274 | 0xF9, 0xFC, 0x00, 0x63, 0x4E, 0x61, 0x4E, 0xF9, 0x7E, 0x00, |
| 275 | 0x63, 0x6F, 0x6E, 0x65, 0xF9, 0x3C, 0x00, 0x69, 0x6F, 0x6E, |
| 276 | 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64, 0xF9, 0x35, 0x55, |
| 277 | 0x76, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, |
| 278 | 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, |
| 279 | 0x69, 0x6F, 0x6E, 0xF9, 0x7B, 0xFF, 0x78, 0x20, 0x6C, 0x61, |
| 280 | 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C, 0x66, |
| 281 | 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E, |
| 282 | 0x20, 0x70, 0x6F, 0x69, 0x6E, 0x74, 0x20, 0x6F, 0x6E, 0x65, |
| 283 | 0xFB, 0x40, 0xEF, 0xFC, 0x03, 0x33, 0x33, 0x33, 0x33, 0x78, |
| 284 | 0x18, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, |
| 285 | 0x20, 0x68, 0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, |
| 286 | 0x69, 0x73, 0x69, 0x6F, 0x6E, 0xFA, 0x47, 0x80, 0x00, 0x00, |
| 287 | 0x72, 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, |
| 288 | 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0xFB, |
| 289 | 0x3E, 0x70, 0x00, 0x00, 0x00, 0x1C, 0x5F, 0x68, 0x6F, 0x73, |
| 290 | 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F, |
| 291 | 0x72, 0x6D, 0x61, 0x6C, 0xFA, 0x38, 0x7F, 0xFF, 0xFF, 0x71, |
| 292 | 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, |
| 293 | 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0xF9, 0x04, 0x00, |
| 294 | 0x70, 0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, |
| 295 | 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0xFB, 0x37, 0xC1, |
| 296 | 0x6C, 0x28, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF9, 0xC0, 0x00, |
| 297 | 0x70, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x73, 0x69, 0x6E, |
| 298 | 0x67, 0x6C, 0x65, 0x20, 0x65, 0x78, 0x70, 0xFA, 0x7F, 0x40, |
| 299 | 0x00, 0x00, 0x74, 0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, |
| 300 | 0x67, 0x65, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, |
| 301 | 0x65, 0x78, 0x70, 0xFB, 0x47, 0xF8, 0x00, 0x00, 0x00, 0x00, |
| 302 | 0x00, 0x00, 0x78, 0x18, 0x62, 0x69, 0x67, 0x67, 0x65, 0x73, |
| 303 | 0x74, 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x77, |
| 304 | 0x69, 0x74, 0x68, 0x20, 0x70, 0x72, 0x65, 0x63, 0xFA, 0x4B, |
| 305 | 0x80, 0x00, 0x00, 0x78, 0x1B, 0x66, 0x69, 0x72, 0x73, 0x74, |
| 306 | 0x20, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x77, 0x69, |
| 307 | 0x74, 0x68, 0x20, 0x70, 0x72, 0x65, 0x63, 0x20, 0x6C, 0x6F, |
| 308 | 0x73, 0x73, 0xFB, 0x41, 0x70, 0x00, 0x00, 0x10, 0x00, 0x00, |
| 309 | 0x00, 0x01, 0x63, 0x66, 0x69, 0x6E |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 313 | int32_t DoubleAsSmallestTest() |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 314 | { |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 315 | UsefulBuf_MAKE_STACK_UB(EncodedHalfsMem, 420); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 316 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 317 | #define QCBOREncode_AddDoubleAsSmallestToMap QCBOREncode_AddDoubleToMap |
| 318 | #define QCBOREncode_AddDoubleAsSmallestToMapN QCBOREncode_AddDoubleToMapN |
| 319 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 320 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 321 | QCBOREncodeContext EC; |
| 322 | QCBOREncode_Init(&EC, EncodedHalfsMem); |
| 323 | // These are mostly from https://en.wikipedia.org/wiki/Half-precision_floating-point_format |
| 324 | QCBOREncode_OpenMap(&EC); |
| 325 | // 64 # text(4) |
| 326 | // 7A65726F # "zero" |
| 327 | // F9 0000 # primitive(0) |
| 328 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "zero", 0.00); |
| 329 | |
| 330 | // 64 # text(4) |
| 331 | // 7A65726F # "negative zero" |
| 332 | // F9 8000 # primitive(0) |
| 333 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "negative zero", -0.00); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 334 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 335 | // 6A # text(10) |
| 336 | // 696E66696E6974697479 # "infinitity" |
| 337 | // F9 7C00 # primitive(31744) |
| 338 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "infinitity", INFINITY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 339 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 340 | // 73 # text(19) |
| 341 | // 6E6567617469766520696E66696E6974697479 # "negative infinitity" |
| 342 | // F9 FC00 # primitive(64512) |
| 343 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "negative infinitity", -INFINITY); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 344 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 345 | // 63 # text(3) |
| 346 | // 4E614E # "NaN" |
| 347 | // F9 7E00 # primitive(32256) |
| 348 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "NaN", NAN); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 349 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 350 | // TODO: test a few NaN variants |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 351 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 352 | // 63 # text(3) |
| 353 | // 6F6E65 # "one" |
| 354 | // F9 3C00 # primitive(15360) |
| 355 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one", 1.0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 356 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 357 | // 69 # text(9) |
| 358 | // 6F6E65207468697264 # "one third" |
| 359 | // F9 3555 # primitive(13653) |
| 360 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one third", 0.333251953125); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 361 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 362 | // 76 # text(22) |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 363 | // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision" |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 364 | // F9 7BFF # primitive(31743) |
| 365 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision",65504.0); |
| 366 | |
| 367 | // 76 # text(22) |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 368 | // 6C6172676573742068616C662D707265636973696F6E # "largest half-precision" |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 369 | // F9 7BFF # primitive(31743) |
| 370 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision point one",65504.1); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 371 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 372 | // Float 65536.0F is 0x47800000 in hex. It has an exponent of 16, which |
| 373 | // is larger than 15, the largest half-precision exponent |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 374 | // 78 18 # text(24) |
| 375 | // 746F6F2D6C617267652068616C662D707265636973696F6E # "too-large half-precision" |
| 376 | // FA 47800000 # primitive(31743) |
| 377 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large half-precision", 65536.0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 378 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 379 | // The smallest possible half-precision subnormal, but digitis are lost converting |
| 380 | // to half, so this turns into a double |
| 381 | // 72 # text(18) |
| 382 | // 736D616C6C657374207375626E6F726D616C # "smallest subnormal" |
| 383 | // FB 3E700000001C5F68 # primitive(4499096027744984936) |
| 384 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "smallest subnormal", 0.0000000596046448); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 385 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 386 | // The smallest possible half-precision snormal, but digitis are lost converting |
| 387 | // to half, so this turns into a single TODO: confirm this is right |
| 388 | // 6F # text(15) |
| 389 | // 736D616C6C657374206E6F726D616C # "smallest normal" |
| 390 | // FA 387FFFFF # primitive(947912703) |
| 391 | // in hex single is 0x387fffff, exponent -15, significand 7fffff |
| 392 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "smallest normal", 0.0000610351526F); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 393 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 394 | // 71 # text(17) |
| 395 | // 62696767657374207375626E6F726D616C # "biggest subnormal" |
| 396 | // F9 0400 # primitive(1024) |
| 397 | // in hex single is 0x38800000, exponent -14, significand 0 |
| 398 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest subnormal", 0.0000610351563F); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 399 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 400 | // 70 # text(16) |
| 401 | // 7375626E6F726D616C2073696E676C65 # "subnormal single" |
| 402 | // FB 37C16C2800000000 # primitive(4017611261645684736) |
| 403 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "subnormal single", 4e-40F); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 404 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 405 | // 03 # unsigned(3) |
| 406 | // F9 C000 # primitive(49152) |
| 407 | QCBOREncode_AddDoubleAsSmallestToMapN(&EC, 3, -2.0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 408 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 409 | // 70 # text(16) |
| 410 | // 6C617267652073696E676C6520657870 # "large single exp" |
| 411 | // FA 7F400000 # primitive(2134900736) |
| 412 | // (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((127LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT); |
| 413 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "large single exp", 2.5521177519070385E+38); // Exponent fits single |
| 414 | |
| 415 | // 74 # text(20) |
| 416 | // 746F6F2D6C617267652073696E676C6520657870 # "too-large single exp" |
| 417 | // FB 47F8000000000000 # primitive(5185894970917126144) |
| 418 | // (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((128LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 419 | // Exponent too large for single |
| 420 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large single exp", 5.104235503814077E+38); |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 421 | |
| 422 | // 66 # text(6) |
| 423 | // 646664666465 # "dfdfde" |
| 424 | // FA 4B800000 # primitive(1266679808) |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 425 | // Single with no precision loss |
| 426 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest single with prec", 16777216); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 427 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 428 | // 78 18 # text(24) |
| 429 | // 626967676573742073696E676C6520776974682070726563 # "biggest single with prec" |
| 430 | // FA 4B800000 # primitive(1266679808) |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 431 | // Double becuase of precision loss |
| 432 | QCBOREncode_AddDoubleAsSmallestToMap(&EC, "first single with prec loss", 16777217); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 433 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 434 | // Just a convenient marker when cutting and pasting encoded CBOR |
| 435 | QCBOREncode_AddSZStringToMapN(&EC, 1, "fin"); |
| 436 | |
| 437 | QCBOREncode_CloseMap(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 438 | |
Laurence Lundblade | 781fd82 | 2018-10-01 09:37:52 -0700 | [diff] [blame] | 439 | UsefulBufC EncodedHalfs; |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 440 | QCBORError uErr = QCBOREncode_Finish(&EC, &EncodedHalfs); |
| 441 | if(uErr) { |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 442 | return -1; |
| 443 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 444 | |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 445 | if(UsefulBuf_Compare(EncodedHalfs, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedSmallest))) { |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 446 | return -3; |
| 447 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 448 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 449 | return 0; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 450 | } |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 451 | |
| 452 | |
| 453 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 454 | #ifdef NAN_EXPERIMENT |
| 455 | /* |
| 456 | Code for checking what the double to float cast does with |
| 457 | NaNs. Not run as part of tests. Keep it around to |
| 458 | be able to check various platforms and CPUs. |
| 459 | */ |
| 460 | |
| 461 | #define DOUBLE_NUM_SIGNIFICAND_BITS (52) |
| 462 | #define DOUBLE_NUM_EXPONENT_BITS (11) |
| 463 | #define DOUBLE_NUM_SIGN_BITS (1) |
| 464 | |
| 465 | #define DOUBLE_SIGNIFICAND_SHIFT (0) |
| 466 | #define DOUBLE_EXPONENT_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS) |
| 467 | #define DOUBLE_SIGN_SHIFT (DOUBLE_NUM_SIGNIFICAND_BITS + DOUBLE_NUM_EXPONENT_BITS) |
| 468 | |
| 469 | #define DOUBLE_SIGNIFICAND_MASK (0xfffffffffffffULL) // The lower 52 bits |
| 470 | #define DOUBLE_EXPONENT_MASK (0x7ffULL << DOUBLE_EXPONENT_SHIFT) // 11 bits of exponent |
| 471 | #define DOUBLE_SIGN_MASK (0x01ULL << DOUBLE_SIGN_SHIFT) // 1 bit of sign |
| 472 | #define DOUBLE_QUIET_NAN_BIT (0x01ULL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) |
| 473 | |
| 474 | |
| 475 | static int NaNExperiments() { |
| 476 | double dqNaN = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | DOUBLE_QUIET_NAN_BIT); |
| 477 | double dsNaN = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | 0x01); |
| 478 | double dqNaNPayload = UsefulBufUtil_CopyUint64ToDouble(DOUBLE_EXPONENT_MASK | DOUBLE_QUIET_NAN_BIT | 0xf00f); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 479 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 480 | float f1 = (float)dqNaN; |
| 481 | float f2 = (float)dsNaN; |
| 482 | float f3 = (float)dqNaNPayload; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 483 | |
| 484 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 485 | uint32_t uqNaN = UsefulBufUtil_CopyFloatToUint32((float)dqNaN); |
| 486 | uint32_t usNaN = UsefulBufUtil_CopyFloatToUint32((float)dsNaN); |
| 487 | uint32_t uqNaNPayload = UsefulBufUtil_CopyFloatToUint32((float)dqNaNPayload); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 488 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 489 | // Result of this on x86 is that every NaN is a qNaN. The intel |
| 490 | // CVTSD2SS instruction ignores the NaN payload and even converts |
| 491 | // a sNaN to a qNaN. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 492 | |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | #endif |
| 496 | |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame^] | 497 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 7d40d81 | 2018-09-30 02:44:01 -0700 | [diff] [blame] | 498 | |
Laurence Lundblade | d711fb2 | 2018-09-26 14:35:22 -0700 | [diff] [blame] | 499 | |