Debian Codegraph @

msvc/gmp/mpz/set_d.c

raw symbols SCIP
1 /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL.
2
3 Copyright 1995, 1996, 2000, 2001, 2002, 2003 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 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., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "config.h"
23
24 #if HAVE_FLOAT_H
25 #include <float.h> /* for DBL_MAX */
26 #endif
27
28 #include "gmp.h"
29 #include "gmp-impl.h"
30
31
32 /* We used to have a special case for d < MP_BASE_AS_DOUBLE, just casting
33 double -> limb. Unfortunately gcc 3.3 on powerpc970-apple-darwin6.8.5
34 got this wrong. (It assumed __fixunsdfdi returned its result in a single
35 64-bit register, where instead that function followed the calling
36 conventions and gave the result in two parts r3 and r4.) Hence the use
37 of __gmp_extract_double in all cases. */
38
39 void
40 mpz_set_d (mpz_ptr r, double d)
41 {
42 int negative;
43 mp_limb_t tp[LIMBS_PER_DOUBLE];
44 mp_ptr rp;
45 mp_size_t rn;
46
47 DOUBLE_NAN_INF_ACTION (d,
48 __gmp_invalid_operation (),
49 __gmp_invalid_operation ());
50
51 negative = d < 0;
52 d = ABS (d);
53
54 rn = __gmp_extract_double (tp, d);
55
56 if (ALLOC(r) < rn)
57 _mpz_realloc (r, rn);
58
59 if(rn <= 0)
60 rn = 0;
61
62 rp = PTR (r);
63
64 switch (rn)
65 {
66 default:
67 MPN_ZERO (rp, rn - LIMBS_PER_DOUBLE);
68 rp += rn - LIMBS_PER_DOUBLE;
69 /* fall through */
70 #if LIMBS_PER_DOUBLE == 2
71 case 2:
72 rp[1] = tp[1], rp[0] = tp[0];
73 break;
74 case 1:
75 rp[0] = tp[1];
76 break;
77 #endif
78 #if LIMBS_PER_DOUBLE == 3
79 case 3:
80 rp[2] = tp[2], rp[1] = tp[1], rp[0] = tp[0];
81 break;
82 case 2:
83 rp[1] = tp[2], rp[0] = tp[1];
84 break;
85 case 1:
86 rp[0] = tp[2];
87 break;
88 #endif
89 #if LIMBS_PER_DOUBLE == 4
90 case 4:
91 rp[3] = tp[3], rp[2] = tp[2], rp[1] = tp[1], rp[0] = tp[0];
92 break;
93 case 3:
94 rp[2] = tp[3], rp[1] = tp[2], rp[0] = tp[1];
95 break;
96 case 2:
97 rp[1] = tp[3], rp[0] = tp[2];
98 break;
99 case 1:
100 rp[0] = tp[3];
101 break;
102 #endif
103 case 0:
104 break;
105 }
106
107 SIZ(r) = negative ? -rn : rn;
108 }