No assumptions about FLT_RADIX need to be made.
[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 # ifdef USE_LONG_DOUBLE
33 #  define FUNC printf_frexpl
34 #  define DOUBLE long double
35 #  define MIN_EXP LDBL_MIN_EXP
36 #  if HAVE_FREXPL_IN_LIBC && HAVE_LDEXPL_IN_LIBC
37 #   define USE_FREXP_LDEXP
38 #   define FREXP frexpl
39 #   define LDEXP ldexpl
40 #  endif
41 #  define L_(literal) literal##L
42 # else
43 #  define FUNC printf_frexp
44 #  define DOUBLE double
45 #  define MIN_EXP DBL_MIN_EXP
46 #  if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
47 #   define USE_FREXP_LDEXP
48 #   define FREXP frexp
49 #   define LDEXP ldexp
50 #  endif
51 #  define L_(literal) literal
52 # endif
53
54 DOUBLE
55 FUNC (DOUBLE x, int *exp)
56 {
57   int exponent;
58
59 # ifdef USE_FREXP_LDEXP
60   /* frexp and ldexp are usually faster than the loop below.  */
61   x = FREXP (x, &exponent);
62
63   x = x + x;
64   exponent -= 1;
65
66   if (exponent < MIN_EXP - 1)
67     {
68       x = LDEXP (x, exponent - (MIN_EXP - 1));
69       exponent = MIN_EXP - 1;
70     }
71 # else
72   /* Since the exponent is an 'int', it fits in 64 bits.  Therefore the
73      loops are executed no more than 64 times.  */
74   DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
75   DOUBLE powh[64]; /* powh[i] = 2^-2^i */
76   int i;
77
78   exponent = 0;
79   if (x >= L_(1.0))
80     {
81       /* A nonnegative exponent.  */
82       {
83         DOUBLE pow2_i; /* = pow2[i] */
84         DOUBLE powh_i; /* = powh[i] */
85
86         /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
87            x * 2^exponent = argument, x >= 1.0.  */
88         for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
89              ;
90              i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
91           {
92             if (x >= pow2_i)
93               {
94                 exponent += (1 << i);
95                 x *= powh_i;
96               }
97             else
98               break;
99
100             pow2[i] = pow2_i;
101             powh[i] = powh_i;
102           }
103       }
104       /* Here 1.0 <= x < 2^2^i.  */
105     }
106   else
107     {
108       /* A negative exponent.  */
109       {
110         DOUBLE pow2_i; /* = pow2[i] */
111         DOUBLE powh_i; /* = powh[i] */
112
113         /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
114            x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1.  */
115         for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
116              ;
117              i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
118           {
119             if (exponent - (1 << i) < MIN_EXP - 1)
120               break;
121
122             exponent -= (1 << i);
123             x *= pow2_i;
124             if (x >= L_(1.0))
125               break;
126
127             pow2[i] = pow2_i;
128             powh[i] = powh_i;
129           }
130       }
131       /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
132          or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
133
134       if (x < L_(1.0))
135         /* Invariants: x * 2^exponent = argument, x < 1.0 and
136            exponent - 2^i < MIN_EXP - 1 <= exponent.  */
137         while (i > 0)
138           {
139             i--;
140             if (exponent - (1 << i) >= MIN_EXP - 1)
141               {
142                 exponent -= (1 << i);
143                 x *= pow2[i];
144                 if (x >= L_(1.0))
145                   break;
146               }
147           }
148
149       /* Here either x < 1.0 and exponent = MIN_EXP - 1,
150          or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
151     }
152
153   /* Invariants: x * 2^exponent = argument, and
154      either x < 1.0 and exponent = MIN_EXP - 1,
155      or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
156   while (i > 0)
157     {
158       i--;
159       if (x >= pow2[i])
160         {
161           exponent += (1 << i);
162           x *= powh[i];
163         }
164     }
165   /* Here either x < 1.0 and exponent = MIN_EXP - 1,
166      or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1.  */
167 # endif
168
169   *exp = exponent;
170   return x;
171 }
172
173 #else
174
175 /* This declaration is solely to ensure that after preprocessing
176    this file is never empty.  */
177 typedef int dummy;
178
179 #endif