Skip to content

Commit

Permalink
Use internal implementation for converting FP numbers to strings.
Browse files Browse the repository at this point in the history
Contributed by Peter Cawley.
  • Loading branch information
Mike Pall committed Feb 26, 2016
1 parent 339a1fd commit 18f6aa9
Show file tree
Hide file tree
Showing 11 changed files with 627 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ LJLIB_C= $(LJLIB_O:.o=.c)
LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \
lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \
lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \
lj_strfmt.o lj_api.o lj_profile.o \
lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \
lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \
lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \
lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_crecord.h lj_strfmt.h
lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_strfmt.h lj_ctype.h \
lj_ccallback.h
lj_ccallback.h lj_buf.h
lj_debug.o: lj_debug.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_err.h lj_errmsg.h lj_debug.h lj_buf.h lj_gc.h lj_str.h lj_tab.h \
lj_state.h lj_frame.h lj_bc.h lj_strfmt.h lj_jit.h lj_ir.h
Expand Down Expand Up @@ -188,6 +188,8 @@ lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_err.h lj_errmsg.h lj_str.h lj_char.h
lj_strfmt.o: lj_strfmt.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_buf.h lj_gc.h lj_str.h lj_state.h lj_char.h lj_strfmt.h
lj_strfmt_num.o: lj_strfmt_num.c lj_obj.h lua.h luaconf.h lj_def.h \
lj_arch.h lj_buf.h lj_gc.h lj_str.h lj_strfmt.h
lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_char.h lj_strscan.h
lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
Expand Down
3 changes: 1 addition & 2 deletions src/lib_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,10 @@ LJLIB_CF(print)
shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring);
for (i = 0; i < nargs; i++) {
cTValue *o = &L->base[i];
char buf[STRFMT_MAXBUF_NUM];
const char *str;
size_t size;
MSize len;
if (shortcut && (str = lj_strfmt_wstrnum(buf, o, &len)) != NULL) {
if (shortcut && (str = lj_strfmt_wstrnum(L, o, &len)) != NULL) {
size = len;
} else {
copyTV(L, L->top+1, o);
Expand Down
3 changes: 1 addition & 2 deletions src/lib_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,8 @@ static int io_file_write(lua_State *L, FILE *fp, int start)
cTValue *tv;
int status = 1;
for (tv = L->base+start; tv < L->top; tv++) {
char buf[STRFMT_MAXBUF_NUM];
MSize len;
const char *p = lj_strfmt_wstrnum(buf, tv, &len);
const char *p = lj_strfmt_wstrnum(L, tv, &len);
if (!p)
lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING);
status = status && (fwrite(p, 1, len, fp) == len);
Expand Down
2 changes: 1 addition & 1 deletion src/lj_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep, int32_t i, int32_t e)
} else if (tvisint(o)) {
p = lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT+seplen), intV(o));
} else if (tvisnum(o)) {
p = lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM+seplen), o);
p = lj_buf_more(lj_strfmt_putfnum(sb, STRFMT_G14, numV(o)), seplen);
} else {
goto badtype;
}
Expand Down
14 changes: 7 additions & 7 deletions src/lj_ctype.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "lj_strfmt.h"
#include "lj_ctype.h"
#include "lj_ccallback.h"
#include "lj_buf.h"

/* -- C type definitions -------------------------------------------------- */

Expand Down Expand Up @@ -571,19 +572,18 @@ GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned)
/* Convert complex to string with 'i' or 'I' suffix. */
GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size)
{
char buf[2*STRFMT_MAXBUF_NUM+2+1], *p = buf;
SBuf *sb = lj_buf_tmp_(L);
TValue re, im;
if (size == 2*sizeof(double)) {
re.n = *(double *)sp; im.n = ((double *)sp)[1];
} else {
re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1];
}
p = lj_strfmt_wnum(p, &re);
if (!(im.u32.hi & 0x80000000u) || im.n != im.n) *p++ = '+';
p = lj_strfmt_wnum(p, &im);
*p = *(p-1) >= 'a' ? 'I' : 'i';
p++;
return lj_str_new(L, buf, p-buf);
lj_strfmt_putfnum(sb, STRFMT_G14, re.n);
if (!(im.u32.hi & 0x80000000u) || im.n != im.n) lj_buf_putchar(sb, '+');
lj_strfmt_putfnum(sb, STRFMT_G14, im.n);
lj_buf_putchar(sb, sbufP(sb)[-1] >= 'a' ? 'I' : 'i');
return lj_buf_str(L, sb);
}

