POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/scalbn.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* @(#)s_scalbn.c 5.1 93/09/24 */
00018 /*
00019  * ====================================================
00020  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00021  *
00022  * Developed at SunPro, a Sun Microsystems, Inc. business.
00023  * Permission to use, copy, modify, and distribute this
00024  * software is freely granted, provided that this notice
00025  * is preserved.
00026  * ====================================================
00027  */
00028 
00029 #ifdef POK_NEEDS_LIBMATH
00030 
00031 /*
00032  * scalbn (double x, int n)
00033  * scalbn(x,n) returns x* 2**n  computed by  exponent
00034  * manipulation rather than by actually performing an
00035  * exponentiation or a multiplication.
00036  */
00037 
00038 #include <libm.h>
00039 #include "math_private.h"
00040 
00041 static const double
00042 two54   =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
00043 twom54  =  5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
00044 huge   = 1.0e+300,
00045 tiny   = 1.0e-300;
00046 
00047 double
00048 scalbn(double x, int n)
00049 {
00050         int32_t k,hx,lx;
00051         EXTRACT_WORDS(hx,lx,x);
00052         k = ((uint32_t)hx&0x7ff00000)>>20;              /* extract exponent */
00053         if (k==0) {                             /* 0 or subnormal x */
00054             if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
00055             x *= two54;
00056             GET_HIGH_WORD(hx,x);
00057             k = (((uint32_t)hx&0x7ff00000)>>20) - 54;
00058             if (n< -50000) return tiny*x;       /*underflow*/
00059             }
00060         if (k==0x7ff) return x+x;               /* NaN or Inf */
00061         k = k+n;
00062         if (k >  0x7fe) return huge*copysign(huge,x); /* overflow  */
00063         if (k > 0)                              /* normal result */
00064             {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
00065         if (k <= -54) {
00066             if (n > 50000)      /* in case integer overflow in n+k */
00067                 return huge*copysign(huge,x);   /*overflow*/
00068             else return tiny*copysign(tiny,x);  /*underflow*/
00069         }
00070         k += 54;                                /* subnormal result */
00071         SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
00072         return x*twom54;
00073 }
00074 
00075 #endif