This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2][BZ #12515] Improve precision of clock function
- From: Paul Eggert <eggert at cs dot ucla dot edu>
- To: munroesj at us dot ibm dot com
- Cc: Steven Munroe <munroesj at linux dot vnet dot ibm dot com>, Siddhesh Poyarekar <siddhesh at redhat dot com>, libc-alpha at sourceware dot org
- Date: Tue, 21 May 2013 14:14:08 -0700
- Subject: Re: [PATCH v2][BZ #12515] Improve precision of clock function
- References: <20130521145611 dot GM8927 at spoyarek dot pnq dot redhat dot com> <20130521151839 dot GA18430 at domone dot kolej dot mff dot cuni dot cz> <20130521153442 dot GO8927 at spoyarek dot pnq dot redhat dot com> <519B9A09 dot 6030305 at cs dot ucla dot edu> <20130521161441 dot GQ8927 at spoyarek dot pnq dot redhat dot com> <1369155615 dot 30724 dot 111 dot camel at spokane1 dot rchland dot ibm dot com> <519BD3EF dot 80109 at cs dot ucla dot edu> <1369169130 dot 30724 dot 114 dot camel at spokane1 dot rchland dot ibm dot com>
On 05/21/13 13:45, Steven Munroe wrote:
>> If that isn't clear, perhaps we can prepend a line like the
>> > following, to make it clear:
>> >
>> > static_assert (1000000000 % CLOCKS_PER_SEC == 0);
>> >
> That assume that __clock_gettime (CLOCK_REALTIME,) and CLOCKS_PER_SEC
> are multiples of 10.
>
> "Although the value of CLOCKS_PER_SEC is required to be 1 million on all
> XSI-conformant systems, it may be variable on other systems, and it
> should not be assumed that CLOCKS_PER_SEC is a compile-time constant."
We're talking about code in the GNU C library, right?
There, CLOCKS_PER_SEC is guaranteed to be a compile-time constant
that is equal to 1 million. The static_assert is meant
merely as a memory aid, to make things clear; it's guaranteed
to succeed.
There's no assumption here that clock_gettime returns a multiple
of 10, or of CLOCKS_PER_SEC. For example, if clock_gettime
returns tv_sec == 975 and tv_nsec = 123456789, then the
expression (ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec /
(1000000000 / CLOCKS_PER_SEC)) returns 975 * 1000000
+ 123456789 / 1000, which is 975123456, which is the
correct value.