jfdctfst-mmx.asm (15555B)
1 ; 2 ; jfdctfst.asm - fast integer FDCT (MMX) 3 ; 4 ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB 5 ; Copyright (C) 2016, 2024, D. R. Commander. 6 ; 7 ; Based on the x86 SIMD extension for IJG JPEG library 8 ; Copyright (C) 1999-2006, MIYASAKA Masaru. 9 ; For conditions of distribution and use, see copyright notice in jsimdext.inc 10 ; 11 ; This file should be assembled with NASM (Netwide Assembler) or Yasm. 12 ; 13 ; This file contains a fast, not so accurate integer implementation of 14 ; the forward DCT (Discrete Cosine Transform). The following code is 15 ; based directly on the IJG's original jfdctfst.c; see the jfdctfst.c 16 ; for more details. 17 18 %include "jsimdext.inc" 19 %include "jdct.inc" 20 21 ; -------------------------------------------------------------------------- 22 23 %define CONST_BITS 8 ; 14 is also OK. 24 25 %if CONST_BITS == 8 26 F_0_382 equ 98 ; FIX(0.382683433) 27 F_0_541 equ 139 ; FIX(0.541196100) 28 F_0_707 equ 181 ; FIX(0.707106781) 29 F_1_306 equ 334 ; FIX(1.306562965) 30 %else 31 ; NASM cannot do compile-time arithmetic on floating-point constants. 32 %define DESCALE(x, n) (((x) + (1 << ((n) - 1))) >> (n)) 33 F_0_382 equ DESCALE( 410903207, 30 - CONST_BITS) ; FIX(0.382683433) 34 F_0_541 equ DESCALE( 581104887, 30 - CONST_BITS) ; FIX(0.541196100) 35 F_0_707 equ DESCALE( 759250124, 30 - CONST_BITS) ; FIX(0.707106781) 36 F_1_306 equ DESCALE(1402911301, 30 - CONST_BITS) ; FIX(1.306562965) 37 %endif 38 39 ; -------------------------------------------------------------------------- 40 SECTION SEG_CONST 41 42 ; PRE_MULTIPLY_SCALE_BITS <= 2 (to avoid overflow) 43 ; CONST_BITS + CONST_SHIFT + PRE_MULTIPLY_SCALE_BITS == 16 (for pmulhw) 44 45 %define PRE_MULTIPLY_SCALE_BITS 2 46 %define CONST_SHIFT (16 - PRE_MULTIPLY_SCALE_BITS - CONST_BITS) 47 48 ALIGNZ 32 49 GLOBAL_DATA(jconst_fdct_ifast_mmx) 50 51 EXTN(jconst_fdct_ifast_mmx): 52 53 PW_F0707 times 4 dw F_0_707 << CONST_SHIFT 54 PW_F0382 times 4 dw F_0_382 << CONST_SHIFT 55 PW_F0541 times 4 dw F_0_541 << CONST_SHIFT 56 PW_F1306 times 4 dw F_1_306 << CONST_SHIFT 57 58 ALIGNZ 32 59 60 ; -------------------------------------------------------------------------- 61 SECTION SEG_TEXT 62 BITS 32 63 ; 64 ; Perform the forward DCT on one block of samples. 65 ; 66 ; GLOBAL(void) 67 ; jsimd_fdct_ifast_mmx(DCTELEM *data) 68 ; 69 70 %define data(b) (b) + 8 ; DCTELEM *data 71 72 %define original_ebp ebp + 0 73 %define wk(i) ebp - (WK_NUM - (i)) * SIZEOF_MMWORD ; mmword wk[WK_NUM] 74 %define WK_NUM 2 75 76 align 32 77 GLOBAL_FUNCTION(jsimd_fdct_ifast_mmx) 78 79 EXTN(jsimd_fdct_ifast_mmx): 80 push ebp 81 mov eax, esp ; eax = original ebp 82 sub esp, byte 4 83 and esp, byte (-SIZEOF_MMWORD) ; align to 64 bits 84 mov [esp], eax 85 mov ebp, esp ; ebp = aligned ebp 86 lea esp, [wk(0)] 87 PUSHPIC ebx 88 ; push ecx ; need not be preserved 89 ; push edx ; need not be preserved 90 ; push esi ; unused 91 ; push edi ; unused 92 93 GET_GOT ebx ; get GOT address 94 95 ; ---- Pass 1: process rows. 96 97 mov edx, POINTER [data(eax)] ; (DCTELEM *) 98 mov ecx, DCTSIZE/4 99 ALIGNX 16, 7 100 .rowloop: 101 102 movq mm0, MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)] 103 movq mm1, MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)] 104 movq mm2, MMWORD [MMBLOCK(2,1,edx,SIZEOF_DCTELEM)] 105 movq mm3, MMWORD [MMBLOCK(3,1,edx,SIZEOF_DCTELEM)] 106 107 ; mm0=(20 21 22 23), mm2=(24 25 26 27) 108 ; mm1=(30 31 32 33), mm3=(34 35 36 37) 109 110 movq mm4, mm0 ; transpose coefficients(phase 1) 111 punpcklwd mm0, mm1 ; mm0=(20 30 21 31) 112 punpckhwd mm4, mm1 ; mm4=(22 32 23 33) 113 movq mm5, mm2 ; transpose coefficients(phase 1) 114 punpcklwd mm2, mm3 ; mm2=(24 34 25 35) 115 punpckhwd mm5, mm3 ; mm5=(26 36 27 37) 116 117 movq mm6, MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)] 118 movq mm7, MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)] 119 movq mm1, MMWORD [MMBLOCK(0,1,edx,SIZEOF_DCTELEM)] 120 movq mm3, MMWORD [MMBLOCK(1,1,edx,SIZEOF_DCTELEM)] 121 122 ; mm6=(00 01 02 03), mm1=(04 05 06 07) 123 ; mm7=(10 11 12 13), mm3=(14 15 16 17) 124 125 movq MMWORD [wk(0)], mm4 ; wk(0)=(22 32 23 33) 126 movq MMWORD [wk(1)], mm2 ; wk(1)=(24 34 25 35) 127 128 movq mm4, mm6 ; transpose coefficients(phase 1) 129 punpcklwd mm6, mm7 ; mm6=(00 10 01 11) 130 punpckhwd mm4, mm7 ; mm4=(02 12 03 13) 131 movq mm2, mm1 ; transpose coefficients(phase 1) 132 punpcklwd mm1, mm3 ; mm1=(04 14 05 15) 133 punpckhwd mm2, mm3 ; mm2=(06 16 07 17) 134 135 movq mm7, mm6 ; transpose coefficients(phase 2) 136 punpckldq mm6, mm0 ; mm6=(00 10 20 30)=data0 137 punpckhdq mm7, mm0 ; mm7=(01 11 21 31)=data1 138 movq mm3, mm2 ; transpose coefficients(phase 2) 139 punpckldq mm2, mm5 ; mm2=(06 16 26 36)=data6 140 punpckhdq mm3, mm5 ; mm3=(07 17 27 37)=data7 141 142 movq mm0, mm7 143 movq mm5, mm6 144 psubw mm7, mm2 ; mm7=data1-data6=tmp6 145 psubw mm6, mm3 ; mm6=data0-data7=tmp7 146 paddw mm0, mm2 ; mm0=data1+data6=tmp1 147 paddw mm5, mm3 ; mm5=data0+data7=tmp0 148 149 movq mm2, MMWORD [wk(0)] ; mm2=(22 32 23 33) 150 movq mm3, MMWORD [wk(1)] ; mm3=(24 34 25 35) 151 movq MMWORD [wk(0)], mm7 ; wk(0)=tmp6 152 movq MMWORD [wk(1)], mm6 ; wk(1)=tmp7 153 154 movq mm7, mm4 ; transpose coefficients(phase 2) 155 punpckldq mm4, mm2 ; mm4=(02 12 22 32)=data2 156 punpckhdq mm7, mm2 ; mm7=(03 13 23 33)=data3 157 movq mm6, mm1 ; transpose coefficients(phase 2) 158 punpckldq mm1, mm3 ; mm1=(04 14 24 34)=data4 159 punpckhdq mm6, mm3 ; mm6=(05 15 25 35)=data5 160 161 movq mm2, mm7 162 movq mm3, mm4 163 paddw mm7, mm1 ; mm7=data3+data4=tmp3 164 paddw mm4, mm6 ; mm4=data2+data5=tmp2 165 psubw mm2, mm1 ; mm2=data3-data4=tmp4 166 psubw mm3, mm6 ; mm3=data2-data5=tmp5 167 168 ; -- Even part 169 170 movq mm1, mm5 171 movq mm6, mm0 172 psubw mm5, mm7 ; mm5=tmp13 173 psubw mm0, mm4 ; mm0=tmp12 174 paddw mm1, mm7 ; mm1=tmp10 175 paddw mm6, mm4 ; mm6=tmp11 176 177 paddw mm0, mm5 178 psllw mm0, PRE_MULTIPLY_SCALE_BITS 179 pmulhw mm0, [GOTOFF(ebx,PW_F0707)] ; mm0=z1 180 181 movq mm7, mm1 182 movq mm4, mm5 183 psubw mm1, mm6 ; mm1=data4 184 psubw mm5, mm0 ; mm5=data6 185 paddw mm7, mm6 ; mm7=data0 186 paddw mm4, mm0 ; mm4=data2 187 188 movq MMWORD [MMBLOCK(0,1,edx,SIZEOF_DCTELEM)], mm1 189 movq MMWORD [MMBLOCK(2,1,edx,SIZEOF_DCTELEM)], mm5 190 movq MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)], mm7 191 movq MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)], mm4 192 193 ; -- Odd part 194 195 movq mm6, MMWORD [wk(0)] ; mm6=tmp6 196 movq mm0, MMWORD [wk(1)] ; mm0=tmp7 197 198 paddw mm2, mm3 ; mm2=tmp10 199 paddw mm3, mm6 ; mm3=tmp11 200 paddw mm6, mm0 ; mm6=tmp12, mm0=tmp7 201 202 psllw mm2, PRE_MULTIPLY_SCALE_BITS 203 psllw mm6, PRE_MULTIPLY_SCALE_BITS 204 205 psllw mm3, PRE_MULTIPLY_SCALE_BITS 206 pmulhw mm3, [GOTOFF(ebx,PW_F0707)] ; mm3=z3 207 208 movq mm1, mm2 ; mm1=tmp10 209 psubw mm2, mm6 210 pmulhw mm2, [GOTOFF(ebx,PW_F0382)] ; mm2=z5 211 pmulhw mm1, [GOTOFF(ebx,PW_F0541)] ; mm1=MULTIPLY(tmp10,FIX_0_54119610) 212 pmulhw mm6, [GOTOFF(ebx,PW_F1306)] ; mm6=MULTIPLY(tmp12,FIX_1_30656296) 213 paddw mm1, mm2 ; mm1=z2 214 paddw mm6, mm2 ; mm6=z4 215 216 movq mm5, mm0 217 psubw mm0, mm3 ; mm0=z13 218 paddw mm5, mm3 ; mm5=z11 219 220 movq mm7, mm0 221 movq mm4, mm5 222 psubw mm0, mm1 ; mm0=data3 223 psubw mm5, mm6 ; mm5=data7 224 paddw mm7, mm1 ; mm7=data5 225 paddw mm4, mm6 ; mm4=data1 226 227 movq MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)], mm0 228 movq MMWORD [MMBLOCK(3,1,edx,SIZEOF_DCTELEM)], mm5 229 movq MMWORD [MMBLOCK(1,1,edx,SIZEOF_DCTELEM)], mm7 230 movq MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)], mm4 231 232 add edx, byte 4*DCTSIZE*SIZEOF_DCTELEM 233 dec ecx 234 jnz near .rowloop 235 236 ; ---- Pass 2: process columns. 237 238 mov edx, POINTER [data(eax)] ; (DCTELEM *) 239 mov ecx, DCTSIZE/4 240 ALIGNX 16, 7 241 .columnloop: 242 243 movq mm0, MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)] 244 movq mm1, MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)] 245 movq mm2, MMWORD [MMBLOCK(6,0,edx,SIZEOF_DCTELEM)] 246 movq mm3, MMWORD [MMBLOCK(7,0,edx,SIZEOF_DCTELEM)] 247 248 ; mm0=(02 12 22 32), mm2=(42 52 62 72) 249 ; mm1=(03 13 23 33), mm3=(43 53 63 73) 250 251 movq mm4, mm0 ; transpose coefficients(phase 1) 252 punpcklwd mm0, mm1 ; mm0=(02 03 12 13) 253 punpckhwd mm4, mm1 ; mm4=(22 23 32 33) 254 movq mm5, mm2 ; transpose coefficients(phase 1) 255 punpcklwd mm2, mm3 ; mm2=(42 43 52 53) 256 punpckhwd mm5, mm3 ; mm5=(62 63 72 73) 257 258 movq mm6, MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)] 259 movq mm7, MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)] 260 movq mm1, MMWORD [MMBLOCK(4,0,edx,SIZEOF_DCTELEM)] 261 movq mm3, MMWORD [MMBLOCK(5,0,edx,SIZEOF_DCTELEM)] 262 263 ; mm6=(00 10 20 30), mm1=(40 50 60 70) 264 ; mm7=(01 11 21 31), mm3=(41 51 61 71) 265 266 movq MMWORD [wk(0)], mm4 ; wk(0)=(22 23 32 33) 267 movq MMWORD [wk(1)], mm2 ; wk(1)=(42 43 52 53) 268 269 movq mm4, mm6 ; transpose coefficients(phase 1) 270 punpcklwd mm6, mm7 ; mm6=(00 01 10 11) 271 punpckhwd mm4, mm7 ; mm4=(20 21 30 31) 272 movq mm2, mm1 ; transpose coefficients(phase 1) 273 punpcklwd mm1, mm3 ; mm1=(40 41 50 51) 274 punpckhwd mm2, mm3 ; mm2=(60 61 70 71) 275 276 movq mm7, mm6 ; transpose coefficients(phase 2) 277 punpckldq mm6, mm0 ; mm6=(00 01 02 03)=data0 278 punpckhdq mm7, mm0 ; mm7=(10 11 12 13)=data1 279 movq mm3, mm2 ; transpose coefficients(phase 2) 280 punpckldq mm2, mm5 ; mm2=(60 61 62 63)=data6 281 punpckhdq mm3, mm5 ; mm3=(70 71 72 73)=data7 282 283 movq mm0, mm7 284 movq mm5, mm6 285 psubw mm7, mm2 ; mm7=data1-data6=tmp6 286 psubw mm6, mm3 ; mm6=data0-data7=tmp7 287 paddw mm0, mm2 ; mm0=data1+data6=tmp1 288 paddw mm5, mm3 ; mm5=data0+data7=tmp0 289 290 movq mm2, MMWORD [wk(0)] ; mm2=(22 23 32 33) 291 movq mm3, MMWORD [wk(1)] ; mm3=(42 43 52 53) 292 movq MMWORD [wk(0)], mm7 ; wk(0)=tmp6 293 movq MMWORD [wk(1)], mm6 ; wk(1)=tmp7 294 295 movq mm7, mm4 ; transpose coefficients(phase 2) 296 punpckldq mm4, mm2 ; mm4=(20 21 22 23)=data2 297 punpckhdq mm7, mm2 ; mm7=(30 31 32 33)=data3 298 movq mm6, mm1 ; transpose coefficients(phase 2) 299 punpckldq mm1, mm3 ; mm1=(40 41 42 43)=data4 300 punpckhdq mm6, mm3 ; mm6=(50 51 52 53)=data5 301 302 movq mm2, mm7 303 movq mm3, mm4 304 paddw mm7, mm1 ; mm7=data3+data4=tmp3 305 paddw mm4, mm6 ; mm4=data2+data5=tmp2 306 psubw mm2, mm1 ; mm2=data3-data4=tmp4 307 psubw mm3, mm6 ; mm3=data2-data5=tmp5 308 309 ; -- Even part 310 311 movq mm1, mm5 312 movq mm6, mm0 313 psubw mm5, mm7 ; mm5=tmp13 314 psubw mm0, mm4 ; mm0=tmp12 315 paddw mm1, mm7 ; mm1=tmp10 316 paddw mm6, mm4 ; mm6=tmp11 317 318 paddw mm0, mm5 319 psllw mm0, PRE_MULTIPLY_SCALE_BITS 320 pmulhw mm0, [GOTOFF(ebx,PW_F0707)] ; mm0=z1 321 322 movq mm7, mm1 323 movq mm4, mm5 324 psubw mm1, mm6 ; mm1=data4 325 psubw mm5, mm0 ; mm5=data6 326 paddw mm7, mm6 ; mm7=data0 327 paddw mm4, mm0 ; mm4=data2 328 329 movq MMWORD [MMBLOCK(4,0,edx,SIZEOF_DCTELEM)], mm1 330 movq MMWORD [MMBLOCK(6,0,edx,SIZEOF_DCTELEM)], mm5 331 movq MMWORD [MMBLOCK(0,0,edx,SIZEOF_DCTELEM)], mm7 332 movq MMWORD [MMBLOCK(2,0,edx,SIZEOF_DCTELEM)], mm4 333 334 ; -- Odd part 335 336 movq mm6, MMWORD [wk(0)] ; mm6=tmp6 337 movq mm0, MMWORD [wk(1)] ; mm0=tmp7 338 339 paddw mm2, mm3 ; mm2=tmp10 340 paddw mm3, mm6 ; mm3=tmp11 341 paddw mm6, mm0 ; mm6=tmp12, mm0=tmp7 342 343 psllw mm2, PRE_MULTIPLY_SCALE_BITS 344 psllw mm6, PRE_MULTIPLY_SCALE_BITS 345 346 psllw mm3, PRE_MULTIPLY_SCALE_BITS 347 pmulhw mm3, [GOTOFF(ebx,PW_F0707)] ; mm3=z3 348 349 movq mm1, mm2 ; mm1=tmp10 350 psubw mm2, mm6 351 pmulhw mm2, [GOTOFF(ebx,PW_F0382)] ; mm2=z5 352 pmulhw mm1, [GOTOFF(ebx,PW_F0541)] ; mm1=MULTIPLY(tmp10,FIX_0_54119610) 353 pmulhw mm6, [GOTOFF(ebx,PW_F1306)] ; mm6=MULTIPLY(tmp12,FIX_1_30656296) 354 paddw mm1, mm2 ; mm1=z2 355 paddw mm6, mm2 ; mm6=z4 356 357 movq mm5, mm0 358 psubw mm0, mm3 ; mm0=z13 359 paddw mm5, mm3 ; mm5=z11 360 361 movq mm7, mm0 362 movq mm4, mm5 363 psubw mm0, mm1 ; mm0=data3 364 psubw mm5, mm6 ; mm5=data7 365 paddw mm7, mm1 ; mm7=data5 366 paddw mm4, mm6 ; mm4=data1 367 368 movq MMWORD [MMBLOCK(3,0,edx,SIZEOF_DCTELEM)], mm0 369 movq MMWORD [MMBLOCK(7,0,edx,SIZEOF_DCTELEM)], mm5 370 movq MMWORD [MMBLOCK(5,0,edx,SIZEOF_DCTELEM)], mm7 371 movq MMWORD [MMBLOCK(1,0,edx,SIZEOF_DCTELEM)], mm4 372 373 add edx, byte 4*SIZEOF_DCTELEM 374 dec ecx 375 jnz near .columnloop 376 377 emms ; empty MMX state 378 379 ; pop edi ; unused 380 ; pop esi ; unused 381 ; pop edx ; need not be preserved 382 ; pop ecx ; need not be preserved 383 POPPIC ebx 384 mov esp, ebp ; esp <- aligned ebp 385 pop esp ; esp <- original ebp 386 pop ebp 387 ret 388 389 ; For some reason, the OS X linker does not honor the request to align the 390 ; segment unless we do this. 391 align 32