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