xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/frexp.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* Split a double into fraction and mantissa.
2*4b169a6bSchristos    Copyright (C) 2007-2022 Free Software Foundation, Inc.
38dffb485Schristos 
4*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
5*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
6*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
7*4b169a6bSchristos    License, or (at your option) any later version.
88dffb485Schristos 
9*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
108dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
118dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*4b169a6bSchristos    GNU Lesser General Public License for more details.
138dffb485Schristos 
14*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
158dffb485Schristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
168dffb485Schristos 
178dffb485Schristos /* Written by Paolo Bonzini <bonzini@gnu.org>, 2003, and
188dffb485Schristos    Bruno Haible <bruno@clisp.org>, 2007.  */
198dffb485Schristos 
208dffb485Schristos #if ! defined USE_LONG_DOUBLE
218dffb485Schristos # include <config.h>
228dffb485Schristos #endif
238dffb485Schristos 
248dffb485Schristos /* Specification.  */
258dffb485Schristos #include <math.h>
268dffb485Schristos 
278dffb485Schristos #include <float.h>
288dffb485Schristos #ifdef USE_LONG_DOUBLE
298dffb485Schristos # include "isnanl-nolibm.h"
308dffb485Schristos # include "fpucw.h"
318dffb485Schristos #else
328dffb485Schristos # include "isnand-nolibm.h"
338dffb485Schristos #endif
348dffb485Schristos 
358dffb485Schristos /* This file assumes FLT_RADIX = 2.  If FLT_RADIX is a power of 2 greater
368dffb485Schristos    than 2, or not even a power of 2, some rounding errors can occur, so that
378dffb485Schristos    then the returned mantissa is only guaranteed to be <= 1.0, not < 1.0.  */
388dffb485Schristos 
398dffb485Schristos #ifdef USE_LONG_DOUBLE
408dffb485Schristos # define FUNC frexpl
418dffb485Schristos # define DOUBLE long double
428dffb485Schristos # define ISNAN isnanl
438dffb485Schristos # define DECL_ROUNDING DECL_LONG_DOUBLE_ROUNDING
448dffb485Schristos # define BEGIN_ROUNDING() BEGIN_LONG_DOUBLE_ROUNDING ()
458dffb485Schristos # define END_ROUNDING() END_LONG_DOUBLE_ROUNDING ()
468dffb485Schristos # define L_(literal) literal##L
478dffb485Schristos #else
488dffb485Schristos # define FUNC frexp
498dffb485Schristos # define DOUBLE double
508dffb485Schristos # define ISNAN isnand
518dffb485Schristos # define DECL_ROUNDING
528dffb485Schristos # define BEGIN_ROUNDING()
538dffb485Schristos # define END_ROUNDING()
548dffb485Schristos # define L_(literal) literal
558dffb485Schristos #endif
568dffb485Schristos 
578dffb485Schristos DOUBLE
FUNC(DOUBLE x,int * expptr)588dffb485Schristos FUNC (DOUBLE x, int *expptr)
598dffb485Schristos {
608dffb485Schristos   int sign;
618dffb485Schristos   int exponent;
628dffb485Schristos   DECL_ROUNDING
638dffb485Schristos 
648dffb485Schristos   /* Test for NaN, infinity, and zero.  */
658dffb485Schristos   if (ISNAN (x) || x + x == x)
668dffb485Schristos     {
678dffb485Schristos       *expptr = 0;
688dffb485Schristos       return x;
698dffb485Schristos     }
708dffb485Schristos 
718dffb485Schristos   sign = 0;
728dffb485Schristos   if (x < 0)
738dffb485Schristos     {
748dffb485Schristos       x = - x;
758dffb485Schristos       sign = -1;
768dffb485Schristos     }
778dffb485Schristos 
788dffb485Schristos   BEGIN_ROUNDING ();
798dffb485Schristos 
808dffb485Schristos   {
818dffb485Schristos     /* Since the exponent is an 'int', it fits in 64 bits.  Therefore the
828dffb485Schristos        loops are executed no more than 64 times.  */
838dffb485Schristos     DOUBLE pow2[64]; /* pow2[i] = 2^2^i */
848dffb485Schristos     DOUBLE powh[64]; /* powh[i] = 2^-2^i */
858dffb485Schristos     int i;
868dffb485Schristos 
878dffb485Schristos     exponent = 0;
888dffb485Schristos     if (x >= L_(1.0))
898dffb485Schristos       {
908dffb485Schristos         /* A positive exponent.  */
918dffb485Schristos         DOUBLE pow2_i; /* = pow2[i] */
928dffb485Schristos         DOUBLE powh_i; /* = powh[i] */
938dffb485Schristos 
948dffb485Schristos         /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
958dffb485Schristos            x * 2^exponent = argument, x >= 1.0.  */
968dffb485Schristos         for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
978dffb485Schristos              ;
988dffb485Schristos              i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
998dffb485Schristos           {
1008dffb485Schristos             if (x >= pow2_i)
1018dffb485Schristos               {
1028dffb485Schristos                 exponent += (1 << i);
1038dffb485Schristos                 x *= powh_i;
1048dffb485Schristos               }
1058dffb485Schristos             else
1068dffb485Schristos               break;
1078dffb485Schristos 
1088dffb485Schristos             pow2[i] = pow2_i;
1098dffb485Schristos             powh[i] = powh_i;
1108dffb485Schristos           }
1118dffb485Schristos         /* Avoid making x too small, as it could become a denormalized
1128dffb485Schristos            number and thus lose precision.  */
1138dffb485Schristos         while (i > 0 && x < pow2[i - 1])
1148dffb485Schristos           {
1158dffb485Schristos             i--;
1168dffb485Schristos             powh_i = powh[i];
1178dffb485Schristos           }
1188dffb485Schristos         exponent += (1 << i);
1198dffb485Schristos         x *= powh_i;
1208dffb485Schristos         /* Here 2^-2^i <= x < 1.0.  */
1218dffb485Schristos       }
1228dffb485Schristos     else
1238dffb485Schristos       {
1248dffb485Schristos         /* A negative or zero exponent.  */
1258dffb485Schristos         DOUBLE pow2_i; /* = pow2[i] */
1268dffb485Schristos         DOUBLE powh_i; /* = powh[i] */
1278dffb485Schristos 
1288dffb485Schristos         /* Invariants: pow2_i = 2^2^i, powh_i = 2^-2^i,
1298dffb485Schristos            x * 2^exponent = argument, x < 1.0.  */
1308dffb485Schristos         for (i = 0, pow2_i = L_(2.0), powh_i = L_(0.5);
1318dffb485Schristos              ;
1328dffb485Schristos              i++, pow2_i = pow2_i * pow2_i, powh_i = powh_i * powh_i)
1338dffb485Schristos           {
1348dffb485Schristos             if (x < powh_i)
1358dffb485Schristos               {
1368dffb485Schristos                 exponent -= (1 << i);
1378dffb485Schristos                 x *= pow2_i;
1388dffb485Schristos               }
1398dffb485Schristos             else
1408dffb485Schristos               break;
1418dffb485Schristos 
1428dffb485Schristos             pow2[i] = pow2_i;
1438dffb485Schristos             powh[i] = powh_i;
1448dffb485Schristos           }
1458dffb485Schristos         /* Here 2^-2^i <= x < 1.0.  */
1468dffb485Schristos       }
1478dffb485Schristos 
1488dffb485Schristos     /* Invariants: x * 2^exponent = argument, and 2^-2^i <= x < 1.0.  */
1498dffb485Schristos     while (i > 0)
1508dffb485Schristos       {
1518dffb485Schristos         i--;
1528dffb485Schristos         if (x < powh[i])
1538dffb485Schristos           {
1548dffb485Schristos             exponent -= (1 << i);
1558dffb485Schristos             x *= pow2[i];
1568dffb485Schristos           }
1578dffb485Schristos       }
1588dffb485Schristos     /* Here 0.5 <= x < 1.0.  */
1598dffb485Schristos   }
1608dffb485Schristos 
1618dffb485Schristos   if (sign < 0)
1628dffb485Schristos     x = - x;
1638dffb485Schristos 
1648dffb485Schristos   END_ROUNDING ();
1658dffb485Schristos 
1668dffb485Schristos   *expptr = exponent;
1678dffb485Schristos   return x;
1688dffb485Schristos }
169