Skip to content

Conversation

@guanqun
Copy link

@guanqun guanqun commented Oct 10, 2015

static_cast should be done after the multiplication operation. Otherwise the milliseconds is already an int.

static_cast should be done after the multiplication operation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

求问这句能解释下么XD

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

milliseconds is already an integer, it doesn't need to be casted to integer again. Instead, kNumMicrosPerMilli is a long, so after multiplication, it needs to be casted into int.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多谢哈 您在akuna么 我看到你的题解了XD 那里感觉如何哇

@EricWF
Copy link
Contributor

EricWF commented Oct 12, 2015

If we choose to perform the cast we should assert that the value is representative is the new type. We don't want to silently truncate.

@dominichamon Should we change the interface of these functions to take size_t for extra precision and to prevent overflows?

@guanqun
Copy link
Author

guanqun commented Oct 12, 2015

The current code doesn't check the overflow sadly...

https://github.com/google/benchmark/blob/master/src/sleep.cc#L47

@dmah42
Copy link
Member

dmah42 commented Oct 12, 2015

We could change to size_t, or int64_t, but we'd end up truncating in some cases as timespec ultimately takes int.

I'm not sure that choosing int64_t for the constants was a good move in the first place as even `kNumNanosPerSecond comfortably fits in 32 bits. The number of nanoseconds required to overflow the 32-bits would be about an hour, if i did my math correctly, which i'm comfortable with as a max sleep.

We should check for overflow, or at least potential overflow, before the multiplications.

@pleroy
Copy link
Contributor

pleroy commented Oct 12, 2015

I'm afraid you didn't do your math correctly, @dominichamon. 32 bits in nanoseconds is about 4 seconds.

@dmah42
Copy link
Member

dmah42 commented Oct 12, 2015

see, this is why i shouldn't be allowed to write anything on the internet before coffee. thanks, @pleroy.

in SleepForMicroseconds we take an int (32-bit) and mod/multiply by 1000 for the nanoseconds. We divide by 1000000 for the seconds. The longest sleep time we can have is, then, 2^32/1000000, which is indeed 4.3 seconds.

There's a subtle bug in SleepForSeconds which takes a double (ie, 64-bits) and silently truncates to a 32-bit int.

I think the correct approach is to take int64_t, do the math in 64-bit, and then check for overflow before truncating to 32-bits right before calling the library functions.

@dmah42
Copy link
Member

dmah42 commented Feb 14, 2016

@guanqun can you make the changes from the above discussion, or would you rather abandon this PR and i'll create an issue for the above?

@guanqun
Copy link
Author

guanqun commented Feb 15, 2016

@dominichamon give me two days and I'll try to fix this. Otherwise it's all yours. :D

@EricWF
Copy link
Contributor

EricWF commented Jun 3, 2017

This patch isn't needed. You're right that the static cast is incorrect, but it's also entirely unneeded. kNumMicrosPerMilli is also an int so the result type of the multiplication also has the type int.

The correct thing is to remove the cast entirely, which has already been done.

@EricWF EricWF closed this Jun 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants