1 /* $OpenBSD: s_scalbnl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $ */ 2 /* @(#)s_scalbn.c 5.1 93/09/24 */ 3 /* 4 * ==================================================== 5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6 * 7 * Developed at SunPro, a Sun Microsystems, Inc. business. 8 * Permission to use, copy, modify, and distribute this 9 * software is freely granted, provided that this notice 10 * is preserved. 11 * ==================================================== 12 */ 13 14 /* 15 * scalbnl (long double x, int n) 16 * scalbnl(x,n) returns x* 2**n computed by exponent 17 * manipulation rather than by actually performing an 18 * exponentiation or a multiplication. 19 */ 20 21 /* 22 * We assume that a long double has a 15-bit exponent. On systems 23 * where long double is the same as double, scalbnl() is an alias 24 * for scalbn(), so we don't use this routine. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/types.h> 29 #include <machine/ieee.h> 30 #include <float.h> 31 #include <math.h> 32 33 #if LDBL_MAX_EXP != 0x4000 34 #error "Unsupported long double format" 35 #endif 36 37 static const long double 38 huge = 0x1p16000L, 39 tiny = 0x1p-16000L; 40 41 long double 42 scalbnl (long double x, int n) 43 { 44 union { 45 long double e; 46 struct ieee_ext bits; 47 } u; 48 int k; 49 u.e = x; 50 k = u.bits.ext_exp; /* extract exponent */ 51 if (k==0) { /* 0 or subnormal x */ 52 if ((u.bits.ext_frach 53 #ifdef EXT_FRACHMBITS 54 | u.bits.ext_frachm 55 #endif /* EXT_FRACHMBITS */ 56 #ifdef EXT_FRACLMBITS 57 | u.bits.ext_fraclm 58 #endif /* EXT_FRACLMBITS */ 59 | u.bits.ext_fracl)==0) return x; /* +-0 */ 60 u.e *= 0x1p+128; 61 k = u.bits.ext_exp - 128; 62 if (n< -50000) return tiny*x; /*underflow*/ 63 } 64 if (k==0x7fff) return x+x; /* NaN or Inf */ 65 k = k+n; 66 if (k >= 0x7fff) return huge*copysignl(huge,x); /* overflow */ 67 if (k > 0) /* normal result */ 68 {u.bits.ext_exp = k; return u.e;} 69 if (k <= -128) 70 if (n > 50000) /* in case integer overflow in n+k */ 71 return huge*copysign(huge,x); /*overflow*/ 72 else return tiny*copysign(tiny,x); /*underflow*/ 73 k += 128; /* subnormal result */ 74 u.bits.ext_exp = k; 75 return u.e*0x1p-128; 76 } 77 78 long double 79 ldexpl(long double x, int n) 80 { 81 return scalbnl(x, n); 82 } 83