1174698Sdas/*
2174698Sdas * From: @(#)s_ilogb.c 5.1 93/09/24
3174698Sdas * ====================================================
4174698Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5174698Sdas *
6174698Sdas * Developed at SunPro, a Sun Microsystems, Inc. business.
7174698Sdas * Permission to use, copy, modify, and distribute this
8174698Sdas * software is freely granted, provided that this notice
9174698Sdas * is preserved.
10174698Sdas * ====================================================
11174698Sdas */
12174698Sdas
13174698Sdas#ifndef lint
14174698Sdasstatic char rcsid[] = "$FreeBSD$";
15174698Sdas#endif
16174698Sdas
17174698Sdas#include <float.h>
18174698Sdas#include <limits.h>
19174698Sdas#include <math.h>
20174698Sdas
21174698Sdas#include "fpmath.h"
22174698Sdas
23174698Sdaslong double
24174698Sdaslogbl(long double x)
25174698Sdas{
26174698Sdas	union IEEEl2bits u;
27174698Sdas	unsigned long m;
28174698Sdas	int b;
29174698Sdas
30174698Sdas	u.e = x;
31174698Sdas	if (u.bits.exp == 0) {
32174698Sdas		if ((u.bits.manl | u.bits.manh) == 0) {	/* x == 0 */
33174698Sdas			u.bits.sign = 1;
34174698Sdas			return (1.0L / u.e);
35174698Sdas		}
36174698Sdas		/* denormalized */
37174698Sdas		if (u.bits.manh == 0) {
38174698Sdas			m = 1lu << (LDBL_MANL_SIZE - 1);
39174698Sdas			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
40174698Sdas				b++;
41174698Sdas		} else {
42174698Sdas			m = 1lu << (LDBL_MANH_SIZE - 1);
43174698Sdas			for (b = 0; !(u.bits.manh & m); m >>= 1)
44174698Sdas				b++;
45174698Sdas		}
46174698Sdas#ifdef LDBL_IMPLICIT_NBIT
47174698Sdas		b++;
48174698Sdas#endif
49174698Sdas		return ((long double)(LDBL_MIN_EXP - b - 1));
50174698Sdas	}
51174698Sdas	if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)	/* normal */
52174698Sdas		return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
53174698Sdas	else						/* +/- inf or nan */
54174698Sdas		return (x * x);
55174698Sdas}
56