From 17e4bb8e19d48ea6a17a620845501e3a83338e05 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 11 Apr 2025 13:57:17 +0100 Subject: [PATCH] core: add functions to calculate the length of an int --- core/include/blue/core/misc.h | 91 +++++++++++++++++++++++++++++++++-- core/misc.c | 38 +++++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 core/misc.c diff --git a/core/include/blue/core/misc.h b/core/include/blue/core/misc.h index 529bf72..6484f3f 100644 --- a/core/include/blue/core/misc.h +++ b/core/include/blue/core/misc.h @@ -1,10 +1,16 @@ -#ifndef BLUELIB_MISB_H_ -#define BLUELIB_MISB_H_ +#ifndef BLUE_CORE_MISC_H_ +#define BLUE_CORE_MISC_H_ + +#include +#include #ifndef _Nonnull #define _Nonnull #endif +#define b_min(type, x, y) (z__b_min_##type(x, y)) +#define b_max(type, x, y) (z__b_max_##type(x, y)) + #define b_unbox(type, box, member) \ ((type *_Nonnull)((box) ? (uintptr_t)(box) - (offsetof(type, member)) : 0)) @@ -28,4 +34,83 @@ #define BLUE_API extern #endif -#endif // B_MISB_H_ +static inline char z__b_min_char(char x, char y) +{ + return x < y ? x : y; +} +static inline unsigned char z__b_min_uchar(unsigned char x, unsigned char y) +{ + return x < y ? x : y; +} +static inline int z__b_min_int(int x, int y) +{ + return x < y ? x : y; +} +static inline unsigned int z__b_min_uint(unsigned int x, unsigned int y) +{ + return x < y ? x : y; +} +static inline long z__b_min_long(long x, long y) +{ + return x < y ? x : y; +} +static inline unsigned int z__b_min_ulong(unsigned long x, unsigned long y) +{ + return x < y ? x : y; +} +static inline long long z__b_min_longlong(long long x, long long y) +{ + return x < y ? x : y; +} +static inline unsigned long long z__b_min_ulonglong( + unsigned long long x, unsigned long long y) +{ + return x < y ? x : y; +} +static inline size_t z__b_min_size_t(size_t x, size_t y) +{ + return x < y ? x : y; +} + +static inline char z__b_max_char(char x, char y) +{ + return x > y ? x : y; +} +static inline unsigned char z__b_max_uchar(unsigned char x, unsigned char y) +{ + return x > y ? x : y; +} +static inline int z__b_max_int(int x, int y) +{ + return x > y ? x : y; +} +static inline unsigned int z__b_max_uint(unsigned int x, unsigned int y) +{ + return x > y ? x : y; +} +static inline long z__b_max_long(long x, long y) +{ + return x > y ? x : y; +} +static inline unsigned int z__b_max_ulong(unsigned long x, unsigned long y) +{ + return x > y ? x : y; +} +static inline long long z__b_max_longlong(long long x, long long y) +{ + return x > y ? x : y; +} +static inline unsigned long long z__b_max_ulonglong( + unsigned long long x, unsigned long long y) +{ + return x > y ? x : y; +} +static inline size_t z__b_max_size_t(size_t x, size_t y) +{ + return x > y ? x : y; +} + +BLUE_API size_t b_int_length(intptr_t v); +BLUE_API size_t b_uint_length(uintptr_t v); + +#endif // BLUE_CORE_MISC_H_ diff --git a/core/misc.c b/core/misc.c new file mode 100644 index 0000000..b2294e7 --- /dev/null +++ b/core/misc.c @@ -0,0 +1,38 @@ +#include + +#define LENGTH_RET(v, boundary, len) \ + if ((v) < (boundary)) \ + return (len); + +size_t b_int_length(intptr_t v) +{ + size_t len = 0; + if (v < 0) { + v *= -1; + len++; + } + + return len + b_uint_length(v); +} + +size_t b_uint_length(uintptr_t v) +{ + LENGTH_RET(v, 10, 1); + LENGTH_RET(v, 100, 2); + LENGTH_RET(v, 1000, 3); + LENGTH_RET(v, 10000, 4); + LENGTH_RET(v, 100000, 5); + LENGTH_RET(v, 1000000, 6); + LENGTH_RET(v, 10000000, 7); + LENGTH_RET(v, 100000000, 8); + LENGTH_RET(v, 1000000000, 9); + LENGTH_RET(v, 10000000000, 10); + + size_t len = 0; + while (v > 0) { + v /= 10; + len++; + } + + return len; +}