diff options
Diffstat (limited to 'include/types.h')
| -rw-r--r-- | include/types.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..7e0066c --- /dev/null +++ b/include/types.h | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | // types.h --- basic types -*- C -*- | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // This file is part of COSMOS Codebase. | ||
| 8 | // | ||
| 9 | // Commentary: | ||
| 10 | // | ||
| 11 | // Code: | ||
| 12 | |||
| 13 | #ifndef INCLUDED_COSMOS_TYPES_H | ||
| 14 | #define INCLUDED_COSMOS_TYPES_H | ||
| 15 | |||
| 16 | #include <stdint.h> | ||
| 17 | #include <float.h> | ||
| 18 | #include <stddef.h> | ||
| 19 | #include <unistd.h> | ||
| 20 | |||
| 21 | typedef int8_t i8; | ||
| 22 | typedef uint8_t u8; | ||
| 23 | typedef int16_t i16; | ||
| 24 | typedef uint16_t u16; | ||
| 25 | typedef int32_t i32; | ||
| 26 | typedef uint32_t u32; | ||
| 27 | typedef int64_t i64; | ||
| 28 | typedef uint64_t u64; | ||
| 29 | |||
| 30 | #define I8_MAX INT8_MAX | ||
| 31 | #define I8_MIN INT8_MIN | ||
| 32 | #define U8_MAX UINT8_MAX | ||
| 33 | #define I16_MAX INT16_MAX | ||
| 34 | #define I16_MIN INT16_MIN | ||
| 35 | #define U16_MAX UINT16_MAX | ||
| 36 | #define I32_MAX INT32_MAX | ||
| 37 | #define I32_MIN INT32_MIN | ||
| 38 | #define U32_MAX UINT32_MAX | ||
| 39 | #define I64_MAX INT64_MAX | ||
| 40 | #define I64_MIN INT64_MIN | ||
| 41 | #define U64_MAX UINT64_MAX | ||
| 42 | |||
| 43 | // typedef __int128 i128; | ||
| 44 | // typedef unsigned __int128 u128; | ||
| 45 | |||
| 46 | typedef float f32; | ||
| 47 | typedef double f64; | ||
| 48 | |||
| 49 | // typedef _Float16 f16; | ||
| 50 | // typedef __float80 f80; | ||
| 51 | // typedef __float128 f128; | ||
| 52 | |||
| 53 | typedef ssize_t isz; | ||
| 54 | typedef size_t usz; | ||
| 55 | |||
| 56 | typedef intptr_t iptr; | ||
| 57 | typedef uintptr_t uptr; | ||
| 58 | |||
| 59 | typedef ptrdiff_t ptrdiff; | ||
| 60 | |||
| 61 | // Memory Block | ||
| 62 | struct mb { void *ptr; usz sz; }; | ||
| 63 | #define MB_NULL ((struct mb) {0}) | ||
| 64 | #define MB_FROM(PTR, SZ) ((struct mb) { .ptr = (void *) PTR, .sz = SZ }) | ||
| 65 | #define MB( lit) ((struct mb) { .ptr = &(lit), .sz = sizeof (lit) }) | ||
| 66 | #define MB0(lit) ((struct mb) { .ptr = &(lit), .sz = sizeof (lit) - 1 }) | ||
| 67 | #define MB_FMT(mb) (int) (mb).sz, (mb).ptr | ||
| 68 | #define MB_END(mb) ((void *) ((u8 *) (mb).ptr + mb.sz)) | ||
| 69 | |||
| 70 | static inline int | ||
| 71 | usz_mul_overflow (usz a, usz b, usz *out) | ||
| 72 | { | ||
| 73 | #if defined (__GNUC__) || defined (__clang__) | ||
| 74 | return __builtin_mul_overflow (a, b, out); | ||
| 75 | #else | ||
| 76 | |||
| 77 | if (a != 0 && b > (usz) -1/a) | ||
| 78 | return 1; | ||
| 79 | |||
| 80 | *out = a*b; | ||
| 81 | |||
| 82 | return 0; | ||
| 83 | #endif | ||
| 84 | } | ||
| 85 | |||
| 86 | #endif // INCLUDED_COSMOS_TYPES_H | ||
| 87 | |||
| 88 | // types.h ends here | ||
