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
13324006Sdim#include <sys/cdefs.h>
14324006Sdim__FBSDID("$FreeBSD: stable/11/lib/msun/src/s_logbl.c 324006 2017-09-26 09:01:56Z dim $");
15174698Sdas
16174698Sdas#include <float.h>
17174698Sdas#include <limits.h>
18174698Sdas#include <math.h>
19174698Sdas
20174698Sdas#include "fpmath.h"
21174698Sdas
22174698Sdaslong double
23174698Sdaslogbl(long double x)
24174698Sdas{
25174698Sdas	union IEEEl2bits u;
26174698Sdas	unsigned long m;
27174698Sdas	int b;
28174698Sdas
29174698Sdas	u.e = x;
30174698Sdas	if (u.bits.exp == 0) {
31174698Sdas		if ((u.bits.manl | u.bits.manh) == 0) {	/* x == 0 */
32174698Sdas			u.bits.sign = 1;
33174698Sdas			return (1.0L / u.e);
34174698Sdas		}
35174698Sdas		/* denormalized */
36174698Sdas		if (u.bits.manh == 0) {
37174698Sdas			m = 1lu << (LDBL_MANL_SIZE - 1);
38174698Sdas			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
39174698Sdas				b++;
40174698Sdas		} else {
41174698Sdas			m = 1lu << (LDBL_MANH_SIZE - 1);
42174698Sdas			for (b = 0; !(u.bits.manh & m); m >>= 1)
43174698Sdas				b++;
44174698Sdas		}
45174698Sdas#ifdef LDBL_IMPLICIT_NBIT
46174698Sdas		b++;
47174698Sdas#endif
48174698Sdas		return ((long double)(LDBL_MIN_EXP - b - 1));
49174698Sdas	}
50174698Sdas	if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)	/* normal */
51174698Sdas		return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
52174698Sdas	else						/* +/- inf or nan */
53174698Sdas		return (x * x);
54174698Sdas}
55