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  * frexpl/ldexpl implementation
26*4887Schin  */
27*4887Schin 
28*4887Schin #include <ast.h>
29*4887Schin 
30*4887Schin #include "FEATURE/float"
31*4887Schin 
32*4887Schin #if _lib_frexpl && _lib_ldexpl
33*4887Schin 
34*4887Schin NoN(frexpl)
35*4887Schin 
36*4887Schin #else
37*4887Schin 
38*4887Schin #ifndef LDBL_MAX_EXP
39*4887Schin #define LDBL_MAX_EXP	DBL_MAX_EXP
40*4887Schin #endif
41*4887Schin 
42*4887Schin #if defined(_ast_fltmax_exp_index) && defined(_ast_fltmax_exp_shift)
43*4887Schin 
44*4887Schin #define INIT()		_ast_fltmax_exp_t _pow_
45*4887Schin #define pow2(i)		(_pow_.f=1,_pow_.e[_ast_fltmax_exp_index]+=((i)<<_ast_fltmax_exp_shift),_pow_.f)
46*4887Schin 
47*4887Schin #else
48*4887Schin 
49*4887Schin static _ast_fltmax_t	pow2tab[LDBL_MAX_EXP + 1];
50*4887Schin 
51*4887Schin static int
52*4887Schin init(void)
53*4887Schin {
54*4887Schin 	register int		x;
55*4887Schin 	_ast_fltmax_t		g;
56*4887Schin 
57*4887Schin 	g = 1;
58*4887Schin 	for (x = 0; x < elementsof(pow2tab); x++)
59*4887Schin 	{
60*4887Schin 		pow2tab[x] = g;
61*4887Schin 		g *= 2;
62*4887Schin 	}
63*4887Schin 	return 0;
64*4887Schin }
65*4887Schin 
66*4887Schin #define INIT()		(pow2tab[0]?0:init())
67*4887Schin 
68*4887Schin #define pow2(i)		(pow2tab[i])
69*4887Schin 
70*4887Schin #endif
71*4887Schin 
72*4887Schin #if !_lib_frexpl
73*4887Schin 
74*4887Schin #undef	frexpl
75*4887Schin 
76*4887Schin extern _ast_fltmax_t
77*4887Schin frexpl(_ast_fltmax_t f, int* p)
78*4887Schin {
79*4887Schin 	register int		k;
80*4887Schin 	register int		x;
81*4887Schin 	_ast_fltmax_t		g;
82*4887Schin 
83*4887Schin 	INIT();
84*4887Schin 
85*4887Schin 	/*
86*4887Schin 	 * normalize
87*4887Schin 	 */
88*4887Schin 
89*4887Schin 	x = k = LDBL_MAX_EXP / 2;
90*4887Schin 	if (f < 1)
91*4887Schin 	{
92*4887Schin 		g = 1.0L / f;
93*4887Schin 		for (;;)
94*4887Schin 		{
95*4887Schin 			k = (k + 1) / 2;
96*4887Schin 			if (g < pow2(x))
97*4887Schin 				x -= k;
98*4887Schin 			else if (k == 1 && g < pow2(x+1))
99*4887Schin 				break;
100*4887Schin 			else
101*4887Schin 				x += k;
102*4887Schin 		}
103*4887Schin 		if (g == pow2(x))
104*4887Schin 			x--;
105*4887Schin 		x = -x;
106*4887Schin 	}
107*4887Schin 	else if (f > 1)
108*4887Schin 	{
109*4887Schin 		for (;;)
110*4887Schin 		{
111*4887Schin 			k = (k + 1) / 2;
112*4887Schin 			if (f > pow2(x))
113*4887Schin 				x += k;
114*4887Schin 			else if (k == 1 && f > pow2(x-1))
115*4887Schin 				break;
116*4887Schin 			else
117*4887Schin 				x -= k;
118*4887Schin 		}
119*4887Schin 		if (f == pow2(x))
120*4887Schin 			x++;
121*4887Schin 	}
122*4887Schin 	else
123*4887Schin 		x = 1;
124*4887Schin 	*p = x;
125*4887Schin 
126*4887Schin 	/*
127*4887Schin 	 * shift
128*4887Schin 	 */
129*4887Schin 
130*4887Schin 	x = -x;
131*4887Schin 	if (x < 0)
132*4887Schin 		f /= pow2(-x);
133*4887Schin 	else if (x < LDBL_MAX_EXP)
134*4887Schin 		f *= pow2(x);
135*4887Schin 	else
136*4887Schin 		f = (f * pow2(LDBL_MAX_EXP - 1)) * pow2(x - (LDBL_MAX_EXP - 1));
137*4887Schin 	return f;
138*4887Schin }
139*4887Schin 
140*4887Schin #endif
141*4887Schin 
142*4887Schin #if !_lib_ldexpl
143*4887Schin 
144*4887Schin #undef	ldexpl
145*4887Schin 
146*4887Schin extern _ast_fltmax_t
147*4887Schin ldexpl(_ast_fltmax_t f, register int x)
148*4887Schin {
149*4887Schin 	INIT();
150*4887Schin 	if (x < 0)
151*4887Schin 		f /= pow2(-x);
152*4887Schin 	else if (x < LDBL_MAX_EXP)
153*4887Schin 		f *= pow2(x);
154*4887Schin 	else
155*4887Schin 		f = (f * pow2(LDBL_MAX_EXP - 1)) * pow2(x - (LDBL_MAX_EXP - 1));
156*4887Schin 	return f;
157*4887Schin }
158*4887Schin 
159*4887Schin #endif
160*4887Schin 
161*4887Schin #endif
162