1; 2; jfdctflt.asm - floating-point FDCT (SSE) 3; 4; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB 5; Copyright (C) 2016, 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), 12; can *not* be assembled with Microsoft's MASM or any compatible 13; assembler (including Borland's Turbo Assembler). 14; NASM is available from http://nasm.sourceforge.net/ or 15; http://sourceforge.net/project/showfiles.php?group_id=6208 16; 17; This file contains a floating-point implementation of the forward DCT 18; (Discrete Cosine Transform). The following code is based directly on 19; the IJG's original jfdctflt.c; see the jfdctflt.c for more details. 20; 21; [TAB8] 22 23%include "jsimdext.inc" 24%include "jdct.inc" 25 26; -------------------------------------------------------------------------- 27 28%macro unpcklps2 2 ; %1=(0 1 2 3) / %2=(4 5 6 7) => %1=(0 1 4 5) 29 shufps %1, %2, 0x44 30%endmacro 31 32%macro unpckhps2 2 ; %1=(0 1 2 3) / %2=(4 5 6 7) => %1=(2 3 6 7) 33 shufps %1, %2, 0xEE 34%endmacro 35 36; -------------------------------------------------------------------------- 37 SECTION SEG_CONST 38 39 alignz 32 40 GLOBAL_DATA(jconst_fdct_float_sse) 41 42EXTN(jconst_fdct_float_sse): 43 44PD_0_382 times 4 dd 0.382683432365089771728460 45PD_0_707 times 4 dd 0.707106781186547524400844 46PD_0_541 times 4 dd 0.541196100146196984399723 47PD_1_306 times 4 dd 1.306562964876376527856643 48 49 alignz 32 50 51; -------------------------------------------------------------------------- 52 SECTION SEG_TEXT 53 BITS 32 54; 55; Perform the forward DCT on one block of samples. 56; 57; GLOBAL(void) 58; jsimd_fdct_float_sse(FAST_FLOAT *data) 59; 60 61%define data(b) (b) + 8 ; FAST_FLOAT *data 62 63%define original_ebp ebp + 0 64%define wk(i) ebp - (WK_NUM - (i)) * SIZEOF_XMMWORD 65 ; xmmword wk[WK_NUM] 66%define WK_NUM 2 67 68 align 32 69 GLOBAL_FUNCTION(jsimd_fdct_float_sse) 70 71EXTN(jsimd_fdct_float_sse): 72 push ebp 73 mov eax, esp ; eax = original ebp 74 sub esp, byte 4 75 and esp, byte (-SIZEOF_XMMWORD) ; align to 128 bits 76 mov [esp], eax 77 mov ebp, esp ; ebp = aligned ebp 78 lea esp, [wk(0)] 79 pushpic ebx 80; push ecx ; need not be preserved 81; push edx ; need not be preserved 82; push esi ; unused 83; push edi ; unused 84 85 get_GOT ebx ; get GOT address 86 87 ; ---- Pass 1: process rows. 88 89 mov edx, POINTER [data(eax)] ; (FAST_FLOAT *) 90 mov ecx, DCTSIZE/4 91 alignx 16, 7 92.rowloop: 93 94 movaps xmm0, XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)] 95 movaps xmm1, XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)] 96 movaps xmm2, XMMWORD [XMMBLOCK(2,1,edx,SIZEOF_FAST_FLOAT)] 97 movaps xmm3, XMMWORD [XMMBLOCK(3,1,edx,SIZEOF_FAST_FLOAT)] 98 99 ; xmm0=(20 21 22 23), xmm2=(24 25 26 27) 100 ; xmm1=(30 31 32 33), xmm3=(34 35 36 37) 101 102 movaps xmm4, xmm0 ; transpose coefficients(phase 1) 103 unpcklps xmm0, xmm1 ; xmm0=(20 30 21 31) 104 unpckhps xmm4, xmm1 ; xmm4=(22 32 23 33) 105 movaps xmm5, xmm2 ; transpose coefficients(phase 1) 106 unpcklps xmm2, xmm3 ; xmm2=(24 34 25 35) 107 unpckhps xmm5, xmm3 ; xmm5=(26 36 27 37) 108 109 movaps xmm6, XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)] 110 movaps xmm7, XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)] 111 movaps xmm1, XMMWORD [XMMBLOCK(0,1,edx,SIZEOF_FAST_FLOAT)] 112 movaps xmm3, XMMWORD [XMMBLOCK(1,1,edx,SIZEOF_FAST_FLOAT)] 113 114 ; xmm6=(00 01 02 03), xmm1=(04 05 06 07) 115 ; xmm7=(10 11 12 13), xmm3=(14 15 16 17) 116 117 movaps XMMWORD [wk(0)], xmm4 ; wk(0)=(22 32 23 33) 118 movaps XMMWORD [wk(1)], xmm2 ; wk(1)=(24 34 25 35) 119 120 movaps xmm4, xmm6 ; transpose coefficients(phase 1) 121 unpcklps xmm6, xmm7 ; xmm6=(00 10 01 11) 122 unpckhps xmm4, xmm7 ; xmm4=(02 12 03 13) 123 movaps xmm2, xmm1 ; transpose coefficients(phase 1) 124 unpcklps xmm1, xmm3 ; xmm1=(04 14 05 15) 125 unpckhps xmm2, xmm3 ; xmm2=(06 16 07 17) 126 127 movaps xmm7, xmm6 ; transpose coefficients(phase 2) 128 unpcklps2 xmm6, xmm0 ; xmm6=(00 10 20 30)=data0 129 unpckhps2 xmm7, xmm0 ; xmm7=(01 11 21 31)=data1 130 movaps xmm3, xmm2 ; transpose coefficients(phase 2) 131 unpcklps2 xmm2, xmm5 ; xmm2=(06 16 26 36)=data6 132 unpckhps2 xmm3, xmm5 ; xmm3=(07 17 27 37)=data7 133 134 movaps xmm0, xmm7 135 movaps xmm5, xmm6 136 subps xmm7, xmm2 ; xmm7=data1-data6=tmp6 137 subps xmm6, xmm3 ; xmm6=data0-data7=tmp7 138 addps xmm0, xmm2 ; xmm0=data1+data6=tmp1 139 addps xmm5, xmm3 ; xmm5=data0+data7=tmp0 140 141 movaps xmm2, XMMWORD [wk(0)] ; xmm2=(22 32 23 33) 142 movaps xmm3, XMMWORD [wk(1)] ; xmm3=(24 34 25 35) 143 movaps XMMWORD [wk(0)], xmm7 ; wk(0)=tmp6 144 movaps XMMWORD [wk(1)], xmm6 ; wk(1)=tmp7 145 146 movaps xmm7, xmm4 ; transpose coefficients(phase 2) 147 unpcklps2 xmm4, xmm2 ; xmm4=(02 12 22 32)=data2 148 unpckhps2 xmm7, xmm2 ; xmm7=(03 13 23 33)=data3 149 movaps xmm6, xmm1 ; transpose coefficients(phase 2) 150 unpcklps2 xmm1, xmm3 ; xmm1=(04 14 24 34)=data4 151 unpckhps2 xmm6, xmm3 ; xmm6=(05 15 25 35)=data5 152 153 movaps xmm2, xmm7 154 movaps xmm3, xmm4 155 addps xmm7, xmm1 ; xmm7=data3+data4=tmp3 156 addps xmm4, xmm6 ; xmm4=data2+data5=tmp2 157 subps xmm2, xmm1 ; xmm2=data3-data4=tmp4 158 subps xmm3, xmm6 ; xmm3=data2-data5=tmp5 159 160 ; -- Even part 161 162 movaps xmm1, xmm5 163 movaps xmm6, xmm0 164 subps xmm5, xmm7 ; xmm5=tmp13 165 subps xmm0, xmm4 ; xmm0=tmp12 166 addps xmm1, xmm7 ; xmm1=tmp10 167 addps xmm6, xmm4 ; xmm6=tmp11 168 169 addps xmm0, xmm5 170 mulps xmm0, [GOTOFF(ebx,PD_0_707)] ; xmm0=z1 171 172 movaps xmm7, xmm1 173 movaps xmm4, xmm5 174 subps xmm1, xmm6 ; xmm1=data4 175 subps xmm5, xmm0 ; xmm5=data6 176 addps xmm7, xmm6 ; xmm7=data0 177 addps xmm4, xmm0 ; xmm4=data2 178 179 movaps XMMWORD [XMMBLOCK(0,1,edx,SIZEOF_FAST_FLOAT)], xmm1 180 movaps XMMWORD [XMMBLOCK(2,1,edx,SIZEOF_FAST_FLOAT)], xmm5 181 movaps XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)], xmm7 182 movaps XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)], xmm4 183 184 ; -- Odd part 185 186 movaps xmm6, XMMWORD [wk(0)] ; xmm6=tmp6 187 movaps xmm0, XMMWORD [wk(1)] ; xmm0=tmp7 188 189 addps xmm2, xmm3 ; xmm2=tmp10 190 addps xmm3, xmm6 ; xmm3=tmp11 191 addps xmm6, xmm0 ; xmm6=tmp12, xmm0=tmp7 192 193 mulps xmm3, [GOTOFF(ebx,PD_0_707)] ; xmm3=z3 194 195 movaps xmm1, xmm2 ; xmm1=tmp10 196 subps xmm2, xmm6 197 mulps xmm2, [GOTOFF(ebx,PD_0_382)] ; xmm2=z5 198 mulps xmm1, [GOTOFF(ebx,PD_0_541)] ; xmm1=MULTIPLY(tmp10,FIX_0_541196) 199 mulps xmm6, [GOTOFF(ebx,PD_1_306)] ; xmm6=MULTIPLY(tmp12,FIX_1_306562) 200 addps xmm1, xmm2 ; xmm1=z2 201 addps xmm6, xmm2 ; xmm6=z4 202 203 movaps xmm5, xmm0 204 subps xmm0, xmm3 ; xmm0=z13 205 addps xmm5, xmm3 ; xmm5=z11 206 207 movaps xmm7, xmm0 208 movaps xmm4, xmm5 209 subps xmm0, xmm1 ; xmm0=data3 210 subps xmm5, xmm6 ; xmm5=data7 211 addps xmm7, xmm1 ; xmm7=data5 212 addps xmm4, xmm6 ; xmm4=data1 213 214 movaps XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)], xmm0 215 movaps XMMWORD [XMMBLOCK(3,1,edx,SIZEOF_FAST_FLOAT)], xmm5 216 movaps XMMWORD [XMMBLOCK(1,1,edx,SIZEOF_FAST_FLOAT)], xmm7 217 movaps XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)], xmm4 218 219 add edx, 4*DCTSIZE*SIZEOF_FAST_FLOAT 220 dec ecx 221 jnz near .rowloop 222 223 ; ---- Pass 2: process columns. 224 225 mov edx, POINTER [data(eax)] ; (FAST_FLOAT *) 226 mov ecx, DCTSIZE/4 227 alignx 16, 7 228.columnloop: 229 230 movaps xmm0, XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)] 231 movaps xmm1, XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)] 232 movaps xmm2, XMMWORD [XMMBLOCK(6,0,edx,SIZEOF_FAST_FLOAT)] 233 movaps xmm3, XMMWORD [XMMBLOCK(7,0,edx,SIZEOF_FAST_FLOAT)] 234 235 ; xmm0=(02 12 22 32), xmm2=(42 52 62 72) 236 ; xmm1=(03 13 23 33), xmm3=(43 53 63 73) 237 238 movaps xmm4, xmm0 ; transpose coefficients(phase 1) 239 unpcklps xmm0, xmm1 ; xmm0=(02 03 12 13) 240 unpckhps xmm4, xmm1 ; xmm4=(22 23 32 33) 241 movaps xmm5, xmm2 ; transpose coefficients(phase 1) 242 unpcklps xmm2, xmm3 ; xmm2=(42 43 52 53) 243 unpckhps xmm5, xmm3 ; xmm5=(62 63 72 73) 244 245 movaps xmm6, XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)] 246 movaps xmm7, XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)] 247 movaps xmm1, XMMWORD [XMMBLOCK(4,0,edx,SIZEOF_FAST_FLOAT)] 248 movaps xmm3, XMMWORD [XMMBLOCK(5,0,edx,SIZEOF_FAST_FLOAT)] 249 250 ; xmm6=(00 10 20 30), xmm1=(40 50 60 70) 251 ; xmm7=(01 11 21 31), xmm3=(41 51 61 71) 252 253 movaps XMMWORD [wk(0)], xmm4 ; wk(0)=(22 23 32 33) 254 movaps XMMWORD [wk(1)], xmm2 ; wk(1)=(42 43 52 53) 255 256 movaps xmm4, xmm6 ; transpose coefficients(phase 1) 257 unpcklps xmm6, xmm7 ; xmm6=(00 01 10 11) 258 unpckhps xmm4, xmm7 ; xmm4=(20 21 30 31) 259 movaps xmm2, xmm1 ; transpose coefficients(phase 1) 260 unpcklps xmm1, xmm3 ; xmm1=(40 41 50 51) 261 unpckhps xmm2, xmm3 ; xmm2=(60 61 70 71) 262 263 movaps xmm7, xmm6 ; transpose coefficients(phase 2) 264 unpcklps2 xmm6, xmm0 ; xmm6=(00 01 02 03)=data0 265 unpckhps2 xmm7, xmm0 ; xmm7=(10 11 12 13)=data1 266 movaps xmm3, xmm2 ; transpose coefficients(phase 2) 267 unpcklps2 xmm2, xmm5 ; xmm2=(60 61 62 63)=data6 268 unpckhps2 xmm3, xmm5 ; xmm3=(70 71 72 73)=data7 269 270 movaps xmm0, xmm7 271 movaps xmm5, xmm6 272 subps xmm7, xmm2 ; xmm7=data1-data6=tmp6 273 subps xmm6, xmm3 ; xmm6=data0-data7=tmp7 274 addps xmm0, xmm2 ; xmm0=data1+data6=tmp1 275 addps xmm5, xmm3 ; xmm5=data0+data7=tmp0 276 277 movaps xmm2, XMMWORD [wk(0)] ; xmm2=(22 23 32 33) 278 movaps xmm3, XMMWORD [wk(1)] ; xmm3=(42 43 52 53) 279 movaps XMMWORD [wk(0)], xmm7 ; wk(0)=tmp6 280 movaps XMMWORD [wk(1)], xmm6 ; wk(1)=tmp7 281 282 movaps xmm7, xmm4 ; transpose coefficients(phase 2) 283 unpcklps2 xmm4, xmm2 ; xmm4=(20 21 22 23)=data2 284 unpckhps2 xmm7, xmm2 ; xmm7=(30 31 32 33)=data3 285 movaps xmm6, xmm1 ; transpose coefficients(phase 2) 286 unpcklps2 xmm1, xmm3 ; xmm1=(40 41 42 43)=data4 287 unpckhps2 xmm6, xmm3 ; xmm6=(50 51 52 53)=data5 288 289 movaps xmm2, xmm7 290 movaps xmm3, xmm4 291 addps xmm7, xmm1 ; xmm7=data3+data4=tmp3 292 addps xmm4, xmm6 ; xmm4=data2+data5=tmp2 293 subps xmm2, xmm1 ; xmm2=data3-data4=tmp4 294 subps xmm3, xmm6 ; xmm3=data2-data5=tmp5 295 296 ; -- Even part 297 298 movaps xmm1, xmm5 299 movaps xmm6, xmm0 300 subps xmm5, xmm7 ; xmm5=tmp13 301 subps xmm0, xmm4 ; xmm0=tmp12 302 addps xmm1, xmm7 ; xmm1=tmp10 303 addps xmm6, xmm4 ; xmm6=tmp11 304 305 addps xmm0, xmm5 306 mulps xmm0, [GOTOFF(ebx,PD_0_707)] ; xmm0=z1 307 308 movaps xmm7, xmm1 309 movaps xmm4, xmm5 310 subps xmm1, xmm6 ; xmm1=data4 311 subps xmm5, xmm0 ; xmm5=data6 312 addps xmm7, xmm6 ; xmm7=data0 313 addps xmm4, xmm0 ; xmm4=data2 314 315 movaps XMMWORD [XMMBLOCK(4,0,edx,SIZEOF_FAST_FLOAT)], xmm1 316 movaps XMMWORD [XMMBLOCK(6,0,edx,SIZEOF_FAST_FLOAT)], xmm5 317 movaps XMMWORD [XMMBLOCK(0,0,edx,SIZEOF_FAST_FLOAT)], xmm7 318 movaps XMMWORD [XMMBLOCK(2,0,edx,SIZEOF_FAST_FLOAT)], xmm4 319 320 ; -- Odd part 321 322 movaps xmm6, XMMWORD [wk(0)] ; xmm6=tmp6 323 movaps xmm0, XMMWORD [wk(1)] ; xmm0=tmp7 324 325 addps xmm2, xmm3 ; xmm2=tmp10 326 addps xmm3, xmm6 ; xmm3=tmp11 327 addps xmm6, xmm0 ; xmm6=tmp12, xmm0=tmp7 328 329 mulps xmm3, [GOTOFF(ebx,PD_0_707)] ; xmm3=z3 330 331 movaps xmm1, xmm2 ; xmm1=tmp10 332 subps xmm2, xmm6 333 mulps xmm2, [GOTOFF(ebx,PD_0_382)] ; xmm2=z5 334 mulps xmm1, [GOTOFF(ebx,PD_0_541)] ; xmm1=MULTIPLY(tmp10,FIX_0_541196) 335 mulps xmm6, [GOTOFF(ebx,PD_1_306)] ; xmm6=MULTIPLY(tmp12,FIX_1_306562) 336 addps xmm1, xmm2 ; xmm1=z2 337 addps xmm6, xmm2 ; xmm6=z4 338 339 movaps xmm5, xmm0 340 subps xmm0, xmm3 ; xmm0=z13 341 addps xmm5, xmm3 ; xmm5=z11 342 343 movaps xmm7, xmm0 344 movaps xmm4, xmm5 345 subps xmm0, xmm1 ; xmm0=data3 346 subps xmm5, xmm6 ; xmm5=data7 347 addps xmm7, xmm1 ; xmm7=data5 348 addps xmm4, xmm6 ; xmm4=data1 349 350 movaps XMMWORD [XMMBLOCK(3,0,edx,SIZEOF_FAST_FLOAT)], xmm0 351 movaps XMMWORD [XMMBLOCK(7,0,edx,SIZEOF_FAST_FLOAT)], xmm5 352 movaps XMMWORD [XMMBLOCK(5,0,edx,SIZEOF_FAST_FLOAT)], xmm7 353 movaps XMMWORD [XMMBLOCK(1,0,edx,SIZEOF_FAST_FLOAT)], xmm4 354 355 add edx, byte 4*SIZEOF_FAST_FLOAT 356 dec ecx 357 jnz near .columnloop 358 359; pop edi ; unused 360; pop esi ; unused 361; pop edx ; need not be preserved 362; pop ecx ; need not be preserved 363 poppic ebx 364 mov esp, ebp ; esp <- aligned ebp 365 pop esp ; esp <- original ebp 366 pop ebp 367 ret 368 369; For some reason, the OS X linker does not honor the request to align the 370; segment unless we do this. 371 align 32 372