DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | void 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; |
| 31 | int pitch = out_width * RGB_PIXELSIZE, offset, num_cols; |
| 32 | unsigned char __attribute__((aligned(16))) tmpbuf[RGB_PIXELSIZE * 16]; |
| 33 | |
| 34 | __vector unsigned char rgb0, rgb1, rgb2, rgbx0, rgbx1, rgbx2, rgbx3, |
| 35 | y, cb, cr, edgel, edgeh, edges, out0, out1, out2, out3; |
| 36 | #if RGB_PIXELSIZE == 4 |
| 37 | __vector unsigned char rgb3, out4; |
| 38 | #endif |
| 39 | __vector short rg0, rg1, rg2, rg3, bx0, bx1, bx2, bx3, yl, yh, cbl, cbh, |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 40 | crl, crh, rl, rh, gl, gh, bl, bh, g0w, g1w, g2w, g3w; |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 41 | __vector int g0, g1, g2, g3; |
| 42 | |
| 43 | /* Constants |
| 44 | * NOTE: The >> 1 is to compensate for the fact that vec_madds() returns 17 |
| 45 | * high-order bits, not 16. |
| 46 | */ |
| 47 | __vector short pw_f0402 = { __8X(F_0_402 >> 1) }, |
| 48 | pw_mf0228 = { __8X(-F_0_228 >> 1) }, |
| 49 | pw_mf0344_f0285 = { __4X2(-F_0_344, F_0_285) }, |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 50 | pw_one = { __8X(1) }, pw_255 = { __8X(255) }, |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 51 | pw_cj = { __8X(CENTERJSAMPLE) }; |
| 52 | __vector int pd_onehalf = { __4X(ONE_HALF) }; |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 53 | __vector unsigned char pb_zero = { __16X(0) }, |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 54 | shift_pack_index = |
| 55 | { 0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29}; |
| 56 | |
| 57 | while (--num_rows >= 0) { |
| 58 | inptr0 = input_buf[0][input_row]; |
| 59 | inptr1 = input_buf[1][input_row]; |
| 60 | inptr2 = input_buf[2][input_row]; |
| 61 | input_row++; |
| 62 | outptr = *output_buf++; |
| 63 | |
| 64 | for (num_cols = pitch; num_cols > 0; |
| 65 | num_cols -= RGB_PIXELSIZE * 16, outptr += RGB_PIXELSIZE * 16, |
| 66 | inptr0 += 16, inptr1 += 16, inptr2 += 16) { |
| 67 | |
| 68 | y = vec_ld(0, inptr0); |
| 69 | /* NOTE: We have to use vec_merge*() here because vec_unpack*() doesn't |
| 70 | * support unsigned vectors. |
| 71 | */ |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 72 | yl = (__vector signed short)vec_mergeh(pb_zero, y); |
| 73 | yh = (__vector signed short)vec_mergel(pb_zero, y); |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 74 | |
| 75 | cb = vec_ld(0, inptr1); |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 76 | cbl = (__vector signed short)vec_mergeh(pb_zero, cb); |
| 77 | cbh = (__vector signed short)vec_mergel(pb_zero, cb); |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 78 | cbl = vec_sub(cbl, pw_cj); |
| 79 | cbh = vec_sub(cbh, pw_cj); |
| 80 | |
| 81 | cr = vec_ld(0, inptr2); |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 82 | crl = (__vector signed short)vec_mergeh(pb_zero, cr); |
| 83 | crh = (__vector signed short)vec_mergel(pb_zero, cr); |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 84 | crl = vec_sub(crl, pw_cj); |
| 85 | crh = vec_sub(crh, pw_cj); |
| 86 | |
| 87 | /* (Original) |
| 88 | * R = Y + 1.40200 * Cr |
| 89 | * G = Y - 0.34414 * Cb - 0.71414 * Cr |
| 90 | * B = Y + 1.77200 * Cb |
| 91 | * |
| 92 | * (This implementation) |
| 93 | * R = Y + 0.40200 * Cr + Cr |
| 94 | * G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr |
| 95 | * B = Y - 0.22800 * Cb + Cb + Cb |
| 96 | */ |
| 97 | bl = vec_add(cbl, cbl); |
| 98 | bh = vec_add(cbh, cbh); |
| 99 | bl = vec_madds(bl, pw_mf0228, pw_one); |
| 100 | bh = vec_madds(bh, pw_mf0228, pw_one); |
| 101 | bl = vec_sra(bl, (__vector unsigned short)pw_one); |
| 102 | bh = vec_sra(bh, (__vector unsigned short)pw_one); |
| 103 | bl = vec_add(bl, cbl); |
| 104 | bh = vec_add(bh, cbh); |
| 105 | bl = vec_add(bl, cbl); |
| 106 | bh = vec_add(bh, cbh); |
| 107 | bl = vec_add(bl, yl); |
| 108 | bh = vec_add(bh, yh); |
| 109 | |
| 110 | rl = vec_add(crl, crl); |
| 111 | rh = vec_add(crh, crh); |
| 112 | rl = vec_madds(rl, pw_f0402, pw_one); |
| 113 | rh = vec_madds(rh, pw_f0402, pw_one); |
| 114 | rl = vec_sra(rl, (__vector unsigned short)pw_one); |
| 115 | rh = vec_sra(rh, (__vector unsigned short)pw_one); |
| 116 | rl = vec_add(rl, crl); |
| 117 | rh = vec_add(rh, crh); |
| 118 | rl = vec_add(rl, yl); |
| 119 | rh = vec_add(rh, yh); |
| 120 | |
DRC | a6a24c2 | 2015-01-13 10:00:12 +0000 | [diff] [blame^] | 121 | g0w = vec_mergeh(cbl, crl); |
| 122 | g1w = vec_mergel(cbl, crl); |
| 123 | g0 = vec_msums(g0w, pw_mf0344_f0285, pd_onehalf); |
| 124 | g1 = vec_msums(g1w, pw_mf0344_f0285, pd_onehalf); |
| 125 | g2w = vec_mergeh(cbh, crh); |
| 126 | g3w = vec_mergel(cbh, crh); |
| 127 | g2 = vec_msums(g2w, pw_mf0344_f0285, pd_onehalf); |
| 128 | g3 = vec_msums(g3w, pw_mf0344_f0285, pd_onehalf); |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 129 | /* Clever way to avoid 4 shifts + 2 packs. This packs the high word from |
| 130 | * each dword into a new 16-bit vector, which is the equivalent of |
| 131 | * descaling the 32-bit results (right-shifting by 16 bits) and then |
| 132 | * packing them. |
| 133 | */ |
DRC | 51eba06 | 2015-01-11 07:38:35 +0000 | [diff] [blame] | 134 | gl = vec_perm((__vector short)g0, (__vector short)g1, shift_pack_index); |
| 135 | gh = vec_perm((__vector short)g2, (__vector short)g3, shift_pack_index); |
DRC | ac4daa7 | 2015-01-10 22:56:26 +0000 | [diff] [blame] | 136 | gl = vec_sub(gl, crl); |
| 137 | gh = vec_sub(gh, crh); |
| 138 | gl = vec_add(gl, yl); |
| 139 | gh = vec_add(gh, yh); |
| 140 | |
| 141 | rg0 = vec_mergeh(rl, gl); |
| 142 | bx0 = vec_mergeh(bl, pw_255); |
| 143 | rg1 = vec_mergel(rl, gl); |
| 144 | bx1 = vec_mergel(bl, pw_255); |
| 145 | rg2 = vec_mergeh(rh, gh); |
| 146 | bx2 = vec_mergeh(bh, pw_255); |
| 147 | rg3 = vec_mergel(rh, gh); |
| 148 | bx3 = vec_mergel(bh, pw_255); |
| 149 | |
| 150 | rgbx0 = vec_packsu(rg0, bx0); |
| 151 | rgbx1 = vec_packsu(rg1, bx1); |
| 152 | rgbx2 = vec_packsu(rg2, bx2); |
| 153 | rgbx3 = vec_packsu(rg3, bx3); |
| 154 | |
| 155 | #if RGB_PIXELSIZE == 3 |
| 156 | /* rgbx0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 X0 B1 X1 B2 X2 B3 X3 |
| 157 | * rgbx1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 X4 B5 X5 B6 X6 B7 X7 |
| 158 | * rgbx2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 X8 B9 X9 Ba Xa Bb Xb |
| 159 | * rgbx3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Xc Bd Xd Be Xe Bf Xf |
| 160 | * |
| 161 | * rgb0 = R0 G0 B0 R1 G1 B1 R2 G2 B2 R3 G3 B3 R4 G4 B4 R5 |
| 162 | * rgb1 = G5 B5 R6 G6 B6 R7 G7 B7 R8 G8 B8 R9 G9 B9 Ra Ga |
| 163 | * rgb2 = Ba Rb Gb Bb Rc Gc Bc Rd Gd Bd Re Ge Be Rf Gf Bf |
| 164 | */ |
| 165 | rgb0 = vec_perm(rgbx0, rgbx1, (__vector unsigned char)RGB_INDEX0); |
| 166 | rgb1 = vec_perm(rgbx1, rgbx2, (__vector unsigned char)RGB_INDEX1); |
| 167 | rgb2 = vec_perm(rgbx2, rgbx3, (__vector unsigned char)RGB_INDEX2); |
| 168 | #else |
| 169 | /* rgbx0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 X0 B1 X1 B2 X2 B3 X3 |
| 170 | * rgbx1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 X4 B5 X5 B6 X6 B7 X7 |
| 171 | * rgbx2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 X8 B9 X9 Ba Xa Bb Xb |
| 172 | * rgbx3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Xc Bd Xd Be Xe Bf Xf |
| 173 | * |
| 174 | * rgb0 = R0 G0 B0 X0 R1 G1 B1 X1 R2 G2 B2 X2 R3 G3 B3 X3 |
| 175 | * rgb1 = R4 G4 B4 X4 R5 G5 B5 X5 R6 G6 B6 X6 R7 G7 B7 X7 |
| 176 | * rgb2 = R8 G8 B8 X8 R9 G9 B9 X9 Ra Ga Ba Xa Rb Gb Bb Xb |
| 177 | * rgb3 = Rc Gc Bc Xc Rd Gd Bd Xd Re Ge Be Xe Rf Gf Bf Xf |
| 178 | */ |
| 179 | rgb0 = vec_perm(rgbx0, rgbx0, (__vector unsigned char)RGB_INDEX); |
| 180 | rgb1 = vec_perm(rgbx1, rgbx1, (__vector unsigned char)RGB_INDEX); |
| 181 | rgb2 = vec_perm(rgbx2, rgbx2, (__vector unsigned char)RGB_INDEX); |
| 182 | rgb3 = vec_perm(rgbx3, rgbx3, (__vector unsigned char)RGB_INDEX); |
| 183 | #endif |
| 184 | |
| 185 | offset = (size_t)outptr & 15; |
| 186 | if (offset) { |
| 187 | __vector unsigned char unaligned_shift_index; |
| 188 | int bytes = num_cols + offset; |
| 189 | |
| 190 | if (bytes < (RGB_PIXELSIZE + 1) * 16 && (bytes & 15)) { |
| 191 | /* Slow path to prevent buffer overwrite. Since there is no way to |
| 192 | * write a partial AltiVec register, overwrite would occur on the |
| 193 | * last chunk of the last image row if the right edge is not on a |
| 194 | * 16-byte boundary. It could also occur on other rows if the bytes |
| 195 | * per row is low enough. Since we can't determine whether we're on |
| 196 | * the last image row, we have to assume every row is the last. |
| 197 | */ |
| 198 | vec_st(rgb0, 0, tmpbuf); |
| 199 | vec_st(rgb1, 16, tmpbuf); |
| 200 | vec_st(rgb2, 32, tmpbuf); |
| 201 | #if RGB_PIXELSIZE == 4 |
| 202 | vec_st(rgb3, 48, tmpbuf); |
| 203 | #endif |
| 204 | memcpy(outptr, tmpbuf, min(num_cols, RGB_PIXELSIZE * 16)); |
| 205 | } else { |
| 206 | /* Fast path */ |
| 207 | unaligned_shift_index = vec_lvsl(0, outptr); |
| 208 | edgel = vec_ld(0, outptr); |
| 209 | edgeh = vec_ld(min(num_cols, RGB_PIXELSIZE * 16) + offset, outptr); |
| 210 | edges = vec_perm(edgeh, edgel, unaligned_shift_index); |
| 211 | unaligned_shift_index = vec_lvsr(0, outptr); |
| 212 | out0 = vec_perm(edges, rgb0, unaligned_shift_index); |
| 213 | out1 = vec_perm(rgb0, rgb1, unaligned_shift_index); |
| 214 | out2 = vec_perm(rgb1, rgb2, unaligned_shift_index); |
| 215 | #if RGB_PIXELSIZE == 4 |
| 216 | out3 = vec_perm(rgb2, rgb3, unaligned_shift_index); |
| 217 | out4 = vec_perm(rgb3, edges, unaligned_shift_index); |
| 218 | #else |
| 219 | out3 = vec_perm(rgb2, edges, unaligned_shift_index); |
| 220 | #endif |
| 221 | vec_st(out0, 0, outptr); |
| 222 | if (bytes > 16) |
| 223 | vec_st(out1, 16, outptr); |
| 224 | if (bytes > 32) |
| 225 | vec_st(out2, 32, outptr); |
| 226 | if (bytes > 48) |
| 227 | vec_st(out3, 48, outptr); |
| 228 | #if RGB_PIXELSIZE == 4 |
| 229 | if (bytes > 64) |
| 230 | vec_st(out4, 64, outptr); |
| 231 | #endif |
| 232 | } |
| 233 | } else { |
| 234 | if (num_cols < RGB_PIXELSIZE * 16 && (num_cols & 15)) { |
| 235 | /* Slow path */ |
| 236 | vec_st(rgb0, 0, tmpbuf); |
| 237 | vec_st(rgb1, 16, tmpbuf); |
| 238 | vec_st(rgb2, 32, tmpbuf); |
| 239 | #if RGB_PIXELSIZE == 4 |
| 240 | vec_st(rgb3, 48, tmpbuf); |
| 241 | #endif |
| 242 | memcpy(outptr, tmpbuf, min(num_cols, RGB_PIXELSIZE * 16)); |
| 243 | } else { |
| 244 | /* Fast path */ |
| 245 | vec_st(rgb0, 0, outptr); |
| 246 | if (num_cols > 16) |
| 247 | vec_st(rgb1, 16, outptr); |
| 248 | if (num_cols > 32) |
| 249 | vec_st(rgb2, 32, outptr); |
| 250 | #if RGB_PIXELSIZE == 4 |
| 251 | if (num_cols > 48) |
| 252 | vec_st(rgb3, 48, outptr); |
| 253 | #endif |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |