TransWikia.com

Why operations on float variable are performed on a double? STM32F401RE, MDK-ARM v5

Electrical Engineering Asked by Artem Glukhov on November 8, 2021

I am debugging a simple line of code:

float temp_f;
...
...
temp_f= temperature[0] +(temperature[1] >> 5)*0.125;

that is assembled into

316:                 temp_f= temperature[0] +(temperature[1] >> 5)*0.125; 
0x080018CE 4C1C      LDR           r4,[pc,#112]  ; @0x08001940
0x080018D0 7860      LDRB          r0,[r4,#0x01]
0x080018D2 0940      LSRS          r0,r0,#5
0x080018D4 F7FEFDD6  BL.W          __aeabi_ui2d (0x08000484)
0x080018D8 ED9F1B1A  VLDR          d1,[pc,#0x68]
0x080018DC EC532B11  VMOV          r2,r3,d1
0x080018E0 F7FEFD5E  BL.W          __aeabi_dmul (0x080003A0)
0x080018E4 EC410B18  VMOV          d8,r0,r1
0x080018E8 7820      LDRB          r0,[r4,#0x00]
0x080018EA F7FEFDCB  BL.W          __aeabi_ui2d (0x08000484)
0x080018EE EC532B18  VMOV          r2,r3,d8
0x080018F2 F7FEFCAE  BL.W          __aeabi_dadd (0x08000252)
0x080018F6 F7FEFDE5  BL.W          __aeabi_d2f (0x080004C4)
0x080018FA 4621      MOV           r1,r4

Since the ARM Cortex-M4 FPU is specifically for single precision floating point operations, I cannot understand why the compilation process links everything to double precision instructions like __aeabi_dadd (from what I understand being an external run-time ABI for ARM that runs in software) and only then converting back to float, instead of doing everything directly in single precision floating point format, maybe even in hardware?

This operation is performed inside a timer interrupt routine but I don’t have anything related to float in my main process.

I’m using HAL, with cubeMX and Keil.

EDIT: solved by suggestion of @nanofarad writing 0.125f instead of 0.125.
Apparently the default x.y notation in C is for double values.

One Answer

I'm assuming that temperature[1] is an integer type of some sort.

The subexpression (temperature[1] >> 5)*0.125 contains an operand 0.125 of type double, resulting in promotion throughout the whole expression. Try changing 0.125 to 0.125f, so that no promotion to double occurs.

Answered by nanofarad on November 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP