source: trunk/sys/libm/s_ilogb.c @ 270

Last change on this file since 270 was 1, checked in by alain, 8 years ago

First import

File size: 1.2 KB
RevLine 
[1]1
2/* @(#)s_ilogb.c 5.1 93/09/24 */
3/*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 *
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 */
13
14/* ilogb(double x)
15 * return the binary exponent of non-zero x
16 * ilogb(0) = 0x80000001
17 * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
18 */
19
20#include <libm/fdlibm.h>
21
22#ifdef __STDC__
23static const double one  = 1.0;
24#else
25static double one  = 1.0;
26#endif
27
28#ifdef __STDC__
29        int ilogb(double x)
30#else
31        int ilogb(x)
32        double x;
33#endif
34{
35        int n0,hx,lx,ix;
36
37        n0 = ((*(int*)&one)>>29)^1;             /* high word index */
38        hx  = (*(n0+(unsigned*)&x))&0x7fffffff; /* high word of x */
39        if(hx<0x00100000) {
40            lx = *(1-n0+(int*)&x);
41            if((hx|lx)==0) 
42                return 0x80000001;      /* ilogb(0) = 0x80000001 */
43            else                        /* subnormal x */
44                if(hx==0) {
45                    for (ix = -1043; lx>0; lx<<=1) ix -=1;
46                } else {
47                    for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
48                }
49            return ix;
50        }
51        else if (hx<0x7ff00000) return (hx>>20)-1023;
52        else return 0x7fffffff;
53}
Note: See TracBrowser for help on using the repository browser.