File: simd.h

package info (click to toggle)
zapping 0.10~cvs6-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,828 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,816; xml: 2,742; makefile: 1,282; perl: 488
file content (1299 lines) | stat: -rw-r--r-- 37,317 bytes parent folder | download | duplicates (6)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
/*
 *  Zapping TV viewer
 *
 *  Copyright (C) 2005 Michael H. Schimek
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: simd.h,v 1.8 2006/04/12 01:45:45 mschimek Exp $ */

#ifndef SIMD_H
#define SIMD_H

#include <assert.h>
#include <inttypes.h>
#include "cpu.h"

/*
   This is a simple vector intrinsics abstraction.  The idea is to use
   the Intel/AMD/AltiVec intrinsics where you must and these macros
   where you can.  Intrinsics are halfway portable and easier to
   maintain than asm code or inline asm.  These macros can save typing
   if the SIMD routine is basically the same for MMX, SSE2 and
   AltiVec.

   To select an implementation #define SIMD x before including this
   header, where x is one of:

   CPU_FEATURE_MMX      x86 and x86_64 MMX extension
   CPU_FEATURE_3DNOW    x86 and x86_64 3DNow! extension
   CPU_FEATURE_SSE_INT  x86 and x86_64 SSE extension
   CPU_FEATURE_SSE_FLT  x86 and x86_64 SSE extension
   CPU_FEATURE_SSE2     x86 and x86_64 SSE2 extension
   CPU_FEATURE_SSE3     x86 and x86_64 SSE3 extension
   CPU_FEATURE_ALTIVEC  powerpc AltiVec extension

   Keep a few subtle differences in mind:
   - SSE2 and AltiVec vectors are 128 bit wide, the rest 64 bit.
     Be careful when doing pointer math.  sizeof() is your friend.
   - MMX loads and stores must be 8 byte aligned, SSE2 and AltiVec
     loads and stores must be 16 byte aligned.
     MMX will not segfault on unaligned accesses but they're slow when
     crossing a cache line boundary.  SSE has instructions for unaligned
     accesses, they're slow too.  AltiVec can shift across two registers
     in 1-2 instructions.
   - AltiVec is big endian, MMX/SSE little endian:
     BE:   v bit 0       v bit n,  MSB first in memory.
         0x01020304050607
     LE:   ^ bit n      ^ bit 0,  LSB first in memory.

   Compiling:
   The configure script defines CAN_COMPILE_MMX, CAN_COMPILE_SSE, ... (both
   #defines and automake conditionals) if the compiler recognizes these
   intrinsics.

   To compile intrinsics GCC needs a target CPU switch, e. g. -mmmx,
   -msse or -march=pentium4.  The resulting code will be unportable.
   Since we want to build different versions of a SIMD routine and choose
   an implementation at runtime, each version must go into another object
   file compiled with the appropriate flags.
 */

/* For gcc bug checks. */
#ifndef GCC_VERSION
#  define GCC_VERSION (__GNUC__ * 10000					\
		       + __GNUC_MINOR__ * 100				\
		       + __GNUC_PATCHLEVEL__)
#endif

#ifndef SIMD
#  define SIMD 0
#endif

#define SCALAR (1 << 30)

/* XXX GCC 4.1 doesn't support nested always_inline functions with -O0? */
#define always_inline __inline__
#define never_inline __attribute__ ((noinline))

/* ------------------------------------------------------------------------- */

#if SIMD

/* Common macros. */

#define vzero16() ((v16) vzero8 ())
#define vzero32() ((v32) vzero8 ())

#define vzerou8() ((vu8) vzero8 ())
#define vzerou16() ((vu16) vzero8 ())
#define vzerou32() ((vu32) vzero8 ())

#define vminus116() ((v16) vminus18 ())
#define vminus132() ((v32) vminus18 ())

/* Common subexpression elimination doesn't seem to work in gcc 3.x
   with _mm_set1(), so use these constants instead. */
#define SIMD_CONST_PROTOS						\
extern const v8 vsplat8_1;	/* vsplat8(1) */			\
extern const v8 vsplat8_m1;	/* vsplat8(-1) */			\
extern const v8 vsplat8_15;	/* vsplat8(15) */			\
extern const v8 vsplat8_127;	/* vsplat8(127 = 0x7F) */		\
extern const vu8 vsplatu8_F8;	/* vsplatu8(0xF8) */			\
extern const vu8 vsplatu8_FC;	/* vsplatu8(0xFC) */			\
extern const v16 vsplat16_1;	/* vsplat16(1) */			\
extern const v16 vsplat16_2;	/* vsplat16(2) */			\
extern const v16 vsplat16_128;	/* vsplat16(128) */			\
extern const v16 vsplat16_255;	/* vsplat16(255 = 0x00FF) */		\
extern const v16 vsplat16_256;	/* vsplat16(256 = 0x0100) */		\
extern const v16 vsplat16_m256;	/* vsplat16(-256 = 0xFF00) */		\
extern const vu16 vsplatu16_F8;	/* vsplatu16(0x00F8) */			\
extern const v32 vsplat32_1;	/* vsplat32(1) */			\
extern const v32 vsplat32_2;	/* vsplat32(2) */

#define vsplatu8_1 ((vu8) vsplat8_1)
#define vsplat16_m1 ((v16) vsplat8_m1)
#define vsplat32_m1 ((v32) vsplat8_m1)
#define vsplatu8_m1 ((vu8) vsplat8_m1)
#define vsplatu16_m1 ((vu16) vsplat8_m1)
#define vsplatu32_m1 ((vu32) vsplat8_m1)
#define vsplatu8_15 ((vu8) vsplat8_15)
#define vsplatu8_127 ((vu8) vsplat8_127)
#define vsplatu16_1 ((vu16) vsplat16_1)
#define vsplatu16_2 ((vu16) vsplat16_2)
#define vsplatu16_128 ((vu16) vsplat16_128)
#define vsplatu16_255 ((vu16) vsplat16_255)
#define vsplatu16_256 ((vu16) vsplat16_256)
#define vsplatu16_m256 ((vu16) vsplat16_m256)
#define vsplatu32_1 ((vu32) vsplat32_1)
#define vsplatu32_2 ((vu32) vsplat32_2)

/* Neither MMX nor AltiVec have cmplt instructions. */

/* Compare signed (_a < _b) ? 0xFF : 0x00. */
#define vcmplt8(_a, _b) vcmpgt8 (_b, _a)
#define vcmplt16(_a, _b) vcmpgt16 (_b, _a)
#define vcmplt32(_a, _b) vcmpgt32 (_b, _a)

