1; 2; jcgryext.asm - grayscale colorspace conversion (SSE2) 3; 4; Copyright (C) 2011, D. R. Commander. 5; 6; Based on the x86 SIMD extension for IJG JPEG library 7; Copyright (C) 1999-2006, MIYASAKA Masaru. 8; For conditions of distribution and use, see copyright notice in jsimdext.inc 9; 10; This file should be assembled with NASM (Netwide Assembler), 11; can *not* be assembled with Microsoft's MASM or any compatible 12; assembler (including Borland's Turbo Assembler). 13; NASM is available from http://nasm.sourceforge.net/ or 14; http://sourceforge.net/project/showfiles.php?group_id=6208 15; 16; [TAB8] 17 18%include "jcolsamp.inc" 19 20; -------------------------------------------------------------------------- 21; 22; Convert some rows of samples to the output colorspace. 23; 24; GLOBAL(void) 25; jsimd_rgb_gray_convert_sse2 (JDIMENSION img_width, 26; JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 27; JDIMENSION output_row, int num_rows); 28; 29 30%define img_width(b) (b)+8 ; JDIMENSION img_width 31%define input_buf(b) (b)+12 ; JSAMPARRAY input_buf 32%define output_buf(b) (b)+16 ; JSAMPIMAGE output_buf 33%define output_row(b) (b)+20 ; JDIMENSION output_row 34%define num_rows(b) (b)+24 ; int num_rows 35 36%define original_ebp ebp+0 37%define wk(i) ebp-(WK_NUM-(i))*SIZEOF_XMMWORD ; xmmword wk[WK_NUM] 38%define WK_NUM 2 39%define gotptr wk(0)-SIZEOF_POINTER ; void * gotptr 40 41 align 16 42 43 global EXTN(jsimd_rgb_gray_convert_sse2) 44 45EXTN(jsimd_rgb_gray_convert_sse2): 46 push ebp 47 mov eax,esp ; eax = original ebp 48 sub esp, byte 4 49 and esp, byte (-SIZEOF_XMMWORD) ; align to 128 bits 50 mov [esp],eax 51 mov ebp,esp ; ebp = aligned ebp 52 lea esp, [wk(0)] 53 pushpic eax ; make a room for GOT address 54 push ebx 55; push ecx ; need not be preserved 56; push edx ; need not be preserved 57 push esi 58 push edi 59 60 get_GOT ebx ; get GOT address 61 movpic POINTER [gotptr], ebx ; save GOT address 62 63 mov ecx, JDIMENSION [img_width(eax)] 64 test ecx,ecx 65 jz near .return 66 67 push ecx 68 69 mov esi, JSAMPIMAGE [output_buf(eax)] 70 mov ecx, JDIMENSION [output_row(eax)] 71 mov edi, JSAMPARRAY [esi+0*SIZEOF_JSAMPARRAY] 72 lea edi, [edi+ecx*SIZEOF_JSAMPROW] 73 74 pop ecx 75 76 mov esi, JSAMPARRAY [input_buf(eax)] 77 mov eax, INT [num_rows(eax)] 78 test eax,eax 79 jle near .return 80 alignx 16,7 81.rowloop: 82 pushpic eax 83 push edi 84 push esi 85 push ecx ; col 86 87 mov esi, JSAMPROW [esi] ; inptr 88 mov edi, JSAMPROW [edi] ; outptr0 89 movpic eax, POINTER [gotptr] ; load GOT address (eax) 90 91 cmp ecx, byte SIZEOF_XMMWORD 92 jae near .columnloop 93 alignx 16,7 94 95%if RGB_PIXELSIZE == 3 ; --------------- 96 97.column_ld1: 98 push eax 99 push edx 100 lea ecx,[ecx+ecx*2] ; imul ecx,RGB_PIXELSIZE 101 test cl, SIZEOF_BYTE 102 jz short .column_ld2 103 sub ecx, byte SIZEOF_BYTE 104 movzx eax, BYTE [esi+ecx] 105.column_ld2: 106 test cl, SIZEOF_WORD 107 jz short .column_ld4 108 sub ecx, byte SIZEOF_WORD 109 movzx edx, WORD [esi+ecx] 110 shl eax, WORD_BIT 111 or eax,edx 112.column_ld4: 113 movd xmmA,eax 114 pop edx 115 pop eax 116 test cl, SIZEOF_DWORD 117 jz short .column_ld8 118 sub ecx, byte SIZEOF_DWORD 119 movd xmmF, XMM_DWORD [esi+ecx] 120 pslldq xmmA, SIZEOF_DWORD 121 por xmmA,xmmF 122.column_ld8: 123 test cl, SIZEOF_MMWORD 124 jz short .column_ld16 125 sub ecx, byte SIZEOF_MMWORD 126 movq xmmB, XMM_MMWORD [esi+ecx] 127 pslldq xmmA, SIZEOF_MMWORD 128 por xmmA,xmmB 129.column_ld16: 130 test cl, SIZEOF_XMMWORD 131 jz short .column_ld32 132 movdqa xmmF,xmmA 133 movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD] 134 mov ecx, SIZEOF_XMMWORD 135 jmp short .rgb_gray_cnv 136.column_ld32: 137 test cl, 2*SIZEOF_XMMWORD 138 mov ecx, SIZEOF_XMMWORD 139 jz short .rgb_gray_cnv 140 movdqa xmmB,xmmA 141 movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD] 142 movdqu xmmF, XMMWORD [esi+1*SIZEOF_XMMWORD] 143 jmp short .rgb_gray_cnv 144 alignx 16,7 145 146.columnloop: 147 movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD] 148 movdqu xmmF, XMMWORD [esi+1*SIZEOF_XMMWORD] 149 movdqu xmmB, XMMWORD [esi+2*SIZEOF_XMMWORD] 150 151.rgb_gray_cnv: 152 ; xmmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05) 153 ; xmmF=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A) 154 ; xmmB=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F) 155 156 movdqa xmmG,xmmA 157 pslldq xmmA,8 ; xmmA=(-- -- -- -- -- -- -- -- 00 10 20 01 11 21 02 12) 158 psrldq xmmG,8 ; xmmG=(22 03 13 23 04 14 24 05 -- -- -- -- -- -- -- --) 159 160 punpckhbw xmmA,xmmF ; xmmA=(00 08 10 18 20 28 01 09 11 19 21 29 02 0A 12 1A) 161 pslldq xmmF,8 ; xmmF=(-- -- -- -- -- -- -- -- 15 25 06 16 26 07 17 27) 162 163 punpcklbw xmmG,xmmB ; xmmG=(22 2A 03 0B 13 1B 23 2B 04 0C 14 1C 24 2C 05 0D) 164 punpckhbw xmmF,xmmB ; xmmF=(15 1D 25 2D 06 0E 16 1E 26 2E 07 0F 17 1F 27 2F) 165 166 movdqa xmmD,xmmA 167 pslldq xmmA,8 ; xmmA=(-- -- -- -- -- -- -- -- 00 08 10 18 20 28 01 09) 168 psrldq xmmD,8 ; xmmD=(11 19 21 29 02 0A 12 1A -- -- -- -- -- -- -- --) 169 170 punpckhbw xmmA,xmmG ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 01 05 09 0D) 171 pslldq xmmG,8 ; xmmG=(-- -- -- -- -- -- -- -- 22 2A 03 0B 13 1B 23 2B) 172 173 punpcklbw xmmD,xmmF ; xmmD=(11 15 19 1D 21 25 29 2D 02 06 0A 0E 12 16 1A 1E) 174 punpckhbw xmmG,xmmF ; xmmG=(22 26 2A 2E 03 07 0B 0F 13 17 1B 1F 23 27 2B 2F) 175 176 movdqa xmmE,xmmA 177 pslldq xmmA,8 ; xmmA=(-- -- -- -- -- -- -- -- 00 04 08 0C 10 14 18 1C) 178 psrldq xmmE,8 ; xmmE=(20 24 28 2C 01 05 09 0D -- -- -- -- -- -- -- --) 179 180 punpckhbw xmmA,xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E) 181 pslldq xmmD,8 ; xmmD=(-- -- -- -- -- -- -- -- 11 15 19 1D 21 25 29 2D) 182 183 punpcklbw xmmE,xmmG ; xmmE=(20 22 24 26 28 2A 2C 2E 01 03 05 07 09 0B 0D 0F) 184 punpckhbw xmmD,xmmG ; xmmD=(11 13 15 17 19 1B 1D 1F 21 23 25 27 29 2B 2D 2F) 185 186 pxor xmmH,xmmH 187 188 movdqa xmmC,xmmA 189 punpcklbw xmmA,xmmH ; xmmA=(00 02 04 06 08 0A 0C 0E) 190 punpckhbw xmmC,xmmH ; xmmC=(10 12 14 16 18 1A 1C 1E) 191 192 movdqa xmmB,xmmE 193 punpcklbw xmmE,xmmH ; xmmE=(20 22 24 26 28 2A 2C 2E) 194 punpckhbw xmmB,xmmH ; xmmB=(01 03 05 07 09 0B 0D 0F) 195 196 movdqa xmmF,xmmD 197 punpcklbw xmmD,xmmH ; xmmD=(11 13 15 17 19 1B 1D 1F) 198 punpckhbw xmmF,xmmH ; xmmF=(21 23 25 27 29 2B 2D 2F) 199 200%else ; RGB_PIXELSIZE == 4 ; ----------- 201 202.column_ld1: 203 test cl, SIZEOF_XMMWORD/16 204 jz short .column_ld2 205 sub ecx, byte SIZEOF_XMMWORD/16 206 movd xmmA, XMM_DWORD [esi+ecx*RGB_PIXELSIZE] 207.column_ld2: 208 test cl, SIZEOF_XMMWORD/8 209 jz short .column_ld4 210 sub ecx, byte SIZEOF_XMMWORD/8 211 movq xmmE, XMM_MMWORD [esi+ecx*RGB_PIXELSIZE] 212 pslldq xmmA, SIZEOF_MMWORD 213 por xmmA,xmmE 214.column_ld4: 215 test cl, SIZEOF_XMMWORD/4 216 jz short .column_ld8 217 sub ecx, byte SIZEOF_XMMWORD/4 218 movdqa xmmE,xmmA 219 movdqu xmmA, XMMWORD [esi+ecx*RGB_PIXELSIZE] 220.column_ld8: 221 test cl, SIZEOF_XMMWORD/2 222 mov ecx, SIZEOF_XMMWORD 223 jz short .rgb_gray_cnv 224 movdqa xmmF,xmmA 225 movdqa xmmH,xmmE 226 movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD] 227 movdqu xmmE, XMMWORD [esi+1*SIZEOF_XMMWORD] 228 jmp short .rgb_gray_cnv 229 alignx 16,7 230 231.columnloop: 232 movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD] 233 movdqu xmmE, XMMWORD [esi+1*SIZEOF_XMMWORD] 234 movdqu xmmF, XMMWORD [esi+2*SIZEOF_XMMWORD] 235 movdqu xmmH, XMMWORD [esi+3*SIZEOF_XMMWORD] 236 237.rgb_gray_cnv: 238 ; xmmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33) 239 ; xmmE=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37) 240 ; xmmF=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B) 241 ; xmmH=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F) 242 243 movdqa xmmD,xmmA 244 punpcklbw xmmA,xmmE ; xmmA=(00 04 10 14 20 24 30 34 01 05 11 15 21 25 31 35) 245 punpckhbw xmmD,xmmE ; xmmD=(02 06 12 16 22 26 32 36 03 07 13 17 23 27 33 37) 246 247 movdqa xmmC,xmmF 248 punpcklbw xmmF,xmmH ; xmmF=(08 0C 18 1C 28 2C 38 3C 09 0D 19 1D 29 2D 39 3D) 249 punpckhbw xmmC,xmmH ; xmmC=(0A 0E 1A 1E 2A 2E 3A 3E 0B 0F 1B 1F 2B 2F 3B 3F) 250 251 movdqa xmmB,xmmA 252 punpcklwd xmmA,xmmF ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C) 253 punpckhwd xmmB,xmmF ; xmmB=(01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D) 254 255 movdqa xmmG,xmmD 256 punpcklwd xmmD,xmmC ; xmmD=(02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E) 257 punpckhwd xmmG,xmmC ; xmmG=(03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F) 258 259 movdqa xmmE,xmmA 260 punpcklbw xmmA,xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E) 261 punpckhbw xmmE,xmmD ; xmmE=(20 22 24 26 28 2A 2C 2E 30 32 34 36 38 3A 3C 3E) 262 263 movdqa xmmH,xmmB 264 punpcklbw xmmB,xmmG ; xmmB=(01 03 05 07 09 0B 0D 0F 11 13 15 17 19 1B 1D 1F) 265 punpckhbw xmmH,xmmG ; xmmH=(21 23 25 27 29 2B 2D 2F 31 33 35 37 39 3B 3D 3F) 266 267 pxor xmmF,xmmF 268 269 movdqa xmmC,xmmA 270 punpcklbw xmmA,xmmF ; xmmA=(00 02 04 06 08 0A 0C 0E) 271 punpckhbw xmmC,xmmF ; xmmC=(10 12 14 16 18 1A 1C 1E) 272 273 movdqa xmmD,xmmB 274 punpcklbw xmmB,xmmF ; xmmB=(01 03 05 07 09 0B 0D 0F) 275 punpckhbw xmmD,xmmF ; xmmD=(11 13 15 17 19 1B 1D 1F) 276 277 movdqa xmmG,xmmE 278 punpcklbw xmmE,xmmF ; xmmE=(20 22 24 26 28 2A 2C 2E) 279 punpckhbw xmmG,xmmF ; xmmG=(30 32 34 36 38 3A 3C 3E) 280 281 punpcklbw xmmF,xmmH 282 punpckhbw xmmH,xmmH 283 psrlw xmmF,BYTE_BIT ; xmmF=(21 23 25 27 29 2B 2D 2F) 284 psrlw xmmH,BYTE_BIT ; xmmH=(31 33 35 37 39 3B 3D 3F) 285 286%endif ; RGB_PIXELSIZE ; --------------- 287 288 ; xmm0=R(02468ACE)=RE, xmm2=G(02468ACE)=GE, xmm4=B(02468ACE)=BE 289 ; xmm1=R(13579BDF)=RO, xmm3=G(13579BDF)=GO, xmm5=B(13579BDF)=BO 290 291 ; (Original) 292 ; Y = 0.29900 * R + 0.58700 * G + 0.11400 * B 293 ; 294 ; (This implementation) 295 ; Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G 296 297 movdqa xmm6,xmm1 298 punpcklwd xmm1,xmm3 299 punpckhwd xmm6,xmm3 300 pmaddwd xmm1,[GOTOFF(eax,PW_F0299_F0337)] ; xmm1=ROL*FIX(0.299)+GOL*FIX(0.337) 301 pmaddwd xmm6,[GOTOFF(eax,PW_F0299_F0337)] ; xmm6=ROH*FIX(0.299)+GOH*FIX(0.337) 302 303 movdqa xmm7, xmm6 ; xmm7=ROH*FIX(0.299)+GOH*FIX(0.337) 304 305 movdqa xmm6,xmm0 306 punpcklwd xmm0,xmm2 307 punpckhwd xmm6,xmm2 308 pmaddwd xmm0,[GOTOFF(eax,PW_F0299_F0337)] ; xmm0=REL*FIX(0.299)+GEL*FIX(0.337) 309 pmaddwd xmm6,[GOTOFF(eax,PW_F0299_F0337)] ; xmm6=REH*FIX(0.299)+GEH*FIX(0.337) 310 311 movdqa XMMWORD [wk(0)], xmm0 ; wk(0)=REL*FIX(0.299)+GEL*FIX(0.337) 312 movdqa XMMWORD [wk(1)], xmm6 ; wk(1)=REH*FIX(0.299)+GEH*FIX(0.337) 313 314 movdqa xmm0, xmm5 ; xmm0=BO 315 movdqa xmm6, xmm4 ; xmm6=BE 316 317 movdqa xmm4,xmm0 318 punpcklwd xmm0,xmm3 319 punpckhwd xmm4,xmm3 320 pmaddwd xmm0,[GOTOFF(eax,PW_F0114_F0250)] ; xmm0=BOL*FIX(0.114)+GOL*FIX(0.250) 321 pmaddwd xmm4,[GOTOFF(eax,PW_F0114_F0250)] ; xmm4=BOH*FIX(0.114)+GOH*FIX(0.250) 322 323 movdqa xmm3,[GOTOFF(eax,PD_ONEHALF)] ; xmm3=[PD_ONEHALF] 324 325 paddd xmm0, xmm1 326 paddd xmm4, xmm7 327 paddd xmm0,xmm3 328 paddd xmm4,xmm3 329 psrld xmm0,SCALEBITS ; xmm0=YOL 330 psrld xmm4,SCALEBITS ; xmm4=YOH 331 packssdw xmm0,xmm4 ; xmm0=YO 332 333 movdqa xmm4,xmm6 334 punpcklwd xmm6,xmm2 335 punpckhwd xmm4,xmm2 336 pmaddwd xmm6,[GOTOFF(eax,PW_F0114_F0250)] ; xmm6=BEL*FIX(0.114)+GEL*FIX(0.250) 337 pmaddwd xmm4,[GOTOFF(eax,PW_F0114_F0250)] ; xmm4=BEH*FIX(0.114)+GEH*FIX(0.250) 338 339 movdqa xmm2,[GOTOFF(eax,PD_ONEHALF)] ; xmm2=[PD_ONEHALF] 340 341 paddd xmm6, XMMWORD [wk(0)] 342 paddd xmm4, XMMWORD [wk(1)] 343 paddd xmm6,xmm2 344 paddd xmm4,xmm2 345 psrld xmm6,SCALEBITS ; xmm6=YEL 346 psrld xmm4,SCALEBITS ; xmm4=YEH 347 packssdw xmm6,xmm4 ; xmm6=YE 348 349 psllw xmm0,BYTE_BIT 350 por xmm6,xmm0 ; xmm6=Y 351 movdqa XMMWORD [edi], xmm6 ; Save Y 352 353 sub ecx, byte SIZEOF_XMMWORD 354 add esi, byte RGB_PIXELSIZE*SIZEOF_XMMWORD ; inptr 355 add edi, byte SIZEOF_XMMWORD ; outptr0 356 cmp ecx, byte SIZEOF_XMMWORD 357 jae near .columnloop 358 test ecx,ecx 359 jnz near .column_ld1 360 361 pop ecx ; col 362 pop esi 363 pop edi 364 poppic eax 365 366 add esi, byte SIZEOF_JSAMPROW ; input_buf 367 add edi, byte SIZEOF_JSAMPROW 368 dec eax ; num_rows 369 jg near .rowloop 370 371.return: 372 pop edi 373 pop esi 374; pop edx ; need not be preserved 375; pop ecx ; need not be preserved 376 pop ebx 377 mov esp,ebp ; esp <- aligned ebp 378 pop esp ; esp <- original ebp 379 pop ebp 380 ret 381 382; For some reason, the OS X linker does not honor the request to align the 383; segment unless we do this. 384 align 16 385