-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy path1-ffi.c
160 lines (132 loc) · 2.99 KB
/
1-ffi.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//translated from Zig
//
//gcc -O3 -mno-fma -march=native -Wall main.c
//clang -O3 -Wno-deprecated -mno-fma -mllvm -polly -mllvm -polly-parallel -lgomp -mllvm -polly-vectorizer=stripmine -lm -o main main.c -fopenmp=libomp -march=native -lcrypto
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <openssl/md5.h>
#define VEC_SIZE 8
typedef struct Vec
{
double x[VEC_SIZE];
} Vec;
void fill(Vec* in,double val)
{
for(int i=0;i<VEC_SIZE;++i)
{
in->x[i]=val;
}
}
void mul(Vec* r,Vec* a,Vec* b)
{
for(int i=0;i<VEC_SIZE;++i)
{
r->x[i]=a->x[i]*b->x[i];
}
}
void minus(Vec* r,Vec* a,Vec* b)
{
for(int i=0;i<VEC_SIZE;++i)
{
r->x[i]=a->x[i]-b->x[i];
}
}
void add(Vec* r,Vec* a,Vec* b)
{
for(int i=0;i<VEC_SIZE;++i)
{
r->x[i]=a->x[i]+b->x[i];
}
}
unsigned char mbrot8(Vec* cr, double civ)
{
Vec ci,zr,zi,tr,ti,absz;
fill(&ci,civ);
fill(&zr,0.0);
fill(&zi,0.0);
fill(&tr,0.0);
fill(&ti,0.0);
fill(&absz,0.0);
Vec tmp;
for(int i=0;i<10;++i)
{
for(int j=0;j<5;++j)
{
add(&tmp, &zr, &zr);
mul(&tmp, &tmp, &zi);
add(&zi, &tmp, &ci);
minus(&tmp, &tr, &ti);
add(&zr, &tmp, cr);
mul(&tr, &zr, &zr);
mul(&ti, &zi, &zi);
}
add(&absz,&tr,&ti);
unsigned char terminate = true;
for(int k=0;k<VEC_SIZE;++k)
{
if(absz.x[k]<=4.0)
{
terminate=false;//0
break;
}
}
if(terminate)
{
return 0;
}
}
char accu=0;
for(int k=0;k<VEC_SIZE;++k)
{
if(absz.x[k]<=4.0)
{
const unsigned char lhs = 0x80;
accu |= (lhs >> k);
}
}
return accu;
}
double init_xloc(int i,double inv)
{
return ((double)i)*inv-1.5;
}
int main(int argc, char **argv)
{
int n = (argc > 1) ? atoi(argv[1]) : 200;
int size = (n + VEC_SIZE - 1)/VEC_SIZE*VEC_SIZE;
int chunk_size=size/VEC_SIZE;
double inv = 2.0 / ((double)size);
Vec *xloc=(Vec*) malloc(chunk_size*sizeof(Vec));
for(int i=0;i<chunk_size;++i)
{
int offset = i*VEC_SIZE;
for(int j=0;j<VEC_SIZE;++j)
{
xloc[i].x[j]=init_xloc(offset+j, inv);
}
}
printf("P4\n%d %d\n",size,size);
unsigned char* pixels = (unsigned char*)malloc(size*chunk_size*sizeof(unsigned char));
for(int y=0;y<size;++y)
{
double ci = ((double)y)*inv-1.0;
for(int x=0;x<chunk_size;++x)
{
pixels[y*chunk_size+x]=mbrot8(&xloc[x],ci);
}
}
/* printf("{ ");
for(int x=0;x<size*chunk_size;++x)
{
printf("%d, ",pixels[x]);
}// */
unsigned char *output = MD5(pixels, size*chunk_size,NULL);
for(int i=0;i<16;++i)
{
printf("%02x", (unsigned char)output[i]);
}
printf("\n");
//free(output);
return 0;
}