Annotation of OpenXM_contrib/gmp/mpz/set_d.c, Revision 1.1.1.3
1.1 maekawa 1: /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL.
2:
1.1.1.3 ! ohara 3: Copyright 1995, 1996, 2000, 2001, 2002 Free Software Foundation, Inc.
1.1 maekawa 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
1.1.1.2 maekawa 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
1.1 maekawa 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
1.1.1.2 maekawa 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 maekawa 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 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;
1.1.1.3 ! ohara 29: mp_limb_t tp[LIMBS_PER_DOUBLE];
1.1 maekawa 30: mp_ptr rp;
1.1.1.2 maekawa 31: mp_size_t rn;
1.1 maekawa 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:
1.1.1.2 maekawa 46: rn = __gmp_extract_double (tp, d);
1.1 maekawa 47:
1.1.1.2 maekawa 48: if (ALLOC(r) < rn)
49: _mpz_realloc (r, rn);
1.1 maekawa 50:
51: rp = PTR (r);
52:
53: #if BITS_PER_MP_LIMB == 32
1.1.1.2 maekawa 54: switch (rn)
1.1 maekawa 55: {
56: default:
1.1.1.2 maekawa 57: MPN_ZERO (rp, rn - 3);
58: rp += rn - 3;
59: /* fall through */
1.1 maekawa 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:
1.1.1.2 maekawa 70: /* handled in "small aguments" case above */
1.1 maekawa 71: abort ();
72: }
73: #else
1.1.1.2 maekawa 74: switch (rn)
1.1 maekawa 75: {
76: default:
1.1.1.2 maekawa 77: MPN_ZERO (rp, rn - 2);
78: rp += rn - 2;
79: /* fall through */
1.1 maekawa 80: case 2:
1.1.1.2 maekawa 81: rp[1] = tp[1], rp[0] = tp[0];
1.1 maekawa 82: break;
83: case 1:
1.1.1.2 maekawa 84: /* handled in "small aguments" case above */
1.1 maekawa 85: abort ();
86: }
87: #endif
88:
1.1.1.2 maekawa 89: SIZ(r) = negative ? -rn : rn;
1.1 maekawa 90: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>