1/* Split a double into fraction and mantissa, for hexadecimal printf.
2   Copyright (C) 2007, 2009, 2010 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 Lesser General Public License as published by
6   the Free Software Foundation; either version 3 of the License, or
7   (at your option) 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 Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17#include <config.h>
18
19/* Specification.  */
20#ifdef USE_LONG_DOUBLE
21# include "printf-frexpl.h"
22#else
23# include "printf-frexp.h"
24#endif
25
26#include <float.h>
27#include <math.h>
28#ifdef USE_LONG_DOUBLE
29# include "fpucw.h"
30#endif
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 DECL_ROUNDING DECL_LONG_DOUBLE_ROUNDING
46# define BEGIN_ROUNDING() BEGIN_LONG_DOUBLE_ROUNDING ()
47# define END_ROUNDING() END_LONG_DOUBLE_ROUNDING ()
48# define L_(literal) literal##L
49#else
50# define FUNC printf_frexp
51# define DOUBLE double
52# define MIN_EXP DBL_MIN_EXP
53# if HAVE_FREXP_IN_LIBC && HAVE_LDEXP_IN_LIBC
54#  define USE_FREXP_LDEXP
55#  define FREXP frexp
56#  define LDEXP ldexp
57# endif
58# define DECL_ROUNDING
59# define BEGIN_ROUNDING()
60# define END_ROUNDING()
61# define L_(literal) literal
62#endif
63
64DOUBLE
65FUNC (DOUBLE x, int *expptr)
66{
67  int exponent;
68  DECL_ROUNDING
69
70  BEGIN_ROUNDING ();
71
72#ifdef USE_FREXP_LDEXP
73  /* frexp and ldexp are usually faster than the loop below.  */
74  x = FREXP (x, &exponent);
75
76  x = x + x;
77  exponent -= 1;
78
79  if (exponent < MIN_EXP - 1)
80    {
81      x = LDEXP (x, exponent - (MIN_EXP - 1));
82      exponent = MIN_EXP - 1;
83    }
84#else
85  {
86    /* Since the exponent is an 'int', it fits in 64 bits.  Therefore the
87       loops are executed no more than 64 times.  */
88    DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
89    DOUBLE powh[64]; /* powh[i] = 2^-2^i */
90    int i;
91
92    exponent = 0;
93    if (x >= L_(1.0))
94      {
95        /* A nonnegative exponent.  */
96        {
97          DOUBLE pow2_i; /* = pow2[i] */
98          DOUBLE powh_i; /* = powh[i] */
99
100          /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
101             x * 2^exponent = argument, x >= 1.0.  */
102          for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
103               ;
104               i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
105            {
106              if (x >= pow2_i)
107                {
108                  exponent += (1 << i);
109                  x *= powh_i;
110                }
111              else
112                break;
113
114              pow2[i] = pow2_i;
115              powh[i] = powh_i;
116            }
117        }
118        /* Here 1.0 <= x < 2^2^i.  */
119      }
120    else
121      {
122        /* A negative exponent.  */
123        {
124          DOUBLE pow2_i; /* = pow2[i] */
125          DOUBLE powh_i; /* = powh[i] */
126
127          /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
128             x * 2^exponent = argument, x < 1.0, exponent >= MIN_EXP - 1.  */
129          for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
130               ;
131               i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
132            {
133              if (exponent - (1 << i) < MIN_EXP - 1)
134                break;
135
136              exponent -= (1 << i);
137              x *= pow2_i;
138              if (x >= L_(1.0))
139                break;
140
141              pow2[i] = pow2_i;
142              powh[i] = powh_i;
143            }
144        }
145        /* Here either x < 1.0 and exponent - 2^i < MIN_EXP - 1 <= exponent,
146           or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
147
148        if (x < L_(1.0))
149          /* Invariants: x * 2^exponent = argument, x < 1.0 and
150             exponent - 2^i < MIN_EXP - 1 <= exponent.  */
151          while (i > 0)
152            {
153              i--;
154              if (exponent - (1 << i) >= MIN_EXP - 1)
155                {
156                  exponent -= (1 << i);
157                  x *= pow2[i];
158                  if (x >= L_(1.0))
159                    break;
160                }
161            }
162
163        /* Here either x < 1.0 and exponent = MIN_EXP - 1,
164           or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
165      }
166
167    /* Invariants: x * 2^exponent = argument, and
168       either x < 1.0 and exponent = MIN_EXP - 1,
169       or 1.0 <= x < 2^2^i and exponent >= MIN_EXP - 1.  */
170    while (i > 0)
171      {
172        i--;
173        if (x >= pow2[i])
174          {
175            exponent += (1 << i);
176            x *= powh[i];
177          }
178      }
179    /* Here either x < 1.0 and exponent = MIN_EXP - 1,
180       or 1.0 <= x < 2.0 and exponent >= MIN_EXP - 1.  */
181  }
182#endif
183
184  END_ROUNDING ();
185
186  *expptr = exponent;
187  return x;
188}
189