rtems: Fix the counter conversion overflows

The 64-bit product of the counter ticks and the scaler overflowed.
rtems_counter_ticks_to_sbintime() returned a wrong value for more than
2 s.  rtems_counter_ticks_to_nanoseconds() returned a wrong value for
more than 4.29 s, where the product overflowed and the cast to 32 bits
truncated the result.  The test framework measures durations with
rtems_counter_ticks_to_sbintime().  On a 25 MHz counter, the
performance test suite reported 107 s for a run of 731 s.

Compute the 96-bit product from two 32-bit by 32-bit products.  The
result is the same as before for each value which did not overflow.
Return the nanoseconds with 64 bits.

The test spcpucounter01 checked the conversions at one second alone.
Check them at three seconds and at the maximum counter ticks.

Close #5806.

Assisted-by: Claude:claude-opus-5-5 claude-code
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
This commit is contained in:
Sebastian Huber authored and Kinsey Moore committed 2026-10-08 17:23:52 -05:00
1 parent 1cfb8b0e11
commit 97cf8529cb
2 files changed
+70 -2

No files matched your search

+23 -2
View File
@@ -50,9 +50,30 @@ static uint64_t to_sbt_scaler;
static uint64_t from_sbt_scaler;
/*
* Returns the product of the value and the scaler shifted right by the shift
* value. The product of the 32-bit value and the 64-bit scaler has 96 bits.
* The two partial products fit into 64 bits each. The shift value is at
* most 32.
*/
static uint64_t rtems_counter_scale(
uint32_t value,
uint64_t scaler,
unsigned int shift
)
{
uint64_t high;
uint64_t low;
high = (uint64_t) value * (uint32_t) ( scaler >> 32 );
low = (uint64_t) value * (uint32_t) scaler;
return ( high << ( 32 - shift ) ) + ( low >> shift );
}
uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks ticks )
{
return (uint32_t) ( ( ticks * to_ns_scaler ) >> 32 );
return rtems_counter_scale( ticks, to_ns_scaler, 32 );
}
rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
@@ -62,7 +83,7 @@ rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks )
{
return (int64_t) ( ( ticks * to_sbt_scaler ) >> 31 );
return (int64_t) rtems_counter_scale( ticks, to_sbt_scaler, 31 );
}
rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt )
+47
View File
@@ -64,6 +64,52 @@ static rtems_interval sync_with_clock_tick( void )
return current;
}
/*
* The conversion rounds up. A result below the exact value yields a large
* error.
*/
static uint64_t nanoseconds_error(
rtems_counter_ticks ticks,
uint32_t frequency
)
{
uint64_t expected;
expected = (uint64_t) ticks * UINT64_C( 1000000000 ) / frequency;
return rtems_counter_ticks_to_nanoseconds( ticks ) - expected;
}
static uint64_t sbintime_error( rtems_counter_ticks ticks, uint32_t frequency )
{
uint64_t expected;
expected = ( (uint64_t) ticks << 32 ) / frequency;
return (uint64_t) rtems_counter_ticks_to_sbintime( ticks ) - expected;
}
static void test_converter_large_values( void )
{
rtems_counter_ticks max;
rtems_counter_ticks three_seconds;
uint32_t frequency;
max = (rtems_counter_ticks) -1;
frequency = rtems_counter_frequency();
if ( frequency <= max / 3 ) {
three_seconds = 3 * frequency;
} else {
three_seconds = max;
}
rtems_test_assert( nanoseconds_error( three_seconds, frequency ) <= 1 );
rtems_test_assert( nanoseconds_error( max, frequency ) <= 1 );
rtems_test_assert( sbintime_error( three_seconds, frequency ) <= 2 );
rtems_test_assert( sbintime_error( max, frequency ) <= 2 );
}
static void test_converter( void )
{
CPU_Counter_ticks frequency;
@@ -282,6 +328,7 @@ static void Init( rtems_task_argument arg )
test_delay_ticks( ctx );
test_overheads( ctx );
test_converter();
test_converter_large_values();
test_report( ctx );
TEST_END();