Turnstone Operating System
Loading...
Searching...
No Matches
utils.h File Reference

util functions and macros More...

#include <types.h>

Macros

#define ___UTILS_H   0
 
#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
 
#define MIN(x, y)   (x < y ? x : y)
 
#define MAX(x, y)   (x > y ? x : y)
 
#define ABS(x)   (x >= 0?x:-1 * x)
 
#define sizeof_field(s, m)   (sizeof((((s*)0)->m)))
 
#define typeof_field(s, m)   (typeof(((s*)0)->m))
 
#define offsetof_field(s, m)   ( ((uint64_t)&((s*)0)->m) )
 
#define itoa_with_buffer(buf, number)   ito_base_with_buffer(buf, number, 10)
 
#define utoa_with_buffer(buf, number)   uto_base_with_buffer(buf, number, 10)
 
#define utoh_with_buffer(buf, number)   uto_base_with_buffer(buf, number, 16)
 
#define ftoa_with_buffer(buf, number)   fto_base_with_buffer(buf, number, 6, 10)
 
#define ftoh_with_buffer(buf, number)   fto_base_with_buffer(buf, number, 6, 16)
 
#define ftoa_with_buffer_and_prec(buf, number, prec)   fto_base_with_buffer(buf, number, prec, 10)
 
#define ftoh_with_buffer_and_prec(buf, number, prec)   fto_base_with_buffer(buf, number, prec, 16)
 
#define BYTE_SWAP16(n)   byte_swap(n, 2)
 
#define BYTE_SWAP32(n)   byte_swap(n, 4)
 
#define BYTE_SWAP64(n)   byte_swap(n, 8)
 
#define DIGIT_TO_HEX(r)   (((r) < 10)?(r) + 48:(r) + 55)
 
#define ROTLEFT(a, b, c)   (((a) << (b)) | ((a) >> (c - (b))))
 
#define ROTRIGHT(a, b, c)   (((a) >> (b)) | ((a) << (c - (b))))
 
#define ROTLEFT8(a, b)   ROTLEFT(a, b, 8)
 
#define ROTRIGHT8(a, b)   ROTRIGHT(a, b, 8)
 
#define ROTLEFT16(a, b)   ROTLEFT(a, b, 16)
 
#define ROTRIGHT16(a, b)   ROTRIGHT(a, b, 16)
 
#define ROTLEFT32(a, b)   ROTLEFT(a, b, 32)
 
#define ROTRIGHT32(a, b)   ROTRIGHT(a, b, 32)
 
#define ROTLEFT64(a, b)   ROTLEFT((uint64_t)a, (uint64_t)b, 64ULL)
 
#define ROTRIGHT64(a, b)   ROTRIGHT((uint64_t)a, (uint64_t)b, 64ULL)
 

Functions

number_t power (number_t base, number_t p)
 power base with p More...
 
int8_t ito_base_with_buffer (char_t *buffer, number_t number, number_t base)
 converts integer to string More...
 
int8_t uto_base_with_buffer (char_t *buffer, unumber_t number, number_t base)
 converts unsigned integer to string More...
 
int8_t fto_base_with_buffer (char_t *buffer, float64_t number, number_t prec, number_t base)
 converts float to string More...
 
static uint64_t byte_swap (uint64_t num, uint8_t bc)
 
uint8_t byte_count (const uint64_t num)
 
static boolean_t bit_test32 (uint32_t *data, uint8_t bitloc)
 test bit value of given data at bitloc More...
 
static boolean_t bit_clear32 (volatile uint32_t *data, uint8_t bitloc)
 clears bit value of given data at bitloc More...
 
static boolean_t bit_set32 (volatile uint32_t *data, uint8_t bitloc)
 sets bit value of given data at bitloc More...
 
static boolean_t bit_test (uint64_t *data, uint8_t bitloc)
 test bit value of given data at bitloc More...
 
static boolean_t bit_clear (uint64_t *data, uint8_t bitloc)
 clears bit value of given data at bitloc More...
 
static boolean_t bit_set (uint64_t *data, uint8_t bitloc)
 sets bit value of given data at bitloc More...
 
static boolean_t bit_change (uint64_t *data, uint8_t bitloc)
 changes bit value of given data at bitloc More...
 
