summaryrefslogtreecommitdiff
path: root/examples/orc/jit.c
blob: 9f40a73d1858b4977cef2b8f62036b71ab1ea423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <orc/orcprogram.h>

#define N 20

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);
    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;
  }
}