diff options
author | justanothercatgirl <sotov2070@gmail.com> | 2024-06-22 18:16:54 +0300 |
---|---|---|
committer | justanothercatgirl <sotov2070@gmail.com> | 2024-06-22 18:16:54 +0300 |
commit | 57033d0ef1810abf4edd9d8e57a95a5795e5a5a9 (patch) | |
tree | b736fc7f72a7d8d720d5c4b86e736a522e07c53e | |
parent | 437c40d8bcd533c8af6804f299f5e0cb974f7a9d (diff) |
added `rstypes` header.
-rw-r--r-- | Makefile | 21 | ||||
-rw-r--r-- | README.md | 21 | ||||
-rw-r--r-- | include/container.h | 4 | ||||
-rw-r--r-- | include/rstypes.h | 39 | ||||
-rw-r--r-- | tests/types.c | 35 |
5 files changed, 100 insertions, 20 deletions
@@ -3,24 +3,27 @@ CC := gcc CFLAGS := -g -Wall -Wextra -Wpedantic -Werror -Wno-language-extension-token -tests: dynarray +tests: container -dynarray: tests/dynarray.c tests/dynarray_struct.c tests/obscure.c tests/binary_search.c +container: tests/dynarray.c tests/dynarray_struct.c tests/obscure.c tests/binary_search.c tests/types.c $(CC) $(CFLAGS) -o $@ $< -DTT_CHAR - ./dynarray + ./$@ $(CC) $(CFLAGS) -o $@ $< -DTT_SHORT - ./dynarray + ./$@ $(CC) $(CFLAGS) -o $@ $< -DTT_INT - ./dynarray + ./$@ $(CC) $(CFLAGS) -o $@ $< -DTT_LONG - ./dynarray + ./$@ $(CC) $(CFLAGS) -o $@ tests/dynarray_struct.c - ./dynarray + ./$@ $(CC) $(CFLAGS) -o $@ tests/obscure.c - ./dynarray + ./$@ $(CC) $(CFLAGS) -o $@ tests/binary_search.c - ./dynarray + ./$@ + $(CC) $(CFLAGS) -o $@ tests/types.c + ./$@ + rm $@ @@ -5,16 +5,17 @@ This is just a collection of (usually single-purpos) header files that I use ref * Names starting with underscores are kind of private-use for this libraries, but they may be declared not `static` because they are used in macros that expose API. * `snake_case` * Function-like macros are not all-caps. -* For now i only support GCC-based compilers. I will consider removing this dependency in future (e.g. getting rid of that [`__qsort_cmps` declaration](include/utility.h)). +* For now i only support GCC-based compilers. I will consider removing this dependency in future (e.g. getting rid of all `typeof` uses) # Descriptions for heders: -## [`dynarray.h`](include/dynarray.h) -* Summary: A flexible and easy-to-use dynamically-sized array implementation based on the idea of storing metadata about array to the left of actual data. this allows it to be used interchangabely as a regular C array and as dynamic array. -* How to use: Define `DYNARRAY_IMPLEMENTATION` macro before including `dynarray.h`. If you are using functions to sort the array (`array_qsort_integral`), you also have to define `UTILITY_IMPLEMENTATION` (as it uses integer comparison functions from there) -* Examples: See [tests](tests/dynarray.c) -## [`utility.h`](include/utility.h) -* Summary: Some commonly-used macros and functions in this library, could as well be called `miscellaneous`. -* How to use: Define `UTILITY_IMPLEMENTATION` macro before including `utility.h`. If you want it to expose helper functions/variables (which you may want, check the source), define macro `UTILITY_EXPOSE_HELPERS`. -* Examples: there are kind of no examples for this, i guess you could search for the usages in [`dynarray.h`](include/dynarray.h) +## [`container.h`](include/container.h) +* Summary: originally was several headers, but since `hash_map.h` used `dynarray.h`, which itself used `utility.h`, it was too complex to work with. So i combined everything in a single header. +* How to use: Define `CONTAINER_IMPLEMENTATION` macro before including `container.h`. It's probably better to `#undef` it after inclusion as well +* Examples: See [tests](tests) +## [`rstypes.h`](include/rstypes.h) +* Summary: rust type aliases (like `u32`, `f128` e.t.c.) +* How to use: if you don't need `i128` or `u128`, then just include the header. If you do need them, define macro `RS_TYPES_USE_128`. +* Examples: See [tests](tests/types.c). +* Notes: Not many compilers support 128-bit wide integers, and ones which DO support them tend to warn you that these are non ISO-C. That's why I've put them behind a macro. If they are not supported, their usage will fail with compilation error. ## [`embed.h`](include/embed.h) * Summary: Code generator for embedding resources directly into an executable. * How to use: It exposes C interface, so in order to use it, you will need to have a C program that builds resources for you. It has it's own repo, but i will be adding CLI to there soon. @@ -25,5 +26,5 @@ nearest TODO for now. # Warning! Everything here is written by a relatively inexperienced student (me), so I guarantee basically nothing. Memory leaks? I am sorry for them, but also not responsible. Security? Haven't heard of that either. That's just how it is. But I try to make everything as good as possible, so you can use the code after a careful review. # License -I did not decide on it yet, but I am inclined towards MIT or even public domain. Don't want to restrict the usage with LGPL, because it's not like this code is a big deal, anyone could write it. Also, I don't want to deal with license issues that I created for myself in future, so yeah, that's how it is. For now, public domain. +I did not decide on it yet, but I am inclined towards MIT or even public domain. Don't want to restrict the usage with LGPL, because it's not like this code is a big deal, anyone could write it. Also, I don't want to deal with license issues that I can create for my future self, so yeah, that's how it is. For now, public domain. diff --git a/include/container.h b/include/container.h index b987d35..47eb0a7 100644 --- a/include/container.h +++ b/include/container.h @@ -1,7 +1,6 @@ #ifndef JUSTANOTHERCATGIRL_HEADERS_CONTAINER #define JUSTANOTHERCATGIRL_HEADERS_CONTAINER -#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -29,6 +28,7 @@ typedef unsigned char byte; unsigned long __bit_scan_32(int32_t number); unsigned long __bit_scan_64(int64_t number); #endif + #if defined(__clang__) || defined(__GNUC__) #define FALLTHROUGH __attribute__((fallthrough)) #elif defined(_MSC_VER) @@ -37,6 +37,8 @@ typedef unsigned char byte; #define FALLTHROUGH ((void)0) #endif +#define TODO (void)(*(volatile char*)0) + typedef int(*qsort_cmp_t)(const void*, const void*); #define get_qsort_cmp(type) __qsort_cmps[sizeof(type)] extern const qsort_cmp_t __qsort_cmps[64]; diff --git a/include/rstypes.h b/include/rstypes.h new file mode 100644 index 0000000..5af5ec5 --- /dev/null +++ b/include/rstypes.h @@ -0,0 +1,39 @@ +#ifndef JUSTANOTHERCATGITL_RSTYPES_H +#define JUSTANOTHERCATGITL_RSTYPES_H + +#include <stdint.h> + +typedef uint8_t u8; +typedef int8_t i8; +typedef uint16_t u16; +typedef int16_t i16; +typedef uint32_t u32; +typedef int32_t i32; +typedef uint64_t u64; +typedef int64_t i64; + +typedef float f32; +typedef double f64; + + +#if defined(__SIZEOF_LONG_DOUBLE__) && __SIZEOF_LONG_DOUBLE__ == 128/8 +typedef long double f128; +#else // __SIZEOF_LONG_DOUBLE__ + #if defined(__clang__) || defined(__GNUC__) + typedef __float128 f128; + #else + #define f128 "128-bit floats are not supported on this platform/compiler" + #endif // __clang__ || __GNUC__ +#endif // __SIZEOF_LONG_DOUBLE__ + +#ifdef RS_TYPES_USE_128 + #if defined(__clang__) || defined(__GNUC__) + typedef __int128 i128; + typedef unsigned __int128 u128; + #else // __clang__ || __GNUC__ + #define i128 "128-bit integers are not supported in this compiler" + #define u128 "128-bit integers are not supported in this compiler" + #endif // __clang__ || __GNUC__ +#endif // RS_TYPES_USE_128 + +#endif // JUSTANOTHERCATGITL_RSTYPES_H diff --git a/tests/types.c b/tests/types.c new file mode 100644 index 0000000..4320e8d --- /dev/null +++ b/tests/types.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include "../include/rstypes.h" + +i32 main() { + u8 a = 215; + i8 b = -127; + u16 c = 65535; + i16 d = -32000; + u32 e = 0xFFFFFFF; + i32 f = -1; + u64 g = 0xFAFAFAFAFAFAFAFA; + i64 h = -4000000000000000000; + u128 i = 0xFFFFFFFFFFFFFFFF; + i128 j = 0; + i <<= 64; i |= 0xFFFFFFFFFFFFFFFF; + j = i; + + f32 k = 5e-23; + f64 l = -2e300; + f128 m = 3e300; + m *= 1e100; + + printf( "a: %hhu, b: %hhi\n" + "c: %hu, d: %hi\n" + "e: %u, f: %d\n" + "g: %lu, h: %li\n", + a, b, c, d, e, f, g, h); + printf("i: %lX%.16lX, jL %lx%.16lx\n", + (unsigned long) (i >> 64), (unsigned long) i, + (long) (j >> 64), (long) j ); + + printf("k: %.2E\nl: %.2lE\nm: %.2LE\n", k, l, m); + + return (j == -1 && i+1 == 0 ? 0 : 1); +} |