core: add functions to calculate the length of an int
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
#ifndef BLUELIB_MISB_H_
|
||||
#define BLUELIB_MISB_H_
|
||||
#ifndef BLUE_CORE_MISC_H_
|
||||
#define BLUE_CORE_MISC_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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_
|
||||
|
||||
38
core/misc.c
Normal file
38
core/misc.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <blue/core/misc.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user