1 /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL.
3 Copyright 1995, 1996, 2000, 2001, 2002, 2003, 2006 Free Software Foundation,
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
24 #include <float.h> /* for DBL_MAX */
31 /* We used to have a special case for d < MP_BASE_AS_DOUBLE, just casting
32 double -> limb. Unfortunately gcc 3.3 on powerpc970-apple-darwin6.8.5
33 got this wrong. (It assumed __fixunsdfdi returned its result in a single
34 64-bit register, where instead that function followed the calling
35 conventions and gave the result in two parts r3 and r4.) Hence the use
36 of __gmp_extract_double in all cases. */
39 mpz_set_d (mpz_ptr r
, double d
)
42 mp_limb_t tp
[LIMBS_PER_DOUBLE
];
46 DOUBLE_NAN_INF_ACTION (d
,
47 __gmp_invalid_operation (),
48 __gmp_invalid_operation ());
53 rn
= __gmp_extract_double (tp
, d
);
66 MPN_ZERO (rp
, rn
- LIMBS_PER_DOUBLE
);
67 rp
+= rn
- LIMBS_PER_DOUBLE
;
69 #if LIMBS_PER_DOUBLE == 2
71 rp
[1] = tp
[1], rp
[0] = tp
[0];
77 #if LIMBS_PER_DOUBLE == 3
79 rp
[2] = tp
[2], rp
[1] = tp
[1], rp
[0] = tp
[0];
82 rp
[1] = tp
[2], rp
[0] = tp
[1];
88 #if LIMBS_PER_DOUBLE == 4
90 rp
[3] = tp
[3], rp
[2] = tp
[2], rp
[1] = tp
[1], rp
[0] = tp
[0];
93 rp
[2] = tp
[3], rp
[1] = tp
[2], rp
[0] = tp
[1];
96 rp
[1] = tp
[3], rp
[0] = tp
[2];
106 SIZ(r
) = negative
? -rn
: rn
;