blob: 7b1a9cf8d007ff16c1d6fe072916a5a8a226e3e3 [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001#include <limits.h>
2#include "libm.h"
3
4#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
5int ilogbl(long double x)
6{
7 return ilogb(x);
8}
9#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
10int ilogbl(long double x)
11{
Szabolcs Nagy4cec31f2013-09-02 23:33:20 +000012 #pragma STDC FENV_ACCESS ON
Rich Felkerb69f6952012-03-13 01:17:53 -040013 union ldshape u = {x};
Szabolcs Nagyaf5f6d92013-09-02 00:38:51 +000014 uint64_t m = u.i.m;
15 int e = u.i.se & 0x7fff;
Rich Felkerb69f6952012-03-13 01:17:53 -040016
17 if (!e) {
Szabolcs Nagybe81f512012-11-12 23:58:18 +010018 if (m == 0) {
Szabolcs Nagy4cec31f2013-09-02 23:33:20 +000019 FORCE_EVAL(0/0.0f);
Rich Felkerb69f6952012-03-13 01:17:53 -040020 return FP_ILOGB0;
Szabolcs Nagybe81f512012-11-12 23:58:18 +010021 }
Rich Felkerb69f6952012-03-13 01:17:53 -040022 /* subnormal x */
Szabolcs Nagy4cec31f2013-09-02 23:33:20 +000023 for (e = -0x3fff+1; m>>63 == 0; e--, m<<=1);
Rich Felkerb69f6952012-03-13 01:17:53 -040024 return e;
25 }
Szabolcs Nagybe81f512012-11-12 23:58:18 +010026 if (e == 0x7fff) {
27 FORCE_EVAL(0/0.0f);
Szabolcs Nagy4cec31f2013-09-02 23:33:20 +000028 return m<<1 ? FP_ILOGBNAN : INT_MAX;
29 }
30 return e - 0x3fff;
31}
32#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
33int ilogbl(long double x)
34{
35 #pragma STDC FENV_ACCESS ON
36 union ldshape u = {x};
37 int e = u.i.se & 0x7fff;
38
39 if (!e) {
40 if (x == 0) {
41 FORCE_EVAL(0/0.0f);
42 return FP_ILOGB0;
43 }
44 /* subnormal x */
45 x *= 0x1p120;
46 return ilogbl(x) - 120;
47 }
48 if (e == 0x7fff) {
49 FORCE_EVAL(0/0.0f);
50 u.i.se = 0;
51 return u.f ? FP_ILOGBNAN : INT_MAX;
Szabolcs Nagybe81f512012-11-12 23:58:18 +010052 }
Rich Felkerb69f6952012-03-13 01:17:53 -040053 return e - 0x3fff;
54}
55#endif