summaryrefslogtreecommitdiff
path: root/matrix/matrix.c
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2015-05-15 02:36:55 +0800
committerDeterminant <[email protected]>2015-05-15 02:36:55 +0800
commitefb786d716363dde8f90ef0672f479790befc79c (patch)
tree1d7a6f07db9cae3fd65a437d3ca6ff398a2b684b /matrix/matrix.c
parentb03471e2b0d604806773b540551cd047979b7b3b (diff)
use C macro to implement matrix template
Diffstat (limited to 'matrix/matrix.c')
-rw-r--r--matrix/matrix.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/matrix/matrix.c b/matrix/matrix.c
new file mode 100644
index 0000000..3a593e5
--- /dev/null
+++ b/matrix/matrix.c
@@ -0,0 +1,26 @@
+#define MATRIX_DATA_FREE(ptr) free(ptr)
+#define MATRIX_DATA_ALLOC(size) malloc(size)
+#define MATRIX_DATA_STRIDE(ncol) (sizeof(float) * (ncol))
+#define MATRIX_GENERIC
+#define nerv_float_matrix_(NAME) nerv_float_matrix_host_ ## NAME
+#include "generic/matrix.c"
+
+int nerv_float_matrix_(get_elem)(lua_State *L) {
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ int idx = luaL_checkinteger(L, 2);
+ if (idx < 0 || idx >= self->nmax)
+ nerv_error(L, "index must be within range [0, %d)", self->nmax);
+ lua_pushnumber(L, self->data.f[idx]);
+ return 1;
+}
+
+int nerv_float_matrix_(set_elem)(lua_State *L) {
+ Matrix *self = luaT_checkudata(L, 1, nerv_float_matrix_tname);
+ int idx = luaL_checkinteger(L, 2);
+ float v = luaL_checknumber(L, 3);
+ long upper = self->nrow * self->ncol;
+ if (idx < 0 || idx >= self->nmax)
+ nerv_error(L, "index must be within range [0, %d)", self->nmax);
+ self->data.f[idx] = v;
+ return 0;
+}