Optimize handling of denormals in logb/logbf/logbl
[glibc.git] / sysdeps / ieee754 / dbl-64 / s_logb.c
blob17aa94b755c7403c924a29a5aa3dc88214f568e8
1 /* @(#)s_logb.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
14 * double logb(x)
15 * IEEE 754 logb. Included to pass IEEE test suite. Not recommend.
16 * Use ilogb instead.
19 #include <math.h>
20 #include <math_private.h>
22 double
23 __logb (double x)
25 int32_t lx, ix, rix;
27 EXTRACT_WORDS (ix, lx, x);
28 ix &= 0x7fffffff; /* high |x| */
29 if ((ix | lx) == 0)
30 return -1.0 / fabs (x);
31 if (ix >= 0x7ff00000)
32 return x * x;
33 if (__builtin_expect ((rix = ix >> 20) == 0, 0))
35 /* POSIX specifies that denormal number is treated as
36 though it were normalized. */
37 int ma;
38 if (ix == 0)
39 ma = __builtin_clz (lx) + 32;
40 else
41 ma = __builtin_clz (ix);
42 rix -= ma - 12;
44 return (double) (rix - 1023);
46 weak_alias (__logb, logb)
47 #ifdef NO_LONG_DOUBLE
48 strong_alias (__logb, __logbl) weak_alias (__logb, logbl)
49 #endif