/* Compare signed (_a <= _b) ? 0xFF : 0x00. */
#define vcmple8(_a, _b) vcmpge8 (_b, _a)
#define vcmple16(_a, _b) vcmpge16 (_b, _a)
#define vcmple32(_a, _b) vcmpge32 (_b, _a)

/* Compare unsigned (_a < _b) ? 0xFF : 0x00. */
#define vcmpltu8(_a, _b) vcmpgtu8 (_b, _a)
#define vcmpltu16(_a, _b) vcmpgtu16 (_b, _a)
#define vcmpltu32(_a, _b) vcmpgtu32 (_b, _a)

/* Compare unsigned (_a <= _b) ? 0xFF : 0x00. */
#define vcmpleu8(_a, _b) vcmpgeu8 (_b, _a)
#define vcmpleu16(_a, _b) vcmpgeu16 (_b, _a)
#define vcmpleu32(_a, _b) vcmpgeu32 (_b, _a)

#define vdump(_a)							\
{									\
	union { vu8 v; uint8_t s[sizeof (vu8)]; } _u;			\
	unsigned int _i;						\
									\
	_u.v = (vu8)(_a);						\
	fprintf (stderr, "%s:%u: %s = ", __FILE__, __LINE__, #_a);	\
	for (_i = 0; _i < sizeof (_u.s); ++_i)				\
		fprintf (stderr, "%02x", _u.s[_i]);			\
	fputc ('\n', stderr);						\
}

#else /* !SIMD */

#define SUFFIX _SCALAR

#define vempty() do {} while (0)
#define sfence() do {} while (0)

#endif /* !SIMD */

/* ------------------------------------------------------------------------- */

#if SIMD & (CPU_FEATURE_MMX | CPU_FEATURE_3DNOW |			\
	    CPU_FEATURE_SSE_INT | CPU_FEATURE_SSE_FLT)

#include <mmintrin.h>

/* AltiVec intrinsics automatically distinguish between element
   size and signedness by the vector type.  Intel intrinsics use macro
   suffixes.  For portability we define it both ways. */

typedef __m64 v8;		/* vector of 8 or 16 int8_t */
typedef __m64 vu8;		/* vector of 8 or 16 uint8_t */
typedef __m64 v16;		/* vector of 4 or 8 int16_t */
typedef __m64 vu16;		/* vector of 4 or 8 uint16_t */
typedef __m64 v32;		/* vector of 2 or 4 int32_t */
typedef __m64 vu32;		/* vector of 2 or 4 uint32_t */

/* Constant 0 (pxor or memory operand). */
#define vzero8() _mm_setzero_si64 ()

/* Constant -1 (should be pcmpeq or memory operand). */
#define vminus18() vsplat8_m1

SIMD_CONST_PROTOS

/* gcc 3.2 bug: _mm_set1_pi16 (_i) produces { _i, _i, 0, 0 } */
/* gcc 4.0 / 4.1 (experimental) bug: uses SSE instruction pshufw
   without -msse. */
/* gcc 4.1 (experimental) bug: simd.c:290: error: unrecognizable insn:
  (insn 743 265 268 17 (set (reg:V2SI 0 ax) (const_vector:V2SI [
   (const_int -1 [0xffffffffffffffff]) (const_int -1 [0xffffffffffffffff])
    ])) -1 (nil) (nil))
  simd.c:290: internal compiler error: in extract_insn, at recog.c:2082
  (That's mmx vsplat8 (-1). Should be pcmpeq I guess, other values work.) */
#if (SIMD & (CPU_FEATURE_MMX | CPU_FEATURE_3DNOW))			\
    && ((GCC_VERSION >= 30200 && GCC_VERSION < 30300)			\
	|| (GCC_VERSION >= 40000 && GCC_VERSION < 40200))

static always_inline __m64
vsplatu32			(uint32_t		_i)
{
	return (__m64)(_i * 0x0000000100000001ULL);
}

static always_inline __m64
vsplat32			(int32_t		_i)
{
	return vsplatu32 ((uint32_t) _i);
}

static always_inline __m64
vsplatu16			(uint16_t		_i)
{
	return (__m64)(_i * 0x0001000100010001ULL);
}

static always_inline __m64
vsplat16			(int16_t		_i)
{
	return vsplatu16 ((uint16_t) _i);
}

static always_inline __m64
vsplatu8			(uint8_t		_i)
{
	return (__m64)(_i * 0x0101010101010101ULL);
}

static always_inline __m64
vsplat8				(int8_t			_i)
{
	return vsplatu8 ((uint8_t) _i);
}

#else
   /* Set each element to _i. */
#  define vsplat8(_i) _mm_set1_pi8 (_i)
#  define vsplat16(_i) _mm_set1_pi16 (_i)
#  define vsplat32(_i) _mm_set1_pi32 (_i)
#  define vsplatu8(_i) _mm_set1_pi8 (_i)
#  define vsplatu16(_i) _mm_set1_pi16 (_i)
#  define vsplatu32(_i) _mm_set1_pi32 (_i)
#endif

#define vsplatu8i(_i) vsplatu8 (_i)
#define vsplatu16i(_i) vsplatu16 (_i)
#define vsplatu32i(_i) vsplatu32 (_i)

/* Load and store from address + offset (in bytes). */
#define vload(_p, _o) (* (const __m64 *)((const uint8_t *)(_p) + (_o)))
#define vstore(_p, _o, _a) (* (__m64 *)((uint8_t *)(_p) + (_o)) = (_a))

/* Slower unaligned load and store. */
#define vloadu(_p, _o) vload (_p, _o)
#define vstoreu(_p, _o, _a) vstore (_p, _o, _a)

/* Non-temporal load and store (SSE, SSE2, AltiVec), to be
   used if we do not access the same data / cache line soon. */
#define vloadnt(_p, _o) vload (_p, _o)
#define vstorent(_p, _o, _a) vstore (_p, _o, _a)

#define vand(_a, _b) _mm_and_si64 (_a, _b)
/* NOTE: a & ~b as the name suggests. */
#define vandnot(_a, _b) _mm_andnot_si64 (_b, _a)
#define vor(_a, _b) _mm_or_si64 (_a, _b)
#define vxor(_a, _b) _mm_xor_si64 (_a, _b)
/* AltiVec has a vnor (and by extension vnot) instruction.
   Note this is a bitwise not. Boolean not is cheaper with vcmpz
   if we already have 0 in a register. */
#define vnot(_a) vxor (_a, vminus18 ())
#define vnand(_a, _b) vnot (vand (_a, _b))
#define vnor(_a, _b) vnot (vor (_a, _b))

/* For each bit: (1 == _mask) ? _a : _b.  One AltiVec instruction
   but expensive with MMX/SSE/SSE2.  Note AltiVec's vec_sel() has
   the parameters reversed. */
