xref: /onnv-gate/usr/src/lib/libast/common/comp/frexp.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin 
24*4887Schin /*
25*4887Schin  * frexp/ldexp implementation
26*4887Schin  */
27*4887Schin 
28*4887Schin #include <ast.h>
29*4887Schin 
30*4887Schin #include "FEATURE/float"
31*4887Schin 
32*4887Schin #if _lib_frexp && _lib_ldexp
33*4887Schin 
34*4887Schin NoN(frexp)
35*4887Schin 
36*4887Schin #else
37*4887Schin 
38*4887Schin #if defined(_ast_dbl_exp_index) && defined(_ast_dbl_exp_shift)
39*4887Schin 
40*4887Schin #define INIT()		_ast_dbl_exp_t _pow_
41*4887Schin #define pow2(i)		(_pow_.f=1,_pow_.e[_ast_dbl_exp_index]+=((i)<<_ast_dbl_exp_shift),_pow_.f)
42*4887Schin 
43*4887Schin #else
44*4887Schin 
45*4887Schin static double		pow2tab[DBL_MAX_EXP + 1];
46*4887Schin 
47*4887Schin static int
48*4887Schin init(void)
49*4887Schin {
50*4887Schin 	register int	x;
51*4887Schin 	double		g;
52*4887Schin 
53*4887Schin 	g = 1;
54*4887Schin 	for (x = 0; x < elementsof(pow2tab); x++)
55*4887Schin 	{
56*4887Schin 		pow2tab[x] = g;
57*4887Schin 		g *= 2;
58*4887Schin 	}
59*4887Schin 	return 0;
60*4887Schin }
61*4887Schin 
62*4887Schin #define INIT()		(pow2tab[0]?0:init())
63*4887Schin 
64*4887Schin #define pow2(i)		pow2tab[i]
65*4887Schin 
66*4887Schin #endif
67*4887Schin 
68*4887Schin #if !_lib_frexp
69*4887Schin 
70*4887Schin extern double
71*4887Schin frexp(double f, int* p)
72*4887Schin {
73*4887Schin 	register int	k;
74*4887Schin 	register int	x;
75*4887Schin 	double		g;
76*4887Schin 
77*4887Schin 	INIT();
78*4887Schin 
79*4887Schin 	/*
80*4887Schin 	 * normalize
81*4887Schin 	 */
82*4887Schin 
83*4887Schin 	x = k = DBL_MAX_EXP / 2;
84*4887Schin 	if (f < 1)
85*4887Schin 	{
86*4887Schin 		g = 1.0L / f;
87*4887Schin 		for (;;)
88*4887Schin 		{
89*4887Schin 			k = (k + 1) / 2;
90*4887Schin 			if (g < pow2(x))
91*4887Schin 				x -= k;
92*4887Schin 			else if (k == 1 && g < pow2(x+1))
93*4887Schin 				break;
94*4887Schin 			else
95*4887Schin 				x += k;
96*4887Schin 		}
97*4887Schin 		if (g == pow2(x))
98*4887Schin 			x--;
99*4887Schin 		x = -x;
100*4887Schin 	}
101*4887Schin 	else if (f > 1)
102*4887Schin 	{
103*4887Schin 		for (;;)
104*4887Schin 		{
105*4887Schin 			k = (k + 1) / 2;
106*4887Schin 			if (f > pow2(x))
107*4887Schin 				x += k;
108*4887Schin 			else if (k == 1 && f > pow2(x-1))
109*4887Schin 				break;
110*4887Schin 			else
111*4887Schin 				x -= k;
112*4887Schin 		}
113*4887Schin 		if (f == pow2(x))
114*4887Schin 			x++;
115*4887Schin 	}
116*4887Schin 	else
117*4887Schin 		x = 1;
118*4887Schin 	*p = x;
119*4887Schin 
120*4887Schin 	/*
121*4887Schin 	 * shift
122*4887Schin 	 */
123*4887Schin 
124*4887Schin 	x = -x;
125*4887Schin 	if (x < 0)
126*4887Schin 		f /= pow2(-x);
127*4887Schin 	else if (x < DBL_MAX_EXP)
128*4887Schin 		f *= pow2(x);
129*4887Schin 	else
130*4887Schin 		f = (f * pow2(DBL_MAX_EXP - 1)) * pow2(x - (DBL_MAX_EXP - 1));
131*4887Schin 	return f;
132*4887Schin }
133*4887Schin 
134*4887Schin #endif
135*4887Schin 
136*4887Schin #if !_lib_ldexp
137*4887Schin 
138*4887Schin extern double
139*4887Schin ldexp(double f, register int x)
140*4887Schin {
141*4887Schin 	INIT();
142*4887Schin 	if (x < 0)
143*4887Schin 		f /= pow2(-x);
144*4887Schin 	else if (x < DBL_MAX_EXP)
145*4887Schin 		f *= pow2(x);
146*4887Schin 	else
147*4887Schin 		f = (f * pow2(DBL_MAX_EXP - 1)) * pow2(x - (DBL_MAX_EXP - 1));
148*4887Schin 	return f;
149*4887Schin }
150*4887Schin 
151*4887Schin #endif
152*4887Schin 
153*4887Schin #endif
154