[fpc-pascal] Records

Vinzent Hoefler JeLlyFish.software at gmx.net
Wed Oct 26 11:56:58 CEST 2005


On Wednesday 26 October 2005 09:26, Micha Nelissen wrote:
> Jonas Maebe wrote:
> > Yes, but two different variables of the same type could have 
> > different values for those pad bytes. So you have to compare 
> > everything but the pad bytes.
>
> Oh right! Something like a masked compare or so...probably not
> implementable in an efficient way indeed I guess.

Whatever you call efficient. A simple memory compare won't do, as others 
already pointed out, and everything else depends on the actual layout 
of the structure.

Let's take a look at a compiler which does such nasty things:

--8< -- snip --
with Ada.Text_IO;

procedure t is

   type Padded_Record is
      record
         A : Integer range 0 .. 3;
         B : Integer range 0 .. 30000;
      end record;

   for Padded_Record use
      record
         A at 0 range 3 ..  5; -- use bits [5:3] of byte 0
         B at 3 range 1 .. 15; -- use bits [15:1] of bytes 3 and 4
      end record;

   function Compare (Left, Right : Padded_Record) return Boolean is
   begin
      return Left = Right;
   end Compare;

   X, Y : Padded_Record;
begin
   X.A := 2;
   X.B := 20000;

   Y := X;

   Ada.Text_IO.Put_Line (Boolean'Image (Compare (X, Y)));
end t;
--8< -- snip --

The interesting compare function after compiled with gcc3.1.1's
"gnatmake -f -S -O3 -fomit-frame-pointer t.adb" we get:

-- 8< -- snip --
t__compare.0:
	pushl	%esi
	xorl	%esi, %esi
	pushl	%ebx
	pushl	%ebx
	movl	%ecx, (%esp)
	movl	20(%esp), %ebx
	movl	16(%esp), %ecx
	movb	(%ebx), %al
	movb	(%ecx), %dl
	andl	$56, %eax
	andl	$56, %edx
	cmpb	%al, %dl
	je	.L49
.L42:
	movl	%esi, %eax
	popl	%edx
	popl	%ebx
	popl	%esi
	ret
	.p2align 4,,7
.L49:
	movb	3(%ecx), %dl
	xorl	%eax, %eax
	movb	4(%ecx), %cl
	movb	4(%ebx), %al
	shrb	%dl
	andl	$255, %ecx
	sall	$7, %ecx
	andl	$255, %edx
	sall	$7, %eax
	orl	%edx, %ecx
	movb	3(%ebx), %dl
	shrb	%dl
	andl	$255, %edx
	orl	%edx, %eax
	cmpw	%ax, %cx
	jne	.L42
	movl	$1, %esi
	jmp	.L42
-- 8< -- snip --

Of course, the type layout I forced here is really nasty, the resulting 
code looks quite a lot better to me when B start at bit 0 again.


Vinzent.

-- 
public key: http://www.t-domaingrabbing.ch/publickey.asc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: signature
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20051026/aa65c628/attachment.sig>


More information about the fpc-pascal mailing list