Floating point issues at cortex-m4

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

THaala
Posts: 12
Joined: Tue Dec 06, 2011 10:27 pm

Re: Floating point issues at cortex-m4

Post by THaala »

Hello Forum,

the problem seems solved.

special thanks tor user "prx" at mikrocontroller.net.
http://www.mikrocontroller.net/topic/252696#new

the compiler switch -mfloat_abi=softfp seems to be right. It ensures the right mix of calling subroutines in case of using "double" values not handled by the fpu and the correct assembler code involving the fpu at cortex-m4 -otherwise.

USE_OPT = -ggdb -fomit-frame-pointer -falign-functions=16 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -fsingle-precision-constant
MPU = cortex-m4
CORTEX_USE_FPU TRUE

Code: Select all

    dy = 168.0;
    fx = (float)dy;
    dy = 202.0;
    fy = 197.0;
    fz = 12.2;
    float d = (float)dy * fx * fy * fz;
    intx = (int)fx;
    inty = (int)dy;
    intz = (int)d;

assembler:
08029bda:   ldrd r0, r1, [sp, #64]  ; 0x40
08029bde:   bl 0x8000a90 <__truncdfsf2>
08029be2:   vmov s14, r0
08029be6:   vldr s15, [sp, #60]     ; 0x3c
08029bea:   vmul.f32 s14, s14, s15
08029bee:   vldr s15, [sp, #56]     ; 0x38
08029bf2:   vmul.f32 s14, s14, s15
08029bf6:   vldr s15, [sp, #52]     ; 0x34
08029bfa:   vmul.f32 s15, s14, s15
08029bfe:   vstr s15, [sp, #48]     ; 0x30
141           intx = (int)fx;
08029c02:   vldr s15, [sp, #60]     ; 0x3c
08029c06:   vcvt.s32.f32 s15, s15
08029c0a:   vmov r3, s15
08029c0e:   str r3, [sp, #44]       ; 0x2c
142           inty = (int)dy;
08029c10:   ldrd r0, r1, [sp, #64]  ; 0x40
08029c14:   bl 0x8000a40 <__fixdfsi>
08029c18:   mov r3, r0
08029c1a:   str r3, [sp, #40]       ; 0x28
143           intz = (int)d;
08029c1c:   vldr s15, [sp, #48]     ; 0x30
08029c20:   vcvt.s32.f32 s15, s15
08029c24:   vmov r3, s15
08029c28:   str r3, [sp, #36]       ; 0x24


i thin it would be a good idea to change the compiler settings inside the makefile and set the use of the fpu to "on"

There is one question left to Giovanni. As far i can see the complier starts using registes s15, s14.... maybe downto s0.
In the context swich routine i can see that only registers s16 - s31 are saved.... is this right or is this wrong ?

Code: Select all

void _port_switch(Thread *ntp, Thread *otp) {

  asm volatile ("push    {r4, r5, r6, r7, r8, r9, r10, r11, lr}"
                : : : "memory");
#if CORTEX_USE_FPU
  asm volatile ("vpush   {s16-s31}" : : : "memory");
#endif

  asm volatile ("str     sp, [%1, #12]                          \n\t"
                "ldr     sp, [%0, #12]" : : "r" (ntp), "r" (otp));

#if CORTEX_USE_FPU
  asm volatile ("vpop    {s16-s31}" : : : "memory");
#endif
  asm volatile ("pop     {r4, r5, r6, r7, r8, r9, r10, r11, pc}"
                : : : "memory");
}


Cheers,
Thilo
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Floating point issues at cortex-m4

Post by Giovanni »

Thanks for the solution, I will try it too.

About registers, it is correct, in ChibiOS the context switch routine saves only the function-preserved registers and not just float registers also normal registers are only partially saved. This means that when switching from thread to thread only about half registers are saved, the other registers are only saved in the "external context" by ISR handlers.

All registers are saved but in different moments and only when needed, this one of the reasons it is so fast.

Giovanni
dm13dv1b
Posts: 6
Joined: Tue Jul 29, 2014 9:29 am

Re: Floating point issues at cortex-m4

Post by dm13dv1b »

As I see it is a fairly old topic but my problem almost the same. I'm using ChibOS 2.6.5 and arm-none-eabi-gcc (4.8.2-14ubuntu1+6) 4.8.2.
Every time I tried to build projects witch are using FPU I get tha same error:

Code: Select all

Compiling crt0.c
arm-none-eabi-gcc: error: unrecognized argument in option '-mfloat-abi=yes'
arm-none-eabi-gcc: note: valid arguments to '-mfloat-abi=' are: hard soft softfp
make: *** [build/obj/crt0.o] Error 1

I have the

Code: Select all

#define CORTEX_USE_FPU                  TRUE

and my gcc

Code: Select all

ifeq ($(USE_FPU),yes)
  USE_OPT = -ggdb -fomit-frame-pointer -falign-functions=16 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -fsingle-precision-constant
  DDEFS += -DCORTEX_USE_FPU=TRUE
else

Am I doing something wrong? :( Thank you!
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Floating point issues at cortex-m4

Post by Giovanni »

Are you using a makefile from a previous version? "yes" is not a valid setting in new makefiles.

Giovanni
dm13dv1b
Posts: 6
Joined: Tue Jul 29, 2014 9:29 am

Re: Floating point issues at cortex-m4

Post by dm13dv1b »

Thank you Giovanni it seems that it was an old makefile. I've changed what you mentioned and the FPU errors gone.
Now the other come :)
Thank you!
Giovanni wrote:Are you using a makefile from a previous version? "yes" is not a valid setting in new makefiles.

Giovanni
dm13dv1b
Posts: 6
Joined: Tue Jul 29, 2014 9:29 am

Re: Floating point issues at cortex-m4

Post by dm13dv1b »

It is a little bit offtopic, but on this page:
http://ugfx.org/get-ugfx/11-documentation/37-get-ugfx-chibios
it's still USE_FPU = yes
And I guess it was my fault :)
Post Reply