source: mainline/uspace/lib/math/generic/scalbn.c@ f250c5a

Last change on this file since f250c5a was f250c5a, checked in by Maurizio Lombardi <mlombard@…>, 4 years ago

math: sync scalbn to FreeBSD 11.2

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/*
2 * ====================================================
3 * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software is freely granted, provided that this notice
7 * is preserved.
8 * ====================================================
9 */
10
11/** @addtogroup libmath
12 * @{
13 */
14/** @file scalbn mathematical function
15 */
16
17#include <math.h>
18#include <stdint.h>
19
20#include "internal.h"
21
22/*
23 * scalbn (double x, int n)
24 * scalbn(x,n) returns x* 2**n computed by exponent
25 * manipulation rather than by actually performing an
26 * exponentiation or a multiplication.
27 */
28
29static const double
30two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
31twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
32huge = 1.0e+300,
33tiny = 1.0e-300;
34
35double
36scalbn (double x, int n)
37{
38 int32_t k,hx,lx;
39 EXTRACT_WORDS(hx,lx,x);
40 k = (hx&0x7ff00000)>>20; /* extract exponent */
41 if (k==0) { /* 0 or subnormal x */
42 if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
43 x *= two54;
44 GET_HIGH_WORD(hx,x);
45 k = ((hx&0x7ff00000)>>20) - 54;
46 if (n< -50000) return tiny*x; /*underflow*/
47 }
48 if (k==0x7ff) return x+x; /* NaN or Inf */
49 k = k+n;
50 if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
51 if (k > 0) /* normal result */
52 {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
53 if (k <= -54) {
54 if (n > 50000) /* in case integer overflow in n+k */
55 return huge*copysign(huge,x); /*overflow*/
56 else
57 return tiny*copysign(tiny,x); /*underflow*/
58 }
59 k += 54; /* subnormal result */
60 SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
61 return x*twom54;
62}
63
64/** @}
65 */
Note: See TracBrowser for help on using the repository browser.