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 Tue, 2013-05-21 at 21:44 +0530, Siddhesh Poyarekar wrote:
> On Tue, May 21, 2013 at 09:00:09AM -0700, Paul Eggert wrote:
> > 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.
> 
> OK, cast removed.
> 
> > 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));
> 
> Thanks, patch updated and tested.  OK to go in with this change?
> After the PLT fix is reviewed of course.

That will cause its own round error problems, so need to cast to long
long for multiply and divide to avoid this.


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