Factor int-properties macros into a single file, except for
[gnulib.git] / lib / intprops.h
1 /* intprops.h -- properties of integer types
2
3    Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #include <limits.h>
22
23 /* The extra casts in the following macros work around compiler bugs,
24    e.g., in Cray C 5.0.3.0.  */
25
26 /* True if the arithmetic type T is an integer type.  bool counts as
27    an integer.  */
28 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
29
30 /* True if negative values of the integer type T use twos complement
31    representation.  */
32 #define TYPE_TWOS_COMPLEMENT(t) ((t) - (t) 1 == (t) ((t) ~ (t) 1 + (t) 1))
33
34 /* True if the arithmetic type T is signed.  */
35 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
36
37 /* The maximum and minimum values for the integer type T.  These
38    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
40    signed-magnitude representation.  If this is a problem for you,
41    please let us know how to fix it for your host.  */
42 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
43                               ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
44 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
45
46 /* 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;
48    add 1 for integer division truncation; add 1 more for a minus sign
49    if needed.  */
50 #define INT_STRLEN_BOUND(t) \
51   ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
52
53 /* Bound on buffer size needed to represent an integer value or type T,
54    including the terminating null.  */
55 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)