Browse Source

* fixed packed bitsize calculation for types with a negative lower bound and
and upper bound just below the next power of two (mantis #34971)

git-svn-id: trunk@41161 -

Jonas Maebe 6 years ago
parent
commit
0db9ff5c39
3 changed files with 30 additions and 4 deletions
  1. 1 0
      .gitattributes
  2. 4 4
      compiler/symdef.pas
  3. 25 0
      tests/webtbs/tw34971.pp

+ 1 - 0
.gitattributes

@@ -16485,6 +16485,7 @@ tests/webtbs/tw3490.pp svneol=native#text/plain
 tests/webtbs/tw3491.pp svneol=native#text/plain
 tests/webtbs/tw3492.pp svneol=native#text/plain
 tests/webtbs/tw3494.pp svneol=native#text/plain
+tests/webtbs/tw34971.pp svneol=native#text/plain
 tests/webtbs/tw3499.pp svneol=native#text/plain
 tests/webtbs/tw3504.pp svneol=native#text/plain
 tests/webtbs/tw3506.pp svneol=native#text/plain

+ 4 - 4
compiler/symdef.pas

@@ -2716,8 +2716,8 @@ implementation
             if (minval>=0) then
               sizeval:=maxval
             else
-              { don't count 0 twice }
-              sizeval:=(cutils.max(-minval,maxval)*2)-1;
+             { don't count 0 twice, but take into account that range goes from -n-1..n }
+              sizeval:=(cutils.max(-minval,maxval+1)*2)-1;
             { 256 must become 512 etc. }
             nextpowerof2(sizeval+1,power);
             result := power;
@@ -2939,8 +2939,8 @@ implementation
             if (low>=0) then
               sizeval:=high
             else
-              { don't count 0 twice }
-              sizeval:=(cutils.max(-low,high)*2)-1;
+              { don't count 0 twice, but take into account that range goes from -n-1..n }
+              sizeval:=(cutils.max(-low,high+1)*2)-1;
             { 256 must become 512 etc. }
             nextpowerof2(sizeval+1,power);
             result := power;

+ 25 - 0
tests/webtbs/tw34971.pp

@@ -0,0 +1,25 @@
+type
+  t1 = -1..1;
+  t2 = -4..3;
+  t3 = -3..4;
+type
+  r1 = bitpacked record
+    f: t1;
+  end;
+
+  r2 = bitpacked record
+    f: t2;
+  end;
+
+  r3 = bitpacked record
+   f: t3;
+  end;
+
+begin
+  if bitsizeof(r1.f)<>2 then
+    halt(1);
+  if bitsizeof(r2.f)<>3 then
+    halt(2);
+  if bitsizeof(r3.f)<>4 then
+    halt(3);
+end.