Line data Source code
1 : #include "tommath_private.h" 2 : #ifdef BN_MP_SET_DOUBLE_C 3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 : /* SPDX-License-Identifier: Unlicense */ 5 : 6 : #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(_M_X86) || defined(__aarch64__) || defined(__arm__) 7 0 : mp_err mp_set_double(mp_int *a, double b) 8 : { 9 0 : uint64_t frac; 10 0 : int exp; 11 0 : mp_err err; 12 0 : union { 13 : double dbl; 14 : uint64_t bits; 15 : } cast; 16 0 : cast.dbl = b; 17 : 18 0 : exp = (int)((unsigned)(cast.bits >> 52) & 0x7FFu); 19 0 : frac = (cast.bits & ((1uLL << 52) - 1uLL)) | (1uLL << 52); 20 : 21 0 : if (exp == 0x7FF) { /* +-inf, NaN */ 22 0 : return MP_VAL; 23 : } 24 0 : exp -= 1023 + 52; 25 : 26 0 : mp_set_u64(a, frac); 27 : 28 0 : err = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); 29 0 : if (err != MP_OKAY) { 30 0 : return err; 31 : } 32 : 33 0 : if (((cast.bits >> 63) != 0uLL) && !MP_IS_ZERO(a)) { 34 0 : a->sign = MP_NEG; 35 : } 36 : 37 0 : return MP_OKAY; 38 : } 39 : #else 40 : /* pragma message() not supported by several compilers (in mostly older but still used versions) */ 41 : # ifdef _MSC_VER 42 : # pragma message("mp_set_double implementation is only available on platforms with IEEE754 floating point format") 43 : # else 44 : # warning "mp_set_double implementation is only available on platforms with IEEE754 floating point format" 45 : # endif 46 : #endif 47 : #endif