[b5440cf] | 1 | /*
|
---|
| 2 | * Copyright (C) 2005 Josef Cejka
|
---|
| 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 |
|
---|
[feef1cd] | 29 | #include "sftypes.h"
|
---|
| 30 | #include "conversion.h"
|
---|
[afffa1e] | 31 | #include "comparison.h"
|
---|
[1d83419] | 32 | #include "common.h"
|
---|
[feef1cd] | 33 |
|
---|
| 34 | float64 convertFloat32ToFloat64(float32 a)
|
---|
| 35 | {
|
---|
| 36 | float64 result;
|
---|
[aa59fa0] | 37 | uint64_t frac;
|
---|
[feef1cd] | 38 |
|
---|
| 39 | result.parts.sign = a.parts.sign;
|
---|
[1266543] | 40 | result.parts.fraction = a.parts.fraction;
|
---|
| 41 | result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE );
|
---|
[feef1cd] | 42 |
|
---|
| 43 | if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
|
---|
| 44 | result.parts.exp = 0x7FF;
|
---|
| 45 | /* TODO; check if its correct for SigNaNs*/
|
---|
| 46 | return result;
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
|
---|
| 50 | if (a.parts.exp == 0) {
|
---|
| 51 | /* normalize denormalized numbers */
|
---|
| 52 |
|
---|
[1266543] | 53 | if (result.parts.fraction == 0ll) { /* fix zero */
|
---|
[feef1cd] | 54 | result.parts.exp = 0ll;
|
---|
| 55 | return result;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1266543] | 58 | frac = result.parts.fraction;
|
---|
[feef1cd] | 59 |
|
---|
[1266543] | 60 | while (!(frac & (0x10000000000000ll))) {
|
---|
| 61 | frac <<= 1;
|
---|
[feef1cd] | 62 | --result.parts.exp;
|
---|
| 63 | };
|
---|
[56a39dde] | 64 |
|
---|
| 65 | ++result.parts.exp;
|
---|
[1266543] | 66 | result.parts.fraction = frac;
|
---|
[feef1cd] | 67 | };
|
---|
| 68 |
|
---|
| 69 | return result;
|
---|
| 70 |
|
---|
[afffa1e] | 71 | }
|
---|
[feef1cd] | 72 |
|
---|
| 73 | float32 convertFloat64ToFloat32(float64 a)
|
---|
| 74 | {
|
---|
| 75 | float32 result;
|
---|
[aa59fa0] | 76 | int32_t exp;
|
---|
| 77 | uint64_t frac;
|
---|
[feef1cd] | 78 |
|
---|
| 79 | result.parts.sign = a.parts.sign;
|
---|
| 80 |
|
---|
| 81 | if (isFloat64NaN(a)) {
|
---|
| 82 |
|
---|
| 83 | result.parts.exp = 0xFF;
|
---|
| 84 |
|
---|
| 85 | if (isFloat64SigNaN(a)) {
|
---|
[aa59fa0] | 86 | result.parts.fraction = 0x400000; /* set first bit of fraction nonzero */
|
---|
[feef1cd] | 87 | return result;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[1266543] | 90 | result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */
|
---|
[feef1cd] | 91 | return result;
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | if (isFloat64Infinity(a)) {
|
---|
[1266543] | 95 | result.parts.fraction = 0;
|
---|
[feef1cd] | 96 | result.parts.exp = 0xFF;
|
---|
| 97 | return result;
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
|
---|
| 101 |
|
---|
| 102 | if (exp >= 0xFF) {
|
---|
| 103 | /*FIXME: overflow*/
|
---|
[1266543] | 104 | result.parts.fraction = 0;
|
---|
[feef1cd] | 105 | result.parts.exp = 0xFF;
|
---|
| 106 | return result;
|
---|
| 107 |
|
---|
| 108 | } else if (exp <= 0 ) {
|
---|
| 109 |
|
---|
| 110 | /* underflow or denormalized */
|
---|
| 111 |
|
---|
| 112 | result.parts.exp = 0;
|
---|
| 113 |
|
---|
| 114 | exp *= -1;
|
---|
[1266543] | 115 | if (exp > FLOAT32_FRACTION_SIZE ) {
|
---|
[feef1cd] | 116 | /* FIXME: underflow */
|
---|
[1266543] | 117 | result.parts.fraction = 0;
|
---|
[feef1cd] | 118 | return result;
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | /* denormalized */
|
---|
| 122 |
|
---|
[1266543] | 123 | frac = a.parts.fraction;
|
---|
| 124 | frac |= 0x10000000000000ll; /* denormalize and set hidden bit */
|
---|
[feef1cd] | 125 |
|
---|
[1266543] | 126 | frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
|
---|
[56a39dde] | 127 |
|
---|
[feef1cd] | 128 | while (exp > 0) {
|
---|
| 129 | --exp;
|
---|
[1266543] | 130 | frac >>= 1;
|
---|
[feef1cd] | 131 | };
|
---|
[1266543] | 132 | result.parts.fraction = frac;
|
---|
[feef1cd] | 133 |
|
---|
| 134 | return result;
|
---|
| 135 | };
|
---|
| 136 |
|
---|
| 137 | result.parts.exp = exp;
|
---|
[1266543] | 138 | result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
|
---|
[feef1cd] | 139 | return result;
|
---|
[afffa1e] | 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | /** Helping procedure for converting float32 to uint32
|
---|
| 144 | * @param a floating point number in normalized form (no NaNs or Inf are checked )
|
---|
| 145 | * @return unsigned integer
|
---|
| 146 | */
|
---|
[aa59fa0] | 147 | static uint32_t _float32_to_uint32_helper(float32 a)
|
---|
[afffa1e] | 148 | {
|
---|
[aa59fa0] | 149 | uint32_t frac;
|
---|
[afffa1e] | 150 |
|
---|
| 151 | if (a.parts.exp < FLOAT32_BIAS) {
|
---|
| 152 | /*TODO: rounding*/
|
---|
| 153 | return 0;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | frac = a.parts.fraction;
|
---|
| 157 |
|
---|
| 158 | frac |= FLOAT32_HIDDEN_BIT_MASK;
|
---|
| 159 | /* shift fraction to left so hidden bit will be the most significant bit */
|
---|
| 160 | frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
|
---|
| 161 |
|
---|
| 162 | frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
|
---|
| 163 | if ((a.parts.sign == 1) && (frac != 0)) {
|
---|
| 164 | frac = ~frac;
|
---|
| 165 | ++frac;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | return frac;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /* Convert float to unsigned int32
|
---|
| 172 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 173 | * - now its the biggest or the smallest int
|
---|
| 174 | */
|
---|
[aa59fa0] | 175 | uint32_t float32_to_uint32(float32 a)
|
---|
[afffa1e] | 176 | {
|
---|
| 177 | if (isFloat32NaN(a)) {
|
---|
| 178 | return MAX_UINT32;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {
|
---|
| 182 | if (a.parts.sign) {
|
---|
| 183 | return MIN_UINT32;
|
---|
| 184 | }
|
---|
| 185 | return MAX_UINT32;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | return _float32_to_uint32_helper(a);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /* Convert float to signed int32
|
---|
| 192 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 193 | * - now its the biggest or the smallest int
|
---|
| 194 | */
|
---|
[aa59fa0] | 195 | int32_t float32_to_int32(float32 a)
|
---|
[afffa1e] | 196 | {
|
---|
| 197 | if (isFloat32NaN(a)) {
|
---|
| 198 | return MAX_INT32;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {
|
---|
| 202 | if (a.parts.sign) {
|
---|
| 203 | return MIN_INT32;
|
---|
| 204 | }
|
---|
| 205 | return MAX_INT32;
|
---|
| 206 | }
|
---|
| 207 | return _float32_to_uint32_helper(a);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
[a82695c] | 211 | /** Helping procedure for converting float64 to uint64
|
---|
| 212 | * @param a floating point number in normalized form (no NaNs or Inf are checked )
|
---|
| 213 | * @return unsigned integer
|
---|
| 214 | */
|
---|
[aa59fa0] | 215 | static uint64_t _float64_to_uint64_helper(float64 a)
|
---|
[a82695c] | 216 | {
|
---|
[aa59fa0] | 217 | uint64_t frac;
|
---|
[a82695c] | 218 |
|
---|
| 219 | if (a.parts.exp < FLOAT64_BIAS) {
|
---|
| 220 | /*TODO: rounding*/
|
---|
| 221 | return 0;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | frac = a.parts.fraction;
|
---|
| 225 |
|
---|
| 226 | frac |= FLOAT64_HIDDEN_BIT_MASK;
|
---|
| 227 | /* shift fraction to left so hidden bit will be the most significant bit */
|
---|
| 228 | frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
|
---|
| 229 |
|
---|
| 230 | frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
|
---|
| 231 | if ((a.parts.sign == 1) && (frac != 0)) {
|
---|
| 232 | frac = ~frac;
|
---|
| 233 | ++frac;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | return frac;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /* Convert float to unsigned int64
|
---|
| 240 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 241 | * - now its the biggest or the smallest int
|
---|
| 242 | */
|
---|
[aa59fa0] | 243 | uint64_t float64_to_uint64(float64 a)
|
---|
[a82695c] | 244 | {
|
---|
| 245 | if (isFloat64NaN(a)) {
|
---|
| 246 | return MAX_UINT64;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
|
---|
| 250 | if (a.parts.sign) {
|
---|
| 251 | return MIN_UINT64;
|
---|
| 252 | }
|
---|
| 253 | return MAX_UINT64;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | return _float64_to_uint64_helper(a);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /* Convert float to signed int64
|
---|
| 260 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 261 | * - now its the biggest or the smallest int
|
---|
| 262 | */
|
---|
[aa59fa0] | 263 | int64_t float64_to_int64(float64 a)
|
---|
[a82695c] | 264 | {
|
---|
| 265 | if (isFloat64NaN(a)) {
|
---|
| 266 | return MAX_INT64;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
|
---|
| 270 | if (a.parts.sign) {
|
---|
| 271 | return MIN_INT64;
|
---|
| 272 | }
|
---|
| 273 | return MAX_INT64;
|
---|
| 274 | }
|
---|
| 275 | return _float64_to_uint64_helper(a);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | /** Helping procedure for converting float32 to uint64
|
---|
| 283 | * @param a floating point number in normalized form (no NaNs or Inf are checked )
|
---|
| 284 | * @return unsigned integer
|
---|
| 285 | */
|
---|
[aa59fa0] | 286 | static uint64_t _float32_to_uint64_helper(float32 a)
|
---|
[a82695c] | 287 | {
|
---|
[aa59fa0] | 288 | uint64_t frac;
|
---|
[a82695c] | 289 |
|
---|
| 290 | if (a.parts.exp < FLOAT32_BIAS) {
|
---|
| 291 | /*TODO: rounding*/
|
---|
| 292 | return 0;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | frac = a.parts.fraction;
|
---|
| 296 |
|
---|
| 297 | frac |= FLOAT32_HIDDEN_BIT_MASK;
|
---|
| 298 | /* shift fraction to left so hidden bit will be the most significant bit */
|
---|
| 299 | frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
|
---|
| 300 |
|
---|
| 301 | frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
|
---|
| 302 | if ((a.parts.sign == 1) && (frac != 0)) {
|
---|
| 303 | frac = ~frac;
|
---|
| 304 | ++frac;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | return frac;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | /* Convert float to unsigned int64
|
---|
| 311 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 312 | * - now its the biggest or the smallest int
|
---|
| 313 | */
|
---|
[aa59fa0] | 314 | uint64_t float32_to_uint64(float32 a)
|
---|
[a82695c] | 315 | {
|
---|
| 316 | if (isFloat32NaN(a)) {
|
---|
| 317 | return MAX_UINT64;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {
|
---|
| 321 | if (a.parts.sign) {
|
---|
| 322 | return MIN_UINT64;
|
---|
| 323 | }
|
---|
| 324 | return MAX_UINT64;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | return _float32_to_uint64_helper(a);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /* Convert float to signed int64
|
---|
| 331 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 332 | * - now its the biggest or the smallest int
|
---|
| 333 | */
|
---|
[aa59fa0] | 334 | int64_t float32_to_int64(float32 a)
|
---|
[a82695c] | 335 | {
|
---|
| 336 | if (isFloat32NaN(a)) {
|
---|
| 337 | return MAX_INT64;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {
|
---|
| 341 | if (a.parts.sign) {
|
---|
| 342 | return (MIN_INT64);
|
---|
| 343 | }
|
---|
| 344 | return MAX_INT64;
|
---|
| 345 | }
|
---|
| 346 | return _float32_to_uint64_helper(a);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 | /* Convert float64 to unsigned int32
|
---|
| 351 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 352 | * - now its the biggest or the smallest int
|
---|
| 353 | */
|
---|
[aa59fa0] | 354 | uint32_t float64_to_uint32(float64 a)
|
---|
[a82695c] | 355 | {
|
---|
| 356 | if (isFloat64NaN(a)) {
|
---|
| 357 | return MAX_UINT32;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {
|
---|
| 361 | if (a.parts.sign) {
|
---|
| 362 | return MIN_UINT32;
|
---|
| 363 | }
|
---|
| 364 | return MAX_UINT32;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[aa59fa0] | 367 | return (uint32_t)_float64_to_uint64_helper(a);
|
---|
[a82695c] | 368 | }
|
---|
| 369 |
|
---|
| 370 | /* Convert float64 to signed int32
|
---|
| 371 | * FIXME: Im not sure what to return if overflow/underflow happens
|
---|
| 372 | * - now its the biggest or the smallest int
|
---|
| 373 | */
|
---|
[aa59fa0] | 374 | int32_t float64_to_int32(float64 a)
|
---|
[a82695c] | 375 | {
|
---|
| 376 | if (isFloat64NaN(a)) {
|
---|
| 377 | return MAX_INT32;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {
|
---|
| 381 | if (a.parts.sign) {
|
---|
| 382 | return MIN_INT32;
|
---|
| 383 | }
|
---|
| 384 | return MAX_INT32;
|
---|
| 385 | }
|
---|
[aa59fa0] | 386 | return (int32_t)_float64_to_uint64_helper(a);
|
---|
[a82695c] | 387 | }
|
---|
| 388 |
|
---|
[1d83419] | 389 | /** Convert unsigned integer to float32
|
---|
| 390 | *
|
---|
| 391 | *
|
---|
| 392 | */
|
---|
[aa59fa0] | 393 | float32 uint32_to_float32(uint32_t i)
|
---|
[1d83419] | 394 | {
|
---|
| 395 | int counter;
|
---|
[aa59fa0] | 396 | int32_t exp;
|
---|
[1d83419] | 397 | float32 result;
|
---|
| 398 |
|
---|
| 399 | result.parts.sign = 0;
|
---|
| 400 | result.parts.fraction = 0;
|
---|
| 401 |
|
---|
| 402 | counter = countZeroes32(i);
|
---|
| 403 |
|
---|
| 404 | exp = FLOAT32_BIAS + 32 - counter - 1;
|
---|
| 405 |
|
---|
| 406 | if (counter == 32) {
|
---|
| 407 | result.binary = 0;
|
---|
| 408 | return result;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | if (counter > 0) {
|
---|
| 412 | i <<= counter - 1;
|
---|
| 413 | } else {
|
---|
| 414 | i >>= 1;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | roundFloat32(&exp, &i);
|
---|
| 418 |
|
---|
| 419 | result.parts.fraction = i >> 7;
|
---|
| 420 | result.parts.exp = exp;
|
---|
| 421 |
|
---|
| 422 | return result;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[aa59fa0] | 425 | float32 int32_to_float32(int32_t i)
|
---|
[1d83419] | 426 | {
|
---|
| 427 | float32 result;
|
---|
| 428 |
|
---|
| 429 | if (i < 0) {
|
---|
[aa59fa0] | 430 | result = uint32_to_float32((uint32_t)(-i));
|
---|
[1d83419] | 431 | } else {
|
---|
[aa59fa0] | 432 | result = uint32_to_float32((uint32_t)i);
|
---|
[1d83419] | 433 | }
|
---|
| 434 |
|
---|
| 435 | result.parts.sign = i < 0;
|
---|
| 436 |
|
---|
| 437 | return result;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[feef1cd] | 440 |
|
---|
[aa59fa0] | 441 | float32 uint64_to_float32(uint64_t i)
|
---|
[1d83419] | 442 | {
|
---|
[ba5870d] | 443 | int counter;
|
---|
[aa59fa0] | 444 | int32_t exp;
|
---|
[e591928] | 445 | uint32_t j;
|
---|
[ba5870d] | 446 | float32 result;
|
---|
| 447 |
|
---|
| 448 | result.parts.sign = 0;
|
---|
| 449 | result.parts.fraction = 0;
|
---|
| 450 |
|
---|
| 451 | counter = countZeroes64(i);
|
---|
| 452 |
|
---|
| 453 | exp = FLOAT32_BIAS + 64 - counter - 1;
|
---|
| 454 |
|
---|
| 455 | if (counter == 64) {
|
---|
| 456 | result.binary = 0;
|
---|
| 457 | return result;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | /* Shift all to the first 31 bits (31. will be hidden 1)*/
|
---|
| 461 | if (counter > 33) {
|
---|
| 462 | i <<= counter - 1 - 32;
|
---|
| 463 | } else {
|
---|
| 464 | i >>= 1 + 32 - counter;
|
---|
| 465 | }
|
---|
[aa59fa0] | 466 |
|
---|
| 467 | j = (uint32_t)i;
|
---|
| 468 | roundFloat32(&exp, &j);
|
---|
[ba5870d] | 469 |
|
---|
[aa59fa0] | 470 | result.parts.fraction = j >> 7;
|
---|
[ba5870d] | 471 | result.parts.exp = exp;
|
---|
| 472 | return result;
|
---|
[1d83419] | 473 | }
|
---|
| 474 |
|
---|
[aa59fa0] | 475 | float32 int64_to_float32(int64_t i)
|
---|
[1d83419] | 476 | {
|
---|
| 477 | float32 result;
|
---|
| 478 |
|
---|
| 479 | if (i < 0) {
|
---|
[aa59fa0] | 480 | result = uint64_to_float32((uint64_t)(-i));
|
---|
[1d83419] | 481 | } else {
|
---|
[aa59fa0] | 482 | result = uint64_to_float32((uint64_t)i);
|
---|
[1d83419] | 483 | }
|
---|
| 484 |
|
---|
| 485 | result.parts.sign = i < 0;
|
---|
| 486 |
|
---|
| 487 | return result;
|
---|
| 488 | }
|
---|
[f37d769] | 489 |
|
---|
| 490 | /** Convert unsigned integer to float64
|
---|
| 491 | *
|
---|
| 492 | *
|
---|
| 493 | */
|
---|
[aa59fa0] | 494 | float64 uint32_to_float64(uint32_t i)
|
---|
[f37d769] | 495 | {
|
---|
| 496 | int counter;
|
---|
[aa59fa0] | 497 | int32_t exp;
|
---|
[f37d769] | 498 | float64 result;
|
---|
[aa59fa0] | 499 | uint64_t frac;
|
---|
[f37d769] | 500 |
|
---|
| 501 | result.parts.sign = 0;
|
---|
| 502 | result.parts.fraction = 0;
|
---|
| 503 |
|
---|
| 504 | counter = countZeroes32(i);
|
---|
| 505 |
|
---|
| 506 | exp = FLOAT64_BIAS + 32 - counter - 1;
|
---|
| 507 |
|
---|
| 508 | if (counter == 32) {
|
---|
| 509 | result.binary = 0;
|
---|
| 510 | return result;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | frac = i;
|
---|
| 514 | frac <<= counter + 32 - 1;
|
---|
| 515 |
|
---|
| 516 | roundFloat64(&exp, &frac);
|
---|
| 517 |
|
---|
| 518 | result.parts.fraction = frac >> 10;
|
---|
| 519 | result.parts.exp = exp;
|
---|
| 520 |
|
---|
| 521 | return result;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[aa59fa0] | 524 | float64 int32_to_float64(int32_t i)
|
---|
[f37d769] | 525 | {
|
---|
| 526 | float64 result;
|
---|
| 527 |
|
---|
| 528 | if (i < 0) {
|
---|
[aa59fa0] | 529 | result = uint32_to_float64((uint32_t)(-i));
|
---|
[f37d769] | 530 | } else {
|
---|
[aa59fa0] | 531 | result = uint32_to_float64((uint32_t)i);
|
---|
[f37d769] | 532 | }
|
---|
| 533 |
|
---|
| 534 | result.parts.sign = i < 0;
|
---|
| 535 |
|
---|
| 536 | return result;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 |
|
---|
[aa59fa0] | 540 | float64 uint64_to_float64(uint64_t i)
|
---|
[f37d769] | 541 | {
|
---|
| 542 | int counter;
|
---|
[aa59fa0] | 543 | int32_t exp;
|
---|
[f37d769] | 544 | float64 result;
|
---|
| 545 |
|
---|
| 546 | result.parts.sign = 0;
|
---|
| 547 | result.parts.fraction = 0;
|
---|
| 548 |
|
---|
| 549 | counter = countZeroes64(i);
|
---|
| 550 |
|
---|
| 551 | exp = FLOAT64_BIAS + 64 - counter - 1;
|
---|
| 552 |
|
---|
| 553 | if (counter == 64) {
|
---|
| 554 | result.binary = 0;
|
---|
| 555 | return result;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | if (counter > 0) {
|
---|
| 559 | i <<= counter - 1;
|
---|
| 560 | } else {
|
---|
| 561 | i >>= 1;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | roundFloat64(&exp, &i);
|
---|
| 565 |
|
---|
| 566 | result.parts.fraction = i >> 10;
|
---|
| 567 | result.parts.exp = exp;
|
---|
| 568 | return result;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[aa59fa0] | 571 | float64 int64_to_float64(int64_t i)
|
---|
[f37d769] | 572 | {
|
---|
| 573 | float64 result;
|
---|
| 574 |
|
---|
| 575 | if (i < 0) {
|
---|
[aa59fa0] | 576 | result = uint64_to_float64((uint64_t)(-i));
|
---|
[f37d769] | 577 | } else {
|
---|
[aa59fa0] | 578 | result = uint64_to_float64((uint64_t)i);
|
---|
[f37d769] | 579 | }
|
---|
| 580 |
|
---|
| 581 | result.parts.sign = i < 0;
|
---|
| 582 |
|
---|
| 583 | return result;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 |
|
---|