This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2][BZ #12515] Improve precision of clock function


On 05/21/13 08:34, Siddhesh Poyarekar wrote:

> +    return (clock_t) ((ts.tv_nsec * CLOCKS_PER_SEC) / 1000000000
> +		      + ts.tv_sec * CLOCKS_PER_SEC);

First, there's no need to cast to clock_t, and in general it's better
to avoid casts if they're not needed.

Second, ts.tv_nsec * CLOCKS_PER_SEC might overflow on a machine
with 32-bit 'long', and afterwards the resulting division by 1000000000
won't wrap around to the "correct" value.

How about this instead?

	return (ts.tv_sec * CLOCKS_PER_SEC
		+ ts.tv_nsec / (1000000000 / CLOCKS_PER_SEC));


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]