summaryrefslogtreecommitdiff
path: root/mpf/set_d.c
blob: b82e5f02338f5344e73de01f367484408bc2c6cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* mpf_set_d -- Assign a float from a IEEE double.

Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
License for more details.

You should have received a copy of the GNU Library General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include "gmp.h"
#include "gmp-impl.h"

void
#if __STDC__
mpf_set_d (mpf_ptr r, double d)
#else
mpf_set_d (r, d)
     mpf_ptr r;
     double d;
#endif
{
  mp_ptr rp;
  mp_size_t size;
  mp_exp_t exp;
  mp_limb_t manh, manl;
  mp_limb_t man2, man1, man0;
  union ieee_double_extract x;
  unsigned sc;

  /* ??? This needs more work since it assumes that limbs are 32 bits.
     See mpz/set_d for ideas on how to handle 64-bit machines.  */

  if (d == 0)
    {
      r->_mp_exp = 0;
      r->_mp_size = 0;
      return;
    }

  rp = r->_mp_d;
  x.d = d;

  exp = x.s.exp;
  sc = (unsigned) (exp + 2) % BITS_PER_MP_LIMB;

  manh = 0x80000000 | (x.s.manh << 11) | (x.s.manl >> 21);
  manl = x.s.manl << 11;
  if (sc != 0)
    {
      man2 = manh >> (BITS_PER_MP_LIMB - sc);
      man1 = (manl >> (BITS_PER_MP_LIMB - sc)) | (manh << sc);
      man0 = manl << sc;
    }
  else
    {
      man2 = manh;
      man1 = manl;
      man0 = 0;
    }

  if (man0 == 0)
    {
      if (man1 == 0)
	{
	  size = 1;
	  rp[0] = man2;
	}
      else
	{
	  size = 2;
	  rp[1] = man2;
	  rp[0] = man1;
	}
    }
  else
    {
      size = 3;
      rp[2] = man2;
      rp[1] = man1;
      rp[0] = man0;
    }

  r->_mp_exp = (exp + 1) / BITS_PER_MP_LIMB - 1024 / BITS_PER_MP_LIMB + 1;
  r->_mp_size = x.s.sig == 0 ? size : -size;
}