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] Define CLOCKS_PER_SEC type to the type clock_t


On Mon, Jan 5, 2015 at 2:51 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Mon, Jan 5, 2015 at 2:45 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Mon, Jan 5, 2015 at 2:32 PM, Paul Eggert <eggert@cs.ucla.edu> wrote:
>>> On 01/05/2015 12:46 PM, H.J. Lu wrote:
>>>>
>>>> C99 specifies that CLOCKS_PER_SEC is a constant expression
>>>
>>>
>>> A minor point (mostly for comments): CLOCKS_PER_SEC needn't be a constant
>>> expression.  Even POSIX doesn't require that: on the contrary, it explicitly
>>> warns you that it might not be a constant expression, even though it must
>>> evaluate to 1000000.
>>>
>>>> +       * bits/time2.h: New file.
>>>> +       * sysdeps/unix/sysv/linux/x86/bits/time2.h: Likewise.
>>>
>>>
>>> Wouldn't it be simpler and clearer to replace "# define CLOCKS_PER_SEC
>>> 1000000l" with "# define CLOCKS_PER_SEC ((clock_t) 1000000)"?  That way, one
>>> wouldn't need all these other little files running around.
>>
>> My copy of C99 has
>>
>> ---
>>       CLOCKS_PER_SEC
>>
>> which expands to a constant expression with type clock_t (described below)
>> that is the number per second of the value returned by the clock function.
>> ---
>>
>> Has it been changed?
>
> Yes, it does:
>
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_281.htm
>
> I will update my patch.

This is the updated patch.  OK to install?

Thanks.

-- 
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.
* time/clocktest.c (main): Replace %ld with %jd and cast to
intmax_t.
From fc8542f839c1a3b47a91b2b59ac3e8851ddae151 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 5 Jan 2015 11:47:31 -0800
Subject: [PATCH] Define CLOCKS_PER_SEC type to the type clock_t

C99 specifies that CLOCKS_PER_SEC is an expression with the type clock_t.
This patch adds a generic <bits/time2.h> to define CLOCKS_PER_SEC and
provides the Linux/x86-64 version of <bits/time2.h> to support x32.

	[BZ #17797]
	* bits/time.h (CLOCKS_PER_SEC): Changed to ((clock_t) 1000000).
	* sysdeps/unix/sysv/linux/bits/time.h (CLOCKS_PER_SEC): Likewise.
	* time/clocktest.c (main): Replace %ld with %jd and cast to
	intmax_t.
---
 ChangeLog                           | 8 ++++++++
 bits/time.h                         | 8 ++++----
 sysdeps/unix/sysv/linux/bits/time.h | 8 ++++----
 time/clocktest.c                    | 4 ++--
 4 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 13448df..ef0aab4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-01-05  H.J. Lu  <hongjiu.lu@intel.com>
+
+	[BZ #17797]
+	* bits/time.h (CLOCKS_PER_SEC): Changed to ((clock_t) 1000000).
+	* sysdeps/unix/sysv/linux/bits/time.h (CLOCKS_PER_SEC): Likewise.
+	* time/clocktest.c (main): Replace %ld with %jd and cast to
+	intmax_t.
+
 2015-01-05  Joseph Myers  <joseph@codesourcery.com>
 
 	[BZ #17748]
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: <time.h>
-   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: <time.h>
    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: <time.h>
-   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: <time.h>
    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/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;
 }
-- 
2.1.0


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