| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Author: Siarhei Siamashka (siarhei.siamashka@nokia.com) |
| 24 | */ |
| 25 |
| 26 | /* |
| 27 | * This file contains implementations of NEON optimized pixel processing |
| 28 | * functions. There is no full and detailed tutorial, but some functions |
| 29 | * (those which are exposing some new or interesting features) are |
| 30 | * extensively commented and can be used as examples. |
| 31 | * |
| 32 | * You may want to have a look at the comments for following functions: |
| 33 | * - pixman_composite_over_8888_0565_asm_neon |
| 34 | * - pixman_composite_over_n_8_0565_asm_neon |
| 35 | */ |
| 36 |
| 37 | /* Prevent the stack from becoming executable for no reason... */ |
| 38 | #if defined(__linux__) && defined(__ELF__) |
| 39 | .section .note.GNU-stack,"",%progbits |
| 40 | #endif |
| 41 |
| 42 | .text |
| 43 | .arch armv8-a |
| 44 | .altmacro |
| 45 | .p2align 2 |
| 46 |
| 47 | /* Supplementary macro for setting function attributes */ |
| 48 | .macro pixman_asm_function fname |
| 49 | .func fname |
| 50 | .global fname |
| 51 | #ifdef __ELF__ |
| 52 | .hidden fname |
| 53 | .type fname, %function |
| 54 | #endif |
| 55 | fname: |
| 56 | .endm |
| 57 |
| 58 | #include "pixman-arma64-neon-asm.h" |
| 59 |
| 60 | /* Global configuration options and preferences */ |
| 61 |
| 62 | /* |
| 63 | * The code can optionally make use of unaligned memory accesses to improve |
| 64 | * performance of handling leading/trailing pixels for each scanline. |
| 65 | * Configuration variable RESPECT_STRICT_ALIGNMENT can be set to 0 for |
| 66 | * example in linux if unaligned memory accesses are not configured to |
| 67 | * generate.exceptions. |
| 68 | */ |
| 69 | .set RESPECT_STRICT_ALIGNMENT, 1 |
| 70 |
| 71 | /* |
| 72 | * Set default prefetch type. There is a choice between the following options: |
| 73 | * |
| 74 | * PREFETCH_TYPE_NONE (may be useful for the ARM cores where PLD is set to work |
| 75 | * as NOP to workaround some HW bugs or for whatever other reason) |
| 76 | * |
| 77 | * PREFETCH_TYPE_SIMPLE (may be useful for simple single-issue ARM cores where |
| 78 | * advanced prefetch intruduces heavy overhead) |
| 79 | * |
| 80 | * PREFETCH_TYPE_ADVANCED (useful for superscalar cores such as ARM Cortex-A8 |
| 81 | * which can run ARM and NEON instructions simultaneously so that extra ARM |
| 82 | * instructions do not add (many) extra cycles, but improve prefetch efficiency) |
| 83 | * |
| 84 | * Note: some types of function can't support advanced prefetch and fallback |
| 85 | * to simple one (those which handle 24bpp pixels) |
| 86 | */ |
| 87 | .set PREFETCH_TYPE_DEFAULT, PREFETCH_TYPE_ADVANCED |
| 88 |
| 89 | /* Prefetch distance in pixels for simple prefetch */ |
| 90 | .set PREFETCH_DISTANCE_SIMPLE, 64 |
| 91 |
| 92 | /* |
| 93 | * Implementation of pixman_composite_over_8888_0565_asm_neon |
| 94 | * |
| 95 | * This function takes a8r8g8b8 source buffer, r5g6b5 destination buffer and |
| 96 | * performs OVER compositing operation. Function fast_composite_over_8888_0565 |
| 97 | * from pixman-fast-path.c does the same in C and can be used as a reference. |
| 98 | * |
| 99 | * First we need to have some NEON assembly code which can do the actual |
| 100 | * operation on the pixels and provide it to the template macro. |
| 101 | * |
| 102 | * Template macro quite conveniently takes care of emitting all the necessary |
| 103 | * code for memory reading and writing (including quite tricky cases of |
| 104 | * handling unaligned leading/trailing pixels), so we only need to deal with |
| 105 | * the data in NEON registers. |
| 106 | * |
| 107 | * NEON registers allocation in general is recommented to be the following: |
| 108 | * v0, v1, v2, v3 - contain loaded source pixel data |
| 109 | * v4, v5, v6, v7 - contain loaded destination pixels (if they are needed) |
| 110 | * v24, v25, v26, v27 - contain loading mask pixel data (if mask is used) |
| 111 | * v28, v29, v30, v31 - place for storing the result (destination pixels) |
| 112 | * |
| 113 | * As can be seen above, four 64-bit NEON registers are used for keeping |
| 114 | * intermediate pixel data and up to 8 pixels can be processed in one step |
| 115 | * for 32bpp formats (16 pixels for 16bpp, 32 pixels for 8bpp). |
| 116 | * |
| 117 | * This particular function uses the following registers allocation: |
| 118 | * v0, v1, v2, v3 - contain loaded source pixel data |
| 119 | * v4, v5 - contain loaded destination pixels (they are needed) |
| 120 | * v28, v29 - place for storing the result (destination pixels) |
| 121 | */ |
| 122 |
| 123 | /* |
| 124 | * Step one. We need to have some code to do some arithmetics on pixel data. |
| 125 | * This is implemented as a pair of macros: '*_head' and '*_tail'. When used |
| 126 | * back-to-back, they take pixel data from {v0, v1, v2, v3} and {v4, v5}, |
| 127 | * perform all the needed calculations and write the result to {v28, v29}. |
| 128 | * The rationale for having two macros and not just one will be explained |
| 129 | * later. In practice, any single monolitic function which does the work can |
| 130 | * be split into two parts in any arbitrary way without affecting correctness. |
| 131 | * |
| 132 | * There is one special trick here too. Common template macro can optionally |
| 133 | * make our life a bit easier by doing R, G, B, A color components |
| 134 | * deinterleaving for 32bpp pixel formats (and this feature is used in |
| 135 | * 'pixman_composite_over_8888_0565_asm_neon' function). So it means that |
| 136 | * instead of having 8 packed pixels in {v0, v1, v2, v3} registers, we |
| 137 | * actually use v0 register for blue channel (a vector of eight 8-bit |
| 138 | * values), v1 register for green, v2 for red and v3 for alpha. This |
| 139 | * simple conversion can be also done with a few NEON instructions: |
| 140 | * |
| 141 | * Packed to planar conversion: // vuzp8 is a wrapper macro |
| 142 | * vuzp8 v0, v1 |
| 143 | * vuzp8 v2, v3 |
| 144 | * vuzp8 v1, v3 |
| 145 | * vuzp8 v0, v2 |
| 146 | * |
| 147 | * Planar to packed conversion: // vzip8 is a wrapper macro |
| 148 | * vzip8 v0, v2 |
| 149 | * vzip8 v1, v3 |
| 150 | * vzip8 v2, v3 |
| 151 | * vzip8 v0, v1 |
| 152 | * |
| 153 | * But pixel can be loaded directly in planar format using LD4 / b NEON |
| 154 | * instruction. It is 1 cycle slower than LD1 / s, so this is not always |
| 155 | * desirable, that's why deinterleaving is optional. |
| 156 | * |
| 157 | * But anyway, here is the code: |
| 158 | */ |
| 159 |
| 160 | .macro pixman_composite_out_reverse_8888_8888_process_pixblock_head |
| 161 | mvn v24.8b, v3.8b /* get inverted alpha */ |
| 162 | /* do alpha blending */ |
| 163 | umull v8.8h, v24.8b, v4.8b |
| 164 | umull v9.8h, v24.8b, v5.8b |
| 165 | umull v10.8h, v24.8b, v6.8b |
| 166 | umull v11.8h, v24.8b, v7.8b |
| 167 | .endm |
| 168 |
| 169 | .macro pixman_composite_out_reverse_8888_8888_process_pixblock_tail |
| 170 | urshr v14.8h, v8.8h, #8 |
| 171 | urshr v15.8h, v9.8h, #8 |
| 172 | urshr v16.8h, v10.8h, #8 |
| 173 | urshr v17.8h, v11.8h, #8 |
| 174 | raddhn v28.8b, v14.8h, v8.8h |
| 175 | raddhn v29.8b, v15.8h, v9.8h |
| 176 | raddhn v30.8b, v16.8h, v10.8h |
| 177 | raddhn v31.8b, v17.8h, v11.8h |
| 178 | .endm |
| 179 |
| 180 | /******************************************************************************/ |
| 181 |
| 182 | .macro pixman_composite_over_8888_8888_process_pixblock_head |
| 183 | pixman_composite_out_reverse_8888_8888_process_pixblock_head |
| 184 | .endm |
| 185 |
| 186 | .macro pixman_composite_over_8888_8888_process_pixblock_tail |
| 187 | pixman_composite_out_reverse_8888_8888_process_pixblock_tail |
| 188 | uqadd v28.8b, v0.8b, v28.8b |
| 189 | uqadd v29.8b, v1.8b, v29.8b |
| 190 | uqadd v30.8b, v2.8b, v30.8b |
| 191 | uqadd v31.8b, v3.8b, v31.8b |
| 192 | .endm |
| 193 |
| 194 | .macro pixman_composite_over_8888_8888_process_pixblock_tail_head |
| 195 | ld4 {v4.8b, v5.8b, v6.8b, v7.8b}, [DST_R], #32 |
| 196 | urshr v14.8h, v8.8h, #8 |
| 197 | PF add PF_X, PF_X, #8 |
| 198 | PF tst PF_CTL, #0xF |
| 199 | urshr v15.8h, v9.8h, #8 |
| 200 | urshr v16.8h, v10.8h, #8 |
| 201 | urshr v17.8h, v11.8h, #8 |
| 202 | PF beq 10f |
| 203 | PF add PF_X, PF_X, #8 |
| 204 | PF sub PF_CTL, PF_CTL, #1 |
| 205 | 10: |
| 206 | raddhn v28.8b, v14.8h, v8.8h |
| 207 | raddhn v29.8b, v15.8h, v9.8h |
| 208 | PF cmp PF_X, ORIG_W |
| 209 | raddhn v30.8b, v16.8h, v10.8h |
| 210 | raddhn v31.8b, v17.8h, v11.8h |
| 211 | uqadd v28.8b, v0.8b, v28.8b |
| 212 | uqadd v29.8b, v1.8b, v29.8b |
| 213 | uqadd v30.8b, v2.8b, v30.8b |
| 214 | uqadd v31.8b, v3.8b, v31.8b |
| 215 | fetch_src_pixblock |
| 216 | PF lsl DUMMY, PF_X, #src_bpp_shift |
| 217 | PF prfm PREFETCH_MODE, [PF_SRC, DUMMY] |
| 218 | mvn v22.8b, v3.8b |
| 219 | PF lsl DUMMY, PF_X, #dst_bpp_shift |
| 220 | PF prfm PREFETCH_MODE, [PF_DST, DUMMY] |
| 221 | st4 {v28.8b, v29.8b, v30.8b, v31.8b}, [DST_W], #32 |
| 222 | PF ble 10f |
| 223 | PF sub PF_X, PF_X, ORIG_W |
| 224 | 10: |
| 225 | umull v8.8h, v22.8b, v4.8b |
| 226 | PF ble 10f |
| 227 | PF subs PF_CTL, PF_CTL, #0x10 |
| 228 | 10: |
| 229 | umull v9.8h, v22.8b, v5.8b |
| 230 | PF ble 10f |
| 231 | PF lsl DUMMY, SRC_STRIDE, #src_bpp_shift |
| 232 | PF ldrsb DUMMY, [PF_SRC, DUMMY] |
| 233 | PF add PF_SRC, PF_SRC, #1 |
| 234 | 10: |
| 235 | umull v10.8h, v22.8b, v6.8b |
| 236 | PF ble 10f |
| 237 | PF lsl DUMMY, DST_STRIDE, #dst_bpp_shift |
| 238 | PF ldrsb DUMMY, [PF_DST, DUMMY] |
| 239 | PF add PF_DST, PF_DST, #1 |
| 240 | 10: |
| 241 | umull v11.8h, v22.8b, v7.8b |
| 242 | .endm |
| 243 |
| 244 | generate_composite_function \ |
| 245 | pixman_composite_over_8888_8888_asm_neon, 32, 0, 32, \ |
| 246 | FLAG_DST_READWRITE | FLAG_DEINTERLEAVE_32BPP, \ |
| 247 | 8, /* number of pixels, processed in a single block */ \ |
| 248 | 5, /* prefetch distance */ \ |
| 249 | default_init, \ |
| 250 | default_cleanup, \ |
| 251 | pixman_composite_over_8888_8888_process_pixblock_head, \ |
| 252 | pixman_composite_over_8888_8888_process_pixblock_tail, \ |
| 253 | pixman_composite_over_8888_8888_process_pixblock_tail_head |
| 254 |
| 255 | generate_composite_function_single_scanline \ |
| 256 | pixman_composite_scanline_over_asm_neon, 32, 0, 32, \ |
| 257 | FLAG_DST_READWRITE | FLAG_DEINTERLEAVE_32BPP, \ |
| 258 | 8, /* number of pixels, processed in a single block */ \ |
| 259 | default_init, \ |
| 260 | default_cleanup, \ |
| 261 | pixman_composite_over_8888_8888_process_pixblock_head, \ |
| 262 | pixman_composite_over_8888_8888_process_pixblock_tail, \ |
| 263 | pixman_composite_over_8888_8888_process_pixblock_tail_head |
| 264 |
| 265 | /******************************************************************************/ |
| 266 |
| 267 | .macro pixman_composite_over_n_8888_process_pixblock_head |
| 268 | /* deinterleaved source pixels in {v0, v1, v2, v3} */ |
| 269 | /* inverted alpha in {v24} */ |
| 270 | /* destination pixels in {v4, v5, v6, v7} */ |
| 271 | umull v8.8h, v24.8b, v4.8b |
| 272 | umull v9.8h, v24.8b, v5.8b |
| 273 | umull v10.8h, v24.8b, v6.8b |
| 274 | umull v11.8h, v24.8b, v7.8b |
| 275 | .endm |
| 276 |
| 277 | .macro pixman_composite_over_n_8888_process_pixblock_tail |
| 278 | urshr v14.8h, v8.8h, #8 |
| 279 | urshr v15.8h, v9.8h, #8 |
| 280 | urshr v16.8h, v10.8h, #8 |
| 281 | urshr v17.8h, v11.8h, #8 |
| 282 | raddhn v28.8b, v14.8h, v8.8h |
| 283 | raddhn v29.8b, v15.8h, v9.8h |
| 284 | raddhn v30.8b, v16.8h, v10.8h |
| 285 | raddhn v31.8b, v17.8h, v11.8h |
| 286 | uqadd v28.8b, v0.8b, v28.8b |
| 287 | uqadd v29.8b, v1.8b, v29.8b |
| 288 | uqadd v30.8b, v2.8b, v30.8b |
| 289 | uqadd v31.8b, v3.8b, v31.8b |
| 290 | .endm |
| 291 |
| 292 | .macro pixman_composite_over_n_8888_process_pixblock_tail_head |
| 293 | urshr v14.8h, v8.8h, #8 |
| 294 | urshr v15.8h, v9.8h, #8 |
| 295 | urshr v16.8h, v10.8h, #8 |
| 296 | urshr v17.8h, v11.8h, #8 |
| 297 | raddhn v28.8b, v14.8h, v8.8h |
| 298 | raddhn v29.8b, v15.8h, v9.8h |
| 299 | raddhn v30.8b, v16.8h, v10.8h |
| 300 | raddhn v31.8b, v17.8h, v11.8h |
| 301 | ld4 {v4.8b, v5.8b, v6.8b, v7.8b}, [DST_R], #32 |
| 302 | uqadd v28.8b, v0.8b, v28.8b |
| 303 | PF add PF_X, PF_X, #8 |
| 304 | PF tst PF_CTL, #0x0F |
| 305 | PF beq 10f |
| 306 | PF add PF_X, PF_X, #8 |
| 307 | PF sub PF_CTL, PF_CTL, #1 |
| 308 | 10: |
| 309 | uqadd v29.8b, v1.8b, v29.8b |
| 310 | uqadd v30.8b, v2.8b, v30.8b |
| 311 | uqadd v31.8b, v3.8b, v31.8b |
| 312 | PF cmp PF_X, ORIG_W |
| 313 | umull v8.8h, v24.8b, v4.8b |
| 314 | PF lsl DUMMY, PF_X, #dst_bpp_shift |
| 315 | PF prfm PREFETCH_MODE, [PF_DST, DUMMY] |
| 316 | umull v9.8h, v24.8b, v5.8b |
| 317 | PF ble 10f |
| 318 | PF sub PF_X, PF_X, ORIG_W |
| 319 | 10: |
| 320 | umull v10.8h, v24.8b, v6.8b |
| 321 | PF subs PF_CTL, PF_CTL, #0x10 |
| 322 | umull v11.8h, v24.8b, v7.8b |
| 323 | PF ble 10f |
| 324 | PF lsl DUMMY, DST_STRIDE, #dst_bpp_shift |
| 325 | PF ldrsb DUMMY, [PF_DST, DUMMY] |
| 326 | PF add PF_DST, PF_DST, #1 |
| 327 | 10: |
| 328 | st4 {v28.8b, v29.8b, v30.8b, v31.8b}, [DST_W], #32 |
| 329 | .endm |
| 330 |
| 331 | .macro pixman_composite_over_n_8888_init |
| 332 | mov v3.s[0], w4 |
| 333 | dup v0.8b, v3.b[0] |
| 334 | dup v1.8b, v3.b[1] |
| 335 | dup v2.8b, v3.b[2] |
| 336 | dup v3.8b, v3.b[3] |
| 337 | mvn v24.8b, v3.8b /* get inverted alpha */ |
| 338 | .endm |
| 339 |
| 340 | generate_composite_function \ |
| 341 | pixman_composite_over_n_8888_asm_neon, 0, 0, 32, \ |
| 342 | FLAG_DST_READWRITE | FLAG_DEINTERLEAVE_32BPP, \ |
| 343 | 8, /* number of pixels, processed in a single block */ \ |
| 344 | 5, /* prefetch distance */ \ |
| 345 | pixman_composite_over_n_8888_init, \ |
| 346 | default_cleanup, \ |
| 347 | pixman_composite_over_8888_8888_process_pixblock_head, \ |
| 348 | pixman_composite_over_8888_8888_process_pixblock_tail, \ |
| 349 | pixman_composite_over_n_8888_process_pixblock_tail_head |
| 350 |
| 351 | /******************************************************************************/ |
| 352 |
| 353 | .macro pixman_composite_src_n_8888_process_pixblock_head |
| 354 | .endm |
| 355 |
| 356 | .macro pixman_composite_src_n_8888_process_pixblock_tail |
| 357 | .endm |
| 358 |
| 359 | .macro pixman_composite_src_n_8888_process_pixblock_tail_head |
| 360 | st1 {v0.2s, v1.2s, v2.2s, v3.2s}, [DST_W], #32 |
| 361 | .endm |
| 362 |
| 363 | .macro pixman_composite_src_n_8888_init |
| 364 | mov v0.s[0], w4 |
| 365 | dup v3.2s, v0.s[0] |
| 366 | dup v2.2s, v0.s[0] |
| 367 | dup v1.2s, v0.s[0] |
| 368 | dup v0.2s, v0.s[0] |
| 369 | .endm |
| 370 |
| 371 | .macro pixman_composite_src_n_8888_cleanup |
| 372 | .endm |
| 373 |
| 374 | generate_composite_function \ |
| 375 | pixman_composite_src_n_8888_asm_neon, 0, 0, 32, \ |
| 376 | FLAG_DST_WRITEONLY, \ |
| 377 | 8, /* number of pixels, processed in a single block */ \ |
| 378 | 0, /* prefetch distance */ \ |
| 379 | pixman_composite_src_n_8888_init, \ |
| 380 | pixman_composite_src_n_8888_cleanup, \ |
| 381 | pixman_composite_src_n_8888_process_pixblock_head, \ |
| 382 | pixman_composite_src_n_8888_process_pixblock_tail, \ |
| 383 | pixman_composite_src_n_8888_process_pixblock_tail_head, \ |
| 384 | 0, /* dst_w_basereg */ \ |
| 385 | 0, /* dst_r_basereg */ \ |
| 386 | 0, /* src_basereg */ \ |
| 387 | 0 /* mask_basereg */ |
| 388 |
| 389 | /******************************************************************************/ |
| 390 |
| 391 | .macro pixman_composite_src_8888_8888_process_pixblock_head |
| 392 | .endm |
| 393 |
| 394 | .macro pixman_composite_src_8888_8888_process_pixblock_tail |
| 395 | .endm |
| 396 |
| 397 | .macro pixman_composite_src_8888_8888_process_pixblock_tail_head |
| 398 | st1 {v0.2s, v1.2s, v2.2s, v3.2s}, [DST_W], #32 |
| 399 | fetch_src_pixblock |
| 400 | cache_preload 8, 8 |
| 401 | .endm |
| 402 |
| 403 | generate_composite_function \ |
| 404 | pixman_composite_src_8888_8888_asm_neon, 32, 0, 32, \ |
| 405 | FLAG_DST_WRITEONLY, \ |
| 406 | 8, /* number of pixels, processed in a single block */ \ |
| 407 | 10, /* prefetch distance */ \ |
| 408 | default_init, \ |
| 409 | default_cleanup, \ |
| 410 | pixman_composite_src_8888_8888_process_pixblock_head, \ |
| 411 | pixman_composite_src_8888_8888_process_pixblock_tail, \ |
| 412 | pixman_composite_src_8888_8888_process_pixblock_tail_head, \ |
| 413 | 0, /* dst_w_basereg */ \ |
| 414 | 0, /* dst_r_basereg */ \ |
| 415 | 0, /* src_basereg */ \ |
| 416 | 0 /* mask_basereg */ |
| 417 |
| 418 | /******************************************************************************/ |
| 419 |