rsys

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

test_real4.h (5829B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "rsys.h"
     17 #include <float.h>
     18 
     19 #define REALX_DIMENSION__ 4
     20 #include "realX_begin.h"
     21 #define REAL REAL_TYPE__
     22 #define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__
     23 
     24 #define CHECK_REAL4(a, b)                                                      \
     25   {                                                                            \
     26     REAL* a__ = (a);                                                           \
     27     REAL* b__ = (b);                                                           \
     28     CHK((a__)[0] ==  (b__)[0]);                                                \
     29     CHK((a__)[1] ==  (b__)[1]);                                                \
     30     CHK((a__)[2] ==  (b__)[2]);                                                \
     31     CHK((a__)[3] ==  (b__)[3]);                                                \
     32   } (void) 0
     33 
     34 int
     35 main(int argc, char** argv)
     36 {
     37   REAL a[4], b[4], dst[4], f, c[4];
     38   REAL_COMPATIBLE d[4];
     39   (void)argc, (void)argv;
     40 
     41   REALX_FUNC__(set)(a, REALX_FUNC__(splat)(c, -1.0));
     42   CHK(a[0] ==  -1.0);
     43   CHK(a[1] ==  -1.0);
     44   CHK(a[2] ==  -1.0);
     45   CHK(a[3] ==  -1.0);
     46   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 1.0, 2.0, 3.0));
     47   CHK(a[0] ==  0.0);
     48   CHK(a[1] ==  1.0);
     49   CHK(a[2] ==  2.0);
     50   CHK(a[3] ==  3.0);
     51   REALX_FUNC__(splat)(a, -2.0);
     52   CHK(a[0] ==  -2.0);
     53   CHK(a[1] ==  -2.0);
     54   CHK(a[2] ==  -2.0);
     55   CHK(a[3] ==  -2.0);
     56 
     57   REALX_FUNC__(set)(a, REALX_CTOR__(c, -1.0, 2.0, -3.0, 4.0));
     58   CHECK_REAL4(REALX_FUNC__(minus)(b, a), REALX_CTOR__(c, 1.0, -2.0, 3.0, -4.0));
     59   CHECK_REAL4(b, REALX_CTOR__(c, 1.0, -2.0, 3.0, -4.0));
     60 
     61   d[0] = (REAL_COMPATIBLE)0.1;
     62   d[1] = (REAL_COMPATIBLE)(1.0/3.0);
     63   d[2] = (REAL_COMPATIBLE)0.3;
     64   d[3] = (REAL_COMPATIBLE)-0.7;
     65   REALX_CTOR__(c,
     66     (REAL)(REAL_COMPATIBLE)0.1,
     67     (REAL)(REAL_COMPATIBLE)(1.0/3.0),
     68     (REAL)(REAL_COMPATIBLE)0.3,
     69     (REAL)(REAL_COMPATIBLE)-0.7);
     70   CHECK_REAL4(REALX_CAST__(dst, d), c);
     71 
     72 
     73   CHECK_REAL4(REALX_FUNC__(add)(dst, a, b), REALX_FUNC__(splat)(c, 0.0));
     74   CHECK_REAL4(dst, REALX_FUNC__(splat)(c, 0.0));
     75   CHECK_REAL4(REALX_REAL_FUNC__(add)(dst, a, 1.0), REALX_CTOR__(c, 0.0, 3.0, -2.0, 5.0));
     76   CHECK_REAL4(dst, REALX_CTOR__(c, 0.0, 3.F, -2.0, 5.0));
     77   CHECK_REAL4(REALX_FUNC__(sub)(dst, a, b), REALX_CTOR__(c, -2.0, 4.0, -6.0, 8.0));
     78   CHECK_REAL4(dst, REALX_CTOR__(c, -2.0, 4.0, -6.0, 8.0));
     79   CHECK_REAL4(REALX_REAL_FUNC__(sub)(dst, a, 1.0), REALX_CTOR__(c, -2.0, 1.0, -4.0, 3.0));
     80   CHECK_REAL4(dst, REALX_CTOR__(c, -2.0, 1.0, -4.0, 3.0));
     81   CHECK_REAL4(REALX_REAL_FUNC__(mul)(dst, a, 2.0), REALX_CTOR__(c, -2.0, 4.0, -6.0, 8.0));
     82   CHECK_REAL4(dst, REALX_CTOR__(c, -2.0, 4.0, -6.0, 8.0));
     83   CHECK_REAL4(REALX_FUNC__(mul)(dst, a, b), REALX_CTOR__(c, -1.0, -4.0, -9.0, -16.0));
     84   CHECK_REAL4(dst, REALX_CTOR__(c, -1.0, -4.0, -9.0, -16.0));
     85   CHECK_REAL4(REALX_FUNC__(div)(dst, dst, a), REALX_CTOR__(c, 1.0, -2.0, 3.0, -4.0));
     86   CHECK_REAL4(dst, REALX_CTOR__(c, 1.0, -2.0, 3.0, -4.0));
     87   CHECK_REAL4(REALX_REAL_FUNC__(div)(dst, a, 2.0), REALX_CTOR__(c, -0.5, 1.0, -1.5, 2.0));
     88   CHECK_REAL4(dst, REALX_CTOR__(c, -0.5, 1.0, -1.5, 2.0));
     89 
     90   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 1.0, 2.0, 4.0));
     91   REALX_FUNC__(set)(b, REALX_CTOR__(c, 1.0, 2.0, -1.0, 1.0));
     92   CHECK_REAL4(REALX_FUNC__(lerp)(dst, a, b, 0.5), REALX_CTOR__(c, 0.5, 1.5, 0.5, 2.5));
     93   CHECK_REAL4(dst, REALX_CTOR__(c, 0.5, 1.5, 0.5, 2.5));
     94   CHK(REALX_FUNC__(sum)(b) ==  3.0);
     95   CHK(REALX_FUNC__(dot)(a, b) ==  4.0);
     96   CHK(eq_eps(REALX_FUNC__(len)(a), sqrt(21.0), REAL_EPSILON__) ==  1);
     97 
     98   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 4.0, 2.0, 3.0));
     99   CHK(REALX_FUNC__(is_normalized)(a) ==  0);
    100   f = REALX_FUNC__(normalize)(dst, a);
    101   CHK(REALX_FUNC__(is_normalized)(a) ==  0);
    102   CHK(REALX_FUNC__(is_normalized)(dst) != 0);
    103   CHK(eq_eps(f, sqrt(29.0), REAL_EPSILON__) ==  1);
    104   CHK(eq_eps(dst[0], a[0] / f, REAL_EPSILON__) ==  1);
    105   CHK(eq_eps(dst[1], a[1] / f, REAL_EPSILON__) ==  1);
    106   CHK(eq_eps(dst[2], a[2] / f, REAL_EPSILON__) ==  1);
    107   CHK(eq_eps(dst[3], a[3] / f, REAL_EPSILON__) ==  1);
    108 
    109   CHK(REALX_FUNC__(eq)(a, a) ==  1);
    110   CHK(REALX_FUNC__(eq)(a, b) ==  0);
    111   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, b[0], a[1], a[2], a[3])) ==  0);
    112   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, a[0], b[1], a[2], a[3])) ==  0);
    113   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, a[0], a[1], b[2], a[3])) ==  0);
    114   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, a[0], a[1], a[2], b[3])) ==  0);
    115 
    116   REALX_FUNC__(set)(b, a);
    117   REALX_FUNC__(add)(b, b, REALX_FUNC__(splat)(c, (REAL)(REAL_EPSILON__ * 0.5)));
    118   CHK(REALX_FUNC__(eq)(a, b) ==  0);
    119   CHK(REALX_FUNC__(eq_eps)(a, b, REAL_EPSILON__) ==  1);
    120   CHK(REALX_FUNC__(eq_eps)(a, b, (REAL)(REAL_EPSILON__ * 0.25)) ==  0);
    121 
    122   REALX_FUNC__(set)(a, REALX_CTOR__(c, 1.0, 3.0, -2.0, (REAL)0.1));
    123   REALX_FUNC__(set)(b, REALX_CTOR__(c, -1.0, (REAL)3.1, (REAL)-2.1, 1.0));
    124   CHECK_REAL4(REALX_FUNC__(max)(dst, a, b), REALX_CTOR__(c, 1.0, (REAL)3.1, -2.0, (REAL)1.0));
    125   CHECK_REAL4(dst, REALX_CTOR__(c, 1.0, (REAL)3.1, -2.0, 1.0));
    126   CHECK_REAL4(REALX_FUNC__(min)(dst, a, b), REALX_CTOR__(c, -1.0, 3.0, (REAL)-2.1, (REAL)0.1));
    127   CHECK_REAL4(dst, REALX_CTOR__(c, -1.0, 3.0, (REAL)-2.1, (REAL)0.1));
    128   return 0;
    129 }