1140607Sdas/* @(#)s_scalbn.c 5.1 93/09/24 */ 2140607Sdas/* @(#)fdlibm.h 5.1 93/09/24 */ 3140607Sdas/* 4140607Sdas * ==================================================== 5140607Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6140607Sdas * 7140607Sdas * Developed at SunPro, a Sun Microsystems, Inc. business. 8140607Sdas * Permission to use, copy, modify, and distribute this 9140607Sdas * software is freely granted, provided that this notice 10140607Sdas * is preserved. 11140607Sdas * ==================================================== 12140607Sdas */ 13140607Sdas 14140607Sdas#include <sys/cdefs.h> 15140607Sdas__FBSDID("$FreeBSD: releng/10.3/lib/libc/gen/ldexp.c 140607 2005-01-22 06:03:40Z das $"); 16140607Sdas 17140607Sdas#include <sys/types.h> 18140607Sdas#include <machine/endian.h> 19140607Sdas#include <math.h> 20140607Sdas 21140607Sdas/* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */ 22140607Sdas 23140607Sdas#if BYTE_ORDER == BIG_ENDIAN 24140607Sdas 25140607Sdastypedef union 26140607Sdas{ 27140607Sdas double value; 28140607Sdas struct 29140607Sdas { 30140607Sdas u_int32_t msw; 31140607Sdas u_int32_t lsw; 32140607Sdas } parts; 33140607Sdas} ieee_double_shape_type; 34140607Sdas 35140607Sdas#endif 36140607Sdas 37140607Sdas#if BYTE_ORDER == LITTLE_ENDIAN 38140607Sdas 39140607Sdastypedef union 40140607Sdas{ 41140607Sdas double value; 42140607Sdas struct 43140607Sdas { 44140607Sdas u_int32_t lsw; 45140607Sdas u_int32_t msw; 46140607Sdas } parts; 47140607Sdas} ieee_double_shape_type; 48140607Sdas 49140607Sdas#endif 50140607Sdas 51140607Sdas/* Get two 32 bit ints from a double. */ 52140607Sdas 53140607Sdas#define EXTRACT_WORDS(ix0,ix1,d) \ 54140607Sdasdo { \ 55140607Sdas ieee_double_shape_type ew_u; \ 56140607Sdas ew_u.value = (d); \ 57140607Sdas (ix0) = ew_u.parts.msw; \ 58140607Sdas (ix1) = ew_u.parts.lsw; \ 59140607Sdas} while (0) 60140607Sdas 61140607Sdas/* Get the more significant 32 bit int from a double. */ 62140607Sdas 63140607Sdas#define GET_HIGH_WORD(i,d) \ 64140607Sdasdo { \ 65140607Sdas ieee_double_shape_type gh_u; \ 66140607Sdas gh_u.value = (d); \ 67140607Sdas (i) = gh_u.parts.msw; \ 68140607Sdas} while (0) 69140607Sdas 70140607Sdas/* Set the more significant 32 bits of a double from an int. */ 71140607Sdas 72140607Sdas#define SET_HIGH_WORD(d,v) \ 73140607Sdasdo { \ 74140607Sdas ieee_double_shape_type sh_u; \ 75140607Sdas sh_u.value = (d); \ 76140607Sdas sh_u.parts.msw = (v); \ 77140607Sdas (d) = sh_u.value; \ 78140607Sdas} while (0) 79140607Sdas 80140607Sdas 81140607Sdasstatic const double 82140607Sdastwo54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ 83140607Sdastwom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ 84140607Sdashuge = 1.0e+300, 85140607Sdastiny = 1.0e-300; 86140607Sdas 87140607Sdasstatic double 88140607Sdas_copysign(double x, double y) 89140607Sdas{ 90140607Sdas u_int32_t hx,hy; 91140607Sdas GET_HIGH_WORD(hx,x); 92140607Sdas GET_HIGH_WORD(hy,y); 93140607Sdas SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000)); 94140607Sdas return x; 95140607Sdas} 96140607Sdas 97140607Sdasdouble 98140607Sdasldexp(double x, int n) 99140607Sdas{ 100140607Sdas int32_t k,hx,lx; 101140607Sdas EXTRACT_WORDS(hx,lx,x); 102140607Sdas k = (hx&0x7ff00000)>>20; /* extract exponent */ 103140607Sdas if (k==0) { /* 0 or subnormal x */ 104140607Sdas if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ 105140607Sdas x *= two54; 106140607Sdas GET_HIGH_WORD(hx,x); 107140607Sdas k = ((hx&0x7ff00000)>>20) - 54; 108140607Sdas if (n< -50000) return tiny*x; /*underflow*/ 109140607Sdas } 110140607Sdas if (k==0x7ff) return x+x; /* NaN or Inf */ 111140607Sdas k = k+n; 112140607Sdas if (k > 0x7fe) return huge*_copysign(huge,x); /* overflow */ 113140607Sdas if (k > 0) /* normal result */ 114140607Sdas {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} 115140607Sdas if (k <= -54) { 116140607Sdas if (n > 50000) /* in case integer overflow in n+k */ 117140607Sdas return huge*_copysign(huge,x); /*overflow*/ 118140607Sdas else return tiny*_copysign(tiny,x); /*underflow*/ 119140607Sdas } 120140607Sdas k += 54; /* subnormal result */ 121140607Sdas SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); 122140607Sdas return x*twom54; 123140607Sdas} 124