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 __ilogbl = ilogbl
315b2ba9d3SPiotr Jasiukajtis
325b2ba9d3SPiotr Jasiukajtis #include "libm.h"
335b2ba9d3SPiotr Jasiukajtis #include "xpg6.h" /* __xpg6 */
345b2ba9d3SPiotr Jasiukajtis
355b2ba9d3SPiotr Jasiukajtis #if defined(__sparc)
365b2ba9d3SPiotr Jasiukajtis #define ISNORMALL(k, x) (k != 0x7fff) /* assuming k != 0 */
375b2ba9d3SPiotr Jasiukajtis #define X86PDNRM(k, x)
385b2ba9d3SPiotr Jasiukajtis #define XSCALE_OFFSET 0x406f /* 0x3fff + 112 */
395b2ba9d3SPiotr Jasiukajtis static const long double xscale = 5192296858534827628530496329220096.0L;
405b2ba9d3SPiotr Jasiukajtis /* 2^112 */
415b2ba9d3SPiotr Jasiukajtis #elif defined(__x86)
425b2ba9d3SPiotr Jasiukajtis /*
435b2ba9d3SPiotr Jasiukajtis * if pseudo-denormal, replace by the equivalent normal
445b2ba9d3SPiotr Jasiukajtis */
455b2ba9d3SPiotr Jasiukajtis #define X86PDNRM(k, x) if (k == 0 && (((int *) &x)[1] & 0x80000000) != 0) \
465b2ba9d3SPiotr Jasiukajtis ((int *) &x)[2] |= k = 1
475b2ba9d3SPiotr Jasiukajtis #if defined(HANDLE_UNSUPPORTED) /* assuming k != 0 */
485b2ba9d3SPiotr Jasiukajtis #define ISNORMALL(k, x) (k != 0x7fff && (((int *) &x)[1] & 0x80000000) != 0)
495b2ba9d3SPiotr Jasiukajtis #else
505b2ba9d3SPiotr Jasiukajtis #define ISNORMALL(k, x) (k != 0x7fff)
515b2ba9d3SPiotr Jasiukajtis #endif
525b2ba9d3SPiotr Jasiukajtis #define XSCALE_OFFSET 0x403e /* 0x3fff + 63 */
535b2ba9d3SPiotr Jasiukajtis static const long double xscale = 9223372036854775808.0L; /* 2^63 */
545b2ba9d3SPiotr Jasiukajtis #endif
555b2ba9d3SPiotr Jasiukajtis
565b2ba9d3SPiotr Jasiukajtis static int
raise_invalid(int v)575b2ba9d3SPiotr Jasiukajtis raise_invalid(int v) { /* SUSv3 requires ilogbl(0,+/-Inf,NaN) raise invalid */
585b2ba9d3SPiotr Jasiukajtis #ifndef lint
595b2ba9d3SPiotr Jasiukajtis if ((__xpg6 & _C99SUSv3_ilogb_0InfNaN_raises_invalid) != 0) {
605b2ba9d3SPiotr Jasiukajtis static const double zero = 0.0;
615b2ba9d3SPiotr Jasiukajtis volatile double dummy;
625b2ba9d3SPiotr Jasiukajtis
635b2ba9d3SPiotr Jasiukajtis dummy = zero / zero;
645b2ba9d3SPiotr Jasiukajtis }
655b2ba9d3SPiotr Jasiukajtis #endif
665b2ba9d3SPiotr Jasiukajtis return (v);
675b2ba9d3SPiotr Jasiukajtis }
685b2ba9d3SPiotr Jasiukajtis
695b2ba9d3SPiotr Jasiukajtis int
ilogbl(long double x)705b2ba9d3SPiotr Jasiukajtis ilogbl(long double x) {
715b2ba9d3SPiotr Jasiukajtis int k = XBIASED_EXP(x);
725b2ba9d3SPiotr Jasiukajtis
735b2ba9d3SPiotr Jasiukajtis X86PDNRM(k, x);
745b2ba9d3SPiotr Jasiukajtis if (k == 0) {
755b2ba9d3SPiotr Jasiukajtis if (ISZEROL(x))
765b2ba9d3SPiotr Jasiukajtis return (raise_invalid(0x80000001));
775b2ba9d3SPiotr Jasiukajtis else {
785b2ba9d3SPiotr Jasiukajtis x *= xscale; /* scale up by 2^112 or 2^63 */
795b2ba9d3SPiotr Jasiukajtis return (XBIASED_EXP(x) - XSCALE_OFFSET);
805b2ba9d3SPiotr Jasiukajtis }
815b2ba9d3SPiotr Jasiukajtis } else if (ISNORMALL(k, x))
825b2ba9d3SPiotr Jasiukajtis return (k - 0x3fff);
835b2ba9d3SPiotr Jasiukajtis else
845b2ba9d3SPiotr Jasiukajtis return (raise_invalid(0x7fffffff));
855b2ba9d3SPiotr Jasiukajtis }
86