1 /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL.
3 Copyright 1995, 1996, 2000-2003, 2006 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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
20 or both in parallel, as here.
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
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/. */
34 #include <float.h> /* for DBL_MAX */
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. */
49 mpz_set_d (mpz_ptr r
, double d
)
52 mp_limb_t tp
[LIMBS_PER_DOUBLE
];
56 DOUBLE_NAN_INF_ACTION (d
,
57 __gmp_invalid_operation (),
58 __gmp_invalid_operation ());
63 rn
= __gmp_extract_double (tp
, d
);
76 MPN_ZERO (rp
, rn
- LIMBS_PER_DOUBLE
);
77 rp
+= rn
- LIMBS_PER_DOUBLE
;
79 #if LIMBS_PER_DOUBLE == 2
81 rp
[1] = tp
[1], rp
[0] = tp
[0];
87 #if LIMBS_PER_DOUBLE == 3
89 rp
[2] = tp
[2], rp
[1] = tp
[1], rp
[0] = tp
[0];
92 rp
[1] = tp
[2], rp
[0] = tp
[1];
98 #if LIMBS_PER_DOUBLE == 4
100 rp
[3] = tp
[3], rp
[2] = tp
[2], rp
[1] = tp
[1], rp
[0] = tp
[0];
103 rp
[2] = tp
[3], rp
[1] = tp
[2], rp
[0] = tp
[1];
106 rp
[1] = tp
[3], rp
[0] = tp
[2];
116 SIZ(r
) = negative
? -rn
: rn
;