-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_dist.c
188 lines (142 loc) · 6.37 KB
/
calc_dist.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* PROJ1-1: YOUR TASK A CODE HERE
*
* You MUST implement the calc_min_dist() function in this file.
*
* You do not need to implement/use the swap(), flip_horizontal(), transpose(), or rotate_ccw_90()
* functions, but you may find them useful. Feel free to define additional helper functions.
*/
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include "digit_rec.h"
#include "utils.h"
#include <nmmintrin.h>
//#include <emmintrin.h>
float min(float a, float b) {
if (a < b) {
return a;
}
return b;
}
/* Swaps the values pointed to by the pointers X and Y. */
void swap(float *x, float *y) {
float temp = *x;
*x = *y;
*y = temp;
}
/* Flips the elements of a square array ARR across the y-axis. */
void flip_horizontal(float *arr, int width) {
int x, y;
for (y = 0; y < width*width; y += width) {
for (x = 0; x < width/2; x += 1) {
swap(&arr[y+x], &arr[width + y - x - 1]);
}
}
}
/* Transposes the square array ARR. */
void transpose(float *arr, int width) {
int size = width * width;
for (int i = 0; i < size; i += width + 1) { //i is x value
for (int j = 1; j < (size - i + width - 1)/width; j++) {
swap(&arr[i + j], &arr[i + j*width]);
}
}
}
/* Rotates the square array ARR by 90 degrees counterclockwise. */
void rotate_ccw_90(float *arr, int width) {
flip_horizontal(arr, width);
transpose(arr, width);
}
/* Calculates the distance between an IMAGE and a TEMPLATE. */
float calc(float *image, float *template, int i_width, int i_height,
int t_width, int i, int j) {
float ret = 0.0;
float array[4];
__m128 dist = _mm_setzero_ps();
for (int q = 0; q < t_width; q++) {
for (int p = 0; p < (t_width/16) * 16; p += 16) { //loop by 16 FLOATS
__m128 sub = _mm_setzero_ps();
__m128 _image = _mm_loadu_ps(image + i + p + (q + j) * i_width);
__m128 _template = _mm_loadu_ps(template + p + q * t_width);
sub = _mm_sub_ps(_template, _image);
dist = _mm_add_ps(dist, _mm_mul_ps(sub, sub));
_image = _mm_loadu_ps(image + i + p + 4 + (q + j) * i_width);
_template = _mm_loadu_ps(template + p + 4 + q * t_width);
sub = _mm_sub_ps(_image, _template);
dist = _mm_add_ps(dist, _mm_mul_ps(sub, sub));
_image = _mm_loadu_ps(image + i + p + 8 + (q + j) * i_width);
_template = _mm_loadu_ps(template + p + 8 + q * t_width);
sub = _mm_sub_ps(_image, _template);
dist = _mm_add_ps(dist, _mm_mul_ps(sub, sub));
_image = _mm_loadu_ps(image + i + p + 12 + (q + j) * i_width);
_template = _mm_loadu_ps(template + p + 12 + q * t_width);
sub = _mm_sub_ps(_image, _template);
dist = _mm_add_ps(dist, _mm_mul_ps(sub, sub));
}
for (int p = (t_width/16)*16; p < t_width; p++) {
ret += (image[i + p + (q + j) * i_width] - template[p + q * t_width])
* (image[i + p + (q + j) * i_width] - template[p + q * t_width]);
}
}
_mm_storeu_ps(array, dist);
ret += array[0] + array[1] + array[2] + array[3];
return ret;
}
/* Translates IMAGE across TEMPLATE. */
float translate(float *image, float *template, int i_width, int i_height,
int t_width) {
float min_dist = FLT_MAX;
for (int j = 0; j <= i_height - t_width; j += 1) {
for (int i = 0; i <= i_width - t_width; i += 1) {
min_dist = min(min_dist, calc(image, template, i_width, i_height, t_width, i, j));
}
}
return min_dist;
}
/* Returns the squared Euclidean distance between TEMPLATE and IMAGE. The size of IMAGE
* is I_WIDTH * I_HEIGHT, while TEMPLATE is square with side length T_WIDTH. The template
* image should be flipped, rotated, and translated across IMAGE.
*/
float calc_min_dist(float *image, int i_width, int i_height,
float *template, int t_width) {
float min_dist = FLT_MAX;
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
flip_horizontal(template, t_width); //one flip
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
transpose(template, t_width); //transpose
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
flip_horizontal(template, t_width); //flipped transpose
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
rotate_ccw_90(template, t_width);
min_dist = min(min_dist, translate(image, template, i_width, i_height, t_width));
return min_dist;
}