factored integer properties into intprops.h

2005-03-09 Thread Paul Eggert
I factored out various places in gnulib that refer to integer
properties like TYPE_MAXIMUM and INT_STRLEN_BOUND, and put them
into a common file intprops.h.  I installed this patch into
gnulib and will do the same to coreutils shortly.

The only developer-visible change should be the new file lib/intprops.h.

I used 146/485 by popular request.  However, I didn't have
INT_STRLEN_BOUND refer to TYPE_SIGNED, as that broke some code in
coreutils that applied INT_STRLEN_BOUND to a value, not to a type.  I
think this usage is useful enough that we should continue to support
it, so I documented it.

2005-03-09  Paul Eggert  [EMAIL PROTECTED]

Factor int-properties macros into a single file, except for
glibc-related files.
* lib/intprops.h: New file.
* lib/getloadavg.c: Include it instead of limits.h.
(INT_STRLEN_BOUND): Remove.
* lib/human.c: Include intprops.h.
(group_number): Use INT_STRLEN_BOUND instead of rolling it ourself.
* lib/human.h (LONGEST_HUMAN_READABLE): Use 146/485 rather than 
302/1000.
* lib/inttostr.h: Include intprops.h instead of limits.h.
(INT_STRLEN_BOUND, INT_BUFSIZE_BOUND): Remove.
* lib/mktime.c (TYPE_IS_INTEGER, TYPE_TWOS_COMPLEMENT): New macros,
for consistency with intprops.h.
(time_t_is_integer, twos_complement_arithmetic): Use them.
* lib/sig2str.h: Include signal.h, intprops.h.
(INT_STRLEN_BOUND): Remove.
* lib/strftime.c (TYPE_SIGNED): Remove.
(INT_STRLEN_BOUND): Switch to same implementation as intprops.h.
* lib/strtol.c: Adjust comments to match intprops.h.
* lib/userspec.c: Include intprops.h.
(TYPE_SIGNED, TYPE_MINIMUM, TYPE_MAXIMUM): Remove.
* lib/utimecmp.c, lib/xnanosleep.c, lib/xstrtol.c: Likewise.
* lib/utimecmp.c (utimecmp): Use TYPE_IS_INTEGER, TYPE_TWOS_COMPLEMENT
instead of rolling our own expressions.
* lib/xstrtol.c: Include xstrtol.h first, to test interface.

* modules/getloadavg (Files): Add lib/intprops.h.
* modules/human (Files): Likewise.
* modules/inttostr (Files): Likewise.
* modules/sig2str (Files): Likewise.
* modules/userspec (Files): Likewise.
* modules/utimecmp (Files): Likewise.
* modules/xnanosleep (Files): Likewise.
* modules/xstrtol (Files): Likewise.

--- /dev/null   2003-03-18 13:55:57 -0800
+++ lib/intprops.h  2005-03-09 10:46:40 -0800
@@ -0,0 +1,55 @@
+/* intprops.h -- properties of integer types
+
+   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include limits.h
+
+/* The extra casts in the following macros work around compiler bugs,
+   e.g., in Cray C 5.0.3.0.  */
+
+/* True if the arithmetic type T is an integer type.  bool counts as
+   an integer.  */
+#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
+
+/* True if negative values of the integer type T use twos complement
+   representation.  */
+#define TYPE_TWOS_COMPLEMENT(t) ((t) - (t) 1 == (t) ((t) ~ (t) 1 + (t) 1))
+
+/* True if the arithmetic type T is signed.  */
+#define TYPE_SIGNED(t) (! ((t) 0  (t) -1))
+
+/* The maximum and minimum values for the integer type T.  These
+   macros have undefined behavior if T is signed and has padding bits
+   (i.e., bits that do not contribute to the value), or if T uses
+   signed-magnitude representation.  If this is a problem for you,
+   please let us know how to fix it for your host.  */
+#define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
+ ? ~ (t) 0  (sizeof (t) * CHAR_BIT - 1) : (t) 0))
+#define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
+
+/* Bound on length of the string representing an integer value or type T.
+   Subtract 1 for the sign bit if t is signed; log10 (2.0)  146/485;
+   add 1 for integer division truncation; add 1 more for a minus sign
+   if needed.  */
+#define INT_STRLEN_BOUND(t) \
+  ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
+
+/* Bound on buffer size needed to represent an integer value or type T,
+   including the terminating null.  */
+#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
Index: lib/getloadavg.c

Re: factored integer properties into intprops.h

2005-03-09 Thread Jim Meyering
Paul Eggert [EMAIL PROTECTED] wrote:
 I factored out various places in gnulib that refer to integer
 properties like TYPE_MAXIMUM and INT_STRLEN_BOUND, and put them
 into a common file intprops.h.  I installed this patch into
 gnulib and will do the same to coreutils shortly.

 The only developer-visible change should be the new file lib/intprops.h.

 I used 146/485 by popular request.  However, I didn't have
 INT_STRLEN_BOUND refer to TYPE_SIGNED, as that broke some code in
 coreutils that applied INT_STRLEN_BOUND to a value, not to a type.  I
 think this usage is useful enough that we should continue to support
 it, so I documented it.

