1 /*
2  *  Copyright (c) 2018 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VPX_VPX_DSP_X86_CONVOLVE_SSE2_H_
12 #define VPX_VPX_DSP_X86_CONVOLVE_SSE2_H_
13 
14 #include <emmintrin.h>  // SSE2
15 
16 #include "./vpx_config.h"
17 
18 // Interprets the input register as 16-bit words 7 6 5 4 3 2 1 0, then returns
19 // values at index 2 and 3 to return 3 2 3 2 3 2 3 2 as 16-bit words
extract_quarter_2_epi16_sse2(const __m128i * const reg)20 static INLINE __m128i extract_quarter_2_epi16_sse2(const __m128i *const reg) {
21   __m128i tmp = _mm_unpacklo_epi32(*reg, *reg);
22   return _mm_unpackhi_epi64(tmp, tmp);
23 }
24 
25 // Interprets the input register as 16-bit words 7 6 5 4 3 2 1 0, then returns
26 // values at index 2 and 3 to return 5 4 5 4 5 4 5 4 as 16-bit words.
extract_quarter_3_epi16_sse2(const __m128i * const reg)27 static INLINE __m128i extract_quarter_3_epi16_sse2(const __m128i *const reg) {
28   __m128i tmp = _mm_unpackhi_epi32(*reg, *reg);
29   return _mm_unpacklo_epi64(tmp, tmp);
30 }
31 
32 // Interprets src as 8-bit words, zero extends to form 16-bit words, then
33 // multiplies with ker and add the adjacent results to form 32-bit words.
34 // Finally adds the result from 1 and 2 together.
mm_madd_add_epi8_sse2(const __m128i * const src_1,const __m128i * const src_2,const __m128i * const ker_1,const __m128i * const ker_2)35 static INLINE __m128i mm_madd_add_epi8_sse2(const __m128i *const src_1,
36                                             const __m128i *const src_2,
37                                             const __m128i *const ker_1,
38                                             const __m128i *const ker_2) {
39   const __m128i src_1_half = _mm_unpacklo_epi8(*src_1, _mm_setzero_si128());
40   const __m128i src_2_half = _mm_unpacklo_epi8(*src_2, _mm_setzero_si128());
41   const __m128i madd_1 = _mm_madd_epi16(src_1_half, *ker_1);
42   const __m128i madd_2 = _mm_madd_epi16(src_2_half, *ker_2);
43   return _mm_add_epi32(madd_1, madd_2);
44 }
45 
46 // Interprets src as 16-bit words, then multiplies with ker and add the
47 // adjacent results to form 32-bit words. Finally adds the result from 1 and 2
48 // together.
mm_madd_add_epi16_sse2(const __m128i * const src_1,const __m128i * const src_2,const __m128i * const ker_1,const __m128i * const ker_2)49 static INLINE __m128i mm_madd_add_epi16_sse2(const __m128i *const src_1,
50                                              const __m128i *const src_2,
51                                              const __m128i *const ker_1,
52                                              const __m128i *const ker_2) {
53   const __m128i madd_1 = _mm_madd_epi16(*src_1, *ker_1);
54   const __m128i madd_2 = _mm_madd_epi16(*src_2, *ker_2);
55   return _mm_add_epi32(madd_1, madd_2);
56 }
57 
mm_madd_packs_epi16_sse2(const __m128i * const src_0,const __m128i * const src_1,const __m128i * const ker)58 static INLINE __m128i mm_madd_packs_epi16_sse2(const __m128i *const src_0,
59                                                const __m128i *const src_1,
60                                                const __m128i *const ker) {
61   const __m128i madd_1 = _mm_madd_epi16(*src_0, *ker);
62   const __m128i madd_2 = _mm_madd_epi16(*src_1, *ker);
63   return _mm_packs_epi32(madd_1, madd_2);
64 }
65 
66 // Interleaves src_1 and src_2
mm_zip_epi32_sse2(const __m128i * const src_1,const __m128i * const src_2)67 static INLINE __m128i mm_zip_epi32_sse2(const __m128i *const src_1,
68                                         const __m128i *const src_2) {
69   const __m128i tmp_1 = _mm_unpacklo_epi32(*src_1, *src_2);
70   const __m128i tmp_2 = _mm_unpackhi_epi32(*src_1, *src_2);
71   return _mm_packs_epi32(tmp_1, tmp_2);
72 }
73 
mm_round_epi32_sse2(const __m128i * const src,const __m128i * const half_depth,const int depth)74 static INLINE __m128i mm_round_epi32_sse2(const __m128i *const src,
75                                           const __m128i *const half_depth,
76                                           const int depth) {
77   const __m128i nearest_src = _mm_add_epi32(*src, *half_depth);
78   return _mm_srai_epi32(nearest_src, depth);
79 }
80 
mm_round_epi16_sse2(const __m128i * const src,const __m128i * const half_depth,const int depth)81 static INLINE __m128i mm_round_epi16_sse2(const __m128i *const src,
82                                           const __m128i *const half_depth,
83                                           const int depth) {
84   const __m128i nearest_src = _mm_adds_epi16(*src, *half_depth);
85   return _mm_srai_epi16(nearest_src, depth);
86 }
87 
88 #endif  // VPX_VPX_DSP_X86_CONVOLVE_SSE2_H_
89