static always_inline __m64
vsel				(__m64			_mask,
				 __m64			_a,
				 __m64			_b)
{
	return vor (vand (_a, _mask), vandnot (_b, _mask));
}

#define vsl(_a, _i) _mm_slli_si64 (_a, _i)
#define vsru(_a, _i) _mm_srli_si64 (_a, _i)
/* Unsigned _a <<= 1 (MMX/3DNow/SSEx have no byte shift ops). */
#define vsl18(_a) _mm_slli_pi16 (vand (_a, vsplat8_127), 1)
/* _a >>= 1. Somewhat expensive, better avoid it. */
#define vsr18(_a) vsel (vsplat8_127, _mm_srli_pi16 (_a, 1), _a)
#define vsr1u8(_a) vand (_mm_srli_pi16 (_a, 1), vsplat8_127)
/* Shift left by immediate. */
#define vsl16(_a, _i) _mm_slli_pi16 (_a, _i)
#define vsl32(_a, _i) _mm_slli_pi32 (_a, _i)
/* Shift right by immediate. */
#define vsr16(_a, _i) _mm_srai_pi16 (_a, _i)
#define vsru16(_a, _i) _mm_srli_pi16 (_a, _i)
#define vsr32(_a, _i) _mm_srai_pi32 (_a, _i)
#define vsru32(_a, _i) _mm_srli_pi32 (_a, _i)

static always_inline __m64
vbswap16			(__m64			_a)
{
	return vor (vsl16 (_a, 8), vsru16 (_a, 8));
}

static always_inline __m64
vwswap32			(__m64			_a)
{
	return vor (vsl32 (_a, 16), vsru32 (_a, 16));
}

/* Long shift right, e.g. (0x0123, 0x4567, 3) -> 0x1234 */
static always_inline __m64
vlsr				(__m64			_h,
				 __m64			_l,
				 const unsigned int	_i)
{
	assert (_i <= 64);

	if (0 == _i) {
		return _l;
	} else if (32 == _i) {
		return _mm_unpackhi_pi32 (_l, vsl (_h, 32));
	} else if (64 == _i) {
		return _h;
	} else {
		return vor (vsru (_l, _i), vsl (_h, 64 - _i));
	}
}

/* Given am = * (const vu8 *) &src[-sizeof (vu8)],
         a0 = * (const vu8 *) src,
         a1 = * (const vu8 *) &src[+sizeof (vu8)],
   where src is uint8_t* and dist is given in bytes
   this emulates an unaligned load from src - dist and src + dist */
static always_inline void
vshiftu2x			(__m64 *		_l,
				 __m64 *		_r,
				 __m64			_am,
				 __m64			_a0,
				 __m64			_a1,
				 const unsigned int	_dist)
{
	assert (_dist <= sizeof (vu8));

#if 0
	if (4 == _dist) {
		_a0 = vwswap32 (_a0);
		*_l = _mm_unpackhi_pi32 (_am, _a0);
		*_r = _mm_unpacklo_pi32 (_a0, _a1);
	} else
#endif
	{
		/* 7654 3210 -> 6543 */
		*_l = vlsr (_a0, _am, (sizeof (vu8) - _dist) * 8);
		/* BA98 7654 -> 8765 */
		*_r = vlsr (_a1, _a0, _dist * 8);
	}
}

#define vunpacklo8(_a, _b) _mm_unpacklo_pi8 (_a, _b)
#define vunpackhi8(_a, _b) _mm_unpackhi_pi8 (_a, _b)
#define vunpacklo16(_a, _b) _mm_unpacklo_pi16 (_a, _b)
#define vunpackhi16(_a, _b) _mm_unpackhi_pi16 (_a, _b)
#define vunpacklo32(_a, _b) _mm_unpacklo_pi32 (_a, _b)
#define vunpackhi32(_a, _b) _mm_unpackhi_pi32 (_a, _b)
#define vunpacklo(_a, _b) _mm_unpacklo_pi32 (_a, _b)
#define vunpackhi(_a, _b) _mm_unpackhi_pi32 (_a, _b)

#define vpacksu16(_a, _b) _mm_packs_pu16 (_a, _b)

/* _a + _b, _a - _b with wrap-around. */
#define vadd8(_a, _b) _mm_add_pi8 (_a, _b)
#define vadd16(_a, _b) _mm_add_pi16 (_a, _b)
#define vadd32(_a, _b) _mm_add_pi32 (_a, _b)
#define vsub8(_a, _b) _mm_sub_pi8 (_a, _b)
#define vsub16(_a, _b) _mm_sub_pi16 (_a, _b)
#define vsub32(_a, _b) _mm_sub_pi32 (_a, _b)

/* Add or subtract with signed saturation. */
#define vadds16(_a, _b) _mm_adds_pi16 (_a, _b)
#define vsubs16(_a, _b) _mm_subs_pi16 (_a, _b)

/* Add or subtract with unsigned saturation. */
#define vaddsu8(_a, _b) _mm_adds_pu8 (_a, _b)
#define vaddsu16(_a, _b) _mm_adds_pu16 (_a, _b)
#define vsubsu8(_a, _b) _mm_subs_pu8 (_a, _b)
#define vsubsu16(_a, _b) _mm_subs_pu16 (_a, _b)

/* Saturate against variable bounds. Expensive! */
#define vsatu8(_a, _min, _max) vminu8 (vmaxu8 (_a, _min), _max)

/* Compare (_a == _b) ? 0xFF : 0x00. */
#define vcmpeq8(_a, _b) _mm_cmpeq_pi8 (_a, _b)
#define vcmpeq16(_a, _b) _mm_cmpeq_pi16 (_a, _b)
#define vcmpeq32(_a, _b) _mm_cmpeq_pi32 (_a, _b)

/* Compare (_a == 0) ? 0xFF : 0x00. */
#define vcmpz8(_a) vcmpeq8 (_a, vzero8 ())
#define vcmpz16(_a) vcmpeq16 (_a, vzero16 ())
#define vcmpz32(_a) vcmpeq32 (_a, vzero32 ())

/* Compare (_a != 0) ? 0xFF : 0x00. */
#define vcmpnz8(_a) vcmpz8 (vcmpz8 (_a))
#define vcmpnz16(_a) vcmpz16 (vcmpz16 (_a))
#define vcmpnz32(_a) vcmpz32 (vcmpz32 (_a))

/* Compare signed (_a > _b) ? 0xFF : 0x00. */
#define vcmpgt8(_a, _b) _mm_cmpgt_pi8 (_a, _b)
#define vcmpgt16(_a, _b) _mm_cmpgt_pi16 (_a, _b)
#define vcmpgt32(_a, _b) _mm_cmpgt_pi32 (_a, _b)

