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

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

libm: add the scalbn() function

  • Property mode set to 100644
File size: 1.1 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
21/* scalbn(x,n) returns x* 2**n computed by exponent
22 * manipulation rather than by actually performing an
23 * exponentiation or a multiplication.
24 */
25
26double scalbn(double x, int n)
27{
28 union {double f; uint64_t i;} u;
29 double_t y = x;
30
31 if (n > 1023) {
32 y *= 0x1p1023;
33 n -= 1023;
34 if (n > 1023) {
35 y *= 0x1p1023;
36 n -= 1023;
37 if (n > 1023)
38 n = 1023;
39 }
40 } else if (n < -1022) {
41 /* make sure final n < -53 to avoid double
42 rounding in the subnormal range */
43 y *= 0x1p-1022 * 0x1p53;
44 n += 1022 - 53;
45 if (n < -1022) {
46 y *= 0x1p-1022 * 0x1p53;
47 n += 1022 - 53;
48 if (n < -1022)
49 n = -1022;
50 }
51 }
52 u.i = (uint64_t)(0x3ff+n)<<52;
53 x = y * u.f;
54
55 return x;
56}
57
58/** @}
59 */
Note: See TracBrowser for help on using the repository browser.