commit 9bf2d4b470cecdc8d6d6b3c4461c812d633e5c23
parent c1c37f0c56b6bdd680c393f41f843ffb25fe1d11
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 1 Apr 2015 12:09:18 +0200
Add the smc_double builtin type
Diffstat:
5 files changed, 150 insertions(+), 89 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -54,8 +54,15 @@ set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
-set(SMC_FILES_SRC smc_device.c smc_integrator.c smc_type.c)
-set(SMC_FILES_INC smc.h smc_device_c.h smc_type_c.h)
+set(SMC_FILES_SRC
+ smc_device.c
+ smc_integrator.c
+ smc_type.c)
+set(SMC_FILES_INC
+ smc.h
+ smc_device_c.h
+ smc_type_real.h
+ smc_type_c.h)
# Prepend each file in the `SMC_FILES_<SRC|INC>' list by `SMC_SOURCE_DIR'
rcmake_prepend_path(SMC_FILES_SRC ${SMC_SOURCE_DIR})
diff --git a/src/smc.h b/src/smc.h
@@ -88,8 +88,11 @@ BEGIN_DECLS
/* Pre-declared SMC types */
SMC_API const struct smc_type smc_float;
+SMC_API const struct smc_type smc_double;
+/* Syntactic sugar macros */
#define SMC_FLOAT(Val) (*(const float*)(Val))
+#define SMC_DOUBLE(Val) (*(const double*)(Val))
/*******************************************************************************
* API deinition
diff --git a/src/smc_type.c b/src/smc_type.c
@@ -33,84 +33,11 @@
#include <rsys/mem_allocator.h>
#include <math.h>
-/*******************************************************************************
- * SMC float definition
- ******************************************************************************/
-static void*
-smc_float_create(struct mem_allocator* allocator)
-{
- ASSERT(allocator);
- return MEM_ALLOC(allocator, sizeof(float));
-}
+/* Declare the smc_float builtin type */
+#define SMC_TYPE_REAL float
+#include "smc_type_real.h"
-static void
-smc_float_destroy(struct mem_allocator* allocator, void* data)
-{
- ASSERT(data);
- MEM_FREE(allocator, data);
-}
-
-static void
-smc_float_set(void* result, const void* value)
-{
- ASSERT(result && value);
- *(float*)result = *(const float*)value;
-}
-
-static void
-smc_float_zero(void* result)
-{
- ASSERT(result);
- *(float*)result = 0;
-}
-
-static void
-smc_float_add(void* result, const void* op0, const void* op1)
-{
- ASSERT(result && op0 && op1);
- *(float*)result = *(const float*)op0 + *(const float*)op1;
-}
-
-static void
-smc_float_sub(void* result, const void* op0, const void* op1)
-{
- ASSERT(result && op0 && op1);
- *(float*)result = *(const float*)op0 - *(const float*)op1;
-}
-
-static void
-smc_float_mul(void* result, const void* op0, const void* op1)
-{
- ASSERT(result && op0 && op1);
- *(float*)result = *(const float*)op0 * *(const float*)op1;
-}
-
-static void
-smc_float_divi(void* result, const void* op0, const unsigned long op1)
-{
- ASSERT(result && op0 && op1);
- *(float*)result = (float)((double)(*(const float*)op0) / (double)op1);
-}
-
-static void
-smc_float_sqrt(void* result, const void* value)
-{
- ASSERT(result && value && *(const float*)value >= 0.f);
- *(float*)result = (float)sqrt(*(const float*)value);
-}
-
-/*******************************************************************************
- * Exported constant
- ******************************************************************************/
-const struct smc_type smc_float = {
- smc_float_create,
- smc_float_destroy,
- smc_float_set,
- smc_float_zero,
- smc_float_add,
- smc_float_sub,
- smc_float_mul,
- smc_float_divi,
- smc_float_sqrt
-};
+/* Declare the smc_double builtin type */
+#define SMC_TYPE_REAL double
+#include "smc_type_real.h"
diff --git a/src/smc_type_real.h b/src/smc_type_real.h
@@ -0,0 +1,125 @@
+/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com)
+ *
+ * This software is a computer program whose purpose is to manage the
+ * statistical estimation of a function.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#ifndef SMC_TYPE_REAL
+ #error "Missing the SMC_TYPE_REAL definition"
+#endif
+
+#define SMC_REAL_FUNC__(Func) \
+ CONCAT(CONCAT(CONCAT(smc_, SMC_TYPE_REAL), _), Func)
+
+/*******************************************************************************
+ * Function definition
+ ******************************************************************************/
+static void*
+SMC_REAL_FUNC__(create)(struct mem_allocator* allocator)
+{
+ ASSERT(allocator);
+ return MEM_ALLOC(allocator, sizeof(SMC_TYPE_REAL));
+}
+
+static void
+SMC_REAL_FUNC__(destroy)(struct mem_allocator* allocator, void* data)
+{
+ ASSERT(data);
+ MEM_FREE(allocator, data);
+}
+
+static void
+SMC_REAL_FUNC__(set)(void* result, const void* value)
+{
+ ASSERT(result && value);
+ *(SMC_TYPE_REAL*)result = *(const SMC_TYPE_REAL*)value;
+}
+
+static void
+SMC_REAL_FUNC__(zero)(void* result)
+{
+ ASSERT(result);
+ *(SMC_TYPE_REAL*)result = 0;
+}
+
+static void
+SMC_REAL_FUNC__(add)(void* result, const void* op0, const void* op1)
+{
+ ASSERT(result && op0 && op1);
+ *(SMC_TYPE_REAL*)result =
+ *(const SMC_TYPE_REAL*)op0 + *(const SMC_TYPE_REAL*)op1;
+}
+
+static void
+SMC_REAL_FUNC__(sub)(void* result, const void* op0, const void* op1)
+{
+ ASSERT(result && op0 && op1);
+ *(SMC_TYPE_REAL*)result =
+ *(const SMC_TYPE_REAL*)op0 - *(const SMC_TYPE_REAL*)op1;
+}
+
+static void
+SMC_REAL_FUNC__(mul)(void* result, const void* op0, const void* op1)
+{
+ ASSERT(result && op0 && op1);
+ *(SMC_TYPE_REAL*)result =
+ *(const SMC_TYPE_REAL*)op0 * *(const SMC_TYPE_REAL*)op1;
+}
+
+static void
+SMC_REAL_FUNC__(divi)(void* result, const void* op0, const unsigned long op1)
+{
+ ASSERT(result && op0 && op1);
+ *(SMC_TYPE_REAL*)result =
+ (SMC_TYPE_REAL)((double)(*(const SMC_TYPE_REAL*)op0) / (double)op1);
+}
+
+static void
+SMC_REAL_FUNC__(sqrt)(void* result, const void* value)
+{
+ ASSERT(result && value && *(const SMC_TYPE_REAL*)value >= (SMC_TYPE_REAL)0);
+ *(SMC_TYPE_REAL*)result = (SMC_TYPE_REAL)sqrt(*(const SMC_TYPE_REAL*)value);
+}
+
+/*******************************************************************************
+ * Exported constant
+ ******************************************************************************/
+const struct smc_type CONCAT(smc_, SMC_TYPE_REAL) = {
+ SMC_REAL_FUNC__(create),
+ SMC_REAL_FUNC__(destroy),
+ SMC_REAL_FUNC__(set),
+ SMC_REAL_FUNC__(zero),
+ SMC_REAL_FUNC__(add),
+ SMC_REAL_FUNC__(sub),
+ SMC_REAL_FUNC__(mul),
+ SMC_REAL_FUNC__(divi),
+ SMC_REAL_FUNC__(sqrt)
+};
+
+#undef SMC_REAL_FUNC__
+#undef SMC_TYPE_REAL
diff --git a/src/test_smc_solve.c b/src/test_smc_solve.c
@@ -39,9 +39,9 @@
static void
rcp_x(void* value, void* ctx)
{
- float* result = value;
+ double* result = value;
/* samp in [1, 2] */
- float samp = ((float)rand() / (float)RAND_MAX) + 1.f;
+ double samp = ((double)rand() / (double)RAND_MAX) + 1.0;
CHECK(ctx, NULL);
(void)ctx;
*result = 1.f / samp;
@@ -84,18 +84,17 @@ main(int argc, char** argv)
CHECK(smc_solve(dev, NULL, NULL, NULL, &estimator), RES_BAD_ARG);
CHECK(smc_solve(NULL, rcp_x, NULL, NULL, &estimator), RES_BAD_ARG);
CHECK(smc_solve(dev, rcp_x, NULL, NULL, &estimator), RES_BAD_ARG);
- CHECK(smc_solve(NULL, NULL, &smc_float, NULL, &estimator), RES_BAD_ARG);
- CHECK(smc_solve(dev, NULL, &smc_float, NULL, &estimator), RES_BAD_ARG);
- CHECK(smc_solve(NULL, rcp_x, &smc_float, NULL, &estimator), RES_BAD_ARG);
- CHECK(smc_solve(dev, rcp_x, &smc_float, NULL, &estimator), RES_OK);
+ CHECK(smc_solve(NULL, NULL, &smc_double, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(dev, NULL, &smc_double, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(NULL, rcp_x, &smc_double, NULL, &estimator), RES_BAD_ARG);
+ CHECK(smc_solve(dev, rcp_x, &smc_double, NULL, &estimator), RES_OK);
CHECK(smc_estimator_get_status(NULL, NULL), RES_BAD_ARG);
CHECK(smc_estimator_get_status(estimator, NULL), RES_BAD_ARG);
CHECK(smc_estimator_get_status(NULL, &status), RES_BAD_ARG);
CHECK(smc_estimator_get_status(estimator, &status), RES_OK);
CHECK(eq_eps
- ((float)(log(2.f) - log(1.f)),
- SMC_FLOAT(status.E), SMC_FLOAT(status.SE)), 1);
+ (log(2.0) - log(1.0), SMC_DOUBLE(status.E), SMC_DOUBLE(status.SE)), 1);
CHECK(smc_estimator_ref_put(estimator), RES_OK);
CHECK(smc_solve(dev, cos_x, &smc_float, (void*)0xC0DE, &estimator), RES_OK);