d03e33ee1f68ca6e1f82ce68dc90285663fffe9a
[jlayton/glibc.git] / sysdeps / ieee754 / dbl-64 / wordsize-64 / s_frexp.c
1 /* Copyright (C) 2011-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <inttypes.h>
20 #include <math.h>
21 #include <math_private.h>
22
23 /*
24  * for non-zero, finite x
25  *      x = frexp(arg,&exp);
26  * return a double fp quantity x such that 0.5 <= |x| <1.0
27  * and the corresponding binary exponent "exp". That is
28  *      arg = x*2^exp.
29  * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg
30  * with *exp=0.
31  */
32
33
34 double
35 __frexp (double x, int *eptr)
36 {
37   int64_t ix;
38   EXTRACT_WORDS64 (ix, x);
39   int32_t ex = 0x7ff & (ix >> 52);
40   int e = 0;
41
42   if (__builtin_expect (ex != 0x7ff && x != 0.0, 1))
43     {
44       /* Not zero and finite.  */
45       e = ex - 1022;
46       if (__builtin_expect (ex == 0, 0))
47         {
48           /* Subnormal.  */
49           x *= 0x1p54;
50           EXTRACT_WORDS64 (ix, x);
51           ex = 0x7ff & (ix >> 52);
52           e = ex - 1022 - 54;
53         }
54
55       ix = (ix & INT64_C (0x800fffffffffffff)) | INT64_C (0x3fe0000000000000);
56       INSERT_WORDS64 (x, ix);
57     }
58
59   *eptr = e;
60   return x;
61 }
62 weak_alias (__frexp, frexp)
63 #ifdef NO_LONG_DOUBLE
64 strong_alias (__frexp, __frexpl)
65 weak_alias (__frexp, frexpl)
66 #endif