Provide missing frexpl(), ldexpl() declarations.
[pspp] / lib / printf-frexp.c
1 /* Split a double into fraction and mantissa, for hexadecimal printf.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 #if !(defined USE_LONG_DOUBLE && !HAVE_LONG_DOUBLE)
21
22 /* Specification.  */
23 # ifdef USE_LONG_DOUBLE
24 #  include "printf-frexpl.h"
25 # else
26 #  include "printf-frexp.h"
27 # endif
28
29 # include <float.h>
30 # include <math.h>
31
32 /* This file assumes FLT_RADIX = 2.  If FLT_RADIX is a power of 2 greater
33    than 2, or not even a power of 2, some rounding errors can occur, so that
34    then the returned mantissa is only guaranteed to be <= 2.0, not < 2.0.  */
35
36 # ifdef USE_LONG_DOUBLE
37 #  define FUNC printf_frexpl
38 #  define DOUBLE long double
39 #  define MIN_EXP LDBL_MIN_EXP
40 #  if HAVE_FREXPL_IN_LIBC && HAVE_LDEXPL_IN_LIBC
41 #   define USE_FREXP_LDEXP
42 #   define FREXP frexpl
43 #   define LDEXP ldexpl
44     /* glibc (2.3..2.5 at least) and MacOS X 10.3 have frexpl and ldexpl in
45        libc, but don't declare them.  */
46 #   if !HAVE_DECL_FREXPL
47 extern long double frexpl (long double, int *);
48 #   endif
49 #   if !HAVE_DECL_LDEXPL
50 extern long double ldexpl (long double, int);
51 #   endif
52 #  endif
53 #  define L_(literal) literal##L
54 # else
55 #  define FUNC printf_frexp
56 #  define DOUBLE double
57 #  define MIN_EXP DBL_MIN_EXP
58 #  if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
59 #   define USE_FREXP_LDEXP
60 #   define FREXP frexp
61 #   define LDEXP ldexp
62 #  endif
63 #  define L_(literal) literal
64 # endif
65
66 DOUBLE
67 FUNC (DOUBLE x, int *exp)
68 {
69   int exponent;
70
71 # ifdef USE_FREXP_LDEXP
72   /* frexp and ldexp are usually faster than the loop below.  */
73   x = FREXP (x, &exponent);
74
75   x = x + x;
76   exponent -= 1;
77
78   if (exponent < MIN_EXP - 1)
79     {
80       x = LDEXP (x, exponent - (MIN_EXP - 1));
81       exponent = MIN_EXP - 1;
82     }
83 # else
84   /* Since the exponent is an 'int', it fits in 64 bits.  Therefore the
85      loops are executed no more than 64 times.  */
86   DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
87   DOUBLE powh[64]; /* powh[i] = 2^-2^i */
88   int i;
89
90   exponent = 0;
91   if (x >= L_(1.0))
92     {
93       /* A nonnegative exponent.  */
94       {
95         DOUBLE pow2_i; /* = pow2[i] */
96         DOUBLE powh_i; /* = powh[i] */
97
98         /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
99            x * 2^exponent = argument, x >= 1.0.  */
100         for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
101              ;
102              i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
103           {
104             if (x >= pow2_i)
105               {
106                 exponent += (1 << i);
107                 x *= powh_i;
108               }
109             else
110               break;
111
112             pow2[i] = pow2_i;
113             powh[i] = powh_i;
114           }
115       }
116       /* Here 1.0 <= x < 2^2^i.  */
117     }
118   else
119     {
120       /* A negative exponent.  */
121       {
122         DOUBLE pow2_i; /* = pow2[i] */
123         DOUBLE powh_i; /* = powh[i] */
124
125         /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
126            x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1.  */
127         for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
128              ;
129              i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
130           {
131             if (exponent - (1 << i) < MIN_EXP - 1)
132               break;
133
134             exponent -= (1 << i);
135             x *= pow2_i;
136             if (x >= L_(1.0))
137               break;
138
139             pow2[i] = pow2_i;
140             powh[i] = powh_i;
141           }
142       }
143       /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
144          or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
145
146       if (x < L_(1.0))
147         /* Invariants: x * 2^exponent = argument, x < 1.0 and
148            exponent - 2^i < MIN_EXP - 1 <= exponent.  */
149         while (i > 0)
150           {
151             i--;
152             if (exponent - (1 << i) >= MIN_EXP - 1)
153               {
154                 exponent -= (1 << i);
155                 x *= pow2[i];
156                 if (x >= L_(1.0))
157                   break;
158               }
159           }
160
161       /* Here either x < 1.0 and exponent = MIN_EXP - 1,
162          or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
163     }
164
165   /* Invariants: x * 2^exponent = argument, and
166      either x < 1.0 and exponent = MIN_EXP - 1,
167      or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
168   while (i > 0)
169     {
170       i--;
171       if (x >= pow2[i])
172         {
173           exponent += (1 << i);
174           x *= powh[i];
175         }
176     }
177   /* Here either x < 1.0 and exponent = MIN_EXP - 1,
178      or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1.  */
179 # endif
180
181   *exp = exponent;
182   return x;
183 }
184
185 #else
186
187 /* This declaration is solely to ensure that after preprocessing
188    this file is never empty.  */
189 typedef int dummy;
190
191 #endif