comparison lib/intprops.h @ 5694:11672ccd9cf8

Add TYPE_ONES_COMPLEMENT and TYPE_SIGNED_MAGNITUDE, and use less-tricky TYPE_MINIMUM and TYPE_MAXIMUM.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 09 Mar 2005 23:22:50 +0000
parents ec62790f0938
children 8b60929a61ff
comparison
equal deleted inserted replaced
5693:9084a7fabcb1 5694:11672ccd9cf8
25 25
26 /* True if the arithmetic type T is an integer type. bool counts as 26 /* True if the arithmetic type T is an integer type. bool counts as
27 an integer. */ 27 an integer. */
28 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) 28 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
29 29
30 /* True if negative values of the integer type T use twos complement 30 /* True if negative values of the signed integer type T use twos
31 representation. */ 31 complement, ones complement, or signed magnitude representation,
32 #define TYPE_TWOS_COMPLEMENT(t) ((t) - (t) 1 == (t) ((t) ~ (t) 1 + (t) 1)) 32 respectively. Much GNU code assumes twos complement, but some
33 people like to be portable to all possible C hosts. */
34 #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
35 #define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
36 #define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
33 37
34 /* True if the arithmetic type T is signed. */ 38 /* True if the arithmetic type T is signed. */
35 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 39 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
36 40
37 /* The maximum and minimum values for the integer type T. These 41 /* The maximum and minimum values for the integer type T. These
38 macros have undefined behavior if T is signed and has padding bits 42 macros have undefined behavior if T is signed and has padding bits.
39 (i.e., bits that do not contribute to the value), or if T uses 43 If this is a problem for you, please let us know how to fix it for
40 signed-magnitude representation. If this is a problem for you, 44 your host. */
41 please let us know how to fix it for your host. */ 45 #define TYPE_MINIMUM(t) \
42 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ 46 ((t) (! TYPE_SIGNED (t) \
43 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0)) 47 ? (t) 0 \
44 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t))) 48 : TYPE_SIGNED_MAGNITUDE (t) \
49 ? ~ (t) 0 \
50 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
51 #define TYPE_MAXIMUM(t) \
52 ((t) (! TYPE_SIGNED (t) \
53 ? (t) -1 \
54 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
45 55
46 /* Bound on length of the string representing an integer value or type T. 56 /* Bound on length of the string representing an integer value or type T.
47 Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485; 57 Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
48 add 1 for integer division truncation; add 1 more for a minus sign 58 add 1 for integer division truncation; add 1 more for a minus sign
49 if needed. */ 59 if needed. */