[CRT] Sync $I10_OUTPUT and test with wine head

Both were broken on MSVC builds, where a long double is 64 bits and not 80 bits like on GCC. The new code works on MSVC builds, too.
This commit is contained in:
Timo Kreuzer 2023-12-20 12:06:05 +02:00
parent 40131fd1ca
commit 86b82e4ce7
5 changed files with 206 additions and 79 deletions

View file

@ -1,5 +1,140 @@
#include <precomp.h>
// From Wine msvcrt.h
typedef struct {ULONG x80[3];} MSVCRT__LDOUBLE; /* Intel 80 bit FP format has sizeof() 12 */
enum fpmod {
FP_ROUND_ZERO, /* only used when dropped part contains only zeros */
FP_ROUND_DOWN,
FP_ROUND_EVEN,
FP_ROUND_UP,
FP_VAL_INFINITY,
FP_VAL_NAN
};
struct fpnum {
int sign;
int exp;
ULONGLONG m;
enum fpmod mod;
};
// From wine bnum.h
#define EXP_BITS 11
#define MANT_BITS 53
int fpnum_double(struct fpnum *fp, double *d)
{
ULONGLONG bits = 0;
if (fp->mod == FP_VAL_INFINITY)
{
*d = fp->sign * INFINITY;
return 0;
}
if (fp->mod == FP_VAL_NAN)
{
bits = ~0;
if (fp->sign == 1)
bits &= ~((ULONGLONG)1 << (MANT_BITS + EXP_BITS - 1));
*d = *(double*)&bits;
return 0;
}
TRACE("%c %#I64x *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
fp->m, fp->exp, fp->mod);
if (!fp->m)
{
*d = fp->sign * 0.0;
return 0;
}
/* make sure that we don't overflow modifying exponent */
if (fp->exp > 1<<EXP_BITS)
{
*d = fp->sign * INFINITY;
return ERANGE;
}
if (fp->exp < -(1<<EXP_BITS))
{
*d = fp->sign * 0.0;
return ERANGE;
}
fp->exp += MANT_BITS - 1;
/* normalize mantissa */
while(fp->m < (ULONGLONG)1 << (MANT_BITS-1))
{
fp->m <<= 1;
fp->exp--;
}
while(fp->m >= (ULONGLONG)1 << MANT_BITS)
{
if (fp->m & 1 || fp->mod != FP_ROUND_ZERO)
{
if (!(fp->m & 1)) fp->mod = FP_ROUND_DOWN;
else if(fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
else fp->mod = FP_ROUND_UP;
}
fp->m >>= 1;
fp->exp++;
}
fp->exp += (1 << (EXP_BITS-1)) - 1;
/* handle subnormals */
if (fp->exp <= 0)
{
if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
else if (fp->m & 1) fp->mod = FP_ROUND_UP;
else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
fp->m >>= 1;
}
while(fp->m && fp->exp<0)
{
if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
else if (fp->m & 1) fp->mod = FP_ROUND_UP;
else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
fp->m >>= 1;
fp->exp++;
}
/* round mantissa */
if (fp->mod == FP_ROUND_UP || (fp->mod == FP_ROUND_EVEN && fp->m & 1))
{
fp->m++;
/* handle subnormal that falls into regular range due to rounding */
if (fp->m == (ULONGLONG)1 << (MANT_BITS - 1))
{
fp->exp++;
}
else if (fp->m >= (ULONGLONG)1 << MANT_BITS)
{
fp->exp++;
fp->m >>= 1;
}
}
if (fp->exp >= (1<<EXP_BITS)-1)
{
*d = fp->sign * INFINITY;
return ERANGE;
}
if (!fp->m || fp->exp < 0)
{
*d = fp->sign * 0.0;
return ERANGE;
}
if (fp->sign == -1)
bits |= (ULONGLONG)1 << (MANT_BITS + EXP_BITS - 1);
bits |= (ULONGLONG)fp->exp << (MANT_BITS - 1);
bits |= fp->m & (((ULONGLONG)1 << (MANT_BITS - 1)) - 1);
TRACE("returning %#I64x\n", bits);
*d = *(double*)&bits;
return 0;
}
#define I10_OUTPUT_MAX_PREC 21
/* Internal structure used by $I10_OUTPUT */
struct _I10_OUTPUT_DATA {
@ -24,22 +159,31 @@ struct _I10_OUTPUT_DATA {
* Native sets last byte of data->str to '0' or '9', I don't know what
* it means. Current implementation sets it always to '0'.
*/
int CDECL MSVCRT_I10_OUTPUT(_LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
int CDECL I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
{
static const char inf_str[] = "1#INF";
static const char nan_str[] = "1#QNAN";
/* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
* Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
* Assume long double uses 80 bit FP, never seen 128 bit FP. */
long double ld = 0;
struct fpnum num;
double d;
char format[8];
char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
char *p;
memcpy(&ld, &ld80, 10);
d = ld;
if ((ld80.x80[2] & 0x7fff) == 0x7fff)
{
if (ld80.x80[0] == 0 && ld80.x80[1] == 0x80000000)
strcpy( data->str, "1#INF" );
else
strcpy( data->str, (ld80.x80[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
data->pos = 1;
data->sign = (ld80.x80[2] & 0x8000) ? '-' : ' ';
data->len = (BYTE)strlen(data->str);
return 0;
}
num.sign = (ld80.x80[2] & 0x8000) ? -1 : 1;
num.exp = (ld80.x80[2] & 0x7fff) - 0x3fff - 63;
num.m = ld80.x80[0] | ((ULONGLONG)ld80.x80[1] << 32);
num.mod = FP_ROUND_EVEN;
fpnum_double( &num, &d );
TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
if(d<0) {
@ -48,24 +192,8 @@ int CDECL MSVCRT_I10_OUTPUT(_LDOUBLE ld80, int prec, int flag, struct _I10_OUTPU
} else
data->sign = ' ';
if(!_finite(d)) {
data->pos = 1;
data->len = 5;
memcpy(data->str, inf_str, sizeof(inf_str));
return 0;
}
if(_isnan(d)) {
data->pos = 1;
data->len = 6;
memcpy(data->str, nan_str, sizeof(nan_str));
return 0;
}
if(flag&1) {
int exp = 1+floor(log10(d));
int exp = 1 + floor(log10(d));
prec += exp;
if(exp < 0)
@ -100,6 +228,3 @@ int CDECL MSVCRT_I10_OUTPUT(_LDOUBLE ld80, int prec, int flag, struct _I10_OUTPU
return 1;
}
#undef I10_OUTPUT_MAX_PREC