xref: /openbsd-src/lib/libm/src/s_scalbnl.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_scalbnl.c,v 1.4 2016/09/12 19:47:02 guenther Exp $	*/
2390c8400Smartynas /* @(#)s_scalbn.c 5.1 93/09/24 */
3390c8400Smartynas /*
4390c8400Smartynas  * ====================================================
5390c8400Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6390c8400Smartynas  *
7390c8400Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
8390c8400Smartynas  * Permission to use, copy, modify, and distribute this
9390c8400Smartynas  * software is freely granted, provided that this notice
10390c8400Smartynas  * is preserved.
11390c8400Smartynas  * ====================================================
12390c8400Smartynas  */
13390c8400Smartynas 
14390c8400Smartynas /*
15390c8400Smartynas  * scalbnl (long double x, int n)
16390c8400Smartynas  * scalbnl(x,n) returns x* 2**n  computed by  exponent
17390c8400Smartynas  * manipulation rather than by actually performing an
18390c8400Smartynas  * exponentiation or a multiplication.
19390c8400Smartynas  */
20390c8400Smartynas 
21390c8400Smartynas /*
22390c8400Smartynas  * We assume that a long double has a 15-bit exponent.  On systems
23390c8400Smartynas  * where long double is the same as double, scalbnl() is an alias
24390c8400Smartynas  * for scalbn(), so we don't use this routine.
25390c8400Smartynas  */
26390c8400Smartynas 
27390c8400Smartynas #include <sys/types.h>
28390c8400Smartynas #include <machine/ieee.h>
29390c8400Smartynas #include <float.h>
30390c8400Smartynas #include <math.h>
31390c8400Smartynas 
32390c8400Smartynas #if LDBL_MAX_EXP != 0x4000
33390c8400Smartynas #error "Unsupported long double format"
34390c8400Smartynas #endif
35390c8400Smartynas 
36390c8400Smartynas static const long double
37390c8400Smartynas huge = 0x1p16000L,
38390c8400Smartynas tiny = 0x1p-16000L;
39390c8400Smartynas 
40390c8400Smartynas long double
scalbnl(long double x,int n)41390c8400Smartynas scalbnl (long double x, int n)
42390c8400Smartynas {
43390c8400Smartynas 	union {
44390c8400Smartynas 		long double e;
45390c8400Smartynas 		struct ieee_ext bits;
46390c8400Smartynas 	} u;
47390c8400Smartynas 	int k;
48390c8400Smartynas 	u.e = x;
49390c8400Smartynas         k = u.bits.ext_exp;			/* extract exponent */
50390c8400Smartynas         if (k==0) {				/* 0 or subnormal x */
51390c8400Smartynas             if ((u.bits.ext_frach
52390c8400Smartynas #ifdef EXT_FRACHMBITS
53390c8400Smartynas 		| u.bits.ext_frachm
54390c8400Smartynas #endif /* EXT_FRACHMBITS */
55390c8400Smartynas #ifdef EXT_FRACLMBITS
56390c8400Smartynas 		| u.bits.ext_fraclm
57390c8400Smartynas #endif /* EXT_FRACLMBITS */
58390c8400Smartynas 		| u.bits.ext_fracl)==0) return x;	/* +-0 */
59390c8400Smartynas 	    u.e *= 0x1p+128;
60390c8400Smartynas 	    k = u.bits.ext_exp - 128;
61390c8400Smartynas             if (n< -50000) return tiny*x; 	/*underflow*/
62390c8400Smartynas 	    }
63390c8400Smartynas         if (k==0x7fff) return x+x;		/* NaN or Inf */
64390c8400Smartynas         k = k+n;
65390c8400Smartynas         if (k >= 0x7fff) return huge*copysignl(huge,x); /* overflow  */
66390c8400Smartynas         if (k > 0) 				/* normal result */
67390c8400Smartynas 	    {u.bits.ext_exp = k; return u.e;}
6896943daaSmartynas         if (k <= -128) {
69390c8400Smartynas             if (n > 50000) 	/* in case integer overflow in n+k */
70390c8400Smartynas 		return huge*copysign(huge,x);	/*overflow*/
71390c8400Smartynas 	    else return tiny*copysign(tiny,x); 	/*underflow*/
7296943daaSmartynas 	}
73390c8400Smartynas         k += 128;				/* subnormal result */
74390c8400Smartynas 	u.bits.ext_exp = k;
75390c8400Smartynas         return u.e*0x1p-128;
76390c8400Smartynas }
77*2f2c0062Sguenther DEF_STD(scalbnl);
78390c8400Smartynas 
79390c8400Smartynas long double
ldexpl(long double x,int n)80390c8400Smartynas ldexpl(long double x, int n)
81390c8400Smartynas {
82390c8400Smartynas 	return scalbnl(x, n);
83390c8400Smartynas }
84*2f2c0062Sguenther DEF_STD(ldexpl);
85