Open yakov-bakhmatov opened 1 year ago
For example, create an empty Makefile project in CubeMX for MCU STM32F407 and compile it by arm-gnu-toolchain-12.2.
arm-none-eabi-size build/stm32f407-empty.elf text data bss dec hex filename 3668 20 1572 5260 148c build/stm32f407-empty.elf
Using the
muldiv
function:arm-none-eabi-size build/stm32f407-empty.elf text data bss dec hex filename 2892 20 1572 4484 1184 build/stm32f407-empty.elf
The difference in binary size is 776 bytes.
What if you do some uint64_t
calculation somewhere else in your application?
Function
uint32_t HAL_RCC_GetSysClockFreq(void)
calculates expressionPLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
using 64-bit multiplication and division.https://github.com/STMicroelectronics/STM32CubeF4/blob/d5af56388ff037735ac99de39abf2b46f9921aa3/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c#L905-L920
This forces the compiler (in particular gcc) to link to an external
__aeabi_uldivmod
function that performs a 64-bit division.But to calculate the expression
a * b / c
, wherea
,b
andc
areuint32_t
and the result is also 32 bits, it is possible without expanding to 64 bits.Let
a = m * c + n
,b = p * c + q
. ThenDefine the auxiliary function:
Expressions in lines 911, 916 are converted to the following
How this change affects the size of the binary.
For example, create an empty Makefile project in CubeMX for MCU STM32F407 and compile it by arm-gnu-toolchain-12.2.
Using the
muldiv
function:The difference in binary size is 776 bytes.