diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-08-29 01:00:28 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-08-29 01:00:28 +0200 |
commit | faca20fb8baa5971caedfd1e582106e3d79172fa (patch) | |
tree | 0356871ac466f31efeb3c2ad32eae7738d02185d | |
parent | add stddef.h (diff) | |
download | slibc-faca20fb8baa5971caedfd1e582106e3d79172fa.tar.gz slibc-faca20fb8baa5971caedfd1e582106e3d79172fa.tar.bz2 slibc-faca20fb8baa5971caedfd1e582106e3d79172fa.tar.xz |
m + add stdint
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | include/bits/intconf.h | 47 | ||||
-rw-r--r-- | include/bits/types.h | 154 | ||||
-rw-r--r-- | include/slibc/version.h | 128 | ||||
-rw-r--r-- | include/stddef.h | 1 | ||||
-rw-r--r-- | include/stdint.h | 422 |
6 files changed, 753 insertions, 1 deletions
@@ -1,5 +1,5 @@ slibc, the Stockholm C Library, is an implementation -of a C libray just for fun. +of a C libray, for POSIX, just for fun. slibc does not aim to support C++, or any other language than C. diff --git a/include/bits/intconf.h b/include/bits/intconf.h new file mode 100644 index 0000000..f76c999 --- /dev/null +++ b/include/bits/intconf.h @@ -0,0 +1,47 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef _BITS_INTCONF_H +#define _BITS_INTCONF_H + + +#define __MAX_TO_MIN(max) (-(max) - 1) +#define __CHAR_BIT 8 +#define __SHORT_BIT 16 +#define __INT_BIT 32 +#define __LONG_BIT 64 +#define __LONG_LONG_BIT 64 +#define __INT8 char +#define __INT16 short int +#define __INT32 int +#define __INT64 long int +#define __INT_FAST8 __INT8 +#define __INT_FAST16 __INT64 +#define __INT_FAST32 __INT64 +#define __INT_FAST64 __INT64 +#define __INT_FAST8_MAX INT8_MAX +#define __INT_FAST16_MAX INT64_MAX +#define __INT_FAST32_MAX INT64_MAX +#define __INT_FAST64_MAX INT64_MAX +#define __UINT_FAST8_MAX UINT8_MAX +#define __UINT_FAST16_MAX UINT64_MAX +#define __UINT_FAST32_MAX UINT64_MAX +#define __UINT_FAST64_MAX UINT64_MAX + + +#endif + diff --git a/include/bits/types.h b/include/bits/types.h index 1fffa0c..618cb98 100644 --- a/include/bits/types.h +++ b/include/bits/types.h @@ -21,6 +21,19 @@ * order headers are included. */ +#include <bits/intconf.h> + + +/** + * Returns the maximum value of an unsigned integer type, + * in that type. + */ +#ifndef __MAX_OF +# define __MAX_OF(type) \ + (((type)1) << sizeof(type)) +#define + + /** * Signed integer type of the result of subtracting two pointers. * May not be greater than the with of type long. @@ -54,6 +67,16 @@ typedef unsigned long int uptrdiff_t; #if defined(__NEED_wchar_t) && !defined(__DEFINED_wchar_t) # define __DEFINED_wchar_t typedef long int wchar_t; +# define __WCHAR_MAX INTPTR_MAX +#endif + + +/** + * Variant of `wchar_t` cna can hold the value of `WEOF`. + */ +#if defined(__NEED_wint_t) && !defined(__DEFINED_wint_t) +# define __DEFINED_wint_t +typedef wchar_t wint_t; #endif @@ -90,3 +113,134 @@ typedef struct } max_align_t; #endif + +/** + * Signed exact-width integer types. + * + * These types are guaranteed to use two's complement. + */ +#if defined(__NEED_intN_t) && !defined(__DEFINED_intN_t) +# define __DEFINED_intN_t +typedef signed __INT8 int8_t; +typedef signed __INT16 int16_t; +typedef signed __INT32 int32_t; +typedef signed __INT64 int64_t; +#endif + + +/** + * Unsigned exact-width integer types. + */ +#if defined(__NEED_uintN_t) && !defined(__DEFINED_uintN_t) +# define __DEFINED_uintN_t +typedef unsigned __INT8 uint8_t; +typedef unsigned __INT16 uint16_t; +typedef unsigned __INT32 uint32_t; +typedef unsigned __INT64 uint64_t; +#endif + + +/** + * Signed integer types with guaranteed minimum length + * with otherwise minimal length. + * + * `int_least8_t`, `int_least16_t`, `int_least32_t`, and + * `int_least64_t` are guaranteed to be defined. + */ +#if defined(__NEED_int_leastN_t) && !defined(__DEFINED_int_leastN_t) +# define __DEFINED_int_leastN_t +typedef signed __INT8 int_least8_t; +typedef signed __INT16 int_least16_t; +typedef signed __INT32 int_least32_t; +typedef signed __INT64 int_least64_t; +#endif + + +/** + * Unsigned integer types with guaranteed minimum length + * with otherwise minimal length. + * + * `uint_least8_t`, `uint_least16_t`, `uint_least32_t`, and + * `uint_least64_t` are guaranteed to be defined. + */ +#if defined(__NEED_uint_leastN_t) && !defined(__DEFINED_uint_leastN_t) +# define __DEFINED_uint_leastN_t +typedef unsigned __INT8 uint_least8_t; +typedef unsigned __INT16 uint_least16_t; +typedef unsigned __INT32 uint_least32_t; +typedef unsigned __INT64 uint_least64_t; +#endif + + +/** + * Signed integer types with guaranteed minimum length + * and are thought to be the fastest to in most use-cases. + * + * `int_fast8_t`, `int_fast16_t`, `int_fast32_t`, and + * `int_fast64_t` are guaranteed to be defined. + * It is howevr strongly discouraged to uses this types + * in serialised/marshalled data, as they may depend + * on the C library the program is compiled against + * and the version of that library. + */ +#if defined(__NEED_int_fastN_t) && !defined(__DEFINED_int_fastN_t) +# define __DEFINED_int_fastN_t +typedef signed __INT_FAST8 int_fast8_t; +typedef signed __INT_FAST16 int_fast16_t; +typedef signed __INT_FAST32 int_fast32_t; +typedef signed __INT_FAST64 int_fast64_t; +#endif + + +/** + * Ubsigned integer types with guaranteed minimum length + * and are thought to be the fastest to in most use-cases. + * + * `uint_fast8_t`, `uint_fast16_t`, `uint_fast32_t`, and + * `uint_fast64_t` are guaranteed to be defined. + * It is howevr strongly discouraged to uses this types + * in serialised/marshalled data, as they may depend + * on the C library the program is compiled against + * and the version of that library. + */ +#if defined(__NEED_uint_fastN_t) && !defined(__DEFINED_uint_fastN_t) +# define __DEFINED_uint_fastN_t +typedef unsigned __INT_FAST8 uint_fast8_t; +typedef unsigned __INT_FAST16 uint_fast16_t; +typedef unsigned __INT_FAST32 uint_fast32_t; +typedef unsigned __INT_FAST64 uint_fast64_t; +#endif + + +/** + * The widest signed integer type available. + */ +#if defined(__NEED_intmax_t) && !defined(__DEFINED_intmax_t) +# define __DEFINED_intmax_t +typedef signed __INT64 intmax_t; +# define __INTMAX_MAX INT64_MAX +#endif + + +/** + * The widest unsigned integer type available. + */ +#if defined(__NEED_uintmax_t) && !defined(__DEFINED_uintmax_t) +# define __DEFINED_uintmax_t +typedef unsigned __INT64 uintmax_t; +# define __UINTMAX_MAX UINT64_MAX +#endif + + +/** + * A integer type over which operations are atomic. + * It may be defined as volatile, slibc does not do + * this however because it is good practice to do + * so explcitily when using the `sig_atomic_t` type. + */ +#if defined(__NEED_sig_atomic_t) && !defined(__DEFINED_sig_atomic_t) +# define __DEFINED_sig_atomic_t +typedef int sig_atomic_t; +# define __SIG_ATOMIC_BIT __INT_BIT +#endif + diff --git a/include/slibc/version.h b/include/slibc/version.h new file mode 100644 index 0000000..00eebba --- /dev/null +++ b/include/slibc/version.h @@ -0,0 +1,128 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef _SLIBC_VERSION_H +#define _SLIBC_VERSION_H + + +/* Any header file is in the top-level of the header direction, + * and is provided by slibc, will include this header file. + * That way you do not need to include an slibc specific header + * to find out whether you are using slibc. */ + + + +/** + * The major-number in the version of slibc that + * you are compiling against. + * + * The version is formatted `<major>.<minor>.<micro>.<patch>`, + * however `.<patch>` is omitted if the patch-number is nought, + * `.<micro>` is omitted if the both the patch- and micro-number + * are nought. + */ +#define __SLIBC_MAJOR__ 0 + +/** + * The minor-number in the version of slibc that + * you are compiling against. + */ +#define __SLIBC_MINOR__ 1 + +/** + * The micro-number in the version of slibc that + * you are compiling against. + */ +#define __SLIBC_MICRO__ 0 + +/** + * The patch-number in the version of slibc that + * you are compiling against. + */ +#define __SLIBC_PATCH__ 0 + + + +/** + * This function is used internal for comparing + * slibc versions. + * + * @param major The major-number of the version. + * @param minor The minor-number of the version. + * @param micro The micro-number of the version. + * @param patch The patch-number of the version. + * @return A scalar integer describing the version. + */ +#define __SLIBC_CONSTRUCT_VERSION(major, minor, micro, patch) \ + (((major ## L) << (3 * 7)) | \ + ((minor ## L) << (2 * 7)) | \ + ((micro ## L) << (1 * 7)) | \ + ((patch ## L) << (0 * 7))) + + +/** + * The scalar integer describing the version + * of slibc you are compiling against. + * + * You can check if this header is defined to + * check whether you are using slibc at all. + */ +#define __SLIBC__ \ + __SLIBC_CONSTRUCT_VERSION(__SLIBC_MAJOR__, __SLIBC_MINOR__, \ + __SLIBC_MICRO__, __SLIBC_PATCH__) + + +/** + * Test whether you are compiling against a + * specific version of slibc, or against any + * newer version of slibc. + * + * @param major The major-number of the least required version of slibc. + * @param minor The minor-number of the least required version of slibc. + * @param micro The micro-number of the least required version of slibc. + * @param patch The patch-number of the least required version of slibc. + */ +#define __SLIBC_REQ_4__(major, minor, micro, patch) \ + (__SLIBC__ >= __SLIBC_CONSTRUCT_VERSION(major, minor, micro, patch)) + +/** + * Test whether you are compiling against a + * specific version of slibc, or against any + * newer version of slibc. + * + * @param major The major-number of the least required version of slibc. + * @param minor The minor-number of the least required version of slibc. + * @param micro The micro-number of the least required version of slibc. + */ +#define __SLIBC_REQ_3__(major, minor, micro) \ + __SLIBC_REQ_4__(major, minor, micro, patch) + +/** + * Test whether you are compiling against a + * specific version of slibc, or against any + * newer version of slibc. + * + * @param major The major-number of the least required version of slibc. + * @param minor The minor-number of the least required version of slibc. + */ +#define __SLIBC_REQ__(major, minor) \ + __SLIBC_REQ_4__(major, minor, 0, 0) + + + +#endif + diff --git a/include/stddef.h b/include/stddef.h index b8ad4ea..8a0ce5d 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -17,6 +17,7 @@ */ #ifndef _STDDEF_H #define _STDDEF_H +#include <slibc/version.h> diff --git a/include/stdint.h b/include/stdint.h new file mode 100644 index 0000000..3486de3 --- /dev/null +++ b/include/stdint.h @@ -0,0 +1,422 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef _STDINT_H +#define _STDINT_H +#include <slibc/version.h> + + + +#define __NEED_intN_t +#define __NEED_uintN_t +#define __NEED_int_leastN_t +#define __NEED_uint_leastN_t +#define __NEED_int_fastN_t +#define __NEED_uint_fastN_t +#define __NEED_intptr_t +#define __NEED_uintptr_t +#define __NEED_intmax_t +#define __NEED_uintmax_t + +#include <bits/types.h> + + + +/** + * Macro that ensure that a signed numeral literal + * of at most 8 encoding bits does not overflow. + */ +#define INT8_C(value) value + +/** + * Macro that ensure that an unsigned numeral literal + * of at most 8 encoding bits does not overflow. + */ +#define UINT8_C(value) value + + +/** + * Macro that ensure that a signed numeral literal + * of at most 16 encoding bits does not overflow. + */ +#define INT16_C(value) value + +/** + * Macro that ensure that an unsigned numeral literal + * of at most 16 encoding bits does not overflow. + */ +#define UINT16_C(value) value # U + + +/** + * Macro that ensure that a signed numeral literal + * of at most 32 encoding bits does not overflow. + */ +#if __INT_BITS == 16 +# define INT32_C(value) value # L +#else +# define INT32_C(value) value +#end + +/** + * Macro that ensure that an unsigned numeral literal + * of at most 32 encoding bits does not overflow. + */ +#if __INT_BITS == 16 +# define UINT32_C(value) value # UL +#else +# define UINT32_C(value) value # U +#end + + +/** + * Macro that ensure that a signed numeral literal + * of at most 64 encoding bits does not overflow. + */ +#if __LONG_BITS == 32 +# define INT64_C(value) value # LL +#else +# define INT64_C(value) value # L +#end + +/** + * Macro that ensure that an unsigned numeral literal + * of at most 64 encoding bits does not overflow. + */ +#if __LONG_BITS == 32 +# define UINT64_C(value) value # ULL +#else +# define UINT64_C(value) value # UL +#end + + +/** + * Macro that ensure that any signed numeral literal + * does not overflow, unless there is no scalar integer + * type that can hold it. + */ +#if __LONG_BITS == __LONG_LONG_BITS +# define INTMAX_C(value) value # L +#else +# define INTMAX_C(value) value # LL +#endif + +/** + * Macro that ensure that any unsigned numeral literal + * does not overflow, unless there is no scalar integer + * type that can hold it. + */ +#if __LONG_BITS == __LONG_LONG_BITS +# define UINTMAX_C(value) value # UL +#else +# define UINTMAX_C(value) value # ULL +#endif + + + +/** + * Maximum value of `uint8_t`. + */ +#define UINT8_MAX (UINT8_C(0xFF)) + +/** + * Maximum value of `int8_t`. + */ +#define INT8_MAX (INT8_C(0x7F)) + +/** + * Minimum value of `int8_t`. + */ +#define INT8_MIN (-(INT8_MAX) - 1) + + +/** + * Maximum value of `uint16_t`. + */ +#define UINT16_MAX (UINT16_C(0xFFFF)) + +/** + * Maximum value of `int16_t`. + */ +#define INT16_MAX (INT16_C(0x7FFF)) + +/** + * Minimum value of `int16_t`. + */ +#define INT16_MIN (-(INT16_MAX) - 1) + + +/** + * Maximum value of `uint32_t`. + */ +#define UINT32_MAX (UINT32_C(0xFFFFFFFF)) + +/** + * Maximum value of `int32_t`. + */ +#define INT32_MAX (INT32_C(0x7FFFFFFF)) + +/** + * Minimum value of `int32_t`. + */ +#define INT32_MIN (-(INT32_MAX) - 1) + + +/** + * Maximum value of `uint64_t`. + */ +#define UINT64_MAX (UINT64_C(0xFFFFFFFFFFFFFFFF)) + +/** + * Maximum value of `int64_t`. + */ +#define INT64_MAX (INT64_C(0x7FFFFFFFFFFFFFFF)) + +/** + * Minimum value of `int64_t`. + */ +#define INT64_MIN (-(INT64_MAX) - 1) + + +/** + * Maximum value of `uint_least8_t`. + */ +#define UINT_LEAST8_MAX UINT8_MAX + +/** + * Maximum value of `int_least8_t`. + */ +#define INT_LEAST8_MAX INT8_MAX + +/** + * Minimum value of `int_least8_t`. + */ +#define INT_LEAST8_MIN (__MAX_TO_MIN(INT_LEAST8_MAX)) + + +/** + * Maximum value of `uint_least16_t`. + */ +#define UINT_LEAST16_MAX UINT16_MAX + +/** + * Maximum value of `int_least16_t`. + */ +#define INT_LEAST16_MAX INT16_MAX + +/** + * Minimum value of `int_least16_t`. + */ +#define INT_LEAST16_MIN (__MAX_TO_MIN(INT_LEAST16_MAX)) + + +/** + * Maximum value of `uint_least32_t`. + */ +#define UINT_LEAST32_MAX UINT32_MAX + +/** + * Maximum value of `int_least32_t`. + */ +#define INT_LEAST32_MAX INT32_MAX + +/** + * Minimum value of `int_least32_t`. + */ +#define INT_LEAST32_MIN (__MAX_TO_MIN(INT_LEAST32_MAX)) + + +/** + * Maximum value of `uint_least64_t`. + */ +#define UINT_LEAST64_MAX UINT64_MAX + +/** + * Maximum value of `int_least64_t`. + */ +#define INT_LEAST64_MAX INT64_MAX + +/** + * Minimum value of `int_least64_t`. + */ +#define INT_LEAST64_MIN (__MAX_TO_MIN(INT_LEAST64_MAX)) + + +/** + * Maximum value of `uint_fast8_t`. + */ +#define UINT_FAST8_MAX __UINT_FAST8_MAX + +/** + * Maximum value of `int_fast8_t`. + */ +#define INT_FAST8_MAX __INT_FAST8_MAX + +/** + * Minimum value of `int_fast8_t`. + */ +#define INT_FAST8_MIN (__MAX_TO_MIN(INT_FAST8_MAX)) + + +/** + * Maximum value of `uint_fast16_t`. + */ +#define UINT_FAST16_MAX __UINT_FAST16_MAX + +/** + * Maximum value of `int_fast16_t`. + */ +#define INT_FAST16_MAX __INT_FAST16_MAX + +/** + * Minimum value of `int_fast16_t`. + */ +#define INT_FAST16_MIN (__MAX_TO_MIN(INT_FAST16_MAX)) + + +/** + * Maximum value of `uint_fast32_t`. + */ +#define UINT_FAST32_MAX __UINT_FAST32_MAX + +/** + * Maximum value of `int_fast32_t`. + */ +#define INT_FAST32_MAX __INT_FAST32_MAX + +/** + * Minimum value of `int_fast32_t`. + */ +#define INT_FAST32_MIN (__MAX_TO_MIN(INT_FAST32_MAX)) + + +/** + * Maximum value of `uint_fast64_t`. + */ +#define UINT_FAST64_MAX __UINT_FAST64_MAX + +/** + * Maximum value of `int_fast64_t`. + */ +#define INT_FAST64_MAX __INT_FAST64_MAX + +/** + * Minimum value of `int_fast64_t`. + */ +#define INT_FAST64_MIN (__MAX_TO_MIN(INT_FAST64_MAX)) + + +/** + * Maximum value of `uintmax_t`. + */ +#define UINTMAX_MAX __UINTMAX_MAX + +/** + * Maximum value of `intmax_t`. + */ +#define INTMAX_MAX __INTMAX_MAX + +/** + * Minimum value of `intmax_t`. + */ +#define INTMAX_MIN (__MAX_TO_MIN(INTMAX_MIN)) + + +/** + * Maximum value of `uintptr_t`. + */ +#if __LONG_BIT == 32 +# define UINTPTR_MAX UINT32_MAX +#else +# define UINTPTR_MAX UINT64_MAX +#endif + +/** + * Maximum value of `intptr_t`. + */ +#if __LONG_BIT == 32 +# define INTPTR_MAX INT32_MAX +#else +# define INTPTR_MAX INT64_MAX +#endif + +/** + * Minimum value of `intptr_t`. + */ +#define INTPTR_MIN (__MAX_TO_MIN(INTPTR_MAX)) + + +/** + * Maximum value of `size_t`. + */ +#define SIZE_MAX UINTPTR_MAX + +/** + * Maximum value of `ssize_t`. + */ +#define SSIZE_MAX INTPTR_MAX + +/** + * Minimum value of `ssize_t`. + */ +#define SSIZE_MIN INTPTR_MIN + + +/** + * Maximum value of `wchar_t`. + */ +#define WCHAR_MAX __WCHAR_MAX + +/** + * Minimum value of `wchar_t`. + */ +#define WCHAR_MIN (__MAX_TO_MIN(WCHAR_MAX)) + + +/** + * Maximum value of `wint_t`. + */ +#define WINT_MAX WCHAR_MAX + +/** + * Minimum value of `wint_t`. + */ +#define WINT_MIN WCHAR_MIN + + +/** + * Maximum value of `sig_atomic_t`. + */ +#if __SIG_ATOMIC_BIT == 8 +# define SIG_ATOMIC_MAX INT8_MAX +#elif __SIG_ATOMIC_BIT == 16 +# define SIG_ATOMIC_MAX INT16_MAX +#elif __SIG_ATOMIC_BIT == 32 +# define SIG_ATOMIC_MAX INT32_MAX +#else +# define SIG_ATOMIC_MAX INT64_MAX +#endif + +/** + * Minimum value of `sig_atomic_t`. + */ +#define SIG_ATOMIC_MIN (__MAX_TO_MIN(__SIG_ATOMIC_MAX)) + + + +#endif + |