SkBlitRow_opts_arm_neon.cpp revision fbfcd5602128ec010c82cb733c9cdc0a3254f9f3
1a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com/*
2a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * Copyright 2012 The Android Open Source Project
3a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com *
4a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * Use of this source code is governed by a BSD-style license that can be
5a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * found in the LICENSE file.
6a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com */
7a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
8a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkBlitRow_opts_arm.h"
9a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
10a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkBlitMask.h"
11a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkBlitRow.h"
12a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkColorPriv.h"
13a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkDither.h"
14a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkMathPriv.h"
15a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkUtils.h"
16a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
17a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include "SkCachePreload_arm.h"
18a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
19a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#include <arm_neon.h>
20a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
21a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32A_D565_Opaque_neon(uint16_t* SK_RESTRICT dst,
22a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                           const SkPMColor* SK_RESTRICT src, int count,
23a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                           U8CPU alpha, int /*x*/, int /*y*/) {
24a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    SkASSERT(255 == alpha);
25a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
26a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count >= 8) {
27a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        uint16_t* SK_RESTRICT keep_dst;
28fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
29a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        asm volatile (
30a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "ands       ip, %[count], #7            \n\t"
31a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmov.u8    d31, #1<<7                  \n\t"
32a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.16    {q12}, [%[dst]]             \n\t"
33a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld4.8     {d0-d3}, [%[src]]           \n\t"
34a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // Thumb does not support the standard ARM conditional
35a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // instructions but instead requires the 'it' instruction
36a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // to signal conditional execution
37a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "it eq                                  \n\t"
38a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "moveq      ip, #8                      \n\t"
39a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "mov        %[keep_dst], %[dst]         \n\t"
40fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
41a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "add        %[src], %[src], ip, LSL#2   \n\t"
42a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "add        %[dst], %[dst], ip, LSL#1   \n\t"
43a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "subs       %[count], %[count], ip      \n\t"
44a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "b          9f                          \n\t"
45a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // LOOP
46a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "2:                                         \n\t"
47fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
48a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.16    {q12}, [%[dst]]!            \n\t"
49a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld4.8     {d0-d3}, [%[src]]!          \n\t"
50a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vst1.16    {q10}, [%[keep_dst]]        \n\t"
51a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "sub        %[keep_dst], %[dst], #8*2   \n\t"
52a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "subs       %[count], %[count], #8      \n\t"
53a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "9:                                         \n\t"
54a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "pld        [%[dst],#32]                \n\t"
55a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // expand 0565 q12 to 8888 {d4-d7}
56a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovn.u16  d4, q12                     \n\t"
57a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q11, q12, #5                \n\t"
58a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q10, q12, #6+5              \n\t"
59a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovn.u16  d5, q11                     \n\t"
60a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovn.u16  d6, q10                     \n\t"
61a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshl.u8    d4, d4, #3                  \n\t"
62a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshl.u8    d5, d5, #2                  \n\t"
63a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshl.u8    d6, d6, #3                  \n\t"
64fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
65a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovl.u8   q14, d31                    \n\t"
66a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovl.u8   q13, d31                    \n\t"
67a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovl.u8   q12, d31                    \n\t"
68fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
69a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // duplicate in 4/2/1 & 8pix vsns
70a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmvn.8     d30, d3                     \n\t"
71a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmlal.u8   q14, d30, d6                \n\t"
72a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmlal.u8   q13, d30, d5                \n\t"
73a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmlal.u8   q12, d30, d4                \n\t"
74a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q8, q14, #5                 \n\t"
75a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q9, q13, #6                 \n\t"
76a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vaddhn.u16 d6, q14, q8                 \n\t"
77a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q8, q12, #5                 \n\t"
78a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vaddhn.u16 d5, q13, q9                 \n\t"
79a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vqadd.u8   d6, d6, d0                  \n\t"  // moved up
80a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vaddhn.u16 d4, q12, q8                 \n\t"
81a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // intentionally don't calculate alpha
82a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // result in d4-d6
83fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
84a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vqadd.u8   d5, d5, d1                  \n\t"
85a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vqadd.u8   d4, d4, d2                  \n\t"
86fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
87a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // pack 8888 {d4-d6} to 0565 q10
88a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshll.u8   q10, d6, #8                 \n\t"
89a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshll.u8   q3, d5, #8                  \n\t"
90a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshll.u8   q2, d4, #8                  \n\t"
91a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vsri.u16   q10, q3, #5                 \n\t"
92a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vsri.u16   q10, q2, #11                \n\t"
93fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
94a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "bne        2b                          \n\t"
95fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
96a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "1:                                         \n\t"
97a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vst1.16      {q10}, [%[keep_dst]]      \n\t"
98a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      : [count] "+r" (count)
99fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com                      : [dst] "r" (dst), [keep_dst] "r" (keep_dst), [src] "r" (src)
100a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      : "ip", "cc", "memory", "d0","d1","d2","d3","d4","d5","d6","d7",
101a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "d16","d17","d18","d19","d20","d21","d22","d23","d24","d25","d26","d27","d28","d29",
102a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "d30","d31"
103a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      );
104a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
105fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    else
106a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    {   // handle count < 8
107a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        uint16_t* SK_RESTRICT keep_dst;
108fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
109a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        asm volatile (
110a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmov.u8    d31, #1<<7                  \n\t"
111a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "mov        %[keep_dst], %[dst]         \n\t"
112fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
113a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "tst        %[count], #4                \n\t"
114a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "beq        14f                         \n\t"
115a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.16    {d25}, [%[dst]]!            \n\t"
116a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.32    {q1}, [%[src]]!             \n\t"
117fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
118a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "14:                                        \n\t"
119a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "tst        %[count], #2                \n\t"
120a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "beq        12f                         \n\t"
121a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.32    {d24[1]}, [%[dst]]!         \n\t"
122a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.32    {d1}, [%[src]]!             \n\t"
123fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
124a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "12:                                        \n\t"
125a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "tst        %[count], #1                \n\t"
126a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "beq        11f                         \n\t"
127a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.16    {d24[1]}, [%[dst]]!         \n\t"
128a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vld1.32    {d0[1]}, [%[src]]!          \n\t"
129fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
130a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "11:                                        \n\t"
131a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // unzips achieve the same as a vld4 operation
132a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vuzpq.u16  q0, q1                      \n\t"
133a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vuzp.u8    d0, d1                      \n\t"
134a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vuzp.u8    d2, d3                      \n\t"
135a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // expand 0565 q12 to 8888 {d4-d7}
136a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovn.u16  d4, q12                     \n\t"
137a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q11, q12, #5                \n\t"
138a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q10, q12, #6+5              \n\t"
139a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovn.u16  d5, q11                     \n\t"
140a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovn.u16  d6, q10                     \n\t"
141a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshl.u8    d4, d4, #3                  \n\t"
142a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshl.u8    d5, d5, #2                  \n\t"
143a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshl.u8    d6, d6, #3                  \n\t"
144fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
145a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovl.u8   q14, d31                    \n\t"
146a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovl.u8   q13, d31                    \n\t"
147a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmovl.u8   q12, d31                    \n\t"
148fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
149a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // duplicate in 4/2/1 & 8pix vsns
150a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmvn.8     d30, d3                     \n\t"
151a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmlal.u8   q14, d30, d6                \n\t"
152a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmlal.u8   q13, d30, d5                \n\t"
153a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vmlal.u8   q12, d30, d4                \n\t"
154a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q8, q14, #5                 \n\t"
155a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q9, q13, #6                 \n\t"
156a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vaddhn.u16 d6, q14, q8                 \n\t"
157a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshr.u16   q8, q12, #5                 \n\t"
158a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vaddhn.u16 d5, q13, q9                 \n\t"
159a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vqadd.u8   d6, d6, d0                  \n\t"  // moved up
160a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vaddhn.u16 d4, q12, q8                 \n\t"
161a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // intentionally don't calculate alpha
162a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // result in d4-d6
163fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
164a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vqadd.u8   d5, d5, d1                  \n\t"
165a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vqadd.u8   d4, d4, d2                  \n\t"
166fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
167a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // pack 8888 {d4-d6} to 0565 q10
168a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshll.u8   q10, d6, #8                 \n\t"
169a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshll.u8   q3, d5, #8                  \n\t"
170a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vshll.u8   q2, d4, #8                  \n\t"
171a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vsri.u16   q10, q3, #5                 \n\t"
172a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vsri.u16   q10, q2, #11                \n\t"
173fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
174a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      // store
175a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "tst        %[count], #4                \n\t"
176a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "beq        24f                         \n\t"
177a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vst1.16    {d21}, [%[keep_dst]]!       \n\t"
178fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
179a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "24:                                        \n\t"
180a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "tst        %[count], #2                \n\t"
181a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "beq        22f                         \n\t"
182a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vst1.32    {d20[1]}, [%[keep_dst]]!    \n\t"
183fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
184a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "22:                                        \n\t"
185a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "tst        %[count], #1                \n\t"
186a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "beq        21f                         \n\t"
187a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "vst1.16    {d20[1]}, [%[keep_dst]]!    \n\t"
188fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
189a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "21:                                        \n\t"
190a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      : [count] "+r" (count)
191a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      : [dst] "r" (dst), [keep_dst] "r" (keep_dst), [src] "r" (src)
192a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      : "ip", "cc", "memory", "d0","d1","d2","d3","d4","d5","d6","d7",
193a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "d16","d17","d18","d19","d20","d21","d22","d23","d24","d25","d26","d27","d28","d29",
194a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      "d30","d31"
195a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      );
196a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
197a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
198a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
199a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32A_D565_Blend_neon(uint16_t* SK_RESTRICT dst,
200a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                          const SkPMColor* SK_RESTRICT src, int count,
201a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                          U8CPU alpha, int /*x*/, int /*y*/) {
202a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
203a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    U8CPU alpha_for_asm = alpha;
204a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
205a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    asm volatile (
206a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    /* This code implements a Neon version of S32A_D565_Blend. The output differs from
207a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     * the original in two respects:
208a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *  1. The results have a few mismatches compared to the original code. These mismatches
209a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *     never exceed 1. It's possible to improve accuracy vs. a floating point
210a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *     implementation by introducing rounding right shifts (vrshr) for the final stage.
211a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *     Rounding is not present in the code below, because although results would be closer
212fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     *     to a floating point implementation, the number of mismatches compared to the
213a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *     original code would be far greater.
214a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *  2. On certain inputs, the original code can overflow, causing colour channels to
215a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *     mix. Although the Neon code can also overflow, it doesn't allow one colour channel
216a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     *     to affect another.
217a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com     */
218fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
219a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if 1
220fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* reflects SkAlpha255To256()'s change from a+a>>7 to a+1 */
221a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "add        %[alpha], %[alpha], #1         \n\t"   // adjust range of alpha 0-256
222a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
223a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "add        %[alpha], %[alpha], %[alpha], lsr #7    \n\t"   // adjust range of alpha 0-256
224a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
225a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmov.u16   q3, #255                        \n\t"   // set up constant
226a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "movs       r4, %[count], lsr #3            \n\t"   // calc. count>>3
227a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmov.u16   d2[0], %[alpha]                 \n\t"   // move alpha to Neon
228a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "beq        2f                              \n\t"   // if count8 == 0, exit
229a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmov.u16   q15, #0x1f                      \n\t"   // set up blue mask
230fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
231a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "1:                                             \n\t"
232a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vld1.u16   {d0, d1}, [%[dst]]              \n\t"   // load eight dst RGB565 pixels
233a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "subs       r4, r4, #1                      \n\t"   // decrement loop counter
234a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vld4.u8    {d24, d25, d26, d27}, [%[src]]! \n\t"   // load eight src ABGR32 pixels
235a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  //  and deinterleave
236fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
237a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshl.u16   q9, q0, #5                      \n\t"   // shift green to top of lanes
238a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vand       q10, q0, q15                    \n\t"   // extract blue
239a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16   q8, q0, #11                     \n\t"   // extract red
240a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16   q9, q9, #10                     \n\t"   // extract green
241a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // dstrgb = {q8, q9, q10}
242fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
243a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8    d24, d24, #3                    \n\t"   // shift red to 565 range
244a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8    d25, d25, #2                    \n\t"   // shift green to 565 range
245a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8    d26, d26, #3                    \n\t"   // shift blue to 565 range
246fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
247a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmovl.u8   q11, d24                        \n\t"   // widen red to 16 bits
248a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmovl.u8   q12, d25                        \n\t"   // widen green to 16 bits
249a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmovl.u8   q14, d27                        \n\t"   // widen alpha to 16 bits
250a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmovl.u8   q13, d26                        \n\t"   // widen blue to 16 bits
251a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // srcrgba = {q11, q12, q13, q14}
252fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
253a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.u16   q2, q14, d2[0]                  \n\t"   // sa * src_scale
254a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.u16   q11, q11, d2[0]                 \n\t"   // red result = src_red * src_scale
255a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.u16   q12, q12, d2[0]                 \n\t"   // grn result = src_grn * src_scale
256a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.u16   q13, q13, d2[0]                 \n\t"   // blu result = src_blu * src_scale
257fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
258a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16   q2, q2, #8                      \n\t"   // sa * src_scale >> 8
259a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsub.u16   q2, q3, q2                      \n\t"   // 255 - (sa * src_scale >> 8)
260a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // dst_scale = q2
261fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
262a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmla.u16   q11, q8, q2                     \n\t"   // red result += dst_red * dst_scale
263a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmla.u16   q12, q9, q2                     \n\t"   // grn result += dst_grn * dst_scale
264a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmla.u16   q13, q10, q2                    \n\t"   // blu result += dst_blu * dst_scale
265a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
266a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if 1
267fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    // trying for a better match with SkDiv255Round(a)
268fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    // C alg is:  a+=128; (a+a>>8)>>8
269fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    // we'll use just a rounding shift [q2 is available for scratch]
270a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vrshr.u16   q11, q11, #8                    \n\t"   // shift down red
271a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vrshr.u16   q12, q12, #8                    \n\t"   // shift down green
272a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vrshr.u16   q13, q13, #8                    \n\t"   // shift down blue
273a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
274fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    // arm's original "truncating divide by 256"
275a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16   q11, q11, #8                    \n\t"   // shift down red
276a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16   q12, q12, #8                    \n\t"   // shift down green
277a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16   q13, q13, #8                    \n\t"   // shift down blue
278a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
279fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
280a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsli.u16   q13, q12, #5                    \n\t"   // insert green into blue
281a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsli.u16   q13, q11, #11                   \n\t"   // insert red into green/blue
282a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vst1.16    {d26, d27}, [%[dst]]!           \n\t"   // write pixel back to dst, update ptr
283fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
284a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "bne        1b                              \n\t"   // if counter != 0, loop
285a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "2:                                             \n\t"   // exit
286fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
287a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  : [src] "+r" (src), [dst] "+r" (dst), [count] "+r" (count), [alpha] "+r" (alpha_for_asm)
288a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  :
289a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  : "cc", "memory", "r4", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31"
290a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  );
291a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
292a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    count &= 7;
293a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count > 0) {
294a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        do {
295a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColor sc = *src++;
296a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            if (sc) {
297a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                uint16_t dc = *dst;
298a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned dst_scale = 255 - SkMulDiv255Round(SkGetPackedA32(sc), alpha);
299a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned dr = SkMulS16(SkPacked32ToR16(sc), alpha) + SkMulS16(SkGetPackedR16(dc), dst_scale);
300a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned dg = SkMulS16(SkPacked32ToG16(sc), alpha) + SkMulS16(SkGetPackedG16(dc), dst_scale);
301a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned db = SkMulS16(SkPacked32ToB16(sc), alpha) + SkMulS16(SkGetPackedB16(dc), dst_scale);
302a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                *dst = SkPackRGB16(SkDiv255Round(dr), SkDiv255Round(dg), SkDiv255Round(db));
303a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            }
304a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            dst += 1;
305a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        } while (--count != 0);
306a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
307a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
308a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
309a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com/* dither matrix for Neon, derived from gDitherMatrix_3Bit_16.
310a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * each dither value is spaced out into byte lanes, and repeated
311a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * to allow an 8-byte load from offsets 0, 1, 2 or 3 from the
312a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * start of each row.
313a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com */
314a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comstatic const uint8_t gDitherMatrix_Neon[48] = {
315a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    0, 4, 1, 5, 0, 4, 1, 5, 0, 4, 1, 5,
316a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    6, 2, 7, 3, 6, 2, 7, 3, 6, 2, 7, 3,
317a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    1, 5, 0, 4, 1, 5, 0, 4, 1, 5, 0, 4,
318a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    7, 3, 6, 2, 7, 3, 6, 2, 7, 3, 6, 2,
319fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
320a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com};
321a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
322a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32_D565_Blend_Dither_neon(uint16_t *dst, const SkPMColor *src,
323a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                int count, U8CPU alpha, int x, int y)
324a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com{
325a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    /* select row and offset for dither array */
326a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    const uint8_t *dstart = &gDitherMatrix_Neon[(y&3)*12 + (x&3)];
327fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
328a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    /* rescale alpha to range 0 - 256 */
329a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    int scale = SkAlpha255To256(alpha);
330fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
331a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    asm volatile (
332a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vld1.8         {d31}, [%[dstart]]              \n\t"   // load dither values
333a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8        d30, d31, #1                    \n\t"   // calc. green dither values
334a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vdup.16        d6, %[scale]                    \n\t"   // duplicate scale into neon reg
335a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmov.i8        d29, #0x3f                      \n\t"   // set up green mask
336a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmov.i8        d28, #0x1f                      \n\t"   // set up blue mask
337a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "1:                                                 \n\t"
338a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vld4.8         {d0, d1, d2, d3}, [%[src]]!     \n\t"   // load 8 pixels and split into argb
339a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8        d22, d0, #5                     \n\t"   // calc. red >> 5
340a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8        d23, d1, #6                     \n\t"   // calc. green >> 6
341a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u8        d24, d2, #5                     \n\t"   // calc. blue >> 5
342a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vaddl.u8       q8, d0, d31                     \n\t"   // add in dither to red and widen
343a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vaddl.u8       q9, d1, d30                     \n\t"   // add in dither to green and widen
344a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vaddl.u8       q10, d2, d31                    \n\t"   // add in dither to blue and widen
345a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsubw.u8       q8, q8, d22                     \n\t"   // sub shifted red from result
346a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsubw.u8       q9, q9, d23                     \n\t"   // sub shifted green from result
347a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsubw.u8       q10, q10, d24                   \n\t"   // sub shifted blue from result
348a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d22, q8, #3                     \n\t"   // shift right and narrow to 5 bits
349a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d23, q9, #2                     \n\t"   // shift right and narrow to 6 bits
350a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d24, q10, #3                    \n\t"   // shift right and narrow to 5 bits
351a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // load 8 pixels from dst, extract rgb
352a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vld1.16        {d0, d1}, [%[dst]]              \n\t"   // load 8 pixels
353a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d17, q0, #5                     \n\t"   // shift green down to bottom 6 bits
354a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmovn.i16      d18, q0                         \n\t"   // narrow to get blue as bytes
355a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshr.u16       q0, q0, #11                     \n\t"   // shift down to extract red
356a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vand           d17, d17, d29                   \n\t"   // and green with green mask
357a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vand           d18, d18, d28                   \n\t"   // and blue with blue mask
358a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmovn.i16      d16, q0                         \n\t"   // narrow to get red as bytes
359a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // src = {d22 (r), d23 (g), d24 (b)}
360a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // dst = {d16 (r), d17 (g), d18 (b)}
361a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // subtract dst from src and widen
362a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsubl.s8       q0, d22, d16                    \n\t"   // subtract red src from dst
363a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsubl.s8       q1, d23, d17                    \n\t"   // subtract green src from dst
364a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsubl.s8       q2, d24, d18                    \n\t"   // subtract blue src from dst
365a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // multiply diffs by scale and shift
366a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.i16       q0, q0, d6[0]                   \n\t"   // multiply red by scale
367a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.i16       q1, q1, d6[0]                   \n\t"   // multiply blue by scale
368a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vmul.i16       q2, q2, d6[0]                   \n\t"   // multiply green by scale
369a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "subs           %[count], %[count], #8          \n\t"   // decrement loop counter
370a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d0, q0, #8                      \n\t"   // shift down red by 8 and narrow
371a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d2, q1, #8                      \n\t"   // shift down green by 8 and narrow
372a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vshrn.i16      d4, q2, #8                      \n\t"   // shift down blue by 8 and narrow
373a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // add dst to result
374a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vaddl.s8       q0, d0, d16                     \n\t"   // add dst to red
375a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vaddl.s8       q1, d2, d17                     \n\t"   // add dst to green
376a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vaddl.s8       q2, d4, d18                     \n\t"   // add dst to blue
377a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  // put result into 565 format
378a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsli.i16       q2, q1, #5                      \n\t"   // shift up green and insert into blue
379a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vsli.i16       q2, q0, #11                     \n\t"   // shift up red and insert into blue
380a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "vst1.16        {d4, d5}, [%[dst]]!             \n\t"   // store result
381a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "bgt            1b                              \n\t"   // loop if count > 0
382a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  : [src] "+r" (src), [dst] "+r" (dst), [count] "+r" (count)
383a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  : [dstart] "r" (dstart), [scale] "r" (scale)
384a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  : "cc", "memory", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23", "d24", "d28", "d29", "d30", "d31"
385a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  );
386fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
387a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    DITHER_565_SCAN(y);
388fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
389a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    while((count & 7) > 0)
390a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    {
391a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        SkPMColor c = *src++;
392fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
393a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        int dither = DITHER_VALUE(x);
394a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        int sr = SkGetPackedR32(c);
395a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        int sg = SkGetPackedG32(c);
396a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        int sb = SkGetPackedB32(c);
397a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        sr = SkDITHER_R32To565(sr, dither);
398a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        sg = SkDITHER_G32To565(sg, dither);
399a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        sb = SkDITHER_B32To565(sb, dither);
400fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
401a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        uint16_t d = *dst;
402a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        *dst++ = SkPackRGB16(SkAlphaBlend(sr, SkGetPackedR16(d), scale),
403a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                             SkAlphaBlend(sg, SkGetPackedG16(d), scale),
404a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                             SkAlphaBlend(sb, SkGetPackedB16(d), scale));
405a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        DITHER_INC_X(x);
406a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        count--;
407a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
408a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
409a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
410a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32A_Opaque_BlitRow32_neon(SkPMColor* SK_RESTRICT dst,
411a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                const SkPMColor* SK_RESTRICT src,
412a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                int count, U8CPU alpha) {
413a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
414a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    SkASSERT(255 == alpha);
415a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count > 0) {
416a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
417a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
418fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    uint8x8_t alpha_mask;
419a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
420fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    static const uint8_t alpha_mask_setup[] = {3,3,3,3,7,7,7,7};
421fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    alpha_mask = vld1_u8(alpha_mask_setup);
422a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
423fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* do the NEON unrolled code */
424fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#define    UNROLL    4
425fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    while (count >= UNROLL) {
426fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t src_raw, dst_raw, dst_final;
427fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t src_raw_2, dst_raw_2, dst_final_2;
428a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
429fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* get the source */
430fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src_raw = vreinterpret_u8_u32(vld1_u32(src));
431fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    UNROLL > 2
432fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src_raw_2 = vreinterpret_u8_u32(vld1_u32(src+2));
433a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
434a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
435fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* get and hold the dst too */
436fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_raw = vreinterpret_u8_u32(vld1_u32(dst));
437fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    UNROLL > 2
438fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_raw_2 = vreinterpret_u8_u32(vld1_u32(dst+2));
439a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
440a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
441fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* 1st and 2nd bits of the unrolling */
442fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    {
443fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t dst_cooked;
444fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t dst_wide;
445fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t alpha_narrow;
446fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t alpha_wide;
447a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
448fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* get the alphas spread out properly */
449fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_narrow = vtbl1_u8(src_raw, alpha_mask);
450a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if 1
451fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* reflect SkAlpha255To256() semantics a+1 vs a+a>>7 */
452fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* we collapsed (255-a)+1 ... */
453fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_wide = vsubw_u8(vdupq_n_u16(256), alpha_narrow);
454a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
455fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_wide = vsubw_u8(vdupq_n_u16(255), alpha_narrow);
456fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_wide = vaddq_u16(alpha_wide, vshrq_n_u16(alpha_wide,7));
457a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
458a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
459fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* spread the dest */
460fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_wide = vmovl_u8(dst_raw);
461a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
462fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* alpha mul the dest */
463fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_wide = vmulq_u16 (dst_wide, alpha_wide);
464fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_cooked = vshrn_n_u16(dst_wide, 8);
465a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
466fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sum -- ignoring any byte lane overflows */
467fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_final = vadd_u8(src_raw, dst_cooked);
468fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
469a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
470fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    UNROLL > 2
471fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* the 3rd and 4th bits of our unrolling */
472fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    {
473fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t dst_cooked;
474fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t dst_wide;
475fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t alpha_narrow;
476fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t alpha_wide;
477a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
478fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_narrow = vtbl1_u8(src_raw_2, alpha_mask);
479a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if 1
480fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* reflect SkAlpha255To256() semantics a+1 vs a+a>>7 */
481fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* we collapsed (255-a)+1 ... */
482fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_wide = vsubw_u8(vdupq_n_u16(256), alpha_narrow);
483a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
484fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_wide = vsubw_u8(vdupq_n_u16(255), alpha_narrow);
485fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha_wide = vaddq_u16(alpha_wide, vshrq_n_u16(alpha_wide,7));
486a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
487a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
488fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* spread the dest */
489fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_wide = vmovl_u8(dst_raw_2);
490a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
491fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* alpha mul the dest */
492fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_wide = vmulq_u16 (dst_wide, alpha_wide);
493fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_cooked = vshrn_n_u16(dst_wide, 8);
494a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
495fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sum -- ignoring any byte lane overflows */
496fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_final_2 = vadd_u8(src_raw_2, dst_cooked);
497fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
498a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
499a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
500fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        vst1_u32(dst, vreinterpret_u32_u8(dst_final));
501fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    UNROLL > 2
502fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        vst1_u32(dst+2, vreinterpret_u32_u8(dst_final_2));
503a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
504a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
505fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src += UNROLL;
506fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst += UNROLL;
507fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        count -= UNROLL;
508fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
509fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#undef    UNROLL
510a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
511fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* do any residual iterations */
512a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        while (--count >= 0) {
513a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            *dst = SkPMSrcOver(*src, *dst);
514a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            src += 1;
515a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            dst += 1;
516a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        }
517a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
518a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
519a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
520a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
521a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com/* Neon version of S32_Blend_BlitRow32()
522a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * portable version is in src/core/SkBlitRow_D32.cpp
523a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com */
524a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32_Blend_BlitRow32_neon(SkPMColor* SK_RESTRICT dst,
525a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                              const SkPMColor* SK_RESTRICT src,
526a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                              int count, U8CPU alpha) {
527a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    SkASSERT(alpha <= 255);
528a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count > 0) {
529a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        uint16_t src_scale = SkAlpha255To256(alpha);
530a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        uint16_t dst_scale = 256 - src_scale;
531a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
532fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* run them N at a time through the NEON unit */
533fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* note that each 1 is 4 bytes, each treated exactly the same,
534fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * so we can work under that guise. We *do* know that the src&dst
535fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * will be 32-bit aligned quantities, so we can specify that on
536fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * the load/store ops and do a neon 'reinterpret' to get us to
537fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * byte-sized (pun intended) pieces that we widen/multiply/shift
538fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * we're limited at 128 bits in the wide ops, which is 8x16bits
539fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * or a pair of 32 bit src/dsts.
540fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     */
541fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* we *could* manually unroll this loop so that we load 128 bits
542fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * (as a pair of 64s) from each of src and dst, processing them
543fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * in pieces. This might give us a little better management of
544fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * the memory latency, but my initial attempts here did not
545fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * produce an instruction stream that looked all that nice.
546fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     */
547fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#define    UNROLL    2
548fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    while (count >= UNROLL) {
549fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t  src_raw, dst_raw, dst_final;
550fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t  src_wide, dst_wide;
551fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
552fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* get 64 bits of src, widen it, multiply by src_scale */
553fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src_raw = vreinterpret_u8_u32(vld1_u32(src));
554fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src_wide = vmovl_u8(src_raw);
555fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* gcc hoists vdupq_n_u16(), better than using vmulq_n_u16() */
556fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src_wide = vmulq_u16 (src_wide, vdupq_n_u16(src_scale));
557fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
558fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* ditto with dst */
559fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_raw = vreinterpret_u8_u32(vld1_u32(dst));
560fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_wide = vmovl_u8(dst_raw);
561fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
562fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* combine add with dst multiply into mul-accumulate */
563fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_wide = vmlaq_u16(src_wide, dst_wide, vdupq_n_u16(dst_scale));
564fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
565fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_final = vshrn_n_u16(dst_wide, 8);
566fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        vst1_u32(dst, vreinterpret_u32_u8(dst_final));
567fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
568fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src += UNROLL;
569fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst += UNROLL;
570fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        count -= UNROLL;
571fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
572fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* RBE: well, i don't like how gcc manages src/dst across the above
573fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * loop it's constantly calculating src+bias, dst+bias and it only
574fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * adjusts the real ones when we leave the loop. Not sure why
575fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * it's "hoisting down" (hoisting implies above in my lexicon ;))
576fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * the adjustments to src/dst/count, but it does...
577fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * (might be SSA-style internal logic...
578fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     */
579fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
580fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    UNROLL == 2
581fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    if (count == 1) {
582a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale);
583fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
584a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
585fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    if (count > 0) {
586a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            do {
587a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale);
588a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                src += 1;
589a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                dst += 1;
590a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            } while (--count > 0);
591fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
592a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
593a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
594fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#undef    UNROLL
595a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
596a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
597a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
598a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com///////////////////////////////////////////////////////////////////////////////
599a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
600fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#undef    DEBUG_OPAQUE_DITHER
601a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
602fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    defined(DEBUG_OPAQUE_DITHER)
603a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comstatic void showme8(char *str, void *p, int len)
604a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com{
605fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    static char buf[256];
606fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    char tbuf[32];
607fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int i;
608fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    char *pc = (char*) p;
609fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    sprintf(buf,"%8s:", str);
610fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    for(i=0;i<len;i++) {
611fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sprintf(tbuf, "   %02x", pc[i]);
612fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        strcat(buf, tbuf);
613fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
614fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkDebugf("%s\n", buf);
615a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
616a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comstatic void showme16(char *str, void *p, int len)
617a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com{
618fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    static char buf[256];
619fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    char tbuf[32];
620fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int i;
621fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    uint16_t *pc = (uint16_t*) p;
622fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    sprintf(buf,"%8s:", str);
623fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    len = (len / sizeof(uint16_t));    /* passed as bytes */
624fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    for(i=0;i<len;i++) {
625fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sprintf(tbuf, " %04x", pc[i]);
626fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        strcat(buf, tbuf);
627fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
628fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    SkDebugf("%s\n", buf);
629a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
630a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
631a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
632a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32A_D565_Opaque_Dither_neon (uint16_t * SK_RESTRICT dst,
633a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                   const SkPMColor* SK_RESTRICT src,
634a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                   int count, U8CPU alpha, int x, int y) {
635a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    SkASSERT(255 == alpha);
636a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
637fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#define    UNROLL    8
638a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
639a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count >= UNROLL) {
640fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    uint8x8_t dbase;
641fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
642fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    defined(DEBUG_OPAQUE_DITHER)
643fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    uint16_t tmpbuf[UNROLL];
644fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int td[UNROLL];
645fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int tdv[UNROLL];
646fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int ta[UNROLL];
647fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int tap[UNROLL];
648fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    uint16_t in_dst[UNROLL];
649fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int offset = 0;
650fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    int noisy = 0;
651a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
652a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
653fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    const uint8_t *dstart = &gDitherMatrix_Neon[(y&3)*12 + (x&3)];
654fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    dbase = vld1_u8(dstart);
655a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
656a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        do {
657fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t sr, sg, sb, sa, d;
658fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t dst8, scale8, alpha8;
659fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t dst_r, dst_g, dst_b;
660fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
661fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    defined(DEBUG_OPAQUE_DITHER)
662fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    /* calculate 8 elements worth into a temp buffer */
663fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    {
664fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com      int my_y = y;
665fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com      int my_x = x;
666fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com      SkPMColor* my_src = (SkPMColor*)src;
667fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com      uint16_t* my_dst = dst;
668fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com      int i;
669a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
670a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com          DITHER_565_SCAN(my_y);
671a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com          for(i=0;i<UNROLL;i++) {
672a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColor c = *my_src++;
673a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColorAssert(c);
674a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            if (c) {
675a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned a = SkGetPackedA32(c);
676fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
677a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                int d = SkAlphaMul(DITHER_VALUE(my_x), SkAlpha255To256(a));
678fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        tdv[i] = DITHER_VALUE(my_x);
679fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        ta[i] = a;
680fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        tap[i] = SkAlpha255To256(a);
681fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        td[i] = d;
682fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
683a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned sr = SkGetPackedR32(c);
684a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned sg = SkGetPackedG32(c);
685a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned sb = SkGetPackedB32(c);
686a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                sr = SkDITHER_R32_FOR_565(sr, d);
687a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                sg = SkDITHER_G32_FOR_565(sg, d);
688a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                sb = SkDITHER_B32_FOR_565(sb, d);
689fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
690a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                uint32_t src_expanded = (sg << 24) | (sr << 13) | (sb << 2);
691a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                uint32_t dst_expanded = SkExpand_rgb_16(*my_dst);
692a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                dst_expanded = dst_expanded * (SkAlpha255To256(255 - a) >> 3);
693a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // now src and dst expanded are in g:11 r:10 x:1 b:10
694a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                tmpbuf[i] = SkCompact_rgb_16((src_expanded + dst_expanded) >> 5);
695fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        td[i] = d;
696a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
697a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            } else {
698fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        tmpbuf[i] = *my_dst;
699fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        ta[i] = tdv[i] = td[i] = 0xbeef;
700fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
701fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        in_dst[i] = *my_dst;
702a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            my_dst += 1;
703a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            DITHER_INC_X(my_x);
704a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com          }
705fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
706a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
707a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
708fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* source is in ABGR */
709fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        {
710fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d0 asm("d0");
711fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d1 asm("d1");
712fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d2 asm("d2");
713fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d3 asm("d3");
714a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
715fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        asm ("vld4.8    {d0-d3},[%4]  /* r=%P0 g=%P1 b=%P2 a=%P3 */"
716fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            : "=w" (d0), "=w" (d1), "=w" (d2), "=w" (d3)
717fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            : "r" (src)
718a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                    );
719fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            sr = d0; sg = d1; sb = d2; sa = d3;
720fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
721a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
722fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* calculate 'd', which will be 0..7 */
723fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* dbase[] is 0..7; alpha is 0..256; 16 bits suffice */
724a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if defined(SK_BUILD_FOR_ANDROID)
725fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* SkAlpha255To256() semantic a+1 vs a+a>>7 */
726fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha8 = vaddw_u8(vmovl_u8(sa), vdup_n_u8(1));
727a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
728fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha8 = vaddw_u8(vmovl_u8(sa), vshr_n_u8(sa, 7));
729a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
730fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        alpha8 = vmulq_u16(alpha8, vmovl_u8(dbase));
731fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        d = vshrn_n_u16(alpha8, 8);    /* narrowing too */
732fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
733fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sr = sr - (sr>>5) + d */
734fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* watching for 8-bit overflow.  d is 0..7; risky range of
735fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com         * sr is >248; and then (sr>>5) is 7 so it offsets 'd';
736fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com         * safe  as long as we do ((sr-sr>>5) + d) */
737fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sr = vsub_u8(sr, vshr_n_u8(sr, 5));
738fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sr = vadd_u8(sr, d);
739fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
740fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sb = sb - (sb>>5) + d */
741fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sb = vsub_u8(sb, vshr_n_u8(sb, 5));
742fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sb = vadd_u8(sb, d);
743fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
744fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sg = sg - (sg>>6) + d>>1; similar logic for overflows */
745fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sg = vsub_u8(sg, vshr_n_u8(sg, 6));
746fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sg = vadd_u8(sg, vshr_n_u8(d,1));
747fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
748fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* need to pick up 8 dst's -- at 16 bits each, 128 bits */
749fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vld1q_u16(dst);
750fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_b = vandq_u16(dst8, vdupq_n_u16(0x001F));
751fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_g = vandq_u16(vshrq_n_u16(dst8,5), vdupq_n_u16(0x003F));
752fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_r = vshrq_n_u16(dst8,11);    /* clearing hi bits */
753fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
754fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* blend */
755a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if 1
756fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* SkAlpha255To256() semantic a+1 vs a+a>>7 */
757fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* originally 255-sa + 1 */
758fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        scale8 = vsubw_u8(vdupq_n_u16(256), sa);
759a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
760fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        scale8 = vsubw_u8(vdupq_n_u16(255), sa);
761fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        scale8 = vaddq_u16(scale8, vshrq_n_u16(scale8, 7));
762a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
763a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
764a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#if 1
765fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* combine the addq and mul, save 3 insns */
766fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        scale8 = vshrq_n_u16(scale8, 3);
767fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_b = vmlaq_u16(vshll_n_u8(sb,2), dst_b, scale8);
768fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_g = vmlaq_u16(vshll_n_u8(sg,3), dst_g, scale8);
769fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_r = vmlaq_u16(vshll_n_u8(sr,2), dst_r, scale8);
770a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#else
771fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* known correct, but +3 insns over above */
772fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        scale8 = vshrq_n_u16(scale8, 3);
773fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_b = vmulq_u16(dst_b, scale8);
774fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_g = vmulq_u16(dst_g, scale8);
775fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_r = vmulq_u16(dst_r, scale8);
776fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
777fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* combine */
778fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* NB: vshll widens, need to preserve those bits */
779fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_b = vaddq_u16(dst_b, vshll_n_u8(sb,2));
780fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_g = vaddq_u16(dst_g, vshll_n_u8(sg,3));
781fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst_r = vaddq_u16(dst_r, vshll_n_u8(sr,2));
782a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
783a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
784fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* repack to store */
785fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vandq_u16(vshrq_n_u16(dst_b, 5), vdupq_n_u16(0x001F));
786fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vsliq_n_u16(dst8, vshrq_n_u16(dst_g, 5), 5);
787fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vsliq_n_u16(dst8, vshrq_n_u16(dst_r,5), 11);
788fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
789fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        vst1q_u16(dst, dst8);
790fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
791fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    defined(DEBUG_OPAQUE_DITHER)
792fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* verify my 8 elements match the temp buffer */
793fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    {
794fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       int i, bad=0;
795fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       static int invocation;
796fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
797fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       for (i=0;i<UNROLL;i++)
798fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        if (tmpbuf[i] != dst[i]) bad=1;
799fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       if (bad) {
800fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        SkDebugf("BAD S32A_D565_Opaque_Dither_neon(); invocation %d offset %d\n",
801fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            invocation, offset);
802fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        SkDebugf("  alpha 0x%x\n", alpha);
803fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        for (i=0;i<UNROLL;i++)
804fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            SkDebugf("%2d: %s %04x w %04x id %04x s %08x d %04x %04x %04x %04x\n",
805fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            i, ((tmpbuf[i] != dst[i])?"BAD":"got"),
806fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            dst[i], tmpbuf[i], in_dst[i], src[i], td[i], tdv[i], tap[i], ta[i]);
807fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
808fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme16("alpha8", &alpha8, sizeof(alpha8));
809fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme16("scale8", &scale8, sizeof(scale8));
810fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme8("d", &d, sizeof(d));
811fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme16("dst8", &dst8, sizeof(dst8));
812fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme16("dst_b", &dst_b, sizeof(dst_b));
813fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme16("dst_g", &dst_g, sizeof(dst_g));
814fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme16("dst_r", &dst_r, sizeof(dst_r));
815fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme8("sb", &sb, sizeof(sb));
816fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme8("sg", &sg, sizeof(sg));
817fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        showme8("sr", &sr, sizeof(sr));
818fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
819fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* cop out */
820fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        return;
821fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       }
822fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       offset += UNROLL;
823fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com       invocation++;
824fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
825a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
826a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
827a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            dst += UNROLL;
828fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src += UNROLL;
829fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        count -= UNROLL;
830fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* skip x += UNROLL, since it's unchanged mod-4 */
831a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        } while (count >= UNROLL);
832a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
833fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#undef    UNROLL
834a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
835a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    /* residuals */
836a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count > 0) {
837a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        DITHER_565_SCAN(y);
838a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        do {
839a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColor c = *src++;
840a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColorAssert(c);
841a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            if (c) {
842a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned a = SkGetPackedA32(c);
843fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
844a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // dither and alpha are just temporary variables to work-around
845a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // an ICE in debug.
846a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned dither = DITHER_VALUE(x);
847a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned alpha = SkAlpha255To256(a);
848a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                int d = SkAlphaMul(dither, alpha);
849fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
850a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned sr = SkGetPackedR32(c);
851a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned sg = SkGetPackedG32(c);
852a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                unsigned sb = SkGetPackedB32(c);
853a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                sr = SkDITHER_R32_FOR_565(sr, d);
854a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                sg = SkDITHER_G32_FOR_565(sg, d);
855a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                sb = SkDITHER_B32_FOR_565(sb, d);
856fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
857a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                uint32_t src_expanded = (sg << 24) | (sr << 13) | (sb << 2);
858a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                uint32_t dst_expanded = SkExpand_rgb_16(*dst);
859a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                dst_expanded = dst_expanded * (SkAlpha255To256(255 - a) >> 3);
860a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // now src and dst expanded are in g:11 r:10 x:1 b:10
861a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                *dst = SkCompact_rgb_16((src_expanded + dst_expanded) >> 5);
862a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            }
863a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            dst += 1;
864a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            DITHER_INC_X(x);
865a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        } while (--count != 0);
866a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
867a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
868a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
869a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com///////////////////////////////////////////////////////////////////////////////
870a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
871a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com/* 2009/10/27: RBE says "a work in progress"; debugging says ok;
872a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * speedup untested, but ARM version is 26 insns/iteration and
873a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * this NEON version is 21 insns/iteration-of-8 (2.62insns/element)
874a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * which is 10x the native version; that's pure instruction counts,
875a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com * not accounting for any instruction or memory latencies.
876a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com */
877a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
878fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#undef    DEBUG_S32_OPAQUE_DITHER
879a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
880a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid S32_D565_Opaque_Dither_neon(uint16_t* SK_RESTRICT dst,
881a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                 const SkPMColor* SK_RESTRICT src,
882a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                                 int count, U8CPU alpha, int x, int y) {
883a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    SkASSERT(255 == alpha);
884a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
885fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#define    UNROLL    8
886a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count >= UNROLL) {
887fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    uint8x8_t d;
888fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    const uint8_t *dstart = &gDitherMatrix_Neon[(y&3)*12 + (x&3)];
889fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    d = vld1_u8(dstart);
890fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
891fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    while (count >= UNROLL) {
892fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint8x8_t sr, sg, sb, sa;
893fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t dr, dg, db, da;
894fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        uint16x8_t dst8;
895fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
896fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* source is in ABGR ordering (R == lsb) */
897fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        {
898fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d0 asm("d0");
899fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d1 asm("d1");
900fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d2 asm("d2");
901fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        register uint8x8_t d3 asm("d3");
902fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
903fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        asm ("vld4.8    {d0-d3},[%4]  /* r=%P0 g=%P1 b=%P2 a=%P3 */"
904fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            : "=w" (d0), "=w" (d1), "=w" (d2), "=w" (d3)
905fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            : "r" (src)
906a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                    );
907fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            sr = d0; sg = d1; sb = d2; sa = d3;
908fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
909fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* XXX: if we want to prefetch, hide it in the above asm()
910fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com         * using the gcc __builtin_prefetch(), the prefetch will
911fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com         * fall to the bottom of the loop -- it won't stick up
912fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com         * at the top of the loop, just after the vld4.
913fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com         */
914fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
915fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sr = sr - (sr>>5) + d */
916fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sr = vsub_u8(sr, vshr_n_u8(sr, 5));
917fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dr = vaddl_u8(sr, d);
918fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
919fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sb = sb - (sb>>5) + d */
920fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sb = vsub_u8(sb, vshr_n_u8(sb, 5));
921fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        db = vaddl_u8(sb, d);
922fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
923fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* sg = sg - (sg>>6) + d>>1; similar logic for overflows */
924fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        sg = vsub_u8(sg, vshr_n_u8(sg, 6));
925fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dg = vaddl_u8(sg, vshr_n_u8(d,1));
926fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* XXX: check that the "d>>1" here is hoisted */
927fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
928fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* pack high bits of each into 565 format  (rgb, b is lsb) */
929fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vshrq_n_u16(db, 3);
930fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vsliq_n_u16(dst8, vshrq_n_u16(dg, 2), 5);
931fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst8 = vsliq_n_u16(dst8, vshrq_n_u16(dr,3), 11);
932fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
933fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* store it */
934fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        vst1q_u16(dst, dst8);
935fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
936fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#if    defined(DEBUG_S32_OPAQUE_DITHER)
937fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        /* always good to know if we generated good results */
938fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        {
939fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        int i, myx = x, myy = y;
940fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        DITHER_565_SCAN(myy);
941fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        for (i=0;i<UNROLL;i++) {
942fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            SkPMColor c = src[i];
943fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            unsigned dither = DITHER_VALUE(myx);
944fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            uint16_t val = SkDitherRGB32To565(c, dither);
945fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            if (val != dst[i]) {
946fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            SkDebugf("RBE: src %08x dither %02x, want %04x got %04x dbas[i] %02x\n",
947fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com                c, dither, val, dst[i], dstart[i]);
948fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            }
949fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com            DITHER_INC_X(myx);
950fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
951fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        }
952a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com#endif
953a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
954fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        dst += UNROLL;
955fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        src += UNROLL;
956fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        count -= UNROLL;
957fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        x += UNROLL;        /* probably superfluous */
958fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    }
959a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
960fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#undef    UNROLL
961a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
962a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    /* residuals */
963a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count > 0) {
964a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        DITHER_565_SCAN(y);
965a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        do {
966a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColor c = *src++;
967a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkPMColorAssert(c);
968a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            SkASSERT(SkGetPackedA32(c) == 255);
969a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
970a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            unsigned dither = DITHER_VALUE(x);
971a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            *dst++ = SkDitherRGB32To565(c, dither);
972a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            DITHER_INC_X(x);
973a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        } while (--count != 0);
974a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
975a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
976a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
977a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comvoid Color32_arm_neon(SkPMColor* dst, const SkPMColor* src, int count,
978a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                      SkPMColor color) {
979a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (count <= 0) {
980a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        return;
981a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
982a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
983a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (0 == color) {
984a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        if (src != dst) {
985a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            memcpy(dst, src, count * sizeof(SkPMColor));
986a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        }
987a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        return;
988a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
989a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
990a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    unsigned colorA = SkGetPackedA32(color);
991a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    if (255 == colorA) {
992a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        sk_memset32(dst, color, count);
993a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    } else {
994a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        unsigned scale = 256 - SkAlpha255To256(colorA);
995a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
996a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        if (count >= 8) {
997a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // at the end of this assembly, count will have been decremented
998a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // to a negative value. That is, if count mod 8 = x, it will be
999a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // -8 +x coming out.
1000a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            asm volatile (
1001a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                PLD128(src, 0)
1002a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1003a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vdup.32    q0, %[color]                \n\t"
1004a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1005a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                PLD128(src, 128)
1006a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1007a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // scale numerical interval [0-255], so load as 8 bits
1008a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vdup.8     d2, %[scale]                \n\t"
1009a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1010a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                PLD128(src, 256)
1011a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1012a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "subs       %[count], %[count], #8      \n\t"
1013a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1014a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                PLD128(src, 384)
1015a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1016a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "Loop_Color32:                          \n\t"
1017a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1018a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // load src color, 8 pixels, 4 64 bit registers
1019a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // (and increment src).
1020a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vld1.32    {d4-d7}, [%[src]]!          \n\t"
1021a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1022a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                PLD128(src, 384)
1023a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1024a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // multiply long by scale, 64 bits at a time,
1025a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // destination into a 128 bit register.
1026a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vmull.u8   q4, d4, d2                  \n\t"
1027a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vmull.u8   q5, d5, d2                  \n\t"
1028a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vmull.u8   q6, d6, d2                  \n\t"
1029a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vmull.u8   q7, d7, d2                  \n\t"
1030a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1031a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // shift the 128 bit registers, containing the 16
1032a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // bit scaled values back to 8 bits, narrowing the
1033a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // results to 64 bit registers.
1034a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vshrn.i16  d8, q4, #8                  \n\t"
1035a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vshrn.i16  d9, q5, #8                  \n\t"
1036a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vshrn.i16  d10, q6, #8                 \n\t"
1037a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vshrn.i16  d11, q7, #8                 \n\t"
1038a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1039a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // adding back the color, using 128 bit registers.
1040a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vadd.i8    q6, q4, q0                  \n\t"
1041a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vadd.i8    q7, q5, q0                  \n\t"
1042a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1043a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // store back the 8 calculated pixels (2 128 bit
1044a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                // registers), and increment dst.
1045a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "vst1.32    {d12-d15}, [%[dst]]!        \n\t"
1046a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1047a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "subs       %[count], %[count], #8      \n\t"
1048a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                "bge        Loop_Color32                \n\t"
1049a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                : [src] "+r" (src), [dst] "+r" (dst), [count] "+r" (count)
1050a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                : [color] "r" (color), [scale] "r" (scale)
1051a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                : "cc", "memory",
1052a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
1053a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                  "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15"
1054a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com                          );
1055a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // At this point, if we went through the inline assembly, count is
1056a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // a negative value:
1057a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // if the value is -8, there is no pixel left to process.
1058a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // if the value is -7, there is one pixel left to process
1059a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // ...
1060a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // And'ing it with 7 will give us the number of pixels
1061a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            // left to process.
1062a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            count = count & 0x7;
1063a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        }
1064a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1065a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        while (count > 0) {
1066a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            *dst = color + SkAlphaMulQ(*src, scale);
1067a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            src += 1;
1068a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            dst += 1;
1069a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com            count--;
1070a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com        }
1071a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    }
1072a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com}
1073a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1074a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com///////////////////////////////////////////////////////////////////////////////
1075a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1076a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comconst SkBlitRow::Proc sk_blitrow_platform_565_procs_arm_neon[] = {
1077a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    // no dither
1078a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    // NOTE: For the two functions below, we don't have a special version
1079a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    //       that assumes that each source pixel is opaque. But our S32A is
1080a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    //       still faster than the default, so use it.
1081a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32A_D565_Opaque_neon,  // really S32_D565_Opaque
1082a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32A_D565_Blend_neon,   // really S32_D565_Blend
1083a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32A_D565_Opaque_neon,
1084a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32A_D565_Blend_neon,
1085a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1086a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    // dither
1087a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32_D565_Opaque_Dither_neon,
1088a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32_D565_Blend_Dither_neon,
1089a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    S32A_D565_Opaque_Dither_neon,
1090a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32A_D565_Blend_Dither
1091a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com};
1092a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1093a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comconst SkBlitRow::Proc sk_blitrow_platform_4444_procs_arm_neon[] = {
1094a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    // no dither
1095a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32_D4444_Opaque,
1096a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32_D4444_Blend,
1097a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32A_D4444_Opaque,
1098a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32A_D4444_Blend,
1099a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1100a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    // dither
1101a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32_D4444_Opaque_Dither,
1102a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32_D4444_Blend_Dither,
1103a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32A_D4444_Opaque_Dither,
1104a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32A_D4444_Blend_Dither
1105a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com};
1106a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com
1107a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.comconst SkBlitRow::Proc32 sk_blitrow_platform_32_procs_arm_neon[] = {
1108a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com    NULL,   // S32_Opaque,
1109fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    S32_Blend_BlitRow32_neon,        // S32_Blend,
1110fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    S32A_Opaque_BlitRow32_neon,        // S32A_Opaque,
1111fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    S32A_Blend_BlitRow32_arm        // S32A_Blend
1112a8dd1ce930811a51cc841f583424d507d95e7e78digit@google.com};
1113