25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* handle trivial cases */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0x4b000000) { /* x is nan, inf, or already integral */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x * x); /* + -> * for Cheetah */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* get the rounding mode */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* flip the sense of directed roundings if x is negative */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (sx && (rm == FE_UPWARD || rm == FE_DOWNWARD))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* handle |x| < 1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FE_UPWARD || (rm == FE_TONEAREST && hx > 0x3f000000))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* round x at the integer bit */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FE_UPWARD || (rm == FE_TONEAREST && (frac > (i >> 1) ||
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Alternate implementations for SPARC, x86, using fp ops. These may
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * be faster depending on how expensive saving and restoring the fp
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * modes and status flags is.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* handle trivial cases */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0x4b000000) /* x is nan, inf, or already integral */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x + 0.0f);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* save the fsr */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* handle |x| < 1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* flip the sense of directed roundings if x is negative */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && hx > 0x3f000000))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* clear the inexact trap */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* round x at the integer bit */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* restore the old fsr */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* inline template */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisextern long double frndint(long double);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis long double z;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* save the control and status words, mask the inexact exception */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis z = frndint((long double) x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * restore the control and status words, preserving all but the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * inexact flag
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* note: the value of z is representable in single precision */