fehler127: WTF - autobreak expects this to compile.
[libfirm] / ir / be / test / fehler068.c
1 /************************************************************************
2  * Program:  scalar_product.c
3  * Function: Calculates the scalar product of vector lying in memory
4  *           Used as a test for the simd optimization.
5  * Author:   Andreas Schoesser
6  * Date:     2007-06-13
7  ************************************************************************/
8
9 #include <stdio.h>
10 #include <malloc.h>
11 #include <stdlib.h>
12 #include <time.h>
13
14 float scalar_product(float *a, float *b, unsigned int max_elements);
15
16 main()
17 {
18         float res;
19         int i, max_elements = 100;
20         double  d_zeitdauer;
21
22         // Allocate memory and make sure pointers are aligned to 16 byte addresses
23         char *a = malloc(16 + max_elements * sizeof(float));
24         char *b = malloc(16 + max_elements * sizeof(float));
25         float c;
26         char *ca = &a[0] + 16 - (unsigned int) ((unsigned int) &a[0] % 16);
27         char *cb = &b[0] + 16 - (unsigned int) ((unsigned int) &b[0] % 16);
28
29         float *aa = (float *) ca;
30         float *ab = (float *) cb;
31
32         srand(0);
33
34         printf("Scalar product\n==============\n\n");
35
36         //printf("Array Position: %u, %u, %u, %u\n", a, b, aa, ba/*(unsigned int) &aa[0] % 16, (unsigned int) &ba[0] % 16*/);
37
38         // Fill both arrays with random values
39         for(i = 0; i < max_elements; i++)
40         {
41                 aa[i] = (float) (rand() % 10);
42                 ab[i] = (float) (rand() % 10);
43
44                 //printf("(%g * %g)  +  ", a[i], b[i]);
45         }
46
47         res = scalar_product(aa, ab, max_elements);
48
49         printf("\nResult: %g\n", res);
50 }
51
52
53 float scalar_product(float * a, float * b, unsigned int max_elements)
54 {
55         float res = 0;
56         int   i;
57
58         for(i = 0; i < max_elements; i += 4) {
59                 res += a[i]     * b[i];
60                 res += a[i + 1] * b[i + 1];
61                 res += a[i + 2] * b[i + 2];
62                 res += a[i + 3] * b[i + 3];
63         }
64
65         return res;
66 }