[fpc-devel] div creates wrong results on cortexm0

Michael Ring mail at michael-ring.org
Sat Jan 26 12:20:47 CET 2019


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]




More information about the fpc-devel mailing list