/* Compare unsigned (_a >= _b) ? 0xFF : 0x00. */
#define vcmpgeu8(_a, _b) vcmpz8 (vsubsu8 (_b, _a))

/* Any ideas for cmpge and cmpgtu? :-) */

/* Multiply v16 giving low/high 16 bit of result (vu16). */
#define vmullo16(_a, _b) _mm_mullo_pi16 (_a, _b)
#define vmulhi16(_a, _b) _mm_mulhi_pi16 (_a, _b)

/* Clear MMX state (emms). */
#define vempty() _mm_empty ()

/* Store fence guarantees that every preceding store is globally visible
   before any subsequent store. */
#define sfence() do {} while (0)

/* abs (_a - _b). */
static always_inline vu8
vabsdiffu8			(vu8			_a,
				 vu8			_b)
{
	return vor (vsubsu8 (_a, _b), vsubsu8 (_b, _a));
}

/* (_a + _b + 1) / 2 (single instruction on all but MMX). */
static always_inline vu8
vavgu8				(vu8			_a,
				 vu8			_b)
{
	vu8 carry;

	/* ((_a & 1) + (_b & 1) + 1) >> 1 */ 
	carry = vand (vor (_a, _b), vsplat8_1);

	/* "or 1" instead of "and 127" because we already have the const
	   in a register.  The MSBs cancel out when added. */
	_a = vsru16 (vor (_a, vsplat8_1), 1);
	_b = vsru16 (vor (_b, vsplat8_1), 1);

	return vadd8 (vadd8 (_a, _b), carry);
}

static always_inline vu8
fast_vavgu8			(vu8			_a,
				 vu8			_b)
{
	/* Faster but inaccurate. */
	return vadd8 (vsr1u8 (_a), vsr1u8 (_b));
}

static always_inline vu16
small_vavgu16			(vu16			_a,
				 vu16			_b)
{
	/* For values < 16384. */
	return vsru16 (vadd16 (vadd16 (_a, vsplat16_1), _b), 1);
}

/* min (_a, _b) (single instruction on all but MMX, 3DNow). */
static always_inline vu8
vminu8				(vu8			_a,
				 vu8			_b)
{
	vu8 t;
				/* a > b   a <= b */ 
	t = vsubsu8 (_a, _b);	/* a - b   0 */
	_a = vxor (_a, _b);	/* a ^ b   a ^ b */
	t = vaddsu8 (t, _b);	/* a       b */
	return vxor (t, _a);	/* b       a */
}

/* max (_a, _b) (single instruction on all but MMX, 3DNow). */
static always_inline vu8
vmaxu8				(vu8			_a,
				 vu8			_b)
{
				 /* a > b   a <= b */ 
	_a = vsubsu8 (_a, _b);	 /* a - b   0 */
	return vaddsu8 (_a, _b); /* a       b */
}

/* min (_a, _b), max (_a, _b).
   With MMX this is faster than vmin(), vmax(). */
static always_inline void
vminmaxu8			(vu8 *			_min,
				 vu8 *			_max,
				 vu8			_a,
				 vu8			_b)
{
	vu8 t;
				/* a > b   a <= b */ 
	t = vsubsu8 (_a, _b);	/* a - b   0 */
	_a = vxor (_a, _b);	/* a ^ b   a ^ b */
	t = vaddsu8 (t, _b);	/* a       b */
	*_max = t;
	*_min = vxor (_a, t);	/* b       a */
}

static always_inline vu16
vminu16i			(vu16			_a,
				 const unsigned int	_i)
{
	vu16 t;

	assert (_i <= 65535);

	t = vsplatu16 (0xFFFF - _i);    /* a > i   a <= i */
	_a = vaddsu16 (_a, t);	 	/* 0xFFFF  a + 0xFFFF - i */
	return vsubsu16 (_a, t);  	/* i       a */
}

/* ------------------------------------------------------------------------- */

#if SIMD == CPU_FEATURE_MMX

#define SUFFIX _MMX

/* ------------------------------------------------------------------------- */

#elif SIMD == CPU_FEATURE_3DNOW

#define SUFFIX _3DNOW

#include <mm3dnow.h>

#define vavgu8(_a, _b) _m_pavgusb (_a, _b)
#define fast_vavgu8(_a, _b) vavgu8 (_a, _b)

/* Fast emms. */
#undef vempty
#define vempty() _m_femms ()

/* ------------------------------------------------------------------------- */

#elif (SIMD == CPU_FEATURE_SSE_INT || SIMD == CPU_FEATURE_SSE_FLT)

#define SUFFIX _SSE

#include <xmmintrin.h>

typedef __m128 vf;

#define vwswap32(_a) _mm_shuffle_pi16 (_a, _MM_SHUFFLE (1, 0, 3, 2))
#define vavgu8(_a, _b) _mm_avg_pu8 (_a, _b)
#define fast_vavgu8(_a, _b) vavgu8 (_a, _b)
#define vavgu16(_a, _b) _mm_avg_pu16 (_a, _b)
#define small_vavgu16(_a, _b) vavgu16 (_a, _b)
#define vminu8(_a, _b) _mm_min_pu8 (_a, _b)
#define vmaxu8(_a, _b) _mm_max_pu8 (_a, _b)
#define vmin16(_a, _b) _mm_min_pi16 (_a, _b)
#define vmax16(_a, _b) _mm_max_pi16 (_a, _b)

#undef sfence
#define sfence() _mm_sfence ()

/* movntq - don't load cache line and don't store _a in cache.
   Might be useful to aggregate stores (write combining, burst writes). */
#undef vstorent
#define vstorent(_p, _o, _a)						\
	_mm_stream_pi ((__m64 *)((uint8_t *)(_p) + (_o)), _a)

#define vloadf(_p, _o)							\
	_mm_load_ps ((const float *)((const uint8_t *)(_p) + (_o)))
#define vstoref(_p, _o, _a)						\
	_mm_store_ps ((float *)((uint8_t *)(_p) + (_o)), _a)

#define vloadfnt(_p, _o) vloadf (_p, _o)
#define vstorefnt(_p, _o, _a)						\
	_mm_stream_ps ((float *)((uint8_t *)(_p) + (_o)), _a)

/* Override MMX inline function vminmaxu8. */
#define vminmaxu8(_minp, _maxp, _a, _b) sse_vminmaxu8 (_minp, _maxp, _a, _b)

static always_inline void
sse_vminmaxu8			(vu8 *			_min,
				 vu8 *			_max,
				 vu8			_a,
				 vu8			_b)
{
	*_min = _mm_min_pu8 (_a, _b);
	*_max = _mm_max_pu8 (_a, _b);
}

#endif /* SIMD == CPU_FEATURE_SSE_INT or _FLT */

