about summary refs log tree commit diff
path: root/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c
blob: 3df42c5666de89e1d88457b8e37dd56aa77b068c (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Copyright (C) 1995-2013 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

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

   The GNU C 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include "gmp.h"
#include "gmp-impl.h"
#include <ieee754.h>
#include <float.h>
#include <math.h>

/* Convert a multi-precision integer of the needed number of bits (106
   for long double) and an integral power of two to a `long double' in
   IBM extended format.  */

long double
__mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign)
{
  union ibm_extended_long_double u;
  unsigned long lzcount;
  unsigned long long hi, lo;
  int exponent2;

  u.ieee.negative = sign;
  u.ieee.negative2 = sign;
  u.ieee.exponent = expt + IBM_EXTENDED_LONG_DOUBLE_BIAS;
  u.ieee.exponent2 = 0;
  exponent2 = expt - 53 + IBM_EXTENDED_LONG_DOUBLE_BIAS;

#if BITS_PER_MP_LIMB == 32
  /* The low order 53 bits (52 + hidden) go into the lower double */
  lo = frac_ptr[0];
  lo |= (frac_ptr[1] & ((1LL << (53 - 32)) - 1)) << 32;
  /* The high order 53 bits (52 + hidden) go into the upper double */
  hi = (frac_ptr[1] >> (53 - 32)) & ((1 << 11) - 1);
  hi |= ((unsigned long long) frac_ptr[2]) << 11;
  hi |= ((unsigned long long) frac_ptr[3]) << (32 + 11);
#elif BITS_PER_MP_LIMB == 64
  /* The low order 53 bits (52 + hidden) go into the lower double */
  lo = frac_ptr[0] & (((mp_limb_t) 1 << 53) - 1);
  /* The high order 53 bits (52 + hidden) go into the upper double */
  hi = (frac_ptr[0] >> 53) & (((mp_limb_t) 1 << 11) - 1);
  hi |= (frac_ptr[1] << 11);
#else
  #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
#endif

  if ((hi & (1LL << 52)) == 0 && (hi | lo) != 0)
    {
      /* denormal number  */
      unsigned long long val = hi ? hi : lo;

      if (sizeof (val) == sizeof (long))
	lzcount = __builtin_clzl (val);
      else if ((val >> 32) != 0)
	lzcount = __builtin_clzl ((long) (val >> 32));
      else
	lzcount = __builtin_clzl ((long) val) + 32;
      if (hi)
	lzcount = lzcount - 11;
      else
	lzcount = lzcount + 42;

      if (lzcount > u.ieee.exponent)
	{
	  lzcount = u.ieee.exponent;
	  u.ieee.exponent = 0;
	  exponent2 -= lzcount;
	}
      else
	{
	  u.ieee.exponent -= (lzcount - 1);
	  exponent2 -= (lzcount - 1);
	}

      if (lzcount <= 53)
	{
	  hi = (hi << lzcount) | (lo >> (53 - lzcount));
	  lo = (lo << lzcount) & ((1LL << 53) - 1);
	}
      else
	{
	  hi = lo << (lzcount - 53);
	  lo = 0;
	}
    }

  if (lo != 0L)
    {
      /* hidden2 bit of low double controls rounding of the high double.
	 If hidden2 is '1' and either the explicit mantissa is non-zero
	 or hi is odd, then round up hi and adjust lo (2nd mantissa)
	 plus change the sign of the low double to compensate.  */
      if ((lo & (1LL << 52)) != 0
	  && ((hi & 1) != 0 || (lo & ((1LL << 52) - 1))))
	{
	  hi++;
	  if ((hi & ((1LL << 52) - 1)) == 0)
	    {
	      if ((hi & (1LL << 53)) != 0)
		hi -= 1LL << 52;
	      u.ieee.exponent++;
	    }
	  u.ieee.negative2 = !sign;
	  lo = (1LL << 53) - lo;
	}

      /* The hidden bit of the lo mantissa is zero so we need to normalize
	 it for the low double.  Shift it left until the hidden bit is '1'
	 then adjust the 2nd exponent accordingly.  */

      if (sizeof (lo) == sizeof (long))
	lzcount = __builtin_clzl (lo);
      else if ((lo >> 32) != 0)
	lzcount = __builtin_clzl ((long) (lo >> 32));
      else
	lzcount = __builtin_clzl ((long) lo) + 32;
      lzcount = lzcount - 11;
      if (lzcount > 0)
	{
	  lo = lo << lzcount;
	  exponent2 = exponent2 - lzcount;
	}
      if (exponent2 > 0)
	u.ieee.exponent2 = exponent2;
      else
	lo >>= 1 - exponent2;
    }
  else
    u.ieee.negative2 = 0;

  u.ieee.mantissa3 = lo & 0xffffffffLL;
  u.ieee.mantissa2 = (lo >> 32) & 0xfffff;
  u.ieee.mantissa1 = hi & 0xffffffffLL;
  u.ieee.mantissa0 = (hi >> 32) & ((1LL << (LDBL_MANT_DIG - 86)) - 1);

  return u.d;
}