add util file

This commit is contained in:
bumbread 2022-08-05 19:17:24 +11:00
parent 79f1449be3
commit 4d4449a5d9
6 changed files with 72 additions and 45 deletions

View File

@ -37,9 +37,10 @@
#include "intrin.h" #include "intrin.h"
#include "util.c"
// Dependencies // Dependencies
#include "decfloat/decfloat.c" #include "decfloat/decfloat.c"
#include "decfloat/decfloat.h"
// Platform-independent stuff // Platform-independent stuff
#include "fmt/gen_fmt.c" #include "fmt/gen_fmt.c"
@ -48,8 +49,8 @@
#include "math/gen_math.c" #include "math/gen_math.c"
#include "math/ieee754.c" #include "math/ieee754.c"
#include "math/round.c" #include "math/round.c"
#include "conv/conversion.c"
#include "stdlib/algorithm.c" #include "stdlib/algorithm.c"
#include "stdlib/conversion.c"
#include "stdlib/multibyte.c" #include "stdlib/multibyte.c"
#include "stdlib/random.c" #include "stdlib/random.c"
#include "ctype.c" #include "ctype.c"

View File

@ -1,16 +1,4 @@
// TODO(bumbread): now that we do static builds perhaps this could be moved
// into the main source file?
// Call me weak if you want but I'm actually too lazy to type
// them out every time, also they take up a lot of horiz space.
typedef long long intll;
typedef long intl;
typedef unsigned long long intull;
typedef unsigned long intul;
typedef unsigned intu;
#define inrange(start, c, end) ((start) <= (c) && (c) <= (end))
static bool isbase(int c, int base) { static bool isbase(int c, int base) {
int val = 0; int val = 0;
if(isdigit(c)) { if(isdigit(c)) {
@ -69,7 +57,7 @@ static intull strtoi_generic(const char *restrict nptr,
if(is_signed) { if(is_signed) {
++int_abs_max; ++int_abs_max;
} }
if(!inrange(0, inbase, 36)) { if(!IN_RANGE(0, inbase, 36)) {
goto finish; goto finish;
} }
intull base = (intull)inbase; intull base = (intull)inbase;

View File

@ -1,6 +1,4 @@
#define in_range(low, v, high) ((low <= v) && (v <= high))
int isalnum(int c) { int isalnum(int c) {
return isalpha(c) || isdigit(c); return isalpha(c) || isdigit(c);
} }
@ -14,11 +12,11 @@ int isblank(int c) {
} }
int iscntrl(int c) { int iscntrl(int c) {
return in_range('\x00', c, '\x1f') || c == '\x7f'; return IN_RANGE('\x00', c, '\x1f') || c == '\x7f';
} }
int isdigit(int c) { int isdigit(int c) {
return in_range('0', c, '9'); return IN_RANGE('0', c, '9');
} }
int isgraph(int c) { int isgraph(int c) {
@ -26,32 +24,32 @@ int isgraph(int c) {
} }
int islower(int c) { int islower(int c) {
return in_range('a', c, 'z'); return IN_RANGE('a', c, 'z');
} }
int isprint(int c) { int isprint(int c) {
return in_range(' ', c, '\x7e'); return IN_RANGE(' ', c, '\x7e');
} }
int ispunct(int c) { int ispunct(int c) {
return in_range('\x21', c, '\x2f') return IN_RANGE('\x21', c, '\x2f')
|| in_range('\x3a', c, '\x40') || IN_RANGE('\x3a', c, '\x40')
|| in_range('\x5b', c, '\x60') || IN_RANGE('\x5b', c, '\x60')
|| in_range('\x7b', c, '\x7e'); || IN_RANGE('\x7b', c, '\x7e');
} }
int isspace(int c) { int isspace(int c) {
return in_range('\x09', c, '\x0d') || c == ' '; return IN_RANGE('\x09', c, '\x0d') || c == ' ';
} }
int isupper(int c) { int isupper(int c) {
return in_range('A', c, 'Z'); return IN_RANGE('A', c, 'Z');
} }
int isxdigit(int c) { int isxdigit(int c) {
return in_range('0', c, '9') return IN_RANGE('0', c, '9')
|| in_range('a', c, 'f') || IN_RANGE('a', c, 'f')
|| in_range('A', c, 'F'); || IN_RANGE('A', c, 'F');
} }
int tolower(int c) { int tolower(int c) {

View File

@ -1,10 +1,4 @@
#if defined(_WIN32)
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#define fe_masks(excepts) (((fexcept_t)(excepts)) << 7) #define fe_masks(excepts) (((fexcept_t)(excepts)) << 7)
#define fe_flags(excepts) ((fexcept_t)(excepts)) #define fe_flags(excepts) ((fexcept_t)(excepts))
#define fe_excepts(masks) ((int)(masks >> 7)) #define fe_excepts(masks) ((int)(masks >> 7))

View File

@ -1,9 +1,7 @@
typedef unsigned char byte;
void *memcpy(void *restrict s1, const void *restrict s2, size_t n) { void *memcpy(void *restrict s1, const void *restrict s2, size_t n) {
const byte *restrict c2 = s2; const u8 *restrict c2 = s2;
byte *restrict c1 = s1; u8 *restrict c1 = s1;
while (n--) { while (n--) {
*c1++ = *c2++; *c1++ = *c2++;
@ -12,8 +10,8 @@ void *memcpy(void *restrict s1, const void *restrict s2, size_t n) {
} }
void *memmove(void *s1, const void *s2, size_t n) { void *memmove(void *s1, const void *s2, size_t n) {
byte* c1 = s1; u8* c1 = s1;
const byte* c2 = s2; const u8* c2 = s2;
// Note(bumbread): shouldn't this be backwards? // Note(bumbread): shouldn't this be backwards?
if (c1 != c2) { if (c1 != c2) {
if (c1 < c2) { if (c1 < c2) {
@ -61,8 +59,8 @@ char *strncat(char *restrict s1, const char *restrict s2, size_t n) {
int memcmp(const void *s1, const void *s2, size_t n) { int memcmp(const void *s1, const void *s2, size_t n) {
const byte *u1 = s1; const u8 *u1 = s1;
const byte *u2 = s2; const u8 *u2 = s2;
for (; n--; u1++, u2++) { for (; n--; u1++, u2++) {
if (*u1 != *u2) return *u1 - *u2; if (*u1 != *u2) return *u1 - *u2;
} }
@ -70,7 +68,7 @@ int memcmp(const void *s1, const void *s2, size_t n) {
} }
void *memset(void *s, int c, size_t n) { void *memset(void *s, int c, size_t n) {
byte *restrict buf = s; u8 *restrict buf = s;
while (n--) { while (n--) {
*buf++ = c; *buf++ = c;
} }

48
src/util.c Normal file
View File

@ -0,0 +1,48 @@
// Some of these more horizontal screen space than I consider healthy, that's
// why I define shorter versions for some of the types. long integers are
// special offenders
// The other part of this file are the convenience macros that could be used
// mostly anywhere in the ciabatta implementation.
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef int_least8_t li8;
typedef int_least16_t li16;
typedef int_least32_t li32;
typedef int_least64_t li64;
typedef int_fast8_t fi8;
typedef int_fast16_t fi16;
typedef int_fast32_t fi32;
typedef int_fast64_t fi64;
typedef long int intl;
typedef long long int intll;
typedef unsigned int intu;
typedef unsigned long int intul;
typedef unsigned long long int intull;
typedef wchar_t wchar;
#define COUNTOF(arr) (sizeof (arr) / sizeof ((arr)[0]))
#define IN_RANGE(start, v, end) ((start) <= (v) && (v) <= (end))
#define CONCAT(a,b) a ## b
#define STR_(a) #a
#define STR(a) STR_(a)
#define DOUBLE_BITS(x) ((union {double f; u64 i;}){x}).i
#define DOUBLE_CONS(x) ((union {double f; u64 i;}){x}).f
#define FLOAT_BITS(x) ((union {float f; u32 i;}){x}).i
#define FLOAT_CONS(x) ((union {float f; u32 i;}){x}).f