2010-12-27 09:07:27 +00:00
|
|
|
#include <precomp.h>
|
|
|
|
|
|
|
|
#define I10_OUTPUT_MAX_PREC 21
|
|
|
|
/* Internal structure used by $I10_OUTPUT */
|
|
|
|
struct _I10_OUTPUT_DATA {
|
|
|
|
short pos;
|
|
|
|
char sign;
|
|
|
|
BYTE len;
|
|
|
|
char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* $I10_OUTPUT (MSVCRT.@)
|
2014-10-13 10:31:36 +00:00
|
|
|
* ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
|
2010-12-27 09:07:27 +00:00
|
|
|
* prec - precision of part, we're interested in
|
|
|
|
* flag - 0 for first prec digits, 1 for fractional part
|
|
|
|
* data - data to be populated
|
|
|
|
*
|
|
|
|
* return value
|
|
|
|
* 0 if given double is NaN or INF
|
|
|
|
* 1 otherwise
|
|
|
|
*
|
|
|
|
* FIXME
|
|
|
|
* 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'.
|
|
|
|
*/
|
2014-10-13 10:31:36 +00:00
|
|
|
int CDECL MSVCRT_I10_OUTPUT(_LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
|
2010-12-27 09:07:27 +00:00
|
|
|
{
|
|
|
|
static const char inf_str[] = "1#INF";
|
|
|
|
static const char nan_str[] = "1#QNAN";
|
|
|
|
|
2014-10-13 10:31:36 +00:00
|
|
|
/* 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;
|
|
|
|
double d;
|
2010-12-27 09:07:27 +00:00
|
|
|
char format[8];
|
|
|
|
char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
|
|
|
|
char *p;
|
|
|
|
|
2014-10-13 10:31:36 +00:00
|
|
|
memcpy(&ld, &ld80, 10);
|
|
|
|
d = ld;
|
2010-12-27 09:07:27 +00:00
|
|
|
TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
|
|
|
|
|
|
|
|
if(d<0) {
|
|
|
|
data->sign = '-';
|
|
|
|
d = -d;
|
|
|
|
} else
|
|
|
|
data->sign = ' ';
|
|
|
|
|
2011-01-26 16:28:34 +00:00
|
|
|
if(!_finite(d)) {
|
2010-12-27 09:07:27 +00:00
|
|
|
data->pos = 1;
|
|
|
|
data->len = 5;
|
|
|
|
memcpy(data->str, inf_str, sizeof(inf_str));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-26 16:28:34 +00:00
|
|
|
if(_isnan(d)) {
|
2010-12-27 09:07:27 +00:00
|
|
|
data->pos = 1;
|
|
|
|
data->len = 6;
|
|
|
|
memcpy(data->str, nan_str, sizeof(nan_str));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flag&1) {
|
2014-10-13 10:31:36 +00:00
|
|
|
int exp = 1+floor(log10(d));
|
2010-12-27 09:07:27 +00:00
|
|
|
|
|
|
|
prec += exp;
|
|
|
|
if(exp < 0)
|
|
|
|
prec--;
|
|
|
|
}
|
|
|
|
prec--;
|
|
|
|
|
|
|
|
if(prec+1 > I10_OUTPUT_MAX_PREC)
|
|
|
|
prec = I10_OUTPUT_MAX_PREC-1;
|
|
|
|
else if(prec < 0) {
|
|
|
|
d = 0.0;
|
|
|
|
prec = 0;
|
|
|
|
}
|
|
|
|
|
2019-04-28 16:27:22 +00:00
|
|
|
sprintf_s(format, sizeof(format), "%%.%dle", prec);
|
|
|
|
sprintf_s(buf, sizeof(buf), format, d);
|
2010-12-27 09:07:27 +00:00
|
|
|
|
|
|
|
buf[1] = buf[0];
|
|
|
|
data->pos = atoi(buf+prec+3);
|
|
|
|
if(buf[1] != '0')
|
|
|
|
data->pos++;
|
|
|
|
|
|
|
|
for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
|
2014-10-13 10:31:36 +00:00
|
|
|
data->len = p-buf;
|
2010-12-27 09:07:27 +00:00
|
|
|
|
|
|
|
memcpy(data->str, buf+1, data->len);
|
|
|
|
data->str[data->len] = '\0';
|
|
|
|
|
|
|
|
if(buf[1]!='0' && prec-data->len+1>0)
|
|
|
|
memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#undef I10_OUTPUT_MAX_PREC
|
|
|
|
|
|
|
|
|
|
|
|
|