From c0efa6f00ed757bc05d3a0874f98c91fcb88dc68 Mon Sep 17 00:00:00 2001
From: Mike Pall <mike>
Date: Sun, 26 Aug 2012 19:41:35 +0200
Subject: Replace divisions with simpler code.

---
 src/lib_string.c | 23 +++++++++++++----------
 src/lj_bcwrite.c |  2 +-
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/lib_string.c b/src/lib_string.c
index 89ec4f25..d4a2b0d1 100644
--- a/src/lib_string.c
+++ b/src/lib_string.c
@@ -668,19 +668,22 @@ static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
   const char *s = strdata(str);
   luaL_addchar(b, '"');
   while (len--) {
-    if (*s == '"' || *s == '\\' || *s == '\n') {
+    uint32_t c = uchar(*s);
+    if (c == '"' || c == '\\' || c == '\n') {
       luaL_addchar(b, '\\');
-      luaL_addchar(b, *s);
-    } else if (lj_char_iscntrl(uchar(*s))) {
-      uint32_t c1, c2, c3;
+    } else if (lj_char_iscntrl(c)) {  /* This can only be 0-31 or 127. */
+      uint32_t d;
       luaL_addchar(b, '\\');
-      c1 = uchar(*s); c3 = c1 % 10; c1 /= 10; c2 = c1 % 10; c1 /= 10;
-      if (c1 + lj_char_isdigit(uchar(s[1]))) luaL_addchar(b, '0' + c1);
-      if (c2 + (c1 + lj_char_isdigit(uchar(s[1])))) luaL_addchar(b, '0' + c2);
-      luaL_addchar(b, '0' + c3);
-    } else {
-      luaL_addchar(b, *s);
+      if (c >= 100 || lj_char_isdigit(uchar(s[1]))) {
+	luaL_addchar(b, '0'+(c >= 100)); if (c >= 100) c -= 100;
+	goto tens;
+      } else if (c >= 10) {
+      tens:
+	d = (c * 205) >> 11; c -= d * 10; luaL_addchar(b, '0'+d);
+      }
+      c += '0';
     }
+    luaL_addchar(b, c);
     s++;
   }
   luaL_addchar(b, '"');
diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c
index 27f08fe8..5050da07 100644
--- a/src/lj_bcwrite.c
+++ b/src/lj_bcwrite.c
@@ -324,7 +324,7 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
   /* Pass buffer to writer function. */
   if (ctx->status == 0) {
     MSize n = ctx->sb.n - 5;
-    MSize nn = 1 + lj_fls(n)/7;
+    MSize nn = (lj_fls(n)+8)*9 >> 6;
     ctx->sb.n = 5 - nn;
     bcwrite_uleb128(ctx, n);  /* Fill in final size. */
     lua_assert(ctx->sb.n == 5);
-- 
cgit v1.2.3-55-g6feb