cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 FPU square-root in C

jparker
Associate
Posted on February 24, 2012 at 19:22

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.
6 REPLIES 6
Posted on February 25, 2012 at 00:38

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?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zaurozavr
Associate II
Posted on February 27, 2012 at 14:17

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.
Posted on February 27, 2012 at 16:23

Keil calls __hardfp_sqrtf, Atollic/GNU presumably calls

__builtin_sqrtf()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zaurozavr
Associate II
Posted on February 27, 2012 at 17:16

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().

Posted on February 27, 2012 at 17:35

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jparker
Associate
Posted on February 27, 2012 at 18:41

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.