initial commit
[libm] / src / math / ilogb.c
1 #include <limits.h>
2 #include "libm.h"
3
4 int ilogb(double x)
5 {
6         union dshape u = {x};
7         int e = u.bits>>52 & 0x7ff;
8
9         if (!e) {
10                 u.bits <<= 12;
11                 if (u.bits == 0)
12                         return FP_ILOGB0;
13                 /* subnormal x */
14                 // FIXME: scale up subnormals with a *0x1p53 or find top set bit with a better method
15                 for (e = -0x3ff; u.bits < (uint64_t)1<<63; e--, u.bits<<=1);
16                 return e;
17         }
18         if (e == 0x7ff)
19                 return u.bits<<12 ? FP_ILOGBNAN : INT_MAX;
20         return e - 0x3ff;
21 }