blob: 28b0cf9b7294ddf502c023d07fce71f3fd17b393 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* @(#)s_scalbn.c 5.1 93/09/24 */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
Elliott Hughes8da8ca42018-05-08 13:35:33 -070013#include <sys/cdefs.h>
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080014__FBSDID("$FreeBSD$");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015
16/*
17 * scalbnl (long double x, int n)
18 * scalbnl(x,n) returns x* 2**n computed by exponent
19 * manipulation rather than by actually performing an
20 * exponentiation or a multiplication.
21 */
22
23/*
24 * We assume that a long double has a 15-bit exponent. On systems
25 * where long double is the same as double, scalbnl() is an alias
26 * for scalbn(), so we don't use this routine.
27 */
28
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#include <float.h>
30#include <math.h>
31
32#include "fpmath.h"
33
34#if LDBL_MAX_EXP != 0x4000
35#error "Unsupported long double format"
36#endif
37
38static const long double
39huge = 0x1p16000L,
40tiny = 0x1p-16000L;
41
42long double
43scalbnl (long double x, int n)
44{
45 union IEEEl2bits u;
46 int k;
47 u.e = x;
48 k = u.bits.exp; /* extract exponent */
49 if (k==0) { /* 0 or subnormal x */
50 if ((u.bits.manh|u.bits.manl)==0) return x; /* +-0 */
51 u.e *= 0x1p+128;
52 k = u.bits.exp - 128;
53 if (n< -50000) return tiny*x; /*underflow*/
54 }
55 if (k==0x7fff) return x+x; /* NaN or Inf */
56 k = k+n;
57 if (k >= 0x7fff) return huge*copysignl(huge,x); /* overflow */
58 if (k > 0) /* normal result */
59 {u.bits.exp = k; return u.e;}
Elliott Hughes8da8ca42018-05-08 13:35:33 -070060 if (k <= -128) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061 if (n > 50000) /* in case integer overflow in n+k */
62 return huge*copysign(huge,x); /*overflow*/
Elliott Hughes8da8ca42018-05-08 13:35:33 -070063 else
64 return tiny*copysign(tiny,x); /*underflow*/
65 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066 k += 128; /* subnormal result */
67 u.bits.exp = k;
68 return u.e*0x1p-128;
69}
70
71__strong_reference(scalbnl, ldexpl);