aboutsummaryrefslogtreecommitdiff
path: root/examples/fib.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/fib.c')
-rw-r--r--examples/fib.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/examples/fib.c b/examples/fib.c
new file mode 100644
index 0000000..fe2b215
--- /dev/null
+++ b/examples/fib.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <time.h>
+
+#include "../include/ejit/ejit.h"
+
+struct ejit_func *compile()
+{
+ struct ejit_operand args[1] = {
+ EJIT_OPERAND_GPR(0, EJIT_INT64) /* loc 0 contains n */
+ };
+ struct ejit_func *f = ejit_create_func(EJIT_INT64, 1, args);
+ struct ejit_reloc recurse = ejit_bgti(f, EJIT_GPR(0), 2); /* n <= 2 */
+ ejit_reti(f, 1);
+
+ struct ejit_label label = ejit_label(f);
+ ejit_patch(f, recurse, label);
+
+ /* fib(n - 1) */
+ ejit_subi(f, EJIT_GPR(0), EJIT_GPR(0), 1);
+ struct ejit_operand arg[1] = {
+ EJIT_OPERAND_GPR(0, EJIT_INT64)
+ };
+ ejit_calli(f, f, 1, arg);
+ ejit_retval(f, EJIT_GPR(1)); /* loc 1 contains temp result */
+
+ /* fib(n - 2) */
+ ejit_subi(f, EJIT_GPR(0), EJIT_GPR(0), 1);
+ ejit_calli(f, f, 1, arg);
+ ejit_retval(f, EJIT_GPR(0)); /* loc 0 now contains second temp result */
+
+ ejit_addr(f, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1)); /* add results */
+ ejit_retr(f, EJIT_GPR(0));
+
+ bool try_jit = true;
+#ifdef NOJIT
+ try_jit = false;
+#endif
+
+ /* the highest location we used was 1, so we need to request 2 locations
+ * for general purpose registers in total. No floating point registers,
+ * so 0. */
+ ejit_select_compile_func(f, 2, 0, true, try_jit);
+ return f;
+}
+
+int main(int argc, char *argv[])
+{
+ if(argc != 3){
+ fprintf(stderr, "Usage: %s compile_num loop_num\n", argv[0]);
+ return -1;
+ }
+
+ size_t compile_num = strtoull(argv[1], 0, 0);
+ struct ejit_func **info = calloc(compile_num, sizeof(struct ejit_func *));
+
+ clock_t t = clock();
+ for(size_t i = 0; i < compile_num; ++i){
+ info[i] = compile();
+ }
+ t = clock() - t;
+
+ double compile_time_total = ((double)t) / CLOCKS_PER_SEC;
+ double compile_time_one = compile_time_total / compile_num;
+ printf("Compilation for n = %lu took %fs (1/%f).\n",
+ compile_num, compile_time_total, compile_time_one);
+
+ size_t run_num = strtoull(argv[2], 0, 0);
+ t = clock();
+ struct ejit_arg arg[1] = {
+ (struct ejit_arg){.type = EJIT_INT64, .l = run_num}
+ };
+ size_t result = ejit_run_func(info[0], 1, arg);
+ t = clock() - t;
+
+ double run_time_total = ((double)t) / CLOCKS_PER_SEC;
+ printf("Running loop for n = %lu took %fs with res %lu\n",
+ run_num, run_time_total, result);
+
+ for(size_t i = 0; i < compile_num; ++i)
+ ejit_destroy_func(info[i]);
+
+ free(info);
+ return 0;
+}