1f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell/* libs/pixelflinger/col32cb16blend_neon.S
2ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher *
3ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * Copyright (C) 2009 The Android Open Source Project
4ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher *
5ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * Licensed under the Apache License, Version 2.0 (the "License");
6ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * you may not use this file except in compliance with the License.
7ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * You may obtain a copy of the License at
8ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher *
9ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher *      http://www.apache.org/licenses/LICENSE-2.0
10ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher *
11ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * Unless required by applicable law or agreed to in writing, software
12ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * distributed under the License is distributed on an "AS IS" BASIS,
13ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * See the License for the specific language governing permissions and
15ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher * limitations under the License.
16ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher */
17ef18202fd1530e5c3106d6d3fc484a68857d3cb0Dave Butcher
18f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
19f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    .text
20f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    .align
21f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
22f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    .global scanline_col32cb16blend_neon
23f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
24f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//
25f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// This function alpha blends a fixed color into a destination scanline, using
26f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// the formula:
27f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//
28f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//     d = s + (((a + (a >> 7)) * d) >> 8)
29f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//
30f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// where d is the destination pixel,
31f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//       s is the source color,
32f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//       a is the alpha channel of the source color.
33f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//
34f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// The NEON implementation processes 16 pixels per iteration. The remaining 0 - 15
35f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// pixels are processed in ARM code.
36f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell//
37f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
38f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// r0 = destination buffer pointer
39f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// r1 = color pointer
40f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell// r2 = count
41f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
42f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
43f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewellscanline_col32cb16blend_neon:
44f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    push        {r4-r11, lr}                    // stack ARM regs
45f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
46f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmov.u16    q15, #256                       // create alpha constant
47f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    movs        r3, r2, lsr #4                  // calc. sixteens iterations
48f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmov.u16    q14, #0x1f                      // create blue mask
49f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
50f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    beq         2f                              // if r3 == 0, branch to singles
51f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
52f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vld4.8      {d0[], d2[], d4[], d6[]}, [r1]  // load color into four registers
53f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                //  split and duplicate them, such that
54f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                //  d0 = 8 equal red values
55f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                //  d2 = 8 equal green values
56f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                //  d4 = 8 equal blue values
57f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                //  d6 = 8 equal alpha values
58f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshll.u8    q0, d0, #5                      // shift up red and widen
59f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshll.u8    q1, d2, #6                      // shift up green and widen
60f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshll.u8    q2, d4, #5                      // shift up blue and widen
61f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
62f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u8     d7, d6, #7                      // extract top bit of alpha
63f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vaddl.u8    q3, d6, d7                      // add top bit into alpha
64f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vsub.u16    q3, q15, q3                     // invert alpha
65f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
66f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell1:
67f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    // This loop processes 16 pixels per iteration. In the comments, references to
68f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    // the first eight pixels are suffixed with "0" (red0, green0, blue0),
69f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    // the second eight are suffixed "1".
70f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                // q8  = dst red0
71f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                // q9  = dst green0
72f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                // q10 = dst blue0
73f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                // q13 = dst red1
74f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                // q12 = dst green1
75f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell                                                // q11 = dst blue1
76f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
77f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vld1.16     {d20, d21, d22, d23}, [r0]      // load 16 dest pixels
78f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q8, q10, #11                    // shift dst red0 to low 5 bits
79f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    pld         [r0, #63]                       // preload next dest pixels
80f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshl.u16    q9, q10, #5                     // shift dst green0 to top 6 bits
81f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vand        q10, q10, q14                   // extract dst blue0
82f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q9, q9, #10                     // shift dst green0 to low 6 bits
83f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmul.u16    q8, q8, q3                      // multiply dst red0 by src alpha
84f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshl.u16    q12, q11, #5                    // shift dst green1 to top 6 bits
85f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmul.u16    q9, q9, q3                      // multiply dst green0 by src alpha
86f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q13, q11, #11                   // shift dst red1 to low 5 bits
87f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmul.u16    q10, q10, q3                    // multiply dst blue0 by src alpha
88f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q12, q12, #10                   // shift dst green1 to low 6 bits
89f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vand        q11, q11, q14                   // extract dst blue1
90f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vadd.u16    q8, q8, q0                      // add src red to dst red0
91f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmul.u16    q13, q13, q3                    // multiply dst red1 by src alpha
92f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vadd.u16    q9, q9, q1                      // add src green to dst green0
93f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmul.u16    q12, q12, q3                    // multiply dst green1 by src alpha
94f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vadd.u16    q10, q10, q2                    // add src blue to dst blue0
95f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vmul.u16    q11, q11, q3                    // multiply dst blue1 by src alpha
96f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q8, q8, #8                      // shift down red0
97f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vadd.u16    q13, q13, q0                    // add src red to dst red1
98f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q9, q9, #8                      // shift down green0
99f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vadd.u16    q12, q12, q1                    // add src green to dst green1
100f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q10, q10, #8                    // shift down blue0
101f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vadd.u16    q11, q11, q2                    // add src blue to dst blue1
102f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vsli.u16    q10, q9, #5                     // shift & insert green0 into blue0
103f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q13, q13, #8                    // shift down red1
104f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vsli.u16    q10, q8, #11                    // shift & insert red0 into blue0
105f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q12, q12, #8                    // shift down green1
106f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vshr.u16    q11, q11, #8                    // shift down blue1
107f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    subs        r3, r3, #1                      // decrement loop counter
108f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vsli.u16    q11, q12, #5                    // shift & insert green1 into blue1
109f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vsli.u16    q11, q13, #11                   // shift & insert red1 into blue1
110f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
111f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    vst1.16     {d20, d21, d22, d23}, [r0]!     // write 16 pixels back to dst
112f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    bne         1b                              // if count != 0, loop
113f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
114f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell2:
115f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    ands        r3, r2, #15                     // calc. single iterations
116f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    beq         4f                              // if r3 == 0, exit
117f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
118f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    ldr         r4, [r1]                        // load source color
119f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r5, r4, lsr #24                 // shift down alpha
120f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    add         r5, r5, r5, lsr #7              // add in top bit
121f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    rsb         r5, r5, #256                    // invert alpha
122f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    and         r11, r4, #0xff                  // extract red
123f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    ubfx        r12, r4, #8, #8                 // extract green
124f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    ubfx        r4, r4, #16, #8                 // extract blue
125f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r11, r11, lsl #5                // prescale red
126f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r12, r12, lsl #6                // prescale green
127f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r4, r4, lsl #5                  // prescale blue
128f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
129f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell3:
130f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    ldrh        r8, [r0]                        // load dest pixel
131f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    subs        r3, r3, #1                      // decrement loop counter
132f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r6, r8, lsr #11                 // extract dest red
133f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    ubfx        r7, r8, #5, #6                  // extract dest green
134f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    and         r8, r8, #0x1f                   // extract dest blue
135f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
136f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    smlabb      r6, r6, r5, r11                 // dest red * alpha + src red
137f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    smlabb      r7, r7, r5, r12                 // dest green * alpha + src green
138f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    smlabb      r8, r8, r5, r4                  // dest blue * alpha + src blue
139f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
140f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r6, r6, lsr #8                  // shift down red
141f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r7, r7, lsr #8                  // shift down green
142f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    mov         r6, r6, lsl #11                 // shift red into 565
143f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    orr         r6, r7, lsl #5                  // shift green into 565
144f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    orr         r6, r8, lsr #8                  // shift blue into 565
145f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
146f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    strh        r6, [r0], #2                    // store pixel to dest, update ptr
147f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    bne         3b                              // if count != 0, loop
148f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell4:
149f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
150f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell    pop         {r4-r11, pc}                    // return
151f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
152f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell
153f9e8ab03bd93d98567e96822535090a877594abaMartyn Capewell