Fix problem with getdate on mingw32 reported by Simon Josefsson
[gnulib.git] / lib / ldexpl.c
1 /* Emulation for ldexpl.
2    Contributed by Paolo Bonzini
3
4    Copyright 2002, 2003, 2007 Free Software Foundation, Inc.
5
6    This file is part of gnulib.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #include <math.h>
25
26 #include <float.h>
27 #include "fpucw.h"
28 #include "isnanl.h"
29
30 long double
31 ldexpl(long double x, int exp)
32 {
33   long double factor;
34   int bit;
35   DECL_LONG_DOUBLE_ROUNDING
36
37   BEGIN_LONG_DOUBLE_ROUNDING ();
38
39   /* Check for zero, nan and infinity. */
40   if (!(isnanl (x) || x + x == x))
41     {
42       if (exp < 0)
43         {
44           exp = -exp;
45           factor = 0.5L;
46         }
47       else
48         factor = 2.0L;
49
50       if (exp > 0)
51         for (bit = 1;;)
52           {
53             /* Invariant: Here bit = 2^i, factor = 2^-2^i or = 2^2^i,
54                and bit <= exp.  */
55             if (exp & bit)
56               x *= factor;
57             bit <<= 1;
58             if (bit > exp)
59               break;
60             factor = factor * factor;
61           }
62     }
63
64   END_LONG_DOUBLE_ROUNDING ();
65
66   return x;
67 }
68
69 #if 0
70 int
71 main (void)
72 {
73   long double x;
74   int y;
75   for (y = 0; y < 29; y++)
76     printf ("%5d %.16Lg %.16Lg\n", y, ldexpl(0.8L, y), ldexpl(0.8L, -y) * ldexpl(0.8L, y));
77 }
78 #endif