define short-hand types for int32/uint32 et cetera

This commit is contained in:
2025-06-20 19:54:34 +02:00
parent ad66d9465a
commit 80b8b65b73
10 changed files with 103 additions and 82 deletions

View File

@@ -3,18 +3,20 @@
#pragma once
#include <stdint.h>
#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;

View File

@@ -1,6 +1,6 @@
#pragma once
#include <stdint.h>
#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) |

View File

@@ -5,50 +5,51 @@
#include <stdint.h>
#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

View File

@@ -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
}
}

View File

@@ -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

View File

@@ -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 <stdint.h>
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