blob: 3211f44090a23e38c47068eb5183193271ca4c71 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * From: @(#)s_ilogb.c 5.1 93/09/24
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
Elliott Hughesa0ee0782013-01-30 19:06:37 -080013#include <sys/cdefs.h>
14__FBSDID("$FreeBSD$");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015
16#include <float.h>
17#include <limits.h>
18#include <math.h>
19
20#include "fpmath.h"
21
22int
23ilogbl(long double x)
24{
25 union IEEEl2bits u;
26 unsigned long m;
27 int b;
28
29 u.e = x;
30 if (u.bits.exp == 0) {
31 if ((u.bits.manl | u.bits.manh) == 0)
32 return (FP_ILOGB0);
33 /* denormalized */
34 if (u.bits.manh == 0) {
35 m = 1lu << (LDBL_MANL_SIZE - 1);
36 for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
37 b++;
38 } else {
39 m = 1lu << (LDBL_MANH_SIZE - 1);
40 for (b = 0; !(u.bits.manh & m); m >>= 1)
41 b++;
42 }
43#ifdef LDBL_IMPLICIT_NBIT
44 b++;
45#endif
46 return (LDBL_MIN_EXP - b - 1);
47 } else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
48 return (u.bits.exp - LDBL_MAX_EXP + 1);
49 else if (u.bits.manl != 0 || u.bits.manh != 0)
50 return (FP_ILOGBNAN);
51 else
52 return (INT_MAX);
53}