rsys

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

commit 50852b9b06ec83a17ec6f8c14b1934dd646c3100
parent ce6c48c98608319cecd4c49a0453279196d162c7
Author: vaplv <vaplv@free.fr>
Date:   Sun,  8 Jun 2014 15:54:54 +0200

Add and test the CLAMP macro

Diffstat:
Mcmake/CMakeLists.txt | 1+
Msrc/math.h | 1+
Msrc/test_math.c | 8++++++++
3 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -62,6 +62,7 @@ set(RSYS_FILES_INC_COMMON clock_time.h dynamic_array.h dynamic_array_char.h + dynamic_array_uchar.h dynamic_array_u64.h free_list.h hash.h diff --git a/src/math.h b/src/math.h @@ -5,6 +5,7 @@ #define MMAX(A, B) ((A) > (B) ? (A) : (B)) #define MMIN(A, B) ((A) < (B) ? (A) : (B)) +#define CLAMP(A, Min, Max) MMIN(MMAX(Min, A), Max) #define IS_POW2(A) (((A) & ((A)-1)) == 0 && (A) > 0) #define INF (1.0/0.0) #define IS_INF(X) ((X==INF) || (X==-INF)) diff --git a/src/test_math.c b/src/test_math.c @@ -16,6 +16,14 @@ main(int argc, char** argv) CHECK(MMIN(-10, 0), -10); CHECK(MMIN(10, 0), 0); + CHECK(CLAMP(0.1f, 0.f, 1.f), 0.1f); + CHECK(CLAMP(-0.1f, 0.f, 1.f), 0.f); + CHECK(CLAMP(1.2f, 0.f, 1.f), 1.f); + CHECK(CLAMP(1.f, 0.f, 1.f), 1.f); + CHECK(CLAMP(0, -127, 127), 0); + CHECK(CLAMP(255, -127, 127), 127); + CHECK(CLAMP(-255, -127, 127), -127); + 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);