Home | History | Annotate | Download | only in include
      1 /* HOST_WIDE_INT definitions for the GNU compiler.
      2    Copyright (C) 1998-2013 Free Software Foundation, Inc.
      3 
      4    This file is part of GCC.
      5 
      6    Provide definitions for macros which depend on HOST_BITS_PER_INT
      7    and HOST_BITS_PER_LONG.  */
      8 
      9 #ifndef GCC_HWINT_H
     10 #define GCC_HWINT_H
     11 
     12 /* This describes the machine the compiler is hosted on.  */
     13 #define HOST_BITS_PER_CHAR  CHAR_BIT
     14 #define HOST_BITS_PER_SHORT (CHAR_BIT * SIZEOF_SHORT)
     15 #define HOST_BITS_PER_INT   (CHAR_BIT * SIZEOF_INT)
     16 #define HOST_BITS_PER_LONG  (CHAR_BIT * SIZEOF_LONG)
     17 
     18 /* The string that should be inserted into a printf style format to
     19    indicate a "long" operand.  */
     20 #ifndef HOST_LONG_FORMAT
     21 #define HOST_LONG_FORMAT "l"
     22 #endif
     23 
     24 /* The string that should be inserted into a printf style format to
     25    indicate a "long long" operand.  */
     26 #ifndef HOST_LONG_LONG_FORMAT
     27 #define HOST_LONG_LONG_FORMAT "ll"
     28 #endif
     29 
     30 /* If HAVE_LONG_LONG and SIZEOF_LONG_LONG aren't defined, but
     31    GCC_VERSION >= 3000, assume this is the second or later stage of a
     32    bootstrap, we do have long long, and it's 64 bits.  (This is
     33    required by C99; we do have some ports that violate that assumption
     34    but they're all cross-compile-only.)  Just in case, force a
     35    constraint violation if that assumption is incorrect.  */
     36 #if !defined HAVE_LONG_LONG
     37 # if GCC_VERSION >= 3000
     38 #  define HAVE_LONG_LONG 1
     39 #  define SIZEOF_LONG_LONG 8
     40 extern char sizeof_long_long_must_be_8[sizeof(long long) == 8 ? 1 : -1];
     41 # endif
     42 #endif
     43 
     44 #ifdef HAVE_LONG_LONG
     45 # define HOST_BITS_PER_LONGLONG (CHAR_BIT * SIZEOF_LONG_LONG)
     46 #endif
     47 #ifdef HAVE___INT64
     48 # define HOST_BITS_PER___INT64 (CHAR_BIT * SIZEOF___INT64)
     49 #endif
     50 
     51 /* Set HOST_WIDE_INT.  This should be the widest efficient host
     52    integer type.  It can be 32 or 64 bits, except that if we are
     53    targeting a machine with 64-bit size_t then it has to be 64 bits.
     54 
     55    With a sane ABI, 'long' is the largest efficient host integer type.
     56    Thus, we use that unless we have to use 'long long' or '__int64'
     57    because we're targeting a 64-bit machine from a 32-bit host.  */
     58 
     59 #if HOST_BITS_PER_LONG >= 64 || !defined NEED_64BIT_HOST_WIDE_INT
     60 #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
     61 #   define HOST_WIDE_INT long
     62 #   define HOST_WIDE_INT_C(X) X ## L
     63 #else
     64 # if HOST_BITS_PER_LONGLONG >= 64
     65 #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONGLONG
     66 #   define HOST_WIDE_INT long long
     67 #   define HOST_WIDE_INT_C(X) X ## LL
     68 # else
     69 #  if HOST_BITS_PER___INT64 >= 64
     70 #   define HOST_BITS_PER_WIDE_INT HOST_BITS_PER___INT64
     71 #   define HOST_WIDE_INT __int64
     72 #   define HOST_WIDE_INT_C(X) X ## i64
     73 #  else
     74     #error "Unable to find a suitable type for HOST_WIDE_INT"
     75 #  endif
     76 # endif
     77 #endif
     78 
     79 #define HOST_WIDE_INT_1 HOST_WIDE_INT_C(1)
     80 
     81 /* This is a magic identifier which allows GCC to figure out the type
     82    of HOST_WIDE_INT for %wd specifier checks.  You must issue this
     83    typedef before using the __asm_fprintf__ format attribute.  */
     84 typedef HOST_WIDE_INT __gcc_host_wide_int__;
     85 
     86 /* Various printf format strings for HOST_WIDE_INT.  */
     87 
     88 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
     89 # define HOST_WIDE_INT_PRINT HOST_LONG_FORMAT
     90 # define HOST_WIDE_INT_PRINT_C "L"
     91   /* 'long' might be 32 or 64 bits, and the number of leading zeroes
     92      must be tweaked accordingly.  */
     93 # if HOST_BITS_PER_WIDE_INT == 64
     94 #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
     95      "0x%" HOST_LONG_FORMAT "x%016" HOST_LONG_FORMAT "x"
     96 # else
     97 #  define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
     98      "0x%" HOST_LONG_FORMAT "x%08" HOST_LONG_FORMAT "x"
     99 # endif
    100 #else
    101 # define HOST_WIDE_INT_PRINT HOST_LONG_LONG_FORMAT
    102 # define HOST_WIDE_INT_PRINT_C "LL"
    103   /* We can assume that 'long long' is at least 64 bits.  */
    104 # define HOST_WIDE_INT_PRINT_DOUBLE_HEX \
    105     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
    106 #endif /* HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG */
    107 
    108 #define HOST_WIDE_INT_PRINT_DEC "%" HOST_WIDE_INT_PRINT "d"
    109 #define HOST_WIDE_INT_PRINT_DEC_C HOST_WIDE_INT_PRINT_DEC HOST_WIDE_INT_PRINT_C
    110 #define HOST_WIDE_INT_PRINT_UNSIGNED "%" HOST_WIDE_INT_PRINT "u"
    111 #define HOST_WIDE_INT_PRINT_HEX "%#" HOST_WIDE_INT_PRINT "x"
    112 #define HOST_WIDE_INT_PRINT_HEX_PURE "%" HOST_WIDE_INT_PRINT "x"
    113 
    114 /* Set HOST_WIDEST_INT.  This is a 64-bit type unless the compiler
    115    in use has no 64-bit type at all; in that case it's 32 bits.  */
    116 
    117 #if HOST_BITS_PER_WIDE_INT >= 64 \
    118     || (HOST_BITS_PER_LONGLONG < 64 && HOST_BITS_PER___INT64 < 64)
    119 # define HOST_WIDEST_INT		      HOST_WIDE_INT
    120 # define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER_WIDE_INT
    121 # define HOST_WIDEST_INT_PRINT                HOST_WIDE_INT_PRINT
    122 # define HOST_WIDEST_INT_PRINT_DEC	      HOST_WIDE_INT_PRINT_DEC
    123 # define HOST_WIDEST_INT_PRINT_DEC_C	      HOST_WIDE_INT_PRINT_DEC_C
    124 # define HOST_WIDEST_INT_PRINT_UNSIGNED	      HOST_WIDE_INT_PRINT_UNSIGNED
    125 # define HOST_WIDEST_INT_PRINT_HEX	      HOST_WIDE_INT_PRINT_HEX
    126 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     HOST_WIDE_INT_PRINT_DOUBLE_HEX
    127 # define HOST_WIDEST_INT_C(X)		      HOST_WIDE_INT(X)
    128 #else
    129 # if HOST_BITS_PER_LONGLONG >= 64
    130 #  define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER_LONGLONG
    131 #  define HOST_WIDEST_INT		      long long
    132 #  define HOST_WIDEST_INT_C(X)		      X ## LL
    133 # else
    134 #  if HOST_BITS_PER___INT64 >= 64
    135 #   define HOST_BITS_PER_WIDEST_INT	      HOST_BITS_PER___INT64
    136 #   define HOST_WIDEST_INT		      __int64
    137 #   define HOST_WIDEST_INT_C(X)		      X ## i64
    138 #  else
    139     #error "This line should be impossible to reach"
    140 #  endif
    141 # endif
    142 # define HOST_WIDEST_INT_PRINT                HOST_LONG_LONG_FORMAT
    143 # define HOST_WIDEST_INT_PRINT_DEC	      "%" HOST_LONG_LONG_FORMAT "d"
    144 # define HOST_WIDEST_INT_PRINT_DEC_C	      "%" HOST_LONG_LONG_FORMAT "dLL"
    145 # define HOST_WIDEST_INT_PRINT_UNSIGNED	      "%" HOST_LONG_LONG_FORMAT "u"
    146 # define HOST_WIDEST_INT_PRINT_HEX	      "%#" HOST_LONG_LONG_FORMAT "x"
    147 # define HOST_WIDEST_INT_PRINT_DOUBLE_HEX     \
    148     "0x%" HOST_LONG_LONG_FORMAT "x%016" HOST_LONG_LONG_FORMAT "x"
    149 #endif
    150 
    151 /* Define HOST_WIDEST_FAST_INT to the widest integer type supported
    152    efficiently in hardware.  (That is, the widest integer type that fits
    153    in a hardware register.)  Normally this is "long" but on some hosts it
    154    should be "long long" or "__int64".  This is no convenient way to
    155    autodetect this, so such systems must set a flag in config.host; see there
    156    for details.  */
    157 
    158 #ifdef USE_LONG_LONG_FOR_WIDEST_FAST_INT
    159 #  ifdef HAVE_LONG_LONG
    160 #    define HOST_WIDEST_FAST_INT long long
    161 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONGLONG
    162 #  elif defined (HAVE___INT64)
    163 #    define HOST_WIDEST_FAST_INT __int64
    164 #    define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER___INT64
    165 #  else
    166 #    error "Your host said it wanted to use long long or __int64 but neither"
    167 #    error "exist"
    168 #  endif
    169 #else
    170 #  define HOST_WIDEST_FAST_INT long
    171 #  define HOST_BITS_PER_WIDEST_FAST_INT HOST_BITS_PER_LONG
    172 #endif
    173 
    174 /* Inline functions operating on HOST_WIDE_INT.  */
    175 #if GCC_VERSION < 3004
    176 
    177 extern int clz_hwi (unsigned HOST_WIDE_INT x);
    178 extern int ctz_hwi (unsigned HOST_WIDE_INT x);
    179 extern int ffs_hwi (unsigned HOST_WIDE_INT x);
    180 
    181 /* Return the number of set bits in X.  */
    182 extern int popcount_hwi (unsigned HOST_WIDE_INT x);
    183 
    184 /* Return log2, or -1 if not exact.  */
    185 extern int exact_log2                  (unsigned HOST_WIDE_INT);
    186 
    187 /* Return floor of log2, with -1 for zero.  */
    188 extern int floor_log2                  (unsigned HOST_WIDE_INT);
    189 
    190 /* Return the smallest n such that 2**n >= X.  */
    191 extern int ceil_log2			(unsigned HOST_WIDE_INT);
    192 
    193 #else /* GCC_VERSION >= 3004 */
    194 
    195 /* For convenience, define 0 -> word_size.  */
    196 static inline int
    197 clz_hwi (unsigned HOST_WIDE_INT x)
    198 {
    199   if (x == 0)
    200     return HOST_BITS_PER_WIDE_INT;
    201 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
    202   return __builtin_clzl (x);
    203 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
    204   return __builtin_clzll (x);
    205 # else
    206   return __builtin_clz (x);
    207 # endif
    208 }
    209 
    210 static inline int
    211 ctz_hwi (unsigned HOST_WIDE_INT x)
    212 {
    213   if (x == 0)
    214     return HOST_BITS_PER_WIDE_INT;
    215 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
    216   return __builtin_ctzl (x);
    217 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
    218   return __builtin_ctzll (x);
    219 # else
    220   return __builtin_ctz (x);
    221 # endif
    222 }
    223 
    224 static inline int
    225 ffs_hwi (unsigned HOST_WIDE_INT x)
    226 {
    227 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
    228   return __builtin_ffsl (x);
    229 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
    230   return __builtin_ffsll (x);
    231 # else
    232   return __builtin_ffs (x);
    233 # endif
    234 }
    235 
    236 static inline int
    237 popcount_hwi (unsigned HOST_WIDE_INT x)
    238 {
    239 # if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
    240   return __builtin_popcountl (x);
    241 # elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
    242   return __builtin_popcountll (x);
    243 # else
    244   return __builtin_popcount (x);
    245 # endif
    246 }
    247 
    248 static inline int
    249 floor_log2 (unsigned HOST_WIDE_INT x)
    250 {
    251   return HOST_BITS_PER_WIDE_INT - 1 - clz_hwi (x);
    252 }
    253 
    254 static inline int
    255 ceil_log2 (unsigned HOST_WIDE_INT x)
    256 {
    257   return floor_log2 (x - 1) + 1;
    258 }
    259 
    260 static inline int
    261 exact_log2 (unsigned HOST_WIDE_INT x)
    262 {
    263   return x == (x & -x) && x ? ctz_hwi (x) : -1;
    264 }
    265 
    266 #endif /* GCC_VERSION >= 3004 */
    267 
    268 #define HOST_WIDE_INT_MIN (HOST_WIDE_INT) \
    269   ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
    270 #define HOST_WIDE_INT_MAX (~(HOST_WIDE_INT_MIN))
    271 
    272 extern HOST_WIDE_INT abs_hwi (HOST_WIDE_INT);
    273 extern unsigned HOST_WIDE_INT absu_hwi (HOST_WIDE_INT);
    274 extern HOST_WIDE_INT gcd (HOST_WIDE_INT, HOST_WIDE_INT);
    275 extern HOST_WIDE_INT pos_mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
    276 extern HOST_WIDE_INT mul_hwi (HOST_WIDE_INT, HOST_WIDE_INT);
    277 extern HOST_WIDE_INT least_common_multiple (HOST_WIDE_INT, HOST_WIDE_INT);
    278 
    279 #endif /* ! GCC_HWINT_H */
    280