/* ========================================================================= */

#elif SIMD & (CPU_FEATURE_SSE2 | CPU_FEATURE_SSE3)

/* Basically the same as MMX/SSE except vectors are 128 bit. */

#include <emmintrin.h>

typedef __m128i v8;
typedef __m128i vu8;
typedef __m128i v16;
typedef __m128i vu16;
typedef __m128i v32;
typedef __m128i vu32;

#define vzero8() _mm_setzero_si128 ()

#define vminus18() vsplat8_m1

SIMD_CONST_PROTOS

#define vsplat8(_i) _mm_set1_epi8 (_i)
#define vsplat16(_i) _mm_set1_epi16 (_i)
#define vsplat32(_i) _mm_set1_epi32 (_i)
#define vsplatu8(_i) _mm_set1_epi8 (_i)
#define vsplatu16(_i) _mm_set1_epi16 (_i)
#define vsplatu32(_i) _mm_set1_epi32 (_i)

#define vsplatu8i(_i) vsplatu8 (_i)
#define vsplatu16i(_i) vsplatu16 (_i)
#define vsplatu32i(_i) vsplatu32 (_i)

/* movd to xmm */
#define vload32(_p, _o)							\
	_mm_cvtsi32_si128 (* (const uint32_t *)				\
			   ((const uint8_t *)(_p) + (_o)))

/* movq to xmm */
#define vload64(_p, _o)							\
	_mm_set_epi64 ((__m64) 0LL,					\
		       * (const __m64 *)((const uint8_t *)(_p) + (_o)))

/* Aligned load and store (movdqa). */
#define vload(_p, _o)							\
	_mm_load_si128 ((const __m128i *)((const uint8_t *)(_p) + (_o)))
#define vstore(_p, _o, _a)						\
	_mm_store_si128 ((__m128i *)((uint8_t *)(_p) + (_o)), _a)

/* Unaligned load and store (movdqu). */
#define vloadu(_p, _o)							\
	_mm_loadu_si128 ((const __m128i *)((const uint8_t *)(_p) + (_o)))
#define vstoreu(_p, _o, _a)						\
	_mm_storeu_si128 ((__m128i *)((uint8_t *)(_p) + (_o)), _a)

#define vloadnt(_p, _o) vload (_p, _o)

/* movntdq - don't load cache line and don't store _a in cache. */
#define vstorent(_p, _o, _a)						\
	_mm_stream_si128 ((__m128i *)((uint8_t *)(_p) + (_o)), _a)

#define vand(_a, _b) _mm_and_si128 (_a, _b)
#define vandnot(_a, _b) _mm_andnot_si128 (_b, _a)
#define vor(_a, _b) _mm_or_si128 (_a, _b)
#define vxor(_a, _b) _mm_xor_si128 (_a, _b)
#define vnot(_a) vxor (vminus18 (), _a)
#define vnand(_a, _b) vnot (vand (_a, _b))
#define vnor(_a, _b) vnot (vor (_a, _b))

/* For each bit: (1 == _mask) ? _a : _b.  One AltiVec instruction
   but expensive with MMX/SSE/SSE2.  Note AltiVec's vec_sel() has
   the parameters reversed. */
static always_inline __m128i
vsel				(__m128i		_mask,
				 __m128i		_a,
				 __m128i		_b)
{
	return vor (vand (_a, _mask), vandnot (_b, _mask));
}

#define vsl18(_a) _mm_slli_epi16 (vand (_a, vsplat8_127), 1)
#define vsr18(_a) vsel (vsplat8_127, _mm_srli_epi16 (_a, 1), _a)
#define vsr1u8(_a) vand (_mm_srli_epi16 (_a, 1), vsplat8_127)
#define vsl16(_a, _i) _mm_slli_epi16 (_a, _i)
#define vsl32(_a, _i) _mm_slli_epi32 (_a, _i)
#define vsr16(_a, _i) _mm_srai_epi16 (_a, _i)
#define vsru16(_a, _i) _mm_srli_epi16 (_a, _i)
#define vsr32(_a, _i) _mm_srai_epi32 (_a, _i)
#define vsru32(_a, _i) _mm_srli_epi32 (_a, _i)

static always_inline __m128i
vbswap16			(__m128i		_a)
{
	return vor (vsl16 (_a, 8), vsru16 (_a, 8));
}

#define vwswap32(_a) _mm_shuffle_epi16 (_a, _MM_SHUFFLE (1, 0, 3, 2))

/* _mm_sxli_si128 is misdefined under gcc -O0.  Arg 2 must be an immediate,
   not a variable which evaluates to one, not even a const variable. */

#if 0

static always_inline __m128i
vsl				(__m128i		_a,
				 const unsigned int	_i)
{
	assert (0 == (_i % 8));

	return _mm_slli_si128 (_a, _i / 8); /* sic */
}

static always_inline __m128i
vsru				(__m128i		_a,
				 const unsigned int	_i)
{
	assert (0 == (_i % 8));

	return _mm_srli_si128 (_a, _i / 8); /* sic */
}

/* Long shift right, e.g. (0x0123, 0x4567, 3) -> 0x1234 */
static always_inline __m128i
vlsr				(__m128i		_h,
				 __m128i		_l,
				 const unsigned int	_i)
{
	assert (_i <= 128);

	if (0 == _i) {
		return _l;
	} else if (64 == _i) {
		return _mm_unpackhi_epi64 (_l, vsl (_h, 64));
	} else if (128 == _i) {
		return _h;
	} else {
		return vor (vsru (_l, _i), vsl (_h, 128 - _i));
	}
}

static always_inline void
vshiftu2x			(__m128i *		_l,
				 __m128i *		_r,
				 __m128i		_am,
				 __m128i		_a0,
				 __m128i		_a1,
				 const unsigned int	_dist)
{
	assert (_dist <= sizeof (vu8));

	if (8 == _dist) {
		_a0 = _mm_shuffle_epi32 (_a0, _MM_SHUFFLE (1, 0, 3, 2));
		*_l = _mm_unpackhi_epi64 (_am, _a0);
		*_r = _mm_unpacklo_epi64 (_a0, _a1);
	} else {
		/* 7654 3210 -> 6543 */
		*_l = vlsr (_a0, _am, (sizeof (vu8) - _dist) * 8);
		/* BA98 7654 -> 8765 */
		*_r = vlsr (_a1, _a0, _dist * 8);
	}
}

#else

#  define vsl(_a, _i)							\
	(assert (0 == ((_i) % 8)), _mm_slli_si128 ((_a), (_i) / 8))
#  define vsru(_a, _i)							\
	(assert (0 == ((_i) % 8)), _mm_srli_si128 ((_a), (_i) / 8))

