xref: /titanic_41/usr/src/lib/libm/common/Q/scalbnl.c (revision a9d3dcd5820128b4f34bf38f447e47aa95c004e8)
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 /*
235b2ba9d3SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
245b2ba9d3SPiotr Jasiukajtis  */
255b2ba9d3SPiotr Jasiukajtis /*
265b2ba9d3SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
275b2ba9d3SPiotr Jasiukajtis  * Use is subject to license terms.
285b2ba9d3SPiotr Jasiukajtis  */
295b2ba9d3SPiotr Jasiukajtis 
30*a9d3dcd5SRichard Lowe #pragma weak __scalbnl = scalbnl
315b2ba9d3SPiotr Jasiukajtis 
325b2ba9d3SPiotr Jasiukajtis #include "libm.h"
335b2ba9d3SPiotr Jasiukajtis #include <float.h>		/* LDBL_MAX, LDBL_MIN */
345b2ba9d3SPiotr Jasiukajtis #include <stdlib.h>		/* abs */
355b2ba9d3SPiotr Jasiukajtis 
365b2ba9d3SPiotr Jasiukajtis #if defined(__sparc)
375b2ba9d3SPiotr Jasiukajtis #define	XSET_EXP(k, x)	((int *) &x)[0] = (((int *) &x)[0] & ~0x7fff0000) | \
385b2ba9d3SPiotr Jasiukajtis 				(k << 16)
395b2ba9d3SPiotr Jasiukajtis #define	ISINFNANL(k, x)	(k == 0x7fff)
405b2ba9d3SPiotr Jasiukajtis #define	XTWOT_OFFSET	113
415b2ba9d3SPiotr Jasiukajtis static const long double xtwot = 10384593717069655257060992658440192.0L,
425b2ba9d3SPiotr Jasiukajtis 								/* 2^113 */
435b2ba9d3SPiotr Jasiukajtis 	twomtm1 = 4.814824860968089632639944856462318296E-35L;	/* 2^-114 */
445b2ba9d3SPiotr Jasiukajtis #elif defined(__x86)
455b2ba9d3SPiotr Jasiukajtis #define	XSET_EXP(k, x)	((int *) &x)[2] = (((int *) &x)[2] & ~0x7fff) | k
465b2ba9d3SPiotr Jasiukajtis #if defined(HANDLE_UNSUPPORTED)
475b2ba9d3SPiotr Jasiukajtis #define	ISINFNANL(k, x)	(k == 0x7fff || k != 0 && \
485b2ba9d3SPiotr Jasiukajtis 				(((int *) &x)[1] & 0x80000000) == 0)
495b2ba9d3SPiotr Jasiukajtis #else
505b2ba9d3SPiotr Jasiukajtis #define	ISINFNANL(k, x)	(k == 0x7fff)
515b2ba9d3SPiotr Jasiukajtis #endif
525b2ba9d3SPiotr Jasiukajtis #define	XTWOT_OFFSET	64
535b2ba9d3SPiotr Jasiukajtis static const long double xtwot = 18446744073709551616.0L,	/* 2^64 */
545b2ba9d3SPiotr Jasiukajtis 	twomtm1 = 2.7105054312137610850186E-20L;		/* 2^-65 */
555b2ba9d3SPiotr Jasiukajtis #endif
565b2ba9d3SPiotr Jasiukajtis 
575b2ba9d3SPiotr Jasiukajtis long double
scalbnl(long double x,int n)585b2ba9d3SPiotr Jasiukajtis scalbnl(long double x, int n) {
595b2ba9d3SPiotr Jasiukajtis 	int k = XBIASED_EXP(x);
605b2ba9d3SPiotr Jasiukajtis 
615b2ba9d3SPiotr Jasiukajtis 	if (ISINFNANL(k, x))
625b2ba9d3SPiotr Jasiukajtis 		return (x + x);
635b2ba9d3SPiotr Jasiukajtis 	if (ISZEROL(x) || n == 0)
645b2ba9d3SPiotr Jasiukajtis 		return (x);
655b2ba9d3SPiotr Jasiukajtis 	if (k == 0) {
665b2ba9d3SPiotr Jasiukajtis 		x *= xtwot;
675b2ba9d3SPiotr Jasiukajtis 		k = XBIASED_EXP(x) - XTWOT_OFFSET;
685b2ba9d3SPiotr Jasiukajtis 	}
695b2ba9d3SPiotr Jasiukajtis 	if ((unsigned) abs(n) >= 131072)	/* cast to unsigned for -2^31 */
705b2ba9d3SPiotr Jasiukajtis 		n >>= 1;		/* avoid subsequent integer overflow */
715b2ba9d3SPiotr Jasiukajtis 	k += n;
725b2ba9d3SPiotr Jasiukajtis 	if (k > 0x7ffe)
735b2ba9d3SPiotr Jasiukajtis 		return (LDBL_MAX * copysignl(LDBL_MAX, x));
745b2ba9d3SPiotr Jasiukajtis 	if (k <= -XTWOT_OFFSET - 1)
755b2ba9d3SPiotr Jasiukajtis 		return (LDBL_MIN * copysignl(LDBL_MIN, x));
765b2ba9d3SPiotr Jasiukajtis 	if (k > 0) {
775b2ba9d3SPiotr Jasiukajtis 		XSET_EXP(k, x);
785b2ba9d3SPiotr Jasiukajtis 		return (x);
795b2ba9d3SPiotr Jasiukajtis 	}
805b2ba9d3SPiotr Jasiukajtis 	k += XTWOT_OFFSET + 1;
815b2ba9d3SPiotr Jasiukajtis 	XSET_EXP(k, x);
825b2ba9d3SPiotr Jasiukajtis 	return (x * twomtm1);
835b2ba9d3SPiotr Jasiukajtis }
84