1136385Sstefanf/*
2136385Sstefanf * From: @(#)s_ilogb.c 5.1 93/09/24
3136385Sstefanf * ====================================================
4136385Sstefanf * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5136385Sstefanf *
6136385Sstefanf * Developed at SunPro, a Sun Microsystems, Inc. business.
7136385Sstefanf * Permission to use, copy, modify, and distribute this
8136385Sstefanf * software is freely granted, provided that this notice
9136385Sstefanf * is preserved.
10136385Sstefanf * ====================================================
11136385Sstefanf */
12136385Sstefanf
13176451Sdas#include <sys/cdefs.h>
14176451Sdas__FBSDID("$FreeBSD$");
15136385Sstefanf
16136385Sstefanf#include <float.h>
17136385Sstefanf#include <limits.h>
18136385Sstefanf#include <math.h>
19136385Sstefanf
20136385Sstefanf#include "fpmath.h"
21136385Sstefanf
22136385Sstefanfint
23136385Sstefanfilogbl(long double x)
24136385Sstefanf{
25136385Sstefanf	union IEEEl2bits u;
26136385Sstefanf	unsigned long m;
27136385Sstefanf	int b;
28136385Sstefanf
29136385Sstefanf	u.e = x;
30136385Sstefanf	if (u.bits.exp == 0) {
31136385Sstefanf		if ((u.bits.manl | u.bits.manh) == 0)
32136385Sstefanf			return (FP_ILOGB0);
33136385Sstefanf		/* denormalized */
34136385Sstefanf		if (u.bits.manh == 0) {
35136385Sstefanf			m = 1lu << (LDBL_MANL_SIZE - 1);
36136385Sstefanf			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
37136385Sstefanf				b++;
38136385Sstefanf		} else {
39136385Sstefanf			m = 1lu << (LDBL_MANH_SIZE - 1);
40136385Sstefanf			for (b = 0; !(u.bits.manh & m); m >>= 1)
41136385Sstefanf				b++;
42136385Sstefanf		}
43136385Sstefanf#ifdef LDBL_IMPLICIT_NBIT
44136385Sstefanf		b++;
45136385Sstefanf#endif
46136385Sstefanf		return (LDBL_MIN_EXP - b - 1);
47136385Sstefanf	} else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
48136385Sstefanf		return (u.bits.exp - LDBL_MAX_EXP + 1);
49136385Sstefanf	else if (u.bits.manl != 0 || u.bits.manh != 0)
50136385Sstefanf		return (FP_ILOGBNAN);
51136385Sstefanf	else
52136385Sstefanf		return (INT_MAX);
53136385Sstefanf}
54