Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"04-23-09 - Telling Time"

7 Comments -

1 – 7 of 7
Blogger won3d said...

Depending on overflow can be funky on some compilers, which can assume that overflow never happens.

Also, to compute the average of two numbers without overflow, you build the adder yourself using bitwise ops. Something like:

(a & b) + ((a^b) >> 1)

April 23, 2009 at 2:58 PM

Blogger cbloom said...

Hmm either I'm not understanding you or you're not understanding me.

The issue is with numbers like :

tsc1 = 250
tsc2 = 2

(in 8 bits)

The correct answer for "average of tsc1 and tsc2" is 254.

If you do (tsc1+tsc2)/2 you get the wrong thing (126).

To get that one valid way is :

U8 delta = tsc2 - tsc1;
U8 avg = tsc1 + (delta/2);

Are you saying that subtracting a larger unsigned int from a smaller one is not always defined as steps around the ring [0,max] ?

BTW I am explicitly using the knowledge here that tsc2 is after tsc1, and it is not more than one ring loop after.

April 23, 2009 at 3:21 PM

Blogger cbloom said...

But BTW of course this is exactly the whole point of rebasing to zero. You don't want to have code scattered around all over your app that deals with this stuff.

This is exactly one of those pitfalls where expert programmers will think "I'll just return the TSC as a U64 it will be fine people can deal with the wrapping at the client site it's easy" - and they are wrong. Maybe it is easy, but there are subtle bugs that are hard to spot (and rarely happen and hard to repro because they only happen when the counter wraps), and even if you do deal with everything right, it means every time you look at any code that does math on timer values, you have to waste brain energy thinking "hmm is that right?"

April 23, 2009 at 3:28 PM

Blogger cbloom said...

.. and I should also mention there's a huge disadvantage to ever using the TSC as a timer :

you have to calibrate it. To my knowledge there's no reliable way to get the TSC frequency.

April 24, 2009 at 10:32 AM

Blogger jwatte_food said...

[quote]Depending on overflow can be funky on some compilers, which can assume that overflow never happens.
[/quote]

I think that's a red herring. Any system and compiler you're likely to write game code for will do the same thing when wrapping unsigned integers using addition and subtraction arithmetic.

April 24, 2009 at 11:18 AM

Anonymous Anonymous said...

Raymond Chen actually had a post on his blog many years ago about the need to compute the average that way.

(As I recall it was written in that annoying Raymond Chen way that seemed to poo-poo the issue that there was all this voodoo knowledge and no actual way for developers to actually have the giant list of all the things they needed to know.)

April 24, 2009 at 9:57 PM

Blogger won3d said...

Yeah, I didn't understand you, because I was just thinking of the problem where if you want to find the average of 128 and 128 the naive way you get 0 if you only have 8-bit internal precision.

April 27, 2009 at 12:10 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.