From 7903ae7711f323697bbc42ad756c8ea0138c70dc Mon Sep 17 00:00:00 2001 From: A404M Date: Tue, 18 Feb 2025 12:22:58 +0330 Subject: add f16 f32 f64 f128 --- src/utils/string.c | 39 ++++++++++++++++++++++++++++++++++++--- src/utils/string.h | 6 ++++-- src/utils/type.c | 21 +++++++++++++++++++++ src/utils/type.h | 20 ++++++++++++++++++++ 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/utils/type.c create mode 100644 src/utils/type.h (limited to 'src/utils') diff --git a/src/utils/string.c b/src/utils/string.c index 5f9b7de..ee8a354 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -1,5 +1,6 @@ #include "string.h" #include "memory.h" +#include "utils/type.h" #include #include @@ -15,8 +16,8 @@ size_t searchInStringArray(const char *array[], size_t array_size, return array_size; } -uint64_t decimalToU64(char *str_begin, char *str_end, bool *success) { - uint64_t result = 0; +u64 decimalToU64(char *str_begin, char *str_end, bool *success) { + u64 result = 0; while (str_begin < str_end) { if (*str_begin < '0' || *str_begin > '9') { @@ -32,7 +33,39 @@ uint64_t decimalToU64(char *str_begin, char *str_end, bool *success) { return result; } -char *u64ToString(uint64_t value) { +f128 numberToFloat(char *str_begin, char *str_end, bool *success) { + f64 left = 0; + f64 right = 0; + bool isPastPoint = false; + + while (str_begin < str_end) { + char c; + if (isPastPoint) { + c = *--str_end; + } else { + c = *str_begin++; + } + if (c >= '0' && c <= '9') { + if (isPastPoint) { + left *= 10; + left += c - '0'; + } else { + right /= 10; + right += c - '0'; + } + } else if (c == '.' && !isPastPoint) { + isPastPoint = true; + } else { + *success = false; + return 0; + } + } + + *success = true; + return left + right; +} + +char *u64ToString(u64 value) { char *str = a404m_malloc(21 * sizeof(*str)); size_t i = 0; diff --git a/src/utils/string.h b/src/utils/string.h index 8f92bc4..54130e5 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -1,10 +1,12 @@ #pragma once +#include "utils/type.h" #include #include size_t searchInStringArray(const char *array[], size_t array_size, const char *str, size_t str_size); -uint64_t decimalToU64(char *str_begin, char *str_end, bool *success); -char* u64ToString(uint64_t value); +u64 decimalToU64(char *str_begin, char *str_end, bool *success); +f128 numberToFloat(char *str_begin, char *str_end, bool *success); +char* u64ToString(u64 value); diff --git a/src/utils/type.c b/src/utils/type.c new file mode 100644 index 0000000..dea08ef --- /dev/null +++ b/src/utils/type.c @@ -0,0 +1,21 @@ +#include "type.h" + +#include + +void checkTypes() { + assert(sizeof(i8) == (8 / 8)); + assert(sizeof(i16) == (16 / 8)); + assert(sizeof(i32) == (32 / 8)); + assert(sizeof(i64) == (64 / 8)); + + assert(sizeof(u8) == (8 / 8)); + assert(sizeof(u16) == (16 / 8)); + assert(sizeof(u32) == (32 / 8)); + assert(sizeof(u64) == (64 / 8)); + + assert(sizeof(f16) == (16 / 8)); + assert(sizeof(f32) == (32 / 8)); + assert(sizeof(f64) == (64 / 8)); + assert(sizeof(f128) == (128 / 8)); +} + diff --git a/src/utils/type.h b/src/utils/type.h new file mode 100644 index 0000000..aad46df --- /dev/null +++ b/src/utils/type.h @@ -0,0 +1,20 @@ +#pragma once + +#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; + +typedef _Float16 f16; +typedef float f32; +typedef double f64; +typedef long double f128; + +void checkTypes(); -- cgit v1.2.3