rsys

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

commit f164778b82c3af09722b22b0c19a6f54f698c311
parent 7ac76c31c9182d37e9795f6172ba984e5e42d02d
Author: vaplv <vaplv@free.fr>
Date:   Wed, 20 Jul 2016 10:35:24 +0200

Add and test the fX_set_dX and dX_set_fX cast functions (X in [2, 3, 4])

Initialise a vector of float from a vector of double and vice versa.

Diffstat:
Msrc/realX.h | 10++++++++++
Msrc/realX_begin.h | 47++++++++++++++++++++++++++++++++---------------
Msrc/realX_end.h | 20++++++++++++--------
Msrc/test_real2.h | 7+++++++
Msrc/test_real3.h | 11+++++++++++
Msrc/test_real4.h | 14++++++++++++++
6 files changed, 86 insertions(+), 23 deletions(-)

diff --git a/src/realX.h b/src/realX.h @@ -52,6 +52,16 @@ REALX_CTOR__ #endif static FINLINE REAL_TYPE__* +REALX_CAST__(REAL_TYPE__* dst, const REAL_TYPE_COMPATIBLE__* src) +{ + int i; + ASSERT(dst && src); + FOR_EACH(i, 0, REALX_DIMENSION__) + dst[i] = (REAL_TYPE__)src[i]; + return dst; +} + +static FINLINE REAL_TYPE__* REALX_FUNC__(splat)(REAL_TYPE__* dst, const REAL_TYPE__ val) { int i; diff --git a/src/realX_begin.h b/src/realX_begin.h @@ -25,36 +25,56 @@ #endif /* Check that internal macros are not already defined */ -#if defined(REALX_CTOR__) \ - || defined(REALX_FUNC__) \ - || defined(REALX_REAL_FUNC__) \ - || defined(REAL_LETTER__) \ - || defined(REAL_LETTER_float) \ - || defined(REAL_LETTER_double) \ - || defined(REAL_EPSILON__) \ - || defined(REAL_EPSILON_float) \ +#if defined(REAL_EPSILON__) \ || defined(REAL_EPSILON_double) \ + || defined(REAL_EPSILON_float) \ || defined(REAL_EQ_EPS__) \ - || defined(REAL_EQ_EPS_float) \ || defined(REAL_EQ_EPS_double) \ + || defined(REAL_EQ_EPS_float) \ + || defined(REAL_LETTER__) \ + || defined(REAL_LETTER_double) \ + || defined(REAL_LETTER_float) \ + || defined(REAL_LETTER_TYPE_COMPATIBLE__) \ + || defined(REAL_TYPE_COMPATIBLE__) \ + || defined(REAL_TYPE_COMPATIBLE_double) \ + || defined(REAL_TYPE_COMPATIBLE_float) \ + || defined(REALX_CAST__) \ + || defined(REALX_CTOR__) \ + || defined(REALX_FUNC__) \ + || defined(REALX_REAL_FUNC__) \ || defined(SIZEOF_REALX__) #error Unexpected macro definition #endif /* Define the epsilon for each real type */ +#define REAL_EPSILON__ CONCAT(REAL_EPSILON_, REAL_TYPE__) #define REAL_EPSILON_double 1.e-8 #define REAL_EPSILON_float 1.e-6f -#define REAL_EPSILON__ CONCAT(REAL_EPSILON_, REAL_TYPE__) /* Define the eq_eps function for each type */ +#define REAL_EQ_EPS__ CONCAT(REAL_EQ_EPS_, REAL_TYPE__) #define REAL_EQ_EPS_double eq_eps #define REAL_EQ_EPS_float eq_epsf -#define REAL_EQ_EPS__ CONCAT(REAL_EQ_EPS_, REAL_TYPE__) /* Define the suffix/prefix letter for each real type */ +#define REAL_LETTER__ CONCAT(REAL_LETTER_, REAL_TYPE__) #define REAL_LETTER_float f #define REAL_LETTER_double d -#define REAL_LETTER__ CONCAT(REAL_LETTER_, REAL_TYPE__) +#define REAL_LETTER_TYPE_COMPATIBLE__ \ + CONCAT(REAL_LETTER_, REAL_TYPE_COMPATIBLE__) + +/* Define the type that is compatible to the current real type */ +#define REAL_TYPE_COMPATIBLE__ CONCAT(REAL_TYPE_COMPATIBLE_, REAL_TYPE__) +#define REAL_TYPE_COMPATIBLE_double float +#define REAL_TYPE_COMPATIBLE_float double + +/* Define name of the cast function */ +#define REALX_CAST__ REALX_FUNC__ \ + (CONCAT(set_, CONCAT(REAL_LETTER_TYPE_COMPATIBLE__, REALX_DIMENSION__))) + +/* Define the realX constructor from reals */ +#define REALX_CTOR__ CONCAT(REAL_LETTER__, REALX_DIMENSION__) + /* Define the function name generator */ #define REALX_FUNC__(Func) \ @@ -63,9 +83,6 @@ /* Define the name generator for the realX x real -> realX functions */ #define REALX_REAL_FUNC__(Func) CONCAT(REALX_FUNC__(Func), REAL_LETTER__) -/* Define the realX constructor from reals */ -#define REALX_CTOR__ CONCAT(REAL_LETTER__, REALX_DIMENSION__) - /* Helper macro */ #define SIZEOF_REALX__ sizeof(REAL_TYPE__[REALX_DIMENSION__]) diff --git a/src/realX_end.h b/src/realX_end.h @@ -17,19 +17,23 @@ #error The realX_begin.h header must be included #endif -#undef REAL_TYPE__ -#undef REALX_DIMENSION__ - -#undef REAL_LETTER__ -#undef REAL_LETTER_float -#undef REAL_LETTER_double #undef REAL_EPSILON__ -#undef REAL_EPSILON_float #undef REAL_EPSILON_double +#undef REAL_EPSILON_float #undef REAL_EQ_EPS__ -#undef REAL_EQ_EPS_float #undef REAL_EQ_EPS_double +#undef REAL_EQ_EPS_float +#undef REAL_LETTER__ +#undef REAL_LETTER_double +#undef REAL_LETTER_float +#undef REAL_LETTER_TYPE_COMPATIBLE__ +#undef REAL_TYPE__ +#undef REAL_TYPE_COMPATIBLE__ +#undef REAL_TYPE_COMPATIBLE_float +#undef REAL_TYPE_COMPATIBLE_double +#undef REALX_CAST__ #undef REALX_CTOR__ +#undef REALX_DIMENSION__ #undef REALX_FUNC__ #undef REALX_REAL_FUNC__ #undef SIZEOF_REALX__ diff --git a/src/test_real2.h b/src/test_real2.h @@ -19,6 +19,7 @@ #define REALX_DIMENSION__ 2 #include "realX_begin.h" #define REAL REAL_TYPE__ +#define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__ #define CHECK_REAL2(a, b) \ { \ @@ -32,6 +33,7 @@ int main(int argc, char** argv) { REAL a[2], b[2], dst[2], f, c[2]; + REAL_COMPATIBLE d[2]; (void)argc, (void)argv; REALX_FUNC__(set)(a, REALX_FUNC__(splat)(c, -1.0)); @@ -49,6 +51,11 @@ main(int argc, char** argv) CHECK_REAL2(REALX_FUNC__(minus)(b, a), REALX_CTOR__(c, 1.0, -2.0)); CHECK_REAL2(b, REALX_CTOR__(c, 1.0, -2.0)); + d[0] = (REAL_COMPATIBLE)0.1; + d[1] = (REAL_COMPATIBLE)(1.0/3.0); + REALX_CTOR__(c, (REAL)(REAL_COMPATIBLE)0.1, (REAL)(REAL_COMPATIBLE)(1.0/3.0)); + CHECK_REAL2(REALX_CAST__(dst, d), c); + CHECK_REAL2(REALX_REAL_FUNC__(add)(dst, a, 1.0), REALX_CTOR__(c, 0.0, 3.0)); CHECK_REAL2(dst, REALX_CTOR__(c, 0.0, 3.0)); CHECK_REAL2(REALX_FUNC__(add)(dst, a, b), REALX_FUNC__(splat)(c, 0.0)); diff --git a/src/test_real3.h b/src/test_real3.h @@ -19,6 +19,7 @@ #define REALX_DIMENSION__ 3 #include "realX_begin.h" #define REAL REAL_TYPE__ +#define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__ #define CHECK_REAL3(a, b) \ { \ @@ -33,6 +34,7 @@ int main(int argc, char** argv) { REAL a[3], b[3], dst[3], f, c[3]; + REAL_COMPATIBLE d[3]; (void)argc, (void)argv; REALX_FUNC__(set)(a, REALX_FUNC__(splat)(c, -1.0)); @@ -52,6 +54,15 @@ main(int argc, char** argv) CHECK_REAL3(REALX_FUNC__(minus)(b, a), REALX_CTOR__(c, 1.0, -2.0, 3.0)); CHECK_REAL3(b, REALX_CTOR__(c, 1.0, -2.0, 3.0)); + d[0] = (REAL_COMPATIBLE)0.1; + d[1] = (REAL_COMPATIBLE)(1.0/3.0); + d[2] = (REAL_COMPATIBLE)0.3; + REALX_CTOR__(c, + (REAL)(REAL_COMPATIBLE)0.1, + (REAL)(REAL_COMPATIBLE)(1.0/3.0), + (REAL)(REAL_COMPATIBLE)0.3); + CHECK_REAL3(REALX_CAST__(dst, d), c); + CHECK_REAL3(REALX_FUNC__(add)(dst, a, b), REALX_FUNC__(splat)(c, 0.0)); CHECK_REAL3(dst, REALX_FUNC__(splat)(c, 0.0)); CHECK_REAL3(REALX_REAL_FUNC__(add)(dst, a, 1), REALX_CTOR__(c, 0.0, 3.0, -2.0)); diff --git a/src/test_real4.h b/src/test_real4.h @@ -19,6 +19,7 @@ #define REALX_DIMENSION__ 4 #include "realX_begin.h" #define REAL REAL_TYPE__ +#define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__ #define CHECK_REAL4(a, b) \ { \ @@ -34,6 +35,7 @@ int main(int argc, char** argv) { REAL a[4], b[4], dst[4], f, c[4]; + REAL_COMPATIBLE d[4]; (void)argc, (void)argv; REALX_FUNC__(set)(a, REALX_FUNC__(splat)(c, -1.0)); @@ -56,6 +58,18 @@ main(int argc, char** argv) CHECK_REAL4(REALX_FUNC__(minus)(b, a), REALX_CTOR__(c, 1.0, -2.0, 3.0, -4.0)); CHECK_REAL4(b, REALX_CTOR__(c, 1.0, -2.0, 3.0, -4.0)); + d[0] = (REAL_COMPATIBLE)0.1; + d[1] = (REAL_COMPATIBLE)(1.0/3.0); + d[2] = (REAL_COMPATIBLE)0.3; + d[3] = (REAL_COMPATIBLE)-0.7; + REALX_CTOR__(c, + (REAL)(REAL_COMPATIBLE)0.1, + (REAL)(REAL_COMPATIBLE)(1.0/3.0), + (REAL)(REAL_COMPATIBLE)0.3, + (REAL)(REAL_COMPATIBLE)-0.7); + CHECK_REAL4(REALX_CAST__(dst, d), c); + + CHECK_REAL4(REALX_FUNC__(add)(dst, a, b), REALX_FUNC__(splat)(c, 0.0)); CHECK_REAL4(dst, REALX_FUNC__(splat)(c, 0.0)); CHECK_REAL4(REALX_REAL_FUNC__(add)(dst, a, 1.0), REALX_CTOR__(c, 0.0, 3.0, -2.0, 5.0));