Hi,
I am using MK22FX512AVLQ12 microcontroller in my custom board design. I want to do some operations such as pow(), multiplication, division which involve uint64_t as well as float operands using FPU of Cortex M4 of controller. The Macro __FPU_PRESENT is 1 in MK22FA12.h file. Also I have defined __FPU_PRESENT in project settings. But Macro __FPU_USED is 0 in core_cm4.h by the conditions defined there.
After referring to link I have enabled all the settings for HardABI in project settings. But after viewing the disassembly code , I find it is still using some of the software library functions.
For eg.
float l_f_DifferenceCurr;
uint64_t l_ui64_DiffMultiplier = 0U;
l_ui64_DiffMultiplier = (uint64_t)(l_f_DifferenceCurr * (float)MF_ONE_LAKH);
Please refer the attached screenshot of disassembly for Above line
I have included arm_math.h file and math.h file in the code.
Please suggest the appropriate steps to use FPU for float operations.
Thanks & Regards,
Vaibhavi
Hi @vaibhavi_padwal ,
this is a float to unsigned 64bit integral conversion. Your FPU is able to do single precision operations (add, sub, mul, ...) and float to intergral 32bit conversion. Because you want to use a 64bit integral, a software conversion has to be used.
So the question is: do you really need 64bit? Then yes, this is what you get.
If you are ok with 32bits, then you can use
l_ui64_DiffMultiplier = (uint32_t)(l_f_DifferenceCurr * (float)MF_ONE_LAKH);
and you get something like this:
10: 4b0a ldr r3, [pc, #40] ; (3c <main+0x3c>)
12: edd3 7a00 vldr s15, [r3]
16: eeb0 7a08 vmov.f32 s14, #8 ; 0x40400000 3.0
1a: ee67 7a87 vmul.f32 s15, s15, s14
1e: eefc 7ae7 vcvt.u32.f32 s15, s15
22: ee17 3a90 vmov r3, s15
26: 461a mov r2, r3
28: f04f 0300 mov.w r3, #0
2c: 4904 ldr r1, [pc, #16] ; (40 <main+0x40>)
2e: e9c1 2300 strd r2, r3, [r1]
Somewhat related: you might need to upgrade to a processor with double precision FPU too (ARM Cortex-M7 for example) if you want a more powerful FPU.
Keep in mind that there are two things: one thing is to tell the compiler to create instructions matching the FPU in the hardware (this is done through the compiler switches, do not mess with the #defines unless you know exactly what you are doing). The other thing is to enable the FPU in the firmware as usually it is disabled by default, see https://mcuoneclipse.com/2016/10/29/tutorial-building-freertos-applications-for-arm-cortex-m4-on-i-m...
If you are new to FPU on ARM Cortex-M4: have a read at https://mcuoneclipse.com/2019/03/29/be-aware-floating-point-operations-on-arm-cortex-m4f/
I hope this helps,
Erich