src/libm/s_scalbn.c
changeset 2756 a98604b691c8
child 3162 dc1eb82ffdaa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/libm/s_scalbn.c	Mon Sep 15 06:33:23 2008 +0000
     1.3 @@ -0,0 +1,79 @@
     1.4 +/* @(#)s_scalbn.c 5.1 93/09/24 */
     1.5 +/*
     1.6 + * ====================================================
     1.7 + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     1.8 + *
     1.9 + * Developed at SunPro, a Sun Microsystems, Inc. business.
    1.10 + * Permission to use, copy, modify, and distribute this
    1.11 + * software is freely granted, provided that this notice
    1.12 + * is preserved.
    1.13 + * ====================================================
    1.14 + */
    1.15 +
    1.16 +#if defined(LIBM_SCCS) && !defined(lint)
    1.17 +static char rcsid[] =
    1.18 +    "$NetBSD: s_scalbn.c,v 1.8 1995/05/10 20:48:08 jtc Exp $";
    1.19 +#endif
    1.20 +
    1.21 +/*
    1.22 + * scalbn (double x, int n)
    1.23 + * scalbn(x,n) returns x* 2**n  computed by  exponent
    1.24 + * manipulation rather than by actually performing an
    1.25 + * exponentiation or a multiplication.
    1.26 + */
    1.27 +
    1.28 +#include "math.h"
    1.29 +#include "math_private.h"
    1.30 +
    1.31 +libm_hidden_proto(copysign)
    1.32 +#ifdef __STDC__
    1.33 +     static const double
    1.34 +#else
    1.35 +     static double
    1.36 +#endif
    1.37 +       two54 = 1.80143985094819840000e+16,      /* 0x43500000, 0x00000000 */
    1.38 +         twom54 = 5.55111512312578270212e-17,   /* 0x3C900000, 0x00000000 */
    1.39 +         huge = 1.0e+300, tiny = 1.0e-300;
    1.40 +
    1.41 +libm_hidden_proto(scalbn)
    1.42 +#ifdef __STDC__
    1.43 +     double scalbn(double x, int n)
    1.44 +#else
    1.45 +     double scalbn(x, n)
    1.46 +     double x;
    1.47 +     int n;
    1.48 +#endif
    1.49 +{
    1.50 +    int32_t k, hx, lx;
    1.51 +    EXTRACT_WORDS(hx, lx, x);
    1.52 +    k = (hx & 0x7ff00000) >> 20;        /* extract exponent */
    1.53 +    if (k == 0) {               /* 0 or subnormal x */
    1.54 +        if ((lx | (hx & 0x7fffffff)) == 0)
    1.55 +            return x;           /* +-0 */
    1.56 +        x *= two54;
    1.57 +        GET_HIGH_WORD(hx, x);
    1.58 +        k = ((hx & 0x7ff00000) >> 20) - 54;
    1.59 +        if (n < -50000)
    1.60 +            return tiny * x;    /*underflow */
    1.61 +    }
    1.62 +    if (k == 0x7ff)
    1.63 +        return x + x;           /* NaN or Inf */
    1.64 +    k = k + n;
    1.65 +    if (k > 0x7fe)
    1.66 +        return huge * copysign(huge, x);        /* overflow  */
    1.67 +    if (k > 0) {                /* normal result */
    1.68 +        SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
    1.69 +        return x;
    1.70 +    }
    1.71 +    if (k <= -54) {
    1.72 +        if (n > 50000)          /* in case integer overflow in n+k */
    1.73 +            return huge * copysign(huge, x);    /*overflow */
    1.74 +        else
    1.75 +            return tiny * copysign(tiny, x);    /*underflow */
    1.76 +    }
    1.77 +    k += 54;                    /* subnormal result */
    1.78 +    SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
    1.79 +    return x * twom54;
    1.80 +}
    1.81 +
    1.82 +libm_hidden_def(scalbn)