[88e7dc5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Matthieu Riolo
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <pcut/pcut.h>
|
---|
| 30 | #include <ieee_double.h>
|
---|
| 31 |
|
---|
| 32 | union {
|
---|
| 33 | uint64_t integer;
|
---|
| 34 | double floating;
|
---|
| 35 | } ieee_double_bits;
|
---|
| 36 |
|
---|
| 37 | const int EXP_BIAS = 1023;
|
---|
| 38 | const int EXP_BIAS_UNDERFLOWED = 1075;
|
---|
| 39 | const int SIGN_SHIFT = 52;
|
---|
| 40 | const uint64_t HIDDEN_BIT = 1ULL << SIGN_SHIFT;
|
---|
| 41 |
|
---|
| 42 | PCUT_INIT;
|
---|
| 43 |
|
---|
| 44 | PCUT_TEST_SUITE(ieee_double);
|
---|
| 45 |
|
---|
| 46 | PCUT_TEST(extract_ieee_sizeof_double)
|
---|
| 47 | {
|
---|
| 48 | PCUT_ASSERT_INT_EQUALS(8, sizeof(double));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | PCUT_TEST(extract_ieee_double_pos_infinity)
|
---|
| 52 | {
|
---|
| 53 | ieee_double_bits.integer = 0x7FF0000000000000ULL;
|
---|
| 54 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 55 |
|
---|
| 56 | PCUT_ASSERT_TRUE(d.is_special);
|
---|
| 57 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 58 | PCUT_ASSERT_TRUE(d.is_infinity);
|
---|
| 59 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 60 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 61 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 62 |
|
---|
| 63 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
| 64 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.exponent);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | PCUT_TEST(extract_ieee_double_neg_infinity)
|
---|
| 68 | {
|
---|
| 69 | ieee_double_bits.integer = 0xFFF0000000000000ULL;
|
---|
| 70 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 71 |
|
---|
| 72 | PCUT_ASSERT_TRUE(d.is_special);
|
---|
| 73 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 74 | PCUT_ASSERT_TRUE(d.is_infinity);
|
---|
| 75 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 76 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 77 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 78 |
|
---|
| 79 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
| 80 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.exponent);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | PCUT_TEST(extract_ieee_double_nan)
|
---|
| 84 | {
|
---|
| 85 | ieee_double_bits.integer = 0xFFFFFFFFFFFFFFFFULL;
|
---|
| 86 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 87 |
|
---|
| 88 | PCUT_ASSERT_TRUE(d.is_special);
|
---|
| 89 | PCUT_ASSERT_TRUE(d.is_nan);
|
---|
| 90 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 91 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 92 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 93 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 94 |
|
---|
| 95 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
| 96 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.exponent);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | PCUT_TEST(extract_ieee_double_pos_zero)
|
---|
| 100 | {
|
---|
| 101 | ieee_double_bits.integer = 0x0000000000000000ULL;
|
---|
| 102 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 103 |
|
---|
| 104 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 105 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 106 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 107 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 108 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 109 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 110 |
|
---|
| 111 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | PCUT_TEST(extract_ieee_double_neg_zero)
|
---|
| 115 | {
|
---|
| 116 | ieee_double_bits.integer = 0x8000000000000000ULL;
|
---|
| 117 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 118 |
|
---|
| 119 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 120 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 121 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 122 |
|
---|
| 123 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 124 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 125 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 126 |
|
---|
| 127 | PCUT_ASSERT_INT_EQUALS(0, d.pos_val.significand);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | PCUT_TEST(extract_ieee_double_normal_pos_one)
|
---|
| 131 | {
|
---|
| 132 | ieee_double_bits.integer = 0x3FF0000000000000ULL;
|
---|
| 133 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 134 |
|
---|
| 135 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 136 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 137 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 138 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 139 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
| 140 | PCUT_ASSERT_TRUE(d.is_accuracy_step);
|
---|
| 141 |
|
---|
| 142 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
| 143 | PCUT_ASSERT_INT_EQUALS(EXP_BIAS - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | PCUT_TEST(extract_ieee_double_normal_neg_one)
|
---|
| 147 | {
|
---|
| 148 | ieee_double_bits.integer = 0xBFF0000000000000ULL;
|
---|
| 149 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 150 |
|
---|
| 151 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 152 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 153 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 154 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 155 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
| 156 | PCUT_ASSERT_TRUE(d.is_accuracy_step);
|
---|
| 157 |
|
---|
| 158 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
| 159 | PCUT_ASSERT_INT_EQUALS(EXP_BIAS - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | PCUT_TEST(extract_ieee_double_denormal_pos_smallest)
|
---|
| 163 | {
|
---|
| 164 | ieee_double_bits.integer = 0x0000000000000001ULL;
|
---|
| 165 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 166 |
|
---|
| 167 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 168 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 169 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 170 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 171 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 172 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 173 |
|
---|
| 174 | PCUT_ASSERT_INT_EQUALS(1, d.pos_val.significand);
|
---|
| 175 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | PCUT_TEST(extract_ieee_double_denormal_neg_smallest)
|
---|
| 179 | {
|
---|
| 180 | ieee_double_bits.integer = 0x8000000000000001ULL;
|
---|
| 181 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 182 |
|
---|
| 183 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 184 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 185 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 186 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 187 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 188 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 189 |
|
---|
| 190 | PCUT_ASSERT_INT_EQUALS(1, d.pos_val.significand);
|
---|
| 191 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | PCUT_TEST(extract_ieee_double_denormal_pos_largest)
|
---|
| 195 | {
|
---|
| 196 | ieee_double_bits.integer = 0x000FFFFFFFFFFFFFULL;
|
---|
| 197 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 198 |
|
---|
| 199 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 200 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 201 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 202 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 203 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 204 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 205 |
|
---|
| 206 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL, d.pos_val.significand);
|
---|
| 207 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | PCUT_TEST(extract_ieee_double_denormal_neg_largest)
|
---|
| 211 | {
|
---|
| 212 | ieee_double_bits.integer = 0x800FFFFFFFFFFFFFULL;
|
---|
| 213 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 214 |
|
---|
| 215 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 216 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 217 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 218 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 219 | PCUT_ASSERT_TRUE(d.is_denormal);
|
---|
| 220 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 221 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL, d.pos_val.significand);
|
---|
| 222 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | PCUT_TEST(extract_ieee_double_normal_pos_smallest)
|
---|
| 226 | {
|
---|
| 227 | ieee_double_bits.integer = 0x0010000000000000ULL;
|
---|
| 228 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 229 |
|
---|
| 230 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 231 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 232 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 233 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 234 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
| 235 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 236 |
|
---|
| 237 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
| 238 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | PCUT_TEST(extract_ieee_double_normal_neg_smallest)
|
---|
| 242 | {
|
---|
| 243 | ieee_double_bits.integer = 0x8010000000000000ULL;
|
---|
| 244 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 245 |
|
---|
| 246 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 247 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 248 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 249 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 250 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
| 251 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 252 |
|
---|
| 253 | PCUT_ASSERT_INT_EQUALS(HIDDEN_BIT, d.pos_val.significand);
|
---|
| 254 | PCUT_ASSERT_INT_EQUALS(1 - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | PCUT_TEST(extract_ieee_double_normal_pos_largest)
|
---|
| 258 | {
|
---|
| 259 | ieee_double_bits.integer = 0x7FEFFFFFFFFFFFFFULL;
|
---|
| 260 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 261 |
|
---|
| 262 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 263 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 264 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 265 | PCUT_ASSERT_FALSE(d.is_negative);
|
---|
| 266 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
| 267 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 268 |
|
---|
| 269 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL + HIDDEN_BIT, d.pos_val.significand);
|
---|
| 270 | PCUT_ASSERT_INT_EQUALS(0x7FEULL - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | PCUT_TEST(extract_ieee_double_normal_neg_largest)
|
---|
| 274 | {
|
---|
| 275 | ieee_double_bits.integer = 0xFFEFFFFFFFFFFFFFULL;
|
---|
| 276 | ieee_double_t d = extract_ieee_double(ieee_double_bits.floating);
|
---|
| 277 |
|
---|
| 278 | PCUT_ASSERT_FALSE(d.is_special);
|
---|
| 279 | PCUT_ASSERT_FALSE(d.is_nan);
|
---|
| 280 | PCUT_ASSERT_FALSE(d.is_infinity);
|
---|
| 281 | PCUT_ASSERT_TRUE(d.is_negative);
|
---|
| 282 | PCUT_ASSERT_FALSE(d.is_denormal);
|
---|
| 283 | PCUT_ASSERT_FALSE(d.is_accuracy_step);
|
---|
| 284 |
|
---|
| 285 | PCUT_ASSERT_INT_EQUALS(0xFFFFFFFFFFFFFULL + HIDDEN_BIT, d.pos_val.significand);
|
---|
| 286 | PCUT_ASSERT_INT_EQUALS(0x7FEULL - EXP_BIAS_UNDERFLOWED, d.pos_val.exponent);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | PCUT_EXPORT(ieee_double);
|
---|