Debian Codegraph @

lib/common/mmx.cpp

raw
1 /********************************************************
2
3 Miscellaneous MMX-accelerated routines
4 Copyright 2000 Eugene Kuznetsov ([email protected])
5
6 *********************************************************/
7
8 #include "avm_cpuinfo.h"
9 #include "mmx.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #if !defined WIN32 && ARCH_X86_32
14
15 static inline void emms(void)
16 {
17 __asm __volatile ("emms;": : :"memory");
18 }
19
20 #define emms_c() \
21 { \
22 emms(); \
23 }
24
25 void mmx_copy(void* to, const void* from, uint_t size)
26 {
27 uint_t sx = size;
28 uint_t s = size & 0x3f;
29 if (s)
30 {
31 int dummy;
32 asm volatile
33 (
34 "rep; movsb"
35 :"=&D"(to), "=&S"(from), "=&c"(dummy)
36 :"0"(to), "1"(from), "2"(s)
37 : "memory"
38 );
39 }
40
41 while (s < sx)
42 {
43 asm volatile
44 (
45 #if 0
46 ".balign 16 \n\t"
47 "1: \n\t"
48 "movq (%0, %2), %%mm0\n\t"
49 "movq 8(%0, %2), %%mm1\n\t"
50 "movq 16(%0, %2), %%mm2\n\t"
51 "movq 24(%0, %2), %%mm3\n\t"
52 "movq 32(%0, %2), %%mm4\n\t"
53 "movq 40(%0, %2), %%mm5\n\t"
54 "movq 48(%0, %2), %%mm6\n\t"
55 "movq 56(%0, %2), %%mm7\n\t"
56 "movq %%mm0, (%1, %2)\n\t"
57 "movq %%mm1, 8(%1, %2)\n\t"
58 "movq %%mm2, 16(%1, %2)\n\t"
59 "movq %%mm3, 24(%1, %2)\n\t"
60 "movq %%mm4, 32(%1, %2)\n\t"
61 "movq %%mm5, 40(%1, %2)\n\t"
62 "movq %%mm6, 48(%1, %2)\n\t"
63 "movq %%mm7, 56(%1, %2)\n\t"
64 #else
65 ".balign 16 \n\t"
66 "1: \n\t"
67 "movq (%0, %2), %%mm0\n\t"
68 "movq 8(%0, %2), %%mm1\n\t"
69 "movq %%mm0, (%1, %2)\n\t"
70 "movq 16(%0, %2), %%mm2\n\t"
71 "movq 24(%0, %2), %%mm3\n\t"
72 "movq 32(%0, %2), %%mm4\n\t"
73 "movq 40(%0, %2), %%mm5\n\t"
74 "movq 48(%0, %2), %%mm6\n\t"
75 "movq 56(%0, %2), %%mm7\n\t"
76 "movq %%mm1, 8(%1, %2)\n\t"
77 "movq %%mm2, 16(%1, %2)\n\t"
78 "movq %%mm3, 24(%1, %2)\n\t"
79 "movq %%mm4, 32(%1, %2)\n\t"
80 "movq %%mm5, 40(%1, %2)\n\t"
81 "movq %%mm6, 48(%1, %2)\n\t"
82 "movq %%mm7, 56(%1, %2)\n\t"
83 #endif
84 :
85 :"r"(from), "r"(to), "r"(s)
86 :"memory"
87 );
88 s += 64;
89 }
90 emms_c();
91 }
92
93
94 static void zoom_16_bpp(uint16_t* dest, const uint16_t* src, int dst_w, int dst_h, int src_w, int src_h, int xdim)
95 {
96 int x_min = (src_w%dst_w);
97 int y_min = src_h%dst_h;
98 int x_accum = 0;
99 int y_accum = 0;
100 const uint16_t *src2=src;
101 int dest_delta = (xdim!=0 ? xdim-dst_w : 0);
102
103 int x_maj = src_w/dst_w*2;
104 int y_maj = (src_h/dst_h)*src_w;
105 if ((x_maj>0) || (y_maj>0))
106 {
107 for(int i=0; i<dst_h; i++)
108 {
109 int j = dst_w;
110 //printf("%d %p %p j:%d\n", y_accum, dest, src2, j);
111 // for(int j=0; j<dst_w; j++)
112 asm volatile
113 ("pushl %%ebx \n\t"
114 "1: \n\t"
115 // *(dest+j)=*src;
116 "movw (%%eax),%%bx \n\t"
117 "movw %%bx, (%%ecx) \n\t"
118 // dest++;
119 "addl $2, %%ecx \n\t"
120 // src+=x_maj;
121 "addl %9, %%eax \n\t"
122 // x_accum-=x_min;
123 "subl %6, %%edx \n\t"
124 // if(x_accum<=0) {
125 "jnc 2 \n\t"
126 // xaccum+=dst_w;
127 "addl %7, %%edx \n\t"
128 // src++;
129 "addl $2, %%eax \n\t"
130 "2: \n\t"
131 "decl %8 \n\t"
132 "jnz 1b \n\t"
133 "popl %%ebx \n\t"
134 : "=a"(src), "=c"(dest), "=d"(x_accum) // output
135 : "a"(src),"c"(dest), "d"(x_accum), "r"(x_min), "g"(dst_w), "r"(j), "g"(x_maj)
136 :"memory"
137 );
138
139 dest+=dest_delta;
140 src2+=y_maj;
141 y_accum+=y_min;
142 if(y_accum>=dst_h)
143 {
144 y_accum-=dst_h;
145 src2+=src_w;
146 }
147
148 src=src2;
149 }
150 }
151 else
152 // cout << "FIXME error - Tried to use ASM code which is not shlib friendly" << endl;
153
154 // has to be fixed - causes reference .rel.text
155 // objdump --headers --private-headers -T libaviplay.so | less
156 // if this output doesn't contain .rel.text than its OK!
157 #if 0
158 // BUGGY
159 {
160 int dest2=0;
161 int i=0;
162 __asm__ __volatile__
163 (
164 "pushal \n\t"
165 "subl $16, %%esp \n\t"
166 "movl %5, %%ebx \n\t" // 8(%%esp) = dest_h
167 "movl %%ebx, 8(%%esp) \n\t"
168 "movl %4, %%ebx \n\t" // 12(%%esp) = dest_w
169 "movl %%ebx, 12(%%esp) \n\t"
170 "movl %5, %%ebx \n\t" // i = src_h
171
172 "movl %%ebx, (%%esp) \n\t"
173
174 "for_i: \n\t"
175 "movl %0, 4(%%esp) \n\t" // 4(%%esp) = dest
176
177 "movl 12(%%esp), %%edx \n\t"
178
179 "movl %4, %%ecx \n\t" // j = src_w
180 "for_j: \n\t"
181
182 "movw (%1), %%bx \n\t"
183
184 "x_dest_copy_again: \n\t"
185 "movw %%bx, (%0) \n\t" // *dest = *src
186 "addl $2, %0 \n\t" // dest++;
187 "subl %6, %%edx \n\t" // 12(%%esp)-=x_min
188 "jnc x_dest_copy_again \n\t" // while (12(%%esp) > 0)
189
190 "no_xdestcopy: \n\t"
191 "addl %2, %%edx \n\t" // 12(%%esp) += dest_w
192 "addl $2, %1 \n\t" // src++;
193 "decl %%ecx \n\t" // j--;
194 "jnz for_j \n\t" // while (j>0) Next
195
196 "y: \n\t"
197 "movl %%edx, 12(%%esp) \n\t"
198
199 "addl %8, %0 \n\t" // dest += dest_delta
200
201 "movl 8(%%esp), %%edx \n\t"
202 "push %1 \n\t" // safe src
203
204 "y_dest_copy_again: \n\t"
205 "subl %7, %%edx \n\t" // 8(%%esp) -= y_min
206 "jc y_no_cpy_again \n\t" // while (8(%%esp)>0)
207 "movl 8(%%esp),%%esi \n\t"
208 "movl %2, %%ecx \n\t"
209 "shrl $1,%%ecx \n\t"
210 "cld \n\t"
211 "rep; movsl \n\t"
212 "addl %8, %0 \n\t" // dest += dest_delta
213
214 "jmp y_dest_copy_again \n\t"
215
216 "y_no_cpy_again: \n\t"
217 "addl %3, %%edx \n\t" // 8(%%esp) += dest_h
218 "movl %%edx, 12(%%esp) \n\t"
219
220 "pop %1 \n\t" // get src
221
222 "decl (%%esp) \n\t"
223 "jnz for_i \n\t"
224 "addl $16, %%esp \n\t"
225
226 "popal \n\t"
227 :
228 : "D"(dest), "S"(src), "r"(dst_w), "r"(dst_h), "r"(src_w), "r"(src_h), "a"(x_min), "r"(y_min), "r"(dest_delta)
229 :"memory"
230 );
231 }
232 #else
233 {
234 // slow dumb implementation which doesn't work
235 uint16_t* pdest = dest;
236 for (int i = 0; i < src_w && i < dst_h; i++)
237 {
238 for (int j = 0; j < src_h; j++)
239 pdest[j] = *src++;
240 pdest += dst_w;
241 }
242 }
243 #endif
244 }
245
246
247 static void zoom_24_bpp(int* dest, const int* src, int dst_w, int dst_h, int src_w, int src_h, int xdim)
248 {
249 int x_maj = src_w/dst_w;
250 int x_min = src_w%dst_w;
251 int y_maj = (src_h/dst_h)*src_w;
252 int y_min = src_h%dst_h;
253 int x_accum = 0;
254 int y_accum = 0;
255 const int *src2=src;
256 int dest_delta = (xdim!=0 ? 3*(xdim-dst_w) : 0);
257
258 // cout << "(" << dst_w << "," << dst_h << "," << src_w << "," << src_h << ")" << endl;
259 for (int i=0; i<dst_h; i++)
260 {
261
262 for(int j=0; j<dst_w; j++)
263 {
264 *dest=*src;
265 dest=(int*)((char*)dest+3);
266
267 src=(const int*)((const char*)src+x_maj*3);
268 x_accum+=x_min;
269 if(x_accum>=dst_w) {
270 x_accum-=dst_w;
271 src=(const int*)((const char*)src+3);
272 }
273 }
274
275 dest = (int*) ((char*) dest + dest_delta);
276
277 src2=(const int*)((const char*)src2+y_maj*3);
278 y_accum+=y_min;
279 if(y_accum>=dst_h)
280 {
281 y_accum-=dst_h;
282 src2=(const int*)((const char*)src2+3*src_w);
283 }
284 src=src2;
285 }
286 }
287
288 static void zoom_32_bpp(int* dest, const int* src, int dst_w, int dst_h, int src_w, int src_h, int xdim)
289 {
290 int x_maj = src_w/dst_w;
291 int x_min = src_w%dst_w;
292 int y_maj = (src_h/dst_h)*src_w;
293 int y_min = src_h%dst_h;
294 int x_accum = 0;
295 int y_accum = 0;
296 const int *src2=src;
297 int dest_delta = (xdim!=0 ? xdim-dst_w : 0);
298
299 // cout << "(" << dst_w << "," << dst_h << "," << src_w << "," << src_h << ")" << endl;
300 for(int i=0; i<dst_h; i++)
301 {
302 for(int j=0; j<dst_w; j++)
303 {
304 *dest=*src;
305 dest++;
306
307 src+=x_maj;
308 x_accum+=x_min;
309 if(x_accum>=dst_w) {
310 x_accum-=dst_w;
311 src++;
312 }
313 }
314
315 dest+=dest_delta;
316 src2+=y_maj;
317 y_accum+=y_min;
318 if(y_accum>=dst_h)
319 {
320 y_accum-=dst_h;
321 src2+=src_w;
322 }
323 src=src2;
324 }
325 }
326
327 void zoom(uint16_t* dest, const uint16_t* src, int dst_w, int dst_h, int src_w, int src_h, int bpp, int xdim)
328 {
329 switch(bpp)
330 {
331 case 15:
332 case 16:
333 return zoom_16_bpp(dest,src,dst_w,dst_h,src_w,src_h,xdim);
334 case 24:
335 return zoom_24_bpp((int*)dest,(const int*)src,dst_w,dst_h,src_w,src_h,xdim);
336 case 32:
337 return zoom_32_bpp((int*)dest,(const int*)src,dst_w,dst_h,src_w,src_h,xdim);
338 }
339 }
340
341 #endif // WIN32
342
343
344 static void zoom_2_16_nommx(uint16_t* dest, const uint16_t* src, int w, int h)
345 {
346 for(int i=0; i<h; i+=2)
347 {
348 for(int j=0; j<w; j+=2)
349 {
350 *dest=*src;
351 dest++;
352 src+=2;
353 }
354 src+=w;
355 }
356 }
357
358 static void zoom_2_16_to565_nommx(uint16_t *dest, const uint16_t *src, int w, int h)
359 {
360 // dest+=w/2*(h/2-1);
361 for(int i=0; i<h/2; i++)
362 {
363 for(int j=0; j<w/2; j++)
364 {
365 uint16_t q = *src++;
366 *dest++ = uint16_t(q + uint16_t(q & 0xFFE0U));
367 }
368 src += w;
369 // dest-=w;
370 }
371 }
372
373 static void v555to565_nommx(uint16_t* dest, const uint16_t* src, int w, int h)
374 {
375 // one line - convertor is in image.cpp anyway
376 // this code is not used
377 uint16_t* d = dest;
378 const uint16_t* s = src;
379 while (w > 1)
380 {
381 w -= 2;
382 unsigned q = *((uint32_t *)&s[w]);
383 *((uint32_t *)&d[w]) = (q & 0x7FFF7FFFU) + (q & 0x7FE07FE0U);
384 }
385 if (w)
386 {
387 uint16_t q = s[0];
388 ;
389 d[0] = uint16_t(q + uint16_t(q & 0x7FE0U));
390 }
391 }
392
393 #if !defined WIN32 && ARCH_X86_32
394 /*********************************************
395
396 WARNING
397
398 All MMX code assumes that dest scanline sizes
399 are multiple of 8 bytes.
400
401 *********************************************/
402 static void v555to565_mmx(uint16_t* dest, const uint16_t* src, int w, int h)
403 {
404 static uint64_t line __attribute__ ((aligned(8))) = 0xFFE0FFE0FFE0FFE0LL;
405
406 bool flip=(h<0);
407 if(flip)
408 {
409 h=-h;
410 src+=w*(h-1);
411 }
412 __asm__ __volatile__
413 (
414 "movq %0, %%mm2 \n\t"
415 : : "m"(line)
416 );
417
418 // dest+=w*(h-1);
419
420 for(int i=0; i<h; i++)
421 {
422 __asm__ __volatile__
423 (
424 "1: \n\t"
425 "movq (%0), %%mm0 \n\t"
426 "movq %%mm0, %%mm1 \n\t"
427 "pand %%mm2, %%mm1 \n\t"
428 "paddw %%mm1, %%mm0 \n\t"
429 "movq %%mm0, (%1) \n\t"
430 "addl $8, %0 \n\t"
431 "addl $8, %1 \n\t"
432 "cmpl %0, %2 \n\t"
433 "ja 1b \n\t"
434 : :"r"(src), "r"(dest), "r"(src + 2*w)
435 :"memory"
436 );
437 if(flip)
438 src-=w;
439 else
440 src+=w;
441 dest+=w;
442 }
443 emms_c();
444 }
445
446 static void zoom_2_16_to565_mmx(uint16_t *dest, const uint16_t *src, int w, int h)
447 {
448 static uint64_t line __attribute__ ((aligned(8))) = 0xFFE0FFE0FFE0FFE0LL;
449 static uint64_t line2 __attribute__ ((aligned(8))) = 0x0000FFFF0000FFFFLL;
450
451 __asm__ __volatile__
452 (
453 "movq %0, %%mm2 \n\t"
454 "movq %1, %%mm3 \n\t"
455 : : "m"(line), "m"(line2)
456 );
457
458 // dest+=w/2*(h/2-1);
459 for(int i=0; i<h/2; i++)
460 {
461 __asm__ __volatile__
462 (
463 "1: \n\t"
464 //Load 8 subsequent pixels into mm0 and mm1.
465 //Drop each second one by pand.
466 "movq (%0), %%mm0 \n\t"
467 "pand %%mm3, %%mm0 \n\t"
468 "movq 8(%0), %%mm1 \n\t"
469 "addl $16, %%eax \n\t"
470 "pand %%mm3, %%mm1 \n\t"
471 //Pack 4 remaining pixels into mm0.
472 "packssdw %%mm1,%%mm0 \n\t"
473 //Convert 555 -> 565.
474 "movq %%mm0, %%mm1 \n\t"
475 "pand %%mm2, %%mm1 \n\t"
476 "paddw %%mm1, %%mm0 \n\t"
477 //Store the result.
478 "movq %%mm0, (%1) \n\t"
479
480 "addl $8, %%ecx \n\t"
481 "cmpl %0, %2 \n\t"
482 "ja 1b \n\t"
483
484 :
485 :"r"(src), "r"(dest), "r"(src + 2*w)
486 :"memory"
487 );
488 src+=2*w;
489 dest+=w/2;
490 }
491 emms_c();
492 }
493
494
495 static void zoom_2_16_mmx(uint16_t *dest, const uint16_t *src, int w, int h)
496 {
497 static uint64_t line2 __attribute__ ((aligned(8))) = 0x0000FFFF0000FFFFLL;
498
499 __asm__ __volatile__
500 (
501 "movq %0, %%mm3 \n\t"
502 : : "m"(line2)
503 );
504
505 for(int i=0; i<h/2; i++)
506 {
507 __asm__ __volatile__
508 (
509 "1: \n\t"
510 //Load 8 subsequent pixels into mm0 and mm1.
511 //Drop each second one by pand.
512 "movq (%0), %%mm0 \n\t"
513 "pand %%mm3, %%mm0 \n\t"
514 "movq 8(%0), %%mm1 \n\t"
515 "addl $16, %%eax \n\t"
516 "pand %%mm3, %%mm1 \n\t"
517 //Pack 4 remaining pixels into mm0.
518 "packssdw %%mm1, %%mm0 \n\t"
519 //Store the result.
520 "movq %%mm0, (%1) \n\t"
521
522 "addl $8, %1 \n\t"
523 "cmpl %0, %2 \n\t"
524 "ja 1b \n\t"
525
526 : :"r"(src), "r"(dest), "r"(src + 2*w)
527 :"memory"
528 );
529 src+=2*w;
530 dest+=w/2;
531 }
532 emms_c();
533 }
534
535
536 void zoom_2_32_mmx(uint32_t *dest, const uint32_t *src, int w, int h)
537 {
538 static uint64_t line2 __attribute__ ((aligned(8))) = 0x00000000FFFFFFFFLL;
539
540 // dest+=w/2*(h/2-1);
541 for(int i=0; i<h/2; i++)
542 {
543 __asm__ __volatile__
544 (
545 "1: \n\t"
546 //Load 4 subsequent pixels into mm0 and mm1.
547 "movq (%0), %%mm0 \n\t"
548 "movq 8(%0), %%mm1 \n\t"
549 "addl $16, %0 \n\t"
550 //Put 2 pixels into mm0.
551 //this should work, but I'm too lazy to check it.
552 "punpckhdq %%mm1, %%mm0 \n\t"
553 //Store the result.
554 "movq %%mm0, (%1) \n\t"
555
556 "addl $8, %1 \n\t"
557 "cmpl %0, %2 \n\t"
558 "ja 1b \n\t"
559
560 : :"r"(src), "r"(dest), "r"(src + 4 * w)
561 :"memory"
562 );
563 src+=2*w;
564 dest+=w/2;
565 }
566 emms_c();
567 }
568
569 void zoom_2_32_nommx(uint32_t *dest, const uint32_t *src, int w, int h)
570 {
571 for (int i=0; i<h/2; i++)
572 {
573 for (int j=0; j<w/2; j++)
574 {
575 *dest=*src;
576 dest++;
577 src+=2;
578 }
579 src+=w;
580 }
581 }
582
583 static void v555to565_stub(uint16_t* dest, const uint16_t* src, int w, int h)
584 {
585 if (freq.HaveMMX())
586 v555to565=v555to565_mmx;
587 else
588 v555to565=v555to565_nommx;
589
590 v555to565(dest,src,w,h);
591 }
592
593 static void zoom_2_16_stub(uint16_t* dest, const uint16_t* src, int w, int h)
594 {
595 if(freq.HaveMMX())
596 zoom_2_16=zoom_2_16_mmx;
597 else
598 zoom_2_16=zoom_2_16_nommx;
599 zoom_2_16(dest,src,w,h);
600 }
601
602 static void zoom_2_16_to565_stub(uint16_t *dest, const uint16_t *src, int w, int h)
603 {
604 if(freq.HaveMMX())
605 zoom_2_16_to565=zoom_2_16_to565_mmx;
606 else
607 zoom_2_16_to565=zoom_2_16_to565_nommx;
608 zoom_2_16_to565(dest,src,w,h);
609 }
610
611 static void zoom_2_32_stub(uint32_t *dest, const uint32_t *src, int w, int h)
612 {
613 if(freq.HaveMMX())
614 zoom_2_32=zoom_2_32_mmx;
615 else
616 zoom_2_32=zoom_2_32_nommx;
617 zoom_2_32(dest,src,w,h);
618 }
619
620 void (*v555to565)(uint16_t*, const uint16_t*, int, int)=v555to565_stub;
621 void (*zoom_2_16)(uint16_t*, const uint16_t*, int, int)=zoom_2_16_stub;
622 void (*zoom_2_16_to565)(uint16_t *, const uint16_t *, int,int)=zoom_2_16_to565_stub;
623 void (*zoom_2_32)(uint32_t *, const uint32_t *, int, int)=zoom_2_32_stub;
624
625 #else
626
627 void (*v555to565)(uint16_t*, const uint16_t*, int, int)=v555to565_nommx;
628 void zoom(uint16_t* dest, const uint16_t* src, int dst_w, int dst_h, int src_w, int src_h, int bpp, int xdim)
629 {
630 #warning ZOOM has to be written
631 }
632 #endif