[ARM, LPC1114] Error DIV result lazarus / 79-Embedded - ARM ---------------------------------------------------------------------- devport · 2019-11-10 12:35 I now, i now it's me again I'm Write code for OLED 1.3" SH1106 driver. Maybe I found a compiler error I use code for display string: 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  procedure SH1106_string(x, y : byte; pstring : PChar; size, mode : byte; buffer : PByte);begin    while (Ord(pstring^) <> $00) do    begin        if (x > (WIDTH - trunc(size / 2))) then        begin            x := 0;          y := y + size;            if (y > (HEIGHT - size)) then          begin                y := 0;                x := 0;            end;        end;         SH1106_char(x, y, pstring^, size, mode, buffer);        x := x + trunc(size / 2);        Inc(pstring);    end;end; Parametrs: size := 16 If "trunc(size/2)" change to "(size div 2)" my OLED bad showing string. WHY? What am I doing wrong? ---------------------------------------------------------------------- howardpc · 2019-11-10 13:36 Do you get the same display difference between (size div 2) and Trunc(size/2) with the following? 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 procedure SH1106_string(x, y : byte; pstring : PChar; size, mode : byte; buffer : PByte);begin   size := size div 2;  // size := Trunc(size/2);   while (Ord(pstring^) <> $00) do   begin       if (x > (WID - size)) then       begin           x := 0;           y := y + size*2;         if (y > (HEI - size*2)) then           begin               y := 0;             x := 0;           end;       end;       SH1106_char(x, y, pstring^, size*2, mode, buffer);       x := x + size;       Inc(pstring);   end;end; ---------------------------------------------------------------------- MiR · 2019-11-10 14:30 Try simplifying, this makes it a lot easier to get a fix if there is an issue. What is important, do you use trunc compiler or official compiler, which optimization level do you use? For the following I am now using trunk compiler from this week and O2 because it makes (in this case) the generated code a little easier to read. Most simple version of a test would be: program test; var   size : byte; begin   size := 16;   size := size div 2; end. then compile this and make sure you have assembler output enabled: /usr/local/bin/ppcarm -O2 -Mobjfpc -Tembedded -al test.pas -WpLPC1114FBD48_302 -CpARMV6M and then have a look at the generated code: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # Var size located in register r1# [5] size := 16;        mov     r1,#16# [6] size := size div 2;        mov     r0,r1        lsr     r0,r0,#31      add     r0,r1,r0        asr     r0,r0,#1        uxtb    r0,r0# Var size located in register r0  You can also run this code in gdb or ozone to see if r0 has correct value in the end. (for me it has) Compiled with trunk compiler the code seems to do what I'd expect. Not the most optimized code for the situation but technically OK Look as the assembler code that was generated by your code now, if the code for div looks very differnet then modify the most simple example to match the exact problem. Then you will be able to properly judge if you found a compiler error or a programming error. ---------------------------------------------------------------------- MiR · 2019-11-10 15:24 Also this version: 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 program test;var  x,size : byte; function doit(ax,asize : byte):byte;begin  Result := ax+(asize div 2);end; begin  size := 16;  x := 10;  size := doit(x,size);end.  looks ok, I now brought in the addition I saw in your code Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # Var $result located in register r0        uxtb    r2,r0# Var ax located in register r2        uxtb    r1,r1# Var asize located in register r1.Ll2:# [7] Result := ax+(asize div 2);        mov     r0,r1        lsr     r0,r0,#31        add     r0,r1,r0        asr     r0,r0,#1      add     r2,r0# Peephole UxtbUxtb2Uxtb done        uxtb    r0,r2# Var $result located in register r0 ---------------------------------------------------------------------- devport · 2019-11-10 15:54 I'll check tomorrow, I'm at work now.  Substituting a constant value the code works fine. ---------------------------------------------------------------------- MiR · 2019-11-10 16:08 In your case (divide by 2) you can also try: (size shr 1) which will create much shorter code: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .Ll2:# [7] Result := ax+(asize shr 1);        lsr     r0,r0,#1        add     r2,r0# Peephole UxtbUxtb2Uxtb done        uxtb    r0,r2# Var $result located in register r0 ---------------------------------------------------------------------- Thaddy · 2019-11-10 16:18 Quote from: MiR on November 10, 2019, 03:30:00 pmCode: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   size := size div 2;It does not matter too much here indeed - because the compiler folds it into a shift - but in general a shift would be the better option. (2) as you explained while posts crossed. ---------------------------------------------------------------------- devport · 2019-11-10 22:40 Bit shifting result is correct! All compile in -Mfpc (no objfpc) and using ppcrossarm v 3.3.1 from source. I paste 3 options: this is -O2 optimization Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [325] begin        push    {r4,r5,r6,r7,r14}        sub     r13,r13,#36      mov     r4,r0# Var x located in register r4        mov     r5,r1# Var y located in register r5        str     r2,[r13, #8]# Var pString located in register r0        mov     r6,r3# Var Size located in register r6        add     r1,r13,#56        ldr     r0,[r13, #12]      ldrb    r0,[r1]        str     r0,[r13, #12]# Var Mode located in register r0        ldr     r7,[r13, #60]# Var buffer located in register r7# [326] while (Ord(pstring^) <> $00) do        bl      .Lj101.Lj100:# [328] if (x > (WIDTH - (size div 2))) then        mov   r1,#128        sub     r1,r0        cmp     r1,r4        bge     .Lj105        bl      .Lj103.Lj105:        bl      .Lj104.Lj103:# [330] x := 0;        mov     r4,#0 this is no optimization: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [325] begin        push    {r4,r14}        sub     r13,r13,#48# Var x located at r13+8, size=OS_8# Var y located at r13+12, size=OS_8# Var pString located at r13+16, size=OS_32# Var Size located at r13+20, size=OS_8# Var Mode located at r13+24, size=OS_8# Var buffer located at r13+28, size=OS_32        add     r4,r13,#8        strb    r0,[r4]        add     r0,r13,#12        strb    r1,[r0]        str     r2,[r13, #16]        add     r0,r13,#20        strb    r3,[r0]        add     r0,r13,#56        add     r1,r13,#24        ldrb    r0,[r0]      strb    r0,[r1]        ldr     r0,[r13, #60]        str     r0,[r13, #28]# [326] while (Ord(pstring^) <> $00) do        bl      .Lj101.Lj100:# [328] if (x > (WIDTH - (size div 2))) then        add   r0,r13,#20        ldrb    r0,[r0]        mov     r1,#128        sub   r1,r0        add     r0,r13,#8        ldrb    r0,[r0]        cmp   r1,r0        bge     .Lj105        bl      .Lj103.Lj105: this is no optimization, I using SHR: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [325] begin        push    {r4,r14}        sub     r13,r13,#48# Var x located at r13+8, size=OS_8# Var y located at r13+12, size=OS_8# Var pString located at r13+16, size=OS_32# Var Size located at r13+20, size=OS_8# Var Mode located at r13+24, size=OS_8# Var buffer located at r13+28, size=OS_32        add     r4,r13,#8        strb    r0,[r4]        add     r0,r13,#12        strb    r1,[r0]        str     r2,[r13, #16]        add     r0,r13,#20        strb    r3,[r0]        add     r0,r13,#56        add     r1,r13,#24        ldrb    r0,[r0]      strb    r0,[r1]        ldr     r0,[r13, #60]        str     r0,[r13, #28]# [326] while (Ord(pstring^) <> $00) do        bl      .Lj101.Lj100:# [328] if (x > (WIDTH - (size shr 1))) then        add   r0,r13,#20        ldrb    r0,[r0]        lsr     r0,r0,#1        mov     r3,#0        mov     r1,#128        mov     r2,#0        sub     r1,r0        sbc     r2,r3        add     r0,r13,#8        ldrb    r0,[r0]        mov     r3,#0        cmp     r2,r3        bge     .Lj105        bl      .Lj103.Lj105: in 2 example I don't see dividing the number 'size'. Is only subtraction from a variable 'WIDTH' ---------------------------------------------------------------------- Thaddy · 2019-11-11 08:08 Yes, the shift it correct. It also shows a little thought is still required by the programmer to choose an optimal algorithm. But in general, the compiler does a good job, even with div. ---------------------------------------------------------------------- MiR · 2019-11-11 13:41 Seems you are on to something, from what I can tell the DIV command vanished into thin air.... in both the -O2 and the -O- version of DIV. it is a lot easier to see in the -O2 Version where r6 is not referenced at all. Can you try to create a very simple sample that shows the issue in one code file? Sometimes the optimizer plays nasty tricks when it detects something is constant it pre-calculates the result and optimizes out a command like a divide by two completely. Had this a week or two ago, so it is good to have a simple example where you can overlook the complete generated code. And beeing able to run the code in a decent debugger also helps a lot. ---------------------------------------------------------------------- Laksen · 2019-11-11 16:59 Looks like a bug ---------------------------------------------------------------------- devport · 2019-11-12 09:10 I used this example to check the DIV: Using Lazarus IDE: ppcrossarm.exe -Tembedded -Parm -CpARMV6M -MObjFPC -l -vewnhibq  -otestprogram -al -WpLPC1114FBD48_303 Only DIV example not power on my LED (pin) 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 program test;var  x,size : byte; function doit(ax,asize : byte):byte;begin  Result := ax+(asize div 2);end; begin   LPC_SYSCON.SYSAHBCLKCTRL := LPC_SYSCON.SYSAHBCLKCTRL or ((1 shl 6) or (1 shl 16));  LPC_GPIO0.DIR := LPC_GPIO0.DIR OR $0002;   size := 16;  x := 10;  size := doit(x,size);  if(size = 18) then   LPC_GPIO0.MASKED_ACCESS[$0002] := 0;end. DIV: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [6] begin        push    {r14}        sub     r13,r13,#40# Var ax located at r13+0, size=OS_8# Var asize located at r13+4, size=OS_8# Var $result located at r13+8, size=OS_8        mov     r2,r13        strb    r0,[r2]        add     r0,r13,#4        strb    r1,[r0]# [7] Result := ax+(asize div 2);        add     r0,r13,#4        ldrb    r0,[r0]      mov     r0,r13        ldrb    r0,[r0]        add     r0,r1      uxtb    r0,r0        add     r1,r13,#8        strb    r0,[r1]# [8] end;        add     r1,r13,#8        ldrb    r0,[r1]        add     r13,r13,#40        pop     {r15} SHR: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # [6] begin        push    {r14}        sub     r13,r13,#40# Var ax located at r13+0, size=OS_8# Var asize located at r13+4, size=OS_8# Var $result located at r13+8, size=OS_8        mov     r2,r13        strb    r0,[r2]        add     r0,r13,#4        strb    r1,[r0]# [7] Result := ax+(asize shr 1);        add     r0,r13,#4        ldrb    r0,[r0]      mov     r1,r0        lsr     r1,r1,#1        mov     r0,r13      ldrb    r0,[r0]        add     r0,r1        uxtb    r0,r0        add   r1,r13,#8        strb    r0,[r1]# [8] end;        add     r1,r13,#8        ldrb    r0,[r1]        add     r13,r13,#40        pop     {r15} all outputs assembly in attachments. ---------------------------------------------------------------------- MiR · 2019-11-12 10:14 How old is your trunk based compiler? with this version: Free Pascal Compiler version 3.3.1 [2019/11/10] for arm the assembler code looks OK Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # Var ax located at r13+0, size=OS_8# Var asize located at r13+4, size=OS_8# Var $result located at r13+8, size=OS_8        mov     r2,r13        strb    r0,[r2]        add     r0,r13,#4        strb    r1,[r0]# [7] Result := ax+(asize div 2);        add     r0,r13,#4        ldrb    r0,[r0]        mov     r1,r0        lsr     r1,r1,#31        add     r1,r0,r1        asr     r1,r1,#1        mov     r0,r13        ldrb    r0,[r0]        add     r0,r1        uxtb    r1,r0        add     r0,r13,#8        strb    r1,[r0]# [8] end;        add     r1,r13,#8      ldrb    r0,[r1]        add     r13,r13,#40        pop     {r15} ---------------------------------------------------------------------- devport · 2019-11-12 10:52 3.3.1 [2019/11/03] If this example compile to x86 on Windows this result is correct. fault is only cross arm compiler. ---------------------------------------------------------------------- Thaddy · 2019-11-12 12:48 Quote from: devport on November 12, 2019, 11:52:28 am3.3.1 [2019/11/03] If this example compile to x86 on Windows this result is correct. fault is only cross arm compiler. On arm it is a bug. You should report it. It is also possibly a regression depending on the compiler you used. If Laksen says it is a possible bug I tend to believe him, specifically since the equivalent shift works as I tested. ---------------------------------------------------------------------- devport · 2019-11-12 16:32 I reported this error.