diff --git a/cpukit/sapi/src/cpucounterconverter.c b/cpukit/sapi/src/cpucounterconverter.c index 08e43ac2d6..11a85f0e6c 100644 --- a/cpukit/sapi/src/cpucounterconverter.c +++ b/cpukit/sapi/src/cpucounterconverter.c @@ -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 ) diff --git a/testsuites/sptests/spcpucounter01/init.c b/testsuites/sptests/spcpucounter01/init.c index c6c7abec40..bdb6c1b890 100644 --- a/testsuites/sptests/spcpucounter01/init.c +++ b/testsuites/sptests/spcpucounter01/init.c @@ -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();