#define vlsr(_h, _l, _i)						\
({									\
	__m128i h = _h;							\
	__m128i l = _l;							\
									\
	assert (_i <= 128);						\
	(0 == (_i)) ? l :						\
	 (64 == (_i)) ? _mm_unpackhi_epi64 (l, vsl (h, 64)) :		\
	 (128 == (_i)) ? h :						\
	  vor (vsru (l, _i), vsl (h, 128 - (_i)));			\
})

#define vshiftu2x(_l, _r, _am, _a0, _a1, _dist)				\
({									\
    *_l = vlsr (_a0, _am, (sizeof (vu8) - _dist) * 8);			\
    *_r = vlsr (_a1, _a0, _dist * 8);					\
})

#endif

#define vunpacklo8(_a, _b) _mm_unpacklo_epi8 (_a, _b)
#define vunpackhi8(_a, _b) _mm_unpackhi_epi8 (_a, _b)
#define vunpacklo16(_a, _b) _mm_unpacklo_epi16 (_a, _b)
#define vunpackhi16(_a, _b) _mm_unpackhi_epi16 (_a, _b)
#define vunpacklo32(_a, _b) _mm_unpacklo_epi32 (_a, _b)
#define vunpackhi32(_a, _b) _mm_unpackhi_epi32 (_a, _b)
#define vunpacklo64(_a, _b) _mm_unpacklo_epi64 (_a, _b)
#define vunpackhi64(_a, _b) _mm_unpackhi_epi64 (_a, _b)
#define vunpacklo(_a, _b) _mm_unpacklo_epi64 (_a, _b)
#define vunpackhi(_a, _b) _mm_unpackhi_epi64 (_a, _b)

#define vpacksu16(_a, _b) _mm_packs_epu16 (_a, _b)

#define vadd8(_a, _b) _mm_add_epi8 (_a, _b)
#define vadd16(_a, _b) _mm_add_epi16 (_a, _b)
#define vadd32(_a, _b) _mm_add_epi32 (_a, _b)
#define vsub8(_a, _b) _mm_sub_epi8 (_a, _b)
#define vsub16(_a, _b) _mm_sub_epi16 (_a, _b)
#define vsub32(_a, _b) _mm_sub_epi32 (_a, _b)
#define vadds16(_a, _b) _mm_adds_epi16 (_a, _b)
#define vsubs16(_a, _b) _mm_subs_epi16 (_a, _b)
#define vaddsu8(_a, _b) _mm_adds_epu8 (_a, _b)
#define vaddsu16(_a, _b) _mm_adds_epu16 (_a, _b)
#define vsubsu8(_a, _b) _mm_subs_epu8 (_a, _b)
#define vsubsu16(_a, _b) _mm_subs_epu16 (_a, _b)

#define vsatu8(_a, _min, _max) vminu8 (vmaxu8 (_a, _min), _max)

#define vcmpeq8(_a, _b) _mm_cmpeq_epi8 (_a, _b)
#define vcmpeq16(_a, _b) _mm_cmpeq_epi16 (_a, _b)
#define vcmpeq32(_a, _b) _mm_cmpeq_epi32 (_a, _b)

#define vcmpz8(_a) vcmpeq8 (_a, vzero8 ())
#define vcmpz16(_a) vcmpeq16 (_a, vzero8 ())
#define vcmpz32(_a) vcmpeq32 (_a, vzero8 ())

#define vcmpnz8(_a) vcmpz8 (vcmpz8 (_a))
#define vcmpnz16(_a) vcmpz16 (vcmpz16 (_a))
#define vcmpnz32(_a) vcmpz32 (vcmpz32 (_a))

#define vcmpgt8(_a, _b) _mm_cmpgt_epi8 (_a, _b)
#define vcmpgt16(_a, _b) _mm_cmpgt_epi16 (_a, _b)
#define vcmpgt32(_a, _b) _mm_cmpgt_epi32 (_a, _b)

/* Compare unsigned (_a >= _b) ? 0xFF : 0x00. */
#define vcmpgeu8(_a, _b) vcmpz8 (vsubsu8 (_b, _a))

#define vmullo16(_a, _b) _mm_mullo_epi16 (_a, _b)
#define vmulhi16(_a, _b) _mm_mulhi_epi16 (_a, _b)

#define vempty() do {} while (0)
#define sfence() _mm_sfence ()

#define vavgu8(_a, _b) _mm_avg_epu8 (_a, _b)
#define fast_vavgu8(_a, _b) vavgu8 (_a, _b)
#define vavgu16(_a, _b) _mm_avg_epu16 (_a, _b)
#define small_vavgu16(_a, _b) vavgu16 (_a, _b)
#define vminu8(_a, _b) _mm_min_epu8 (_a, _b)
#define vmaxu8(_a, _b) _mm_max_epu8 (_a, _b)
#define vmin16(_a, _b) _mm_min_epi16 (_a, _b)
#define vmax16(_a, _b) _mm_max_epi16 (_a, _b)

/* abs (_a - _b). */
static always_inline vu8
vabsdiffu8			(vu8			_a,
				 vu8			_b)
{
	return vor (vsubsu8 (_a, _b), vsubsu8 (_b, _a));
}

/* min (_a, _b), max (_a, _b). */
static always_inline void
vminmaxu8			(vu8 *			_min,
				 vu8 *			_max,
				 vu8			_a,
				 vu8			_b)
{
	*_min = _mm_min_epu8 (_a, _b);
	*_max = _mm_max_epu8 (_a, _b);
}

/* ------------------------------------------------------------------------- */

#if SIMD == CPU_FEATURE_SSE2

#define SUFFIX _SSE2

/* ------------------------------------------------------------------------- */

#elif SIMD == CPU_FEATURE_SSE3

#define SUFFIX _SSE3

#include <pmmintrin.h>

#if 0
#undef vloadu
/* lddqu - loads 2x128 bits and shifts, might be faster than movdqu if
   the data crosses a cache line boundary. */
#define vloadu(_p, _o)							\
	_mm_lddqu_si128 ((__m128i *)((uint8_t *)(_p) + (_o)))
#endif

#endif /* SIMD == CPU_FEATURE_SSE3 */

/* ========================================================================= */

#elif SIMD == CPU_FEATURE_ALTIVEC

#define SUFFIX _ALTIVEC

/* AltiVec equivalent of the MMX/SSE macros. */
/* Please avoid macro nesting, that compiles much slower. */

#include <altivec.h>

typedef vector signed char v8;
typedef vector unsigned char vu8;
typedef vector signed short v16;
typedef vector unsigned short vu16;
typedef vector signed int v32;
typedef vector unsigned int vu32;

