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