/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "lint.h"
#include <sys/isa_defs.h>
#include <floatingpoint.h>
#include <limits.h>
#include "libc.h"
/*
* Ensure that this "portable" code is only used on big-endian ISAs
*/
#if !defined(_BIG_ENDIAN) || defined(_LITTLE_ENDIAN)
#error "big-endian only!"
#endif
/*
* Convert a double precision floating point number into a 64-bit int.
*/
long long
{
unsigned int _fp_current_exceptions = 0;
union {
int i[2];
double d;
} u;
/*
* Extract the exponent and check boundary conditions.
* Notice that the exponent is equal to the bit number where
* we want the most significant bit to live.
*/
u.d = dval;
i0 = u.i[0];
i1 = u.i[1];
if (exp < 0) {
return ((long long)0); /* abs(x) < 1.0, so round to 0 */
} else if (exp > 62) {
/*
* fp_invalid NOT raised if <i0,i1> == LLONG_MIN
*/
/*
* abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
* overflow, Inf, NaN set fp_invalid exception
*/
(void) _Q_set_exception(_fp_current_exceptions);
}
if (i0 < 0)
return (LLONG_MIN);
else
return (LLONG_MAX); /* MAXLONG */
}
/* Extract the mantissa. */
/*
* The most significant bit of the mantissa is now in bit 62 of m0:m1.
* Shift right by (62 - exp) bits.
*/
switch (exp) {
case 62:
break;
case 30:
m0 = 0;
break;
default:
if (exp > 30) {
} else {
m0 = 0;
}
break;
}
if (i0 < 0) {
if (++m1 == 0)
m0++;
}
(void) _Q_set_exception(_fp_current_exceptions);
}
/*
* Convert a floating point number into a 64-bit int.
*/
long long
{
int i0;
unsigned int _fp_current_exceptions = 0;
union {
int i;
float f;
} u;
/*
* Extract the exponent and check boundary conditions.
* Notice that the exponent is equal to the bit number where
* we want the most significant bit to live.
*/
u.f = fval;
i0 = u.i;
if (exp < 0) {
return ((long long) 0); /* abs(x) < 1.0, so round to 0 */
} else if (exp > 62) {
/*
* fp_invalid NOT raised if <i0> == LLONG_MIN
*/
/*
* abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
* overflow, Inf, NaN set fp_invalid exception
*/
(void) _Q_set_exception(_fp_current_exceptions);
}
if (i0 < 0)
return (LLONG_MIN);
else
return (LLONG_MAX); /* MAXLONG */
}
/* Extract the mantissa. */
m1 = 0;
/*
* The most significant bit of the mantissa is now in bit 62 of m0:m1.
* Shift right by (62 - exp) bits.
*/
switch (exp) {
case 62:
break;
case 30:
m0 = 0;
break;
default:
if (exp > 30) {
} else {
m0 = 0;
}
break;
}
if (i0 < 0) {
if (++m1 == 0)
m0++;
}
(void) _Q_set_exception(_fp_current_exceptions);
}
/*
* Convert an extended precision floating point number into a 64-bit int.
*/
long long
{
int i0;
unsigned int _fp_current_exceptions = 0;
/*
* Only 96-bits of precision used
*/
/*
* Extract the exponent and check boundary conditions.
* Notice that the exponent is equal to the bit number where
* we want the most significant bit to live.
*/
if (exp < 0) {
return ((long long)0); /* abs(x) < 1.0, so round to 0 */
} else if (exp > 62) {
/*
* fp_invalid NOT raised if <i0,i1,i2,i3> when chopped to
* 64 bits == LLONG_MIN
*/
(i2 & 0xfffe0000) != 0) {
/*
* abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
* overflow, Inf, NaN set fp_invalid exception
*/
(void) _Q_set_exception(_fp_current_exceptions);
}
if (i0 < 0)
return (LLONG_MIN);
else
return (LLONG_MAX); /* MAXLONG */
}
/* Extract the mantissa. */
/*
* The most significant bit of the mantissa is now in bit 62 of m0:m1.
* Shift right by (62 - exp) bits.
*/
switch (exp) {
case 62:
break;
case 30:
m0 = 0;
break;
default:
if (exp > 30) {
} else {
m0 = 0;
}
break;
}
if (i0 < 0) {
if (++m1 == 0)
m0++;
}
(void) _Q_set_exception(_fp_current_exceptions);
}