Debian Codegraph @

cvd_src/SSE2/two_thirds_sample.cc

raw symbols SCIP
1 #include "cvd/vision.h"
2 #include <emmintrin.h>
3
4 namespace CVD
5 {
6 namespace
7 {
8
9 #define _mm_shuffle_32(X, Y, Z) \
10 _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(X), _mm_castsi128_ps(Y), Z))
11
12 void shift_3(__m128i& w_01_08, __m128i& w_09_16, __m128i& w_17_24)
13 {
14 //This performs a shift by one word (2 bytes), over three
15 //registers, spilling 0 in to pixel 1, pixel 8 in to pixel 9 and
16 //so on.
17 //
18 //The shift direction moves upwards in memory, so since the three registers
19 //represent a row of pixels, the shift moves pixels to the right.
20 __m128i w_extra = _mm_shuffle_32(_mm_srli_si128(w_09_16, 2), w_01_08, _MM_SHUFFLE(3, 3, 3, 3));
21 w_17_24 = _mm_or_si128(_mm_slli_si128(w_17_24, 2), _mm_and_si128(w_extra, _mm_set_epi16(0, 0, 0, 0, 0, 0, 0, short(0xffff))));
22 w_09_16 = _mm_or_si128(_mm_slli_si128(w_09_16, 2), _mm_srli_si128(w_extra, 14));
23 w_01_08 = _mm_slli_si128(w_01_08, 2);
24 }
25 void extract_pairs(__m128i& w_01_08, __m128i& w_09_16, __m128i& w_17_24, __m128i& w_chnk1, __m128i& w_chnk2)
26 {
27 //[w_01_08 w_09_16 w_17_24] is [ [A B C D E F G H] [I J K L M N O P] [Q R S T U V W X] ]
28 //We want to end with a selection of pairs:
29 //[ chunk1 chunk2 ] = [ [ B C E F H I K L ] [ N O Q R T W W X ] ]
30
31 __m128i w_23tmp = _mm_srli_si128(w_01_08, 2);
32 w_01_08 = _mm_and_si128(w_01_08, _mm_set_epi16(0, 0, short(0xffff), short(0xffff), 0, 0, 0, 0));
33 w_23tmp = _mm_and_si128(w_23tmp, _mm_set_epi16(short(0xffff), short(0xffff), 0, 0, 0, 0, short(0xffff), short(0xffff)));
34 w_chnk1 = _mm_or_si128(w_01_08, w_23tmp);
35
36 //This gives us:
37 //w_01_08 = [ 0 0 0 0 E F 0 0 ]
38 //w23_tmp = [ B C 0 0 0 0 H 0 ]
39 //w_chnk1 = [ B C 0 0 E F H 0 ]
40
41 w_chnk1 = _mm_shuffle_32(w_chnk1, w_chnk1, _MM_SHUFFLE(1, 3, 2, 0));
42 //w_chnk1 = [ B C E F H 0 0 0 ]
43
44 //w_09_16 = [ I J K L M N O P]
45 __m128i w_4bitt = _mm_and_si128(_mm_slli_si128(w_09_16, 2), _mm_set_epi16(short(0xffff), short(0xffff), 0, 0, 0, 0, short(0xffff), 0));
46 __m128i w_5_tmp = _mm_and_si128(w_09_16, _mm_set_epi16(0, 0, 0, 0, short(0xffff), short(0xffff), 0, 0));
47 //w_4bitt = [0 I 0 0 0 0 N O]
48 //w_5_tmp = [0 0 K L 0 0 0 0]
49
50 w_chnk1 = _mm_or_si128(w_chnk1, _mm_slli_si128(_mm_or_si128(w_4bitt, w_5_tmp), 8));
51 //w_chnk1 = [ B C E F H I K L ]
52
53 //w_17_24 = [ Q R S T U V W X]
54 w_chnk2 = _mm_shuffle_32(_mm_slli_si128(w_17_24, 6), _mm_setzero_si128(), _MM_SHUFFLE(0, 0, 0, 3));
55 //w_chnk2 = [ T U 0 0 0 0 0 0]
56
57 //w_17_24 = [ Q R S T U V W X]
58 //w_4bitt = [ 0 I 0 0 0 0 N O]
59 __m128i w_chnl2 = _mm_shuffle_32(w_4bitt, w_17_24, _MM_SHUFFLE(3, 0, 3, 2));
60 //w_chnl2 = [ 0 0 n o q r w x ]
61
62 w_chnk2 = _mm_or_si128(w_chnk2, w_chnl2);
63 w_chnk2 = _mm_shuffle_32(w_chnk2, w_chnk2, _MM_SHUFFLE(3, 0, 2, 1));
64 //w_chnk2 = [ n o q r t u w x ]
65
66 //__m128i out_16b = _mm_packs_epi16(w_chnk1, w_chnk2);
67 //out_16b = bcefhiklnoqrtuwx
68 //return out_16b;
69 }
70
71 __m128i square_average(const __m128i& top_01_08, const __m128i& top_09_16, const __m128i& top_17_24)
72 {
73
74 //Step 3: the shift-add stage
75 //Perform the shift-add stage
76 __m128i s_top_a = top_01_08, s_top_b = top_09_16, s_top_c = top_17_24;
77 shift_3(s_top_a, s_top_b, s_top_c);
78 s_top_a = _mm_add_epi16(top_01_08, s_top_a);
79 s_top_b = _mm_add_epi16(top_09_16, s_top_b);
80 s_top_c = _mm_add_epi16(top_17_24, s_top_c);
81
82 //Step 4: extract pairs
83 __m128i chunk1, chunk2;
84 extract_pairs(s_top_a, s_top_b, s_top_c, chunk1, chunk2);
85
86 //Finish off: divide by 9 and pack to bytes.
87
88 //Multiply by 65536/9 and right shift by 16
89 //by taking the top word of the integer.
90 //Therby multiplying by 9. This provides enough
91 //precision.
92 chunk1 = _mm_mulhi_epu16(chunk1, _mm_set_epi16(7282, 7282, 7282, 7282, 7282, 7282, 7282, 7282));
93 chunk2 = _mm_mulhi_epu16(chunk2, _mm_set_epi16(7282, 7282, 7282, 7282, 7282, 7282, 7282, 7282));
94 return _mm_packus_epi16(chunk1, chunk2);
95 }
96
97 void weight_top(__m128i& top_01_08, __m128i& top_09_16, __m128i& top_17_24)
98 {
99 top_01_08 = _mm_mullo_epi16(top_01_08, _mm_set_epi16(2, 4, 4, 2, 4, 4, 2, 4));
100 top_09_16 = _mm_mullo_epi16(top_09_16, _mm_set_epi16(4, 4, 2, 4, 4, 2, 4, 4));
101 top_17_24 = _mm_mullo_epi16(top_17_24, _mm_set_epi16(4, 2, 4, 4, 2, 4, 4, 2));
102 }
103
104 void weight_mid(__m128i& mid_01_08, __m128i& mid_09_16, __m128i& mid_17_24)
105 {
106 mid_01_08 = _mm_mullo_epi16(mid_01_08, _mm_set_epi16(1, 2, 2, 1, 2, 2, 1, 2));
107 mid_09_16 = _mm_mullo_epi16(mid_09_16, _mm_set_epi16(2, 2, 1, 2, 2, 1, 2, 2));
108 mid_17_24 = _mm_mullo_epi16(mid_17_24, _mm_set_epi16(2, 1, 2, 2, 1, 2, 2, 1));
109 }
110
111 static __m128i load(const byte* m)
112 {
113 return _mm_loadu_si128((__m128i*)m);
114 }
115
116 static void store(const byte* m, const __m128i& v)
117 {
118 _mm_storeu_si128((__m128i*)m, v);
119 }
120
121 //Reduce a 48x3 pixel strip in to a 32x2 pixel strip
122 //Template on the alignedness of all 5 pointers.
123 void reduce_48(const byte* data0, const byte* data1, const byte* data2, byte* out0, byte* out1)
124 {
125 //The first step is to extract the pixels from the image, and
126 //unpack them from 8 bit ints to 16 bit ints for sufficient
127 //precision.
128 //
129 //Three rows of pixels looks like this:
130 // A B C D E F (top)
131 // G H I J K L (mid)
132 // M N O P Q R (bot)
133 //
134 // After averaging, it will look like this:
135 // a b c d
136 // e f g h
137 //
138 // Where a = (4A + 2B + 2G + H)/9
139 // b = (4C + 2B + 2I + H)/9
140 // c = (4M + 2N + 2G + H)/9
141 // d = (4O + 2N + 2I + H)/9
142 //
143 // Step 1: Weighting
144 // -----------------
145 // The first step is to perform weighting on top and mid:
146 //
147 // top = A B C D E F
148 // *= *=
149 // w = 4 2 4 4 2 4
150 //
151 // top = 4A 2B 4C 4D 2E 4F
152 //
153 // and mid:
154 //
155 // mid = G H I J K L
156 // *= *=
157 // w = 2 1 2 2 1 2
158 //
159 // mid = 2G H 2I 2J K 2L
160 //
161 // Step 2: Sum rows
162 // ----------------
163 //
164 // Doing top += mid gives:
165 //
166 // top = | 4A | 2B | 4C | 4D | 2E | 4F |
167 // += | | | += | | | |
168 // mid = | 2G | H | 2I | 2J | K | 2L |
169 // | | | | | | |
170 // top = | 4A+2G | 2B+H | 4C+2I | 4D+2J | 2E+K | 4F+2L |
171 //
172 // Step 3: Shift and add
173 // ---------------------
174 //
175 // Adding top to a shifted version of itself gives:
176 //
177 // top = | 4A+2G | 2B+H | 4C+2I | 4D+2J | 2E+K | 4F+2L |
178 // shifted_top = | 0 |4A+2G | 2B+H | 4C+2I | 4D+2J | 2E+K |
179 // result = | junk | a | b | junk | c | d |
180 //
181 // The fourth step extracts the pairs a b and c d and so on, ignoring the
182 // junk values. The result is then divided by 9 and packed from words
183 // back in to bytes.
184 //
185 // The algorithm works on rows of 24 pixels, sampling them down to 16
186 // pixels. Since 24 pixels does is not a multiple of 16, the complete
187 // algorithm works on chunks of 48 pixels, reducing the to 32 pixels.
188 //
189 // Furthermore, the third row (bot) is averaged against mid in exactly
190 // the same way as top, producing a second output row. In this way,
191 // 3 rows of 48 pixels are converted in to two rows of 32 pixels.
192
193 //Load 48 consecutive bytes from memory
194 __m128i t_1_16 = load(data0);
195 __m128i t_17_32 = load(data0 + 16);
196 __m128i t_33_48 = load(data0 + 32);
197
198 __m128i m_1_16 = load(data1);
199 __m128i m_17_32 = load(data1 + 16);
200 __m128i m_33_48 = load(data1 + 32);
201
202 __m128i b_1_16 = load(data2);
203 __m128i b_17_32 = load(data2 + 16);
204 __m128i b_33_48 = load(data2 + 32);
205
206 //Unpack the first 24 bytes in to words
207 __m128i top_01_08 = _mm_unpacklo_epi8(t_1_16, _mm_setzero_si128());
208 __m128i top_09_16 = _mm_unpackhi_epi8(t_1_16, _mm_setzero_si128());
209 __m128i top_17_24 = _mm_unpacklo_epi8(t_17_32, _mm_setzero_si128());
210
211 __m128i mid_01_08 = _mm_unpacklo_epi8(m_1_16, _mm_setzero_si128());
212 __m128i mid_09_16 = _mm_unpackhi_epi8(m_1_16, _mm_setzero_si128());
213 __m128i mid_17_24 = _mm_unpacklo_epi8(m_17_32, _mm_setzero_si128());
214
215 //Step 1: Weighting
216 //Perform the weightings 4 2 4 4 2 4 4 2 4 (top and bottom) 2 1 2 2 1 2 (mid)
217
218 weight_top(top_01_08, top_09_16, top_17_24);
219 weight_mid(mid_01_08, mid_09_16, mid_17_24);
220
221 //Step 2: Sum rows
222 //Sum top to mid
223 top_01_08 = _mm_add_epi16(top_01_08, mid_01_08);
224 top_09_16 = _mm_add_epi16(top_09_16, mid_09_16);
225 top_17_24 = _mm_add_epi16(top_17_24, mid_17_24);
226
227 //Steps 3, 4:
228 store(out0, square_average(top_01_08, top_09_16, top_17_24));
229
230 /////////////////////////////////////////////
231 //
232 //Do bot, but in exactly the same way as top.
233 //
234 top_01_08 = _mm_unpacklo_epi8(b_1_16, _mm_setzero_si128());
235 top_09_16 = _mm_unpackhi_epi8(b_1_16, _mm_setzero_si128());
236 top_17_24 = _mm_unpacklo_epi8(b_17_32, _mm_setzero_si128());
237 weight_top(top_01_08, top_09_16, top_17_24);
238 top_01_08 = _mm_add_epi16(top_01_08, mid_01_08);
239 top_09_16 = _mm_add_epi16(top_09_16, mid_09_16);
240 top_17_24 = _mm_add_epi16(top_17_24, mid_17_24);
241 store(out1, square_average(top_01_08, top_09_16, top_17_24));
242
243 //////////////////////////////////////////////////////////////////////////////////////////
244 //
245 // Do the next 24 pixels in the same way as the first 24
246 //
247 top_01_08 = _mm_unpackhi_epi8(t_17_32, _mm_setzero_si128());
248 top_09_16 = _mm_unpacklo_epi8(t_33_48, _mm_setzero_si128());
249 top_17_24 = _mm_unpackhi_epi8(t_33_48, _mm_setzero_si128());
250 mid_01_08 = _mm_unpackhi_epi8(m_17_32, _mm_setzero_si128());
251 mid_09_16 = _mm_unpacklo_epi8(m_33_48, _mm_setzero_si128());
252 mid_17_24 = _mm_unpackhi_epi8(m_33_48, _mm_setzero_si128());
253
254 weight_top(top_01_08, top_09_16, top_17_24);
255 weight_mid(mid_01_08, mid_09_16, mid_17_24);
256 top_01_08 = _mm_add_epi16(top_01_08, mid_01_08);
257 top_09_16 = _mm_add_epi16(top_09_16, mid_09_16);
258 top_17_24 = _mm_add_epi16(top_17_24, mid_17_24);
259
260 store(out0 + 16, square_average(top_01_08, top_09_16, top_17_24));
261
262 top_01_08 = _mm_unpackhi_epi8(b_17_32, _mm_setzero_si128());
263 top_09_16 = _mm_unpacklo_epi8(b_33_48, _mm_setzero_si128());
264 top_17_24 = _mm_unpackhi_epi8(b_33_48, _mm_setzero_si128());
265 weight_top(top_01_08, top_09_16, top_17_24);
266 top_01_08 = _mm_add_epi16(top_01_08, mid_01_08);
267 top_09_16 = _mm_add_epi16(top_09_16, mid_09_16);
268 top_17_24 = _mm_add_epi16(top_17_24, mid_17_24);
269 store(out1 + 16, square_average(top_01_08, top_09_16, top_17_24));
270 }
271
272 }
273
274 void twoThirdsSample(const BasicImage<byte>& in, BasicImage<byte>& out)
275 {
276 if((in.size() / 3 * 2) != out.size())
277 throw Exceptions::Vision::IncompatibleImageSizes(__FUNCTION__);
278
279 for(int yy = 0, y = 0; y < in.size().y - 2; y += 3, yy += 2)
280 {
281 int xx = 0, x = 0;
282 for(; x < in.size().x - 47; x += 48, xx += 32)
283 {
284 //New CPUs are fast with unaligned loads.
285 reduce_48(in[y] + x, in[y + 1] + x, in[y + 2] + x, out[yy] + xx, out[yy + 1] + xx);
286 }
287
288 //Resample any remaining pixels.
289 for(; x < in.size().x - 2; x += 3, xx += 2)
290 {
291 // a b c
292 // d e f
293 // g h i
294
295 int b = in[y][x + 1] * 2;
296 int d = in[y + 1][x] * 2;
297 int f = in[y + 1][x + 2] * 2;
298 int h = in[y + 2][x + 1] * 2;
299 int e = in[y + 1][x + 1];
300
301 out[yy][xx] = static_cast<byte>((in[y][x] * 4 + b + d + e) / 9);
302 out[yy][xx + 1] = static_cast<byte>((in[y][x + 2] * 4 + b + f + e) / 9);
303 out[yy + 1][xx] = static_cast<byte>((in[y + 2][x] * 4 + h + d + e) / 9);
304 out[yy + 1][xx + 1] = static_cast<byte>((in[y + 2][x + 2] * 4 + h + f + e) / 9);
305 }
306 }
307 }
308
309 }