133965Sjdp/* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc. 2218822Sdim This file is part of the GNU C Library. 3218822Sdim 433965Sjdp The GNU C Library is free software; you can redistribute it and/or 533965Sjdp modify it under the terms of the GNU Lesser General Public 633965Sjdp License as published by the Free Software Foundation; either 733965Sjdp version 2.1 of the License, or (at your option) any later version. 833965Sjdp 933965Sjdp The GNU C Library is distributed in the hope that it will be useful, 1033965Sjdp but WITHOUT ANY WARRANTY; without even the implied warranty of 1133965Sjdp MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1233965Sjdp Lesser General Public License for more details. 1333965Sjdp 1433965Sjdp You should have received a copy of the GNU Lesser General Public 1533965Sjdp License along with the GNU C Library; if not, write to the Free 1633965Sjdp Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 1733965Sjdp 02111-1307 USA. */ 1833965Sjdp 19218822Sdim#include "gmp.h" 20218822Sdim#include "gmp-impl.h" 2133965Sjdp#include "longlong.h" 2233965Sjdp#include <ieee754.h> 2333965Sjdp#include <float.h> 2433965Sjdp#include <stdlib.h> 2533965Sjdp 2633965Sjdp/* Convert a `double' in IEEE754 standard double-precision format to a 2733965Sjdp multi-precision integer representing the significand scaled up by its 2833965Sjdp number of bits (52 for double) and an integral power of two (MPN frexp). */ 2933965Sjdp 3033965Sjdpmp_size_t 3133965Sjdp__mpn_extract_double (mp_ptr res_ptr, mp_size_t size, 3233965Sjdp int *expt, int *is_neg, 3333965Sjdp double value) 3433965Sjdp{ 3533965Sjdp union ieee754_double u; 3633965Sjdp u.d = value; 3733965Sjdp 3833965Sjdp *is_neg = u.ieee.negative; 3933965Sjdp *expt = (int) u.ieee.exponent - IEEE754_DOUBLE_BIAS; 4033965Sjdp 4133965Sjdp#if BITS_PER_MP_LIMB == 32 4233965Sjdp res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction. */ 4333965Sjdp res_ptr[1] = u.ieee.mantissa0; /* High-order 20 bits. */ 4433965Sjdp #define N 2 4533965Sjdp#elif BITS_PER_MP_LIMB == 64 4633965Sjdp /* Hopefully the compiler will combine the two bitfield extracts 4733965Sjdp and this composition into just the original quadword extract. */ 4833965Sjdp res_ptr[0] = ((unsigned long int) u.ieee.mantissa0 << 32) | u.ieee.mantissa1; 4933965Sjdp #define N 1 5033965Sjdp#else 5133965Sjdp #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for" 5233965Sjdp#endif 5333965Sjdp/* The format does not fill the last limb. There are some zeros. */ 5433965Sjdp#define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \ 5533965Sjdp - (DBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB))) 5633965Sjdp 5733965Sjdp if (u.ieee.exponent == 0) 5833965Sjdp { 5933965Sjdp /* A biased exponent of zero is a special case. 6033965Sjdp Either it is a zero or it is a denormal number. */ 6133965Sjdp if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2. */ 6233965Sjdp /* It's zero. */ 6333965Sjdp *expt = 0; 6433965Sjdp else 6533965Sjdp { 6633965Sjdp /* It is a denormal number, meaning it has no implicit leading 6733965Sjdp one bit, and its exponent is in fact the format minimum. */ 6833965Sjdp int cnt; 6933965Sjdp 7033965Sjdp if (res_ptr[N - 1] != 0) 7133965Sjdp { 7233965Sjdp count_leading_zeros (cnt, res_ptr[N - 1]); 7333965Sjdp cnt -= NUM_LEADING_ZEROS; 7433965Sjdp#if N == 2 7533965Sjdp res_ptr[N - 1] = res_ptr[1] << cnt 7633965Sjdp | (N - 1) 7733965Sjdp * (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt)); 7833965Sjdp res_ptr[0] <<= cnt; 7933965Sjdp#else 8033965Sjdp res_ptr[N - 1] <<= cnt; 8133965Sjdp#endif 8233965Sjdp *expt = DBL_MIN_EXP - 1 - cnt; 8333965Sjdp } 8433965Sjdp else 8533965Sjdp { 8633965Sjdp count_leading_zeros (cnt, res_ptr[0]); 8733965Sjdp if (cnt >= NUM_LEADING_ZEROS) 8833965Sjdp { 8933965Sjdp res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS); 9033965Sjdp res_ptr[0] = 0; 9133965Sjdp } 92218822Sdim else 9333965Sjdp { 94107492Sobrien res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt); 9533965Sjdp res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt); 9633965Sjdp } 9733965Sjdp *expt = DBL_MIN_EXP - 1 9833965Sjdp - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt; 9933965Sjdp } 10033965Sjdp } 10133965Sjdp } 10233965Sjdp else 10333965Sjdp /* Add the implicit leading one bit for a normalized number. */ 10433965Sjdp res_ptr[N - 1] |= 1L << (DBL_MANT_DIG - 1 - ((N - 1) * BITS_PER_MP_LIMB)); 10533965Sjdp 10633965Sjdp return N; 10733965Sjdp} 10833965Sjdp