better implementation for `floating-point bytes'

This commit is contained in:
Roberto Ierusalimschy 2004-11-01 12:06:50 -03:00
parent 45d566f676
commit 737ec947d3
4 changed files with 20 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.4 2004/07/09 16:01:38 roberto Exp roberto $
** $Id: lobject.c,v 2.5 2004/10/06 18:34:16 roberto Exp $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -31,12 +31,21 @@ const TValue luaO_nilobject = {{NULL}, LUA_TNIL};
** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm)
*/
int luaO_int2fb (unsigned int x) {
int m = 0; /* mantissa */
while (x >= (1<<3)) {
int e = 0; /* expoent */
while (x >= 16) {
x = (x+1) >> 1;
m++;
e++;
}
return (m << 3) | cast(int, x);
if (x < 8) return x;
else return ((e+1) << 3) | (cast(int, x) - 8);
}
/* converts back */
int luaO_fb2int (int x) {
int e = (x >> 3) & 31;
if (e == 0) return x;
else return ((x & 7)+8) << (e - 1);
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 2.5 2004/05/31 18:51:50 roberto Exp roberto $
** $Id: lobject.h,v 2.6 2004/10/06 18:34:16 roberto Exp $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -352,7 +352,7 @@ extern const TValue luaO_nilobject;
int luaO_log2 (unsigned int x);
int luaO_int2fb (unsigned int x);
#define fb2int(x) (((x) & 7) << ((x) >> 3))
int luaO_fb2int (int x);
int luaO_rawequalObj (const TValue *t1, const TValue *t2);
int luaO_str2d (const char *s, lua_Number *result);

View File

@ -1,5 +1,5 @@
/*
** $Id: ltests.c,v 2.13 2004/09/29 21:00:25 roberto Exp roberto $
** $Id: ltests.c,v 2.14 2004/10/06 18:34:16 roberto Exp $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
@ -759,7 +759,7 @@ static int log2_aux (lua_State *L) {
static int int2fb_aux (lua_State *L) {
int b = luaO_int2fb(luaL_checkint(L, 1));
lua_pushinteger(L, b);
lua_pushinteger(L, fb2int(b));
lua_pushinteger(L, luaO_fb2int(b));
return 2;
}

4
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.15 2004/10/04 19:01:53 roberto Exp roberto $
** $Id: lvm.c,v 2.16 2004/10/28 17:45:51 roberto Exp $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -461,7 +461,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
}
case OP_NEWTABLE: {
int b = GETARG_B(i);
b = fb2int(b);
b = luaO_fb2int(b);
sethvalue(L, ra, luaH_new(L, b, GETARG_C(i)));
L->ci->savedpc = pc;
luaC_checkGC(L); /***/