blob: 1ae91b9c0e513084ab68407763d00a903c08e542 [file] [log] [blame]
DRCac4daa72015-01-10 22:56:26 +00001/*
2 * AltiVec optimizations for libjpeg-turbo
3 *
4 * Copyright (C) 2015, D. R. Commander.
5 * All rights reserved.
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
9 *
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
13 *
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22
23/* This file is included by jdcolor-altivec.c */
24
25
26void jsimd_ycc_rgb_convert_altivec (JDIMENSION out_width, JSAMPIMAGE input_buf,
27 JDIMENSION input_row,
28 JSAMPARRAY output_buf, int num_rows)
29{
30 JSAMPROW outptr, inptr0, inptr1, inptr2;
DRC771ab192015-02-20 19:57:21 +000031 int pitch = out_width * RGB_PIXELSIZE, num_cols;
32#if __BIG_ENDIAN__
33 int offset;
34#endif
DRCac4daa72015-01-10 22:56:26 +000035 unsigned char __attribute__((aligned(16))) tmpbuf[RGB_PIXELSIZE * 16];
36
37 __vector unsigned char rgb0, rgb1, rgb2, rgbx0, rgbx1, rgbx2, rgbx3,
DRC771ab192015-02-20 19:57:21 +000038 y, cb, cr;
39#if __BIG_ENDIAN__
40 __vector unsigned char edgel, edgeh, edges, out0, out1, out2, out3;
DRCac4daa72015-01-10 22:56:26 +000041#if RGB_PIXELSIZE == 4
DRC771ab192015-02-20 19:57:21 +000042 __vector unsigned char out4;
43#endif
44#endif
45#if RGB_PIXELSIZE == 4
46 __vector unsigned char rgb3;
DRCac4daa72015-01-10 22:56:26 +000047#endif
48 __vector short rg0, rg1, rg2, rg3, bx0, bx1, bx2, bx3, yl, yh, cbl, cbh,
DRCa6a24c22015-01-13 10:00:12 +000049 crl, crh, rl, rh, gl, gh, bl, bh, g0w, g1w, g2w, g3w;
DRCac4daa72015-01-10 22:56:26 +000050 __vector int g0, g1, g2, g3;
51
52 /* Constants
53 * NOTE: The >> 1 is to compensate for the fact that vec_madds() returns 17
54 * high-order bits, not 16.
55 */
56 __vector short pw_f0402 = { __8X(F_0_402 >> 1) },
57 pw_mf0228 = { __8X(-F_0_228 >> 1) },
58 pw_mf0344_f0285 = { __4X2(-F_0_344, F_0_285) },
DRCa6a24c22015-01-13 10:00:12 +000059 pw_one = { __8X(1) }, pw_255 = { __8X(255) },
DRCac4daa72015-01-10 22:56:26 +000060 pw_cj = { __8X(CENTERJSAMPLE) };
61 __vector int pd_onehalf = { __4X(ONE_HALF) };
DRCa6a24c22015-01-13 10:00:12 +000062 __vector unsigned char pb_zero = { __16X(0) },
DRC771ab192015-02-20 19:57:21 +000063#if __BIG_ENDIAN__
DRC406bb012015-01-13 11:47:20 +000064 shift_pack_index = {0,1,4,5,8,9,12,13,16,17,20,21,24,25,28,29};
DRC771ab192015-02-20 19:57:21 +000065#else
66 shift_pack_index = {2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31};
67#endif
DRCac4daa72015-01-10 22:56:26 +000068
69 while (--num_rows >= 0) {
70 inptr0 = input_buf[0][input_row];
71 inptr1 = input_buf[1][input_row];
72 inptr2 = input_buf[2][input_row];
73 input_row++;
74 outptr = *output_buf++;
75
76 for (num_cols = pitch; num_cols > 0;
77 num_cols -= RGB_PIXELSIZE * 16, outptr += RGB_PIXELSIZE * 16,
78 inptr0 += 16, inptr1 += 16, inptr2 += 16) {
79
80 y = vec_ld(0, inptr0);
81 /* NOTE: We have to use vec_merge*() here because vec_unpack*() doesn't
82 * support unsigned vectors.
83 */
DRC771ab192015-02-20 19:57:21 +000084 yl = (__vector signed short)VEC_UNPACKHU(y);
85 yh = (__vector signed short)VEC_UNPACKLU(y);
DRCac4daa72015-01-10 22:56:26 +000086
87 cb = vec_ld(0, inptr1);
DRC771ab192015-02-20 19:57:21 +000088 cbl = (__vector signed short)VEC_UNPACKHU(cb);
89 cbh = (__vector signed short)VEC_UNPACKLU(cb);
DRCac4daa72015-01-10 22:56:26 +000090 cbl = vec_sub(cbl, pw_cj);
91 cbh = vec_sub(cbh, pw_cj);
92
93 cr = vec_ld(0, inptr2);
DRC771ab192015-02-20 19:57:21 +000094 crl = (__vector signed short)VEC_UNPACKHU(cr);
95 crh = (__vector signed short)VEC_UNPACKLU(cr);
DRCac4daa72015-01-10 22:56:26 +000096 crl = vec_sub(crl, pw_cj);
97 crh = vec_sub(crh, pw_cj);
98
99 /* (Original)
100 * R = Y + 1.40200 * Cr
101 * G = Y - 0.34414 * Cb - 0.71414 * Cr
102 * B = Y + 1.77200 * Cb
103 *
104 * (This implementation)
105 * R = Y + 0.40200 * Cr + Cr
106 * G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr
107 * B = Y - 0.22800 * Cb + Cb + Cb
108 */
109 bl = vec_add(cbl, cbl);
110 bh = vec_add(cbh, cbh);
111 bl = vec_madds(bl, pw_mf0228, pw_one);
112 bh = vec_madds(bh, pw_mf0228, pw_one);
113 bl = vec_sra(bl, (__vector unsigned short)pw_one);
114 bh = vec_sra(bh, (__vector unsigned short)pw_one);
115 bl = vec_add(bl, cbl);
116 bh = vec_add(bh, cbh);
117 bl = vec_add(bl, cbl);
118 bh = vec_add(bh, cbh);
119 bl = vec_add(bl, yl);
120 bh = vec_add(bh, yh);
121
122 rl = vec_add(crl, crl);
123 rh = vec_add(crh, crh);
124 rl = vec_madds(rl, pw_f0402, pw_one);
125 rh = vec_madds(rh, pw_f0402, pw_one);
126 rl = vec_sra(rl, (__vector unsigned short)pw_one);
127 rh = vec_sra(rh, (__vector unsigned short)pw_one);
128 rl = vec_add(rl, crl);
129 rh = vec_add(rh, crh);
130 rl = vec_add(rl, yl);
131 rh = vec_add(rh, yh);
132
DRCa6a24c22015-01-13 10:00:12 +0000133 g0w = vec_mergeh(cbl, crl);
134 g1w = vec_mergel(cbl, crl);
135 g0 = vec_msums(g0w, pw_mf0344_f0285, pd_onehalf);
136 g1 = vec_msums(g1w, pw_mf0344_f0285, pd_onehalf);
137 g2w = vec_mergeh(cbh, crh);
138 g3w = vec_mergel(cbh, crh);
139 g2 = vec_msums(g2w, pw_mf0344_f0285, pd_onehalf);
140 g3 = vec_msums(g3w, pw_mf0344_f0285, pd_onehalf);
DRCac4daa72015-01-10 22:56:26 +0000141 /* Clever way to avoid 4 shifts + 2 packs. This packs the high word from
142 * each dword into a new 16-bit vector, which is the equivalent of
143 * descaling the 32-bit results (right-shifting by 16 bits) and then
144 * packing them.
145 */
DRC51eba062015-01-11 07:38:35 +0000146 gl = vec_perm((__vector short)g0, (__vector short)g1, shift_pack_index);
147 gh = vec_perm((__vector short)g2, (__vector short)g3, shift_pack_index);
DRCac4daa72015-01-10 22:56:26 +0000148 gl = vec_sub(gl, crl);
149 gh = vec_sub(gh, crh);
150 gl = vec_add(gl, yl);
151 gh = vec_add(gh, yh);
152
153 rg0 = vec_mergeh(rl, gl);
154 bx0 = vec_mergeh(bl, pw_255);
155 rg1 = vec_mergel(rl, gl);
156 bx1 = vec_mergel(bl, pw_255);
157 rg2 = vec_mergeh(rh, gh);
158 bx2 = vec_mergeh(bh, pw_255);
159 rg3 = vec_mergel(rh, gh);
160 bx3 = vec_mergel(bh, pw_255);
161
162 rgbx0 = vec_packsu(rg0, bx0);
163 rgbx1 = vec_packsu(rg1, bx1);
164 rgbx2 = vec_packsu(rg2, bx2);
165 rgbx3 = vec_packsu(rg3, bx3);
166
167#if RGB_PIXELSIZE == 3
168 /* rgbx0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 X0 B1 X1 B2 X2 B3 X3
169 * rgbx1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 X4 B5 X5 B6 X6 B7 X7
170 * rgbx2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 X8 B9 X9 Ba Xa Bb Xb
171 * rgbx3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Xc Bd Xd Be Xe Bf Xf
172 *
173 * rgb0 = R0 G0 B0 R1 G1 B1 R2 G2 B2 R3 G3 B3 R4 G4 B4 R5
174 * rgb1 = G5 B5 R6 G6 B6 R7 G7 B7 R8 G8 B8 R9 G9 B9 Ra Ga
175 * rgb2 = Ba Rb Gb Bb Rc Gc Bc Rd Gd Bd Re Ge Be Rf Gf Bf
176 */
177 rgb0 = vec_perm(rgbx0, rgbx1, (__vector unsigned char)RGB_INDEX0);
178 rgb1 = vec_perm(rgbx1, rgbx2, (__vector unsigned char)RGB_INDEX1);
179 rgb2 = vec_perm(rgbx2, rgbx3, (__vector unsigned char)RGB_INDEX2);
180#else
181 /* rgbx0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 X0 B1 X1 B2 X2 B3 X3
182 * rgbx1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 X4 B5 X5 B6 X6 B7 X7
183 * rgbx2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 X8 B9 X9 Ba Xa Bb Xb
184 * rgbx3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Xc Bd Xd Be Xe Bf Xf
185 *
186 * rgb0 = R0 G0 B0 X0 R1 G1 B1 X1 R2 G2 B2 X2 R3 G3 B3 X3
187 * rgb1 = R4 G4 B4 X4 R5 G5 B5 X5 R6 G6 B6 X6 R7 G7 B7 X7
188 * rgb2 = R8 G8 B8 X8 R9 G9 B9 X9 Ra Ga Ba Xa Rb Gb Bb Xb
189 * rgb3 = Rc Gc Bc Xc Rd Gd Bd Xd Re Ge Be Xe Rf Gf Bf Xf
190 */
191 rgb0 = vec_perm(rgbx0, rgbx0, (__vector unsigned char)RGB_INDEX);
192 rgb1 = vec_perm(rgbx1, rgbx1, (__vector unsigned char)RGB_INDEX);
193 rgb2 = vec_perm(rgbx2, rgbx2, (__vector unsigned char)RGB_INDEX);
194 rgb3 = vec_perm(rgbx3, rgbx3, (__vector unsigned char)RGB_INDEX);
195#endif
196
DRC771ab192015-02-20 19:57:21 +0000197#if __BIG_ENDIAN__
DRCac4daa72015-01-10 22:56:26 +0000198 offset = (size_t)outptr & 15;
199 if (offset) {
200 __vector unsigned char unaligned_shift_index;
201 int bytes = num_cols + offset;
202
203 if (bytes < (RGB_PIXELSIZE + 1) * 16 && (bytes & 15)) {
204 /* Slow path to prevent buffer overwrite. Since there is no way to
205 * write a partial AltiVec register, overwrite would occur on the
206 * last chunk of the last image row if the right edge is not on a
207 * 16-byte boundary. It could also occur on other rows if the bytes
208 * per row is low enough. Since we can't determine whether we're on
209 * the last image row, we have to assume every row is the last.
210 */
211 vec_st(rgb0, 0, tmpbuf);
212 vec_st(rgb1, 16, tmpbuf);
213 vec_st(rgb2, 32, tmpbuf);
214#if RGB_PIXELSIZE == 4
215 vec_st(rgb3, 48, tmpbuf);
216#endif
217 memcpy(outptr, tmpbuf, min(num_cols, RGB_PIXELSIZE * 16));
218 } else {
219 /* Fast path */
220 unaligned_shift_index = vec_lvsl(0, outptr);
221 edgel = vec_ld(0, outptr);
DRC11c40102015-01-14 13:07:06 +0000222 edgeh = vec_ld(min(num_cols - 1, RGB_PIXELSIZE * 16), outptr);
DRCac4daa72015-01-10 22:56:26 +0000223 edges = vec_perm(edgeh, edgel, unaligned_shift_index);
224 unaligned_shift_index = vec_lvsr(0, outptr);
225 out0 = vec_perm(edges, rgb0, unaligned_shift_index);
226 out1 = vec_perm(rgb0, rgb1, unaligned_shift_index);
227 out2 = vec_perm(rgb1, rgb2, unaligned_shift_index);
228#if RGB_PIXELSIZE == 4
229 out3 = vec_perm(rgb2, rgb3, unaligned_shift_index);
230 out4 = vec_perm(rgb3, edges, unaligned_shift_index);
231#else
232 out3 = vec_perm(rgb2, edges, unaligned_shift_index);
233#endif
234 vec_st(out0, 0, outptr);
235 if (bytes > 16)
236 vec_st(out1, 16, outptr);
237 if (bytes > 32)
238 vec_st(out2, 32, outptr);
239 if (bytes > 48)
240 vec_st(out3, 48, outptr);
241#if RGB_PIXELSIZE == 4
242 if (bytes > 64)
243 vec_st(out4, 64, outptr);
244#endif
245 }
246 } else {
DRC771ab192015-02-20 19:57:21 +0000247#endif /* __BIG_ENDIAN__ */
DRCac4daa72015-01-10 22:56:26 +0000248 if (num_cols < RGB_PIXELSIZE * 16 && (num_cols & 15)) {
249 /* Slow path */
DRC771ab192015-02-20 19:57:21 +0000250 VEC_ST(rgb0, 0, tmpbuf);
251 VEC_ST(rgb1, 16, tmpbuf);
252 VEC_ST(rgb2, 32, tmpbuf);
DRCac4daa72015-01-10 22:56:26 +0000253#if RGB_PIXELSIZE == 4
DRC771ab192015-02-20 19:57:21 +0000254 VEC_ST(rgb3, 48, tmpbuf);
DRCac4daa72015-01-10 22:56:26 +0000255#endif
256 memcpy(outptr, tmpbuf, min(num_cols, RGB_PIXELSIZE * 16));
257 } else {
258 /* Fast path */
DRC771ab192015-02-20 19:57:21 +0000259 VEC_ST(rgb0, 0, outptr);
DRCac4daa72015-01-10 22:56:26 +0000260 if (num_cols > 16)
DRC771ab192015-02-20 19:57:21 +0000261 VEC_ST(rgb1, 16, outptr);
DRCac4daa72015-01-10 22:56:26 +0000262 if (num_cols > 32)
DRC771ab192015-02-20 19:57:21 +0000263 VEC_ST(rgb2, 32, outptr);
DRCac4daa72015-01-10 22:56:26 +0000264#if RGB_PIXELSIZE == 4
265 if (num_cols > 48)
DRC771ab192015-02-20 19:57:21 +0000266 VEC_ST(rgb3, 48, outptr);
DRCac4daa72015-01-10 22:56:26 +0000267#endif
268 }
DRC771ab192015-02-20 19:57:21 +0000269#if __BIG_ENDIAN__
DRCac4daa72015-01-10 22:56:26 +0000270 }
DRC771ab192015-02-20 19:57:21 +0000271#endif
DRCac4daa72015-01-10 22:56:26 +0000272 }
273 }
274}