Thanks for all of that.
I've made the following additional changes (only in coreutils for now),
any one of which would have been enough to ensure that intprops.h is
included in the coreutils distribution:

2005-03-10  Jim Meyering  [EMAIL PROTECTED]

* human.m4 (gl_HUMAN): Add intprops.h.
* inttostr.m4 (gl_INTTOSTR): Likewise
* sig2str.m4 (gl_FUNC_SIG2STR): Likewise.
* userspec.m4 (gl_USERSPEC): Likewise.
* utimecmp.m4 (gl_UTIMECMP): Likewise.
* xnanosleep.m4 (gl_XNANOSLEEP): Likewise.

* xstrtol.m4 (gl_XSTRTOL): Use AC_LIBSOURCES and AC_LIBOBJ
to list the required files.

Index: m4/human.m4
===
RCS file: /fetish/cu/m4/human.m4,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -p -u -r1.6 -r1.7
--- m4/human.m4 29 Jan 2005 00:16:39 -  1.6
+++ m4/human.m4 9 Mar 2005 23:02:25 -   1.7
@@ -1,4 +1,4 @@
-# human.m4 serial 8
+#serial 9
 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,7 +6,7 @@ dnl with or without modifications, as lo
 
 AC_DEFUN([gl_HUMAN],
 [
-  AC_LIBSOURCES([human.c, human.h])
+  AC_LIBSOURCES([human.c, human.h, intprops.h])
   AC_LIBOBJ([human])
 
   dnl Prerequisites of lib/human.h.
Index: m4/inttostr.m4
===
RCS file: /fetish/cu/m4/inttostr.m4,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -p -u -r1.4 -r1.5
--- m4/inttostr.m4  11 Feb 2005 20:27:16 -  1.4
+++ m4/inttostr.m4  9 Mar 2005 23:03:42 -   1.5
@@ -1,4 +1,4 @@
-# inttostr.m4 serial 4
+#serial 5
 dnl Copyright (C) 2004, 2005 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,7 +6,7 @@ dnl with or without modifications, as lo
 
 AC_DEFUN([gl_INTTOSTR],
 [
-  AC_LIBSOURCES([inttostr.c, inttostr.h])
+  AC_LIBSOURCES([inttostr.c, inttostr.h, intprops.h])
 
   dnl We don't technically need to list the following .c files, since their
   dnl functions are named in the AC_LIBOBJ calls, but this is an unusual
Index: m4/sig2str.m4
===
RCS file: /fetish/cu/m4/sig2str.m4,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -p -u -r1.3 -r1.4
--- m4/sig2str.m4   29 Jan 2005 00:16:39 -  1.3
+++ m4/sig2str.m4   9 Mar 2005 23:04:47 -   1.4
@@ -1,4 +1,4 @@
-# sig2str.m4 serial 3
+#serial 4
 dnl Copyright (C) 2002, 2005 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,7 +6,7 @@ dnl with or without modifications, as lo
 
 AC_DEFUN([gl_FUNC_SIG2STR],
 [
-  AC_LIBSOURCES([sig2str.c, sig2str.h])
+  AC_LIBSOURCES([sig2str.c, sig2str.h, intprops.h])
 
   AC_REPLACE_FUNCS(sig2str)
   if test $ac_cv_func_sig2str = no; then
Index: m4/userspec.m4
===
RCS file: /fetish/cu/m4/userspec.m4,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -p -u -r1.5 -r1.6
--- m4/userspec.m4  29 Jan 2005 00:16:39 -  1.5
+++ m4/userspec.m4  9 Mar 2005 23:05:32 -   1.6
@@ -1,4 +1,4 @@
-# userspec.m4 serial 6
+#serial 7
 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,7 +6,7 @@ dnl with or without modifications, as lo
 
 AC_DEFUN([gl_USERSPEC],
 [
-  AC_LIBSOURCES([userspec.c, userspec.h])
+  AC_LIBSOURCES([userspec.c, userspec.h, intprops.h])
   AC_LIBOBJ([userspec])
 
   dnl Prerequisites of lib/userspec.c.
Index: m4/utimecmp.m4
===
RCS file: /fetish/cu/m4/utimecmp.m4,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -p -u -r1.3 -r1.4
--- m4/utimecmp.m4  29 Jan 2005 00:16:39 -  1.3
+++ m4/utimecmp.m4  9 Mar 2005 23:06:16 -   1.4
@@ -1,3 +1,4 @@
+#serial 1
 dnl Copyright (C) 2004, 2005 Free Software