stm32duino / STM32RTC

Arduino RTC library for STM32.
127 stars 48 forks source link

Remove usage of log2() with floating point value #40

Closed fpistm closed 3 years ago

fpistm commented 3 years ago

This allows to save space. (around 4k). Thanks to @PeeJay in this comment: https://github.com/stm32duino/STM32LowPower/issues/13#issuecomment-689512983

Note that I've added a check to 0 value because __builtin_clz with 0 produce an undefined result: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

Built-in Function: int __builtin_clz (unsigned int x)

Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined. 
PeeJay commented 3 years ago

You beat me to it! :) I noticed my M0+ core doesn't actually have the clz instruction, so the compiler implements it instead in 84 bytes.