lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-devel] LWIP_U32_DIFF is unnecessarily complex


From: Simon Goldschmidt
Subject: Re: [lwip-devel] LWIP_U32_DIFF is unnecessarily complex
Date: Wed, 23 Nov 2011 22:20:48 +0100

Mike Kleshov <address@hidden> wrote:

>> Since unsigned addition cannot overflow, nor can unsigned subtraction. So
>> the macro LWIP_U32_DIFF should be defined simply:
>> 
>> #define LWIP_U32_DIFF(a, b) ((a) - (b))

Which is what is used by now. BTW, the define has been removed and we're just 
directly using "a - b" by now.

>> assuming the comment "'a' is expected to be higher than 'b'" is valid.
>> 
>> If that comment is not valid, a better definition would be:
>> 
>> #define LWIP_U32_DIFF(a, b) (((a) >= (b)) ? ((a) - (b)) : ((b) - (a)))
> 
> This last define is so wrong. You seem to misunderstand the phrase
> "'a' is expected to be higher than 'b'".

I guess that's my fault, I should have explained it better in that comment...

> It assumes that a and b are
> snapshots of a free-running 32-bit timer, so 'a' would be later than
> 'b'. They can easily be (a > b) or (a < b) in unsigned arithmetic
> sense.

Correct. Anyway, "a - b" gives correct results (and I didn't have the slightest 
worries it wouldn't be standard). I don't know who led me to using that strange 
define in the first place, but thanks for sorting that out, everyone.

Simon



reply via email to

[Prev in Thread] Current Thread [Next in Thread]