Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-01

div creates wrong results on cortexm0

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Ring

I thought I was going crazy when debugging code that worked fine on cortex m3/4 but created unexpected results on cortex m0.

Turns out that the compiler creates incorrect assembler code for cortexm0 for div when used with constant expressions. Optimization level was -O1 and -O-

Take this code:

program divtest;
var
   a,b,c,d : word;
begin
   a := 64;
   b := 8;
   c := a div 8; //.LI4
   d := a div b; //.LI5
end.

looking at the assembler code it is very evident that no shift operations are done on cortex m0 (see .LI4). Is this a known issue or should I create a new one in bugzilla? (Search did not reveal a hit that looked like a match)

Michael

assembler cortex m0:

.Ll2:
    mov    r1,#64
    ldr    r0,.Lj3
    strh    r1,[r0]
.Ll3:
    mov    r1,#8
    ldr    r0,.Lj4
    strh    r1,[r0]
.Ll4:
    ldr    r0,.Lj3
    ldrh    r0,[r0]
    uxth    r0,r0
    ldr    r1,.Lj6
    strh    r0,[r1]
.Ll5:
    ldr    r0,.Lj3
    ldrh    r1,[r0]
    ldr    r0,.Lj4
    ldrh    r0,[r0]
    bl    fpc_div_longint
    uxth    r0,r0
    ldr    r1,.Lj9
    strh    r0,[r1]

assembler cortex m4:

.Ll2:
    movs    r1,#64
    ldr    r0,.Lj3
    strh    r1,[r0]
.Ll3:
    movs    r1,#8
    ldr    r0,.Lj4
    strh    r1,[r0]
.Ll4:
    ldr    r0,.Lj3
    ldrh    r0,[r0]
    asrs    r1,r0,#31
    add    r0,r0,r1,lsr #29
    asrs    r0,#3
    uxth    r1,r0
    ldr    r0,.Lj6
    strh    r1,[r0]
.Ll5:
    ldr    r0,.Lj3
    ldrh    r0,[r0]
    ldr    r1,.Lj4
    ldrh    r1,[r1]
    sdiv    r0,r0,r1
    uxth    r0,r0
    ldr    r1,.Lj9
    strh    r0,[r1]

Reply by email

Jeppe Johansen
to Michael Ring

Does the attached patch work?

Michael Ring wrote 80 lines
I thought I was going crazy when debugging code that worked fine on cortex m3/4 but created unexpected results on cortex m0. Turns out that the compiler creates incorrect assembler code for cortexm0 for div when used with constant expressions. Optimization level was -O1 and -O- Take this code: program divtest; var   a,b,c,d : word; begin   a := 64;   b := 8;   c := a div 8; //.LI4   d := a div b; //.LI5 end. looking at the assembler code it is very evident that no shift operations are done on cortex m0 (see .LI4). Is this a known issue or should I create a new one in bugzilla? (Search did not reveal a hit that looked like a match) Michael assembler cortex m0: .Ll2:     mov    r1,#64     ldr    r0,.Lj3     strh    r1,[r0] .Ll3:     mov    r1,#8     ldr    r0,.Lj4     strh    r1,[r0] .Ll4:     ldr    r0,.Lj3     ldrh    r0,[r0]     uxth    r0,r0     ldr    r1,.Lj6     strh    r0,[r1] .Ll5:     ldr    r0,.Lj3     ldrh    r1,[r0]     ldr    r0,.Lj4     ldrh    r0,[r0]     bl    fpc_div_longint     uxth    r0,r0     ldr    r1,.Lj9     strh    r0,[r1] assembler cortex m4: .Ll2:     movs    r1,#64     ldr    r0,.Lj3     strh    r1,[r0] .Ll3:     movs    r1,#8     ldr    r0,.Lj4     strh    r1,[r0] .Ll4:     ldr    r0,.Lj3     ldrh    r0,[r0]     asrs    r1,r0,#31     add    r0,r0,r1,lsr #29     asrs    r0,#3     uxth    r1,r0     ldr    r0,.Lj6     strh    r1,[r0] .Ll5:     ldr    r0,.Lj3     ldrh    r0,[r0]     ldr    r1,.Lj4     ldrh    r1,[r1]     sdiv    r0,r0,r1     uxth    r0,r0     ldr    r1,.Lj9     strh    r0,[r1]

Reply by email

Michael Ring
to Jeppe Johansen

My basic testcase looks fine now and also the 'real' code now works as expected when I switch back from shr commands to div

Thank you very much, Jeppe!

Michael

Jeppe Johansen wrote 83 lines
Does the attached patch work? On 1/26/19 12:20 PM, Michael Ring wrote: > I thought I was going crazy when debugging code that worked fine on > cortex m3/4 but created unexpected results on cortex m0. > > Turns out that the compiler creates incorrect assembler code for > cortexm0 for div when used with constant expressions. Optimization > level was -O1 and -O- > > Take this code: > > program divtest; > var >   a,b,c,d : word; > begin >   a := 64; >   b := 8; >   c := a div 8; //.LI4 >   d := a div b; //.LI5 > end. > > looking at the assembler code it is very evident that no shift > operations are done on cortex m0 (see .LI4). Is this a known issue or > should I create a new one in bugzilla? (Search did not reveal a hit > that looked like a match) > > Michael > > assembler cortex m0: > > .Ll2: >     mov    r1,#64 >     ldr    r0,.Lj3 >     strh    r1,[r0] > .Ll3: >     mov    r1,#8 >     ldr    r0,.Lj4 >     strh    r1,[r0] > .Ll4: >     ldr    r0,.Lj3 >     ldrh    r0,[r0] >     uxth    r0,r0 >     ldr    r1,.Lj6 >     strh    r0,[r1] > .Ll5: >     ldr    r0,.Lj3 >     ldrh    r1,[r0] >     ldr    r0,.Lj4 >     ldrh    r0,[r0] >     bl    fpc_div_longint >     uxth    r0,r0 >     ldr    r1,.Lj9 >     strh    r0,[r1] > > assembler cortex m4: > > .Ll2: >     movs    r1,#64 >     ldr    r0,.Lj3 >     strh    r1,[r0] > .Ll3: >     movs    r1,#8 >     ldr    r0,.Lj4 >     strh    r1,[r0] > .Ll4: >     ldr    r0,.Lj3 >     ldrh    r0,[r0] >     asrs    r1,r0,#31 >     add    r0,r0,r1,lsr #29 >     asrs    r0,#3 >     uxth    r1,r0 >     ldr    r0,.Lj6 >     strh    r1,[r0] > .Ll5: >     ldr    r0,.Lj3 >     ldrh    r0,[r0] >     ldr    r1,.Lj4 >     ldrh    r1,[r1] >     sdiv    r0,r0,r1 >     uxth    r0,r0 >     ldr    r1,.Lj9 >     strh    r0,[r1]

Reply by email