-
Notifications
You must be signed in to change notification settings - Fork 1
/
idctfpu.cpp
450 lines (416 loc) · 9.81 KB
/
idctfpu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* idct.c, inverse fast discrete cosine transform */
/*************************************************************/
/* inverse two dimensional DCT, Chen-Wang algorithm */
/* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
/* */
/* floating point conversion by Miha Peternel */
/* x87 hand-optimized assembly by Miha Peternel */
/* 27.11. - 11.12.2000 */
/* */
/* You are free to use this code in your project if: */
/* - no changes are made to this message */
/* - any changes to this code are publicly available */
/* - your project documentation contains the following text: */
/* "This software contains fast high-quality IDCT decoder */
/* by Miha Peternel." */
/* */
/*************************************************************/
/////////////////////////////////////////////////////
//
// TODO:
// - loops can be easily vectorized for SIMD
//
/////////////////////////////////////////////////////
#include <math.h>
#define PI 3.1415926535897932384626433832795
#define FLOAT double
const static double RC = 1.0*1024*1024*1024*1024*256*16 + 1024; // magic + clip center
static FLOAT W1; // /* sqrt(2)*cos(1*pi/16) */
static FLOAT W2; // /* sqrt(2)*cos(2*pi/16) */
static FLOAT W5; // /* sqrt(2)*cos(5*pi/16) */
static FLOAT W1_8;
static FLOAT W2_8;
static FLOAT W5_8;
static FLOAT W7; // /* sqrt(2)*cos(7*pi/16) */
static FLOAT W1mW7; // W1-W7
static FLOAT W1pW7; // W1+W7
static FLOAT W3; // /* sqrt(2)*cos(3*pi/16) */
static FLOAT W3mW5; // W3-W5
static FLOAT W3pW5; // W3+W5
static FLOAT W6; // /* sqrt(2)*cos(6*pi/16) */
static FLOAT W2mW6; // W2-W6
static FLOAT W2pW6; // W2+W6
static FLOAT S2; // 1/sqrt(2)
static FLOAT D8 = 1.0/8;
static FLOAT W7_8;
static FLOAT W1mW7_8;
static FLOAT W1pW7_8;
static FLOAT W3_8;
static FLOAT W3mW5_8;
static FLOAT W3pW5_8;
static FLOAT W6_8;
static FLOAT W2mW6_8;
static FLOAT W2pW6_8;
/* private data */
static short iclip[1024+1024]; /* clipping table */
void Initialize_FPU_IDCT()
{
int i;
S2 = sqrt(0.5); // 1.0/sqrt(2);
W1 = sqrt(2.0)*cos(PI*(1.0/16));
W1_8 = W1/8;
W2 = sqrt(2.0)*cos(PI*(2.0/16));
W2_8 = W2/8;
W3 = sqrt(2.0)*cos(PI*(3.0/16));
W3_8 = W3/8;
W5 = sqrt(2.0)*cos(PI*(5.0/16));
W5_8 = W5/8;
W6 = sqrt(2.0)*cos(PI*(6.0/16));
W6_8 = W6/8;
W7 = sqrt(2.0)*cos(PI*(7.0/16));
W7_8 = W7/8;
W1mW7 = W1-W7; W1mW7_8 = W1mW7/8;
W1pW7 = W1+W7; W1pW7_8 = W1pW7/8;
W3mW5 = W3-W5; W3mW5_8 = W3mW5/8;
W3pW5 = W3+W5; W3pW5_8 = W3pW5/8;
W2mW6 = W2-W6; W2mW6_8 = W2mW6/8;
W2pW6 = W2+W6; W2pW6_8 = W2pW6/8;
for (i= -1024; i<1024; i++)
iclip[i+1024] = (i<-256) ? -256 : ((i>255) ? 255 : i);
}
void FPU_IDCT(short *block)
{
int *b = (int *) block;
if( b[0]==0 && (b[31]==0x10000 || b[31]==0) )
{
if( b[ 1]|b[ 2]|b[ 3]|b[ 4]|b[ 5] )
goto normal;
if( b[ 6]|b[ 7]|b[ 8]|b[ 9]|b[10] )
goto normal;
if( b[11]|b[12]|b[13]|b[14]|b[15] )
goto normal;
if( b[16]|b[17]|b[18]|b[19]|b[20] )
goto normal;
if( b[21]|b[22]|b[23]|b[24]|b[25] )
goto normal;
if( b[26]|b[27]|b[28]|b[29]|b[30] )
goto normal;
b[31]=0;
////empty++;
return;
}
normal:
#define tmp ebx
#define tmp1 ebx-1*8
#define tmp2 ebx-2*8
#define tmp3 ebx-3*8
#define int0 ebx-3*8-1*4
#define int1 ebx-3*8-2*4
#define int2 ebx-3*8-3*4
#define int3 ebx-3*8-4*4
#define int4 ebx-3*8-5*4
#define int5 ebx-3*8-6*4
#define int6 ebx-3*8-7*4
#define int7 ebx-3*8-8*4
#define SIZE 8*8*8+3*8+8*4+16 // locals + 16-byte alignment area
__asm
{
lea ebx,[esp-8*8*8]
sub esp,SIZE
and ebx,-16 // force 16-byte alignment of locals
// rows
mov esi,[block]
lea edi,[tmp]
mov ecx,8
align 16
Lrows:
movsx eax,word ptr [esi+2]
or eax, [esi+4]
or eax, [esi+8]
or eax, [esi+12]
jnz L1
fild word ptr [esi+0*2]
fst qword ptr [edi+7*8]
fst qword ptr [edi+6*8]
fst qword ptr [edi+5*8]
fst qword ptr [edi+4*8]
fst qword ptr [edi+3*8]
fst qword ptr [edi+2*8]
fst qword ptr [edi+1*8]
fstp qword ptr [edi+0*8]
jmp L2
align 16
L1:
fild word ptr [esi+7*2]
fld st(0)
fild word ptr [esi+1*2]
fadd st(1),st(0)
fld qword ptr [W7]
fxch st(1)
fmul qword ptr [W1mW7]
fxch st(1)
fmulp st(2),st(0)
fadd st(0),st(1)
fstp qword ptr [tmp1]
fild word ptr [esi+3*2]
fld st(0)
fxch st(3)
fmul qword ptr [W1pW7]
fild word ptr [esi+5*2]
fadd st(4),st(0)
fmul qword ptr [W3mW5]
fxch st(1)
fsubp st(3),st(0)//fsubrp
fld qword ptr [W3]
fmulp st(4),st(0)
fsubr st(0),st(3)
fstp qword ptr [tmp2]
fmul qword ptr [W3pW5]
fsubp st(2),st(0)//fsubrp
fxch st(1)
fstp qword ptr [tmp3]
fild word ptr [esi+0*2]
fild word ptr [esi+4*2]
fild word ptr [esi+2*2]
fld st(0)
fmul qword ptr [W2mW6]
fld st(3)
fild word ptr [esi+6*2]
fxch st(5)
fsub st(0),st(4)
fxch st(3)
fadd st(0),st(5)
fxch st(1)
faddp st(4),st(0)
fld qword ptr [W6]
fmulp st(1),st(0)
fxch st(4)
fmul qword ptr [W2pW6]
fld qword ptr [tmp1]
fsub qword ptr [tmp2]
fld st(5)
fxch st(3)
faddp st(6),st(0)
fld qword ptr [tmp1]
fxch st(1)
fstp qword ptr [tmp1]
fld st(6)
fadd qword ptr [tmp3]
fxch st(1)
fadd qword ptr [tmp2]
fxch st(7)
fsub qword ptr [tmp3]
fxch st(1)
fstp qword ptr [tmp2]
fld st(4)
fxch st(3)
fsubrp st(2),st(0)//fsubp
fxch st(4)
fsub st(0),st(5)
fxch st(2)
faddp st(5),st(0)
fld st(2)
fsub st(0),st(1)
fxch st(5)
fstp qword ptr [tmp3]
fld qword ptr [tmp1]
fld qword ptr [S2]
fxch st(4)
faddp st(2),st(0)
fld st(3)
fxch st(1)
fadd st(0),st(5)
fmulp st(1),st(0)
fld qword ptr [tmp3]
fadd st(0),st(7)
fxch st(5)
fsubr qword ptr [tmp1]
fxch st(5)
fstp qword ptr [edi+0*8]
fxch st(6)
fsubr qword ptr [tmp3]
fld st(2)
fxch st(1)
fstp qword ptr [edi+7*8]
fadd qword ptr [tmp2]
fxch st(3)
fmulp st(4),st(0)
fxch st(2)
fstp qword ptr [edi+3*8]
fld st(1)
fadd st(0),st(5)
fxch st(1)
fsub qword ptr [tmp2]
fxch st(2)
fsubrp st(5),st(0)//fsubp
fstp qword ptr [edi+1*8]
fld st(2)
fxch st(1)
fstp qword ptr [edi+4*8]
fxch st(2)
fsub st(0),st(1)
fxch st(2)
faddp st(1),st(0)
fxch st(2)
fstp qword ptr [edi+6*8]
fstp qword ptr [edi+5*8]
fstp qword ptr [edi+2*8]
L2:
add esi,8*2
add edi,8*8
dec ecx
jnz Lrows
// columns
lea esi,[tmp]
mov edi,[block]
lea edx,[iclip+1024*2]
mov ecx,8
align 16
Lcols:
fld qword ptr [esi+7*8*8]
fld st(0)
fld qword ptr [esi+1*8*8]
fadd st(1),st(0)
fld qword ptr [W7_8]
fxch st(1)
fmul qword ptr [W1mW7_8]
fxch st(1)
fmulp st(2),st(0)
fadd st(0),st(1)
fstp qword ptr [tmp2]
fld qword ptr [esi+3*8*8]
fld st(0)
fxch st(3)
fmul qword ptr [W1pW7_8]
fld qword ptr [esi+5*8*8]
fadd st(4),st(0)
fmul qword ptr [W3mW5_8]
fxch st(1)
fsubp st(3),st(0)//fsubrp
fld qword ptr [W3_8]
fmulp st(4),st(0)
fsubr st(0),st(3)
fstp qword ptr [tmp3]
fld qword ptr [D8]
fld qword ptr [esi+0*8*8]
fmul st(0),st(1)
fxch st(2)
fmul qword ptr [W3pW5_8]
fld qword ptr [esi+4*8*8]
fmulp st(2),st(0)
fld qword ptr [esi+6*8*8]
fld st(3)
fxch st(6)
fsubrp st(2),st(0)//fsubp
fld qword ptr [esi+2*8*8]
fld st(0)
fxch st(5)
fsub st(0),st(4)
fxch st(7)
faddp st(4),st(0)
fxch st(4)
fadd st(0),st(1)
fld qword ptr [W6_8]
fxch st(2)
fmul qword ptr [W2pW6_8]
fxch st(2)
fmulp st(1),st(0)
fxch st(4)
fmul qword ptr [W2mW6_8]
fld qword ptr [tmp2]
fsub qword ptr [tmp3]
fxch st(2)
fsubr st(0),st(5)
fxch st(1)
faddp st(5),st(0)
fld qword ptr [tmp2]
fxch st(2)
fstp qword ptr [tmp2]
fld st(5)
fxch st(2)
fadd qword ptr [tmp3]
fxch st(6)
fsub st(0),st(3)
fxch st(2)
faddp st(3),st(0)
fld st(3)
fsub st(0),st(5)
fxch st(3)
fstp qword ptr [tmp3]
fxch st(3)
faddp st(4),st(0)
fld st(5)
fld qword ptr [tmp2]
fxch st(7)
fsub st(0),st(4)
fxch st(7)
fadd st(0),st(2)
fxch st(1)
faddp st(4),st(0)
fld qword ptr [S2]
fmul st(1),st(0)
fxch st(1)
fstp qword ptr [tmp1]
fld st(4)
fadd st(0),st(6)
fxch st(2)
fsubr qword ptr [tmp2]
fxch st(5)
fsubrp st(6),st(0)//fsubp
fxch st(1)
fistp dword ptr [int0]
fxch st(4)
mov eax,[int0]
movsx eax,word ptr [edx+2*eax]
mov [edi+0*8*2],ax
fistp dword ptr [int7]
mov eax,[int7]
fld st(0)
movsx eax,word ptr [edx+2*eax]
mov [edi+7*8*2],ax
fadd qword ptr [tmp3]
fistp dword ptr [int3]
mov eax,[int3]
movsx eax,word ptr [edx+2*eax]
mov [edi+3*8*2],ax
fsub qword ptr [tmp3]
fld st(1)
fxch st(1)
fistp dword ptr [int4]
mov eax,[int4]
movsx eax,word ptr [edx+2*eax]
mov [edi+4*8*2],ax
fadd qword ptr [tmp1]
fxch st(3)
fmulp st(2),st(0)
fxch st(2)
fistp dword ptr [int1]
fxch st(1)
mov eax,[int1]
movsx eax,word ptr [edx+2*eax]
mov [edi+1*8*2],ax
fsub qword ptr [tmp1]
fld st(2)
fsub st(0),st(2)
fxch st(1)
fistp dword ptr [int6]
fxch st(2)
mov eax,[int6]
faddp st(1),st(0)
movsx eax,word ptr [edx+2*eax]
mov [edi+6*8*2],ax
fistp dword ptr [int2]
mov eax,[int2]
movsx eax,word ptr [edx+2*eax]
mov [edi+2*8*2],ax
fistp dword ptr [int5]
mov eax,[int5]
movsx eax,word ptr [edx+2*eax]
mov [edi+5*8*2],ax
add esi,8
add edi,2
dec ecx
jnz Lcols
add esp,SIZE
}
}