/* vec_splat_s8() broken in gcc 3.4? */
#define vzero8() ((v8) vec_splat_s16 (0))

#define vminus18() vec_splat_s8 (-1)

SIMD_CONST_PROTOS

#define vsplatu8i(_i) vec_splat_u8 (_i)
#define vsplatu16i(_i) vec_splat_u16 (_i)
#define vsplatu32i(_i) vec_splat_u32 (_i)

/* FIXME these macros load a scalar variable into each element
   of the vector.  AltiVec has another instruction to load an
   immediate, but it's limited to -16 ... 15. */
#define vsplat8(_i) vec_splat ((v8) vec_lde (0, &(_i)), 0)
#define vsplat16(_i) vec_splat ((v16) vec_lde (0, &(_i)), 0)
#define vsplat32(_i) vec_splat ((v32) vec_lde (0, &(_i)), 0)
#define vsplatu8(_i) vec_splat ((vu8) vec_lde (0, &(_i)), 0)
#define vsplatu16(_i) vec_splat ((vu16) vec_lde (0, &(_i)), 0)
#define vsplatu32(_i) vec_splat ((vu32) vec_lde (0, &(_i)), 0)

#define vload(_p, _o) vec_ld (_o, _p)
#define vstore(_p, _o, _a) vec_st (_a, _o, _p)

#define vloadnt(_p, _o) vec_ldl (_o, _p)
#define vstorent(_p, _o, _a) vec_stl (_a, _o, _p)

#define vand(_a, _b) vec_and (_a, _b)
#define vandnot(_a, _b) vec_andc (_a, _b)
#define vor(_a, _b) vec_or (_a, _b)
#define vxor(_a, _b) vec_xor (_a, _b)
#define vnot(_a) ({ __typeof__ (_a) __a = (_a); vec_nor (__a, __a); })
#define vnand(_a, _b) vnot (vand (_a, _b))
#define vnor(_a, _b) vec_nor (_a, _b)

/* NOTE I've reversed the parameters to be more like ?: */
#define vsel(_mask, _a, _b) vec_sel (_b, _a, _mask)

#define vadd8(_a, _b) vec_add (_a, _b)
#define vadd16(_a, _b) vec_add (_a, _b)
#define vadd32(_a, _b) vec_add (_a, _b)
#define vsub8(_a, _b) vec_sub (_a, _b)
#define vsub16(_a, _b) vec_sub (_a, _b)
#define vsub32(_a, _b) vec_sub (_a, _b)
#define vadds16(_a, _b) vec_adds (_a, _b)
#define vsubs16(_a, _b) vec_subs (_a, _b)
#define vaddsu8(_a, _b) vec_adds (_a, _b)
#define vaddsu16(_a, _b) vec_adds (_a, _b)
#define vsubsu8(_a, _b) vec_subs (_a, _b)
#define vsubsu16(_a, _b) vec_subs (_a, _b)

static always_inline vu8
vsatu8				(vu8			_a,
				 vu8			_min,
				 vu8			_max)
{
	vu8 m = vec_max (_a, _min);
	return vec_min (m, _max);
}

static always_inline v8
vsl18				(v8			_a)
{
	vu8 one = vec_splat_u8 (1);
	return vec_sl (_a, one);
}

static always_inline v8
vsr18				(v8			_a)
{
	vu8 one = vec_splat_u8 (1);
	return vec_sra (_a, one);
}

static always_inline vu8
vsr1u8				(vu8			_a)
{
	vu8 one = vec_splat_u8 (1);
	return vec_sr (_a, one);
}

static always_inline v16
vsl16				(v16			_a,
				 const unsigned int	_i)
{
	vu16 i = vec_splat_u16 (_i);
	return vec_sl (_a, i);
}

static always_inline v16
vsr16				(v16			_a,
				 const unsigned int	_i)
{
	vu16 i = vec_splat_u16 (_i);
	return vec_sra (_a, i);
}

static always_inline vu16
vsru16				(vu16			_a,
				 const unsigned int	_i)
{
	vu16 i = vec_splat_u16 (_i);
	return vec_sr (_a, i);
}

static always_inline v32
vsl32				(v32			_a,
				 const unsigned int	_i)
{
	vu32 i = vec_splat_u32 (_i);
	return vec_sl (_a, i);
}

static always_inline v32
vsr32				(v32			_a,
				 const unsigned int	_i)
{
	vu32 i = vec_splat_u32 (_i);
	return vec_sra (_a, i);
}

static always_inline vu32
vsru32				(vu32			_a,
				 const unsigned int	_i)
{
	vu32 i = vec_splat_u32 (_i);
	return vec_sr (_a, i);
}

/* Long shift right, e.g. (0x0123, 0x4567, 3) -> 0x1234 */
static always_inline vu8
vlsr				(vu8			_h,
				 vu8			_l,
				 const unsigned int	_i)
{
	assert (_i <= 128);

	if (0 == _i) {
		return _l;
	} else if (128 == _i) {
		return _h;
	} else {
		vu8 sel;

		assert (0 == (_i % 8));

		/* 0x000102..1E1F >> _i */
		sel = vec_lvsr (_i / 8, (const unsigned char *) 0);

		return vec_perm (_h, _l, sel);
				
	}
}

static always_inline void
vshiftu2x			(vu8 *			_l,
				 vu8 *			_r,
				 vu8			_am,
				 vu8			_a0,
				 vu8			_a1,
				 const unsigned int	_dist)
{
	assert (_dist <= sizeof (vu8));

	/* 0123 4567 -> 3456 */
	*_l = vlsr (_am, _a0, _dist * 8);
	/* 4567 89AB -> 5678 */
	*_r = vlsr (_a0, _a1, (sizeof (vu8) - _dist) * 8);
}

#define vcmpeq8(_a, _b) vec_cmpeq (_a, _b)
#define vcmpeq16(_a, _b) vec_cmpeq (_a, _b)
#define vcmpeq32(_a, _b) vec_cmpeq (_a, _b)

static always_inline vector bool char
vcmpz8				(v8			_a)
{
	v8 z = vzero8 ();
	return vec_cmpeq (_a, z);
}

static always_inline vector bool short
vcmpz16				(v16			_a)
{
	v16 z = vzero16 ();
	return vec_cmpeq (_a, z);
}

static always_inline vector bool int
vcmpz32				(v32			_a)
{
	v32 z = vzero32 ();
	return vec_cmpeq (_a, z);
}

static always_inline vector bool char
vcmpnz8				(v8			_a)
{
	vector bool char b = vcmpz8 (_a);
	return vnot (b);
}

static always_inline vector bool short
vcmpnz16			(v16			_a)
{
	vector bool short b = vcmpz16 (_a);
	return vnot (b);
}

