12fe8fb19SBen Gras /* @(#)s_scalbn.c 5.1 93/09/24 */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * ====================================================
42fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
72fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
82fe8fb19SBen Gras * software is freely granted, provided that this notice
92fe8fb19SBen Gras * is preserved.
102fe8fb19SBen Gras * ====================================================
112fe8fb19SBen Gras */
122fe8fb19SBen Gras
132fe8fb19SBen Gras #include <sys/cdefs.h>
142fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_scalbn.c,v 1.18 2013/05/20 19:40:09 joerg Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras
182fe8fb19SBen Gras /*
192fe8fb19SBen Gras * scalbn (double x, int n)
202fe8fb19SBen Gras * scalbn(x,n) returns x* 2**n computed by exponent
212fe8fb19SBen Gras * manipulation rather than by actually performing an
222fe8fb19SBen Gras * exponentiation or a multiplication.
232fe8fb19SBen Gras */
242fe8fb19SBen Gras
252fe8fb19SBen Gras #include "namespace.h"
262fe8fb19SBen Gras #include "math.h"
272fe8fb19SBen Gras #include "math_private.h"
282fe8fb19SBen Gras
29*84d9c625SLionel Sambuc #ifndef _LP64
30*84d9c625SLionel Sambuc __strong_alias(_scalbn, _scalbln)
31*84d9c625SLionel Sambuc #endif
32*84d9c625SLionel Sambuc
33*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
34*84d9c625SLionel Sambuc __strong_alias(_scalbnl, _scalbn)
35*84d9c625SLionel Sambuc __strong_alias(_scalblnl, _scalbln)
36*84d9c625SLionel Sambuc __weak_alias(scalbnl, _scalbnl)
37*84d9c625SLionel Sambuc __weak_alias(scalblnl, _scalblnl)
38*84d9c625SLionel Sambuc #endif
39*84d9c625SLionel Sambuc
402fe8fb19SBen Gras #ifdef __weak_alias
412fe8fb19SBen Gras __weak_alias(scalbn, _scalbn)
42*84d9c625SLionel Sambuc __weak_alias(scalbln, _scalbln)
43*84d9c625SLionel Sambuc __weak_alias(ldexp, _scalbn)
442fe8fb19SBen Gras #endif
452fe8fb19SBen Gras
462fe8fb19SBen Gras static const double
47*84d9c625SLionel Sambuc two54 = 0x1.0p54, /* 0x43500000, 0x00000000 */
48*84d9c625SLionel Sambuc twom54 = 0x1.0p-54, /* 0x3C900000, 0x00000000 */
492fe8fb19SBen Gras huge = 1.0e+300,
502fe8fb19SBen Gras tiny = 1.0e-300;
512fe8fb19SBen Gras
52*84d9c625SLionel Sambuc #ifdef _LP64
532fe8fb19SBen Gras double
scalbn(double x,int n)542fe8fb19SBen Gras scalbn(double x, int n)
552fe8fb19SBen Gras {
56*84d9c625SLionel Sambuc return scalbln(x, n);
57*84d9c625SLionel Sambuc }
58*84d9c625SLionel Sambuc #endif
59*84d9c625SLionel Sambuc
60*84d9c625SLionel Sambuc double
scalbln(double x,long n)61*84d9c625SLionel Sambuc scalbln(double x, long n)
62*84d9c625SLionel Sambuc {
632fe8fb19SBen Gras int32_t k,hx,lx;
642fe8fb19SBen Gras EXTRACT_WORDS(hx,lx,x);
652fe8fb19SBen Gras k = ((uint32_t)hx&0x7ff00000)>>20; /* extract exponent */
662fe8fb19SBen Gras if (k==0) { /* 0 or subnormal x */
672fe8fb19SBen Gras if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
682fe8fb19SBen Gras x *= two54;
692fe8fb19SBen Gras GET_HIGH_WORD(hx,x);
702fe8fb19SBen Gras k = (((uint32_t)hx&0x7ff00000)>>20) - 54;
712fe8fb19SBen Gras if (n< -50000) return tiny*x; /*underflow*/
722fe8fb19SBen Gras }
732fe8fb19SBen Gras if (k==0x7ff) return x+x; /* NaN or Inf */
742fe8fb19SBen Gras k = k+n;
752fe8fb19SBen Gras if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
762fe8fb19SBen Gras if (k > 0) /* normal result */
772fe8fb19SBen Gras {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
782fe8fb19SBen Gras if (k <= -54) {
792fe8fb19SBen Gras if (n > 50000) /* in case integer overflow in n+k */
802fe8fb19SBen Gras return huge*copysign(huge,x); /*overflow*/
812fe8fb19SBen Gras else return tiny*copysign(tiny,x); /*underflow*/
822fe8fb19SBen Gras }
832fe8fb19SBen Gras k += 54; /* subnormal result */
842fe8fb19SBen Gras SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
852fe8fb19SBen Gras return x*twom54;
862fe8fb19SBen Gras }
87