grayscale_simd.c 1.44 KB
Newer Older
1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>
#include "arrays.h"

#ifndef N
#define N 2
#endif

Marc's avatar
Marc committed
9
10
11
12
13
14
15
16
17
//void init(unsigned char a[N][N][4]){
//    for (int i = 0; i<N; i++)
//        for (int j = 0; j<N; j++){
//            a[i][j][0] = rand()%255;
//            a[i][j][1] = rand()%255;
//            a[i][j][2] = rand()%255;
//            a[i][j][3] = 255;
//        }
//}
18

Marc's avatar
Marc committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//int b = 0xfcfcfc7f;

//int shift_and_add(int a){
   //int r;
   //asm("srl %1, %0, %0" 
   //        : "=r"(r) 
   //        : "r"(a), "0"(b));
   //return r;
//}

int shift_and_add(int a);
asm("shift_and_add:");
asm("retl"); 
asm("srl %o0, %g1, %g1"); 
33
34


Marc's avatar
Marc committed
35
36
__attribute__((optimize("unroll-loops")))
void grayscale(unsigned char src[N][N][4], unsigned char dst[N][N]){
37
    unsigned char color;
Marc's avatar
Marc committed
38
39
40
    asm("nop");
    asm("srl %i0, %o1, %g2");
    asm("nop");
41
42
    for (int i = 0; i<N; i++)
        for (int j = 0; j<N; j++){
Marc's avatar
Marc committed
43
            dst[i][j]= *((int *) &src[i][j][0]) >> 2; //shift_and_add(*((int *) &src[i][j][0]));
44
        }
Marc's avatar
Marc committed
45
46
47
    asm("nop");
    asm("srl %i0, %o1, %g2");
    asm("nop");
48
49
}

Marc's avatar
Marc committed
50
void print(unsigned char src[N][N]) {
51
52
53
    printf("P3\n%d %d\n255\n",N,N);
    for (int i = 0; i<N; i++){
        for (int j = 0; j<N; j++){
Marc's avatar
Marc committed
54
            printf("%d %d %d ", src[i][j], src[i][j], src[i][j]);
55
56
57
58
59
60
61
62
        }
        printf("\n");
    }
}
    

int main(){
    unsigned char source[N][N][4] = {IMAGE_ARRAY};
Marc's avatar
Marc committed
63
    unsigned char dest[N][N];
64
65
66
67
    //init(source);
    grayscale(source, dest);
    print(dest);
}