15b2ba9d3SPiotr Jasiukajtis /*
25b2ba9d3SPiotr Jasiukajtis * CDDL HEADER START
35b2ba9d3SPiotr Jasiukajtis *
45b2ba9d3SPiotr Jasiukajtis * The contents of this file are subject to the terms of the
55b2ba9d3SPiotr Jasiukajtis * Common Development and Distribution License (the "License").
65b2ba9d3SPiotr Jasiukajtis * You may not use this file except in compliance with the License.
75b2ba9d3SPiotr Jasiukajtis *
85b2ba9d3SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95b2ba9d3SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
105b2ba9d3SPiotr Jasiukajtis * See the License for the specific language governing permissions
115b2ba9d3SPiotr Jasiukajtis * and limitations under the License.
125b2ba9d3SPiotr Jasiukajtis *
135b2ba9d3SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
145b2ba9d3SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155b2ba9d3SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
165b2ba9d3SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
175b2ba9d3SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
185b2ba9d3SPiotr Jasiukajtis *
195b2ba9d3SPiotr Jasiukajtis * CDDL HEADER END
205b2ba9d3SPiotr Jasiukajtis */
215b2ba9d3SPiotr Jasiukajtis /*
225b2ba9d3SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
235b2ba9d3SPiotr Jasiukajtis */
245b2ba9d3SPiotr Jasiukajtis /*
255b2ba9d3SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
265b2ba9d3SPiotr Jasiukajtis * Use is subject to license terms.
275b2ba9d3SPiotr Jasiukajtis */
285b2ba9d3SPiotr Jasiukajtis
29*a9d3dcd5SRichard Lowe #pragma weak __scalbn = scalbn
305b2ba9d3SPiotr Jasiukajtis
315b2ba9d3SPiotr Jasiukajtis #include "libm.h"
325b2ba9d3SPiotr Jasiukajtis
335b2ba9d3SPiotr Jasiukajtis static const double
345b2ba9d3SPiotr Jasiukajtis one = 1.0,
355b2ba9d3SPiotr Jasiukajtis huge = 1.0e300,
365b2ba9d3SPiotr Jasiukajtis tiny = 1.0e-300,
375b2ba9d3SPiotr Jasiukajtis twom54 = 5.5511151231257827021181583404541015625e-17;
385b2ba9d3SPiotr Jasiukajtis
39abd342e1SRichard Lowe #if defined(__x86)
405b2ba9d3SPiotr Jasiukajtis static const double two52 = 4503599627370496.0;
415b2ba9d3SPiotr Jasiukajtis #else
425b2ba9d3SPiotr Jasiukajtis /*
435b2ba9d3SPiotr Jasiukajtis * Normalize non-zero subnormal x and return biased exponent of x in [-51,0]
445b2ba9d3SPiotr Jasiukajtis */
455b2ba9d3SPiotr Jasiukajtis static int
ilogb_biased(unsigned * px)465b2ba9d3SPiotr Jasiukajtis ilogb_biased(unsigned *px) {
475b2ba9d3SPiotr Jasiukajtis int s = 52;
485b2ba9d3SPiotr Jasiukajtis unsigned v = px[HIWORD] & ~0x80000000, w = px[LOWORD], t = v;
495b2ba9d3SPiotr Jasiukajtis
505b2ba9d3SPiotr Jasiukajtis if (t)
515b2ba9d3SPiotr Jasiukajtis s -= 32;
525b2ba9d3SPiotr Jasiukajtis else
535b2ba9d3SPiotr Jasiukajtis t = w;
545b2ba9d3SPiotr Jasiukajtis if (t & 0xffff0000)
555b2ba9d3SPiotr Jasiukajtis s -= 16, t >>= 16;
565b2ba9d3SPiotr Jasiukajtis if (t & 0xff00)
575b2ba9d3SPiotr Jasiukajtis s -= 8, t >>= 8;
585b2ba9d3SPiotr Jasiukajtis if (t & 0xf0)
595b2ba9d3SPiotr Jasiukajtis s -= 4, t >>= 4;
605b2ba9d3SPiotr Jasiukajtis t <<= 1;
615b2ba9d3SPiotr Jasiukajtis s -= (0xffffaa50 >> t) & 0x3;
625b2ba9d3SPiotr Jasiukajtis if (s < 32) {
635b2ba9d3SPiotr Jasiukajtis v = (v << s) | w >> (32 - s);
645b2ba9d3SPiotr Jasiukajtis w <<= s;
655b2ba9d3SPiotr Jasiukajtis } else {
665b2ba9d3SPiotr Jasiukajtis v = w << (s - 32);
675b2ba9d3SPiotr Jasiukajtis w = 0;
685b2ba9d3SPiotr Jasiukajtis }
695b2ba9d3SPiotr Jasiukajtis px[HIWORD] = (px[HIWORD] & 0x80000000) | v;
705b2ba9d3SPiotr Jasiukajtis px[LOWORD] = w;
715b2ba9d3SPiotr Jasiukajtis return (1 - s);
725b2ba9d3SPiotr Jasiukajtis }
73abd342e1SRichard Lowe #endif /* defined(__x86) */
745b2ba9d3SPiotr Jasiukajtis
755b2ba9d3SPiotr Jasiukajtis double
scalbn(double x,int n)765b2ba9d3SPiotr Jasiukajtis scalbn(double x, int n) {
775b2ba9d3SPiotr Jasiukajtis int *px, ix, hx, k;
785b2ba9d3SPiotr Jasiukajtis
795b2ba9d3SPiotr Jasiukajtis px = (int *)&x;
805b2ba9d3SPiotr Jasiukajtis ix = px[HIWORD];
815b2ba9d3SPiotr Jasiukajtis hx = ix & ~0x80000000;
825b2ba9d3SPiotr Jasiukajtis k = hx >> 20;
835b2ba9d3SPiotr Jasiukajtis
845b2ba9d3SPiotr Jasiukajtis if (k == 0x7ff) /* x is inf or NaN */
855b2ba9d3SPiotr Jasiukajtis return (x * one);
865b2ba9d3SPiotr Jasiukajtis
875b2ba9d3SPiotr Jasiukajtis if (k == 0) {
885b2ba9d3SPiotr Jasiukajtis if ((hx | px[LOWORD]) == 0 || n == 0)
895b2ba9d3SPiotr Jasiukajtis return (x);
90abd342e1SRichard Lowe #if defined(__x86)
915b2ba9d3SPiotr Jasiukajtis x *= two52;
925b2ba9d3SPiotr Jasiukajtis ix = px[HIWORD];
935b2ba9d3SPiotr Jasiukajtis k = ((ix & ~0x80000000) >> 20) - 52;
945b2ba9d3SPiotr Jasiukajtis #else
955b2ba9d3SPiotr Jasiukajtis k = ilogb_biased((unsigned *)px);
965b2ba9d3SPiotr Jasiukajtis ix = px[HIWORD];
975b2ba9d3SPiotr Jasiukajtis #endif
985b2ba9d3SPiotr Jasiukajtis /* now k is in the range -51..0 */
995b2ba9d3SPiotr Jasiukajtis k += n;
1005b2ba9d3SPiotr Jasiukajtis if (k > n) /* integer overflow occurred */
1015b2ba9d3SPiotr Jasiukajtis k = -100;
1025b2ba9d3SPiotr Jasiukajtis } else {
1035b2ba9d3SPiotr Jasiukajtis /* k is in the range 1..1023 */
1045b2ba9d3SPiotr Jasiukajtis k += n;
1055b2ba9d3SPiotr Jasiukajtis if (k < n) /* integer overflow occurred */
1065b2ba9d3SPiotr Jasiukajtis k = 0x7ff;
1075b2ba9d3SPiotr Jasiukajtis }
1085b2ba9d3SPiotr Jasiukajtis
1095b2ba9d3SPiotr Jasiukajtis if (k > 0x7fe)
1105b2ba9d3SPiotr Jasiukajtis return (huge * ((ix < 0)? -huge : huge));
1115b2ba9d3SPiotr Jasiukajtis if (k < 1) {
1125b2ba9d3SPiotr Jasiukajtis if (k <= -54)
1135b2ba9d3SPiotr Jasiukajtis return (tiny * ((ix < 0)? -tiny : tiny));
1145b2ba9d3SPiotr Jasiukajtis k += 54;
1155b2ba9d3SPiotr Jasiukajtis px[HIWORD] = (ix & ~0x7ff00000) | (k << 20);
1165b2ba9d3SPiotr Jasiukajtis return (x * twom54);
1175b2ba9d3SPiotr Jasiukajtis }
1185b2ba9d3SPiotr Jasiukajtis px[HIWORD] = (ix & ~0x7ff00000) | (k << 20);
1195b2ba9d3SPiotr Jasiukajtis return (x);
1205b2ba9d3SPiotr Jasiukajtis }
121