blob: 79dd1f07e3c20cb6adf4c6b5bc94131410c8f01c [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
26#ifdef __aarch64__
27
28template <>
29inline void MergeResults<12, 8>(int32_t *out, const int32_t *in, const int ldout, const int y0, const int ymax, const int x0, const int xmax, const int32_t alpha, const int32_t beta)
30{
31 const int32_t *inptr = in;
32 prefetch_6x(inptr);
33 prefetch_6x(inptr + 96);
34
35 int32x4_t alpha_value = vdupq_n_s32(alpha);
36 int32x4_t beta_value = vdupq_n_s32(beta);
37
38 for(int y = y0; y < ymax; y += 8)
39 {
40 int32_t *outptr0 = out + (y * ldout) + x0;
41 int32_t *outptr1 = outptr0 + ldout;
42 int32_t *outptr2 = outptr1 + ldout;
43 int32_t *outptr3 = outptr2 + ldout;
44 int32_t *outptr4 = outptr3 + ldout;
45 int32_t *outptr5 = outptr4 + ldout;
46 int32_t *outptr6 = outptr5 + ldout;
47 int32_t *outptr7 = outptr6 + ldout;
48
49 prefetch_2x(outptr0);
50 prefetch_2x(outptr1);
51 prefetch_2x(outptr2);
52 prefetch_2x(outptr3);
53 prefetch_2x(outptr4);
54 prefetch_2x(outptr5);
55 prefetch_2x(outptr6);
56 prefetch_2x(outptr7);
57
58 for(int i = x0; i < xmax; i += 12)
59 {
60 int32_t dummyres[12];
61
62 /* Make sure we throw away results if Y isn't a multiple of 8.
63 * We do this by pointing the result pointer at a dummy buffer
64 * we later discard. */
65 if((y + 7) >= ymax)
66 {
67 switch((y + 7) - ymax)
68 {
69 case 6:
70 outptr1 = dummyres;
71 case 5:
72 outptr2 = dummyres;
73 case 4:
74 outptr3 = dummyres;
75 case 3:
76 outptr4 = dummyres;
77 case 2:
78 outptr5 = dummyres;
79 case 1:
80 outptr6 = dummyres;
81 case 0:
82 outptr7 = dummyres;
83 break;
84
85 default:
86 UNREACHABLE("Impossible.");
87 }
88 }
89
90 /* For ragged X, manually copy over the valid results. */
91 if((i + 11) >= xmax)
92 {
93 for(int xi = 0; xi < 12; xi++)
94 {
95 if((i + xi) < xmax)
96 {
97 *outptr0 = (alpha * inptr[xi]) + (*outptr0 * beta);
98 outptr0++;
99 *outptr1 = (alpha * inptr[xi + 12]) + (*outptr1 * beta);
100 outptr1++;
101 *outptr2 = (alpha * inptr[xi + 24]) + (*outptr2 * beta);
102 outptr2++;
103 *outptr3 = (alpha * inptr[xi + 36]) + (*outptr3 * beta);
104 outptr3++;
105 *outptr4 = (alpha * inptr[xi + 48]) + (*outptr4 * beta);
106 outptr4++;
107 *outptr5 = (alpha * inptr[xi + 60]) + (*outptr5 * beta);
108 outptr5++;
109 *outptr6 = (alpha * inptr[xi + 72]) + (*outptr6 * beta);
110 outptr6++;
111 *outptr7 = (alpha * inptr[xi + 84]) + (*outptr7 * beta);
112 outptr7++;
113 }
114 }
115 inptr += 96;
116 }
117 else
118 {
119 /* Optimized routine to copy an entire block */
120 __asm __volatile(
121 // Row 0
122 ASM_PREFETCH("[%x[outptr1], #192]")
123 "ldr q3, [%x[outptr0]]\n"
124 "ldr q4, [%x[outptr0], #0x10]\n"
125 "ldr q5, [%x[outptr0], #0x20]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100126 "mul v3.4s, v3.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000127 "ldr q6, [%x[inptr]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100128 "mul v4.4s, v4.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000129 "ldr q7, [%x[inptr], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100130 "mul v5.4s, v5.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000131 "ldr q8, [%x[inptr], #0x20]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100132 "mla v3.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000133 "ldr q0, [%x[outptr1]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100134 "mla v4.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000135 "ldr q1, [%x[outptr1], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100136 "mla v5.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000137 "ldr q2, [%x[outptr1], #0x20]\n"
138
139 // Row 1
140 ASM_PREFETCH("[%x[outptr2], #192]")
David Mansell4d1e8a22018-05-15 15:24:39 +0100141 "mul v0.4s, v0.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000142 "ldr q6, [%x[inptr], #0x30]\n"
143 "str q3, [%x[outptr0]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100144 "mul v1.4s, v1.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000145 "ldr q7, [%x[inptr], #0x40]\n"
146 "str q4, [%x[outptr0]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100147 "mul v2.4s, v2.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000148 "ldr q8, [%x[inptr], #0x50]\n"
149 "str q5, [%x[outptr0]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100150 "mla v0.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000151 "ldr q3, [%x[outptr2]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100152 "mla v1.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000153 "ldr q4, [%x[outptr2], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100154 "mla v2.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000155 "ldr q5, [%x[outptr2], #0x20]\n"
156
157 // Row 2
158 ASM_PREFETCH("[%x[outptr3], #192]")
David Mansell4d1e8a22018-05-15 15:24:39 +0100159 "mul v3.4s, v3.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000160 "ldr q6, [%x[inptr], #0x60]\n"
161 "str q0, [%x[outptr1]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100162 "mul v4.4s, v4.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000163 "ldr q7, [%x[inptr], #0x70]\n"
164 "str q1, [%x[outptr1]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100165 "mul v5.4s, v5.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000166 "ldr q8, [%x[inptr], #0x80]\n"
167 "str q2, [%x[outptr1]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100168 "mla v3.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000169 "ldr q0, [%x[outptr3]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100170 "mla v4.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000171 "ldr q1, [%x[outptr3], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100172 "mla v5.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000173 "ldr q2, [%x[outptr3], #0x20]\n"
174
175 // Row 3
176 ASM_PREFETCH("[%x[outptr4], #192]")
David Mansell4d1e8a22018-05-15 15:24:39 +0100177 "mul v0.4s, v0.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000178 "ldr q6, [%x[inptr], #0x90]\n"
179 "str q3, [%x[outptr2]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100180 "mul v1.4s, v1.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000181 "ldr q7, [%x[inptr], #0xa0]\n"
182 "str q4, [%x[outptr2]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100183 "mul v2.4s, v2.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000184 "ldr q8, [%x[inptr], #0xb0]\n"
185 "str q5, [%x[outptr2]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100186 "mla v0.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000187 "ldr q3, [%x[outptr4]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100188 "mla v1.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000189 "ldr q4, [%x[outptr4], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100190 "mla v2.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000191 "ldr q5, [%x[outptr4], #0x20]\n"
192
193 // Row 4
194 ASM_PREFETCH("[%x[outptr5], #192]")
David Mansell4d1e8a22018-05-15 15:24:39 +0100195 "mul v3.4s, v3.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000196 "ldr q6, [%x[inptr], #0xc0]\n"
197 "str q0, [%x[outptr3]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100198 "mul v4.4s, v4.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000199 "ldr q7, [%x[inptr], #0xd0]\n"
200 "str q1, [%x[outptr3]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100201 "mul v5.4s, v5.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000202 "ldr q8, [%x[inptr], #0xe0]\n"
203 "str q2, [%x[outptr3]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100204 "mla v3.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000205 "ldr q0, [%x[outptr5]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100206 "mla v4.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000207 "ldr q1, [%x[outptr5], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100208 "mla v5.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000209 "ldr q2, [%x[outptr5], #0x20]\n"
210
211 // Row 5
212 ASM_PREFETCH("[%x[outptr6], #192]")
David Mansell4d1e8a22018-05-15 15:24:39 +0100213 "mul v0.4s, v0.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000214 "ldr q6, [%x[inptr], #0xf0]\n"
215 "str q3, [%x[outptr4]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100216 "mul v1.4s, v1.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000217 "ldr q7, [%x[inptr], #0x100]\n"
218 "str q4, [%x[outptr4]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100219 "mul v2.4s, v2.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000220 "ldr q8, [%x[inptr], #0x110]\n"
221 "str q5, [%x[outptr4]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100222 "mla v0.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000223 "ldr q3, [%x[outptr6]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100224 "mla v1.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000225 "ldr q4, [%x[outptr6], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100226 "mla v2.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000227 "ldr q5, [%x[outptr6], #0x20]\n"
228
229 // Row 6
230 ASM_PREFETCH("[%x[outptr7], #192]")
David Mansell4d1e8a22018-05-15 15:24:39 +0100231 "mul v3.4s, v3.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000232 "ldr q6, [%x[inptr], #0x120]\n"
233 "str q0, [%x[outptr5]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100234 "mul v4.4s, v4.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000235 "ldr q7, [%x[inptr], #0x130]\n"
236 "str q1, [%x[outptr5]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100237 "mul v5.4s, v5.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000238 "ldr q8, [%x[inptr], #0x140]\n"
239 "str q2, [%x[outptr5]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100240 "mla v3.4s, v6.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000241 "ldr q0, [%x[outptr7]]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100242 "mla v4.4s, v7.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000243 "ldr q1, [%x[outptr7], #0x10]\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100244 "mla v5.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000245 "ldr q2, [%x[outptr7], #0x20]\n"
246
247 // Row 7
David Mansell4d1e8a22018-05-15 15:24:39 +0100248 "mul v0.4s, v0.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000249 "ldr q6, [%x[inptr], #0x150]\n"
250 "str q3, [%x[outptr6]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100251 "mul v1.4s, v1.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000252 "ldr q7, [%x[inptr], #0x160]\n"
253 "str q4, [%x[outptr6]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100254 "mul v2.4s, v2.4s, %[beta_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000255 "ldr q8, [%x[inptr], #0x170]\n"
256 "str q5, [%x[outptr6]], #0x10\n"
David Mansell4d1e8a22018-05-15 15:24:39 +0100257 "mla v0.4s, v6.4s, %[alpha_value].4s\n"
258 "mla v1.4s, v7.4s, %[alpha_value].4s\n"
259 "mla v2.4s, v8.4s, %[alpha_value].4s\n"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000260 "str q0, [%x[outptr7]], #0x10\n"
261 "str q1, [%x[outptr7]], #0x10\n"
262 "str q2, [%x[outptr7]], #0x10\n"
263
264 "add %x[inptr], %x[inptr], #0x180\n"
265 : [outptr0] "+r"(outptr0),
266 [outptr1] "+r"(outptr1),
267 [outptr2] "+r"(outptr2),
268 [outptr3] "+r"(outptr3),
269 [outptr4] "+r"(outptr4),
270 [outptr5] "+r"(outptr5),
271 [outptr6] "+r"(outptr6),
272 [outptr7] "+r"(outptr7),
273 [inptr] "+r"(inptr)
274 : [alpha_value] "w"(alpha_value),
275 [beta_value] "w"(beta_value)
276 : "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8");
277 }
278 }
279 }
280}
281
282template <>
283inline void MergeResults<12, 8>(uint32_t *out, const uint32_t *in, const int ldout, const int y0, const int ymax, const int x0, const int xmax, const uint32_t alpha, const uint32_t beta)
284{
285 // Since the above code uses only MUL and MLA instructions discard the "unsignedness" and proceed safely.
286 MergeResults<12, 8>(reinterpret_cast<int32_t *>(out), reinterpret_cast<const int32_t *>(in), ldout, y0, ymax, x0, xmax, static_cast<const int32_t>(alpha), static_cast<const int32_t>(beta));
287}
288
289#endif // __aarch64__