github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/set_d.c (about) 1 /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL. 2 3 Copyright 1995, 1996, 2000-2003, 2006 Free Software Foundation, Inc. 4 5 This file is part of the GNU MP Library. 6 7 The GNU MP Library is free software; you can redistribute it and/or modify 8 it under the terms of either: 9 10 * the GNU Lesser General Public License as published by the Free 11 Software Foundation; either version 3 of the License, or (at your 12 option) any later version. 13 14 or 15 16 * the GNU General Public License as published by the Free Software 17 Foundation; either version 2 of the License, or (at your option) any 18 later version. 19 20 or both in parallel, as here. 21 22 The GNU MP Library is distributed in the hope that it will be useful, but 23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 25 for more details. 26 27 You should have received copies of the GNU General Public License and the 28 GNU Lesser General Public License along with the GNU MP Library. If not, 29 see https://www.gnu.org/licenses/. */ 30 31 #include "config.h" 32 33 #if HAVE_FLOAT_H 34 #include <float.h> /* for DBL_MAX */ 35 #endif 36 37 #include "gmp.h" 38 #include "gmp-impl.h" 39 40 41 /* We used to have a special case for d < MP_BASE_AS_DOUBLE, just casting 42 double -> limb. Unfortunately gcc 3.3 on powerpc970-apple-darwin6.8.5 43 got this wrong. (It assumed __fixunsdfdi returned its result in a single 44 64-bit register, where instead that function followed the calling 45 conventions and gave the result in two parts r3 and r4.) Hence the use 46 of __gmp_extract_double in all cases. */ 47 48 void 49 mpz_set_d (mpz_ptr r, double d) 50 { 51 int negative; 52 mp_limb_t tp[LIMBS_PER_DOUBLE]; 53 mp_ptr rp; 54 mp_size_t rn; 55 56 DOUBLE_NAN_INF_ACTION (d, 57 __gmp_invalid_operation (), 58 __gmp_invalid_operation ()); 59 60 negative = d < 0; 61 d = ABS (d); 62 63 rn = __gmp_extract_double (tp, d); 64 65 if (ALLOC(r) < rn) 66 _mpz_realloc (r, rn); 67 68 if (rn <= 0) 69 rn = 0; 70 71 rp = PTR (r); 72 73 switch (rn) 74 { 75 default: 76 MPN_ZERO (rp, rn - LIMBS_PER_DOUBLE); 77 rp += rn - LIMBS_PER_DOUBLE; 78 /* fall through */ 79 #if LIMBS_PER_DOUBLE == 2 80 case 2: 81 rp[1] = tp[1], rp[0] = tp[0]; 82 break; 83 case 1: 84 rp[0] = tp[1]; 85 break; 86 #endif 87 #if LIMBS_PER_DOUBLE == 3 88 case 3: 89 rp[2] = tp[2], rp[1] = tp[1], rp[0] = tp[0]; 90 break; 91 case 2: 92 rp[1] = tp[2], rp[0] = tp[1]; 93 break; 94 case 1: 95 rp[0] = tp[2]; 96 break; 97 #endif 98 #if LIMBS_PER_DOUBLE == 4 99 case 4: 100 rp[3] = tp[3], rp[2] = tp[2], rp[1] = tp[1], rp[0] = tp[0]; 101 break; 102 case 3: 103 rp[2] = tp[3], rp[1] = tp[2], rp[0] = tp[1]; 104 break; 105 case 2: 106 rp[1] = tp[3], rp[0] = tp[2]; 107 break; 108 case 1: 109 rp[0] = tp[3]; 110 break; 111 #endif 112 case 0: 113 break; 114 } 115 116 SIZ(r) = negative ? -rn : rn; 117 }