kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

stdint.h (6627B)


      1 /* stdint.h -- C11 7.20 -- Integer types of specified widths
      2  *
      3  * Given kit's assumptions (CHAR_BIT == 8, two's complement, int == 32-bit,
      4  * long long == 64-bit), every exact-width and minimum-width *limit* is
      5  * fixed and hardcoded below. The underlying *type aliases* still vary
      6  * (e.g. int32_t may be `int` or `long` depending on the data model),
      7  * so types remain compiler-supplied.
      8  *
      9  * `intptr_t`, `intmax_t`, and the FAST family genuinely vary across
     10  * targets and stay delegated.
     11  */
     12 #ifndef KIT_STDINT_H
     13 #define KIT_STDINT_H
     14 
     15 /* ------------------------------------------------------------------ */
     16 /* 7.20.1.1  Exact-width integer types (aliases vary by data model)   */
     17 /* ------------------------------------------------------------------ */
     18 typedef __INT8_TYPE__ int8_t;
     19 typedef __INT16_TYPE__ int16_t;
     20 typedef __INT32_TYPE__ int32_t;
     21 typedef __INT64_TYPE__ int64_t;
     22 
     23 typedef __UINT8_TYPE__ uint8_t;
     24 typedef __UINT16_TYPE__ uint16_t;
     25 typedef __UINT32_TYPE__ uint32_t;
     26 typedef __UINT64_TYPE__ uint64_t;
     27 
     28 /* ------------------------------------------------------------------ */
     29 /* 7.20.1.2  Minimum-width integer types                              */
     30 /* ------------------------------------------------------------------ */
     31 typedef __INT_LEAST8_TYPE__ int_least8_t;
     32 typedef __INT_LEAST16_TYPE__ int_least16_t;
     33 typedef __INT_LEAST32_TYPE__ int_least32_t;
     34 typedef __INT_LEAST64_TYPE__ int_least64_t;
     35 
     36 typedef __UINT_LEAST8_TYPE__ uint_least8_t;
     37 typedef __UINT_LEAST16_TYPE__ uint_least16_t;
     38 typedef __UINT_LEAST32_TYPE__ uint_least32_t;
     39 typedef __UINT_LEAST64_TYPE__ uint_least64_t;
     40 
     41 /* ------------------------------------------------------------------ */
     42 /* 7.20.1.3  Fastest minimum-width integer types (vary by target)     */
     43 /* ------------------------------------------------------------------ */
     44 typedef __INT_FAST8_TYPE__ int_fast8_t;
     45 typedef __INT_FAST16_TYPE__ int_fast16_t;
     46 typedef __INT_FAST32_TYPE__ int_fast32_t;
     47 typedef __INT_FAST64_TYPE__ int_fast64_t;
     48 
     49 typedef __UINT_FAST8_TYPE__ uint_fast8_t;
     50 typedef __UINT_FAST16_TYPE__ uint_fast16_t;
     51 typedef __UINT_FAST32_TYPE__ uint_fast32_t;
     52 typedef __UINT_FAST64_TYPE__ uint_fast64_t;
     53 
     54 /* ------------------------------------------------------------------ */
     55 /* 7.20.1.4  Pointer-holding integer types (32 or 64 bit)             */
     56 /* ------------------------------------------------------------------ */
     57 typedef __INTPTR_TYPE__ intptr_t;
     58 typedef __UINTPTR_TYPE__ uintptr_t;
     59 
     60 /* ------------------------------------------------------------------ */
     61 /* 7.20.1.5  Greatest-width integer types                             */
     62 /* ------------------------------------------------------------------ */
     63 typedef __INTMAX_TYPE__ intmax_t;
     64 typedef __UINTMAX_TYPE__ uintmax_t;
     65 
     66 /* ------------------------------------------------------------------ */
     67 /* 7.20.2  Limits of specified-width integer types                    */
     68 /* ------------------------------------------------------------------ */
     69 
     70 /* exact-width -- two's complement, 8-bit byte */
     71 #define INT8_MAX 127
     72 #define INT16_MAX 32767
     73 #define INT32_MAX 2147483647
     74 #define INT64_MAX 9223372036854775807LL
     75 #define INT8_MIN (-128)
     76 #define INT16_MIN (-32768)
     77 #define INT32_MIN (-INT32_MAX - 1)
     78 #define INT64_MIN (-INT64_MAX - 1LL)
     79 #define UINT8_MAX 255
     80 #define UINT16_MAX 65535
     81 #define UINT32_MAX 4294967295U
     82 #define UINT64_MAX 18446744073709551615ULL
     83 
     84 /* minimum-width -- equal to exact-width on every modern target */
     85 #define INT_LEAST8_MAX INT8_MAX
     86 #define INT_LEAST16_MAX INT16_MAX
     87 #define INT_LEAST32_MAX INT32_MAX
     88 #define INT_LEAST64_MAX INT64_MAX
     89 #define INT_LEAST8_MIN INT8_MIN
     90 #define INT_LEAST16_MIN INT16_MIN
     91 #define INT_LEAST32_MIN INT32_MIN
     92 #define INT_LEAST64_MIN INT64_MIN
     93 #define UINT_LEAST8_MAX UINT8_MAX
     94 #define UINT_LEAST16_MAX UINT16_MAX
     95 #define UINT_LEAST32_MAX UINT32_MAX
     96 #define UINT_LEAST64_MAX UINT64_MAX
     97 
     98 /* fastest minimum-width -- the underlying type is target-dependent
     99    (often `int` for FAST{8,16}_t on 32/64-bit machines), so the limits
    100    stay compiler-supplied. */
    101 #define INT_FAST8_MAX __INT_FAST8_MAX__
    102 #define INT_FAST16_MAX __INT_FAST16_MAX__
    103 #define INT_FAST32_MAX __INT_FAST32_MAX__
    104 #define INT_FAST64_MAX __INT_FAST64_MAX__
    105 #define INT_FAST8_MIN (-INT_FAST8_MAX - 1)
    106 #define INT_FAST16_MIN (-INT_FAST16_MAX - 1)
    107 #define INT_FAST32_MIN (-INT_FAST32_MAX - 1)
    108 #define INT_FAST64_MIN (-INT_FAST64_MAX - 1)
    109 #define UINT_FAST8_MAX __UINT_FAST8_MAX__
    110 #define UINT_FAST16_MAX __UINT_FAST16_MAX__
    111 #define UINT_FAST32_MAX __UINT_FAST32_MAX__
    112 #define UINT_FAST64_MAX __UINT_FAST64_MAX__
    113 
    114 /* pointer-holding */
    115 #define INTPTR_MAX __INTPTR_MAX__
    116 #define INTPTR_MIN (-INTPTR_MAX - 1)
    117 #define UINTPTR_MAX __UINTPTR_MAX__
    118 
    119 /* greatest-width -- intmax_t is `long` on LP64 and `long long` on LLP64,
    120    so the value is fixed at 2^63-1 but the literal type is not. Keep the
    121    compiler's macro to preserve the right type. */
    122 #define INTMAX_MAX __INTMAX_MAX__
    123 #define INTMAX_MIN (-INTMAX_MAX - 1)
    124 #define UINTMAX_MAX __UINTMAX_MAX__
    125 
    126 /* ------------------------------------------------------------------ */
    127 /* 7.20.3  Limits of other integer types                              */
    128 /* ------------------------------------------------------------------ */
    129 #define PTRDIFF_MAX __PTRDIFF_MAX__
    130 #define PTRDIFF_MIN (-PTRDIFF_MAX - 1)
    131 
    132 #define SIZE_MAX __SIZE_MAX__
    133 
    134 #define WCHAR_MAX __WCHAR_MAX__
    135 #define WCHAR_MIN __WCHAR_MIN__
    136 
    137 #define WINT_MAX __WINT_MAX__
    138 #define WINT_MIN __WINT_MIN__
    139 
    140 #define SIG_ATOMIC_MAX __SIG_ATOMIC_MAX__
    141 #define SIG_ATOMIC_MIN __SIG_ATOMIC_MIN__
    142 
    143 /* ------------------------------------------------------------------ */
    144 /* 7.20.4  Macros for integer constants                               */
    145 /* ------------------------------------------------------------------ */
    146 
    147 /* Widths up to 32 fit in `int`, so a bare literal has the right type. */
    148 #define INT8_C(c) (c)
    149 #define INT16_C(c) (c)
    150 #define INT32_C(c) (c)
    151 #define UINT8_C(c) (c)
    152 #define UINT16_C(c) (c)
    153 #define UINT32_C(c) (c##U)
    154 
    155 /* 64-bit literal suffix is L on LP64 and LL on LLP64; intmax_t same.
    156    All supported compilers (clang 14+, GCC 8+) predefine the __*_C_SUFFIX__
    157    tokens, which is the portable approach that works across all data models
    158    without relying on __INT64_C/__UINT64_C function-like macros (those are
    159    inconsistently predefined across compiler versions and targets). */
    160 #define __KIT_PASTE(a, b) a##b
    161 #define __KIT_XPASTE(a, b) __KIT_PASTE(a, b)
    162 #define INT64_C(c)  __KIT_XPASTE(c, __INT64_C_SUFFIX__)
    163 #define UINT64_C(c) __KIT_XPASTE(c, __UINT64_C_SUFFIX__)
    164 #define INTMAX_C(c)  __KIT_XPASTE(c, __INTMAX_C_SUFFIX__)
    165 #define UINTMAX_C(c) __KIT_XPASTE(c, __UINTMAX_C_SUFFIX__)
    166 
    167 #endif