2012-02-24 10:22 AM
Does anybody know how to use the square-root instruction in the STM32F4 FPU, using C code?
I'm running Atollic TrueStudio Lite 2.3.0, it's using the FPU for all other floating-point operations. For now, I'm using the sqrt() function but it calls the routine in libm.2012-02-24 03:38 PM
Atollic Lite doesn't permit linking external libraries, so perhaps you can implement with in-line assembler? Or implementing a simple assembler function in startup.s, or whatever.
Also consider that sqrt() is using doubles, the FPU doesn't support operations on doubles, perhaps sqrtf() would be more appropriate?2012-02-27 05:17 AM
As clive1 said (btw, no external libs are used in this case), you should write sqrtf() instead of sqrt().
But there is a bug (in Atollic toolchain or maybe worse) that only the first sqrtf() call runs in hardware (with asm vsqrtf instruction), all the other will use a software routine. I was told by Atollic support : ''I can't say when we will fix this, but we will definately look more at this.'' Temporarily you can eliminate this problem by adding -fno-math-errno in compilator option.2012-02-27 07:23 AM
Keil calls __hardfp_sqrtf, Atollic/GNU presumably calls
__builtin_sqrtf()
2012-02-27 08:16 AM
I never used Keil and there is no name trick.
Assuming we use HW FP (in Atollic 2.3 you should check this option implicitly - you have SW, HW and MIX choice) and technically speaking, the very first assembler instruction vsqrtf() sets IOE flag (Invalid Operation Event) in FPSCR register irrelevant to the operation result. IOE can't be cleared in user mode, only in processor priveledged mode. So if IOE event does not properly processed, no more HW vsqrtf() call possible.By the way, Cortex-M4F supports some double operations and its single FPU registers S0..S31 can be used as double D0..D15 but sqrt().
2012-02-27 08:35 AM
By the way, Cortex-M4F supports some double operations and its single FPU registers S0..S31 can be used as double D0..D15 but sqrt().
You must be looking at some other documentation (TRM r0p1), I don't see the M4F doing anything useful with 64-bit precision. Load/Store, Push/Pop really don't count.
2012-02-27 09:41 AM
Thanks for your replies guys. I tried using sqrtf() but it still called a software routine. I added -fno-math-errno and now it's using the FPU, so thanks for that one zaurozavr.