vector test
[libfirm] / ir / be / test / vadd_store.c
1 /************************************************************************
2  * Program:  vadd_store.c
3  * Function: Add 2 vectors (lying in memory) and store the result in
4  *           a another vector in memory.
5  *           Used as a test for the simd optimization.
6  * Author:   Andreas Schoesser
7  * Date:     2007-02-13
8  ************************************************************************/
9
10 #include <stdio.h>
11 #include <malloc.h>
12 #include <stdlib.h>
13
14 void vadd_store();
15 //void vadd_loop();
16 //void array_test(int *a[]);
17
18 main()
19 {
20         int a[5][5];
21         a[1][1] = 20;
22
23         printf("1. vload -> vadd -> vstore\n===================\n\n");
24 //      vadd_store();
25
26         printf("2. vload -> vadd -> vstore, multi dimensional array, in loop\n==========================================\n\n");
27         vadd_loop();
28
29 //      array_test(a);
30 }
31
32 #if 0
33 void vadd_store()
34 {
35         /*Also possible: Local pointers instead of function parameters: */
36         //int *a = (int *) alloca(8), *b = (int *) alloca(8), *c = (int *) alloca(8);
37         int a[2], b[2], c[2];
38         int i;
39
40         for(i = 0; i < 2; i++)
41         {
42                 a[i] = rand() % 10;
43                 b[i] = rand() % 10;
44         }
45
46
47 /*  Also possible: Local arrays, but may be optimized so that no pattern is found
48         int a[2], b[2], c[2];      */
49
50
51 /*  Version 1: Directly adding.  */
52
53         c[0] = a[0] + b[0];
54         c[1] = a[1] + b[1];
55
56
57
58 /*      Version 2: Adding of local variables
59
60         int a0, a1, b0, b1, c0, c1;
61
62         a0 = a[0];
63         a1 = a[1];
64         b0 = b[0];
65         b1 = b[1];
66
67         c0 = a0 + b0;
68         c1 = a1 + b1;
69
70         c[0] = c0;
71         c[1] = c1; */
72
73
74
75 /*  Version 3: Mixed adding of locals and pointers
76
77         int a0, b1, c0;
78
79         a0 = a[0];
80         b1 = b[1];
81
82         c0 = a0 + b[0];
83         c[1] = a[1] + b1;
84
85         c[0] = c0; */
86
87         for(i = 0; i < 2; i++)
88                 printf("%d + %d = %d\n", a[i], b[i], c[i]);
89         printf("\n");
90
91 }
92 #endif
93
94 #if 1
95 void vadd_loop()
96 {
97
98         /* Version 4: Manually unrolled loop  */
99
100         int i, j;
101         int d[5][5], e[5][5], f[5][5];
102
103         for(j = 0; j < 4; j++)
104                 for(i = 0; i < 2; i++)
105                 {
106                         e[j][i] = rand() % 10;
107                         f[j][i] = rand() % 10;
108                 }
109
110         for(j = 0; j < 4; j++)
111                 //for(i = 0; i < 2; i ++ )
112         {
113                 d[j][0] = e[j][0] + f[j][0];
114                 d[j][1] = e[j][1] + f[j][1];
115         }
116
117         for(j = 0; j < 4; j++)
118         {
119                 for(i = 0; i < 2; i++)
120                         printf("%d + %d = %d\n", e[j][i], f[j][i], d[j][i]);
121                 printf("\n");
122         }
123 }
124 #endif
125
126 #if 0
127 void array_test(int a[][5])
128 {
129         printf("\n a[1][1]: %d\n", a[1][1]);
130 }
131 #endif