Debian Codegraph @

src/thirdparty/VLFeat/imopv_sse2.c

raw
1 /** @file imopv_sse2.c
2 ** @brief Vectorized image operations - SSE2 - Definition
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 All rights reserved.
9
10 This file is part of the VLFeat library and is made available under
11 the terms of the BSD license (see the COPYING file).
12 */
13
14 #if ! defined(VL_DISABLE_SSE2) & ! defined(__SSE2__)
15 #error "Compiling with SSE2 enabled, but no __SSE2__ defined"
16 #endif
17
18 #if ! defined(VL_DISABLE_SSE2)
19
20 #ifndef VL_IMOPV_SSE2_INSTANTIATING
21
22 #include <emmintrin.h>
23
24 #include "imopv.h"
25 #include "imopv_sse2.h"
26
27 #define FLT VL_TYPE_FLOAT
28 #define VL_IMOPV_SSE2_INSTANTIATING
29 #include "imopv_sse2.c"
30
31 #define FLT VL_TYPE_DOUBLE
32 #define VL_IMOPV_SSE2_INSTANTIATING
33 #include "imopv_sse2.c"
34
35 /* ---------------------------------------------------------------- */
36 /* VL_IMOPV_SSE2_INSTANTIATING */
37 #else
38
39 #include "float.h"
40
41 /* ---------------------------------------------------------------- */
42 void
43 VL_XCAT3(_vl_imconvcol_v, SFX, _sse2)
44 (T* dst, vl_size dst_stride,
45 T const* src,
46 vl_size src_width, vl_size src_height, vl_size src_stride,
47 T const* filt, vl_index filt_begin, vl_index filt_end,
48 int step, unsigned int flags)
49 {
50 vl_index x = 0 ;
51 vl_index y ;
52 vl_index dheight = (src_height - 1) / step + 1 ;
53 vl_bool use_simd = VALIGNED(src_stride) ;
54 vl_bool transp = flags & VL_TRANSPOSE ;
55 vl_bool zeropad = (flags & VL_PAD_MASK) == VL_PAD_BY_ZERO ;
56 double totcol = 0 ;
57 double simdcol = 0 ;
58
59 /* let filt point to the last sample of the filter */
60 filt += filt_end - filt_begin ;
61
62 while (x < (signed)src_width) {
63 /* Calculate dest[x,y] = sum_p image[x,p] filt[y - p]
64 * where supp(filt) = [filt_begin, filt_end] = [fb,fe].
65 *
66 * CHUNK_A: y - fe <= p < 0
67 * completes VL_MAX(fe - y, 0) samples
68 * CHUNK_B: VL_MAX(y - fe, 0) <= p < VL_MIN(y - fb, height - 1)
69 * completes fe - VL_MAX(fb, height - y) + 1 samples
70 * CHUNK_C: completes all samples
71 */
72
73 T const *filti ;
74 vl_index stop ;
75
76 if ((x + VSIZE < (signed)src_width) &
77 VALIGNED(src + x) & use_simd)
78 {
79 /* ---------------------------------------------- Vectorized */
80 for (y = 0 ; y < (signed)src_height ; y += step) {
81 union {VTYPE v ; T x [VSIZE] ; } acc ;
82 VTYPE v, c ;
83 T const *srci ;
84 acc.v = VSTZ () ;
85 v = VSTZ() ;
86
87 filti = filt ;
88 stop = filt_end - y ;
89 srci = src + x - stop * src_stride ;
90
91 if (stop > 0) {
92 if (zeropad) {
93 v = VSTZ () ;
94 } else {
95 v = * (VTYPE*) (src + x) ;
96 }
97 while (filti > filt - stop) {
98 c = VLD1 (filti--) ;
99 acc.v = VADD (acc.v, VMUL (v, c)) ;
100 srci += src_stride ;
101 }
102 }
103
104 stop = filt_end - VL_MAX(filt_begin, y - (signed)src_height + 1) + 1 ;
105 while (filti > filt - stop) {
106 v = * (VTYPE*) srci ;
107 c = VLD1 (filti--) ;
108 acc.v = VADD (acc.v, VMUL (v, c)) ;
109 srci += src_stride ;
110 }
111
112 if (zeropad) v = VSTZ () ;
113
114 stop = filt_end - filt_begin + 1;
115 while (filti > filt - stop) {
116 c = VLD1 (filti--) ;
117 acc.v = VADD (acc.v, VMUL (v, c)) ;
118 }
119
120 if (transp) {
121 *dst = acc.x[0] ; dst += dst_stride ;
122 *dst = acc.x[1] ; dst += dst_stride ;
123 #if(VSIZE == 4)
124 *dst = acc.x[2] ; dst += dst_stride ;
125 *dst = acc.x[3] ; dst += dst_stride ;
126 #endif
127 dst += 1 * 1 - VSIZE * dst_stride ;
128 } else {
129 *dst = acc.x[0] ; dst += 1 ;
130 *dst = acc.x[1] ; dst += 1 ;
131 #if(VSIZE == 4)
132 *dst = acc.x[2] ; dst += 1 ;
133 *dst = acc.x[3] ; dst += 1 ;
134 #endif
135 dst += 1 * dst_stride - VSIZE * 1 ;
136 }
137 } /* next y */
138 if (transp) {
139 dst += VSIZE * dst_stride - dheight * 1 ;
140 } else {
141 dst += VSIZE * 1 - dheight * dst_stride ;
142 }
143 x += VSIZE ;
144 simdcol += VSIZE ;
145 totcol += VSIZE ;
146 } else {
147 /* ------------------------------------------------- Vanilla */
148 for (y = 0 ; y < (signed)src_height ; y += step) {
149 T acc = 0 ;
150 T v = 0, c ;
151 T const* srci ;
152
153 filti = filt ;
154 stop = filt_end - y ;
155 srci = src + x - stop * src_stride ;
156
157 if (stop > 0) {
158 if (zeropad) {
159 v = 0 ;
160 } else {
161 v = *(src + x) ;
162 }
163 while (filti > filt - stop) {
164 c = *filti-- ;
165 acc += v * c ;
166 srci += src_stride ;
167 }
168 }
169
170 stop = filt_end - VL_MAX(filt_begin, y - (signed)src_height + 1) + 1 ;
171 while (filti > filt - (signed)stop) {
172 v = *srci ;
173 c = *filti-- ;
174 acc += v * c ;
175 srci += src_stride ;
176 }
177
178 if (zeropad) v = 0 ;
179
180 stop = filt_end - filt_begin + 1 ;
181 while (filti > filt - stop) {
182 c = *filti-- ;
183 acc += v * c ;
184 }
185
186 if (transp) {
187 *dst = acc ; dst += 1 ;
188 } else {
189 *dst = acc ; dst += dst_stride ;
190 }
191 } /* next y */
192 if (transp) {
193 dst += 1 * dst_stride - dheight * 1 ;
194 } else {
195 dst += 1 * 1 - dheight * dst_stride ;
196 }
197 x += 1 ;
198 totcol += 1 ;
199 } /* next x */
200 }
201 }
202
203 /* ---------------------------------------------------------------- */
204 #if 0
205 void
206 VL_XCAT(_vl_imconvcoltri_v, SFX, sse2)
207 (T* dst, int dst_stride,
208 T const* src,
209 int src_width, int src_height, int src_stride,
210 int filt_size,
211 int step, unsigned int flags)
212 {
213 int x = 0 ;
214 int y ;
215 int dheight = (src_height - 1) / step + 1 ;
216 vl_bool use_simd = ((src_stride & ALIGNSTRIDE) == 0) &&
217 (! (flags & VL_NO_SIMD)) ;
218 vl_bool transp = flags & VL_TRANSPOSE ;
219 vl_bool zeropad = (flags & VL_PAD_MASK) == VL_PAD_BY_ZERO ;
220
221 T * buff = vl_malloc(sizeof(T) * (src_height + filt_size)) ;
222 #define fa (1.0 / (double) (filt_size + 1))
223 T scale = fa*fa*fa*fa ;
224 buff += filt_size ;
225
226 while (x < src_width) {
227 T const *srci ;
228
229 use_simd = 0 ;
230 if ((x + VSIZE < src_width) &
231 (((vl_ptrint)(src + x) & ALIGNPTR) == 0) &
232 use_simd)
233 {
234
235 } else {
236 int stridex = transp ? dst_stride : 1 ;
237 int stridey = transp ? 1 : dst_stride ;
238 srci = src + x + src_stride * (src_height - 1) ;
239
240 /* integrate backward the column */
241 buff [src_height - 1] = *srci ;
242 for (y = src_height-2 ; y >= 0 ; --y) {
243 srci -= src_stride ;
244 buff [y] = buff [y+1] + *srci ;
245 }
246 if (zeropad) {
247 for ( ; y >= - filt_size ; --y) {
248 buff [y] = buff [y+1] ;
249 }
250 } else {
251 for ( ; y >= - filt_size ; --y) {
252 buff [y] = buff[y+1] + *srci ;
253 }
254 }
255
256 /* compute the filter forward */
257 for (y = - filt_size ; y < src_height - filt_size ; ++y) {
258 buff [y] = buff [y] - buff [y + filt_size] ;
259 }
260 if (! zeropad) {
261 for (y = src_height - filt_size ; y < src_height ; ++y) {
262 buff [y] = buff [y] - buff [src_height-1] *
263 (src_height - filt_size - y) ;
264 }
265 }
266
267 /* integrate forward the column */
268 for (y = - filt_size + 1 ; y < src_height ; ++y) {
269 buff [y] += buff [y - 1] ;
270 }
271
272 /* compute the filter backward */
273 for (y = src_height - 1 ; y >= 0 ; --y) {
274 dst [x*stridex + y*stridey]
275 = scale * (buff [y] - buff [y - filt_size]) ;
276 }
277 } /* next y */
278 x += 1 ;
279 }
280 vl_free (buff - filt_size) ;
281 }
282 #endif
283
284 #undef FLT
285 #undef VL_IMOPV_SSE2_INSTANTIATING
286 #endif
287
288 /* ! VL_DISABLE_SSE2 */
289 #endif