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