Debian Codegraph @

cvd_src/SSE2/gradient.cc

raw symbols SCIP
1 #include "cvd/vision.h"
2 #include <emmintrin.h>
3
4 using namespace std;
5
6 namespace CVD
7 {
8
9 #ifdef WIN32
10 #define shift_left_7(thing) _mm_slli_epi16(thing, 7)
11 #else
12 // I have to use this because gcc 3.3 has an internal bug with _mm_slli_epi16
13 #define shift_left_7(thing) asm("psllw $0x7, %0 \n\t" \
14 : \
15 : "x"(thing) \
16 : "%0");
17 #endif
18
19 void gradient(const byte* in, short (*out)[2], int w, int h)
20 {
21 const byte zeros[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
22 const byte* curr = in + w;
23 out += w;
24 int total = (w * (h - 2)) >> 4;
25 __m128i zero = _mm_loadu_si128((const __m128i*)zeros);
26 while(total--)
27 {
28 __m128i up = _mm_load_si128((const __m128i*)(curr - w));
29 __m128i down = _mm_load_si128((const __m128i*)(curr + w));
30 __m128i hor_left, hor_right;
31 {
32 __m128i hor = _mm_load_si128((const __m128i*)curr);
33 hor_left = _mm_slli_si128(hor, 1);
34 hor_right = _mm_srli_si128(hor, 1);
35 }
36 {
37 __m128i left = _mm_unpacklo_epi8(hor_right, zero);
38 __m128i right = _mm_unpacklo_epi8(hor_left, zero);
39 __m128i hdiff = _mm_insert_epi16(_mm_sub_epi16(right, left), short(curr[1]) - short(curr[-1]), 0);
40 __m128i vdiff = _mm_sub_epi16(_mm_unpacklo_epi8(down, zero), _mm_unpacklo_epi8(up, zero));
41 {
42 __m128i part1 = _mm_unpacklo_epi16(hdiff, vdiff);
43 shift_left_7(part1);
44 _mm_stream_si128((__m128i*)out, part1);
45 }
46 {
47 __m128i part2 = _mm_unpackhi_epi16(hdiff, vdiff);
48 shift_left_7(part2);
49 _mm_stream_si128((__m128i*)(out + 4), part2);
50 }
51 }
52 {
53 __m128i left = _mm_unpackhi_epi8(hor_right, zero);
54 __m128i right = _mm_unpackhi_epi8(hor_left, zero);
55 __m128i hdiff = _mm_insert_epi16(_mm_sub_epi16(right, left), short(curr[16]) - short(curr[14]), 7);
56 __m128i vdiff = _mm_sub_epi16(_mm_unpackhi_epi8(down, zero), _mm_unpackhi_epi8(up, zero));
57 {
58 __m128i part3 = _mm_unpacklo_epi16(hdiff, vdiff);
59 shift_left_7(part3);
60 _mm_stream_si128((__m128i*)(out + 8), part3);
61 }
62 {
63 __m128i part4 = _mm_unpackhi_epi16(hdiff, vdiff);
64 shift_left_7(part4);
65 _mm_stream_si128((__m128i*)(out + 12), part4);
66 }
67 }
68 curr += 16;
69 out += 16;
70 }
71 _mm_empty();
72 }
73
74 void gradient(const BasicImage<byte>& im, BasicImage<short[2]>& out)
75 {
76 if(im.size() != out.size())
77 throw Exceptions::Vision::IncompatibleImageSizes("gradient");
78
79 if(is_aligned<16>(im.data()) && is_aligned<16>(out.data()) && im.size().x % 16 == 0)
80 {
81 zeroBorders(out);
82 gradient(im.data(), out.data(), im.size().x, im.size().y);
83 }
84 else
85 gradient<byte, short[2]>(im, out);
86 }
87
88 };