Added 2 testcases for the IA32 backend
[libfirm] / ir / be / test / fehler68.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 = 100000000;
20         clock_t t_time_bev, t_time_after, t_clocks_dauer;
21         double  d_zeitdauer;
22
23         // Allocate memory and make sure pointers are aligned to 16 byte addresses
24         char *a = malloc(16 + max_elements * sizeof(float));
25         char *b = malloc(16 + max_elements * sizeof(float));
26         float c;
27         char *ca = &a[0] + 16 - (unsigned int) ((unsigned int) &a[0] % 16);
28         char *cb = &b[0] + 16 - (unsigned int) ((unsigned int) &b[0] % 16);
29
30         float *aa = (float *) ca;
31         float *ab = (float *) cb;
32
33         printf("Scalar product\n==============\n\n");
34
35         //printf("Array Position: %u, %u, %u, %u\n", a, b, aa, ba/*(unsigned int) &aa[0] % 16, (unsigned int) &ba[0] % 16*/);
36
37         // Fill both arrays with random values
38         for(i = 0; i < max_elements; i++)
39         {
40                 aa[i] = (float) (rand() % 10);
41                 ab[i] = (float) (rand() % 10);
42
43                 //printf("(%g * %g)  +  ", a[i], b[i]);
44         }
45
46         // Start time measurement
47         t_time_bev = clock();
48
49         //for(i = 0; i < max_elements - 4; i += 4)
50         res = scalar_product(aa, ab, max_elements);
51
52         // Stop time measurement
53         t_time_after = clock();
54         t_clocks_dauer = (t_time_after-t_time_bev);
55         d_zeitdauer = (double) (t_time_after-t_time_bev) / CLOCKS_PER_SEC;
56
57         #ifdef __GNUC__
58                 printf("Zeitdauer %g s\n", d_zeitdauer);
59         #else
60                 printf("Zeitdauer %g ms\n", d_zeitdauer);
61         #endif
62
63         printf("\nResult: %g\n", res);
64 }
65
66
67 float scalar_product(float * a, float * b, unsigned int max_elements)
68 {
69         float res;
70         int   i;
71
72         /*for(i = 0; i < 4; i++)
73         {
74                 a[i] = (float) (rand() % 10);
75                 b[i] = (float) (rand() % 10);
76
77                 printf("(%g * %g)  +  ", a[i], b[i]);
78         }*/
79
80         for(i = 0; i < max_elements; i += 4)
81                 res += a[i] * b[i] + a[i + 1] * b[i + 1] + a[i + 2] * b[i + 2] + a[i + 3] * b[i + 3];
82
83         return(res);
84 }