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

Packit 6c4009
/* s_ilogbl.c -- long double version of s_ilogb.c.
Packit 6c4009
 * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
Packit 6c4009
 */
Packit 6c4009
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
/* ilogbl(long double x)
Packit 6c4009
 * return the binary exponent of non-zero x
Packit 6c4009
 * ilogbl(0) = FP_ILOGB0
Packit 6c4009
 * ilogbl(NaN) = FP_ILOGBNAN (no signal is raised)
Packit 6c4009
 * ilogbl(+-Inf) = INT_MAX (no signal is raised)
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math_ldbl_opt.h>
Packit 6c4009
Packit 6c4009
int __ieee754_ilogbl(long double x)
Packit 6c4009
{
Packit 6c4009
	int64_t hx, hxs;
Packit 6c4009
	int ix;
Packit 6c4009
	double xhi, xlo;
Packit 6c4009
Packit 6c4009
	ldbl_unpack (x, &xhi, &xlo;;
Packit 6c4009
	EXTRACT_WORDS64 (hx, xhi);
Packit 6c4009
	hxs = hx;
Packit 6c4009
	hx &= 0x7fffffffffffffffLL;
Packit 6c4009
	if(hx <= 0x0010000000000000LL) {
Packit 6c4009
	    if(hx==0)
Packit 6c4009
		return FP_ILOGB0;	/* ilogbl(0) = FP_ILOGB0 */
Packit 6c4009
	    else			/* subnormal x */
Packit 6c4009
		for (ix = -1022, hx<<=11; hx>0; hx<<=1) ix -=1;
Packit 6c4009
	    return ix;
Packit 6c4009
	}
Packit 6c4009
	else if (hx < 0x7ff0000000000000LL)
Packit 6c4009
	  {
Packit 6c4009
	    int hexp = (hx >> 52) - 0x3ff;
Packit 6c4009
	    /* If the high part is a power of 2, and the low part is
Packit 6c4009
	       nonzero with the opposite sign, the low part affects
Packit 6c4009
	       the exponent.  */
Packit 6c4009
	    if ((hx & 0x000fffffffffffffLL) == 0)
Packit 6c4009
	      {
Packit 6c4009
		int64_t lx;
Packit 6c4009
		EXTRACT_WORDS64 (lx, xlo);
Packit 6c4009
		if ((hxs ^ lx) < 0 && (lx & 0x7fffffffffffffffLL) != 0)
Packit 6c4009
		  hexp--;
Packit 6c4009
	      }
Packit 6c4009
	    return hexp;
Packit 6c4009
	  }
Packit 6c4009
	else if (FP_ILOGBNAN != INT_MAX) {
Packit 6c4009
	    /* ISO C99 requires ilogbl(+-Inf) == INT_MAX.  */
Packit 6c4009
	    if (hx==0x7ff0000000000000LL)
Packit 6c4009
		return INT_MAX;
Packit 6c4009
	}
Packit 6c4009
	return FP_ILOGBNAN;
Packit 6c4009
}