xref: /onnv-gate/usr/src/lib/libast/common/comp/frexp.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin /*
254887Schin  * frexp/ldexp implementation
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin 
304887Schin #include "FEATURE/float"
314887Schin 
324887Schin #if _lib_frexp && _lib_ldexp
334887Schin 
344887Schin NoN(frexp)
354887Schin 
364887Schin #else
374887Schin 
384887Schin #if defined(_ast_dbl_exp_index) && defined(_ast_dbl_exp_shift)
394887Schin 
404887Schin #define INIT()		_ast_dbl_exp_t _pow_
414887Schin #define pow2(i)		(_pow_.f=1,_pow_.e[_ast_dbl_exp_index]+=((i)<<_ast_dbl_exp_shift),_pow_.f)
424887Schin 
434887Schin #else
444887Schin 
454887Schin static double		pow2tab[DBL_MAX_EXP + 1];
464887Schin 
474887Schin static int
484887Schin init(void)
494887Schin {
504887Schin 	register int	x;
514887Schin 	double		g;
524887Schin 
534887Schin 	g = 1;
544887Schin 	for (x = 0; x < elementsof(pow2tab); x++)
554887Schin 	{
564887Schin 		pow2tab[x] = g;
574887Schin 		g *= 2;
584887Schin 	}
594887Schin 	return 0;
604887Schin }
614887Schin 
624887Schin #define INIT()		(pow2tab[0]?0:init())
634887Schin 
644887Schin #define pow2(i)		pow2tab[i]
654887Schin 
664887Schin #endif
674887Schin 
684887Schin #if !_lib_frexp
694887Schin 
704887Schin extern double
714887Schin frexp(double f, int* p)
724887Schin {
734887Schin 	register int	k;
744887Schin 	register int	x;
754887Schin 	double		g;
764887Schin 
774887Schin 	INIT();
784887Schin 
794887Schin 	/*
804887Schin 	 * normalize
814887Schin 	 */
824887Schin 
834887Schin 	x = k = DBL_MAX_EXP / 2;
844887Schin 	if (f < 1)
854887Schin 	{
864887Schin 		g = 1.0L / f;
874887Schin 		for (;;)
884887Schin 		{
894887Schin 			k = (k + 1) / 2;
904887Schin 			if (g < pow2(x))
914887Schin 				x -= k;
924887Schin 			else if (k == 1 && g < pow2(x+1))
934887Schin 				break;
944887Schin 			else
954887Schin 				x += k;
964887Schin 		}
974887Schin 		if (g == pow2(x))
984887Schin 			x--;
994887Schin 		x = -x;
1004887Schin 	}
1014887Schin 	else if (f > 1)
1024887Schin 	{
1034887Schin 		for (;;)
1044887Schin 		{
1054887Schin 			k = (k + 1) / 2;
1064887Schin 			if (f > pow2(x))
1074887Schin 				x += k;
1084887Schin 			else if (k == 1 && f > pow2(x-1))
1094887Schin 				break;
1104887Schin 			else
1114887Schin 				x -= k;
1124887Schin 		}
1134887Schin 		if (f == pow2(x))
1144887Schin 			x++;
1154887Schin 	}
1164887Schin 	else
1174887Schin 		x = 1;
1184887Schin 	*p = x;
1194887Schin 
1204887Schin 	/*
1214887Schin 	 * shift
1224887Schin 	 */
1234887Schin 
1244887Schin 	x = -x;
1254887Schin 	if (x < 0)
1264887Schin 		f /= pow2(-x);
1274887Schin 	else if (x < DBL_MAX_EXP)
1284887Schin 		f *= pow2(x);
1294887Schin 	else
1304887Schin 		f = (f * pow2(DBL_MAX_EXP - 1)) * pow2(x - (DBL_MAX_EXP - 1));
1314887Schin 	return f;
1324887Schin }
1334887Schin 
1344887Schin #endif
1354887Schin 
1364887Schin #if !_lib_ldexp
1374887Schin 
1384887Schin extern double
1394887Schin ldexp(double f, register int x)
1404887Schin {
1414887Schin 	INIT();
1424887Schin 	if (x < 0)
1434887Schin 		f /= pow2(-x);
1444887Schin 	else if (x < DBL_MAX_EXP)
1454887Schin 		f *= pow2(x);
1464887Schin 	else
1474887Schin 		f = (f * pow2(DBL_MAX_EXP - 1)) * pow2(x - (DBL_MAX_EXP - 1));
1484887Schin 	return f;
1494887Schin }
1504887Schin 
1514887Schin #endif
1524887Schin 
1534887Schin #endif
154