summaryrefslogtreecommitdiffstats
path: root/lib/libm/src/s_ilogbl.c
blob: 9c1f83fff4f643c49e7879a19950f82a6c65e4cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*	$OpenBSD: s_ilogbl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $	*/
/*
 * From: @(#)s_ilogb.c 5.1 93/09/24
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 * ====================================================
 */

#include <sys/types.h>
#include <machine/ieee.h>
#include <float.h>
#include <limits.h>
#include <math.h>

int
ilogbl(long double x)
{
	struct ieee_ext *p = (struct ieee_ext *)&x;
	unsigned long m;
	int b;

	if (p->ext_exp == 0) {
		if ((p->ext_fracl
#ifdef EXT_FRACLMBITS
			| p->ext_fraclm
#endif /* EXT_FRACLMBITS */
#ifdef EXT_FRACHMBITS
			| p->ext_frachm
#endif /* EXT_FRACHMBITS */
			| p->ext_frach) == 0)
			return (FP_ILOGB0);
		/* denormalized */
		if (p->ext_frach == 0
#ifdef EXT_FRACHMBITS
			&& p->ext_frachm == 0
#endif
			) {
			m = 1lu << (EXT_FRACLBITS - 1);
			for (b = EXT_FRACHBITS; !(p->ext_fracl & m); m >>= 1)
				b++;
#if defined(EXT_FRACHMBITS) && defined(EXT_FRACLMBITS)
			m = 1lu << (EXT_FRACLMBITS - 1);
			for (b += EXT_FRACHMBITS; !(p->ext_fraclm & m); m >>= 1)
				b++;
#endif /* defined(EXT_FRACHMBITS) && defined(EXT_FRACLMBITS) */
		} else {
			m = 1lu << (EXT_FRACHBITS - 1);
			for (b = 0; !(p->ext_frach & m); m >>= 1)
				b++;
#ifdef EXT_FRACHMBITS
			m = 1lu << (EXT_FRACHMBITS - 1);
			for (; !(p->ext_frachm & m); m >>= 1)
				b++;
#endif /* EXT_FRACHMBITS */
		}
#ifdef EXT_IMPLICIT_NBIT
		b++;
#endif
		return (LDBL_MIN_EXP - b - 1);
	} else if (p->ext_exp < (LDBL_MAX_EXP << 1) - 1)
		return (p->ext_exp - LDBL_MAX_EXP + 1);
	else if (p->ext_fracl != 0
#ifdef EXT_FRACLMBITS
		|| p->ext_fraclm != 0
#endif /* EXT_FRACLMBITS */
#ifdef EXT_FRACHMBITS
		|| p->ext_frachm != 0
#endif /* EXT_FRACHMBITS */
		|| p->ext_frach != 0)
		return (FP_ILOGBNAN);
	else
		return (INT_MAX);
}
DEF_STD(ilogbl);