mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Jens Gustedt <Jens.Gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: [musl] [C23 128 bit 4/4] C23: implement proper support for int128_t and uint128_t
Date: Wed, 31 May 2023 16:15:50 +0200	[thread overview]
Message-ID: <1c8e850ed3190af39b9e3f501d79899d438e7292.1685536608.git.Jens.Gustedt@inria.fr> (raw)
In-Reply-To: <cover.1685536608.git.Jens.Gustedt@inria.fr>

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


  parent reply	other threads:[~2023-05-31 14:16 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-31 14:15 [musl] [C23 128 bit 0/4] implement the library part of 128 bit support Jens Gustedt
2023-05-31 14:15 ` [musl] [C23 128 bit 1/4] add an emulation for 128 bit arithmetic as needed for C library support Jens Gustedt
2023-05-31 14:15 ` [musl] [C23 128 bit 2/4] C23: implement w128 and wf128 support for printf Jens Gustedt
2023-05-31 14:15 ` [musl] [C23 128 bit 3/4] C23: implement w128 and wf128 for scanf and similar Jens Gustedt
2023-05-31 14:15 ` Jens Gustedt [this message]
2023-05-31 14:27   ` [musl] [C23 128 bit 4/4] C23: implement proper support for int128_t and uint128_t Rich Felker
2023-05-31 14:29     ` Rich Felker
2023-05-31 14:36     ` Jₑₙₛ Gustedt
2023-05-31 14:41       ` Rich Felker
2023-05-31 14:55         ` Jₑₙₛ Gustedt
2023-05-31 14:57           ` Rich Felker
2023-05-31 15:07             ` Jₑₙₛ Gustedt
2023-05-31 15:14               ` Rich Felker
2023-05-31 15:37                 ` Jₑₙₛ Gustedt
2023-05-31 15:40                   ` Rich Felker
2023-05-31 15:56                     ` Jₑₙₛ Gustedt
2023-05-31 16:30                       ` Alexander Monakov
2023-05-31 16:58                         ` Jens Gustedt
2023-05-31 17:03                           ` Rich Felker
2023-05-31 17:09                           ` Alexander Monakov
2023-06-01  7:24                             ` Jₑₙₛ Gustedt
2023-05-31 14:42     ` Jₑₙₛ Gustedt
2023-05-31 14:47       ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1c8e850ed3190af39b9e3f501d79899d438e7292.1685536608.git.Jens.Gustedt@inria.fr \
    --to=jens.gustedt@inria.fr \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).