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