aboutsummaryrefslogtreecommitdiff
path: root/examples/natmod/features2
diff options
context:
space:
mode:
authorDamien George2019-12-12 15:02:27 +1100
committerDamien George2019-12-12 20:15:28 +1100
commit60c3c22a0dcd5aba9e65ae4c3890889f42a18567 (patch)
tree49a78a6c61a327c5e3d0641cb2ac92373fcff490 /examples/natmod/features2
parent4eef940edbd3ee4eecead37785cafd9845d763a2 (diff)
examples/natmod: Add features1 and features2 examples.
Diffstat (limited to 'examples/natmod/features2')
-rw-r--r--examples/natmod/features2/Makefile14
-rw-r--r--examples/natmod/features2/main.c83
-rw-r--r--examples/natmod/features2/prod.c9
-rw-r--r--examples/natmod/features2/prod.h1
-rw-r--r--examples/natmod/features2/test.py26
5 files changed, 133 insertions, 0 deletions
diff --git a/examples/natmod/features2/Makefile b/examples/natmod/features2/Makefile
new file mode 100644
index 000000000..4fd23c687
--- /dev/null
+++ b/examples/natmod/features2/Makefile
@@ -0,0 +1,14 @@
+# Location of top-level MicroPython directory
+MPY_DIR = ../../..
+
+# Name of module
+MOD = features2
+
+# Source files (.c or .py)
+SRC = main.c prod.c test.py
+
+# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
+ARCH = x64
+
+# Include to get the rules for compiling and linking the module
+include $(MPY_DIR)/py/dynruntime.mk
diff --git a/examples/natmod/features2/main.c b/examples/natmod/features2/main.c
new file mode 100644
index 000000000..de83bf2be
--- /dev/null
+++ b/examples/natmod/features2/main.c
@@ -0,0 +1,83 @@
+/* This example demonstrates the following features in a native module:
+ - using floats
+ - defining additional code in Python (see test.py)
+ - have extra C code in a separate file (see prod.c)
+*/
+
+// Include the header file to get access to the MicroPython API
+#include "py/dynruntime.h"
+
+// Include the header for auxiliary C code for this module
+#include "prod.h"
+
+// Automatically detect if this module should include double-precision code.
+// If double precision is supported by the target architecture then it can
+// be used in native module regardless of what float setting the target
+// MicroPython runtime uses (being none, float or double).
+#if defined(__i386__) || defined(__x86_64__) || (defined(__ARM_FP) && (__ARM_FP & 8))
+#define USE_DOUBLE 1
+#else
+#define USE_DOUBLE 0
+#endif
+
+// A function that uses the default float type configured for the current target
+// This default can be overridden by specifying MICROPY_FLOAT_IMPL at the make level
+STATIC mp_obj_t add(mp_obj_t x, mp_obj_t y) {
+ return mp_obj_new_float(mp_obj_get_float(x) + mp_obj_get_float(y));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(add_obj, add);
+
+// A function that explicitly uses single precision floats
+STATIC mp_obj_t add_f(mp_obj_t x, mp_obj_t y) {
+ return mp_obj_new_float_from_f(mp_obj_get_float_to_f(x) + mp_obj_get_float_to_f(y));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(add_f_obj, add_f);
+
+#if USE_DOUBLE
+// A function that explicitly uses double precision floats
+STATIC mp_obj_t add_d(mp_obj_t x, mp_obj_t y) {
+ return mp_obj_new_float_from_d(mp_obj_get_float_to_d(x) + mp_obj_get_float_to_d(y));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(add_d_obj, add_d);
+#endif
+
+// A function that computes the product of floats in an array.
+// This function uses the most general C argument interface, which is more difficult
+// to use but has access to the globals dict of the module via self->globals.
+STATIC mp_obj_t productf(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
+ // Check number of arguments is valid
+ mp_arg_check_num(n_args, n_kw, 1, 1, false);
+
+ // Extract buffer pointer and verify typecode
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_RW);
+ if (bufinfo.typecode != 'f') {
+ mp_raise_ValueError("expecting float array");
+ }
+
+ // Compute product, store result back in first element of array
+ float *ptr = bufinfo.buf;
+ float prod = prod_array(bufinfo.len / sizeof(*ptr), ptr);
+ ptr[0] = prod;
+
+ return mp_const_none;
+}
+
+// This is the entry point and is called when the module is imported
+mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
+ // This must be first, it sets up the globals dict and other things
+ MP_DYNRUNTIME_INIT_ENTRY
+
+ // Make the functions available in the module's namespace
+ mp_store_global(MP_QSTR_add, MP_OBJ_FROM_PTR(&add_obj));
+ mp_store_global(MP_QSTR_add_f, MP_OBJ_FROM_PTR(&add_f_obj));
+ #if USE_DOUBLE
+ mp_store_global(MP_QSTR_add_d, MP_OBJ_FROM_PTR(&add_d_obj));
+ #endif
+
+ // The productf function uses the most general C argument interface
+ mp_store_global(MP_QSTR_productf, MP_DYNRUNTIME_MAKE_FUNCTION(productf));
+
+ // This must be last, it restores the globals dict
+ MP_DYNRUNTIME_INIT_EXIT
+}
diff --git a/examples/natmod/features2/prod.c b/examples/natmod/features2/prod.c
new file mode 100644
index 000000000..7791dcad1
--- /dev/null
+++ b/examples/natmod/features2/prod.c
@@ -0,0 +1,9 @@
+#include "prod.h"
+
+float prod_array(int n, float *ar) {
+ float ans = 1;
+ for (int i = 0; i < n; ++i) {
+ ans *= ar[i];
+ }
+ return ans;
+}
diff --git a/examples/natmod/features2/prod.h b/examples/natmod/features2/prod.h
new file mode 100644
index 000000000..f27dd8d03
--- /dev/null
+++ b/examples/natmod/features2/prod.h
@@ -0,0 +1 @@
+float prod_array(int n, float *ar);
diff --git a/examples/natmod/features2/test.py b/examples/natmod/features2/test.py
new file mode 100644
index 000000000..2e9db00f4
--- /dev/null
+++ b/examples/natmod/features2/test.py
@@ -0,0 +1,26 @@
+# This Python code will be merged with the C code in main.c
+
+import array
+
+def isclose(a, b):
+ return abs(a - b) < 1e-3
+
+def test():
+ tests = [
+ isclose(add(0.1, 0.2), 0.3),
+ isclose(add_f(0.1, 0.2), 0.3),
+ ]
+
+ ar = array.array('f', [1, 2, 3.5])
+ productf(ar)
+ tests.append(isclose(ar[0], 7))
+
+ if 'add_d' in globals():
+ tests.append(isclose(add_d(0.1, 0.2), 0.3))
+
+ print(tests)
+
+ if not all(tests):
+ raise SystemExit(1)
+
+test()