star-mc

Parallel estimation of Monte Carlo integrators
git clone git://git.meso-star.fr/star-mc.git
Log | Files | Refs | README | LICENSE

smc_type_real.h (3282B)


      1 /* Copyright (C) 2015-2018, 2021-2023 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef SMC_TYPE_REAL
     17   #error "Missing the SMC_TYPE_REAL definition"
     18 #endif
     19 
     20 #include <rsys/math.h>
     21 
     22 #define SMC_REAL_FUNC__(Func) \
     23   CONCAT(CONCAT(CONCAT(smc_, SMC_TYPE_REAL), _), Func)
     24 
     25 /*******************************************************************************
     26  * Function definition
     27  ******************************************************************************/
     28 static void*
     29 SMC_REAL_FUNC__(create)(struct mem_allocator* allocator, void* ctx)
     30 {
     31   ASSERT(allocator);
     32   (void)ctx;
     33   return MEM_ALLOC(allocator, sizeof(SMC_TYPE_REAL));
     34 }
     35 
     36 static void
     37 SMC_REAL_FUNC__(destroy)(struct mem_allocator* allocator, void* data)
     38 {
     39   ASSERT(data);
     40   MEM_RM(allocator, data);
     41 }
     42 
     43 static void
     44 SMC_REAL_FUNC__(set)(void* result, const void* value)
     45 {
     46   ASSERT(result && value);
     47   *(SMC_TYPE_REAL*)result = *(const SMC_TYPE_REAL*)value;
     48 }
     49 
     50 static void
     51 SMC_REAL_FUNC__(zero)(void* result)
     52 {
     53   ASSERT(result);
     54   *(SMC_TYPE_REAL*)result = 0;
     55 }
     56 
     57 static void
     58 SMC_REAL_FUNC__(add)(void* result, const void* op0, const void* op1)
     59 {
     60   ASSERT(result && op0 && op1);
     61   *(SMC_TYPE_REAL*)result =
     62     *(const SMC_TYPE_REAL*)op0 + *(const SMC_TYPE_REAL*)op1;
     63 }
     64 
     65 static void
     66 SMC_REAL_FUNC__(sub)(void* result, const void* op0, const void* op1)
     67 {
     68   ASSERT(result && op0 && op1);
     69   *(SMC_TYPE_REAL*)result =
     70     *(const SMC_TYPE_REAL*)op0 - *(const SMC_TYPE_REAL*)op1;
     71 }
     72 
     73 static void
     74 SMC_REAL_FUNC__(mul)(void* result, const void* op0, const void* op1)
     75 {
     76   ASSERT(result && op0 && op1);
     77   *(SMC_TYPE_REAL*)result =
     78     *(const SMC_TYPE_REAL*)op0 * *(const SMC_TYPE_REAL*)op1;
     79 }
     80 
     81 static void
     82 SMC_REAL_FUNC__(divi)(void* result, const void* op0, const size_t op1)
     83 {
     84   ASSERT(result && op0 && op1);
     85   *(SMC_TYPE_REAL*)result =
     86     (SMC_TYPE_REAL)((double)(*(const SMC_TYPE_REAL*)op0) / (double)op1);
     87 }
     88 
     89 static void
     90 SMC_REAL_FUNC__(sqrt)(void* result, const void* value)
     91 {
     92   ASSERT(result && value && *(const SMC_TYPE_REAL*)value >= (SMC_TYPE_REAL)-1.e-6);
     93   *(SMC_TYPE_REAL*)result = (SMC_TYPE_REAL)sqrt
     94     (MMAX(*(const SMC_TYPE_REAL*)value, (SMC_TYPE_REAL)0.0));
     95 }
     96 
     97 /*******************************************************************************
     98  * Exported constant
     99  ******************************************************************************/
    100 const struct smc_type CONCAT(smc_, SMC_TYPE_REAL) = {
    101   SMC_REAL_FUNC__(create),
    102   SMC_REAL_FUNC__(destroy),
    103   SMC_REAL_FUNC__(set),
    104   SMC_REAL_FUNC__(zero),
    105   SMC_REAL_FUNC__(add),
    106   SMC_REAL_FUNC__(sub),
    107   SMC_REAL_FUNC__(mul),
    108   SMC_REAL_FUNC__(divi),
    109   SMC_REAL_FUNC__(sqrt)
    110 };
    111 
    112 #undef SMC_REAL_FUNC__
    113 #undef SMC_TYPE_REAL
    114