Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1c8e850ed3190af39b9e3f501d79899d438e7292.1685536608.git.Jens.Gustedt@inria.fr>
Date: Wed, 31 May 2023 16:15:50 +0200
From: Jens Gustedt <Jens.Gustedt@...ia.fr>
To: musl@...ts.openwall.com
Subject: [C23 128 bit 4/4] C23: implement proper support for int128_t and uint128_t

By implementing support for w128 length specifiers for printf and
scanf, the only final bit that we need to be able to support these
types officially as extended integer type and map them to the
fixed-width types are the integer literals. In C23, the new types
`_IntBit(N)` and their literals are mandatory. Most commonly they will
support N >= 128. The maximum value for N can be queried with
`BITINT_MAXWIDTH`. If we have such literals, use them to construct the
constants for `[U]INT128_C` and provide the rest of the macros for
these types.
---
 include/inttypes.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++
 include/stdint.h   | 37 +++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/include/inttypes.h b/include/inttypes.h
index 780c2bb0..0d81029b 100644
--- a/include/inttypes.h
+++ b/include/inttypes.h
@@ -272,6 +272,61 @@ uintmax_t wcstoumax(const wchar_t *__restrict, wchar_t **__restrict, int);
 #define SCNuPTR __PRIPTR "u"
 #define SCNxPTR __PRIPTR "x"
 
+#if __has_int128_t
+#define __PRILEAST128 "w128"
+
+#define PRIdLEAST128 __PRILEAST128 "d"
+#define PRIiLEAST128 __PRILEAST128 "i"
+#define PRIoLEAST128 __PRILEAST128 "o"
+#define PRIuLEAST128 __PRILEAST128 "u"
+#define PRIxLEAST128 __PRILEAST128 "x"
+#define PRIXLEAST128 __PRILEAST128 "X"
+#define PRIbLEAST128 __PRILEAST128 "b"
+#define PRIBLEAST128 __PRILEAST128 "B"
+
+#define SCNbLEAST128 __PRILEAST128 "b"
+#define SCNdLEAST128 __PRILEAST128 "d"
+#define SCNiLEAST128 __PRILEAST128 "i"
+#define SCNoLEAST128 __PRILEAST128 "o"
+#define SCNuLEAST128 __PRILEAST128 "u"
+#define SCNxLEAST128 __PRILEAST128 "x"
+
+#define PRId128 PRIdLEAST128
+#define PRIi128 PRIiLEAST128
+#define PRIo128 PRIoLEAST128
+#define PRIu128 PRIuLEAST128
+#define PRIx128 PRIxLEAST128
+#define PRIX128 PRIXLEAST128
+#define PRIb128 PRIbLEAST128
+#define PRIB128 PRIBLEAST128
+
+#define SCNb128 SCNbLEAST128
+#define SCNd128 SCNdLEAST128
+#define SCNi128 SCNiLEAST128
+#define SCNo128 SCNoLEAST128
+#define SCNu128 SCNuLEAST128
+#define SCNx128 SCNxLEAST128
+
+#define __PRIFAST128 "wf128"
+
+#define PRIdFAST128 __PRIFAST128 "d"
+#define PRIiFAST128 __PRIFAST128 "i"
+#define PRIoFAST128 __PRIFAST128 "o"
+#define PRIuFAST128 __PRIFAST128 "u"
+#define PRIxFAST128 __PRIFAST128 "x"
+#define PRIXFAST128 __PRIFAST128 "X"
+#define PRIbFAST128 __PRIFAST128 "b"
+#define PRIBFAST128 __PRIFAST128 "B"
+
+#define SCNbFAST128 __PRIFAST128 "b"
+#define SCNdFAST128 __PRIFAST128 "d"
+#define SCNiFAST128 __PRIFAST128 "i"
+#define SCNoFAST128 __PRIFAST128 "o"
+#define SCNuFAST128 __PRIFAST128 "u"
+#define SCNxFAST128 __PRIFAST128 "x"
+
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/stdint.h b/include/stdint.h
index 1f68b3bb..8dda9991 100644
--- a/include/stdint.h
+++ b/include/stdint.h
@@ -187,4 +187,41 @@ typedef uint64_t uint_least64_t;
 #define BITINT_MAXWIDTH __BITINT_MAXWIDTH__
 #endif
 
+#if __has_int128_t
+typedef __int128 int128_t;
+typedef int128_t int_fast128_t;
+typedef int128_t int_least128_t;
+
+typedef unsigned __int128 uint128_t;
+typedef uint128_t uint_fast128_t;
+typedef uint128_t uint_least128_t;
+
+#define INT128_WIDTH  128
+#define INT_LEAST128_WIDTH  128
+#define INT_FAST128_WIDTH  128
+#define UINT128_WIDTH  128
+#define UINT_LEAST128_WIDTH  128
+#define UINT_FAST128_WIDTH  128
+
+// This uses the new integer constants for _BitInt(N) types for the
+// 128 bit type and is the only thing that we need in addition to the
+// types themselves to enable 128 bit support officially. Usually this
+// will not be fit for preprocessor arithmetic, but we should do an
+// effort to make this possible.
+#define INT128_C(X)  ((int128_t)+(X ## wb))
+#define UINT128_C(X)  ((uint128_t)+(X ## wbu))
+
+#define INT128_MAX  INT128_C(0x7fffffffffffffffffffffffffffffff)
+#define INT128_MIN  (-1-INT128_MAX)
+#define UINT128_MAX UINT128_C(0xffffffffffffffffffffffffffffffff)
+
+#define INT_LEAST128_MAX INT128_MAX
+#define INT_LEAST128_MIN INT128_MIN
+#define INT_FAST128_MAX INT128_MAX
+#define INT_FAST128_MIN INT128_MIN
+#define UINT_LEAST128_MAX UINT128_MAX
+#define UINT_FAST128_MAX UINT128_MAX
+
+#endif
+
 #endif
-- 
2.34.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.