Floating point issues at cortex-m4

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

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

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

Floating point issues at cortex-m4

Postby THaala » Wed Mar 07, 2012 11:43 pm

Dear Forum,

regarding this topic
http://forum.chibios.org/phpbb/viewtopic.php?f=2&t=286

i got similar problem. I am ready with porting STM32 Graphics Library to my Touchscreen based on a F4Discovery - Board.
My port was based onto the demo.c for this board. With existing compiler settings i got always exception in following statements:

Code: Select all

  AddPageControlObj( (uint16_t)((LCD_Width / 11)*9.3), (uint16_t)((LCD_Height / 10)*1.8), pageLabel_0a, pageStart );
//  AddPageControlObj( (uint16_t)269, (uint16_t)43, pageLabel_0a, pageStart );

  pageLabel_0b = NewLabel( 3, (uint8_t*)"Presentation & Demo", GL_HORIZONTAL, GL_FONT_BIG, GL_Black );
  AddPageControlObj( (uint16_t)((LCD_Width / 11)*10.4), (uint16_t)((LCD_Height / 10)*2.8), pageLabel_0b, pageStart );


after simplification i got following code with removing Optimization from compiler options -O2

following code causes an exception in line 2

Code: Select all

  float fa = (320 / 11)*9.3;
  int da = (uint16_t)fa;
  float fb = (240 / 10)*1.8;
  int db = (uint16_t)fb;
  float fc = (320 / 11)*10.4;
  int dc = (uint16_t)fc;
  float fd = (240 / 10)*2.8;
  int dd = (uint16_t)fd;

  int de = da + db + dc + de;


i can avoid excepions by removing following compiler options:
# USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 -mhard-float -mfpu=fpv4-sp-d16 -fsingle-precision-constant
USE_OPT = -ggdb -fomit-frame-pointer -falign-functions=16
# MCU = cortex-m4
MCU = cortex-m3

seems problems with the use of fpu are continuing...
does anybody have a solution ?

User avatar
Giovanni
Site Admin
Posts: 12573
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 643 times
Been thanked: 539 times
Contact:

Re: Floating point issues at cortex-m4

Postby Giovanni » Thu Mar 08, 2012 8:50 am

Basically you are removing support for HW floating point. Does removing JUST "-fsingle-precision-constant" and keeping "-mhard-float -mfpu=fpv4-sp-d16" fix the problem? this option has already been reported to create problems.

If not, creating a small example project would help us find the issue.

Giovanni

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

Re: Floating point issues at cortex-m4

Postby THaala » Thu Mar 08, 2012 7:55 pm

Dear Giovanni,

i have created and attached a eclipse project with minimal build based on the ARMCM4-STM32F407-DISCOVERY demo. 8-)

i am sure the problem also occurs if you switch off optmizer and add following lines to the original demo file:

Code: Select all

  float fa = (320 / 11)*9.3;
  uint16_t da = (uint16_t)fa;
  float fb = (240 / 10)*1.8;
  int db = (int)fb;

  int de = da + db;

Exception will be thrown on line 2 while converting to uint16 value.
i have tested with different compiler options. The problem disapears only after removing the main FPU switch "-mhard-float".
A simple remove of "-fsingle-precision-constant" doesnt solve the problem.
i think this might be a problem of gcc ? :geek:

By the way - how can i determine which exception has been occurred ? In debugger i can see only a while true - loop, but i couldn't detect the reason......

with kind reggards,
Thilo
Attachments
DiscoveryF4_LCDHLA.zip
Eclipse Project
(118.94 KiB) Downloaded 140 times

User avatar
Giovanni
Site Admin
Posts: 12573
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 643 times
Been thanked: 539 times
Contact:

Re: Floating point issues at cortex-m4

Postby Giovanni » Thu Mar 08, 2012 8:07 pm

Probably it is GCC related, that code does not interact with the OS and probably it would crash with or without ChibiOS, it would be good to try with IAR or Keil in order to confirm.

It should be easy to understand the problem by stepping into asm code, another thing to do is to determine the exception nature by inspecting system registers.

I hope to be able to give it a try in the weekend.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 12573
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 643 times
Been thanked: 539 times
Contact:

Re: Floating point issues at cortex-m4

Postby Giovanni » Sun Mar 11, 2012 12:30 pm

Hi,

I tested the code, it works correctly here. I noticed that in the chconf.h included in your demo you have:

Code: Select all

