Update base

This commit is contained in:
Gregory Lirent 2022-06-03 11:56:01 +03:00
parent 759c2460bb
commit a0a7299fe0
3 changed files with 214 additions and 276 deletions

View File

@ -7,96 +7,84 @@
/*#####################################################################################################################*/
static vnode_t create_number(vtype xt, const void* v, vtype t) {
var_t _ = { .ptr = 0 };
static vnode_t create_value(vtype xt, const void* v, vtype t) {
var_t _;
if (t == VTYPE_POINTER) t = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (t == VTYPE_BOOLEAN) t = VTYPE_UINT8;
if (sizeof(void*) == 8) {
if (t == VTYPE_UINT8 || t == VTYPE_INT8 || t == VTYPE_BOOLEAN ) {
_.u64 = *(u8_t*)v;
} else if (t == VTYPE_UINT16 || t == VTYPE_INT16) {
_.u64 = *(u16_t*)v;
} else if (t == VTYPE_UINT32 || t == VTYPE_INT32 || (sizeof(void*) == 4 && t == VTYPE_POINTER)) {
_.u64 = *(u32_t*)v;
} else if (t == VTYPE_UINT64 || t == VTYPE_INT64 || (sizeof(void*) == 8 && t == VTYPE_POINTER)) {
_.u64 = *(u64_t*)v;
}
if (xt == VTYPE_POINTER) xt = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (xt == VTYPE_BOOLEAN) xt = VTYPE_UINT8;
if (is_big_endian) {
if (xt == VTYPE_BOOLEAN) _.u8 = (_.u64 == 0) ? 0 : 1;
else if (xt == VTYPE_UINT8 || xt == VTYPE_INT8) {
_.u64 <<= 56;
} else if (xt == VTYPE_UINT16 || xt == VTYPE_INT16) {
_.u64 <<= 48;
} else if (xt == VTYPE_UINT32 || xt == VTYPE_INT32) {
_.u64 <<= 32;
}
} else if (xt == VTYPE_BOOLEAN) _.u64 = (_.u64 == 0) ? 0 : 1;
switch (xt) { default: default_: abort();
case VTYPE_INT8:
case VTYPE_UINT8: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u8 = *(u8_t*)v; break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u8 = *(u16_t*)v; break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u8 = *(u32_t*)v; break;
case VTYPE_INT64:
case VTYPE_UINT64: _.u8 = *(u64_t*)v; break;
}
break;
} else {
if (t == VTYPE_UINT8 || t == VTYPE_INT8 || t == VTYPE_BOOLEAN ) {
_.u64 = *(u8_t*)v;
} else if (t == VTYPE_UINT16 || t == VTYPE_INT16) {
_.u64 = *(u16_t*)v;
} else if (t == VTYPE_UINT32 || t == VTYPE_INT32 || (sizeof(void*) == 4 && t == VTYPE_POINTER)) {
_.u64 = *(u32_t*)v;
} else if (t == VTYPE_UINT64 || t == VTYPE_INT64 || (sizeof(void*) == 8 && t == VTYPE_POINTER)) {
_.u64 = *(u64_t*)v;
}
case VTYPE_INT16:
case VTYPE_UINT16: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u16 = *(u8_t*)v; break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u16 = *(u16_t*)v; break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u16 = *(u32_t*)v; break;
case VTYPE_INT64:
case VTYPE_UINT64: _.u16 = *(u64_t*)v; break;
}
break;
if (xt == VTYPE_UINT64 || xt == VTYPE_UINT32) {
_.ptr = memndup(&_.u64, sizeof(_.u64));
} else if (is_big_endian) {
_.u64 <<= 32;
if (xt == VTYPE_BOOLEAN) _.u8 = (_.u32 == 0) ? 0 : 1;
else if (xt == VTYPE_UINT8 || xt == VTYPE_INT8) {
_.u32 <<= 24;
} else if (xt == VTYPE_UINT16 || xt == VTYPE_INT16) {
_.u32 <<= 16;
}
} else if (xt == VTYPE_BOOLEAN) _.u32 = (_.u32 == 0) ? 0 : 1;
}
case VTYPE_INT32:
case VTYPE_UINT32: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u32 = *(u8_t*)v; break;
case VTYPE_INT16:
case VTYPE_UINT16: _.u32 = *(u16_t*)v; break;
case VTYPE_INT32:
case VTYPE_UINT32: _.u32 = *(u32_t*)v; break;
case VTYPE_INT64:
case VTYPE_UINT64: _.u32 = *(u64_t*)v; break;
}
break;
return _.ptr;
}
case VTYPE_INT64:
case VTYPE_UINT64: switch (t) { default: goto default_;
case VTYPE_INT8:
case VTYPE_UINT8: _.u64 = *(u8_t*)v; goto u64_;
case VTYPE_INT16:
case VTYPE_UINT16: _.u64 = *(u16_t*)v; goto u64_;
case VTYPE_INT32:
case VTYPE_UINT32: _.u64 = *(u32_t*)v; goto u64_;
case VTYPE_INT64:
case VTYPE_UINT64: _.u64 = *(u64_t*)v;
u64_: if (!is_x64) _.ptr = memndup(&_.u64, sizeof(_.u64));
break;
}
break;
case VTYPE_FLOAT: switch (t) { default: goto default_;
case VTYPE_DOUBLE: _.f = *(dbl_t*)v; goto f_;
case VTYPE_LDOUBLE: _.f = *(ldbl_t*)v; goto f_;
case VTYPE_FLOAT: _.f = *(fl_t*)v;
f_: if (!is_permissible(fl_t)) _.ptr = memndup(&_.f, sizeof(_.f));
break;
}
break;
static vnode_t create_float(vtype xt, const void* v, vtype t) {
var_t _;
case VTYPE_DOUBLE: switch (t) { default: goto default_;
case VTYPE_FLOAT: _.d = *(fl_t*)v; goto d_;
case VTYPE_LDOUBLE: _.d = *(ldbl_t*)v; goto d_;
case VTYPE_DOUBLE: _.d = *(dbl_t*)v;
d_: if (!is_permissible(dbl_t)) _.ptr = memndup(&_.d, sizeof(_.d));
break;
}
break;
if (t == VTYPE_FLOAT) {
_.ld = *(fl_t*)v;
} else if (t == VTYPE_DOUBLE) {
_.ld = *(dbl_t*)v;
} else { // (t == VTYPE_LDOUBLE)
_.ld = *(ldbl_t*)v;
}
case VTYPE_LDOUBLE: switch (t) { default: goto default_;
case VTYPE_FLOAT: _.ld = *(fl_t*)v; goto ld_;
case VTYPE_DOUBLE: _.ld = *(dbl_t*)v; goto ld_;
case VTYPE_LDOUBLE: _.ld = *(ldbl_t*)v;
ld_: if (!is_permissible(ldbl_t)) _.ptr = memndup(&_.ld, sizeof(_.ld));
break;
}
break;
if (xt == VTYPE_FLOAT) {
_.f = _.ld;
if (!is_permissible(fl_t)) {
_.ptr = memndup(&_.f, sizeof(_.f));
}
} else if (xt == VTYPE_DOUBLE) {
_.f = _.d;
if (!is_permissible(dbl_t)) {
_.ptr = memndup(&_.d, sizeof(_.d));
}
} else { // (xt == VTYPE_LDOUBLE)
if (!is_permissible(ldbl_t)) {
_.ptr = memndup(&_.ld, sizeof(_.ld));
}
}
return _.ptr;
@ -124,8 +112,7 @@ vnode_t libcdsb_vnode_create(const void* v, vtype t) {
break;
case VTYPE_INT64:
case VTYPE_UINT64:
u64_: if (is_x64) _.u64 = *(u64_t*)v;
case VTYPE_UINT64: if (is_x64) _.u64 = *(u64_t*)v;
else _.ptr = memndup(v, sizeof(u64_t));
break;
@ -244,10 +231,10 @@ vnode_t libcdsb_vnode_create_target(vtype xt, const void* v, vtype t) {
if (is_integer(xt)) {
tvalue_assert(t);
return create_number(xt, v, t);
return create_value(xt, v, t);
} else if (is_float(xt)) {
tfloat_assert(t);
return create_number(xt, v, t);
return create_float(xt, v, t);
}
type_assert(xt, t);

92
src/vtype-extra.c Normal file
View File

@ -0,0 +1,92 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <float.h>
#include <stdio.h>
#include "__internal/include.h"
/*#####################################################################################################################*/
#define sh__(a) #a
#define s__(a) sh__(a)
#define fstring(v) _Generic((v),\
unsigned char: "%hhu", signed char: "%hhd",\
unsigned short: "%hu", signed short: "%hd",\
unsigned int: "%u", signed int: "%d",\
unsigned long: "%lu", signed long: "%ld",\
unsigned long long: "%llu", signed long long: "%lld",\
float: "%."s__(FLT_DECIMAL_DIG)"g",\
double: "%."s__(DBL_DIG)"lg",\
long double: "%."s__(DBL_DECIMAL_DIG)"Lg")
#define stringify(v) sprintf(STRINGIFY_BUFFER, fstring(v), (v))
static _Thread_local char STRINGIFY_BUFFER[64];
/*#####################################################################################################################*/
const size_t LIBCDSB_VTYPE_SIZES[18] = {
sizeof(void*), sizeof(_Bool),
sizeof(u8_t), sizeof(u16_t), sizeof(u32_t), sizeof(u64_t),
sizeof(s8_t), sizeof(s16_t), sizeof(s32_t), sizeof(s64_t),
sizeof(fl_t), sizeof(dbl_t), sizeof(ldbl_t),
sizeof(str_t), sizeof(map_t), sizeof(arr_t),
sizeof(list_t), sizeof(set_t)
};
/*#####################################################################################################################*/
const char* libcdsb_vtype_name(vtype t) {
switch (t) { default: abort();
case VTYPE_POINTER: return "VTYPE_POINTER";
case VTYPE_BOOLEAN: return "VTYPE_BOOLEAN";
case VTYPE_UINT8: return "VTYPE_UINT8";
case VTYPE_UINT16: return "VTYPE_UINT16";
case VTYPE_UINT32: return "VTYPE_UINT32";
case VTYPE_UINT64: return "VTYPE_UINT64";
case VTYPE_INT8: return "VTYPE_INT8";
case VTYPE_INT16: return "VTYPE_INT16";
case VTYPE_INT32: return "VTYPE_INT32";
case VTYPE_INT64: return "VTYPE_INT64";
case VTYPE_FLOAT: return "VTYPE_FLOAT";
case VTYPE_DOUBLE: return "VTYPE_DOUBLE";
case VTYPE_LDOUBLE: return "VTYPE_LDOUBLE";
case VTYPE_STRING: return "VTYPE_STRING";
case VTYPE_MAP: return "VTYPE_MAP";
case VTYPE_ARRAY: return "VTYPE_ARRAY";
case VTYPE_LIST: return "VTYPE_LIST";
case VTYPE_SET: return "VTYPE_SET";
}
}
const char* libcdsb_vtype_stringify(const void* v, vtype t) {
if (is_null(v) || (t == VTYPE_POINTER && is_null(*(void**)v))) return "null";
if (t == VTYPE_BOOLEAN) return (*(vtype_bool*)v) ? "true" : "false";
if (t == VTYPE_STRING) return *(char**)v;
switch (t) {
case VTYPE_INT8: stringify(*( s8_t*)v); break;
case VTYPE_INT16: stringify(*( s16_t*)v); break;
case VTYPE_INT32: stringify(*( s32_t*)v); break;
case VTYPE_INT64: stringify(*( s64_t*)v); break;
case VTYPE_UINT8: stringify(*( u8_t*)v); break;
case VTYPE_UINT16: stringify(*( u16_t*)v); break;
case VTYPE_UINT32: stringify(*( u32_t*)v); break;
case VTYPE_UINT64: stringify(*( u64_t*)v); break;
case VTYPE_FLOAT: stringify(*( fl_t*)v); break;
case VTYPE_DOUBLE: stringify(*( dbl_t*)v); break;
case VTYPE_LDOUBLE: stringify(*(ldbl_t*)v); break;
case VTYPE_POINTER: sprintf(STRINGIFY_BUFFER, (sizeof(void*) == 8) ? "0x%016lx" : "0x%08x", (uintptr_t)*(void**)v); break;
default: sprintf(STRINGIFY_BUFFER, "<%s>", libcdsb_vtype_name(t));
break;
}
return STRINGIFY_BUFFER;
}

View File

@ -1,6 +1,7 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "__internal/assert.h"
#include "__internal/include.h"
#define max_align _Alignof(max_align_t)
@ -8,212 +9,70 @@
/*#####################################################################################################################*/
ainline(vtype expandu8(u32_t* v)) {
if (is_little_endian) {
*v &= 0x000000ff;
} else {
*v >>= 24;
static vtype normalize_value(u64_t* d, const void* v, vtype t) {
if (t == VTYPE_BOOLEAN || t == VTYPE_UINT8) {
*d = *(u8_t*)v;
t = VTYPE_UINT64;
} else if (t == VTYPE_UINT16) {
*d = *(u16_t*)v;
t = VTYPE_UINT64;
} else if (t == VTYPE_UINT32 || (sizeof(void*) == 4 && t == VTYPE_POINTER)) {
*d = *(u32_t*)v;
t = VTYPE_UINT64;
} else if (t == VTYPE_UINT64 || (sizeof(void*) == 8 && t == VTYPE_POINTER)) {
*d = *(u64_t*)v;
t = VTYPE_UINT64;
} else if (t == VTYPE_INT8) {
*d = *(s8_t*)v;
t = (*(s8_t*)v >= 0) ? VTYPE_UINT64 : VTYPE_INT64;
} else if (t == VTYPE_INT16) {
*d = *(s16_t*)v;
t = (*(s16_t*)v >= 0) ? VTYPE_UINT64 : VTYPE_INT64;
} else if (t == VTYPE_INT32) {
*d = *(s32_t*)v;
t = (*(s32_t*)v >= 0) ? VTYPE_UINT64 : VTYPE_INT64;
} else if (t == VTYPE_INT64) {
*d = *(s64_t*)v;
t = (*(s64_t*)v >= 0) ? VTYPE_UINT64 : VTYPE_INT64;
}
return VTYPE_UINT32;
return t;
}
ainline(vtype expandu16(u32_t* v)) {
if (is_little_endian) {
*v &= 0x0000ffff;
} else {
*v >>= 16;
}
return VTYPE_UINT32;
}
ainline(vtype expands8(u32_t* v)) {
if (is_big_endian)
*v >>= 24;
*v |= 0xffffff00;
return VTYPE_INT32;
}
ainline(vtype expands16(u32_t* v)) {
if (is_big_endian)
*v >>= 16;
*v |= 0xffff0000;
return VTYPE_INT32;
}
/*#####################################################################################################################*/
const size_t LIBCDSB_VTYPE_SIZES[18] = {
sizeof(void*), sizeof(_Bool),
sizeof(u8_t), sizeof(u16_t), sizeof(u32_t), sizeof(u64_t),
sizeof(s8_t), sizeof(s16_t), sizeof(s32_t), sizeof(s64_t),
sizeof(fl_t), sizeof(dbl_t), sizeof(ldbl_t),
sizeof(str_t), sizeof(map_t), sizeof(arr_t),
sizeof(list_t), sizeof(set_t)
};
/*#####################################################################################################################*/
int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype t1) {
if (t0 == t1) return libcdsb_vtype_compare_values_eq(s0, s1, t0);
int c;
size_t _Alignas(max_align) v0[max_size/sizeof(size_t)];
size_t _Alignas(max_align) v1[max_size/sizeof(size_t)];
if (is_integer(t0) && is_integer(t1)) {
u64_t v0, v1;
c = t0 - t1;
t0 = normalize_value(&v0, s0, t0);
t1 = normalize_value(&v1, s1, t1);
if (t0 >= VTYPE_STRING || t1 >= VTYPE_STRING) {
static int(*compare[5])(const void*,const void*) = {
(void*)string_compare,
(void*) map_compare,
(void*) array_compare,
(void*) list_compare,
(void*) vset_compare
};
if (c) return c;
t0 -= VTYPE_STRING;
return compare[t0](s0, s1);
}
if (t0 == VTYPE_POINTER) t0 = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (t1 == VTYPE_POINTER) t1 = (sizeof(void*) == 8) ? VTYPE_UINT64 : VTYPE_UINT32;
if (t0 == VTYPE_BOOLEAN) t0 = VTYPE_UINT8;
if (t1 == VTYPE_BOOLEAN) t1 = VTYPE_UINT8;
if (!c) {
if (s0 == s1)
return 0;
if (t0 == VTYPE_LDOUBLE) {
*(ldbl_t*)v0 = *(ldbl_t*)s0 - *(ldbl_t*)s1;
ldouble_:
if (!*(ldbl_t*)v0)
return 0;
return *(ldbl_t*)v0 < 0 ? -1 : 1;
} else if (t0 == VTYPE_DOUBLE) {
*(ldbl_t*)v0 = *(dbl_t*)s0 - *(dbl_t*)s1;
goto ldouble_;
} else if (t0 == VTYPE_FLOAT) {
*(ldbl_t*)v0 = *(fl_t*)s0 - *(fl_t*)s1;
goto ldouble_;
if (t0 == t1) {
if (t0 > VTYPE_UINT64)
return (s64_t)v0 - (s64_t)v1;
return v0 - v1;
}
if (t0 == VTYPE_UINT8)
return *(u8_t*)s0 - *(u8_t*)s1;
if (t0 == VTYPE_UINT16)
return *(u16_t*)s0 - *(u16_t*)s1;
if (t0 == VTYPE_UINT32)
return *(u32_t*)s0 - *(u32_t*)s1;
return t1 - t0;
return *(u64_t*)s0 - *(u64_t*)s1;
} else if (is_float(t0) && is_float(t1)) {
ldbl_t v;
} else if (t0 >= VTYPE_FLOAT && t1 >= VTYPE_FLOAT) {
if (t0 == VTYPE_FLOAT) {
*(ldbl_t*)v0 = *(fl_t*)s0;
} else if (t0 == VTYPE_DOUBLE) {
*(ldbl_t*)v0 = *(dbl_t*)s0;
} else if (t0 == VTYPE_LDOUBLE) {
*(ldbl_t*)v0 = *(ldbl_t*)s0;
}
if (t0 == VTYPE_FLOAT) v = *(fl_t*)s0;
else if (t0 == VTYPE_DOUBLE) v = *(dbl_t*)s0;
else v = *(ldbl_t*)s0;
if (t1 == VTYPE_FLOAT) {
*(ldbl_t*)v0 -= *(fl_t*)s1;
} else if (t1 == VTYPE_DOUBLE) {
*(ldbl_t*)v0 -= *(dbl_t*)s1;
} else {
*(ldbl_t*)v0 -= *(ldbl_t*)s1;
}
if (t1 == VTYPE_FLOAT) v -= *(fl_t*)s1;
else if (t1 == VTYPE_DOUBLE) v -= *(dbl_t*)s1;
else v -= *(ldbl_t*)s1;
goto ldouble_;
}
if (v < 0) return -1;
memcpy(v0, s0, 8);
memcpy(v1, s1, 8);
if (t0 <= VTYPE_UINT64 && t1 <= VTYPE_UINT64) {
if (t0 == VTYPE_UINT8) {
t0 = expandu8((void*)v0);
} else if (t0 == VTYPE_UINT16) {
t0 = expandu16((void*)v0);
} else if (t0 == VTYPE_UINT64 && *(u64_t*)v0 <= UINT32_MAX) {
t0 = VTYPE_UINT32;
}
if (t1 == VTYPE_UINT8) {
t1 = expandu8((void*)v1);
} else if (t1 == VTYPE_UINT16) {
t1 = expandu16((void*)v1);
} else if (t1 == VTYPE_UINT64 && *(u64_t*)v1 <= UINT32_MAX) {
t0 = VTYPE_UINT32;
}
unsigned_: c = t0 - t1;
if (!c) return *(u32_t*)v0 - *(u32_t*)v1;
} else if (t0 <= VTYPE_INT64 && t1 <= VTYPE_INT64) {
if (t0 == VTYPE_INT8) {
t0 = (*(u8_t*)v0 <= INT8_MAX) ? expandu8((void*)v0) : expands8((void*)v0);
} else if (t0 == VTYPE_INT16) {
t0 = (*(u16_t*)v0 <= INT16_MAX) ? expandu16((void*)v0) : expands16((void*)v0);
} else if (t0 == VTYPE_INT32) {
if (*(u32_t*)v0 <= INT32_MAX) {
t0 = VTYPE_UINT32;
}
} else if (t0 == VTYPE_INT64) {
if (*(u64_t*)v0 <= UINT32_MAX) {
t0 = VTYPE_UINT32;
} else if (*(u64_t*)v0 <= INT64_MAX) {
t0 = VTYPE_UINT64;
}
}
if (t1 == VTYPE_INT8) {
t1 = (*(u8_t*)v1 <= INT8_MAX) ? expandu8((void*)v1) : expands8((void*)v1);
} else if (t1 == VTYPE_INT16) {
t1 = (*(u16_t*)v1 <= INT16_MAX) ? expandu16((void*)v1) : expands16((void*)v1);
} else if (t1 == VTYPE_INT32) {
if (*(u32_t*)v1 <= INT32_MAX) {
t1 = VTYPE_UINT32;
}
} else if (t1 == VTYPE_INT64) {
if (*(u64_t*)v1 <= UINT32_MAX) {
t1 = VTYPE_UINT32;
} else if (*(u64_t*)v1 <= INT64_MAX) {
t1 = VTYPE_UINT64;
}
}
if (t0 <= VTYPE_UINT64 && t1 <= VTYPE_UINT64)
goto unsigned_;
if (t0 <= VTYPE_UINT64) return 1;
if (t1 <= VTYPE_UINT64) return -1;
c = t0 - t1;
if (!c) return *(u32_t*)v1 - *(u32_t*)v0;
}
return c;
return (v > 0) ? 1 : 0;
} else return t0 - t1;
}
/*#####################################################################################################################*/
@ -226,7 +85,7 @@ int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t) {
switch (t) { default: abort();
case VTYPE_INT8: return *(s8_t*)s0 - *(s8_t*)s1;
case VTYPE_INT8: return *(s8_t*)s0 - *(s8_t*)s1;
case VTYPE_INT16: return *(s16_t*)s0 - *(s16_t*)s1;
case VTYPE_INT32: return *(s32_t*)s0 - *(s32_t*)s1;
case VTYPE_INT64: return *(s64_t*)s0 - *(s64_t*)s1;