Blame sysdeps/ieee754/ldbl-128ibm/s_scalbnl.c

Packit 6c4009
/* s_scalbnl.c -- long double version of s_scalbn.c.
Packit 6c4009
 * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* @(#)s_scalbn.c 5.1 93/09/24 */
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBM_SCCS) && !defined(lint)
Packit 6c4009
static char rcsid[] = "$NetBSD: $";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * scalbnl (long double x, int n)
Packit 6c4009
 * scalbnl(x,n) returns x* 2**n  computed by  exponent
Packit 6c4009
 * manipulation rather than by actually performing an
Packit 6c4009
 * exponentiation or a multiplication.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math_ldbl_opt.h>
Packit 6c4009
Packit 6c4009
static const long double
Packit 6c4009
twolm54 = 5.55111512312578270212e-17, /* 0x3C90000000000000, 0 */
Packit 6c4009
huge   = 1.0E+300L,
Packit 6c4009
tiny   = 1.0E-300L;
Packit 6c4009
static const double
Packit 6c4009
two54 = 1.80143985094819840000e+16, /* 0x4350000000000000 */
Packit 6c4009
twom54 = 5.55111512312578270212e-17; /* 0x3C90000000000000 */
Packit 6c4009
Packit 6c4009
long double __scalbnl (long double x, int n)
Packit 6c4009
{
Packit 6c4009
	int64_t k,l,hx,lx;
Packit 6c4009
	union { int64_t i; double d; } u;
Packit 6c4009
	double xhi, xlo;
Packit 6c4009
Packit 6c4009
	ldbl_unpack (x, &xhi, &xlo;;
Packit 6c4009
	EXTRACT_WORDS64 (hx, xhi);
Packit 6c4009
	EXTRACT_WORDS64 (lx, xlo);
Packit 6c4009
	k = (hx>>52)&0x7ff;		/* extract exponent */
Packit 6c4009
	l = (lx>>52)&0x7ff;
Packit 6c4009
	if (k==0) {				/* 0 or subnormal x */
Packit 6c4009
	    if ((hx&0x7fffffffffffffffULL)==0) return x; /* +-0 */
Packit 6c4009
	    u.i = hx;
Packit 6c4009
	    u.d *= two54;
Packit 6c4009
	    hx = u.i;
Packit 6c4009
	    k = ((hx>>52)&0x7ff) - 54;
Packit 6c4009
	}
Packit 6c4009
	else if (k==0x7ff) return x+x;		/* NaN or Inf */
Packit 6c4009
	if (n< -50000) return tiny*__copysignl(tiny,x); /*underflow */
Packit 6c4009
	if (n> 50000 || k+n > 0x7fe)
Packit 6c4009
	  return huge*__copysignl(huge,x); /* overflow */
Packit 6c4009
	/* Now k and n are bounded we know that k = k+n does not
Packit 6c4009
	   overflow.  */
Packit 6c4009
	k = k+n;
Packit 6c4009
	if (k > 0) {				/* normal result */
Packit 6c4009
	    hx = (hx&0x800fffffffffffffULL)|(k<<52);
Packit 6c4009
	    if ((lx & 0x7fffffffffffffffULL) == 0) { /* low part +-0 */
Packit 6c4009
		INSERT_WORDS64 (xhi, hx);
Packit 6c4009
		INSERT_WORDS64 (xlo, lx);
Packit 6c4009
		x = ldbl_pack (xhi, xlo);
Packit 6c4009
		return x;
Packit 6c4009
	    }
Packit 6c4009
	    if (l == 0) { /* low part subnormal */
Packit 6c4009
		u.i = lx;
Packit 6c4009
		u.d *= two54;
Packit 6c4009
		lx = u.i;
Packit 6c4009
		l = ((lx>>52)&0x7ff) - 54;
Packit 6c4009
	    }
Packit 6c4009
	    l = l + n;
Packit 6c4009
	    if (l > 0)
Packit 6c4009
		lx = (lx&0x800fffffffffffffULL)|(l<<52);
Packit 6c4009
	    else if (l <= -54)
Packit 6c4009
		lx = (lx&0x8000000000000000ULL);
Packit 6c4009
	    else {
Packit 6c4009
		l += 54;
Packit 6c4009
		u.i = (lx&0x800fffffffffffffULL)|(l<<52);
Packit 6c4009
		u.d *= twom54;
Packit 6c4009
		lx = u.i;
Packit 6c4009
	    }
Packit 6c4009
	    INSERT_WORDS64 (xhi, hx);
Packit 6c4009
	    INSERT_WORDS64 (xlo, lx);
Packit 6c4009
	    x = ldbl_pack (xhi, xlo);
Packit 6c4009
	    return x;
Packit 6c4009
	}
Packit 6c4009
	if (k <= -54)
Packit 6c4009
	  return tiny*__copysignl(tiny,x); 	/*underflow*/
Packit 6c4009
	k += 54;				/* subnormal result */
Packit 6c4009
	lx &= 0x8000000000000000ULL;
Packit 6c4009
	hx &= 0x800fffffffffffffULL;
Packit 6c4009
	INSERT_WORDS64 (xhi, hx|(k<<52));
Packit 6c4009
	INSERT_WORDS64 (xlo, lx);
Packit 6c4009
	x = ldbl_pack (xhi, xlo);
Packit 6c4009
	return x*twolm54;
Packit 6c4009
}