[fpc-pascal] Conversion from C to Pascal - Left bit shift

Markus Greim info at schleibinger.com
Fri Sep 3 16:30:26 CEST 2021


After 35 years of Pascal experience I would urgently recommend NOT to trust any automatic type conversion in the case of shift operators. Alteady Turbo Pascal failed here on x386 architectures. Force input and output variables to a certain data type before you use the shift operator. 


Just my 5 cents 
Markus Greim

Mit freundlichen Grüßen

Markus Greim

Schleibinger Geräte
Teubert u. Greim GmbH
Gewerbestrasse 4
84428 Buchbach
Germany

Tel. +49 8086 94731-10
Fax. +49 8086 94731-14
Mobil +49 172 8 999 196
http://www.schleibinger.com


Amtsgericht Traunstein HRB 9646
Geschäftsführer:
Dipl.-Ing. (FH) Oliver Teubert
Dipl.-Ing. (Univ.) Markus Greim
UST-ID. DE 174 175 046

--- original message ---
On September 3, 2021 at 2:36 PM GMT+2 fpc-pascal at lists.freepascal.org wrote:

I made a few tests on Ubuntu 64 bits (arch x86_64) with variations on a small test program:
var
E2: Byte= 3;
E1: LongWord= 1;
E: QWord;
begin
E:= (1000*E1) shl E2;
writeln( 'E2', E2);
writeln( 'E1', E1);
writeln( 'E', E);
end.

In the assembly window, shl is computed on 64 bits %rax, I get :

project1.lpr:132 E:= (1000*E1) shl E2;
00000000004011B2 8b05d8f50c00 mov 0xcf5d8(%rip),%eax # 0x4d0790 <TC_$P$PROJECT1_$$_E1>
00000000004011B8 4869c0e8030000 imul $0x3e8,%rax,%rax
00000000004011BF 0fb60dbaf50c00 movzbl 0xcf5ba(%rip),%ecx # 0x4d0780 <TC_$P$PROJECT1_$$_E2>
00000000004011C6 48d3e0 shl %cl,%rax
00000000004011C9 48890580b51000 mov %rax,0x10b580(%rip) # 0x50c750 <U_$P$PROJECT1_$$_E>

Changing the formula to E:= E1 shl E2, shl computed on 32 bits %edx, (I don't understand the "and %edx,%edx", may be just to clear the carry ?)
I get :

project1.lpr:132 E:= E1 shl E2;
00000000004011B2 0fb605c7f50c00 movzbl 0xcf5c7(%rip),%eax # 0x4d0780 <TC_$P$PROJECT1_$$_E2>
00000000004011B9 8b15d1f50c00 mov 0xcf5d1(%rip),%edx # 0x4d0790 <TC_$P$PROJECT1_$$_E1>
00000000004011BF 89c1 mov %eax,%ecx
00000000004011C1 d3e2 shl %cl,%edx
00000000004011C3 21d2 and %edx,%edx
00000000004011C5 48891584b51000 mov %rdx,0x10b584(%rip) # 0x50c750 <U_$P$PROJECT1_$$_E>

Changing E1 to QWord ( E1: QWord= 1; ), shl is computed on 64 bits %rax, I get :

project1.lpr:132 E:= E1 shl E2;
00000000004011B2 0fb605c7f50c00 movzbl 0xcf5c7(%rip),%eax # 0x4d0780 <TC_$P$PROJECT1_$$_E2>
00000000004011B9 488b15d0f50c00 mov 0xcf5d0(%rip),%rdx # 0x4d0790 <TC_$P$PROJECT1_$$_E1>
00000000004011C0 4889c1 mov %rax,%rcx
00000000004011C3 48d3e2 shl %cl,%rdx
00000000004011C6 48891583b51000 mov %rdx,0x10b583(%rip) # 0x50c750 <U_$P$PROJECT1_$$_E>

_______________________________________________
fpc-pascal maillist - fpc-pascal at lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
--- end of original message ---
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210903/6073c193/attachment.htm>


More information about the fpc-pascal mailing list