static always_inline vector bool int
vcmpnz32			(v32			_a)
{
	vector bool b = vcmpz32 (_a);
	return vnot (b);
}

#define vcmpgt8(_a, _b) vec_cmpgt (_a, _b)
#define vcmpgt16(_a, _b) vec_cmpgt (_a, _b)
#define vcmpgt32(_a, _b) vec_cmpgt (_a, _b)

#define vcmpgtu8(_a, _b) vec_cmpgt (_a, _b)
#define vcmpgtu16(_a, _b) vec_cmpgt (_a, _b)
#define vcmpgtu32(_a, _b) vec_cmpgt (_a, _b)

/* Has no integer cmpge. */
static always_inline vector bool char
vcmpgeu8			(vu8			_a,
				 vu8			_b)
{
	v8 d = (v8) vsubsu8 (_b, _a);
	return vcmpz8 (d);
}

static always_inline vu16
vmullo16			(v16			_a,
				 v16			_b)
{
	v16 zero = vzero16 ();
	return (vu16) vec_mladd (_a, _b, zero);
}

#define vempty() do {} while (0)
#define sfence() do {} while (0)
#define vavgu8(_a, _b) vec_avg (_a, _b)
#define fast_vavgu8(_a, _b) vavgu8 (_a, _b)
#define vminu8(_a, _b) vec_min (_a, _b)
#define vmaxu8(_a, _b) vec_max (_a, _b)
#define vmin16(_a, _b) vec_min (_a, _b)
#define vmax16(_a, _b) vec_max (_a, _b)

static always_inline vu8
vabsdiffu8			(vu8			_a,
				 vu8			_b)
{
	/* Shorter than vec_abs (vec_sub (_a, _b)). */
	return vec_sub (vec_max (_a, _b), vec_min (_a, _b));
}

static always_inline void
vminmaxu8			(vu8 *			_min,
				 vu8 *			_max,
				 vu8			_a,
				 vu8			_b)
{
	*_min = vec_min (_a, _b);
	*_max = vec_max (_a, _b);
}

#endif /* SIMD == CPU_FEATURE_ALTIVEC */

/* ========================================================================= */

#define SIMD_NAME2(name, suffix) name ## suffix
#define SIMD_NAME1(name, suffix) SIMD_NAME2 (name, suffix)

/* Depending on the definition of SIMD this turns
   SIMD_NAME (foobar) into foobar_MMX, foobar_SSE etc. */
#define SIMD_NAME(name) SIMD_NAME1 (name, SUFFIX)

#define SIMD_FN_PROTOS(fn_type, name)					\
extern fn_type name ## _SCALAR;						\
extern fn_type name ## _MMX;						\
extern fn_type name ## _3DNOW;						\
extern fn_type name ## _SSE;						\
extern fn_type name ## _SSE2;						\
extern fn_type name ## _SSE3;						\
extern fn_type name ## _ALTIVEC;

#define SIMD_FN_ARRAY_PROTOS(fn_type, name, dimensions)			\
extern fn_type name ## _SCALAR dimensions;				\
extern fn_type name ## _MMX dimensions;					\
extern fn_type name ## _3DNOW dimensions;				\
extern fn_type name ## _SSE dimensions;					\
extern fn_type name ## _SSE2 dimensions;				\
extern fn_type name ## _SSE3 dimensions;				\
extern fn_type name ## _ALTIVEC dimensions;

#if defined (CAN_COMPILE_MMX)
#  define SIMD_FN_SELECT_MMX(name, avail)				\
	(((avail) & CPU_FEATURE_MMX) & cpu_features) ? name ## _MMX
#else
#  define SIMD_FN_SELECT_MMX(name, avail) 0 ? NULL
#endif

#if defined (CAN_COMPILE_3DNOW)
#  define SIMD_FN_SELECT_3DNOW(name, avail)				\
	(((avail) & CPU_FEATURE_3DNOW) & cpu_features) ? name ## _3DNOW
#else
#  define SIMD_FN_SELECT_3DNOW(name, avail) 0 ? NULL
#endif

#if defined (CAN_COMPILE_SSE)
#  define SIMD_FN_SELECT_SSE(name, avail)				\
	(((avail) & (CPU_FEATURE_SSE_INT |				\
		     CPU_FEATURE_SSE_FLT)) & cpu_features) ? name ## _SSE
#else
#  define SIMD_FN_SELECT_SSE(name, avail) 0 ? NULL
#endif

#if defined (CAN_COMPILE_SSE2)
#  define SIMD_FN_SELECT_SSE2(name, avail)				\
	(((avail) & CPU_FEATURE_SSE2) & cpu_features) ? name ## _SSE2
#else
#  define SIMD_FN_SELECT_SSE2(name, avail) 0 ? NULL
#endif

#if defined (CAN_COMPILE_SSE3)
#  define SIMD_FN_SELECT_SSE3(name, avail)				\
	(((avail) & CPU_FEATURE_SSE3) & cpu_features) ? name ## _SSE3
#else
#  define SIMD_FN_SELECT_SSE3(name, avail) 0 ? NULL
#endif

#if defined (CAN_COMPILE_ALTIVEC)
#  define SIMD_FN_SELECT_ALTIVEC(name, avail)				\
	(((avail) & CPU_FEATURE_ALTIVEC) & cpu_features) ? name ## _ALTIVEC
#else
#  define SIMD_FN_SELECT_ALTIVEC(name, avail) 0 ? NULL
#endif

/* Selects a SIMD function depending on CPU features. */
/* TODO: function automagically learning which of the executable
   versions works fastest on this machine. */
#define SIMD_FN_SELECT(name, avail)					\
	(SIMD_FN_SELECT_ALTIVEC (name, avail) :				\
	 SIMD_FN_SELECT_SSE3 (name, avail) :				\
	 SIMD_FN_SELECT_SSE2 (name, avail) :				\
	 SIMD_FN_SELECT_SSE (name, avail) :				\
	 SIMD_FN_SELECT_3DNOW (name, avail) :				\
	 SIMD_FN_SELECT_MMX (name, avail) :				\
	 ((avail) & SCALAR) ? name ## _SCALAR : NULL)

#define SIMD_FN_ALIGNED_SELECT(name, align, avail)			\
	(likely (0 == ((align) & 15)) ?					\
	 SIMD_FN_SELECT (name, avail) :					\
	 likely (0 == ((align) & 7)) ?					\
	 SIMD_FN_SELECT (name, (avail) & (CPU_FEATURE_SSE_INT |		\
					  CPU_FEATURE_3DNOW |		\
					  CPU_FEATURE_MMX |		\
					  SCALAR)) :			\
	 SIMD_FN_SELECT (name, (avail) & SCALAR))

#endif /* SIMD_H */