#define CORTEX_USE_FPU                  FALSE


I think this is the cause of your problem, the FPU is not initialized and fpu instructions trigger an exception.

Giovanni

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

Re: Floating point issues at cortex-m4

Postby THaala » Sun Mar 11, 2012 3:43 pm

Hello Giovanni,

you are right - after setting this Flag to TRUE the problem disappears.
Thank you much. :D

As i mentioned before i have had used the ARMCM4-STM32F407-DISCOVERY demo basically for my actions.
it would be better to set this Flag in the Demo - App too, because of others mustn't fall into same pit.
Seems that the Demo - App doesnt use floats but ist compiled with all settings to handle it with the FPU except this Flag.

Have a nice Weekend.

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

Re: Floating point issues at cortex-m4

Postby THaala » Fri Mar 16, 2012 12:25 am

Dear Giovanni, dear Forum,

again i have trouble with easy floating - point calculations.

After i have had switched ON the define, Giovanni mentioned above it seems first that all errors have gone.
But some floating point calculations gone wrong!
Now i am fighting again against such a simple problem:

Code: Select all

int main(void) {

   static const evhandler_t evhndl[] = {
     InsertHandler,
     RemoveHandler
   };
   Thread *shelltp = NULL;
   struct EventListener el0, el1;
   int32_t maxY, lalla;
   double base_y;

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
   halInit();
   chSysInit();
   base_y = 168.0;
   maxY = (int32_t)base_y;


all readers may guess that the value of maxY is 168 but it is zero in fact.

Disassembly of line "maxY = (int32_t)base_y;":

Code: Select all

08028f5c:   vldr d0, [sp, #40]      ; 0x28
08028f60:   bl 0x8000a40 <__fixdfsi>
08028f64:   mov r3, r0
08028f66:   str r3, [sp, #36]       ; 0x24


switching compiler settings to-
cortex-m3
CORTEX_USE_FPU FALSE
USE_OPT = -ggdb -fomit-frame-pointer -falign-functions=16
solves the problem.

Disassembly of line "maxY = (int32_t)base_y;" gives now:

Code: Select all

08029de2:   ldrd r0, r1, [sp, #40]  ; 0x28
08029de6:   bl 0x8000a40 <__fixdfsi>
08029dea:   mov r3, r0
08029dec:   str r3, [sp, #36]       ; 0x24


this lib function is linked from Codesourcery (both variant use same library)
__fixdfsi d:/winarm/sourcery_codebench_lite_for_arm_eabi/bin/../lib/gcc/arm-none-eabi/4.6.1/thumb2\libgcc.a(_arm_fixdfsi.o)

it is not really a problem for me because i can use M3 configuration instead - but i hope this help solve this fpu issue.
Any advices ?

regards,
Thilo

User avatar
Giovanni
Site Admin
Posts: 12573
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 643 times
Been thanked: 539 times
Contact:

Re: Floating point issues at cortex-m4

Postby Giovanni » Fri Mar 16, 2012 8:38 am

Hi,

You may try another compiler, YAGARTO for example, this is likely a compiler library problem.

You are using a double type, the M4 FPU only supports float types, it is possible that the library is compiled to use a FPU double-capable so it fails on the M4. It would be a good idea to ask CodeSourcery the correct options to use on the M4.

Giovanni

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

Re: Floating point issues at cortex-m4

Postby THaala » Sat Mar 17, 2012 4:16 pm

Dear Forum,

in the meanwhile i was in contact with the Head of YAGARTO - special thanks to him.
Bad news: He's not planning update for the future :(
But he points to the newest guy "Germia" who is providing a tooolchain.

https://launchpad.net/gcc-arm-embedded

I have tried all three toochains now. The problem appears with all of them.
Using a float instead of a double solves the problem.

it seems that the simple truth is...
080296ac: vldr d0, [sp, #40] ; 0x28
080296b0: bl 0x8000a40 <__fixdfsi>

fixdfsi not evaluate the special register "d0"....

little hint: this toolchain seems to need the use of
-mfloat-abi=hard switch instead of the -mhard-float switch!

i have tried to report this bug to Germia.
with kind regards,
Thilo

DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Floating point issues at cortex-m4

Postby DrunkenDonkey » Sat Mar 17, 2012 9:46 pm

Offtopic: too bad for yagarto, I love this toolchain, codesourcery being unstable for me, and the arm official one makes substantial bigger code on the same settings


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 1 guest