Define CLOCKS_PER_SEC type to the type clock_t

Message ID 20150105204653.GA31001@intel.com
State Superseded
Headers

Commit Message

Lu, Hongjiu Jan. 5, 2015, 8:46 p.m. UTC
  C99 specifies that CLOCKS_PER_SEC is a constant 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.
Tested on i686, x32 and x86-64.  OK to install?

Thanks.

H.J.
---
	[BZ #17797]
	* bits/time2.h: New file.
	* sysdeps/unix/sysv/linux/x86/bits/time2.h: Likewise.
	* bits/time.h: Include <bits/time2.h>.
	(CLOCKS_PER_SEC): Removed.
	* sysdeps/unix/sysv/linux/bits/time.h: Likewise.
	* time/Makefile (headers): Add bits/time2.h.
	* time/clocktest.c (main): Replace %ld with %jd and cast to
	intmax_t.
---
 ChangeLog                                | 12 ++++++++++
 bits/time.h                              |  9 ++------
 bits/time2.h                             | 34 ++++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/bits/time.h      |  9 ++------
 sysdeps/unix/sysv/linux/x86/bits/time2.h | 38 ++++++++++++++++++++++++++++++++
 time/Makefile                            |  2 +-
 time/clocktest.c                         |  4 ++--
 7 files changed, 91 insertions(+), 17 deletions(-)
 create mode 100644 bits/time2.h
 create mode 100644 sysdeps/unix/sysv/linux/x86/bits/time2.h
  

Comments

Paul Eggert Jan. 5, 2015, 10:32 p.m. UTC | #1
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.
  
H.J. Lu Jan. 5, 2015, 10:45 p.m. UTC | #2
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?
  
H.J. Lu Jan. 5, 2015, 10:51 p.m. UTC | #3
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.

Thanks.
  

Patch

diff --git a/ChangeLog b/ChangeLog
index 4674e72..f97ee98 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@ 
+2015-01-05  H.J. Lu  <hongjiu.lu@intel.com>
+
+	[BZ #17797]
+	* bits/time2.h: New file.
+	* sysdeps/unix/sysv/linux/x86/bits/time2.h: Likewise.
+	* bits/time.h: Include <bits/time2.h>.
+	(CLOCKS_PER_SEC): Removed.
+	* sysdeps/unix/sysv/linux/bits/time.h: Likewise.
+	* time/Makefile (headers): Add bits/time2.h.
+	* time/clocktest.c (main): Replace %ld with %jd and cast to
+	intmax_t.
+
 2015-01-05  Joseph Myers  <joseph@codesourcery.com>
 
 	[BZ #17796]
diff --git a/bits/time.h b/bits/time.h
index ae17b7f..8ea1ce9 100644
--- a/bits/time.h
+++ b/bits/time.h
@@ -24,13 +24,8 @@ 
 # 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. */
-/* 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
+/* Get CLOCKS_PER_SEC.  */
+#  include <bits/time2.h>
 
 #  if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
 /* Even though CLOCKS_PER_SEC has such a strange value CLK_TCK
diff --git a/bits/time2.h b/bits/time2.h
new file mode 100644
index 0000000..1d1203d
--- /dev/null
+++ b/bits/time2.h
@@ -0,0 +1,34 @@ 
+/* CLOCKS_PER_SEC.  Generic version.
+   Copyright (C) 2015 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/*
+ * Never include this file directly; use <time.h> instead.
+ */
+
+#ifndef	_BITS_TIME_H
+# error "Never include <bits/time2.h> directly; use <time.h> instead."
+#endif
+
+/* ISO/IEC 9899:1999 7.23: Date and time <time.h>
+   The macro `CLOCKS_PER_SEC' is a constant 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
diff --git a/sysdeps/unix/sysv/linux/bits/time.h b/sysdeps/unix/sysv/linux/bits/time.h
index 226d6dd..6207f07 100644
--- a/sysdeps/unix/sysv/linux/bits/time.h
+++ b/sysdeps/unix/sysv/linux/bits/time.h
@@ -39,13 +39,8 @@  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. */
-/* 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
+/* Get CLOCKS_PER_SEC.  */
+#  include <bits/time2.h>
 
 #  if (!defined __STRICT_ANSI__ || defined __USE_POSIX) \
    && !defined __USE_XOPEN2K
diff --git a/sysdeps/unix/sysv/linux/x86/bits/time2.h b/sysdeps/unix/sysv/linux/x86/bits/time2.h
new file mode 100644
index 0000000..6cce326
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86/bits/time2.h
@@ -0,0 +1,38 @@ 
+/* CLOCKS_PER_SEC.  Linux/x86-64 version.
+   Copyright (C) 2015 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/*
+ * Never include this file directly; use <time.h> instead.
+ */
+
+#ifndef	_BITS_TIME_H
+# error "Never include <bits/time2.h> directly; use <time.h> instead."
+#endif
+
+/* ISO/IEC 9899:1999 7.23: Date and time <time.h>
+   The macro `CLOCKS_PER_SEC' is a constant 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. */
+#if defined __x86_64__ && defined __ILP32__
+# define CLOCKS_PER_SEC  1000000ll
+#else
+# define CLOCKS_PER_SEC  1000000l
+#endif
diff --git a/time/Makefile b/time/Makefile
index a411f62..da2ff6b 100644
--- a/time/Makefile
+++ b/time/Makefile
@@ -22,7 +22,7 @@  subdir	:= time
 
 include ../Makeconfig
 
-headers	:= time.h sys/time.h sys/timeb.h bits/time.h
+headers	:= time.h sys/time.h sys/timeb.h bits/time.h bits/time2.h
 
 routines := offtime asctime clock ctime ctime_r difftime \
 	    gmtime localtime mktime time		 \
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;
 }