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