Index: Common.h =================================================================== diff -u -r6b3109b7090a207d9ec7ab67006bd95389ea0b28 -r810dd01b8fa7d5864adea6aaf13c43c61f2c889b --- Common.h (.../Common.h) (revision 6b3109b7090a207d9ec7ab67006bd95389ea0b28) +++ Common.h (.../Common.h) (revision 810dd01b8fa7d5864adea6aaf13c43c61f2c889b) @@ -94,6 +94,7 @@ #define NEARLY_ZERO 0.00000001 ///< Value that is nearly zero. Used for floating point zero comparisons (e.g. divide by zero checks) #define HEX_64_K 0x10000 ///< 64K (65536 in decimal) +#define HEX_32_BIT_FULL_SCALE 0xFFFFFFFF ///< 32-bit full scale value #define MASK_OFF_MSB 0x00FF ///< Bits to mask off the most significant byte of a 2-byte word #define MASK_OFF_LSB 0xFF00 ///< Bits to mask off the least significant byte of a 2-byte word #define MASK_OFF_MSW 0x0000FFFF ///< Bits to mask off the most significant 2-byte word of a 4-byte word 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 Index: Utilities.h =================================================================== diff -u -r2932935752882cd7bc9808bfe238b03753d3e167 -r810dd01b8fa7d5864adea6aaf13c43c61f2c889b --- Utilities.h (.../Utilities.h) (revision 2932935752882cd7bc9808bfe238b03753d3e167) +++ Utilities.h (.../Utilities.h) (revision 810dd01b8fa7d5864adea6aaf13c43c61f2c889b) @@ -85,6 +85,7 @@ U16 crc16( const U08 *address, U32 len ); U08 crc8( const U08 *address, U32 len ); U32 u32DiffWithWrap( U32 start, U32 end ); +S32 u32BiDiffWithWrap( U32 start, U32 end ); U16 u16DiffWithWrap( U16 start, U16 end ); S16 u16BiDiffWithWrap( U16 start, U16 end ); void initTimeWindowedCount( TIME_WINDOWED_COUNT_T cnt, U32 maxCnt, U32 winMs );