[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,6 +1,6 @@
# msvcrt.dll - MS VC++ Run Time Library
@ cdecl -arch=x86_64 -version=0x502 $I10_OUTPUT() MSVCRT_I10_OUTPUT
@ cdecl -arch=x86_64 -version=0x502 $I10_OUTPUT() I10_OUTPUT
# **************** x86 C++ functions ****************
@ cdecl -arch=i386 -norelay ??0__non_rtti_object@@QAE@ABV0@@Z(ptr) MSVCRT___non_rtti_object_copy_ctor # public: __thiscall __non_rtti_object::__non_rtti_object(class __non_rtti_object const &)
@ -180,7 +180,7 @@
# **************** Common functions ****************
@ cdecl -arch=i386 $I10_OUTPUT() MSVCRT_I10_OUTPUT
@ cdecl -arch=i386 $I10_OUTPUT() I10_OUTPUT
@ cdecl -arch=i386 _CIacos()
@ cdecl -arch=i386 _CIasin()
@ cdecl -arch=i386 _CIatan()

View file

@ -438,7 +438,7 @@
@ stub ?xalloc@ios@@SAHXZ #
@ stub ?xsgetn@streambuf@@UAEHPADH@Z #
@ stub ?xsputn@streambuf@@UAEHPBDH@Z #
@ cdecl $I10_OUTPUT() MSVCRT_I10_OUTPUT
@ cdecl $I10_OUTPUT() I10_OUTPUT
@ cdecl -arch=i386 _CIacos()
@ cdecl -arch=i386 _CIasin()
@ cdecl -arch=i386 _CIatan()

View file

@ -482,7 +482,7 @@
@ stub ?xalloc@ios@@SAHXZ
@ stub ?xsgetn@streambuf@@UAEHPADH@Z
@ stub ?xsputn@streambuf@@UAEHPBDH@Z
@ cdecl $I10_OUTPUT() MSVCRT_I10_OUTPUT
@ cdecl $I10_OUTPUT() I10_OUTPUT
@ cdecl -arch=i386 _CIacos()
@ cdecl -arch=i386 _CIasin()
@ cdecl -arch=i386 _CIatan()

View file

@ -51,8 +51,11 @@ static inline BOOL almost_equal(double d1, double d2) {
return FALSE;
}
/* MS' "long double" is an 80 bit FP that takes 12 bytes*/
struct uld { ULONG lo, hi, exp; };
static int (__cdecl *prand_s)(unsigned int *);
static int (__cdecl *pI10_OUTPUT)(long double, int, int, void*);
static int (__cdecl *pI10_OUTPUT)(struct uld, int, int, void*);
static int (__cdecl *pstrerror_s)(char *, MSVCRT_size_t, int);
static int (__cdecl *p_get_doserrno)(int *);
static int (__cdecl *p_get_errno)(int *);
@ -115,7 +118,7 @@ typedef struct _I10_OUTPUT_data {
} I10_OUTPUT_data;
typedef struct _I10_OUTPUT_test {
long double d;
struct uld d;
int size;
int flags;
@ -126,63 +129,62 @@ typedef struct _I10_OUTPUT_test {
static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
/* arg3 = 0 */
{ 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
{ 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
{ -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
{ 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
{ 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
{ 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
{ 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
{ 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
{ 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
{ { 0x00000000, 0x00000000, 0x0000 /* 0.0 */ }, 10, 0, {0, ' ', 1, "0"}, 1, "" },
{ { 0x00000000, 0x80000000, 0x3fff /* 1.0 */ }, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
{ { 0x00000000, 0x80000000, 0xbfff /* -1.0 */ }, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
{ { 0x0a3d7000, 0x9d70a3d7, 0x3fff /* 1.23 */ }, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
{ { 0x00000000, 0x9184e72a, 0x402a /* 1e13 */ }, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
{ { 0x04675000, 0xc9f2c9cd, 0x4062 /* 1e30 */ }, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
{ { 0x4bb41000, 0xe12e1342, 0x3fd3 /* 1e-13 */ }, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
{ { 0x00000000, 0x80000000, 0x3ffd /* 0.25 */ }, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
{ { 0xbf94d800, 0x800000d6, 0x3fff /* 1.0000001 */ }, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
{ { 0x00000000, 0x80000000, 0x7fff /* +inf */ }, 10, 0, {1, ' ', 5, "1#INF"}, 0, "" },
{ { 0x00000000, 0x80000000, 0xffff /* -inf */ }, 10, 0, {1, '-', 5, "1#INF"}, 0, "" },
{ { 0x00000001, 0x80000000, 0x7fff /* snan */ }, 10, 0, {1, ' ', 6, "1#SNAN"}, 0, "" },
{ { 0x00000001, 0x80000000, 0xffff /* snan */ }, 10, 0, {1, '-', 6, "1#SNAN"}, 0, "" },
{ { 0x00000000, 0xc0000000, 0x7fff /* qnan */ }, 10, 0, {1, ' ', 6, "1#QNAN"}, 0, "" },
{ { 0x00000000, 0x40000000, 0xffff /* qnan */ }, 10, 0, {1, '-', 6, "1#QNAN"}, 0, "" },
/* arg3 = 1 */
{ 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
{ 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
{ -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
{ 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
{ 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
{ 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
{ 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
{ 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
{ 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
{ 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
{ { 0x00000000, 0x00000000, 0x0000 /* 0 */ }, 10, 1, {0, ' ', 1, "0"}, 1, "" },
{ { 0x00000000, 0x80000000, 0x3fff /* 1 */ }, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
{ { 0x00000000, 0x80000000, 0xbfff /* -1 */ }, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
{ { 0x0a3d7000, 0x9d70a3d7, 0x3fff /* 1.23 */ }, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
{ { 0x00000000, 0x9184e72a, 0x402a /* 1e13 */ }, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
{ { 0x04675000, 0xc9f2c9cd, 0x4062 /* 1e30 */ }, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
{ { 0x4bb41000, 0xe12e1342, 0x3fd3 /* 1e-13 */ }, 10, 1, {0, ' ', 1, "0"}, 1, "" },
{ { 0xe57a4000, 0xd6bf94d5, 0x3fe7 /* 1e-7 */ }, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
{ { 0x00000000, 0x80000000, 0x3ffd /* 0.25 */ }, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
{ { 0xbf94d800, 0x800000d6, 0x3fff /* 1.0000001 */ }, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
{ { 0x00000000, 0x80000000, 0x7fff /* +inf */ }, 10, 1, {1, ' ', 5, "1#INF"}, 0, "" },
{ { 0x00000000, 0x80000000, 0xffff /* -inf */ }, 10, 1, {1, '-', 5, "1#INF"}, 0, "" },
{ { 0x00000001, 0x80000000, 0x7fff /* snan */ }, 10, 1, {1, ' ', 6, "1#SNAN"}, 0, "" },
{ { 0x00000000, 0xc0000000, 0x7fff /* qnan */ }, 10, 1, {1, ' ', 6, "1#QNAN"}, 0, "" },
{ { 0x00000000, 0x40000000, 0x7fff /* qnan */ }, 10, 1, {1, ' ', 6, "1#QNAN"}, 0, "" },
/* too small buffer */
{ 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
{ 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
{ 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
{ 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
{ 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
{ 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
{ 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
{ 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
{ 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
{ { 0x00000000, 0x00000000, 0x0000 /* 0 */ }, 0, 0, {0, ' ', 1, "0"}, 1, "" },
{ { 0x00000000, 0x00000000, 0x0000 /* 0 */ }, 0, 1, {0, ' ', 1, "0"}, 1, "" },
{ { 0x00000000, 0xf6000000, 0x4005 /* 123 */ }, 2, 0, {3, ' ', 2, "12"}, 1, "" },
{ { 0x00000000, 0xf6000000, 0x4005 /* 123 */ }, 0, 0, {0, ' ', 1, "0"}, 1, "" },
{ { 0x00000000, 0xf6000000, 0x4005 /* 123 */ }, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
{ { 0x0a3d7000, 0xfd70a3d7, 0x3ffe /* 0.99 */ }, 1, 0, {1, ' ', 1, "1"}, 1, "" },
{ { 0x00000000, 0x9a5db800, 0x4013 /* 1264567.0 */ }, 2, 0, {7, ' ', 2, "13"}, 1, "" },
{ { 0x00000000, 0x9a5db800, 0x4013 /* 1264567.0 */ }, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
{ { 0x00000000, 0x932c05a6, 0x401d /* 1234567891.0 */ }, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
};
static void test_I10_OUTPUT(void)
{
I10_OUTPUT_data out;
int i, j = sizeof(long double), ret;
int i, j, ret;
if(!pI10_OUTPUT) {
win_skip("I10_OUTPUT not available\n");
return;
}
if (j != 12)
trace("sizeof(long double) = %d on this machine\n", j);
for(i=0; i<ARRAY_SIZE(I10_OUTPUT_tests); i++) {
memset(out.str, '#', sizeof(out.str));
if (sizeof(long double) == 12)
ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
else {
/* MS' "long double" is an 80 bit FP that takes 12 bytes*/
typedef struct { ULONG x80[3]; } uld; /* same calling convention */
union { long double ld; uld ld12; } fp80;
int (__cdecl *pI10_OUTPUT12)(uld, int, int, void*) = (void*)pI10_OUTPUT;
fp80.ld = I10_OUTPUT_tests[i].d;
ret = pI10_OUTPUT12(fp80.ld12, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
}
ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
@ -192,7 +194,7 @@ static void test_I10_OUTPUT(void)
j = strlen(I10_OUTPUT_tests[i].remain);
todo_wine_if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
"%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
"%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
if(out.str[j] != '#')

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