s_scalbn.c revision 97413
11541Srgrimes/* @(#)s_scalbn.c 5.1 93/09/24 */ 21541Srgrimes/* 31541Srgrimes * ==================================================== 41541Srgrimes * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 51541Srgrimes * 61541Srgrimes * Developed at SunPro, a Sun Microsystems, Inc. business. 71541Srgrimes * Permission to use, copy, modify, and distribute this 81541Srgrimes * software is freely granted, provided that this notice 91541Srgrimes * is preserved. 101541Srgrimes * ==================================================== 111541Srgrimes */ 121541Srgrimes 131541Srgrimes#ifndef lint 141541Srgrimesstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_scalbn.c 97413 2002-05-28 18:15:04Z alfred $"; 151541Srgrimes#endif 161541Srgrimes 171541Srgrimes/* 181541Srgrimes * scalbn (double x, int n) 191541Srgrimes * scalbn(x,n) returns x* 2**n computed by exponent 201541Srgrimes * manipulation rather than by actually performing an 211541Srgrimes * exponentiation or a multiplication. 221541Srgrimes */ 231541Srgrimes 241541Srgrimes#include "math.h" 251541Srgrimes#include "math_private.h" 261541Srgrimes 271541Srgrimesstatic const double 281541Srgrimestwo54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ 291541Srgrimestwom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ 301541Srgrimeshuge = 1.0e+300, 311541Srgrimestiny = 1.0e-300; 321541Srgrimes 331541Srgrimesdouble 341541Srgrimes__generic_scalbn (double x, int n) 351541Srgrimes{ 3636503Speter int32_t k,hx,lx; 3748274Speter EXTRACT_WORDS(hx,lx,x); 381541Srgrimes k = (hx&0x7ff00000)>>20; /* extract exponent */ 391541Srgrimes if (k==0) { /* 0 or subnormal x */ 401541Srgrimes if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ 411541Srgrimes x *= two54; 421541Srgrimes GET_HIGH_WORD(hx,x); 431541Srgrimes k = ((hx&0x7ff00000)>>20) - 54; 441541Srgrimes if (n< -50000) return tiny*x; /*underflow*/ 451541Srgrimes } 4648274Speter if (k==0x7ff) return x+x; /* NaN or Inf */ 4748274Speter k = k+n; 4831886Sbde if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */ 491541Srgrimes if (k > 0) /* normal result */ 501541Srgrimes {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} 511541Srgrimes if (k <= -54) 521541Srgrimes if (n > 50000) /* in case integer overflow in n+k */ 531541Srgrimes return huge*copysign(huge,x); /*overflow*/ 541541Srgrimes else return tiny*copysign(tiny,x); /*underflow*/ 551541Srgrimes k += 54; /* subnormal result */ 569336Sdfr SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); 572997Swollman return x*twom54; 582997Swollman} 591541Srgrimes