rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 1367db74ff430bce862e75b33feb27f10544416a
parent d34aeecf2ef0463e6b40819432b37371afc2f122
Author: vaplv <vaplv@free.fr>
Date:   Thu,  1 May 2014 18:12:50 +0200

Add and rename several math macros

Add math tests

Diffstat:
Mcmake/CMakeLists.txt | 1+
Msrc/hash_table.h | 4++--
Msrc/math.h | 27+++++++++++++++++++--------
Msrc/mem_allocator.c | 6+++---
Asrc/test_math.c | 44++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -116,6 +116,7 @@ new_test(test_library rsys) new_test(test_list rsys) new_test(test_mem_allocator rsys) new_test(test_binary_heap rsys) +new_test(test_math) new_test(test_ref) new_test(test_signal rsys) new_test(test_str rsys) diff --git a/src/hash_table.h b/src/hash_table.h @@ -235,7 +235,7 @@ HTABLE_FUNC__(find_slot__)(const struct HTABLE__* htbl, HTABLE_KEY const* key) const char* used_pairs = NULL; ASSERT(htbl && key); /* The tbl size must be a pow of 2 */ - ASSERT(IS_POWER_OF_2(HTABLE_DATA_FUNC__(size_get)(&htbl->table))); + ASSERT(IS_POW2(HTABLE_DATA_FUNC__(size_get)(&htbl->table))); /* The tbl is not full */ ASSERT(htbl->table_size_in_use < HTABLE_DATA_FUNC__(size_get)(&htbl->table)); @@ -424,7 +424,7 @@ HTABLE_FUNC__(erase)(struct HTABLE__* htbl, HTABLE_KEY const* key) pairs = HTABLE_DATA_FUNC__(data_get)(&htbl->table); tbl_size = HTABLE_DATA_FUNC__(size_get)(&htbl->table); - ASSERT(IS_POWER_OF_2(tbl_size)); + ASSERT(IS_POW2(tbl_size)); used_pairs = darray_char_data_get(&htbl->table_slot_is_used); i = HTABLE_FUNC__(find_slot__)(htbl, key); diff --git a/src/math.h b/src/math.h @@ -3,19 +3,30 @@ #include "rsys.h" -#define RMAX(A, B) ((A) > (B) ? (A) : (B)) -#define RMIN(A, B) ((A) < (B) ? (A) : (B)) -#define IS_POWER_OF_2(A) (((A) & ((A)-1)) == 0 && (A) > 0) +#define MMAX(A, B) ((A) > (B) ? (A) : (B)) +#define MMIN(A, B) ((A) < (B) ? (A) : (B)) +#define IS_POW2(A) (((A) & ((A)-1)) == 0 && (A) > 0) +#define INF (1.0/0.0) +#define IS_INF(X) (X==INF) || (X==-INF) +#define PI 3.14159265358979323846 +#define RCP_PI 0.31830988618379067154 /* 1/ pi */ +#define SQRT2 1.41421356237309504880 /* sqrt( 2 ) */ + static FINLINE size_t round_up_pow2(const size_t i) { - size_t j = i - 1; - unsigned k; - for(k = 1; k < sizeof(int)*8; k <<= 1) { - j |= j >> k; + if(IS_POW2(i)) { + return i; + } else if(!i) { + return 1; + } else { + size_t j = i - 1; + unsigned k; + for(k = 1; k < sizeof(int)*8; k <<= 1) + j |= j >> k; + return j + 1; } - return j + 1; } static FINLINE float diff --git a/src/mem_allocator.c b/src/mem_allocator.c @@ -94,7 +94,7 @@ mem_alloc_aligned(const size_t size, const size_t alignment) void* mem = NULL; if(size - && IS_POWER_OF_2( alignment ) + && IS_POW2( alignment ) && alignment <= 32768 /* 32 KB */) { #if defined(MINGW) mem = _aligned_offset_malloc @@ -283,7 +283,7 @@ default_alloc_aligned (void)filename; (void)fileline; - if(size && IS_POWER_OF_2(alignment)) { + if(size && IS_POW2(alignment)) { mem = mem_alloc_aligned(size, alignment); #ifndef TRACK_DEFAULT_ALLOC (void)data; @@ -392,7 +392,7 @@ proxy_alloc_aligned ASSERT(data); proxy_data = data; - if((IS_POWER_OF_2(align) == 0) || align > 32768) + if((IS_POW2(align) == 0) || align > 32768) return NULL; align_adjusted = align < PROXY_DEFAULT_ALIGNMENT ? PROXY_DEFAULT_ALIGNMENT : align; diff --git a/src/test_math.c b/src/test_math.c @@ -0,0 +1,44 @@ +#include "math.h" + +int +main(int argc, char** argv) +{ + float f, g; + (void)argc, (void)argv; + + f = -3.14159f; CHECK(absf(f),-f); + f = 3.14159f; CHECK(absf(f), f); + f = -3.14159f; g = 2.71828f; + CHECK(MMAX(f, g), g); + CHECK(MMIN(f, g), f); + CHECK(MMAX(-10, 0), 0); + CHECK(MMAX(10, 0), 10); + CHECK(MMIN(-10, 0), -10); + CHECK(MMIN(10, 0), 0); + + NCHECK(eq_eps((float)PI, 3.14159265358979323846f, 1.e-6f), 0); + NCHECK(eq_eps((float)RCP_PI, 1.f / (float)PI, 1.e-6f), 0); + + CHECK(1.f/0.f, (float)INF); + CHECK(-1.f/0.f, (float)-INF); + NCHECK(1.0/0.0, -INF); + CHECK(1.0/0.0, INF); + NCHECK(-1.0/0.0, INF); + CHECK(-1.0/0.0, -INF); + NCHECK(IS_INF(PI/0.0), 0); + NCHECK(IS_INF(-PI/0.0), 0); + CHECK(IS_INF(PI), 0); + + CHECK(IS_POW2(0), 0); + CHECK(IS_POW2(1), 1); + CHECK(IS_POW2(2), 1); + CHECK(IS_POW2(3), 0); + CHECK(IS_POW2(31), 0); + CHECK(IS_POW2(64), 1); + CHECK(IS_POW2(1 << 16), 1); + CHECK(round_up_pow2(0), 1); + CHECK(round_up_pow2(3), 4); + CHECK(round_up_pow2(4), 4); + CHECK(round_up_pow2(100), 128); + return 0; +}