Recent

Author Topic: question about bitpacking  (Read 929 times)

440bx

  • Hero Member
  • *****
  • Posts: 4901
question about bitpacking
« on: February 26, 2024, 06:34:28 am »
Hello,

I just want to make sure I don't make the wrong assumption about how the compiler bitpacks ranges. 

I expect that when bitpacked, the following two ranges will be packed in the _same_ number of bits:
Code: Pascal  [Select][+][-]
  1. type
  2.   RANGE_1 = 0..7;
  3.   RANGE_2 = 0..4;

In both cases, I expect the number of bits required to be 3 (bits 0, 1 and 2 - ignoring ordering dependencies that may vary from one cpu to another).  I'm seeking confirmation of that and if it is not, then some explanation that sheds light on the reason(s) why not.

Thank you for your help.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: question about bitpacking
« Reply #1 on: February 26, 2024, 06:45:16 am »
I think I can confirm, both ranges when bitpacked will take 3 bits each. Of course they need to sit next to each other, eg in a bitpacked record, otherwise they will be padded to full byte.

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: question about bitpacking
« Reply #2 on: February 26, 2024, 07:42:33 am »
Thank you Fibonacci.

I understand the padding part.

I just want to make sure that the compiler doesn't do something unexpected if all the possible bits in the range are not specified (or just plain zeroes), e.g, it takes 3 bits to make the number 4 and also takes 3 bits for the number 7.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5815
  • Compiler Developer
Re: question about bitpacking
« Reply #3 on: February 27, 2024, 09:09:28 pm »
I expect that when bitpacked, the following two ranges will be packed in the _same_ number of bits:
Code: Pascal  [Select][+][-]
  1. type
  2.   RANGE_1 = 0..7;
  3.   RANGE_2 = 0..4;

Correct.

Code: Pascal  [Select][+][-]
  1. program tbitsize;
  2.  
  3. type
  4.   RANGE_1 = 0..7;
  5.   RANGE_2 = 0..4;
  6.  
  7.   R = bitpacked record
  8.     r1: RANGE_1;
  9.     r2: RANGE_2;
  10.     b1: Boolean;
  11.     b2: Boolean;
  12.   end;
  13.  
  14.   A = bitpacked array[0..3] of RANGE_1;
  15.   B = bitpacked array[0..3] of RANGE_2;
  16.  
  17. begin
  18.   Writeln(BitSizeOf(RANGE_1));
  19.   Writeln(BitSizeOf(RANGE_2));
  20.   Writeln(BitSizeOf(R));
  21.   Writeln(BitSizeOf(A));
  22.   Writeln(BitSizeOf(B));
  23. end.

Code: [Select]
PS C:\fpc\git> .\testoutput\tbitsize.exe
8
8
8
16
16

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: question about bitpacking
« Reply #4 on: February 27, 2024, 09:23:01 pm »
Excellent !

Thank you for confirming that PascalDragon.  That's what makes sense but I still wanted confirmation.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018