1*181254a7Smrg /* s_ilogbl.c -- long double version of s_ilogb.c.
2*181254a7Smrg * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3*181254a7Smrg */
4*181254a7Smrg
5*181254a7Smrg /*
6*181254a7Smrg * ====================================================
7*181254a7Smrg * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*181254a7Smrg *
9*181254a7Smrg * Developed at SunPro, a Sun Microsystems, Inc. business.
10*181254a7Smrg * Permission to use, copy, modify, and distribute this
11*181254a7Smrg * software is freely granted, provided that this notice
12*181254a7Smrg * is preserved.
13*181254a7Smrg * ====================================================
14*181254a7Smrg */
15*181254a7Smrg
16*181254a7Smrg #if defined(LIBM_SCCS) && !defined(lint)
17*181254a7Smrg static char rcsid[] = "NetBSD: ";
18*181254a7Smrg #endif
19*181254a7Smrg
20*181254a7Smrg /* ilogbl(long double x)
21*181254a7Smrg * return the binary exponent of non-zero x
22*181254a7Smrg * ilogbl(0) = FP_ILOGB0
23*181254a7Smrg * ilogbl(NaN) = FP_ILOGBNAN (no signal is raised)
24*181254a7Smrg * ilogbl(+-Inf) = INT_MAX (no signal is raised)
25*181254a7Smrg */
26*181254a7Smrg
27*181254a7Smrg #include <math.h>
28*181254a7Smrg #include "quadmath-imp.h"
29*181254a7Smrg #ifndef FP_ILOGB0
30*181254a7Smrg # define FP_ILOGB0 INT_MIN
31*181254a7Smrg #endif
32*181254a7Smrg #ifndef FP_ILOGBNAN
33*181254a7Smrg # define FP_ILOGBNAN INT_MAX
34*181254a7Smrg #endif
35*181254a7Smrg
ilogbq(__float128 x)36*181254a7Smrg int ilogbq (__float128 x)
37*181254a7Smrg {
38*181254a7Smrg int64_t hx,lx;
39*181254a7Smrg int ix;
40*181254a7Smrg
41*181254a7Smrg GET_FLT128_WORDS64(hx,lx,x);
42*181254a7Smrg hx &= 0x7fffffffffffffffLL;
43*181254a7Smrg if(hx <= 0x0001000000000000LL) {
44*181254a7Smrg if((hx|lx)==0)
45*181254a7Smrg { errno = EDOM; feraiseexcept (FE_INVALID); return FP_ILOGB0; } /* ilogbl(0) = FP_ILOGB0 */
46*181254a7Smrg else /* subnormal x */
47*181254a7Smrg if(hx==0) {
48*181254a7Smrg for (ix = -16431; lx>0; lx<<=1) ix -=1;
49*181254a7Smrg } else {
50*181254a7Smrg for (ix = -16382, hx<<=15; hx>0; hx<<=1) ix -=1;
51*181254a7Smrg }
52*181254a7Smrg return ix;
53*181254a7Smrg }
54*181254a7Smrg else if (hx<0x7fff000000000000LL) return (hx>>48)-0x3fff;
55*181254a7Smrg else if (FP_ILOGBNAN != INT_MAX) {
56*181254a7Smrg /* ISO C99 requires ilogbl(+-Inf) == INT_MAX. */
57*181254a7Smrg if (((hx^0x7fff000000000000LL)|lx) == 0)
58*181254a7Smrg { errno = EDOM; feraiseexcept (FE_INVALID); return INT_MAX; }
59*181254a7Smrg }
60*181254a7Smrg { errno = EDOM; feraiseexcept (FE_INVALID); return FP_ILOGBNAN; }
61*181254a7Smrg }
62