Index: Utilities.c =================================================================== diff -u -r37ac678630a7bf229d56a3702c5a7389f4b37ce2 -r37e193deafd55a34125d398054407222eaf036c7 --- Utilities.c (.../Utilities.c) (revision 37ac678630a7bf229d56a3702c5a7389f4b37ce2) +++ Utilities.c (.../Utilities.c) (revision 37e193deafd55a34125d398054407222eaf036c7) @@ -160,22 +160,90 @@ *************************************************************************/ 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 + * unsigned 16-bit numbers. If the second (ending) number is less than the + * first (starting) number, then the difference is calculated with a wrap past + * 16-bit full scale to zero. + * @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 + *************************************************************************/ +U16 u16DiffWithWrap( U16 start, U16 end ) +{ + U16 result = ( end >= start ? end - start : ( HEX_64_K - start ) + end ); + + 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 + *************************************************************************/ +S16 u16BiDiffWithWrap( U16 start, U16 end ) +{ + S16 result; + + U16 incDelta = ( end >= start ? end - start : ( HEX_64_K - start ) + end ); + U16 decDelta = HEX_64_K - incDelta; + + if ( incDelta < decDelta ) + { + result = (S16)incDelta; + } + else + { + result = (S16)decDelta * -1; + } + + return result; +} + /*********************************************************************//** * @brief * The initTimeWindowedCount function initializes a given time windowed count.