[fpc-pascal] Can FPC optimize: if (s[i]='a') or ...

Bernd Oppolzer bernd.oppolzer at t-online.de
Mon Apr 15 07:52:22 CEST 2019


Am 15.04.2019 um 03:35 schrieb wkitty42 at windstream.net:
> On 4/14/19 7:28 AM, Rainer Stratmann wrote:
>> On Samstag, 13. April 2019 22:30:55 CEST Alexey Tor. wrote:
>>> E.g. i have a loop which test each s[i] char for several cases: 'a',
>>> 'b', 'c'.
>>>
>>> for i:= 1 to length(s) do
>>>
>>> if (s[i]='a') or (s[i]='b') or (s[i]='c') then ...
>>>
>>> Can FPC optimize it so it only reads s[i] once (to register), not 3 
>>> times?
>>
>> You can optimise by yourself.
>>
>> var
>>   c : char;
>>   l : longint;
>>
>> begin
>>   l := length( s );
>>   for i := 1 to l do
>>    c := s[ i ];
>>    if ( c = 'a' ) or ( c = 'b' ) or ( c = 'c' ) then ...
>
>
> this looks like it will read three times like the original instead of 
> once like using the IN set operation... it is still stepping through 
> each one of the comparison steps instead of just doing a set match...
>
>

True for New Stanford Pascal:

      23       1N   1)    for I := 1 to LENGTH ( S ) do
      24       2N   1)      begin
      25       2N   1)        C := S [ I ] ;
      26       2N   1)        if ( C = 'a' ) or ( C = 'b' ) or ( C = 'c' 
) then
      27       2N   1)          WRITELN ( I , '-tes Zeichen ist a, b 
oder c' ) ;
      28       2N   1)      end (* for *)

Lines without @@ = P-Code
Lines with @@ = IBM 370 Machine Code (Assembler notation)
as documented by Stage 2 (PASCAL2.PAS) when the A+ switch is set

-------------------- LOC  26 --------------------------------
      0250:           LOD  C,1,424
      0250:           LDC  C,'a'
      0250:           EQU  C
@@ 0250:       CLI   424(13),97  --- compare storage location with 'a'
      0254:           LOD  C,1,424
@@ 0254:       LA    2,1
@@ 0258:       BC    8,*+6
@@ 025C:       SR    2,2
      025E:           LDC  C,'b'
      025E:           EQU  C
@@ 025E:       CLI   424(13),98  --- compare storage location with 'b'
      0262:           IOR  B
@@ 0262:       LA    3,1
@@ 0266:       BC    8,*+6
@@ 026A:       SR    3,3
@@ 026C:       OR    2,3
      026E:           LOD  C,1,424
      026E:           LDC  C,'c'
      026E:           EQU  C
@@ 026E:       CLI   424(13),99  --- compare storage location with 'c'
      0272:           IOR  B
@@ 0272:       LA    3,1
@@ 0276:       BC    8,*+6
@@ 027A:       SR    3,3
@@ 027C:       OR    2,3
      027E:           FJP  L16
@@ 027E:       BC    8,L16





More information about the fpc-pascal mailing list