Skip to content

Commit df63712

Browse files
committed
Remove usage of log2() with floating point value
This allows to save space. Thanks to @PeeJay. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 5be72b0 commit df63712

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/rtc.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
*/
3636

3737
#include "rtc.h"
38-
#include <math.h>
3938

4039
#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) &&\
4140
defined(HAL_RTC_MODULE_ENABLED) && !defined(HAL_RTC_MODULE_ONLY)
@@ -77,6 +76,11 @@ static void RTC_initClock(sourceClock_t source);
7776
static void RTC_computePrediv(int8_t *asynch, int16_t *synch);
7877
#endif /* !STM32F1xx */
7978

79+
static inline int _log2(int x)
80+
{
81+
return (x > 0) ? (sizeof(int) * 8 - __builtin_clz(x) - 1) : 0;
82+
}
83+
8084
/* Exported functions --------------------------------------------------------*/
8185

8286
/**
@@ -217,7 +221,7 @@ void RTC_setPrediv(int8_t asynch, int16_t synch)
217221
} else {
218222
RTC_computePrediv(&predivAsync, &predivSync);
219223
}
220-
predivSync_bits = (uint8_t)log2(predivSync) + 1;
224+
predivSync_bits = (uint8_t)_log2(predivSync) + 1;
221225
#else
222226
UNUSED(asynch);
223227
UNUSED(synch);
@@ -241,7 +245,7 @@ void RTC_getPrediv(int8_t *asynch, int16_t *synch)
241245
*asynch = predivAsync;
242246
*synch = predivSync;
243247
}
244-
predivSync_bits = (uint8_t)log2(predivSync) + 1;
248+
predivSync_bits = (uint8_t)_log2(predivSync) + 1;
245249
#else
246250
UNUSED(asynch);
247251
UNUSED(synch);

0 commit comments

Comments
 (0)