1

I'm trying to launch simple LED blinking application, all works fine but if I add -ffast-math, it goes to hard fault. I debugged via GDB and found that if I add -ffast-math, it inserts call to __arm_set_fast_math from __libc_init_array as part of initialization routine (from .init_array section)

Disassembling __arm_set_fast_math shows:

080001f4 <__arm_set_fast_math>:
 80001f4:       eef1 3a10       vmrs    r3, fpscr
 80001f8:       f043 7380       orr.w   r3, r3, #16777216       ;0x1000000
 80001fc:       eee1 3a10       vmsr    fpscr, r3
 8000200:       4770            bx      lr
 8000202:       bf00            nop

According GDB:

0x080001f4 in __arm_set_fast_math ()
(gdb) stepi
HardFault_Handler () at src/stm32f4xx_it.c:61

it jumps to hardFault handler immediately. removing --fast-math fixes everything. any idea? thanks!

Zhani Baramidze
  • 1,407
  • 1
  • 13
  • 32
  • 1
    I assume the fault is an illegal-instruction fault? Also that your CPU doesn't have a hardware FPU? Is there some reason you want to use `-ffast-math`? Are you using software-emulated floating-point? – Peter Cordes Aug 27 '17 at 15:19
  • I have mfloat-abi=hard -mfpu=fpv4-sp-d16, and -ffast-math just came with template that I got, took 2 days to debug it and now I want to know why it caused such behaviour. fault is "hard fault" – Zhani Baramidze Aug 27 '17 at 15:22
  • Did that build template come from your hardware vendor directly? Or was it just something you found? In any case, this documents the FPSCR layout: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0344b/Chdfafia.html. That fast-math init function is setting the FZ bit (flush denormals to zero), which presumably speeds up the FPU when it would have produced a denormal. – Peter Cordes Aug 27 '17 at 15:29
  • but M4 (core I'm using) also has same register: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/BABBFJEC.html and I'm trying to figure out why why it hardfaults – Zhani Baramidze Aug 27 '17 at 15:30
  • Without `-ffast-math`, do you get a hard fault when you compile code that does FP math? Like `double x = 1.111, y=2.33;` `double z = x * y;` (and compile with `-O0`, or use `volatile` or something so it's not evaluated at compile time.) – Peter Cordes Aug 27 '17 at 15:31
  • no, it works fine – Zhani Baramidze Aug 27 '17 at 15:32
  • You should edit some of that into your question. It's important that regular VFP math instructions work, but `vmrs` doesn't. (It would be good if you could show disassembly of a specific VFP instruction that does work as part of your `main()` function.) And also list the exact build options you're using, in the question for other people to see. IDK the answer myself. `vmrs` is supposed to work for CPUs that support VFP, I think. http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204h/Bcfbdihi.html – Peter Cordes Aug 27 '17 at 15:44
  • What is the specific CPU chip you're using? – Ross Ridge Aug 27 '17 at 21:39
  • did you enable the coprocessor(s) so that they will execute floating point? they generally power up disabled. – old_timer Aug 28 '17 at 02:59
  • does the cpuid registers for your chip show that the vendor included the floating point unit? – old_timer Aug 28 '17 at 02:59

0 Answers0