Update copyright in libquadmath.
[official-gcc.git] / libquadmath / printf / flt1282mpn.c
blob0c091efb95d880995d0e12d98631774987a56a75
1 /* Copyright (C) 1995-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <float.h>
20 #include <math.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
24 /* Convert a `__float128' in IEEE854 quad-precision format to a
25 multi-precision integer representing the significand scaled up by its
26 number of bits (113 for long double) and an integral power of two
27 (MPN frexpl). */
29 mp_size_t
30 mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size,
31 int *expt, int *is_neg,
32 __float128 value)
34 ieee854_float128 u;
35 u.value = value;
37 *is_neg = u.ieee.negative;
38 *expt = (int) u.ieee.exponent - IEEE854_FLOAT128_BIAS;
40 #if BITS_PER_MP_LIMB == 32
41 res_ptr[0] = u.ieee.mant_low; /* Low-order 32 bits of fraction. */
42 res_ptr[1] = (u.ieee.mant_low >> 32);
43 res_ptr[2] = u.ieee.mant_high;
44 res_ptr[3] = (u.ieee.mant_high >> 32); /* High-order 32 bits. */
45 #define N 4
46 #elif BITS_PER_MP_LIMB == 64
47 res_ptr[0] = u.ieee.mant_low;
48 res_ptr[1] = u.ieee.mant_high;
49 #define N 2
50 #else
51 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
52 #endif
53 /* The format does not fill the last limb. There are some zeros. */
54 #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
55 - (FLT128_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
57 if (u.ieee.exponent == 0)
59 /* A biased exponent of zero is a special case.
60 Either it is a zero or it is a denormal number. */
61 if (res_ptr[0] == 0 && res_ptr[1] == 0
62 && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4. */
63 /* It's zero. */
64 *expt = 0;
65 else
67 /* It is a denormal number, meaning it has no implicit leading
68 one bit, and its exponent is in fact the format minimum. */
69 int cnt;
71 #if N == 2
72 if (res_ptr[N - 1] != 0)
74 count_leading_zeros (cnt, res_ptr[N - 1]);
75 cnt -= NUM_LEADING_ZEROS;
76 res_ptr[N - 1] = res_ptr[N - 1] << cnt
77 | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
78 res_ptr[0] <<= cnt;
79 *expt = FLT128_MIN_EXP - 1 - cnt;
81 else
83 count_leading_zeros (cnt, res_ptr[0]);
84 if (cnt >= NUM_LEADING_ZEROS)
86 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
87 res_ptr[0] = 0;
89 else
91 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
92 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
94 *expt = FLT128_MIN_EXP - 1
95 - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
97 #else
98 int j, k, l;
100 for (j = N - 1; j > 0; j--)
101 if (res_ptr[j] != 0)
102 break;
104 count_leading_zeros (cnt, res_ptr[j]);
105 cnt -= NUM_LEADING_ZEROS;
106 l = N - 1 - j;
107 if (cnt < 0)
109 cnt += BITS_PER_MP_LIMB;
110 l--;
112 if (!cnt)
113 for (k = N - 1; k >= l; k--)
114 res_ptr[k] = res_ptr[k-l];
115 else
117 for (k = N - 1; k > l; k--)
118 res_ptr[k] = res_ptr[k-l] << cnt
119 | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
120 res_ptr[k--] = res_ptr[0] << cnt;
123 for (; k >= 0; k--)
124 res_ptr[k] = 0;
125 *expt = FLT128_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
126 #endif
129 else
130 /* Add the implicit leading one bit for a normalized number. */
131 res_ptr[N - 1] |= (mp_limb_t) 1 << (FLT128_MANT_DIG - 1
132 - ((N - 1) * BITS_PER_MP_LIMB));
134 return N;