[PATCH] tools/nolibc: Add stdint.h

Vincent Dagonneau posted 1 patch 2 years, 7 months ago
There is a newer version of this series
tools/include/nolibc/std.h    | 15 +------
tools/include/nolibc/stdint.h | 77 +++++++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 14 deletions(-)
create mode 100644 tools/include/nolibc/stdint.h
[PATCH] tools/nolibc: Add stdint.h
Posted by Vincent Dagonneau 2 years, 7 months ago
Hi,

I was testing out nolibc with some simple programs originally linked
against glibc. I noticed a quick fix for some of them was to have an
stdint.h with limits macros.

Here are the changes I made. I've got a couple more ideas for fleshing
out nolibc, let me know if you are interested in contributions.

Add stdint.h and moved the relevant definitions from std.h. Also added
macros for limits and *_least_* types.

---
 tools/include/nolibc/std.h    | 15 +------
 tools/include/nolibc/stdint.h | 77 +++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 14 deletions(-)
 create mode 100644 tools/include/nolibc/stdint.h

diff --git a/tools/include/nolibc/std.h b/tools/include/nolibc/std.h
index 1747ae125392..c65ddf2e4db1 100644
--- a/tools/include/nolibc/std.h
+++ b/tools/include/nolibc/std.h
@@ -18,20 +18,7 @@
 #define NULL ((void *)0)
 #endif
 
-/* stdint types */
-typedef unsigned char       uint8_t;
-typedef   signed char        int8_t;
-typedef unsigned short     uint16_t;
-typedef   signed short      int16_t;
-typedef unsigned int       uint32_t;
-typedef   signed int        int32_t;
-typedef unsigned long long uint64_t;
-typedef   signed long long  int64_t;
-typedef unsigned long        size_t;
-typedef   signed long       ssize_t;
-typedef unsigned long     uintptr_t;
-typedef   signed long      intptr_t;
-typedef   signed long     ptrdiff_t;
+#include <stdint.h>
 
 /* those are commonly provided by sys/types.h */
 typedef unsigned int          dev_t;
diff --git a/tools/include/nolibc/stdint.h b/tools/include/nolibc/stdint.h
new file mode 100644
index 000000000000..1f11fffe2119
--- /dev/null
+++ b/tools/include/nolibc/stdint.h
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * Standard definitions and types for NOLIBC
+ * Copyright (C) 2023 Vincent Dagonneau <v@vda.io>
+ */
+
+#ifndef _NOLIBC_STDINT_H
+#define _NOLIBC_STDINT_H
+
+typedef unsigned char       uint8_t;
+typedef   signed char        int8_t;
+typedef unsigned short     uint16_t;
+typedef   signed short      int16_t;
+typedef unsigned int       uint32_t;
+typedef   signed int        int32_t;
+typedef unsigned long long uint64_t;
+typedef   signed long long  int64_t;
+typedef unsigned long        size_t;
+typedef   signed long       ssize_t;
+typedef unsigned long     uintptr_t;
+typedef   signed long      intptr_t;
+typedef   signed long     ptrdiff_t;
+
+typedef   int8_t       int_least8_t;
+typedef  uint8_t      uint_least8_t;
+typedef  int16_t      int_least16_t;
+typedef uint16_t     uint_least16_t;
+typedef  int32_t      int_least32_t;
+typedef uint32_t     uint_least32_t;
+typedef  int64_t      int_least64_t;
+typedef uint64_t     uint_least64_t;
+
+typedef  int64_t           intmax_t;
+typedef uint64_t          uintmax_t;
+
+/* limits of integral types */
+
+#define        INT8_MIN  (-128)
+#define       INT16_MIN  (-32767-1)
+#define       INT32_MIN  (-2147483647-1)
+#define       INT64_MIN  (-9223372036854775807-1)
+
+#define        INT8_MAX  (127)
+#define       INT16_MAX  (32767)
+#define       INT32_MAX  (2147483647)
+#define       INT64_MAX  (9223372036854775807)
+
+#define       UINT8_MAX  (255)
+#define      UINT16_MAX  (65535)
+#define      UINT32_MAX  (4294967295U)
+#define      UINT64_MAX  (18446744073709551615)
+
+#define  INT_LEAST8_MIN  (-128)
+#define INT_LEAST16_MIN  (-32767-1)
+#define INT_LEAST32_MIN  (-2147483647-1)
+#define INT_LEAST64_MIN  (-9223372036854775807-1)
+
+#define  INT_LEAST8_MAX  (127)
+#define INT_LEAST16_MAX  (32767)
+#define INT_LEAST32_MAX  (2147483647)
+#define INT_LEAST64_MAX  (9223372036854775807)
+
+#define  UINT_LEAST8_MAX  (255)
+#define UINT_LEAST16_MAX  (65535)
+#define UINT_LEAST32_MAX  (4294967295U)
+#define UINT_LEAST64_MAX  (18446744073709551615)
+
+#define         SIZE_MAX  (18446744073709551615UL)
+
+#define       INTPTR_MIN  (-9223372036854775807L-1)
+#define       INTPTR_MAX  (9223372036854775807L)
+#define      UINTPTR_MAX  (18446744073709551615UL)
+
+#define      PTRDIFF_MIN  (-9223372036854775807L-1)
+#define      PTRDIFF_MAX  (9223372036854775807L)
+
+#endif /* _NOLIBC_STDINT_H */
-- 
2.39.1
Re: [PATCH] tools/nolibc: Add stdint.h
Posted by Thomas Weißschuh 2 years, 7 months ago
On Thu, Feb 02, 2023 at 11:02:37AM -0500, Vincent Dagonneau wrote:
> Hi,
> 
> I was testing out nolibc with some simple programs originally linked
> against glibc. I noticed a quick fix for some of them was to have an
> stdint.h with limits macros.
> 
> Here are the changes I made. I've got a couple more ideas for fleshing
> out nolibc, let me know if you are interested in contributions.
> 
> Add stdint.h and moved the relevant definitions from std.h. Also added
> macros for limits and *_least_* types.
> 
> ---
>  tools/include/nolibc/std.h    | 15 +------
>  tools/include/nolibc/stdint.h | 77 +++++++++++++++++++++++++++++++++++
>  2 files changed, 78 insertions(+), 14 deletions(-)
>  create mode 100644 tools/include/nolibc/stdint.h
> 
> diff --git a/tools/include/nolibc/std.h b/tools/include/nolibc/std.h
> index 1747ae125392..c65ddf2e4db1 100644
> --- a/tools/include/nolibc/std.h
> +++ b/tools/include/nolibc/std.h
> @@ -18,20 +18,7 @@
>  #define NULL ((void *)0)
>  #endif
>  
> -/* stdint types */
> -typedef unsigned char       uint8_t;
> -typedef   signed char        int8_t;
> -typedef unsigned short     uint16_t;
> -typedef   signed short      int16_t;
> -typedef unsigned int       uint32_t;
> -typedef   signed int        int32_t;
> -typedef unsigned long long uint64_t;
> -typedef   signed long long  int64_t;
> -typedef unsigned long        size_t;
> -typedef   signed long       ssize_t;
> -typedef unsigned long     uintptr_t;
> -typedef   signed long      intptr_t;
> -typedef   signed long     ptrdiff_t;
> +#include <stdint.h>

