diff options
author | David Schleef <ds@ginger.bigkitten.com> | 2008-06-07 15:00:28 -0700 |
---|---|---|
committer | David Schleef <ds@ginger.bigkitten.com> | 2008-06-07 15:00:28 -0700 |
commit | bad2dbffb219c7a69ed5991236da496bc03bcdf4 (patch) | |
tree | c676e83d0bc1bcce2cfb166763f5c87a8451fc72 /examples |
Initial commit. Previous history is in liboil
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 10 | ||||
-rw-r--r-- | examples/jit.c | 97 | ||||
-rw-r--r-- | examples/simple.c | 144 |
3 files changed, 251 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..9023579 --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,10 @@ + +noinst_PROGRAMS = jit simple + +AM_LDFLAGS = $(ORC_LIBS) $(GLIB_LIBS) +AM_CFLAGS = $(ORC_CFLAGS) $(GLIB_CFLAGS) + +jit_SOURCES = jit.c + +simple_SOURCES = simple.c + diff --git a/examples/jit.c b/examples/jit.c new file mode 100644 index 0000000..52cd1be --- /dev/null +++ b/examples/jit.c @@ -0,0 +1,97 @@ + +#include "config.h" + +#include <glib.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include <orc/orcprogram.h> + +#define N 19 + +int16_t src1[N]; +int16_t src2[N]; +int16_t dest[N]; + +void test(OrcExecutor *ex); + +int +main (int argc, char *argv[]) +{ + OrcProgram *p; + OrcExecutor *ex; + int s1, s2, d1, offset, shift; + int t1; + + orc_init (); + + p = orc_program_new (); + + d1 = orc_program_add_destination (p, "s16", "d1"); + s1 = orc_program_add_source (p, "s16", "s1"); + s2 = orc_program_add_source (p, "s16", "s2"); + t1 = orc_program_add_temporary (p, "s16", "t1"); + offset = orc_program_add_constant (p, "s16", 1, "offset"); + shift = orc_program_add_constant (p, "s16", 1, "shift"); + + orc_program_append (p, "add_s16", t1, s1, s2); + orc_program_append (p, "add_s16", t1, t1, offset); + orc_program_append (p, "rshift_s16", d1, t1, shift); + +#if 0 + orc_program_append (p, "lshift_s16", d1, s1, shift); + //orc_program_append (p, "sub_s16", t1, t1, shift); + //orc_program_append (p, "mul_s16", d1, s1, s2); + //orc_program_append (p, "_loadi_s16", t1, t1, shift); +#endif + + orc_program_compile (p); + + if (1) { + int i; + + for(i=0;i<N;i++){ + src1[i] = rand()&0xf; + src2[i] = rand()&0xf; + } + + ex = orc_executor_new (p); + + orc_executor_set_n (ex, N-4); + orc_executor_set_array (ex, s1, src1); + orc_executor_set_array (ex, s2, src2); + orc_executor_set_array (ex, d1, dest); + + printf("#code exec %p\n", ex->program->code_exec); + + orc_executor_run (ex); + //orc_executor_emulate (ex); + + for(i=0;i<N;i++){ + printf("# %4d %4d %4d %4d\n", src1[i], src2[i], dest[i], + (src1[i] + src2[i] + 1) >> 1); + } + + orc_executor_free (ex); + } + + orc_program_free (p); + + return 0; +} + + + +void +test1 (int16_t *dest, int16_t *src1, int16_t *src2, int n) +{ + int i; + int16_t t1, t2; + for(i=0;i<n;i++){ + t1 = src1[i] + src2[i]; + t2 = t1 + 1; + dest[i] = t2>>1; + } +} + diff --git a/examples/simple.c b/examples/simple.c new file mode 100644 index 0000000..388475c --- /dev/null +++ b/examples/simple.c @@ -0,0 +1,144 @@ + +#include "config.h" + +#include <glib.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include <orc/orcprogram.h> + +#define N 100 + +int16_t src1[N+4]; +int16_t src2[N]; +int16_t dest_test[N]; +int16_t dest_ref[N]; +int16_t dest[N]; + +void test1(void); +void test2(void); + +int +main (int argc, char *argv[]) +{ + orc_init (); + + test1(); + + exit(0); +} + + +void +test1(void) +{ + OrcProgram *p; + OrcExecutor *ex; + + p = orc_program_new_dss ("s16", "s16", "s16"); + + orc_program_append_str (p, "add_s16", "d1", "s1", "s2"); + + orc_program_compile (p); + + ex = orc_executor_new (p); + orc_executor_set_n (ex, N - 4); + orc_executor_set_array_str (ex, "s1", src1); + orc_executor_set_array_str (ex, "s2", src2); + orc_executor_set_array_str (ex, "d1", dest); + + if (1) { + int i; + + for(i=0;i<N;i++){ + src1[i] = rand()&0xf; + src2[i] = rand()&0xf; + } + + orc_executor_run (ex); + //orc_executor_emulate (ex); + + for(i=0;i<N;i++){ + printf("# %4d %4d %4d %4d\n", src1[i], src2[i], dest[i], + src1[i] + src2[i]); + } + } + + orc_executor_free (ex); + orc_program_free (p); +} + + +void +test2(void) +{ + OrcProgram *p; + OrcExecutor *ex; + int s1, s2, s3, s4, d1; + int t1, t2; + int c1, c2, c3; + int n; + + p = orc_program_new (); + + d1 = orc_program_add_destination (p, "s16", "d1"); + s1 = orc_program_add_source (p, "s16", "s1"); + s2 = orc_program_add_source (p, "s16", "s2"); + s3 = orc_program_add_source (p, "s16", "s3"); + s4 = orc_program_add_source (p, "s16", "s4"); + c1 = orc_program_add_constant (p, "s16", 3, "c1"); + c2 = orc_program_add_constant (p, "s16", 4, "c2"); + c3 = orc_program_add_constant (p, "s16", 3, "c3"); + t1 = orc_program_add_temporary (p, "s16", "t1"); + t2 = orc_program_add_temporary (p, "s16", "t2"); + + orc_program_append (p, "add_s16", t1, s2, s3); + orc_program_append (p, "add_s16", t2, s1, s4); + orc_program_append (p, "mul_s16", t1, t1, c1); + orc_program_append (p, "sub_s16", t1, t1, t2); + orc_program_append (p, "add_s16", t1, t1, c2); + orc_program_append (p, "rshift_s16", d1, t1, c3); + + orc_program_compile (p); + + ex = orc_executor_new (p); + orc_executor_set_n (ex, N); + orc_executor_set_array (ex, s1, src1); + orc_executor_set_array (ex, s2, src1 + 1); + orc_executor_set_array (ex, s3, src1 + 2); + orc_executor_set_array (ex, s4, src1 + 3); + + for(n=0;n<20;n++) { + int i; + + for(i=0;i<n+3;i++){ + src1[i] = rand()&0xff; + } + for(i=0;i<n+4;i++){ + dest[i] = 0; + } + + orc_executor_set_n (ex, n); + orc_executor_set_array (ex, d1, dest_ref); + orc_executor_emulate (ex); +#if 0 + for(i=0;i<n;i++){ + dest_ref[i] = (3*(src1[i+1]+src1[i+2])-(src1[i]+src1[i+3])+4)>>3; + } +#endif + + orc_executor_set_array (ex, d1, dest_test); + orc_executor_run (ex); + + for(i=0;i<n+4;i++){ + printf("# %d: %4d %4d %4d %c\n", i, src1[i], dest_ref[i], dest_test[i], + (dest_ref[i] == dest_test[i])?' ':'*'); + } + } + + orc_executor_free (ex); + orc_program_free (p); +} + + |