Annotation of OpenXM_contrib/gmp/mpz/set_d.c, Revision 1.1.1.2
1.1 maekawa 1: /* mpz_set_d(integer, val) -- Assign INTEGER with a double value VAL.
2:
1.1.1.2 ! maekawa 3: Copyright (C) 1995, 1996, 2000 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: #if __STDC__
27: mpz_set_d (mpz_ptr r, double d)
28: #else
29: mpz_set_d (r, d)
30: mpz_ptr r;
31: double d;
32: #endif
33: {
34: int negative;
35: mp_limb_t tp[3];
36: mp_ptr rp;
1.1.1.2 ! maekawa 37: mp_size_t rn;
1.1 maekawa 38:
39: negative = d < 0;
40: d = ABS (d);
41:
42: /* Handle small arguments quickly. */
43: if (d < MP_BASE_AS_DOUBLE)
44: {
45: mp_limb_t tmp;
46: tmp = d;
47: PTR(r)[0] = tmp;
48: SIZ(r) = negative ? -(tmp != 0) : (tmp != 0);
49: return;
50: }
51:
1.1.1.2 ! maekawa 52: rn = __gmp_extract_double (tp, d);
1.1 maekawa 53:
1.1.1.2 ! maekawa 54: if (ALLOC(r) < rn)
! 55: _mpz_realloc (r, rn);
1.1 maekawa 56:
57: rp = PTR (r);
58:
59: #if BITS_PER_MP_LIMB == 32
1.1.1.2 ! maekawa 60: switch (rn)
1.1 maekawa 61: {
62: default:
1.1.1.2 ! maekawa 63: MPN_ZERO (rp, rn - 3);
! 64: rp += rn - 3;
! 65: /* fall through */
1.1 maekawa 66: case 3:
67: rp[2] = tp[2];
68: rp[1] = tp[1];
69: rp[0] = tp[0];
70: break;
71: case 2:
72: rp[1] = tp[2];
73: rp[0] = tp[1];
74: break;
75: case 1:
1.1.1.2 ! maekawa 76: /* handled in "small aguments" case above */
1.1 maekawa 77: abort ();
78: }
79: #else
1.1.1.2 ! maekawa 80: switch (rn)
1.1 maekawa 81: {
82: default:
1.1.1.2 ! maekawa 83: MPN_ZERO (rp, rn - 2);
! 84: rp += rn - 2;
! 85: /* fall through */
1.1 maekawa 86: case 2:
1.1.1.2 ! maekawa 87: rp[1] = tp[1], rp[0] = tp[0];
1.1 maekawa 88: break;
89: case 1:
1.1.1.2 ! maekawa 90: /* handled in "small aguments" case above */
1.1 maekawa 91: abort ();
92: }
93: #endif
94:
1.1.1.2 ! maekawa 95: SIZ(r) = negative ? -rn : rn;
1.1 maekawa 96: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>