Shouldn't this be

#include "stdint.h"?

>  /* those are commonly provided by sys/types.h */
>  typedef unsigned int          dev_t;
> diff --git a/tools/include/nolibc/stdint.h b/tools/include/nolibc/stdint.h
> new file mode 100644
> index 000000000000..1f11fffe2119
> --- /dev/null
> +++ b/tools/include/nolibc/stdint.h
> @@ -0,0 +1,77 @@
> +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
> +/*
> + * Standard definitions and types for NOLIBC
> + * Copyright (C) 2023 Vincent Dagonneau <v@vda.io>
> + */
> +
> +#ifndef _NOLIBC_STDINT_H
> +#define _NOLIBC_STDINT_H
> +
> +typedef unsigned char       uint8_t;
> +typedef   signed char        int8_t;
> +typedef unsigned short     uint16_t;
> +typedef   signed short      int16_t;
> +typedef unsigned int       uint32_t;
> +typedef   signed int        int32_t;
> +typedef unsigned long long uint64_t;
> +typedef   signed long long  int64_t;
> +typedef unsigned long        size_t;
> +typedef   signed long       ssize_t;
> +typedef unsigned long     uintptr_t;
> +typedef   signed long      intptr_t;
> +typedef   signed long     ptrdiff_t;
> +
> +typedef   int8_t       int_least8_t;
> +typedef  uint8_t      uint_least8_t;
> +typedef  int16_t      int_least16_t;
> +typedef uint16_t     uint_least16_t;
> +typedef  int32_t      int_least32_t;
> +typedef uint32_t     uint_least32_t;
> +typedef  int64_t      int_least64_t;
> +typedef uint64_t     uint_least64_t;
> +
> +typedef  int64_t           intmax_t;
> +typedef uint64_t          uintmax_t;
> +
> +/* limits of integral types */
> +
> +#define        INT8_MIN  (-128)
> +#define       INT16_MIN  (-32767-1)
> +#define       INT32_MIN  (-2147483647-1)
> +#define       INT64_MIN  (-9223372036854775807-1)
> +
> +#define        INT8_MAX  (127)
> +#define       INT16_MAX  (32767)
> +#define       INT32_MAX  (2147483647)
> +#define       INT64_MAX  (9223372036854775807)
> +
> +#define       UINT8_MAX  (255)
> +#define      UINT16_MAX  (65535)
> +#define      UINT32_MAX  (4294967295U)
> +#define      UINT64_MAX  (18446744073709551615)
> +
> +#define  INT_LEAST8_MIN  (-128)
> +#define INT_LEAST16_MIN  (-32767-1)
> +#define INT_LEAST32_MIN  (-2147483647-1)
> +#define INT_LEAST64_MIN  (-9223372036854775807-1)

As int_least8_t is a typedef to int8_t, you could use

#define INT_LEAST8_MIN INT8_MIN

etc.

> +#define  INT_LEAST8_MAX  (127)
> +#define INT_LEAST16_MAX  (32767)
> +#define INT_LEAST32_MAX  (2147483647)
> +#define INT_LEAST64_MAX  (9223372036854775807)
> +
> +#define  UINT_LEAST8_MAX  (255)
> +#define UINT_LEAST16_MAX  (65535)
> +#define UINT_LEAST32_MAX  (4294967295U)
> +#define UINT_LEAST64_MAX  (18446744073709551615)
> +
> +#define         SIZE_MAX  (18446744073709551615UL)
> +
> +#define       INTPTR_MIN  (-9223372036854775807L-1)
> +#define       INTPTR_MAX  (9223372036854775807L)
> +#define      UINTPTR_MAX  (18446744073709551615UL)
> +
> +#define      PTRDIFF_MIN  (-9223372036854775807L-1)
> +#define      PTRDIFF_MAX  (9223372036854775807L)

The above break on 32 bit.

> +#endif /* _NOLIBC_STDINT_H */
> -- 
> 2.39.1
>