diff --git a/src/io/render.c b/src/io/render.c index bf505d0..4fa8eb6 100644 --- a/src/io/render.c +++ b/src/io/render.c @@ -8,6 +8,7 @@ #include #include "../error.h" +#include "../util/types.h" #include "../util/vec/int2.h" #include "shader.h" @@ -18,7 +19,7 @@ static GLuint vao; // vertex array object static GLuint screen_loc; // location to where OpenGL sends to the shaders of the screen dimensions static void screen_resize(int w, int h) { - int32_t verts[VERTC][4] = { + i32 verts[VERTC][4] = { {0, 0, w, 20 }, {0, 20, w, h - 40}, {0, h, w, -20 }, @@ -55,7 +56,7 @@ int render_init(void) { // set VBO info glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableVertexAttribArray(0); // set the array data index to 0 - glVertexAttribIPointer(0, 4, GL_INT, 4 * sizeof(int32_t), NULL); + glVertexAttribIPointer(0, 4, GL_INT, 4 * sizeof(i32), NULL); glBindVertexArray(0); return 0; diff --git a/src/util/colour32.h b/src/util/colour32.h index db94fa2..af337d7 100644 --- a/src/util/colour32.h +++ b/src/util/colour32.h @@ -3,18 +3,20 @@ #pragma once #include +#include "types.h" + // stores colour in a rgba format stored as little-endian, each channel being a 8 bits wide. typedef union colour32 { - uint32_t dat; // full colour data; little-endian + u32 dat; // full colour data; little-endian #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ // colour channel information for little-endian systems struct { - uint8_t a, b, g, r; + u8 a, b, g, r; } ch; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ // colour channel information for big-endian systems struct { - uint8_t r, g, b, a; + u8 r, g, b, a; } ch; #endif } colour32; diff --git a/src/util/compat/bswap.h b/src/util/compat/bswap.h index a8acdcf..39455bc 100644 --- a/src/util/compat/bswap.h +++ b/src/util/compat/bswap.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "../types.h" #if defined(__GNUC__) || defined(__clang__) #define bswap16 __builtin_bswap16 @@ -19,18 +19,18 @@ #endif #if defined(IMPL_BSWAP) || !defined(NDEBUG) -static inline uint16_t bswap16_impl(uint16_t x) { +static inline u16 bswap16_impl(u16 x) { return (x << 8) | (x >> 8); } -static inline uint32_t bswap32_impl(uint32_t x) { +static inline u32 bswap32_impl(u32 x) { return (x << 24) | ((0x0000FF00U & x) << 8) | ((0x00FF0000U & x) >> 8) | (x >> 24); } -static inline uint64_t bswap64_impl(uint64_t x) { +static inline u64 bswap64_impl(u64 x) { return (x << 56) | ((0x000000000000FF00ULL & x) << 40) | ((0x0000000000FF0000ULL & x) << 24) | diff --git a/src/util/compat/endian.h b/src/util/compat/endian.h index a3f6d9e..e1fcb95 100644 --- a/src/util/compat/endian.h +++ b/src/util/compat/endian.h @@ -5,50 +5,51 @@ #include #include "../atrb.h" +#include "../types.h" #include "bswap.h" /* little endian */ -atrb_const static inline uint16_t le16ton(uint16_t); // converts little-endian (LE) encoding to native for a 16 bit integer. (NOOP if native is LE) -atrb_const static inline uint32_t le32ton(uint32_t); // converts little-endian (LE) encoding to native for a 32 bit integer. (NOOP if native is LE) -atrb_const static inline uint64_t le64ton(uint64_t); // converts little-endian (LE) encoding to native for a 64 bit integer. (NOOP if native is LE) -atrb_const static inline uint16_t ntole16(uint16_t); // converts native encoding to little-endian (LE) for a 16 bit integer. (NOOP if native is LE) -atrb_const static inline uint32_t ntole32(uint32_t); // converts native encoding to little-endian (LE) for a 32 bit integer. (NOOP if native is LE) -atrb_const static inline uint64_t ntole64(uint64_t); // converts native encoding to little-endian (LE) for a 64 bit integer. (NOOP if native is LE) +atrb_const static inline u16 le16ton(u16); // converts little-endian (LE) encoding to native for a 16 bit integer. (NOOP if native is LE) +atrb_const static inline u32 le32ton(u32); // converts little-endian (LE) encoding to native for a 32 bit integer. (NOOP if native is LE) +atrb_const static inline u64 le64ton(u64); // converts little-endian (LE) encoding to native for a 64 bit integer. (NOOP if native is LE) +atrb_const static inline u16 ntole16(u16); // converts native encoding to little-endian (LE) for a 16 bit integer. (NOOP if native is LE) +atrb_const static inline u32 ntole32(u32); // converts native encoding to little-endian (LE) for a 32 bit integer. (NOOP if native is LE) +atrb_const static inline u64 ntole64(u64); // converts native encoding to little-endian (LE) for a 64 bit integer. (NOOP if native is LE) /* big endian */ -atrb_const static inline uint16_t be16ton(uint16_t); // converts big-endian (BE) encoding to native for a 16 bit integer. (NOOP if native is BE) -atrb_const static inline uint32_t be32ton(uint32_t); // converts big-endian (BE) encoding to native for a 32 bit integer. (NOOP if native is BE) -atrb_const static inline uint64_t be64ton(uint64_t); // converts big-endian (BE) encoding to native for a 64 bit integer. (NOOP if native is BE) -atrb_const static inline uint16_t ntobe16(uint16_t); // converts native encoding to big-endian (BE) for a 16 bit integer. (NOOP if native is BE) -atrb_const static inline uint32_t ntobe32(uint32_t); // converts native encoding to big-endian (BE) for a 32 bit integer. (NOOP if native is BE) -atrb_const static inline uint64_t ntobe64(uint64_t); // converts native encoding to big-endian (BE) for a 64 bit integer. (NOOP if native is BE) +atrb_const static inline u16 be16ton(u16); // converts big-endian (BE) encoding to native for a 16 bit integer. (NOOP if native is BE) +atrb_const static inline u32 be32ton(u32); // converts big-endian (BE) encoding to native for a 32 bit integer. (NOOP if native is BE) +atrb_const static inline u64 be64ton(u64); // converts big-endian (BE) encoding to native for a 64 bit integer. (NOOP if native is BE) +atrb_const static inline u16 ntobe16(u16); // converts native encoding to big-endian (BE) for a 16 bit integer. (NOOP if native is BE) +atrb_const static inline u32 ntobe32(u32); // converts native encoding to big-endian (BE) for a 32 bit integer. (NOOP if native is BE) +atrb_const static inline u64 ntobe64(u64); // converts native encoding to big-endian (BE) for a 64 bit integer. (NOOP if native is BE) #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -uint16_t le16ton(uint16_t x) { return x; } -uint32_t le32ton(uint32_t x) { return x; } -uint64_t le64ton(uint64_t x) { return x; } -uint16_t ntole16(uint16_t x) { return x; } -uint32_t ntole32(uint32_t x) { return x; } -uint64_t ntole64(uint64_t x) { return x; } -uint16_t be16ton(uint16_t x) { return bswap16(x); } -uint32_t be32ton(uint32_t x) { return bswap32(x); } -uint64_t be64ton(uint64_t x) { return bswap64(x); } -uint16_t ntobe16(uint16_t x) { return bswap16(x); } -uint32_t ntobe32(uint32_t x) { return bswap32(x); } -uint64_t ntobe64(uint64_t x) { return bswap64(x); } +u16 le16ton(u16 x) { return x; } +u32 le32ton(u32 x) { return x; } +u64 le64ton(u64 x) { return x; } +u16 ntole16(u16 x) { return x; } +u32 ntole32(u32 x) { return x; } +u64 ntole64(u64 x) { return x; } +u16 be16ton(u16 x) { return bswap16(x); } +u32 be32ton(u32 x) { return bswap32(x); } +u64 be64ton(u64 x) { return bswap64(x); } +u16 ntobe16(u16 x) { return bswap16(x); } +u32 ntobe32(u32 x) { return bswap32(x); } +u64 ntobe64(u64 x) { return bswap64(x); } #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -uint16_t le16ton(uint16_t x) { bswap_16(x); } -uint32_t le32ton(uint32_t x) { bswap_32(x); } -uint64_t le64ton(uint64_t x) { bswap_64(x); } -uint16_t ntole16(uint16_t x) { bswap_16(x); } -uint32_t ntole32(uint32_t x) { bswap_32(x); } -uint64_t ntole64(uint64_t x) { bswap_64(x); } -uint16_t be16ton(uint16_t x) { return x; } -uint32_t be32ton(uint32_t x) { return x; } -uint64_t be64ton(uint64_t x) { return x; } -uint16_t ntobe16(uint16_t x) { return x; } -uint32_t ntobe32(uint32_t x) { return x; } -uint64_t ntobe64(uint64_t x) { return x; } +u16 le16ton(u16 x) { bswap_16(x); } +u32 le32ton(u32 x) { bswap_32(x); } +u64 le64ton(u64 x) { bswap_64(x); } +u16 ntole16(u16 x) { bswap_16(x); } +u32 ntole32(u32 x) { bswap_32(x); } +u64 ntole64(u64 x) { bswap_64(x); } +u16 be16ton(u16 x) { return x; } +u32 be32ton(u32 x) { return x; } +u64 be64ton(u64 x) { return x; } +u16 ntobe16(u16 x) { return x; } +u32 ntobe32(u32 x) { return x; } +u64 ntobe64(u64 x) { return x; } #else #error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc) #endif diff --git a/src/util/conf.c b/src/util/conf.c index 4a4600e..30f51e2 100644 --- a/src/util/conf.c +++ b/src/util/conf.c @@ -12,6 +12,7 @@ #include "../error.h" #include "atrb.h" +#include "types.h" int conf_procbuf(char const* restrict buf, char* restrict kout, char* restrict vout, size_t len) { bool feq = false; // whether we've found the equal sign @@ -68,7 +69,7 @@ int conf_procval(struct conf_entry const* opt, char const* restrict val) { // parse the data errno = 0; char* end; - int8_t dat[sizeof(long long)]; // long long is guaranteed to be ≥64 bits in ISO C (double is always 64 bit) + u8 dat[sizeof(u64)]; switch (opt->type) { // signed integer data parsing @@ -76,19 +77,19 @@ int conf_procval(struct conf_entry const* opt, char const* restrict val) { case CONF_I16: case CONF_I32: case CONF_I64: - *(long long*)dat = strtoll(val, &end, 10); // for signed integer types + *(i64*)dat = strtoll(val, &end, 10); // for signed integer types break; // unsigned integer data parsing case CONF_U8: case CONF_U16: case CONF_U32: case CONF_U64: - *(long long*)dat = strtoull(val, &end, 10); // for unsigned integer types + *(u64*)dat = strtoull(val, &end, 10); // for unsigned integer types break; // floating-point data parsing - case CONF_F32: *(float*)dat = strtof(val, &end); break; - case CONF_F64: *(double*)dat = strtod(val, &end); break; + case CONF_F32: *(f32*)dat = strtof(val, &end); break; + case CONF_F64: *(f64*)dat = strtod(val, &end); break; // string data parsing case CONF_STR: @@ -112,19 +113,17 @@ int conf_procval(struct conf_entry const* opt, char const* restrict val) { return CONF_EPARSE; } - typedef unsigned long long ull; - typedef signed long long ll; switch (opt->type) { - case CONF_U8: *(uint8_t*)opt->out = *(ull*)dat >= UINT8_MAX ? UINT8_MAX : *(ull*)dat; return 0; - case CONF_U16: *(uint16_t*)opt->out = *(ull*)dat >= UINT16_MAX ? UINT16_MAX : *(ull*)dat; return 0; - case CONF_U32: *(uint32_t*)opt->out = *(ull*)dat >= UINT32_MAX ? UINT32_MAX : *(ull*)dat; return 0; - case CONF_U64: *(uint64_t*)opt->out = *(ull*)dat >= UINT64_MAX ? UINT64_MAX : *(ull*)dat; return 0; - case CONF_I8: *(int8_t*)opt->out = *(ll*)dat >= INT8_MAX ? INT8_MAX : (*(ll*)dat <= INT8_MIN ? INT8_MIN : *(ll*)dat); return 0; - case CONF_I16: *(int16_t*)opt->out = *(ll*)dat >= INT16_MAX ? INT16_MAX : (*(ll*)dat <= INT16_MIN ? INT16_MIN : *(ll*)dat); return 0; - case CONF_I32: *(int32_t*)opt->out = *(ll*)dat >= INT32_MAX ? INT32_MAX : (*(ll*)dat <= INT32_MIN ? INT32_MIN : *(ll*)dat); return 0; - case CONF_I64: *(int64_t*)opt->out = *(ll*)dat >= INT64_MAX ? INT64_MAX : (*(ll*)dat <= INT64_MIN ? INT64_MIN : *(ll*)dat); return 0; - case CONF_F32: *(float*)opt->out = *(float*)dat; return 0; - case CONF_F64: *(double*)opt->out = *(double*)dat; return 0; + case CONF_U8: *(u8*)opt->out = *(u64*)dat >= UINT8_MAX ? UINT8_MAX : *(u64*)dat; return 0; + case CONF_U16: *(u16*)opt->out = *(u64*)dat >= UINT16_MAX ? UINT16_MAX : *(u64*)dat; return 0; + case CONF_U32: *(u32*)opt->out = *(u64*)dat >= UINT32_MAX ? UINT32_MAX : *(u64*)dat; return 0; + case CONF_U64: *(u64*)opt->out = *(u64*)dat >= UINT64_MAX ? UINT64_MAX : *(u64*)dat; return 0; + case CONF_I8: *(i8*)opt->out = *(i64*)dat >= INT8_MAX ? INT8_MAX : (*(i64*)dat <= INT8_MIN ? INT8_MIN : *(i64*)dat); return 0; + case CONF_I16: *(i16*)opt->out = *(i64*)dat >= INT16_MAX ? INT16_MAX : (*(i64*)dat <= INT16_MIN ? INT16_MIN : *(i64*)dat); return 0; + case CONF_I32: *(i32*)opt->out = *(i64*)dat >= INT32_MAX ? INT32_MAX : (*(i64*)dat <= INT32_MIN ? INT32_MIN : *(i64*)dat); return 0; + case CONF_I64: *(i64*)opt->out = *(i64*)dat >= INT64_MAX ? INT64_MAX : (*(i64*)dat <= INT64_MIN ? INT64_MIN : *(i64*)dat); return 0; + case CONF_F32: *(f32*)opt->out = *(f32*)dat; return 0; + case CONF_F64: *(f64*)opt->out = *(f64*)dat; return 0; default: fatal("invalid switch state, all cases should be handled already"); // abort; this shouldn't be possible, so I blame the programmer } } diff --git a/src/util/macro.h b/src/util/macro.h index d3f99fb..cdb271b 100644 --- a/src/util/macro.h +++ b/src/util/macro.h @@ -2,7 +2,7 @@ // Licensed under the MIT Licence. See LICENSE for details #pragma once -#define MACRO_WIDTH(t) (sizeof(t) * 8) // gets the bit width of a type +#define WIDTHOF(t) (sizeof(t) * 8) // gets the bit width of a type #define MACRO_CAT(x, y) x##y // concatenate two macro variables together #define MACRO_CAT2(x, y) MACRO_CAT(x, y) // concatenate two macro variables together #define MACRO_STR(v) #v // for converting macro variable into a string diff --git a/src/util/types.h b/src/util/types.h index a43edc7..0f536d2 100644 --- a/src/util/types.h +++ b/src/util/types.h @@ -2,8 +2,23 @@ // Licensed under the MIT Licence. See LICENSE for details #pragma once -/* integer types */ +/* variable-width integer types */ typedef unsigned int uint; // ≥16 bit unsigned integer typedef unsigned long ulong; // ≥32 bit unsigned integer typedef signed long long llong; // ≥64 bit signed integer typedef unsigned long long ullong; // ≥64 bit unsigned integer + +/* fixed-width integer types */ +#include +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +/* floating point types */ +typedef float f32; // single-precision floating-point +typedef double f64; // double-precision floating-point diff --git a/test/dat.h b/test/dat.h index aaebf88..e7ef888 100644 --- a/test/dat.h +++ b/test/dat.h @@ -1,6 +1,7 @@ #include #include "../src/util/conf.h" +#include "../src/util/types.h" #include "../src/util/vec/float3.h" #include "t_conf.h" #include "t_util.h" @@ -9,12 +10,12 @@ testdat tests[] = { {"", test_float3_norm, &(float3){2.0F, 0.67F, 5.0F} }, {"", test_float3_norm, &(float3){0.2F, 0.4F, 0.1F} }, - {"", test_bswap16, (uint16_t[]){0x11EE, 0xEE11} }, - {"", test_bswap32, (uint32_t[]){0x1142ADEE, 0xEEAD4211} }, - {"", test_bswap64, (uint64_t[]){0x114266897799ADEE, 0xEEAD997789664211} }, - {"", test_bswap16_impl, (uint16_t[]){0x11EE, 0xEE11} }, - {"", test_bswap32_impl, (uint32_t[]){0x1142ADEE, 0xEEAD4211} }, - {"", test_bswap64_impl, (uint64_t[]){0x114266897799ADEE, 0xEEAD997789664211} }, + {"", test_bswap16, (u16[]){0x11EE, 0xEE11} }, + {"", test_bswap32, (u32[]){0x1142ADEE, 0xEEAD4211} }, + {"", test_bswap64, (u64[]){0x114266897799ADEE, 0xEEAD997789664211} }, + {"", test_bswap16_impl, (u16[]){0x11EE, 0xEE11} }, + {"", test_bswap32_impl, (u32[]){0x1142ADEE, 0xEEAD4211} }, + {"", test_bswap64_impl, (u64[]){0x114266897799ADEE, 0xEEAD997789664211} }, {"k=v", test_procbuf, &(struct test_procbuf){"key=val", "key", "val", 0} }, {"sometxt", test_procbuf, &(struct test_procbuf){"sometxt", "sometxt", "", CONF_ESYNTAX} }, {"comment", test_procbuf, &(struct test_procbuf){"# comment", "", "", CONF_ENODAT} }, @@ -27,7 +28,7 @@ testdat tests[] = { {"invalid", test_matchopt, &(struct test_matchopt){"nono", -1} }, {"", test_colour32_endianess, NULL }, // NOTE: formatter is fucking with alignment making it use tabs @.@ {"i32", test_procval_int, &(struct test_procval_int){"42", 42, CONF_I32} }, - {"i32_neg", test_procval_int, &(struct test_procval_int){"-42", (uint32_t)-42, CONF_I32} }, + {"i32_neg", test_procval_int, &(struct test_procval_int){"-42", (u32)-42, CONF_I32} }, {"u32_max", test_procval_int, &(struct test_procval_int){"4294967295", UINT32_MAX, CONF_U64} }, {"u64", test_procval_int, &(struct test_procval_int){"3141592653589793238", 3141592653589793238, CONF_U64}}, {"u8_overflow", test_procval_int, &(struct test_procval_int){"256", 255, CONF_U8} }, diff --git a/test/t_conf.h b/test/t_conf.h index 2bb8dde..cfca9ff 100644 --- a/test/t_conf.h +++ b/test/t_conf.h @@ -5,6 +5,7 @@ #include #include "../src/util/conf.h" +#include "../src/util/types.h" #include "test.h" // Environment saver structure for conf_getpat tests @@ -96,19 +97,19 @@ int test_matchopt(void* arg) { struct test_procval_int { char const* val; - uint64_t xres; - uint8_t type; + u64 xres; + u8 type; }; int test_procval_int(void* arg) { struct test_procval_int* dat = arg; - uint64_t out; + u64 out; return assert_true(!conf_procval(&(struct conf_entry){NULL, &out, dat->type}, dat->val)) || assert_true(out == dat->xres); } int test_procval_f32(void* arg) { (void)arg; - float out; + f32 out; return assert_true(!conf_procval(&(struct conf_entry){NULL, &out, CONF_F32}, "3.14159265")) || assert_true(fabsf(out - 3.14159265F) < 1e-6F); } @@ -149,7 +150,7 @@ int test_procval_fstr_trunc(void* arg) { int test_procval_eparse(void* arg) { (void)arg; - int32_t out; + i32 out; return assert_true(conf_procval(&(struct conf_entry){NULL, &out, CONF_I32}, "owo") == CONF_EPARSE); } diff --git a/test/t_util.h b/test/t_util.h index e63c123..9bdd087 100644 --- a/test/t_util.h +++ b/test/t_util.h @@ -4,6 +4,7 @@ #include "../src/util/colour32.h" #include "../src/util/compat/bswap.h" +#include "../src/util/types.h" #include "../src/util/vec/float3.h" #include "test.h" @@ -24,31 +25,31 @@ int test_colour32_endianess(void* d) { } int test_bswap16(void* d) { - uint16_t* arg = d; + u16* arg = d; return assert_true(bswap16(arg[0]) == arg[1]); } int test_bswap32(void* d) { - uint32_t* arg = d; + u32* arg = d; return assert_true(bswap32(arg[0]) == arg[1]); } int test_bswap64(void* d) { - uint64_t* arg = d; + u64* arg = d; return assert_true(bswap64(arg[0]) == arg[1]); } int test_bswap16_impl(void* d) { - uint16_t* arg = d; + u16* arg = d; return assert_true(bswap16_impl(arg[0]) == arg[1]); } int test_bswap32_impl(void* d) { - uint32_t* arg = d; + u32* arg = d; return assert_true(bswap32_impl(arg[0]) == arg[1]); } int test_bswap64_impl(void* d) { - uint64_t* arg = d; + u64* arg = d; return assert_true(bswap64_impl(arg[0]) == arg[1]); }