1/* s_ilogbl.c -- long double version of s_ilogb.c.
2 * Conversion to 128-bit long double by Mark Kettenis, kettenis@openbsd.org.
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/* ilogbl(long double x)
17 * return the binary exponent of non-zero x
18 * ilogb(0) = FP_ILOGB0
19 * ilogb(NaN) = FP_ILOGBNAN (no signal is raised)
20 * ilogb(inf) = INT_MAX (no signal is raised)
21 */
22
23#include <float.h>
24#include <math.h>
25
26#include "math_private.h"
27
28int
29ilogbl(long double x)
30{
31	int64_t hx,lx;
32	int32_t ix;
33
34	GET_LDOUBLE_WORDS64(hx,lx,x);
35	hx &= 0x7fffffffffffffffLL;
36	if(hx<0x0001000000000000LL) {
37	    if((hx|lx)==0)
38		return FP_ILOGB0;	/* ilogb(0) = FP_ILOGB0 */
39	    else			/* subnormal x */
40		if(hx==0) {
41		    for (ix = -16431; lx>0; lx<<=1) ix -=1;
42		} else {
43		    for (ix = -16382, hx<<=15; hx>0; hx<<=1) ix -=1;
44		}
45	    return ix;
46	}
47	else if (hx<0x7fff000000000000LL) return (hx>>48)-16383;
48	else if (hx>0x7fff000000000000LL || lx!=0) return FP_ILOGBNAN;
49	else return INT_MAX;
50}
51DEF_STD(ilogbl);
52