]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/ldbl-128/ldbl2mpn.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-128 / ldbl2mpn.c
CommitLineData
bfff8b1b 1/* Copyright (C) 1995-2017 Free Software Foundation, Inc.
377a515b
UD
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
377a515b
UD
8
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
41bdb6e2 12 Lesser General Public License for more details.
377a515b 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4
RM
17
18#include "gmp.h"
19#include "gmp-impl.h"
20#include "longlong.h"
981993e1 21#include <ieee754.h>
28f540f4 22#include <float.h>
abfbdde1 23#include <math.h>
28f540f4
RM
24#include <stdlib.h>
25
abfbdde1 26/* Convert a `long double' in IEEE854 quad-precision format to a
28f540f4 27 multi-precision integer representing the significand scaled up by its
abfbdde1
UD
28 number of bits (113 for long double) and an integral power of two
29 (MPN frexpl). */
28f540f4
RM
30
31mp_size_t
abfbdde1
UD
32__mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
33 int *expt, int *is_neg,
34 long double value)
28f540f4 35{
abfbdde1 36 union ieee854_long_double u;
28f540f4
RM
37 u.d = value;
38
39 *is_neg = u.ieee.negative;
abfbdde1 40 *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS;
28f540f4
RM
41
42#if BITS_PER_MP_LIMB == 32
abfbdde1
UD
43 res_ptr[0] = u.ieee.mantissa3; /* Low-order 32 bits of fraction. */
44 res_ptr[1] = u.ieee.mantissa2;
45 res_ptr[2] = u.ieee.mantissa1;
46 res_ptr[3] = u.ieee.mantissa0; /* High-order 32 bits. */
47 #define N 4
28f540f4
RM
48#elif BITS_PER_MP_LIMB == 64
49 /* Hopefully the compiler will combine the two bitfield extracts
50 and this composition into just the original quadword extract. */
d8a5edc2
RM
51 res_ptr[0] = ((mp_limb_t) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
52 res_ptr[1] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
abfbdde1 53 #define N 2
28f540f4
RM
54#else
55 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
56#endif
57/* The format does not fill the last limb. There are some zeros. */
58#define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
abfbdde1 59 - (LDBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
28f540f4
RM
60
61 if (u.ieee.exponent == 0)
62 {
63 /* A biased exponent of zero is a special case.
64 Either it is a zero or it is a denormal number. */
abfbdde1
UD
65 if (res_ptr[0] == 0 && res_ptr[1] == 0
66 && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4. */
28f540f4
RM
67 /* It's zero. */
68 *expt = 0;
69 else
70 {
71 /* It is a denormal number, meaning it has no implicit leading
350635a5 72 one bit, and its exponent is in fact the format minimum. */
28f540f4
RM
73 int cnt;
74
abfbdde1 75#if N == 2
28f540f4
RM
76 if (res_ptr[N - 1] != 0)
77 {
78 count_leading_zeros (cnt, res_ptr[N - 1]);
79 cnt -= NUM_LEADING_ZEROS;
abfbdde1
UD
80 res_ptr[N - 1] = res_ptr[N - 1] << cnt
81 | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
28f540f4 82 res_ptr[0] <<= cnt;
abfbdde1 83 *expt = LDBL_MIN_EXP - 1 - cnt;
28f540f4
RM
84 }
85 else
86 {
87 count_leading_zeros (cnt, res_ptr[0]);
88 if (cnt >= NUM_LEADING_ZEROS)
89 {
90 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
a182affd 91 res_ptr[0] = 0;
28f540f4
RM
92 }
93 else
94 {
95 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
96 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
97 }
abfbdde1
UD
98 *expt = LDBL_MIN_EXP - 1
99 - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
100 }
101#else
102 int j, k, l;
103
52e1b618 104 for (j = N - 1; j > 0; j--)
abfbdde1
UD
105 if (res_ptr[j] != 0)
106 break;
107
108 count_leading_zeros (cnt, res_ptr[j]);
109 cnt -= NUM_LEADING_ZEROS;
110 l = N - 1 - j;
111 if (cnt < 0)
112 {
113 cnt += BITS_PER_MP_LIMB;
52e1b618 114 l--;
28f540f4 115 }
abfbdde1
UD
116 if (!cnt)
117 for (k = N - 1; k >= l; k--)
118 res_ptr[k] = res_ptr[k-l];
119 else
52e1b618
UD
120 {
121 for (k = N - 1; k > l; k--)
122 res_ptr[k] = res_ptr[k-l] << cnt
123 | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
124 res_ptr[k--] = res_ptr[0] << cnt;
125 }
abfbdde1
UD
126
127 for (; k >= 0; k--)
128 res_ptr[k] = 0;
52e1b618 129 *expt = LDBL_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
abfbdde1 130#endif
28f540f4
RM
131 }
132 }
133 else
134 /* Add the implicit leading one bit for a normalized number. */
d8a5edc2
RM
135 res_ptr[N - 1] |= (mp_limb_t) 1 << (LDBL_MANT_DIG - 1
136 - ((N - 1) * BITS_PER_MP_LIMB));
28f540f4
RM
137
138 return N;
139}