You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
7.5 KiB

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