浏览代码

* patch by Rika: improve EncodeUleb128/EncodeSleb128, second part of #39750, resolves #39750

florian 3 年之前
父节点
当前提交
927e3fd455
共有 1 个文件被更改,包括 5 次插入22 次删除
  1. 5 22
      compiler/cutils.pas

+ 5 - 22
compiler/cutils.pas

@@ -1596,34 +1596,17 @@ implementation
       begin
         result:=0;
         repeat
-          a := a shr 7;
           inc(result);
-          if a=0 then
-            break;
-        until false;
+          a := a shr 7;
+        until a=0;
       end;
 
 
     function LengthSleb128(a: int64) : byte;
-      var
-        b: byte;
-        more: boolean;
       begin
-        more := true;
-        result:=0;
-        repeat
-          b := a and $7f;
-          a := SarInt64(a, 7);
-
-          if (
-            ((a = 0) and (b and $40 = 0)) or
-            ((a = -1) and (b and $40 <> 0))
-          ) then
-            more := false;
-          inc(result);
-          if not(more) then
-            break;
-        until false;
+        { 'a xor SarInt64(a,63)' has upper bits 0...01 where '0's symbolize sign bits of 'a' and 1 symbolizes its most significant non-sign bit.
+          'shl 1' ensures storing the sign bit. }
+        result:=LengthUleb128(qword(a xor SarInt64(a,63)) shl 1);
       end;