1/*
2 *  Copyright (c) 2014 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#include <arm_neon.h>
12
13static const int16_t cospi8sqrt2minus1 = 20091;
14static const int16_t sinpi8sqrt2 = 17734;
15// because the lowest bit in 0x8a8c is 0, we can pre-shift this
16
17void idct_dequant_full_2x_neon(int16_t *q, int16_t *dq, unsigned char *dst,
18                               int stride) {
19  unsigned char *dst0, *dst1;
20  int32x2_t d28, d29, d30, d31;
21  int16x8_t q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11;
22  int16x8_t qEmpty = vdupq_n_s16(0);
23  int32x4x2_t q2tmp0, q2tmp1;
24  int16x8x2_t q2tmp2, q2tmp3;
25  int16x4_t dLow0, dLow1, dHigh0, dHigh1;
26
27  d28 = d29 = d30 = d31 = vdup_n_s32(0);
28
29  // load dq
30  q0 = vld1q_s16(dq);
31  dq += 8;
32  q1 = vld1q_s16(dq);
33
34  // load q
35  q2 = vld1q_s16(q);
36  vst1q_s16(q, qEmpty);
37  q += 8;
38  q3 = vld1q_s16(q);
39  vst1q_s16(q, qEmpty);
40  q += 8;
41  q4 = vld1q_s16(q);
42  vst1q_s16(q, qEmpty);
43  q += 8;
44  q5 = vld1q_s16(q);
45  vst1q_s16(q, qEmpty);
46
47  // load src from dst
48  dst0 = dst;
49  dst1 = dst + 4;
50  d28 = vld1_lane_s32((const int32_t *)dst0, d28, 0);
51  dst0 += stride;
52  d28 = vld1_lane_s32((const int32_t *)dst1, d28, 1);
53  dst1 += stride;
54  d29 = vld1_lane_s32((const int32_t *)dst0, d29, 0);
55  dst0 += stride;
56  d29 = vld1_lane_s32((const int32_t *)dst1, d29, 1);
57  dst1 += stride;
58
59  d30 = vld1_lane_s32((const int32_t *)dst0, d30, 0);
60  dst0 += stride;
61  d30 = vld1_lane_s32((const int32_t *)dst1, d30, 1);
62  dst1 += stride;
63  d31 = vld1_lane_s32((const int32_t *)dst0, d31, 0);
64  d31 = vld1_lane_s32((const int32_t *)dst1, d31, 1);
65
66  q2 = vmulq_s16(q2, q0);
67  q3 = vmulq_s16(q3, q1);
68  q4 = vmulq_s16(q4, q0);
69  q5 = vmulq_s16(q5, q1);
70
71  // vswp
72  dLow0 = vget_low_s16(q2);
73  dHigh0 = vget_high_s16(q2);
74  dLow1 = vget_low_s16(q4);
75  dHigh1 = vget_high_s16(q4);
76  q2 = vcombine_s16(dLow0, dLow1);
77  q4 = vcombine_s16(dHigh0, dHigh1);
78
79  dLow0 = vget_low_s16(q3);
80  dHigh0 = vget_high_s16(q3);
81  dLow1 = vget_low_s16(q5);
82  dHigh1 = vget_high_s16(q5);
83  q3 = vcombine_s16(dLow0, dLow1);
84  q5 = vcombine_s16(dHigh0, dHigh1);
85
86  q6 = vqdmulhq_n_s16(q4, sinpi8sqrt2);
87  q7 = vqdmulhq_n_s16(q5, sinpi8sqrt2);
88  q8 = vqdmulhq_n_s16(q4, cospi8sqrt2minus1);
89  q9 = vqdmulhq_n_s16(q5, cospi8sqrt2minus1);
90
91  q10 = vqaddq_s16(q2, q3);
92  q11 = vqsubq_s16(q2, q3);
93
94  q8 = vshrq_n_s16(q8, 1);
95  q9 = vshrq_n_s16(q9, 1);
96
97  q4 = vqaddq_s16(q4, q8);
98  q5 = vqaddq_s16(q5, q9);
99
100  q2 = vqsubq_s16(q6, q5);
101  q3 = vqaddq_s16(q7, q4);
102
103  q4 = vqaddq_s16(q10, q3);
104  q5 = vqaddq_s16(q11, q2);
105  q6 = vqsubq_s16(q11, q2);
106  q7 = vqsubq_s16(q10, q3);
107
108  q2tmp0 = vtrnq_s32(vreinterpretq_s32_s16(q4), vreinterpretq_s32_s16(q6));
109  q2tmp1 = vtrnq_s32(vreinterpretq_s32_s16(q5), vreinterpretq_s32_s16(q7));
110  q2tmp2 = vtrnq_s16(vreinterpretq_s16_s32(q2tmp0.val[0]),
111                     vreinterpretq_s16_s32(q2tmp1.val[0]));
112  q2tmp3 = vtrnq_s16(vreinterpretq_s16_s32(q2tmp0.val[1]),
113                     vreinterpretq_s16_s32(q2tmp1.val[1]));
114
115  // loop 2
116  q8 = vqdmulhq_n_s16(q2tmp2.val[1], sinpi8sqrt2);
117  q9 = vqdmulhq_n_s16(q2tmp3.val[1], sinpi8sqrt2);
118  q10 = vqdmulhq_n_s16(q2tmp2.val[1], cospi8sqrt2minus1);
119  q11 = vqdmulhq_n_s16(q2tmp3.val[1], cospi8sqrt2minus1);
120
121  q2 = vqaddq_s16(q2tmp2.val[0], q2tmp3.val[0]);
122  q3 = vqsubq_s16(q2tmp2.val[0], q2tmp3.val[0]);
123
124  q10 = vshrq_n_s16(q10, 1);
125  q11 = vshrq_n_s16(q11, 1);
126
127  q10 = vqaddq_s16(q2tmp2.val[1], q10);
128  q11 = vqaddq_s16(q2tmp3.val[1], q11);
129
130  q8 = vqsubq_s16(q8, q11);
131  q9 = vqaddq_s16(q9, q10);
132
133  q4 = vqaddq_s16(q2, q9);
134  q5 = vqaddq_s16(q3, q8);
135  q6 = vqsubq_s16(q3, q8);
136  q7 = vqsubq_s16(q2, q9);
137
138  q4 = vrshrq_n_s16(q4, 3);
139  q5 = vrshrq_n_s16(q5, 3);
140  q6 = vrshrq_n_s16(q6, 3);
141  q7 = vrshrq_n_s16(q7, 3);
142
143  q2tmp0 = vtrnq_s32(vreinterpretq_s32_s16(q4), vreinterpretq_s32_s16(q6));
144  q2tmp1 = vtrnq_s32(vreinterpretq_s32_s16(q5), vreinterpretq_s32_s16(q7));
145  q2tmp2 = vtrnq_s16(vreinterpretq_s16_s32(q2tmp0.val[0]),
146                     vreinterpretq_s16_s32(q2tmp1.val[0]));
147  q2tmp3 = vtrnq_s16(vreinterpretq_s16_s32(q2tmp0.val[1]),
148                     vreinterpretq_s16_s32(q2tmp1.val[1]));
149
150  q4 = vreinterpretq_s16_u16(
151      vaddw_u8(vreinterpretq_u16_s16(q2tmp2.val[0]), vreinterpret_u8_s32(d28)));
152  q5 = vreinterpretq_s16_u16(
153      vaddw_u8(vreinterpretq_u16_s16(q2tmp2.val[1]), vreinterpret_u8_s32(d29)));
154  q6 = vreinterpretq_s16_u16(
155      vaddw_u8(vreinterpretq_u16_s16(q2tmp3.val[0]), vreinterpret_u8_s32(d30)));
156  q7 = vreinterpretq_s16_u16(
157      vaddw_u8(vreinterpretq_u16_s16(q2tmp3.val[1]), vreinterpret_u8_s32(d31)));
158
159  d28 = vreinterpret_s32_u8(vqmovun_s16(q4));
160  d29 = vreinterpret_s32_u8(vqmovun_s16(q5));
161  d30 = vreinterpret_s32_u8(vqmovun_s16(q6));
162  d31 = vreinterpret_s32_u8(vqmovun_s16(q7));
163
164  dst0 = dst;
165  dst1 = dst + 4;
166  vst1_lane_s32((int32_t *)dst0, d28, 0);
167  dst0 += stride;
168  vst1_lane_s32((int32_t *)dst1, d28, 1);
169  dst1 += stride;
170  vst1_lane_s32((int32_t *)dst0, d29, 0);
171  dst0 += stride;
172  vst1_lane_s32((int32_t *)dst1, d29, 1);
173  dst1 += stride;
174
175  vst1_lane_s32((int32_t *)dst0, d30, 0);
176  dst0 += stride;
177  vst1_lane_s32((int32_t *)dst1, d30, 1);
178  dst1 += stride;
179  vst1_lane_s32((int32_t *)dst0, d31, 0);
180  vst1_lane_s32((int32_t *)dst1, d31, 1);
181  return;
182}
183