<div dir="ltr">The reasoning about the math looks safe. I'm not competent to judge its correctness :)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 26, 2021 at 8:11 AM Sebastian Huber <<a href="mailto:sebastian.huber@embedded-brains.de">sebastian.huber@embedded-brains.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">The maximum frequency is UINT32_MAX. Converted to a uint64_t variable<br>
it can be shifted by 32. The addition does not overflow since bin_per_s<br>
- 1 is UINT32_MAX.<br>
---<br>
cpukit/sapi/src/cpucounterconverter.c | 7 ++++---<br>
1 file changed, 4 insertions(+), 3 deletions(-)<br>
<br>
diff --git a/cpukit/sapi/src/cpucounterconverter.c b/cpukit/sapi/src/cpucounterconverter.c<br>
index 08a745dfc6..88403987d2 100644<br>
--- a/cpukit/sapi/src/cpucounterconverter.c<br>
+++ b/cpukit/sapi/src/cpucounterconverter.c<br>
@@ -42,19 +42,20 @@ int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks )<br>
<br>
rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt )<br>
{<br>
- return (rtems_counter_ticks) (((uint64_t) sbt * from_sbt_scaler) >> 31);<br>
+ return (rtems_counter_ticks) (((uint64_t) sbt * from_sbt_scaler) >> 32);<br>
}<br>
<br>
void rtems_counter_initialize_converter( uint32_t frequency )<br>
{<br>
uint64_t ns_per_s = UINT64_C(1000000000);<br>
uint64_t bin_per_s = UINT64_C(1) << 32;<br>
+ uint64_t bin_freq = (uint64_t) frequency << 32;<br>
<br>
to_ns_scaler = ((ns_per_s << 32) + frequency - 1) / frequency;<br>
- from_ns_scaler = ((UINT64_C(1) << 32) * frequency + ns_per_s - 1) / ns_per_s;<br>
+ from_ns_scaler = (bin_freq + ns_per_s - 1) / ns_per_s;<br>
<br>
to_sbt_scaler = ((bin_per_s << 31) + frequency - 1) / frequency;<br>
- from_sbt_scaler = ((UINT64_C(1) << 31) * frequency + bin_per_s - 1) / bin_per_s;<br>
+ from_sbt_scaler = (bin_freq + bin_per_s - 1) / bin_per_s;<br>
}<br>
<br>
static void rtems_counter_sysinit( void )<br>
-- <br>
2.26.2<br>
<br>
_______________________________________________<br>
devel mailing list<br>
<a href="mailto:devel@rtems.org" target="_blank">devel@rtems.org</a><br>
<a href="http://lists.rtems.org/mailman/listinfo/devel" rel="noreferrer" target="_blank">http://lists.rtems.org/mailman/listinfo/devel</a><br>
</blockquote></div>