소스 검색

better implementation for `floating-point bytes'

Roberto Ierusalimschy 21 년 전
부모
커밋
737ec947d3
4개의 변경된 파일20개의 추가작업 그리고 11개의 파일을 삭제
  1. 14 5
      lobject.c
  2. 2 2
      lobject.h
  3. 2 2
      ltests.c
  4. 2 2
      lvm.c

+ 14 - 5
lobject.c

@@ -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);
 }
 
 

+ 2 - 2
lobject.h

@@ -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);

+ 2 - 2
ltests.c

@@ -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;
 }
 

+ 2 - 2
lvm.c

@@ -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);  /***/