source: trunk/third/gmp/insert-dbl.c @ 18191

Revision 18191, 2.1 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* __gmp_scale2 -- Scale a double by exponent manipulation.
2
3Copyright 1996, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include "gmp.h"
23#include "gmp-impl.h"
24
25#ifdef XDEBUG
26#undef _GMP_IEEE_FLOATS
27#endif
28
29#ifndef _GMP_IEEE_FLOATS
30#define _GMP_IEEE_FLOATS 0
31#endif
32
33double
34__gmp_scale2 (double d, int exp)
35{
36#if _GMP_IEEE_FLOATS
37  {
38#if defined (__alpha) && __GNUC__ == 2 && __GNUC_MINOR__ == 8
39    /* Work around alpha-specific bug in GCC 2.8.x.  */
40    volatile
41#endif
42    union ieee_double_extract x;
43    x.d = d;
44    exp += x.s.exp;
45    x.s.exp = exp;
46    if (exp >= 2047)
47      {
48        /* Return +-infinity */
49        x.s.exp = 2047;
50        x.s.manl = x.s.manh = 0;
51      }
52    else if (exp < 1)
53      {
54        x.s.exp = 1;            /* smallest exponent (biased) */
55        /* Divide result by 2 until we have scaled it to the right IEEE
56           denormalized number, but stop if it becomes zero.  */
57        while (exp < 1 && x.d != 0)
58          {
59            x.d *= 0.5;
60            exp++;
61          }
62      }
63    return x.d;
64  }
65#else
66  {
67    double factor, r;
68
69    factor = 2.0;
70    if (exp < 0)
71      {
72        factor = 0.5;
73        exp = -exp;
74      }
75    r = d;
76    if (exp != 0)
77      {
78        if ((exp & 1) != 0)
79          r *= factor;
80        exp >>= 1;
81        while (exp != 0)
82          {
83            factor *= factor;
84            if ((exp & 1) != 0)
85              r *= factor;
86            exp >>= 1;
87          }
88      }
89    return r;
90  }
91#endif
92}
Note: See TracBrowser for help on using the repository browser.