/*
* 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 # into a 64-bit unsigned int.
*
* For compatibility with Sun's other conversion routines, pretend result
* is signed if input is negative.
*/
unsigned 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) {
/* abs(x) < 1.0, so round to 0 */
return ((unsigned long long)0);
} else if (exp > 63) {
/*
* abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
* overflow, Inf, NaN set fp_invalid exception
*/
(void) _Q_set_exception(_fp_current_exceptions);
if (i0 < 0)
return ((unsigned long long)LLONG_MIN);
else
return (ULLONG_MAX); /* MAXLONG */
}
/* Extract the mantissa. */
/*
* The most significant bit of the mantissa is now in bit 63 of m0:m1.
* Shift right by (63 - exp) bits.
*/
switch (exp) {
case 63:
break;
case 31:
m0 = 0;
break;
default:
if (exp > 31) {
} else {
m0 = 0;
}
break;
}
if (i0 < 0) {
if ((int)m0 < 0) { /* x < MINLLONG; return MINLLONG */
m0 = 0x80000000;
m1 = 0;
} else {
if (++m1 == 0)
m0++;
}
}
(void) _Q_set_exception(_fp_current_exceptions);
}
/*
* Convert a floating point number into a 64-bit unsigned int.
*
* For compatibility with Sun's other conversion routines, pretend result
* is signed if input is negative.
*/
unsigned long long
{
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) {
/* abs(x) < 1.0, so round to 0 */
return ((unsigned long long)0);
} else if (exp > 63) {
/*
* abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
* overflow, Inf, NaN set fp_invalid exception
*/
(void) _Q_set_exception(_fp_current_exceptions);
if (i0 < 0)
return ((unsigned long long)LLONG_MIN);
else
return (ULLONG_MAX); /* MAXLONG */
}
/* Extract the mantissa. */
m1 = 0;
/*
* The most significant bit of the mantissa is now in bit 63 of m0:m1.
* Shift right by (63 - exp) bits.
*/
switch (exp) {
case 63:
break;
case 31:
m0 = 0;
break;
default:
if (exp > 31) {
} else {
m0 = 0;
}
break;
}
if (i0 < 0) {
if ((int)m0 < 0) { /* x < MINLLONG; return MINLLONG */
m0 = 0x80000000;
m1 = 0;
} else {
if (++m1 == 0)
m0++;
}
}
(void) _Q_set_exception(_fp_current_exceptions);
}
/*
* Convert an extended precision floating point # into a 64-bit unsigned int.
*
* For compatibility with Sun's other conversion routines, pretend result
* is signed if input is negative.
*/
unsigned 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 > 63) {
/*
* abs(x) > MAXLLONG; return {MIN,MAX}ULLONG and as
* overflow, Inf, NaN set fp_invalid exception
*/
(void) _Q_set_exception(_fp_current_exceptions);
if (i0 < 0)
return ((unsigned long long)LLONG_MIN);
else
return (ULLONG_MAX); /* MAXLONG */
}
/* Extract the mantissa. */
/*
* The most significant bit of the mantissa is now in bit 63 of m0:m1.
* Shift right by (63 - exp) bits.
*/
switch (exp) {
case 63:
break;
case 31:
m0 = 0;
break;
default:
if (exp > 31) {
} else {
m0 = 0;
}
break;
}
if (i0 < 0) {
if ((int)m0 < 0) { /* x < MINLLONG; return MINLLONG */
m0 = 0x80000000;
m1 = 0;
} else {
if (++m1 == 0)
m0++;
}
}
(void) _Q_set_exception(_fp_current_exceptions);
}