Back to index

glibc  2.9
s_scalbnl.c
Go to the documentation of this file.
00001 /* s_scalbnl.c -- long double version of s_scalbn.c.
00002  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
00003  */
00004 
00005 /* @(#)s_scalbn.c 5.1 93/09/24 */
00006 /*
00007  * ====================================================
00008  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00009  *
00010  * Developed at SunPro, a Sun Microsystems, Inc. business.
00011  * Permission to use, copy, modify, and distribute this
00012  * software is freely granted, provided that this notice
00013  * is preserved.
00014  * ====================================================
00015  */
00016 
00017 #if defined(LIBM_SCCS) && !defined(lint)
00018 static char rcsid[] = "$NetBSD: $";
00019 #endif
00020 
00021 /*
00022  * scalbnl (long double x, int n)
00023  * scalbnl(x,n) returns x* 2**n  computed by  exponent
00024  * manipulation rather than by actually performing an
00025  * exponentiation or a multiplication.
00026  */
00027 
00028 #include "math.h"
00029 #include "math_private.h"
00030 #include <math_ldbl_opt.h>
00031 
00032 #ifdef __STDC__
00033 static const long double
00034 #else
00035 static long double
00036 #endif
00037 twolm54 = 5.55111512312578270212e-17, /* 0x3C90000000000000, 0 */
00038 huge   = 1.0E+300L,
00039 tiny   = 1.0E-300L;
00040 #ifdef __STDC__
00041 static const double
00042 #else
00043 static double
00044 #endif
00045 two54 = 1.80143985094819840000e+16, /* 0x4350000000000000 */
00046 twom54 = 5.55111512312578270212e-17; /* 0x3C90000000000000 */
00047 
00048 #ifdef __STDC__
00049        long double __scalbnl (long double x, int n)
00050 #else
00051        long double __scalbnl (x,n)
00052        long double x; int n;
00053 #endif
00054 {
00055        int64_t k,l,hx,lx;
00056        union { int64_t i; double d; } u;
00057        GET_LDOUBLE_WORDS64(hx,lx,x);
00058        k = (hx>>52)&0x7ff;         /* extract exponent */
00059        l = (lx>>52)&0x7ff;
00060        if (k==0) {                        /* 0 or subnormal x */
00061            if (((hx|lx)&0x7fffffffffffffffULL)==0) return x; /* +-0 */
00062            u.i = hx;
00063            u.d *= two54;
00064            hx = u.i;
00065            k = ((hx>>52)&0x7ff) - 54;
00066        }
00067        else if (k==0x7ff) return x+x;            /* NaN or Inf */
00068        k = k+n;
00069        if (n> 50000 || k > 0x7fe)
00070          return huge*__copysignl(huge,x); /* overflow */
00071        if (n< -50000) return tiny*__copysignl(tiny,x); /*underflow */
00072        if (k > 0) {                       /* normal result */
00073            hx = (hx&0x800fffffffffffffULL)|(k<<52);
00074            if ((lx & 0x7fffffffffffffffULL) == 0) { /* low part +-0 */
00075               SET_LDOUBLE_WORDS64(x,hx,lx);
00076               return x;
00077            }
00078            if (l == 0) { /* low part subnormal */
00079               u.i = lx;
00080               u.d *= two54;
00081               lx = u.i;
00082               l = ((lx>>52)&0x7ff) - 54;
00083            }
00084            l = l + n;
00085            if (l > 0)
00086               lx = (lx&0x800fffffffffffffULL)|(l<<52);
00087            else if (l <= -54)
00088               lx = (lx&0x8000000000000000ULL);
00089            else {
00090               l += 54;
00091               u.i = (lx&0x800fffffffffffffULL)|(l<<52);
00092               u.d *= twom54;
00093               lx = u.i;
00094            }
00095            SET_LDOUBLE_WORDS64(x,hx,lx);
00096            return x;
00097        }
00098        if (k <= -54)
00099          return tiny*__copysignl(tiny,x);        /*underflow*/
00100        k += 54;                           /* subnormal result */
00101        lx &= 0x8000000000000000ULL;
00102        SET_LDOUBLE_WORDS64(x,(hx&0x800fffffffffffffULL)|(k<<52),lx);
00103        return x*twolm54;
00104 }
00105 #ifdef IS_IN_libm
00106 long_double_symbol (libm, __scalbnl, scalbnl);
00107 #else
00108 long_double_symbol (libc, __scalbnl, scalbnl);
00109 #endif