Index: Utilities.c =================================================================== diff -u -r2932935752882cd7bc9808bfe238b03753d3e167 -r810dd01b8fa7d5864adea6aaf13c43c61f2c889b --- Utilities.c (.../Utilities.c) (revision 2932935752882cd7bc9808bfe238b03753d3e167) +++ Utilities.c (.../Utilities.c) (revision 810dd01b8fa7d5864adea6aaf13c43c61f2c889b) @@ -160,22 +160,41 @@ *************************************************************************/ U32 u32DiffWithWrap( U32 start, U32 end ) { - U32 result; + U32 result = ( end >= start ? end - start : ( HEX_32_BIT_FULL_SCALE - start ) + end + 1 ); - // no wrap - if ( end >= start ) + return result; +} + +/*********************************************************************//** + * @brief + * The u16BiDiffWithWrap function calculates the difference between two given + * unsigned 16-bit numbers. This version of function returns a signed value + * to allow for a negative difference. + * @details Inputs: none + * @details Outputs: none + * @param start first number to compute difference for + * @param end second number to compute difference for + * @return the difference between the two given numbers + *************************************************************************/ +S32 u32BiDiffWithWrap( U32 start, U32 end ) +{ + S32 result; + + U32 incDelta = ( end >= start ? end - start : ( HEX_32_BIT_FULL_SCALE - start ) + end + 1 ); + U32 decDelta = HEX_32_BIT_FULL_SCALE - incDelta + 1; + + if ( incDelta < decDelta ) { - result = end - start; + result = (S32)incDelta; } - // wrapped else { - result = ( 0xFFFFFFFF - start ) + end + 1; + result = (S32)decDelta * -1; } return result; -} - +} + /*********************************************************************//** * @brief * The u16DiffWithWrap function calculates the difference between two given