Boolean expressions not always reduced to simplest expression by compiler lazarus / 64-FPC development ---------------------------------------------------------------------- ccrause · 2017-11-05 10:44 So I check the code emitted by the compiler for various equivalent boolean expressions - it seems as if the compiler simplify the expression left to right only? AVR example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var a: byte;...  a := a or 1 or 2 or 4;  a2:   20 91 00 01     lds     r18, 0x0100  a6:   21 60           ori     r18, 0x01       ; 1  a8:   22 60           ori     r18, 0x02       ; 2  aa:   24 60           ori     r18, 0x04       ; 4  ac:   20 93 00 01     sts     0x0100, r18   a := a or (1 or 2 or 4);  b0:   20 91 00 01     lds     r18, 0x0100  b4:   27 60           ori     r18, 0x07       ; 7  b6:   20 93 00 01     sts     0x0100, r18   a := 1 or 2 or 4 or a;  ba:   20 91 00 01     lds   r18, 0x0100  be:   27 60           ori     r18, 0x07       ; 7  c0: 20 93 00 01     sts     0x0100, r18 Linux x64 example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var a: byte;...project1.lpr:10                           a := a or 1 or 2 or 4;00000000004001D2 0fb605a7db2200           movzbl 0x22dba7(%rip),%eax        # 0x62dd80 00000000004001D9 83c801                 or     $0x1,%eax00000000004001DC 83c802                   or   $0x2,%eax00000000004001DF 83c804                   or     $0x4,%eax00000000004001E2 880598db2200             mov    %al,0x22db98(%rip)        # 0x62dd80  project1.lpr:11                           a := a or (1 or 2 or 4);00000000004001E8 0fb60591db2200           movzbl 0x22db91(%rip),%eax        # 0x62dd80 00000000004001EF 83c807                   or     $0x7,%eax00000000004001F2 880588db2200             mov    %al,0x22db88(%rip)        # 0x62dd80  project1.lpr:12                           a := 1 or 2 or 4 or a;00000000004001F8 0fb60581db2200           movzbl 0x22db81(%rip),%eax        # 0x62dd80 00000000004001FF 83c807                   or     $0x7,%eax0000000000400202 880578db2200             mov    %al,0x22db78(%rip)        # 0x62dd80 Is it worth the effort to create a bug report for this?  Alternative is to remember bracketing constant expressions to help the compiler ---------------------------------------------------------------------- Nitorami · 2017-11-05 13:19 Confirm same behaviour on win32. Certainly a minor issue, still, the compiler should be able to optimize such simple expressions. ---------------------------------------------------------------------- ASerge · 2017-11-06 15:35 If you need deep optimization, there is nothing shameful in giving a hint to the compiler (an example of brackets in your case) . Another example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Test(A, B: Integer): Boolean;begin  Result := (A > 0) and (B < 0);end;but this is more efficient (without jumping): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TestFullBool(A, B: Integer): Boolean;begin{$PUSH}{$BOOLEVAL ON}  Result := (A > 0) and (B < 0);{$POP}end;Such kinds of micro optimization is not performed, as far as I know, by all versions of Delphi. ---------------------------------------------------------------------- kupferstecher · 2017-11-06 17:54 Quote from: ccrause on November 05, 2017, 11:44:59 amboolean expressionsWhy you call it boolean? Don't you speak of bitwise operations? Quote from: ASerge on November 06, 2017, 04:35:08 pmIf you need deep optimization, there is nothing shameful in giving a hint to the compiler (an example of brackets in your case) .Such expressions are very common for target embedded (AVR,ARM etc.), when bits of hardware registers are set (mostly initialization phase). So this is not a use case where one would do micro optimisations, but as its so often used, compiler support would be reasonable. ---------------------------------------------------------------------- ccrause · 2017-11-06 19:09 Quote from: ASerge on November 06, 2017, 04:35:08 pm...but this is more efficient (without jumping): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$BOOLEVAL ON}  Result := (A > 0) and (B < 0); I see the opposite behaviour with the AVR compiler, if $BOOLEVAL is off more compact code is generated.  I guess there are platform dependent optimizations that also play a role here. ---------------------------------------------------------------------- ccrause · 2017-11-06 19:14 Quote from: kupferstecher on November 06, 2017, 06:54:03 pmWhy you call it boolean? Don't you speak of bitwise operations?Correct indeed, my code perform bitwise manipulations ---------------------------------------------------------------------- engkin · 2017-11-07 00:41 Quote from: ccrause on November 05, 2017, 11:44:59 amIs it worth the effort to create a bug report for this? This is a bug. :-|