#include "lint.h"
#include <sys/isa_defs.h>
#include <floatingpoint.h>
#include <limits.h>
#include "libc.h"
#if !defined(_BIG_ENDIAN) || defined(_LITTLE_ENDIAN)
#error "big-endian only!"
#endif
long long
__dtoll(double dval)
{
int i0, i1;
int exp;
int m0;
unsigned int m1;
unsigned int _fp_current_exceptions = 0;
union {
int i[2];
double d;
} u;
u.d = dval;
i0 = u.i[0];
i1 = u.i[1];
exp = ((i0 >> 20) & 0x7ff) - 0x3ff;
if (exp < 0) {
return ((long long)0);
} else if (exp > 62) {
if (i0 >= 0 || exp != 63 || (i0 & 0xfffff) != 0 || i1 != 0) {
_fp_current_exceptions |= (1 << (int)fp_invalid);
(void) _Q_set_exception(_fp_current_exceptions);
}
if (i0 < 0)
return (LLONG_MIN);
else
return (LLONG_MAX);
}
m0 = 0x40000000 | ((i0 << 10) & 0x3ffffc00) | ((i1 >> 22) & 0x3ff);
m1 = i1 << 10;
switch (exp) {
case 62:
break;
case 30:
m1 = m0;
m0 = 0;
break;
default:
if (exp > 30) {
m1 = (m0 << (exp - 30)) |
(m1 >> (62 - exp)) & ~(UINT_MAX << (exp - 30));
m0 >>= 62 - exp;
} else {
m1 = m0 >> (30 - exp);
m0 = 0;
}
break;
}
if (i0 < 0) {
m0 = ~m0;
m1 = ~m1;
if (++m1 == 0)
m0++;
}
(void) _Q_set_exception(_fp_current_exceptions);
return ((long long)(((unsigned long long)m0 << 32) | m1));
}
long long
__ftoll(float fval)
{
int i0;
int exp;
int m0;
unsigned int m1;
unsigned int _fp_current_exceptions = 0;
union {
int i;
float f;
} u;
u.f = fval;
i0 = u.i;
exp = ((i0 >> 23) & 0xff) - 0x7f;
if (exp < 0) {
return ((long long) 0);
} else if (exp > 62) {
if (i0 >= 0 || exp != 63 || (i0 & 0x7fffff) != 0) {
_fp_current_exceptions |= (1 << (int)fp_invalid);
(void) _Q_set_exception(_fp_current_exceptions);
}
if (i0 < 0)
return (LLONG_MIN);
else
return (LLONG_MAX);
}
m0 = 0x40000000 | (i0 << 7) & 0x3fffff80;
m1 = 0;
switch (exp) {
case 62:
break;
case 30:
m1 = m0;
m0 = 0;
break;
default:
if (exp > 30) {
m1 = m0 << (exp - 30);
m0 >>= 62 - exp;
} else {
m1 = m0 >> (30 - exp);
m0 = 0;
}
break;
}
if (i0 < 0) {
m0 = ~m0;
m1 = ~m1;
if (++m1 == 0)
m0++;
}
(void) _Q_set_exception(_fp_current_exceptions);
return ((long long)(((unsigned long long)m0 << 32) | m1));
}
long long
_Q_qtoll(long double longdbl)
{
int i0;
unsigned int i1, i2;
int *plngdbl = (int *)&longdbl;
int exp;
int m0;
unsigned int m1;
unsigned int _fp_current_exceptions = 0;
i0 = plngdbl[0];
i1 = plngdbl[1];
i2 = plngdbl[2];
exp = ((i0 >> 16) & 0x7fff) - 0x3fff;
if (exp < 0) {
return ((long long)0);
} else if (exp > 62) {
if (i0 >= 0 || exp != 63 || (i0 & 0xffff) != 0 || i1 != 0 ||
(i2 & 0xfffe0000) != 0) {
_fp_current_exceptions |= (1 << (int)fp_invalid);
(void) _Q_set_exception(_fp_current_exceptions);
}
if (i0 < 0)
return (LLONG_MIN);
else
return (LLONG_MAX);
}
m0 = 0x40000000 | ((i0 << 14) & 0x3fffc000) | ((i1 >> 18) & 0x3fff);
m1 = (i1 << 14) | ((i2 >> 18) & 0x3fff);
switch (exp) {
case 62:
break;
case 30:
m1 = m0;
m0 = 0;
break;
default:
if (exp > 30) {
m1 = (m0 << (exp - 30)) |
(m1 >> (62 - exp)) & ~(UINT_MAX << (exp - 30));
m0 >>= 62 - exp;
} else {
m1 = m0 >> (30 - exp);
m0 = 0;
}
break;
}
if (i0 < 0) {
m0 = ~m0;
m1 = ~m1;
if (++m1 == 0)
m0++;
}
(void) _Q_set_exception(_fp_current_exceptions);
return ((long long)(((unsigned long long)m0 << 32) | m1));
}