Update the include/bits implementation

This commit is contained in:
Gregory Lirent 2022-08-22 12:03:02 +03:00
parent e92ff17be4
commit 205c8337c2
3 changed files with 39 additions and 17 deletions

View File

@ -2,8 +2,8 @@
/* Copyright © 2022 Gregory Lirent */
#include <stdlib.h>
#include "../include/extra/cstring.h"
#include "__internal/include.h"
#undef aligned_alloc
#undef malloc
#undef realloc

View File

@ -1,9 +1,27 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../include/extra/cstring.h"
#include "../modules/libunic/include.h"
#include "__internal/include.h"
static _Thread_local int CHAR_BUFFER_POS = 0;
static _Thread_local char CHAR_BUFFER[16][5];
const char* libcdsb_char_to_cstring(int c) {
char* p;
if (CHAR_BUFFER_POS > 15)
CHAR_BUFFER_POS = 0;
if (is_null(p = tochar_unicode(CHAR_BUFFER[CHAR_BUFFER_POS], c))) {
CHAR_BUFFER[CHAR_BUFFER_POS][0] = 0;
} else *p = 0;
return CHAR_BUFFER[CHAR_BUFFER_POS++];
}
size_t libcdsb_strlen(const char* s) {
static const size_t m = (sizeof(size_t) == 8) ? 0x8080808080808080UL : 0x80808080UL;
static const size_t d = (sizeof(size_t) == 8) ? 0x0101010101010101UL : 0x01010101UL;

View File

@ -19,13 +19,13 @@
double: "%."s__(DBL_DIG)"lg",\
long double: "%."s__(LDBL_DIG)"Lg")
#define stringify(v) sprintf(STRINGIFY_BUFFER, fstring(v), (v))
#define stringify(v) sprintf(STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS], fstring(v), (v))
static _Thread_local char STRINGIFY_BUFFER[64];
static _Thread_local int STRINGIFY_BUFFER_POS = 0;
static _Thread_local char STRINGIFY_BUFFER[16][64];
/*#####################################################################################################################*/
const size_t LIBCDSB_VTYPE_SIZES[19] = {
sizeof(void*), sizeof(bool),
sizeof(u8_t), sizeof(u16_t), sizeof(u32_t), sizeof(u64_t),
@ -35,12 +35,13 @@ const size_t LIBCDSB_VTYPE_SIZES[19] = {
sizeof(list_t), sizeof(set_t), sizeof(dict_t)
};
/*#####################################################################################################################*/
const char* libcdsb_vtype_name(vtype t) {
switch (t) { default: abort();
switch (t) {
#ifndef NDEBUG
default: abort();
#endif
case VTYPE_POINTER: return "VTYPE_POINTER";
case VTYPE_BOOLEAN: return "VTYPE_BOOLEAN";
case VTYPE_UINT8: return "VTYPE_UINT8";
@ -69,6 +70,9 @@ const char* libcdsb_vtype_stringify(const void* v, vtype t) {
if (t == VTYPE_BOOLEAN) return (*(vtype_bool*)v) ? "true" : "false";
if (t == VTYPE_STRING) return *(char**)v;
if (STRINGIFY_BUFFER_POS > 15)
STRINGIFY_BUFFER_POS = 0;
switch (t) {
case VTYPE_INT8: stringify(*( s8_t*)v); break;
case VTYPE_INT16: stringify(*( s16_t*)v); break;
@ -79,25 +83,25 @@ const char* libcdsb_vtype_stringify(const void* v, vtype t) {
case VTYPE_UINT32: stringify(*( u32_t*)v); break;
case VTYPE_UINT64: stringify(*( u64_t*)v); break;
case VTYPE_FLOAT: if (abs(*(fl_t*)v) <= FLT_EPSILON) {
STRINGIFY_BUFFER[0] = 0x30;
STRINGIFY_BUFFER[1] = 0x00;
STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS][0] = 0x30;
STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS][1] = 0x00;
} else stringify(*(fl_t*)v);
break;
case VTYPE_DOUBLE: if (abs(*(dbl_t*)v) <= DBL_EPSILON) {
STRINGIFY_BUFFER[0] = 0x30;
STRINGIFY_BUFFER[1] = 0x00;
STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS][0] = 0x30;
STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS][1] = 0x00;
} else stringify(*(dbl_t*)v);
break;
case VTYPE_LDOUBLE: if (abs(*(ldbl_t*)v) <= LDBL_EPSILON) {
STRINGIFY_BUFFER[0] = 0x30;
STRINGIFY_BUFFER[1] = 0x00;
STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS][0] = 0x30;
STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS][1] = 0x00;
} else stringify(*(ldbl_t*)v);
break;
case VTYPE_POINTER: sprintf(STRINGIFY_BUFFER, (sizeof(void*) == 8) ? "0x%016lx" : "0x%08x", (uintptr_t)*(void**)v); break;
case VTYPE_POINTER: sprintf(STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS], (sizeof(void*) == 8) ? "0x%016lx" : "0x%08x", (uintptr_t)*(void**)v); break;
default: sprintf(STRINGIFY_BUFFER, "<%s>", libcdsb_vtype_name(t));
default: sprintf(STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS], "<%s>", libcdsb_vtype_name(t));
break;
}
return STRINGIFY_BUFFER;
return STRINGIFY_BUFFER[STRINGIFY_BUFFER_POS++];
}