/* -- C type state -------------------------------------------------------- */
Expand Down
14 changes: 7 additions & 7 deletions src/lj_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,25 @@ TValue *lj_meta_cat(lua_State *L, TValue *top, int left)
*/
TValue *e, *o = top;
uint64_t tlen = tvisstr(o) ? strV(o)->len : STRFMT_MAXBUF_NUM;
char *p, *buf;
SBuf *sb;
do {
o--; tlen += tvisstr(o) ? strV(o)->len : STRFMT_MAXBUF_NUM;
} while (--left > 0 && (tvisstr(o-1) || tvisnumber(o-1)));
if (tlen >= LJ_MAX_STR) lj_err_msg(L, LJ_ERR_STROV);
p = buf = lj_buf_tmp(L, (MSize)tlen);
sb = lj_buf_tmp_(L);
lj_buf_more(sb, (MSize)tlen);
for (e = top, top = o; o <= e; o++) {
if (tvisstr(o)) {
GCstr *s = strV(o);
MSize len = s->len;
p = lj_buf_wmem(p, strdata(s), len);
lj_buf_putmem(sb, strdata(s), len);
} else if (tvisint(o)) {
p = lj_strfmt_wint(p, intV(o));
lj_strfmt_putint(sb, intV(o));
} else {
lua_assert(tvisnum(o));
p = lj_strfmt_wnum(p, o);
lj_strfmt_putfnum(sb, STRFMT_G14, numV(o));
}
}
setstrV(L, top, lj_str_new(L, buf, (size_t)(p-buf)));
setstrV(L, top, lj_buf_str(L, sb));
}
} while (left >= 1);
if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) {
Expand Down
108 changes: 11 additions & 97 deletions src/lj_strfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/* -- Format parser ------------------------------------------------------- */

static const uint8_t strfmt_map[('x'-'A')+1] = {
STRFMT_A,0,0,0,STRFMT_E,0,STRFMT_G,0,0,0,0,0,0,
STRFMT_A,0,0,0,STRFMT_E,STRFMT_F,STRFMT_G,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,STRFMT_X,0,0,
0,0,0,0,0,0,
STRFMT_A,0,STRFMT_C,STRFMT_D,STRFMT_E,STRFMT_F,STRFMT_G,0,STRFMT_I,0,0,0,0,
Expand Down Expand Up @@ -89,24 +89,6 @@ SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs)

/* -- Raw conversions ----------------------------------------------------- */

/* Write number to bufer. */
char * LJ_FASTCALL lj_strfmt_wnum(char *p, cTValue *o)
{
if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */
#if __BIONIC__
if (tvismzero(o)) { *p++ = '-'; *p++ = '0'; return p; }
#endif
return p + lua_number2str(p, o->n);
} else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) {
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
} else if ((o->u32.hi & 0x80000000) == 0) {
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
} else {
*p++ = '-'; *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
}
return p;
}

#define WINT_R(x, sh, sc) \
{ uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); }

Expand Down Expand Up @@ -168,21 +150,22 @@ char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v)
return p;
}

/* Return string or write number to buffer and return pointer to start. */
const char *lj_strfmt_wstrnum(char *buf, cTValue *o, MSize *lenp)
/* Return string or write number to tmp buffer and return pointer to start. */
const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp)
{
SBuf *sb;
if (tvisstr(o)) {
*lenp = strV(o)->len;
return strVdata(o);
} else if (tvisint(o)) {
*lenp = (MSize)(lj_strfmt_wint(buf, intV(o)) - buf);
return buf;
sb = lj_strfmt_putint(lj_buf_tmp_(L), intV(o));
} else if (tvisnum(o)) {
*lenp = (MSize)(lj_strfmt_wnum(buf, o) - buf);
return buf;
sb = lj_strfmt_putfnum(lj_buf_tmp_(L), STRFMT_G14, o->n);
} else {
return NULL;
}
*lenp = sbuflen(sb);
return sbufB(sb);
}

/* -- Unformatted conversions to buffer ----------------------------------- */
Expand All @@ -198,8 +181,7 @@ SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k)
/* Add number to buffer. */
SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o)
{
setsbufP(sb, lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM), o));
return sb;
return lj_strfmt_putfnum(sb, STRFMT_G14, o->n);
}
#endif

Expand Down Expand Up @@ -360,63 +342,6 @@ SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n)
return lj_strfmt_putfxint(sb, sf, (uint64_t)k);
}

/* Max. sprintf buffer size needed. At least #string.format("%.99f", -1e308). */
#define STRFMT_FMTNUMBUF 512

