summaryrefslogtreecommitdiff
path: root/lib/intprops.h
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2005-03-09 23:22:14 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2005-03-09 23:22:14 +0000
commit4d645f30537183fd2b994f7e73f0351f10c34148 (patch)
tree15a04819add07a1d3d4ca005603cfa9039341bdb /lib/intprops.h
parentf0f31b512fbe26d985014c11cee12d04ea5fb3ee (diff)
downloadcoreutils-4d645f30537183fd2b994f7e73f0351f10c34148.tar.xz
Add TYPE_ONES_COMPLEMENT and TYPE_SIGNED_MAGNITUDE, and use
less-tricky TYPE_MINIMUM and TYPE_MAXIMUM.
Diffstat (limited to 'lib/intprops.h')
-rw-r--r--lib/intprops.h30
1 files changed, 20 insertions, 10 deletions
diff --git a/lib/intprops.h b/lib/intprops.h
index cd1c17212..eb79ac22d 100644
--- a/lib/intprops.h
+++ b/lib/intprops.h
@@ -27,21 +27,31 @@
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 negative values of the signed integer type T use twos
+ complement, ones complement, or signed magnitude representation,
+ respectively. Much GNU code assumes twos complement, but some
+ people like to be portable to all possible C hosts. */
+#define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
+#define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
+#define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (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)))
+ macros have undefined behavior if T is signed and has padding bits.
+ 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 \
+ : TYPE_SIGNED_MAGNITUDE (t) \
+ ? ~ (t) 0 \
+ : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
+#define TYPE_MAXIMUM(t) \
+ ((t) (! TYPE_SIGNED (t) \
+ ? (t) -1 \
+ : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
/* 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;