2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1994-1997, by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "quad.h"
2N/A
2N/A/*
2N/A * _Qp_qtoux(x) returns (unsigned long)*x.
2N/A */
2N/Aunsigned long
2N/A_Qp_qtoux(const union longdouble *x)
2N/A{
2N/A union longdouble z;
2N/A unsigned long i, round;
2N/A unsigned int xm, fsr;
2N/A
2N/A xm = x->l.msw & 0x7fffffff;
2N/A
2N/A __quad_getfsrp(&fsr);
2N/A
2N/A /* handle nan, inf, and out-of-range cases */
2N/A if (xm >= 0x403e0000) {
2N/A if (x->l.msw < 0x403f0000) {
2N/A i = 0x8000000000000000ul |
2N/A ((long) (xm & 0xffff) << 47) |
2N/A ((long) x->l.frac2 << 15) | (x->l.frac3 >> 17);
2N/A if ((x->l.frac3 & 0x1ffff) | x->l.frac4) {
2N/A /* signal inexact */
2N/A if (fsr & FSR_NXM) {
2N/A /* z = x - 2^63 */
2N/A if (xm & 0xffff ||
2N/A x->l.frac2 & 0xffff0000) {
2N/A z.l.msw = xm & 0xffff;
2N/A z.l.frac2 = x->l.frac2;
2N/A z.l.frac3 = x->l.frac3;
2N/A z.l.frac4 = x->l.frac4;
2N/A } else if (x->l.frac2 & 0xffff ||
2N/A x->l.frac3 & 0xffff0000) {
2N/A z.l.msw = x->l.frac2;
2N/A z.l.frac2 = x->l.frac3;
2N/A z.l.frac3 = x->l.frac4;
2N/A z.l.frac4 = 0;
2N/A } else if (x->l.frac3 & 0xffff ||
2N/A x->l.frac4 & 0xffff0000) {
2N/A z.l.msw = x->l.frac3;
2N/A z.l.frac2 = x->l.frac4;
2N/A z.l.frac3 = z.l.frac4 = 0;
2N/A } else {
2N/A z.l.msw = x->l.frac4;
2N/A z.l.frac2 = z.l.frac3 =
2N/A z.l.frac4 = 0;
2N/A }
2N/A xm = 0x403e;
2N/A while ((z.l.msw & 0x10000) == 0) {
2N/A z.l.msw = (z.l.msw << 1) |
2N/A (z.l.frac2 >> 31);
2N/A z.l.frac2 = (z.l.frac2 << 1) |
2N/A (z.l.frac3 >> 31);
2N/A z.l.frac3 = (z.l.frac3 << 1) |
2N/A (z.l.frac4 >> 31);
2N/A z.l.frac4 <<= 1;
2N/A xm--;
2N/A }
2N/A z.l.msw |= (xm << 16);
2N/A __quad_fqtox(&z, (long *)&i);
2N/A i |= 0x8000000000000000ul;
2N/A } else {
2N/A fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
2N/A FSR_NXC;
2N/A __quad_setfsrp(&fsr);
2N/A }
2N/A }
2N/A return (i);
2N/A }
2N/A if (x->l.msw == 0xc03e0000 && x->l.frac2 == 0 &&
2N/A (x->l.frac3 & 0xfffe0000) == 0) {
2N/A /* return largest negative 64 bit int */
2N/A i = 0x8000000000000000ul;
2N/A if ((x->l.frac3 & 0x1ffff) | x->l.frac4) {
2N/A /* signal inexact */
2N/A if (fsr & FSR_NXM) {
2N/A __quad_fqtox(x, (long *)&i);
2N/A } else {
2N/A fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
2N/A FSR_NXC;
2N/A __quad_setfsrp(&fsr);
2N/A }
2N/A }
2N/A return (i);
2N/A }
2N/A i = 0x7fffffffffffffffl;
2N/A if (fsr & FSR_NVM) {
2N/A __quad_fqtox(x, (long *)&i);
2N/A } else {
2N/A fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
2N/A __quad_setfsrp(&fsr);
2N/A }
2N/A return (i);
2N/A }
2N/A if (xm < 0x3fff0000) {
2N/A i = 0l;
2N/A if (xm | x->l.frac2 | x->l.frac3 | x->l.frac4) {
2N/A /* signal inexact */
2N/A if (fsr & FSR_NXM) {
2N/A __quad_fqtox(x, (long *)&i);
2N/A } else {
2N/A fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
2N/A __quad_setfsrp(&fsr);
2N/A }
2N/A }
2N/A return (i);
2N/A }
2N/A
2N/A /* now x is in the range of long */
2N/A i = 0x4000000000000000l | ((long) (xm & 0xffff) << 46) |
2N/A ((long) x->l.frac2 << 14) | (x->l.frac3 >> 18);
2N/A round = i & ((1l << (0x403d - (xm >> 16))) - 1);
2N/A i >>= (0x403d - (xm >> 16));
2N/A if (x->l.msw & 0x80000000)
2N/A i = -i;
2N/A if (round | (x->l.frac3 & 0x3ffff) | x->l.frac4) {
2N/A /* signal inexact */
2N/A if (fsr & FSR_NXM) {
2N/A __quad_fqtox(x, (long *)&i);
2N/A } else {
2N/A fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
2N/A __quad_setfsrp(&fsr);
2N/A }
2N/A }
2N/A return (i);
2N/A}