inttypes.h: do a single ersatz <inttypes.h> based on <limits.h>
authorH. Peter Anvin <hpa@zytor.com>
Thu, 21 Jun 2007 06:15:42 +0000 (06:15 +0000)
committerH. Peter Anvin <hpa@zytor.com>
Thu, 21 Jun 2007 06:15:42 +0000 (06:15 +0000)
inttypes/i16l32/inttypes.h [deleted file]
inttypes/i32l32/inttypes.h [deleted file]
inttypes/i32l64/inttypes.h [deleted file]
inttypes/inttypes.h [new file with mode: 0644]

diff --git a/inttypes/i16l32/inttypes.h b/inttypes/i16l32/inttypes.h
deleted file mode 100644 (file)
index c0151e8..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * i16l32/inttypes.h
- *
- * Small subset of <inttypes.h>
- * for the int 16, long 32, long long 64 model.
- */
-
-#ifndef INTTYPES_H
-#define INTTYPES_H
-
-typedef signed char            int8_t;
-typedef signed int             int16_t;
-typedef signed long            int32_t;
-typedef signed long long       int64_t;
-
-typedef unsigned char          uint8_t;
-typedef unsigned int           uint16_t;
-typedef unsigned long          uint32_t;
-typedef unsigned long long     uint64_t;
-
-#define _scn8  "hh"
-#define _scn16 ""
-#define _scn32 "l"
-#define _scn64 "ll"
-
-#define _pri8  ""
-#define _pri16 ""
-#define _pri32 "l"
-#define _pri64 "ll"
-
-#define _cst8
-#define _cst16
-#define _cst32 L
-#define _cst64 LL
-
-#define INT8_C(x)      x
-#define INT16_C(x)     x
-#define INT32_C(x)     x ## L
-#define INT64_C(x)     x ## LL
-
-#define UINT8_C(x)     x ## U
-#define UINT16_C(x)    x ## U
-#define UINT32_C(x)    x ## UL
-#define UINT64_C(x)    x ## ULL
-
-/* The rest of this is common to all models */
-
-#define PRId8          _pri8  "d"
-#define PRId16         _pri16 "d"
-#define PRId32         _pri32 "d"
-#define PRId64         _pri64 "d"
-
-#define PRIi8          _pri8  "i"
-#define PRIi16         _pri16 "i"
-#define PRIi32         _pri32 "i"
-#define PRIi64         _pri64 "i"
-
-#define PRIo8          _pri8  "o"
-#define PRIo16         _pri16 "o"
-#define PRIo32         _pri32 "o"
-#define PRIo64         _pri64 "o"
-
-#define PRIu8          _pri8  "u"
-#define PRIu16         _pri16 "u"
-#define PRIu32         _pri32 "u"
-#define PRIu64         _pri64 "u"
-
-#define PRIx8          _pri8  "x"
-#define PRIx16         _pri16 "x"
-#define PRIx32         _pri32 "x"
-#define PRIx64         _pri64 "x"
-
-#define PRIX8          _pri8  "X"
-#define PRIX16         _pri16 "X"
-#define PRIX32         _pri32 "X"
-#define PRIX64         _pri64 "X"
-
-#define SCNd8          _scn8  "d"
-#define SCNd16         _scn16 "d"
-#define SCNd32         _scn32 "d"
-#define SCNd64         _scn64 "d"
-
-#define SCNi8          _scn8  "i"
-#define SCNi16         _scn16 "i"
-#define SCNi32         _scn32 "i"
-#define SCNi64         _scn64 "i"
-
-#define SCNo8          _scn8  "o"
-#define SCNo16         _scn16 "o"
-#define SCNo32         _scn32 "o"
-#define SCNo64         _scn64 "o"
-
-#define SCNu8          _scn8  "u"
-#define SCNu16         _scn16 "u"
-#define SCNu32         _scn32 "u"
-#define SCNu64         _scn64 "u"
-
-#define SCNx8          _scn8  "x"
-#define SCNx16         _scn16 "x"
-#define SCNx32         _scn32 "x"
-#define SCNx64         _scn64 "x"
-
-#endif /* INTTYPES_H */
diff --git a/inttypes/i32l32/inttypes.h b/inttypes/i32l32/inttypes.h
deleted file mode 100644 (file)
index 034f085..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * i32l32/inttypes.h
- *
- * Small subset of <inttypes.h>
- * for the short 16, int 32, long long 64 model.
- */
-
-#ifndef INTTYPES_H
-#define INTTYPES_H
-
-typedef signed char            int8_t;
-typedef signed short           int16_t;
-typedef signed int             int32_t;
-typedef signed long long       int64_t;
-
-typedef unsigned char          uint8_t;
-typedef unsigned short         uint16_t;
-typedef unsigned int           uint32_t;
-typedef unsigned long long     uint64_t;
-
-#define _scn8  "hh"
-#define _scn16 "h"
-#define _scn32 ""
-#define _scn64 "ll"
-
-#define _pri8  ""
-#define _pri16 ""
-#define _pri32 ""
-#define _pri64 "ll"
-
-#define INT8_C(x)      x
-#define INT16_C(x)     x
-#define INT32_C(x)     x
-#define INT64_C(x)     x ## LL
-
-#define UINT8_C(x)     x ## U
-#define UINT16_C(x)    x ## U
-#define UINT32_C(x)    x ## U
-#define UINT64_C(x)    x ## ULL
-
-/* The rest of this is common to all models */
-
-#define PRId8          _pri8  "d"
-#define PRId16         _pri16 "d"
-#define PRId32         _pri32 "d"
-#define PRId64         _pri64 "d"
-
-#define PRIi8          _pri8  "i"
-#define PRIi16         _pri16 "i"
-#define PRIi32         _pri32 "i"
-#define PRIi64         _pri64 "i"
-
-#define PRIo8          _pri8  "o"
-#define PRIo16         _pri16 "o"
-#define PRIo32         _pri32 "o"
-#define PRIo64         _pri64 "o"
-
-#define PRIu8          _pri8  "u"
-#define PRIu16         _pri16 "u"
-#define PRIu32         _pri32 "u"
-#define PRIu64         _pri64 "u"
-
-#define PRIx8          _pri8  "x"
-#define PRIx16         _pri16 "x"
-#define PRIx32         _pri32 "x"
-#define PRIx64         _pri64 "x"
-
-#define PRIX8          _pri8  "X"
-#define PRIX16         _pri16 "X"
-#define PRIX32         _pri32 "X"
-#define PRIX64         _pri64 "X"
-
-#define SCNd8          _scn8  "d"
-#define SCNd16         _scn16 "d"
-#define SCNd32         _scn32 "d"
-#define SCNd64         _scn64 "d"
-
-#define SCNi8          _scn8  "i"
-#define SCNi16         _scn16 "i"
-#define SCNi32         _scn32 "i"
-#define SCNi64         _scn64 "i"
-
-#define SCNo8          _scn8  "o"
-#define SCNo16         _scn16 "o"
-#define SCNo32         _scn32 "o"
-#define SCNo64         _scn64 "o"
-
-#define SCNu8          _scn8  "u"
-#define SCNu16         _scn16 "u"
-#define SCNu32         _scn32 "u"
-#define SCNu64         _scn64 "u"
-
-#define SCNx8          _scn8  "x"
-#define SCNx16         _scn16 "x"
-#define SCNx32         _scn32 "x"
-#define SCNx64         _scn64 "x"
-
-#endif /* INTTYPES_H */
diff --git a/inttypes/i32l64/inttypes.h b/inttypes/i32l64/inttypes.h
deleted file mode 100644 (file)
index a0531c0..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * i32l64/inttypes.h
- *
- * Small subset of <inttypes.h>
- * for the short 16, int 32, long 64 model.
- */
-
-#ifndef INTTYPES_H
-#define INTTYPES_H
-
-typedef signed char            int8_t;
-typedef signed short           int16_t;
-typedef signed int             int32_t;
-typedef signed long            int64_t;
-
-typedef unsigned char          uint8_t;
-typedef unsigned short         uint16_t;
-typedef unsigned int           uint32_t;
-typedef unsigned long          uint64_t;
-
-#define _scn8  "hh"
-#define _scn16 "h"
-#define _scn32 ""
-#define _scn64 "l"
-
-#define _pri8  ""
-#define _pri16 ""
-#define _pri32 ""
-#define _pri64 "l"
-
-#define INT8_C(x)      x
-#define INT16_C(x)     x
-#define INT32_C(x)     x
-#define INT64_C(x)     x ## L
-
-#define UINT8_C(x)     x ## U
-#define UINT16_C(x)    x ## U
-#define UINT32_C(x)    x ## U
-#define UINT64_C(x)    x ## UL
-
-/* The rest of this is common to all models */
-
-#define PRId8          _pri8  "d"
-#define PRId16         _pri16 "d"
-#define PRId32         _pri32 "d"
-#define PRId64         _pri64 "d"
-
-#define PRIi8          _pri8  "i"
-#define PRIi16         _pri16 "i"
-#define PRIi32         _pri32 "i"
-#define PRIi64         _pri64 "i"
-
-#define PRIo8          _pri8  "o"
-#define PRIo16         _pri16 "o"
-#define PRIo32         _pri32 "o"
-#define PRIo64         _pri64 "o"
-
-#define PRIu8          _pri8  "u"
-#define PRIu16         _pri16 "u"
-#define PRIu32         _pri32 "u"
-#define PRIu64         _pri64 "u"
-
-#define PRIx8          _pri8  "x"
-#define PRIx16         _pri16 "x"
-#define PRIx32         _pri32 "x"
-#define PRIx64         _pri64 "x"
-
-#define PRIX8          _pri8  "X"
-#define PRIX16         _pri16 "X"
-#define PRIX32         _pri32 "X"
-#define PRIX64         _pri64 "X"
-
-#define SCNd8          _scn8  "d"
-#define SCNd16         _scn16 "d"
-#define SCNd32         _scn32 "d"
-#define SCNd64         _scn64 "d"
-
-#define SCNi8          _scn8  "i"
-#define SCNi16         _scn16 "i"
-#define SCNi32         _scn32 "i"
-#define SCNi64         _scn64 "i"
-
-#define SCNo8          _scn8  "o"
-#define SCNo16         _scn16 "o"
-#define SCNo32         _scn32 "o"
-#define SCNo64         _scn64 "o"
-
-#define SCNu8          _scn8  "u"
-#define SCNu16         _scn16 "u"
-#define SCNu32         _scn32 "u"
-#define SCNu64         _scn64 "u"
-
-#define SCNx8          _scn8  "x"
-#define SCNx16         _scn16 "x"
-#define SCNx32         _scn32 "x"
-#define SCNx64         _scn64 "x"
-
-#endif /* INTTYPES_H */
diff --git a/inttypes/inttypes.h b/inttypes/inttypes.h
new file mode 100644 (file)
index 0000000..8f1305f
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * inttypes.h
+ *
+ * Small ersatz subset of <inttypes.h>, deriving the types from
+ * <limits.h>.
+ *
+ * Important: the preprocessor may truncate numbers too large for it.
+ * Therefore, test the signed types only ... truncation won't generate
+ * a 01111111... bit pattern.
+ */
+
+#ifndef INTTYPES_H
+#define INTTYPES_H
+
+#include <limits.h>
+
+/*** 64-bit type: long or long long ***/
+
+/* Some old versions of gcc <limits.h> omit LLONG_MAX */
+#ifndef LLONG_MAX
+# ifdef __LONG_LONG_MAX__
+#  define LLONG_MAX __LONG_LONG_MAX__
+# else
+#  define LLONG_MAX 0          /* Assume long long is unusable */
+# endif
+#endif
+
+#if LONG_MAX == 9223372036854775807
+
+/* long is 64 bits */
+typedef signed long            int64_t;
+typedef unsigned long          uint64_t;
+#define _scn64                 "l"
+#define _pri64                 "l"
+#define INT64_C(x)             x ## L
+#define UINT64_C(x)            x ## UL
+
+#elif LLONG_MAX == 9223372036854775807
+
+/* long long is 64 bits */
+typedef signed long long       int64_t;
+typedef unsigned long long     uint64_t;
+#define _scn64                 "ll"
+#define _pri64                 "ll"
+#define INT64_C(x)             x ## LL
+#define UINT64_C(x)            x ## ULL
+
+#else
+
+#error "Neither long nor long long is 64 bits in size"
+
+#endif
+
+/*** 32-bit type: int or long ***/
+
+#if INT_MAX == 2147483647
+
+/* int is 32 bits */
+typedef signed int             int32_t;
+typedef unsigned int           uint32_t;
+#define _scn32                 ""
+#define _pri32                 ""
+#define INT32_C(x)             x
+#define UINT32_C(x)            x ## U
+
+#elif LONG_MAX == 2147483647
+
+/* long is 32 bits */
+typedef signed long            int32_t;
+typedef unsigned long          uint32_t;
+#define _scn32                 "l"
+#define _pri32                 "l"
+#define INT32_C(x)             x ## L
+#define UINT32_C(x)            x ## UL
+
+#else
+
+#error "Neither int nor long is 32 bits in size"
+
+#endif
+
+/*** 16-bit size: int or short ***/
+
+#if INT_MAX == 32767
+
+/* int is 16 bits */
+typedef signed int             int16_t;
+typedef unsigned int           uint16_t;
+#define _scn16                 ""
+#define _pri16                 ""
+#define INT16_C(x)             x
+#define UINT16_C(x)            x ## U
+
+#elif SHORT_MAX == 32767
+
+/* short is 16 bits */
+typedef signed short           int16_t;
+typedef unsigned short         uint16_t;
+#define _scn16                 "h"
+#define _pri16                 ""
+#define INT16_C(x)             x
+#define UINT16_C(x)            x ## U
+
+#else
+
+#error "Neither short nor int is 16 bits in size"
+
+#endif
+
+/*** 8-bit size: char ***/
+
+#if SCHAR_MAX == 127
+
+/* char is 8 bits */
+typedef signed char            int8_t;
+typedef unsigned char          uint8_t;
+#define _scn8                  "hh"
+#define _pri8                  ""
+#define INT8_C(x)              x
+#define UINT8_C(x)             x ## U
+
+#else
+
+#error "char is not 8 bits in size"
+
+#endif
+
+/* The rest of this is common to all models */
+
+#define PRId8          _pri8  "d"
+#define PRId16         _pri16 "d"
+#define PRId32         _pri32 "d"
+#define PRId64         _pri64 "d"
+
+#define PRIi8          _pri8  "i"
+#define PRIi16         _pri16 "i"
+#define PRIi32         _pri32 "i"
+#define PRIi64         _pri64 "i"
+
+#define PRIo8          _pri8  "o"
+#define PRIo16         _pri16 "o"
+#define PRIo32         _pri32 "o"
+#define PRIo64         _pri64 "o"
+
+#define PRIu8          _pri8  "u"
+#define PRIu16         _pri16 "u"
+#define PRIu32         _pri32 "u"
+#define PRIu64         _pri64 "u"
+
+#define PRIx8          _pri8  "x"
+#define PRIx16         _pri16 "x"
+#define PRIx32         _pri32 "x"
+#define PRIx64         _pri64 "x"
+
+#define PRIX8          _pri8  "X"
+#define PRIX16         _pri16 "X"
+#define PRIX32         _pri32 "X"
+#define PRIX64         _pri64 "X"
+
+#define SCNd8          _scn8  "d"
+#define SCNd16         _scn16 "d"
+#define SCNd32         _scn32 "d"
+#define SCNd64         _scn64 "d"
+
+#define SCNi8          _scn8  "i"
+#define SCNi16         _scn16 "i"
+#define SCNi32         _scn32 "i"
+#define SCNi64         _scn64 "i"
+
+#define SCNo8          _scn8  "o"
+#define SCNo16         _scn16 "o"
+#define SCNo32         _scn32 "o"
+#define SCNo64         _scn64 "o"
+
+#define SCNu8          _scn8  "u"
+#define SCNu16         _scn16 "u"
+#define SCNu32         _scn32 "u"
+#define SCNu64         _scn64 "u"
+
+#define SCNx8          _scn8  "x"
+#define SCNx16         _scn16 "x"
+#define SCNx32         _scn32 "x"
+#define SCNx64         _scn64 "x"
+
+#endif /* INTTYPES_H */