-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL2_imageFilter.c
7371 lines (6943 loc) · 304 KB
/
SDL2_imageFilter.c
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SDL2_imageFilter.c: byte-image "filter" routines
Copyright (C) 2012-2014 Andreas Schiffler
Copyright (C) 2013 Sylvain Beucler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
*/
/*
Note: Uses inline x86 MMX or ASM optimizations if available and enabled.
Note: Most of the MMX code is based on published routines
by Vladimir Kravtchenko at [email protected] - credits go to
him for his work.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
/* Use GCC intrinsics if available: they support both i386 and x86_64,
provide ASM-grade performances, and lift the PUSHA/POPA issues. */
#ifdef __GNUC__
# ifdef USE_MMX
# include <mmintrin.h>
# endif
# include <SDL_cpuinfo.h>
#endif
#include "SDL2_imageFilter.h"
/*!
\brief Swaps the byte order in a 32bit integer (LSB becomes MSB, etc.).
*/
#define SWAP_32(x) (((x) >> 24) | (((x) & 0x00ff0000) >> 8) | (((x) & 0x0000ff00) << 8) | ((x) << 24))
/* ------ Static variables ----- */
/*!
\brief Static state which enables the use of the MMX routines. Enabled by default
*/
static int SDL_imageFilterUseMMX = 1;
/* Detect GCC */
#if defined(__GNUC__)
#define GCC__
#endif
/*!
\brief MMX detection routine (with override flag).
\returns 1 of MMX was detected, 0 otherwise.
*/
int SDL_imageFilterMMXdetect(void)
{
/* Check override flag */
if (SDL_imageFilterUseMMX == 0) {
return (0);
}
return SDL_HasMMX();
}
/*!
\brief Disable MMX check for filter functions and and force to use non-MMX C based code.
*/
void SDL_imageFilterMMXoff()
{
SDL_imageFilterUseMMX = 0;
}
/*!
\brief Enable MMX check for filter functions and use MMX code if available.
*/
void SDL_imageFilterMMXon()
{
SDL_imageFilterUseMMX = 1;
}
/* ------------------------------------------------------------------------------------ */
/*!
\brief Internal MMX Filter using Add: D = saturation255(S1 + S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
static int SDL_imageFilterAddMMX(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov eax, Src1 /* load Src1 address into eax */
mov ebx, Src2 /* load Src2 address into ebx */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
shr ecx, 3 /* counter/8 (MMX loads 8 bytes at a time) */
align 16 /* 16 byte alignment of the loop entry */
L1010:
movq mm1, [eax] /* load 8 bytes from Src1 into mm1 */
paddusb mm1, [ebx] /* mm1=Src1+Src2 (add 8 bytes with saturation) */
movq [edi], mm1 /* store result in Dest */
add eax, 8 /* increase Src1, Src2 and Dest */
add ebx, 8 /* register pointers by 8 */
add edi, 8
dec ecx /* decrease loop counter */
jnz L1010 /* check loop termination, proceed if required */
emms /* exit MMX state */
popa
}
#else
/* i386 and x86_64 */
__m64 *mSrc1 = (__m64*)Src1;
__m64 *mSrc2 = (__m64*)Src2;
__m64 *mDest = (__m64*)Dest;
int i;
for (i = 0; i < SrcLength/8; i++) {
*mDest = _m_paddusb(*mSrc1, *mSrc2); /* Src1+Src2 (add 8 bytes with saturation) */
mSrc1++;
mSrc2++;
mDest++;
}
_m_empty(); /* clean MMX state */
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using Add: D = saturation255(S1 + S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterAdd(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
{
unsigned int i, istart;
unsigned char *cursrc1, *cursrc2, *curdst;
int result;
/* Validate input parameters */
if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
return(-1);
if (length == 0)
return(0);
if ((SDL_imageFilterMMXdetect()) && (length > 7)) {
/* Use MMX assembly routine */
SDL_imageFilterAddMMX(Src1, Src2, Dest, length);
/* Check for unaligned bytes */
if ((length & 7) > 0) {
/* Setup to process unaligned bytes */
istart = length & 0xfffffff8;
cursrc1 = &Src1[istart];
cursrc2 = &Src2[istart];
curdst = &Dest[istart];
} else {
/* No unaligned bytes - we are done */
return (0);
}
} else {
/* Setup to process whole image */
istart = 0;
cursrc1 = Src1;
cursrc2 = Src2;
curdst = Dest;
}
/* C routine to process image */
for (i = istart; i < length; i++) {
result = (int) *cursrc1 + (int) *cursrc2;
if (result > 255)
result = 255;
*curdst = (unsigned char) result;
/* Advance pointers */
cursrc1++;
cursrc2++;
curdst++;
}
return (0);
}
/*!
\brief Internal MMX Filter using Mean: D = S1/2 + S2/2
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\param Mask Mask array containing 8 bytes with 0x7F value.
]
\return Returns 0 for success or -1 for error.
*/
static int SDL_imageFilterMeanMMX(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength,
unsigned char *Mask)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov edx, Mask /* load Mask address into edx */
movq mm0, [edx] /* load Mask into mm0 */
mov eax, Src1 /* load Src1 address into eax */
mov ebx, Src2 /* load Src2 address into ebx */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
shr ecx, 3 /* counter/8 (MMX loads 8 bytes at a time) */
align 16 /* 16 byte alignment of the loop entry */
L21011:
movq mm1, [eax] /* load 8 bytes from Src1 into mm1 */
movq mm2, [ebx] /* load 8 bytes from Src2 into mm2 */
/* --- Byte shift via Word shift --- */
psrlw mm1, 1 /* shift 4 WORDS of mm1 1 bit to the right */
psrlw mm2, 1 /* shift 4 WORDS of mm2 1 bit to the right */
pand mm1, mm0 // apply Mask to 8 BYTES of mm1 */
/* byte 0x0f, 0xdb, 0xc8 */
pand mm2, mm0 // apply Mask to 8 BYTES of mm2 */
/* byte 0x0f, 0xdb, 0xd0 */
paddusb mm1, mm2 /* mm1=mm1+mm2 (add 8 bytes with saturation) */
movq [edi], mm1 /* store result in Dest */
add eax, 8 /* increase Src1, Src2 and Dest */
add ebx, 8 /* register pointers by 8 */
add edi, 8
dec ecx /* decrease loop counter */
jnz L21011 /* check loop termination, proceed if required */
emms /* exit MMX state */
popa
}
#else
/* i386 and x86_64 */
__m64 *mSrc1 = (__m64*)Src1;
__m64 *mSrc2 = (__m64*)Src2;
__m64 *mDest = (__m64*)Dest;
__m64 *mMask = (__m64*)Mask;
int i;
for (i = 0; i < SrcLength/8; i++) {
__m64 mm1 = *mSrc1,
mm2 = *mSrc2;
mm1 = _m_psrlwi(mm1, 1); /* shift 4 WORDS of mm1 1 bit to the right */
mm2 = _m_psrlwi(mm2, 1); /* shift 4 WORDS of mm2 1 bit to the right */
mm1 = _m_pand(mm1, *mMask); /* apply Mask to 8 BYTES of mm1 */
mm2 = _m_pand(mm2, *mMask); /* apply Mask to 8 BYTES of mm2 */
*mDest = _m_paddusb(mm1, mm2); /* mm1+mm2 (add 8 bytes with saturation) */
mSrc1++;
mSrc2++;
mDest++;
}
_m_empty(); /* clean MMX state */
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using Mean: D = S1/2 + S2/2
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterMean(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
{
static unsigned char Mask[8] = { 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };
unsigned int i, istart;
unsigned char *cursrc1, *cursrc2, *curdst;
int result;
/* Validate input parameters */
if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
return(-1);
if (length == 0)
return(0);
if ((SDL_imageFilterMMXdetect()) && (length > 7)) {
/* MMX routine */
SDL_imageFilterMeanMMX(Src1, Src2, Dest, length, Mask);
/* Check for unaligned bytes */
if ((length & 7) > 0) {
/* Setup to process unaligned bytes */
istart = length & 0xfffffff8;
cursrc1 = &Src1[istart];
cursrc2 = &Src2[istart];
curdst = &Dest[istart];
} else {
/* No unaligned bytes - we are done */
return (0);
}
} else {
/* Setup to process whole image */
istart = 0;
cursrc1 = Src1;
cursrc2 = Src2;
curdst = Dest;
}
/* C routine to process image */
for (i = istart; i < length; i++) {
result = (int) *cursrc1 / 2 + (int) *cursrc2 / 2;
*curdst = (unsigned char) result;
/* Advance pointers */
cursrc1++;
cursrc2++;
curdst++;
}
return (0);
}
/*!
\brief Internal MMX Filter using Sub: D = saturation0(S1 - S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
static int SDL_imageFilterSubMMX(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov eax, Src1 /* load Src1 address into eax */
mov ebx, Src2 /* load Src2 address into ebx */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
shr ecx, 3 /* counter/8 (MMX loads 8 bytes at a time) */
align 16 /* 16 byte alignment of the loop entry */
L1012:
movq mm1, [eax] /* load 8 bytes from Src1 into mm1 */
psubusb mm1, [ebx] /* mm1=Src1-Src2 (sub 8 bytes with saturation) */
movq [edi], mm1 /* store result in Dest */
add eax, 8 /* increase Src1, Src2 and Dest */
add ebx, 8 /* register pointers by 8 */
add edi, 8
dec ecx /* decrease loop counter */
jnz L1012 /* check loop termination, proceed if required */
emms /* exit MMX state */
popa
}
#else
/* i386 and x86_64 */
__m64 *mSrc1 = (__m64*)Src1;
__m64 *mSrc2 = (__m64*)Src2;
__m64 *mDest = (__m64*)Dest;
int i;
for (i = 0; i < SrcLength/8; i++) {
*mDest = _m_psubusb(*mSrc1, *mSrc2); /* Src1-Src2 (sub 8 bytes with saturation) */
mSrc1++;
mSrc2++;
mDest++;
}
_m_empty(); /* clean MMX state */
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using Sub: D = saturation0(S1 - S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterSub(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
{
unsigned int i, istart;
unsigned char *cursrc1, *cursrc2, *curdst;
int result;
/* Validate input parameters */
if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
return(-1);
if (length == 0)
return(0);
if ((SDL_imageFilterMMXdetect()) && (length > 7)) {
/* MMX routine */
SDL_imageFilterSubMMX(Src1, Src2, Dest, length);
/* Check for unaligned bytes */
if ((length & 7) > 0) {
/* Setup to process unaligned bytes */
istart = length & 0xfffffff8;
cursrc1 = &Src1[istart];
cursrc2 = &Src2[istart];
curdst = &Dest[istart];
} else {
/* No unaligned bytes - we are done */
return (0);
}
} else {
/* Setup to process whole image */
istart = 0;
cursrc1 = Src1;
cursrc2 = Src2;
curdst = Dest;
}
/* C routine to process image */
for (i = istart; i < length; i++) {
result = (int) *cursrc1 - (int) *cursrc2;
if (result < 0)
result = 0;
*curdst = (unsigned char) result;
/* Advance pointers */
cursrc1++;
cursrc2++;
curdst++;
}
return (0);
}
/*!
\brief Internal MMX Filter using AbsDiff: D = | S1 - S2 |
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
static int SDL_imageFilterAbsDiffMMX(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov eax, Src1 /* load Src1 address into eax */
mov ebx, Src2 /* load Src2 address into ebx */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
shr ecx, 3 /* counter/8 (MMX loads 8 bytes at a time) */
align 16 /* 16 byte alignment of the loop entry */
L1013:
movq mm1, [eax] /* load 8 bytes from Src1 into mm1 */
movq mm2, [ebx] /* load 8 bytes from Src2 into mm2 */
psubusb mm1, [ebx] /* mm1=Src1-Src2 (sub 8 bytes with saturation) */
psubusb mm2, [eax] /* mm2=Src2-Src1 (sub 8 bytes with saturation) */
por mm1, mm2 /* combine both mm2 and mm1 results */
movq [edi], mm1 /* store result in Dest */
add eax, 8 /* increase Src1, Src2 and Dest */
add ebx, 8 /* register pointers by 8 */
add edi, 8
dec ecx /* decrease loop counter */
jnz L1013 /* check loop termination, proceed if required */
emms /* exit MMX state */
popa
}
#else
/* i386 and x86_64 */
__m64 *mSrc1 = (__m64*)Src1;
__m64 *mSrc2 = (__m64*)Src2;
__m64 *mDest = (__m64*)Dest;
int i;
for (i = 0; i < SrcLength/8; i++) {
__m64 mm1 = _m_psubusb(*mSrc2, *mSrc1); /* Src1-Src2 (sub 8 bytes with saturation) */
__m64 mm2 = _m_psubusb(*mSrc1, *mSrc2); /* Src2-Src1 (sub 8 bytes with saturation) */
*mDest = _m_por(mm1, mm2); /* combine both mm2 and mm1 results */
mSrc1++;
mSrc2++;
mDest++;
}
_m_empty(); /* clean MMX state */
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using AbsDiff: D = | S1 - S2 |
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterAbsDiff(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
{
unsigned int i, istart;
unsigned char *cursrc1, *cursrc2, *curdst;
int result;
/* Validate input parameters */
if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
return(-1);
if (length == 0)
return(0);
if ((SDL_imageFilterMMXdetect()) && (length > 7)) {
/* MMX routine */
SDL_imageFilterAbsDiffMMX(Src1, Src2, Dest, length);
/* Check for unaligned bytes */
if ((length & 7) > 0) {
/* Setup to process unaligned bytes */
istart = length & 0xfffffff8;
cursrc1 = &Src1[istart];
cursrc2 = &Src2[istart];
curdst = &Dest[istart];
} else {
/* No unaligned bytes - we are done */
return (0);
}
} else {
/* Setup to process whole image */
istart = 0;
cursrc1 = Src1;
cursrc2 = Src2;
curdst = Dest;
}
/* C routine to process image */
for (i = istart; i < length; i++) {
result = abs((int) *cursrc1 - (int) *cursrc2);
*curdst = (unsigned char) result;
/* Advance pointers */
cursrc1++;
cursrc2++;
curdst++;
}
return (0);
}
/*!
\brief Internal MMX Filter using Mult: D = saturation255(S1 * S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
static int SDL_imageFilterMultMMX(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov eax, Src1 /* load Src1 address into eax */
mov ebx, Src2 /* load Src2 address into ebx */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
shr ecx, 3 /* counter/8 (MMX loads 8 bytes at a time) */
pxor mm0, mm0 /* zero mm0 register */
align 16 /* 16 byte alignment of the loop entry */
L1014:
movq mm1, [eax] /* load 8 bytes from Src1 into mm1 */
movq mm3, [ebx] /* load 8 bytes from Src2 into mm3 */
movq mm2, mm1 /* copy mm1 into mm2 */
movq mm4, mm3 /* copy mm3 into mm4 */
punpcklbw mm1, mm0 /* unpack low bytes of Src1 into words */
punpckhbw mm2, mm0 /* unpack high bytes of Src1 into words */
punpcklbw mm3, mm0 /* unpack low bytes of Src2 into words */
punpckhbw mm4, mm0 /* unpack high bytes of Src2 into words */
pmullw mm1, mm3 /* mul low bytes of Src1 and Src2 */
pmullw mm2, mm4 /* mul high bytes of Src1 and Src2 */
/* Take abs value of the results (signed words) */
movq mm5, mm1 /* copy mm1 into mm5 */
movq mm6, mm2 /* copy mm2 into mm6 */
psraw mm5, 15 /* fill mm5 words with word sign bit */
psraw mm6, 15 /* fill mm6 words with word sign bit */
pxor mm1, mm5 /* take 1's compliment of only neg. words */
pxor mm2, mm6 /* take 1's compliment of only neg. words */
psubsw mm1, mm5 /* add 1 to only neg. words, W-(-1) or W-0 */
psubsw mm2, mm6 /* add 1 to only neg. words, W-(-1) or W-0 */
packuswb mm1, mm2 /* pack words back into bytes with saturation */
movq [edi], mm1 /* store result in Dest */
add eax, 8 /* increase Src1, Src2 and Dest */
add ebx, 8 /* register pointers by 8 */
add edi, 8
dec ecx /* decrease loop counter */
jnz L1014 /* check loop termination, proceed if required */
emms /* exit MMX state */
popa
}
#else
/* i386 ASM with constraints: */
/* asm volatile ( */
/* "shr $3, %%ecx \n\t" /\* counter/8 (MMX loads 8 bytes at a time) *\/ */
/* "pxor %%mm0, %%mm0 \n\t" /\* zero mm0 register *\/ */
/* ".align 16 \n\t" /\* 16 byte alignment of the loop entry *\/ */
/* "1: movq (%%eax), %%mm1 \n\t" /\* load 8 bytes from Src1 into mm1 *\/ */
/* "movq (%%ebx), %%mm3 \n\t" /\* load 8 bytes from Src2 into mm3 *\/ */
/* "movq %%mm1, %%mm2 \n\t" /\* copy mm1 into mm2 *\/ */
/* "movq %%mm3, %%mm4 \n\t" /\* copy mm3 into mm4 *\/ */
/* "punpcklbw %%mm0, %%mm1 \n\t" /\* unpack low bytes of Src1 into words *\/ */
/* "punpckhbw %%mm0, %%mm2 \n\t" /\* unpack high bytes of Src1 into words *\/ */
/* "punpcklbw %%mm0, %%mm3 \n\t" /\* unpack low bytes of Src2 into words *\/ */
/* "punpckhbw %%mm0, %%mm4 \n\t" /\* unpack high bytes of Src2 into words *\/ */
/* "pmullw %%mm3, %%mm1 \n\t" /\* mul low bytes of Src1 and Src2 *\/ */
/* "pmullw %%mm4, %%mm2 \n\t" /\* mul high bytes of Src1 and Src2 *\/ */
/* /\* Take abs value of the results (signed words) *\/ */
/* "movq %%mm1, %%mm5 \n\t" /\* copy mm1 into mm5 *\/ */
/* "movq %%mm2, %%mm6 \n\t" /\* copy mm2 into mm6 *\/ */
/* "psraw $15, %%mm5 \n\t" /\* fill mm5 words with word sign bit *\/ */
/* "psraw $15, %%mm6 \n\t" /\* fill mm6 words with word sign bit *\/ */
/* "pxor %%mm5, %%mm1 \n\t" /\* take 1's compliment of only neg. words *\/ */
/* "pxor %%mm6, %%mm2 \n\t" /\* take 1's compliment of only neg. words *\/ */
/* "psubsw %%mm5, %%mm1 \n\t" /\* add 1 to only neg. words, W-(-1) or W-0 *\/ */
/* "psubsw %%mm6, %%mm2 \n\t" /\* add 1 to only neg. words, W-(-1) or W-0 *\/ */
/* "packuswb %%mm2, %%mm1 \n\t" /\* pack words back into bytes with saturation *\/ */
/* "movq %%mm1, (%%edi) \n\t" /\* store result in Dest *\/ */
/* "add $8, %%eax \n\t" /\* increase Src1, Src2 and Dest *\/ */
/* "add $8, %%ebx \n\t" /\* register pointers by 8 *\/ */
/* "add $8, %%edi \n\t" */
/* "dec %%ecx \n\t" /\* decrease loop counter *\/ */
/* "jnz 1b \n\t" /\* check loop termination, proceed if required *\/ */
/* "emms \n\t" /\* exit MMX state *\/ */
/* : "+a" (Src1), /\* load Src1 address into rax, modified by the loop *\/ */
/* "+b" (Src2), /\* load Src2 address into rbx, modified by the loop *\/ */
/* "+c" (SrcLength), /\* load loop counter (SIZE) into rcx, modified by the loop *\/ */
/* "+D" (Dest) /\* load Dest address into rdi, modified by the loop *\/ */
/* : */
/* : "memory", /\* *Dest is modified *\/ */
/* "mm0","mm1","mm2","mm3","mm4","mm5","mm6" /\* registers modified *\/ */
/* ); */
/* i386 and x86_64 */
__m64 *mSrc1 = (__m64*)Src1;
__m64 *mSrc2 = (__m64*)Src2;
__m64 *mDest = (__m64*)Dest;
__m64 mm0 = _m_from_int(0); /* zero mm0 register */
int i;
for (i = 0; i < SrcLength/8; i++) {
__m64 mm1, mm2, mm3, mm4, mm5, mm6;
mm1 = _m_punpcklbw(*mSrc1, mm0); /* unpack low bytes of Src1 into words */
mm2 = _m_punpckhbw(*mSrc1, mm0); /* unpack high bytes of Src1 into words */
mm3 = _m_punpcklbw(*mSrc2, mm0); /* unpack low bytes of Src2 into words */
mm4 = _m_punpckhbw(*mSrc2, mm0); /* unpack high bytes of Src2 into words */
mm1 = _m_pmullw(mm1, mm3); /* mul low bytes of Src1 and Src2 */
mm2 = _m_pmullw(mm2, mm4); /* mul high bytes of Src1 and Src2 */
mm5 = _m_psrawi(mm1, 15); /* fill mm5 words with word sign bit */
mm6 = _m_psrawi(mm2, 15); /* fill mm6 words with word sign bit */
mm1 = _m_pxor(mm1, mm5); /* take 1's compliment of only neg. words */
mm2 = _m_pxor(mm2, mm6); /* take 1's compliment of only neg. words */
mm1 = _m_psubsw(mm1, mm5); /* add 1 to only neg. words, W-(-1) or W-0 */
mm2 = _m_psubsw(mm2, mm6); /* add 1 to only neg. words, W-(-1) or W-0 */
*mDest = _m_packuswb(mm1, mm2); /* pack words back into bytes with saturation */
mSrc1++;
mSrc2++;
mDest++;
}
_m_empty(); /* clean MMX state */
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using Mult: D = saturation255(S1 * S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterMult(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
{
unsigned int i, istart;
unsigned char *cursrc1, *cursrc2, *curdst;
int result;
/* Validate input parameters */
if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
return(-1);
if (length == 0)
return(0);
if ((SDL_imageFilterMMXdetect()) && (length > 7)) {
/* MMX routine */
SDL_imageFilterMultMMX(Src1, Src2, Dest, length);
/* Check for unaligned bytes */
if ((length & 7) > 0) {
/* Setup to process unaligned bytes */
istart = length & 0xfffffff8;
cursrc1 = &Src1[istart];
cursrc2 = &Src2[istart];
curdst = &Dest[istart];
} else {
/* No unaligned bytes - we are done */
return (0);
}
} else {
/* Setup to process whole image */
istart = 0;
cursrc1 = Src1;
cursrc2 = Src2;
curdst = Dest;
}
/* C routine to process image */
for (i = istart; i < length; i++) {
/* NOTE: this is probably wrong - dunno what the MMX code does */
result = (int) *cursrc1 * (int) *cursrc2;
if (result > 255)
result = 255;
*curdst = (unsigned char) result;
/* Advance pointers */
cursrc1++;
cursrc2++;
curdst++;
}
return (0);
}
/*!
\brief Internal ASM Filter using MultNor: D = S1 * S2
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterMultNorASM(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov edx, Src1 /* load Src1 address into edx */
mov esi, Src2 /* load Src2 address into esi */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
align 16 /* 16 byte alignment of the loop entry */
L10141:
mov al, [edx] /* load a byte from Src1 */
mul [esi] /* mul with a byte from Src2 */
mov [edi], al /* move a byte result to Dest */
inc edx /* increment Src1, Src2, Dest */
inc esi /* pointer registers by one */
inc edi
dec ecx /* decrease loop counter */
jnz L10141 /* check loop termination, proceed if required */
popa
}
#else
/* Note: ~5% gain on i386, less efficient than C on x86_64 */
/* Also depends on whether this function is static (?!) */
asm volatile (
".align 16 \n\t" /* 16 byte alignment of the loop entry */
# if defined(i386)
"1:mov (%%edx), %%al \n\t" /* load a byte from Src1 */
"mulb (%%esi) \n\t" /* mul with a byte from Src2 */
"mov %%al, (%%edi) \n\t" /* move a byte result to Dest */
"inc %%edx \n\t" /* increment Src1, Src2, Dest */
"inc %%esi \n\t" /* pointer registers by one */
"inc %%edi \n\t"
"dec %%ecx \n\t" /* decrease loop counter */
# elif defined(__x86_64__)
"1:mov (%%rdx), %%al \n\t" /* load a byte from Src1 */
"mulb (%%rsi) \n\t" /* mul with a byte from Src2 */
"mov %%al, (%%rdi) \n\t" /* move a byte result to Dest */
"inc %%rdx \n\t" /* increment Src1, Src2, Dest */
"inc %%rsi \n\t" /* pointer registers by one */
"inc %%rdi \n\t"
"dec %%rcx \n\t" /* decrease loop counter */
# endif
"jnz 1b \n\t" /* check loop termination, proceed if required */
: "+d" (Src1), /* load Src1 address into edx */
"+S" (Src2), /* load Src2 address into esi */
"+c" (SrcLength), /* load loop counter (SIZE) into ecx */
"+D" (Dest) /* load Dest address into edi */
:
: "memory", "rax"
);
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using MultNor: D = S1 * S2
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterMultNor(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)
{
unsigned int i, istart;
unsigned char *cursrc1, *cursrc2, *curdst;
/* Validate input parameters */
if ((Src1 == NULL) || (Src2 == NULL) || (Dest == NULL))
return(-1);
if (length == 0)
return(0);
if (SDL_imageFilterMMXdetect()) {
if (length > 0) {
/* ASM routine */
SDL_imageFilterMultNorASM(Src1, Src2, Dest, length);
/* Check for unaligned bytes */
if ((length & 7) > 0) {
/* Setup to process unaligned bytes */
istart = length & 0xfffffff8;
cursrc1 = &Src1[istart];
cursrc2 = &Src2[istart];
curdst = &Dest[istart];
} else {
/* No unaligned bytes - we are done */
return (0);
}
} else {
/* No bytes - we are done */
return (0);
}
} else {
/* Setup to process whole image */
istart = 0;
cursrc1 = Src1;
cursrc2 = Src2;
curdst = Dest;
}
/* C routine to process image */
for (i = istart; i < length; i++) {
*curdst = (int)*cursrc1 * (int)*cursrc2; // (int) for efficiency
/* Advance pointers */
cursrc1++;
cursrc2++;
curdst++;
}
return (0);
}
/*!
\brief Internal MMX Filter using MultDivby2: D = saturation255(S1/2 * S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param SrcLength The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
static int SDL_imageFilterMultDivby2MMX(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int SrcLength)
{
#ifdef USE_MMX
#if !defined(GCC__)
__asm
{
pusha
mov eax, Src1 /* load Src1 address into eax */
mov ebx, Src2 /* load Src2 address into ebx */
mov edi, Dest /* load Dest address into edi */
mov ecx, SrcLength /* load loop counter (SIZE) into ecx */
shr ecx, 3 /* counter/8 (MMX loads 8 bytes at a time) */
pxor mm0, mm0 /* zero mm0 register */
align 16 /* 16 byte alignment of the loop entry */
L1015:
movq mm1, [eax] /* load 8 bytes from Src1 into mm1 */
movq mm3, [ebx] /* load 8 bytes from Src2 into mm3 */
movq mm2, mm1 /* copy mm1 into mm2 */
movq mm4, mm3 /* copy mm3 into mm4 */
punpcklbw mm1, mm0 /* unpack low bytes of Src1 into words */
punpckhbw mm2, mm0 /* unpack high bytes of Src1 into words */
punpcklbw mm3, mm0 /* unpack low bytes of Src2 into words */
punpckhbw mm4, mm0 /* unpack high bytes of Src2 into words */
psrlw mm1, 1 /* divide mm1 words by 2, Src1 low bytes */
psrlw mm2, 1 /* divide mm2 words by 2, Src1 high bytes */
pmullw mm1, mm3 /* mul low bytes of Src1 and Src2 */
pmullw mm2, mm4 /* mul high bytes of Src1 and Src2 */
packuswb mm1, mm2 /* pack words back into bytes with saturation */
movq [edi], mm1 /* store result in Dest */
add eax, 8 /* increase Src1, Src2 and Dest */
add ebx, 8 /* register pointers by 8 */
add edi, 8
dec ecx /* decrease loop counter */
jnz L1015 /* check loop termination, proceed if required */
emms /* exit MMX state */
popa
}
#else
/* i386 and x86_64 */
__m64 *mSrc1 = (__m64*)Src1;
__m64 *mSrc2 = (__m64*)Src2;
__m64 *mDest = (__m64*)Dest;
__m64 mm0 = _m_from_int(0); /* zero mm0 register */
int i;
for (i = 0; i < SrcLength/8; i++) {
__m64 mm1, mm2, mm3, mm4, mm5, mm6;
mm1 = _m_punpcklbw(*mSrc1, mm0); /* unpack low bytes of Src1 into words */
mm2 = _m_punpckhbw(*mSrc1, mm0); /* unpack high bytes of Src1 into words */
mm3 = _m_punpcklbw(*mSrc2, mm0); /* unpack low bytes of Src2 into words */
mm4 = _m_punpckhbw(*mSrc2, mm0); /* unpack high bytes of Src2 into words */
mm1 = _m_psrlwi(mm1, 1); /* divide mm1 words by 2, Src1 low bytes */
mm2 = _m_psrlwi(mm2, 1); /* divide mm2 words by 2, Src1 high bytes */
mm1 = _m_pmullw(mm1, mm3); /* mul low bytes of Src1 and Src2 */
mm2 = _m_pmullw(mm2, mm4); /* mul high bytes of Src1 and Src2 */
*mDest = _m_packuswb(mm1, mm2); /* pack words back into bytes with saturation */
mSrc1++;
mSrc2++;
mDest++;
}
_m_empty(); /* clean MMX state */
#endif
return (0);
#else
return (-1);
#endif
}
/*!
\brief Filter using MultDivby2: D = saturation255(S1/2 * S2)
\param Src1 Pointer to the start of the first source byte array (S1).
\param Src2 Pointer to the start of the second source byte array (S2).
\param Dest Pointer to the start of the destination byte array (D).
\param length The number of bytes in the source arrays.
\return Returns 0 for success or -1 for error.
*/
int SDL_imageFilterMultDivby2(unsigned char *Src1, unsigned char *Src2, unsigned char *Dest, unsigned int length)