From 0db9ff5c39f66e967a1cd4bdaa7536877a48210e Mon Sep 17 00:00:00 2001 From: Jonas Maebe Date: Fri, 1 Feb 2019 17:31:53 +0000 Subject: [PATCH] * 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 - --- .gitattributes | 1 + compiler/symdef.pas | 8 ++++---- tests/webtbs/tw34971.pp | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 tests/webtbs/tw34971.pp diff --git a/.gitattributes b/.gitattributes index 3554d00acf..d346cb518c 100644 --- a/.gitattributes +++ b/.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 diff --git a/compiler/symdef.pas b/compiler/symdef.pas index 6836510f84..502851b4ed 100644 --- a/compiler/symdef.pas +++ b/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; diff --git a/tests/webtbs/tw34971.pp b/tests/webtbs/tw34971.pp new file mode 100644 index 0000000000..7d957ba0e5 --- /dev/null +++ b/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.