1143206Sdas/* @(#)s_scalbn.c 5.1 93/09/24 */ 2143206Sdas/* 3143206Sdas * ==================================================== 4143206Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5143206Sdas * 6143206Sdas * Developed at SunPro, a Sun Microsystems, Inc. business. 7143206Sdas * Permission to use, copy, modify, and distribute this 8143206Sdas * software is freely granted, provided that this notice 9143206Sdas * is preserved. 10143206Sdas * ==================================================== 11143206Sdas */ 12143206Sdas 13143206Sdas#ifndef lint 14143206Sdasstatic char rcsid[] = "$FreeBSD$"; 15143206Sdas#endif 16143206Sdas 17143206Sdas/* 18143206Sdas * scalbnl (long double x, int n) 19143206Sdas * scalbnl(x,n) returns x* 2**n computed by exponent 20143206Sdas * manipulation rather than by actually performing an 21143206Sdas * exponentiation or a multiplication. 22143206Sdas */ 23143206Sdas 24143206Sdas/* 25143206Sdas * We assume that a long double has a 15-bit exponent. On systems 26143206Sdas * where long double is the same as double, scalbnl() is an alias 27143206Sdas * for scalbn(), so we don't use this routine. 28143206Sdas */ 29143206Sdas 30143206Sdas#include <sys/cdefs.h> 31143206Sdas#include <float.h> 32143206Sdas#include <math.h> 33143206Sdas 34143206Sdas#include "fpmath.h" 35143206Sdas 36143206Sdas#if LDBL_MAX_EXP != 0x4000 37143206Sdas#error "Unsupported long double format" 38143206Sdas#endif 39143206Sdas 40143206Sdasstatic const long double 41143206Sdashuge = 0x1p16000L, 42143206Sdastiny = 0x1p-16000L; 43143206Sdas 44143206Sdaslong double 45143206Sdasscalbnl (long double x, int n) 46143206Sdas{ 47143206Sdas union IEEEl2bits u; 48143206Sdas int k; 49143206Sdas u.e = x; 50143206Sdas k = u.bits.exp; /* extract exponent */ 51143206Sdas if (k==0) { /* 0 or subnormal x */ 52143206Sdas if ((u.bits.manh|u.bits.manl)==0) return x; /* +-0 */ 53143206Sdas u.e *= 0x1p+128; 54143206Sdas k = u.bits.exp - 128; 55143206Sdas if (n< -50000) return tiny*x; /*underflow*/ 56143206Sdas } 57143206Sdas if (k==0x7fff) return x+x; /* NaN or Inf */ 58143206Sdas k = k+n; 59143206Sdas if (k >= 0x7fff) return huge*copysignl(huge,x); /* overflow */ 60143206Sdas if (k > 0) /* normal result */ 61143206Sdas {u.bits.exp = k; return u.e;} 62143206Sdas if (k <= -128) 63143206Sdas if (n > 50000) /* in case integer overflow in n+k */ 64143206Sdas return huge*copysign(huge,x); /*overflow*/ 65143206Sdas else return tiny*copysign(tiny,x); /*underflow*/ 66143206Sdas k += 128; /* subnormal result */ 67143206Sdas u.bits.exp = k; 68143206Sdas return u.e*0x1p-128; 69143206Sdas} 70143206Sdas 71143206Sdas__strong_reference(scalbnl, ldexpl); 72