[15293] | 1 | /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL. |
---|
| 2 | |
---|
[18190] | 3 | Copyright 1995, 1996, 2000, 2001, 2002 Free Software Foundation, Inc. |
---|
[15293] | 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 the GNU Lesser General Public License as published by |
---|
| 9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
| 10 | option) any later version. |
---|
| 11 | |
---|
| 12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
| 15 | License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU Lesser General Public License |
---|
| 18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
| 19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
| 20 | MA 02111-1307, USA. */ |
---|
| 21 | |
---|
| 22 | #include "gmp.h" |
---|
| 23 | #include "gmp-impl.h" |
---|
| 24 | |
---|
| 25 | void |
---|
| 26 | mpz_set_d (mpz_ptr r, double d) |
---|
| 27 | { |
---|
| 28 | int negative; |
---|
[18190] | 29 | mp_limb_t tp[LIMBS_PER_DOUBLE]; |
---|
[15293] | 30 | mp_ptr rp; |
---|
| 31 | mp_size_t rn; |
---|
| 32 | |
---|
| 33 | negative = d < 0; |
---|
| 34 | d = ABS (d); |
---|
| 35 | |
---|
| 36 | /* Handle small arguments quickly. */ |
---|
| 37 | if (d < MP_BASE_AS_DOUBLE) |
---|
| 38 | { |
---|
| 39 | mp_limb_t tmp; |
---|
| 40 | tmp = d; |
---|
| 41 | PTR(r)[0] = tmp; |
---|
| 42 | SIZ(r) = negative ? -(tmp != 0) : (tmp != 0); |
---|
| 43 | return; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | rn = __gmp_extract_double (tp, d); |
---|
| 47 | |
---|
| 48 | if (ALLOC(r) < rn) |
---|
| 49 | _mpz_realloc (r, rn); |
---|
| 50 | |
---|
| 51 | rp = PTR (r); |
---|
| 52 | |
---|
| 53 | #if BITS_PER_MP_LIMB == 32 |
---|
| 54 | switch (rn) |
---|
| 55 | { |
---|
| 56 | default: |
---|
| 57 | MPN_ZERO (rp, rn - 3); |
---|
| 58 | rp += rn - 3; |
---|
| 59 | /* fall through */ |
---|
| 60 | case 3: |
---|
| 61 | rp[2] = tp[2]; |
---|
| 62 | rp[1] = tp[1]; |
---|
| 63 | rp[0] = tp[0]; |
---|
| 64 | break; |
---|
| 65 | case 2: |
---|
| 66 | rp[1] = tp[2]; |
---|
| 67 | rp[0] = tp[1]; |
---|
| 68 | break; |
---|
| 69 | case 1: |
---|
| 70 | /* handled in "small aguments" case above */ |
---|
| 71 | abort (); |
---|
| 72 | } |
---|
| 73 | #else |
---|
| 74 | switch (rn) |
---|
| 75 | { |
---|
| 76 | default: |
---|
| 77 | MPN_ZERO (rp, rn - 2); |
---|
| 78 | rp += rn - 2; |
---|
| 79 | /* fall through */ |
---|
| 80 | case 2: |
---|
| 81 | rp[1] = tp[1], rp[0] = tp[0]; |
---|
| 82 | break; |
---|
| 83 | case 1: |
---|
| 84 | /* handled in "small aguments" case above */ |
---|
| 85 | abort (); |
---|
| 86 | } |
---|
| 87 | #endif |
---|
| 88 | |
---|
| 89 | SIZ(r) = negative ? -rn : rn; |
---|
| 90 | } |
---|