1 /* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. 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 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. 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 12 Lesser General Public License for more details. 13 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. */ 18 19 #include "gmp.h" 20 #include "gmp-impl.h" 21 #include "longlong.h" 22 #include <ieee754.h> 23 #include <float.h> 24 #include <math.h> 25 #include <stdlib.h> 26 27 /* Convert a `long double' in IEEE854 standard double-precision format to a 28 multi-precision integer representing the significand scaled up by its 29 number of bits (64 for long double) and an integral power of two 30 (MPN frexpl). */ 31 32 mp_size_t 33 __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size, 34 int *expt, int *is_neg, 35 long double value) 36 { 37 union ieee854_long_double u; 38 u.d = value; 39 40 *is_neg = u.ieee.negative; 41 *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS; 42 43 #if BITS_PER_MP_LIMB == 32 44 res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction. */ 45 res_ptr[1] = u.ieee.mantissa0; /* High-order 32 bits. */ 46 #define N 2 47 #elif BITS_PER_MP_LIMB == 64 48 /* Hopefully the compiler will combine the two bitfield extracts 49 and this composition into just the original quadword extract. */ 50 res_ptr[0] = ((unsigned long int) u.ieee.mantissa0 << 32) | u.ieee.mantissa1; 51 #define N 1 52 #else 53 #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for" 54 #endif 55 56 if (u.ieee.exponent == 0) 57 { 58 /* A biased exponent of zero is a special case. 59 Either it is a zero or it is a denormal number. */ 60 if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2. */ 61 /* It's zero. */ 62 *expt = 0; 63 else 64 { 65 /* It is a denormal number, meaning it has no implicit leading 66 one bit, and its exponent is in fact the format minimum. */ 67 int cnt; 68 69 if (res_ptr[N - 1] != 0) 70 { 71 count_leading_zeros (cnt, res_ptr[N - 1]); 72 if (cnt != 0) 73 { 74 #if N == 2 75 res_ptr[N - 1] = res_ptr[N - 1] << cnt 76 | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt)); 77 res_ptr[0] <<= cnt; 78 #else 79 res_ptr[N - 1] <<= cnt; 80 #endif 81 } 82 *expt = LDBL_MIN_EXP - 1 - cnt; 83 } 84 else 85 { 86 count_leading_zeros (cnt, res_ptr[0]); 87 res_ptr[N - 1] = res_ptr[0] << cnt; 88 res_ptr[0] = 0; 89 *expt = LDBL_MIN_EXP - 1 - BITS_PER_MP_LIMB - cnt; 90 } 91 } 92 } 93 94 return N; 95 } 96