static uint64_t bit_most_significant (uint64_t num)
 gets most significant bit location More...
 
uint64_t read_memio (uint64_t va, uint8_t size)
 
void write_memio (uint64_t va, uint64_t val, uint8_t size)
 
static uint64_t reverse_bits (uint64_t bits, uint8_t bit_count)
 
boolean_t isalpha (char_t c)
 
boolean_t isdigit (char_t c)
 
boolean_t isalnum (char_t c)
 
boolean_t isxdigit (char_t c)
 
boolean_t islower (char_t c)
 
boolean_t isupper (char_t c)
 
boolean_t isspace (char_t c)
 
boolean_t ispunct (char_t c)
 
boolean_t isprint (char_t c)
 
boolean_t isgraph (char_t c)
 
boolean_t iscntrl (char_t c)
 
boolean_t isblank (char_t c)
 
boolean_t isascii (char_t c)
 
boolean_t isalnumw (char_t c)
 
const char_trandstr (uint32_t len)
 

Detailed Description

util functions and macros

This work is licensed under TURNSTONE OS Public License. Please read and understand latest version of Licence.

Macro Definition Documentation

◆ ___UTILS_H

#define ___UTILS_H   0

prevent duplicate header error macro

◆ MAX

#define MAX (   x,
 
)    (x > y ? x : y)

find max of two numbers

Parameters
[in]xfirst number
[in]yfirst number
Returns
maximum of x or y

◆ MIN

#define MIN (   x,
 
)    (x < y ? x : y)

find min of two numbers

Parameters
[in]xfirst number
[in]yfirst number
Returns
minimum of x or y

Function Documentation

◆ bit_change()

static boolean_t bit_change ( uint64_t data,
uint8_t  bitloc 
)
inlinestatic

changes bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
old value

◆ bit_clear()

static boolean_t bit_clear ( uint64_t data,
uint8_t  bitloc 
)
inlinestatic

clears bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
old value

◆ bit_clear32()

static boolean_t bit_clear32 ( volatile uint32_t data,
uint8_t  bitloc 
)
inlinestatic

clears bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
old value

◆ bit_most_significant()

static uint64_t bit_most_significant ( uint64_t  num)
inlinestatic

gets most significant bit location

Parameters
[in]numnumber to get
Returns
bit location

◆ bit_set()

static boolean_t bit_set ( uint64_t data,
uint8_t  bitloc 
)
inlinestatic

sets bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
old value

◆ bit_set32()

static boolean_t bit_set32 ( volatile uint32_t data,
uint8_t  bitloc 
)
inlinestatic

sets bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
old value

◆ bit_test()

static boolean_t bit_test ( uint64_t data,
uint8_t  bitloc 
)
inlinestatic

test bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
bit value

◆ bit_test32()

static boolean_t bit_test32 ( uint32_t data,
uint8_t  bitloc 
)
inlinestatic

test bit value of given data at bitloc

Parameters
[in]databit array
[in]bitlocbit location at data
Returns
bit value

◆ fto_base_with_buffer()

int8_t fto_base_with_buffer ( char_t buffer,
float64_t  number,
number_t  prec,
number_t  base 
)

converts float to string

Parameters
[in]bufferdestination buffer
[in]numberto convert
[in]baseto convert
Returns
0 if successed

buffer should be enough to take data. it's malloc free

◆ ito_base_with_buffer()

int8_t ito_base_with_buffer ( char_t buffer,
number_t  number,
number_t  base 
)

converts integer to string

Parameters
[in]bufferdestination buffer
[in]numberto convert
[in]baseto convert
Returns
0 if successed

buffer should be enough to take data. it's malloc free

◆ power()

number_t power ( number_t  base,
number_t  p 
)

power base with p

Parameters
[in]basethe base
[in]pthe power
Returns
base^p with fast power algorithm

◆ uto_base_with_buffer()

int8_t uto_base_with_buffer ( char_t buffer,
unumber_t  number,
number_t  base 
)

converts unsigned integer to string

Parameters
[in]bufferdestination buffer
[in]numberunsigned number to convert
[in]baseto convert
Returns
0 if successed

buffer should be enough to take data. it's malloc free