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 unsigned i[4];
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis long double q;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* handle trivial cases */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0x406f0000) { /* x is nan, inf, or already integral */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* check for signaling nan */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if ((hx > 0x7fff0000 || (hx == 0x7fff0000 &&
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis (xx.i[1] | xx.i[2] | xx.i[3]))) && !(hx & 0x8000)) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if ((hx | xx.i[1] | xx.i[2] | xx.i[3]) == 0) /* x is zero */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* get the rounding mode */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* flip the sense of directed roundings if x is negative */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* handle |x| < 1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && (hx >= 0x3ffe0000 &&
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ((hx & 0xffff) | xx.i[1] | xx.i[2] | xx.i[3]))))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* round x at the integer bit */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (j >= 96) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis frac = ((xx.i[0] << 1) << (127 - j)) | (xx.i[1] >> (j - 96));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if ((xx.i[1] & (i - 1)) | xx.i[2] | xx.i[3])
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (j >= 64) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis frac = ((xx.i[1] << 1) << (95 - j)) | (xx.i[2] >> (j - 64));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis (frac == 0x80000000 && (xx.i[1] & i))))) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (j >= 32) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis frac = ((xx.i[2] << 1) << (63 - j)) | (xx.i[3] >> (j - 32));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis (frac == 0x80000000 && (xx.i[2] & i))))) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (rm == FSR_RP || (rm == FSR_RN && (frac > 0x80000000u ||
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis (frac == 0x80000000 && (xx.i[3] & i))))) {
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 * restore the control and status words, preserving all but the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * inexact flag