| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  | #include "tommath_private.h"
 | 
					
						
							|  |  |  | #ifdef BN_MP_SET_DOUBLE_C
 | 
					
						
							|  |  |  | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * LibTomMath is a library that provides multiple-precision | 
					
						
							|  |  |  |  * integer arithmetic as well as number theoretic functionality. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The library was designed directly after the MPI library by | 
					
						
							|  |  |  |  * Michael Fromberger but has been written from scratch with | 
					
						
							|  |  |  |  * additional optimizations in place. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-29 17:56:20 +01:00
										 |  |  |  * SPDX-License-Identifier: Unlicense | 
					
						
							| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559)
 | 
					
						
							| 
									
										
										
										
											2018-11-28 10:46:12 +01:00
										 |  |  | int mp_set_double(mp_int *a, double b) | 
					
						
							| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  | { | 
					
						
							|  |  |  |    uint64_t frac; | 
					
						
							|  |  |  |    int exp, res; | 
					
						
							|  |  |  |    union { | 
					
						
							|  |  |  |       double   dbl; | 
					
						
							|  |  |  |       uint64_t bits; | 
					
						
							|  |  |  |    } cast; | 
					
						
							| 
									
										
										
										
											2018-11-28 10:46:12 +01:00
										 |  |  |    cast.dbl = b; | 
					
						
							| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-01 08:33:02 +01:00
										 |  |  |    exp = (int)((unsigned)(cast.bits >> 52) & 0x7FFU); | 
					
						
							| 
									
										
										
										
											2018-11-28 10:48:04 +01:00
										 |  |  |    frac = (cast.bits & ((1ULL << 52) - 1ULL)) | (1ULL << 52); | 
					
						
							| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    if (exp == 0x7FF) { /* +-inf, NaN */ | 
					
						
							|  |  |  |       return MP_VAL; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |    exp -= 1023 + 52; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    res = mp_set_long_long(a, frac); | 
					
						
							|  |  |  |    if (res != MP_OKAY) { | 
					
						
							|  |  |  |       return res; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-28 10:50:20 +01:00
										 |  |  |    res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a); | 
					
						
							| 
									
										
										
										
											2018-12-01 08:35:22 +01:00
										 |  |  |    if (res != MP_OKAY) { | 
					
						
							|  |  |  |       return res; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-28 10:44:22 +01:00
										 |  |  |    if (((cast.bits >> 63) != 0ULL) && (mp_iszero(a) == MP_NO)) { | 
					
						
							| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  |       SIGN(a) = MP_NEG; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    return MP_OKAY; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-12-07 21:18:43 +01:00
										 |  |  | /* pragma message() not supported by several compilers (in mostly older but still used versions) */ | 
					
						
							| 
									
										
										
										
											2018-12-07 20:47:51 +01:00
										 |  |  | #  ifdef _MSC_VER
 | 
					
						
							|  |  |  | #    pragma message("mp_set_double implementation is only available on platforms with IEEE754 floating point format")
 | 
					
						
							|  |  |  | #  else
 | 
					
						
							|  |  |  | #    warning "mp_set_double implementation is only available on platforms with IEEE754 floating point format"
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2018-09-10 18:51:26 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ref:         $Format:%D$ */ | 
					
						
							|  |  |  | /* git commit:  $Format:%H$ */ | 
					
						
							|  |  |  | /* commit time: $Format:%ai$ */ |