/* Add formatted floating-point number to buffer. */
SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat sf, lua_Number n)
{
TValue tv;
tv.n = n;
if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {
/* Canonicalize output of non-finite values. */
MSize width = STRFMT_WIDTH(sf), len = 3;
int prefix = 0, ch = (sf & STRFMT_F_UPPER) ? 0x202020 : 0;
char *p;
if (((tv.u32.hi & 0x000fffff) | tv.u32.lo) != 0) {
ch ^= ('n' << 16) | ('a' << 8) | 'n';
if ((sf & STRFMT_F_SPACE)) prefix = ' ';
} else {
ch ^= ('i' << 16) | ('n' << 8) | 'f';
if ((tv.u32.hi & 0x80000000)) prefix = '-';
else if ((sf & STRFMT_F_PLUS)) prefix = '+';
else if ((sf & STRFMT_F_SPACE)) prefix = ' ';
}
if (prefix) len = 4;
p = lj_buf_more(sb, width > len ? width : len);
if (!(sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' ';
if (prefix) *p++ = prefix;
*p++ = (char)(ch >> 16); *p++ = (char)(ch >> 8); *p++ = (char)ch;
if ((sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' ';
setsbufP(sb, p);
} else { /* Delegate to sprintf() for now. */
uint8_t width = (uint8_t)STRFMT_WIDTH(sf), prec = (uint8_t)STRFMT_PREC(sf);
char fmt[1+5+2+3+1+1], *p = fmt;
*p++ = '%';
if ((sf & STRFMT_F_LEFT)) *p++ = '-';
if ((sf & STRFMT_F_PLUS)) *p++ = '+';
if ((sf & STRFMT_F_ZERO)) *p++ = '0';
if ((sf & STRFMT_F_SPACE)) *p++ = ' ';
if ((sf & STRFMT_F_ALT)) *p++ = '#';
if (width) {
uint8_t x = width / 10, y = width % 10;
if (x) *p++ = '0' + x;
*p++ = '0' + y;
}
if (prec != 255) {
uint8_t x = prec / 10, y = prec % 10;
*p++ = '.';
if (x) *p++ = '0' + x;
*p++ = '0' + y;
}
*p++ = (0x67666561 >> (STRFMT_FP(sf)<<3)) ^ ((sf & STRFMT_F_UPPER)?0x20:0);
*p = '\0';
p = lj_buf_more(sb, STRFMT_FMTNUMBUF);
setsbufP(sb, p + sprintf(p, fmt, n));
}
return sb;
}

/* -- Conversions to strings ---------------------------------------------- */

/* Convert integer to string. */
Expand All @@ -427,14 +352,6 @@ GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k)
return lj_str_new(L, buf, len);
}

/* Convert number to string. */
GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o)
{
char buf[STRFMT_MAXBUF_NUM];
MSize len = (MSize)(lj_strfmt_wnum(buf, o) - buf);
return lj_str_new(L, buf, len);
}

/* Convert integer or number to string. */
GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o)
{
Expand Down Expand Up @@ -510,12 +427,9 @@ const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp)
case STRFMT_UINT:
lj_strfmt_putfxint(sb, sf, va_arg(argp, uint32_t));
break;
case STRFMT_NUM: {
TValue tv;
tv.n = va_arg(argp, lua_Number);
setsbufP(sb, lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM), &tv));
case STRFMT_NUM:
lj_strfmt_putfnum(sb, STRFMT_G14, va_arg(argp, lua_Number));
break;
}
case STRFMT_STR: {
const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)";
Expand Down
6 changes: 3 additions & 3 deletions src/lj_strfmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ typedef enum FormatType {
#define STRFMT_S (STRFMT_STR)
#define STRFMT_U (STRFMT_UINT)
#define STRFMT_X (STRFMT_UINT|STRFMT_T_HEX)
#define STRFMT_G14 (STRFMT_G | ((14+1) << STRFMT_SH_PREC))

/* Maximum buffer sizes for conversions. */
#define STRFMT_MAXBUF_XINT (1+22) /* '0' prefix + uint64_t in octal. */
#define STRFMT_MAXBUF_INT (1+10) /* Sign + int32_t in decimal. */
#define STRFMT_MAXBUF_NUM LUAI_MAXNUMBER2STR
#define STRFMT_MAXBUF_NUM 32 /* Must correspond with STRFMT_G14. */
#define STRFMT_MAXBUF_PTR (2+2*sizeof(ptrdiff_t)) /* "0x" + hex ptr. */

/* Format parser. */
Expand All @@ -83,10 +84,9 @@ static LJ_AINLINE void lj_strfmt_init(FormatState *fs, const char *p, MSize len)

/* Raw conversions. */
LJ_FUNC char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k);
LJ_FUNC char * LJ_FASTCALL lj_strfmt_wnum(char *p, cTValue *o);
LJ_FUNC char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v);
LJ_FUNC char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v);
LJ_FUNC const char *lj_strfmt_wstrnum(char *buf, cTValue *o, MSize *lenp);
LJ_FUNC const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp);

/* Unformatted conversions to buffer. */
LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k);
Expand Down
Loading

0 comments on commit 18f6aa9

Please sign in to comment.