From patchwork Tue Jan 6 13:03:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 4520 Received: (qmail 4491 invoked by alias); 6 Jan 2015 13:03:32 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 4480 invoked by uid 89); 6 Jan 2015 13:03:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-yh0-f46.google.com X-Received: by 10.170.113.130 with SMTP id f124mr70504316ykb.90.1420549405706; Tue, 06 Jan 2015 05:03:25 -0800 (PST) Date: Tue, 6 Jan 2015 05:03:23 -0800 From: "H.J. Lu" To: Paul Eggert Cc: GNU C Library Subject: Re: [PATCH] Define CLOCKS_PER_SEC type to the type clock_t Message-ID: <20150106130323.GA25803@gmail.com> References: <20150105204653.GA31001@intel.com> <54AB1115.80608@cs.ucla.edu> <54AB2185.2060402@cs.ucla.edu> <54AB68E7.7050903@cs.ucla.edu> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <54AB68E7.7050903@cs.ucla.edu> User-Agent: Mutt/1.5.23 (2014-03-12) On Mon, Jan 05, 2015 at 08:47:35PM -0800, Paul Eggert wrote: > H.J. Lu wrote: > >assert (CLOCKS_PER_SEC == 1000000l); > > This fails only at runtime if the value is wrong, which is not as good as > checking it at compile-time. Something like this, perhaps: > > #if __STDC_VERSION__ < 201112L > # define _Static_assert(e, s) extern int (*static_assert_checker (void)) \ > [sizeof (struct { unsigned int error_if_negative: (e) ? 1 : -1; })] > #endif > #define verify(e) _Static_assert (e, "verify (" #e ")") > > and then use 'verify' instead of 'assert'. > > Also, there's no need for the trailing 'l' here. This is what I am checking in. _Static_assert is added to GCC 4.6 which is the minimum GCC version required for glibc build. Tested with GCC 4.6 and 4.8. H.J. --- [BZ #17797] * bits/time.h (CLOCKS_PER_SEC): Changed to ((clock_t) 1000000). * sysdeps/unix/sysv/linux/bits/time.h (CLOCKS_PER_SEC): Likewise. * sysdeps/unix/sysv/linux/clock.c (clock): _Static_assert CLOCKS_PER_SEC == 1000000. * time/clocktest.c (main): Replace %ld with %jd and cast to intmax_t. --- ChangeLog | 10 ++++++++++ NEWS | 2 +- bits/time.h | 8 ++++---- sysdeps/unix/sysv/linux/bits/time.h | 8 ++++---- sysdeps/unix/sysv/linux/clock.c | 7 +++---- time/clocktest.c | 4 ++-- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 262e300..be5eb18 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2015-01-06 H.J. Lu + + [BZ #17797] + * bits/time.h (CLOCKS_PER_SEC): Changed to ((clock_t) 1000000). + * sysdeps/unix/sysv/linux/bits/time.h (CLOCKS_PER_SEC): Likewise. + * sysdeps/unix/sysv/linux/clock.c (clock): _Static_assert + CLOCKS_PER_SEC == 1000000. + * time/clocktest.c (main): Replace %ld with %jd and cast to + intmax_t. + 2015-01-05 Roland McGrath * sysdeps/generic/unwind-resume.h: New file. diff --git a/NEWS b/NEWS index 63918df..49c0a2d 100644 --- a/NEWS +++ b/NEWS @@ -16,7 +16,7 @@ Version 2.21 17583, 17584, 17585, 17589, 17594, 17601, 17608, 17616, 17625, 17630, 17633, 17634, 17635, 17647, 17653, 17657, 17664, 17665, 17668, 17682, 17717, 17719, 17722, 17723, 17724, 17725, 17732, 17733, 17744, 17745, - 17746, 17747, 17775, 17777, 17780, 17781, 17782, 17793, 17796 + 17746, 17747, 17775, 17777, 17780, 17781, 17782, 17793, 17796, 17797 * i386 memcpy functions optimized with SSE2 unaligned load/store. diff --git a/bits/time.h b/bits/time.h index ae17b7f..7585ddf 100644 --- a/bits/time.h +++ b/bits/time.h @@ -24,13 +24,13 @@ # ifndef _BITS_TIME_H # define _BITS_TIME_H 1 -/* ISO/IEC 9899:1990 7.12.1: - The macro `CLOCKS_PER_SEC' is the number per second of the value - returned by the `clock' function. */ +/* ISO/IEC 9899:1999 7.23.1: Components of time + The macro `CLOCKS_PER_SEC' is an expression with type `clock_t' that is + the number per second of the value returned by the `clock' function. */ /* CAE XSH, Issue 4, Version 2: The value of CLOCKS_PER_SEC is required to be 1 million on all XSI-conformant systems. */ -# define CLOCKS_PER_SEC 1000000l +# define CLOCKS_PER_SEC ((clock_t) 1000000) # if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K /* Even though CLOCKS_PER_SEC has such a strange value CLK_TCK diff --git a/sysdeps/unix/sysv/linux/bits/time.h b/sysdeps/unix/sysv/linux/bits/time.h index 226d6dd..706946c 100644 --- a/sysdeps/unix/sysv/linux/bits/time.h +++ b/sysdeps/unix/sysv/linux/bits/time.h @@ -39,13 +39,13 @@ struct timeval # ifndef _BITS_TIME_H # define _BITS_TIME_H 1 -/* ISO/IEC 9899:1990 7.12.1: - The macro `CLOCKS_PER_SEC' is the number per second of the value - returned by the `clock' function. */ +/* ISO/IEC 9899:1999 7.23.1: Components of time + The macro `CLOCKS_PER_SEC' is an expression with type `clock_t' that is + the number per second of the value returned by the `clock' function. */ /* CAE XSH, Issue 4, Version 2: The value of CLOCKS_PER_SEC is required to be 1 million on all XSI-conformant systems. */ -# define CLOCKS_PER_SEC 1000000l +# define CLOCKS_PER_SEC ((clock_t) 1000000) # if (!defined __STRICT_ANSI__ || defined __USE_POSIX) \ && !defined __USE_XOPEN2K diff --git a/sysdeps/unix/sysv/linux/clock.c b/sysdeps/unix/sysv/linux/clock.c index e06c4b3..e568bf3 100644 --- a/sysdeps/unix/sysv/linux/clock.c +++ b/sysdeps/unix/sysv/linux/clock.c @@ -20,15 +20,14 @@ #include #include -#if CLOCKS_PER_SEC != 1000000l -# error "CLOCKS_PER_SEC should be 1000000" -#endif - clock_t clock (void) { struct timespec ts; + _Static_assert (CLOCKS_PER_SEC == 1000000, + "CLOCKS_PER_SEC should be 1000000"); + /* clock_gettime shouldn't fail here since CLOCK_PROCESS_CPUTIME_ID is supported since 2.6.12. Check the return value anyway in case the kernel barfs on us for some reason. */ diff --git a/time/clocktest.c b/time/clocktest.c index 2e6457d..13b7420 100644 --- a/time/clocktest.c +++ b/time/clocktest.c @@ -30,7 +30,7 @@ main (int argc, char ** argv) printf ("%jd clock ticks per second (start=%jd,stop=%jd)\n", (intmax_t) (stop - start), (intmax_t) start, (intmax_t) stop); - printf ("CLOCKS_PER_SEC=%ld, sysconf(_SC_CLK_TCK)=%ld\n", - CLOCKS_PER_SEC, sysconf(_SC_CLK_TCK)); + printf ("CLOCKS_PER_SEC=%jd, sysconf(_SC_CLK_TCK)=%ld\n", + (intmax_t) CLOCKS_PER_SEC, sysconf(_SC_CLK_TCK)); return 0; }