ack/lang/cem/libcc.ansi/core/math/ldexp.c
David Given d1cdb07719 Realise that the libc core can safely call other libc core functions, even if
they're not defined in the core: so putw() can call stdio stuff, for example.
So the earlier concept of pureness isn't necessary. Rename accordingly.
2018-06-21 23:24:23 +02:00

66 lines
984 B
C

/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Id$ */
#include <math.h>
#include <float.h>
#include <errno.h>
double
ldexp(double fl, int exp)
{
int sign = 1;
int currexp;
if (__IsNan(fl))
{
errno = EDOM;
return fl;
}
if (fl == 0.0)
return 0.0;
if (fl < 0)
{
fl = -fl;
sign = -1;
}
if (fl > DBL_MAX)
{ /* for infinity */
errno = ERANGE;
return sign * fl;
}
fl = frexp(fl, &currexp);
exp += currexp;
if (exp > 0)
{
if (exp > DBL_MAX_EXP)
{
errno = ERANGE;
return sign * HUGE_VAL;
}
while (exp > 30)
{
fl *= (double)(1L << 30);
exp -= 30;
}
fl *= (double)(1L << exp);
}
else
{
/* number need not be normalized */
if (exp < DBL_MIN_EXP - DBL_MANT_DIG)
{
return 0.0;
}
while (exp < -30)
{
fl /= (double)(1L << 30);
exp += 30;
}
fl /= (double)(1L << -exp);
}
return sign * fl;
}