summaryrefslogtreecommitdiff
path: root/src/xgi_memcpy.c
blob: fd90b33ae8e2468fb0131e3e637c93fdcda5254c (plain)
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
1300
1301
1302
1303
1304
1305
/*
 * XGI memcpy() routines (assembly)
 *
 * Copyright (C) 2004-2005 Thomas Winischhofer
 *
 * Idea and some code bits from via_memcpy.c which is
 * Copyright (C) 2004 Thomas Hellstroem, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE CODE SUPPLIER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* #include "xgi.h" */

/* Jong 07/10/2008; use run-time debug instead except for HW acceleration routines */
/* extern bool g_bRunTimeDebug;
#define RUNTIMEDEBUG(p)		if(g_bRunTimeDebug)p; */

/* #define DEBUG */
#ifdef  DEBUG
#define PDEBUG(p)     p  /* RUNTIMEDEBUG(p) */
#else
#define PDEBUG(p)
#endif

/* Jong 01/15/2009; CPU flags (for memcpy() etc.) */
#define XGI_CPUFL_LIBC  0x0001
#define XGI_CPUFL_BI    0x0002
#define XGI_CPUFL_SSE   0x0004
#define XGI_CPUFL_MMX   0x0008
#define XGI_CPUFL_3DNOW 0x0010
#define XGI_CPUFL_MMX2  0x0020
#define XGI_CPUFL_BI2   0x0040
#define XGI_CPUFL_SSE2  0x0080
#define XGI_CPUFL_FLAG  0x8000

#if 0			/* Debug */
#define XGIDGBMC
#endif

/* Jong 01/07/2009; debug */
/*
#ifdef XGI_DEBUG
#define PDEBUG(p)  p
#else
#define PDEBUG(p)
#endif
*/

#if 0 /* Jong 01/15/2009; ignore at the moment */
extern unsigned int XGIAllocateFBMemory(ScrnInfoPtr pScrn, void **handle, int bytesize);
extern void	    XGIFreeFBMemory(ScrnInfoPtr pScrn, void **handle);
#endif

#define CPUBUFFERSIZE 2048       /* Size of /proc/cpuinfo buffer */
#define BUFFERSIZE (576 * 1152)  /* Matches 720x576 YUV420 */

/************************************************************************/
/*                   arch specific memcpy() routines                    */
/************************************************************************/

/* i386, AMD64 */

#define FENCE 			\
     __asm__ __volatile__( 	\
		  " sfence\n" 	\
		  :		\
		  :		\
		  : "memory");

#define FENCEMMS 		\
     __asm__ __volatile__ (	\
		  " sfence\n"	\
		  " emms\n"	\
		  :		\
		  :		\
		  : "memory");

#define FEMMS 			\
     __asm__ __volatile__(	\
		  " femms\n"	\
		  :		\
		  :		\
		  : "memory");

#define EMMS 			\
     __asm__ __volatile__(	\
		  " emms\n"	\
		  :		\
		  :		\
		  : "memory");

#define SSE_PREFETCH " prefetchnta "
#define NOW_PREFETCH " prefetch "

#define PREFETCH1(arch_prefetch,from)		\
    __asm__ __volatile__ (			\
		  arch_prefetch "(%0)\n"	\
		  arch_prefetch "32(%0)\n"	\
		  arch_prefetch "64(%0)\n"	\
		  arch_prefetch "96(%0)\n"	\
		  arch_prefetch "128(%0)\n"	\
		  arch_prefetch "160(%0)\n"	\
		  arch_prefetch "192(%0)\n"	\
		  arch_prefetch "256(%0)\n"	\
		  arch_prefetch "288(%0)\n"	\
		  : 				\
		  : "r" (from) );

#define PREFETCH2(arch_prefetch,from)		\
    __asm__ __volatile__ (			\
		  arch_prefetch "320(%0)\n"	\
		  : 				\
		  : "r" (from) );

#define PREFETCH3(arch_prefetch,from)		\
    __asm__ __volatile__ (			\
		  arch_prefetch "288(%0)\n"	\
		  : 				\
		  : "r" (from) );

#define small_memcpy_i386(to,from,n)					\
    {									\
	__asm__ __volatile__(						\
		  " cld\n"						\
		  " shrl $1, %%ecx\n"					\
		  " jnc 1f\n"						\
		  " movsb\n"						\
		"1: shrl $1, %%ecx\n"					\
		  " jnc 2f\n"						\
		  " movsw\n"						\
		"2: rep ; movsl"					\
		  : "=&D" (to), "=&S" (from)				\
		  : "c" (n), "0" ((long) to), "1" ((long) from) 	\
		  : "memory", "cc");					\
      PDEBUG(ErrorF("Jong-small_memcpy_i386(to,from,n)...\n"));		\
    }

#define small_memcpy_amd64(to,from,n)					\
    {									\
	__asm__ __volatile__(						\
		  " cld\n"						\
		  " shrq $1, %%rcx\n"					\
		  " jnc 1f\n"						\
		  " movsb\n"						\
		"1: shrq $1, %%rcx\n"					\
		  " jnc 2f\n"						\
		  " movsw\n"						\
		"2: shrq $1, %%rcx\n"					\
		  " jnc 3f\n"						\
		  " movsl\n"						\
		"3: rep ; movsq"					\
		  : "=&D" (to), "=&S" (from)				\
		  : "c" (n), "0" ((long) to), "1" ((long) from) 	\
		  : "memory", "cc");					\
      PDEBUG(ErrorF("Jong-small_memcpy_amd64(to,from,n)...\n"));		\
    }

#define MMX_CPY(prefetch,from,to,dummy,lcnt)				\
    __asm__ __volatile__ (						\
	        "1:\n"							\
		    prefetch "320(%1)\n"				\
	          " movq (%1), %%mm0\n"					\
		  " movq 8(%1), %%mm1\n"				\
		  " movq 16(%1), %%mm2\n"				\
		  " movq 24(%1), %%mm3\n"				\
		  " movq %%mm0, (%0)\n"					\
		  " movq %%mm1, 8(%0)\n"				\
		  " movq %%mm2, 16(%0)\n"				\
		  " movq %%mm3, 24(%0)\n"				\
		    prefetch "352(%1)\n"				\
		  " movq 32(%1), %%mm0\n"				\
		  " movq 40(%1), %%mm1\n"				\
		  " movq 48(%1), %%mm2\n"				\
		  " movq 56(%1), %%mm3\n"				\
		  " leal 64(%1),%1\n"					\
		  " movq %%mm0, 32(%0)\n"				\
		  " movq %%mm1, 40(%0)\n"				\
		  " movq %%mm2, 48(%0)\n"				\
		  " movq %%mm3, 56(%0)\n"				\
		  " decl %2\n"						\
		  " leal 64(%0),%0\n"					\
		  " jne 1b\n"						\
		  : "=&D"(to), "=&S"(from), "=&r"(dummy)		\
		  : "0" (to), "1" (from), "2" (lcnt) 			\
		  : "memory", "cc");	\
        PDEBUG(ErrorF("Jong-MMX_CPY(prefetch,from,to,dummy,lcnt)...\n"));


#define SSE_CPY(prefetch,from,to,dummy,lcnt)				\
    if((ULong) from & 15) {						\
	__asm__ __volatile__ (						\
		"1:\n"							\
		    prefetch "320(%1)\n"				\
		  " movups (%1), %%xmm0\n"				\
		  " movups 16(%1), %%xmm1\n"				\
		  " movntps %%xmm0, (%0)\n"				\
		  " movntps %%xmm1, 16(%0)\n"				\
		    prefetch "352(%1)\n"				\
		  " movups 32(%1), %%xmm2\n"				\
		  " movups 48(%1), %%xmm3\n"				\
		  " leal 64(%1),%1\n"					\
		  " movntps %%xmm2, 32(%0)\n"				\
		  " movntps %%xmm3, 48(%0)\n"				\
		  " decl %2\n"						\
		  " leal 64(%0),%0\n"					\
		  " jne 1b\n"						\
		  : "=&D"(to), "=&S"(from), "=&r"(dummy)		\
		  : "0" (to), "1" (from), "2" (lcnt)			\
		  : "memory", "cc"); 					\
	      PDEBUG(ErrorF("Jong-SSE_CPY(prefetch,from,to,dummy,lcnt)-1...\n")); \
    } else {								\
	__asm__ __volatile__ (						\
		"2:\n"							\
		    prefetch "320(%1)\n"				\
		  " movaps (%1), %%xmm0\n"				\
		  " movaps 16(%1), %%xmm1\n"				\
		  " movntps %%xmm0, (%0)\n"				\
		  " movntps %%xmm1, 16(%0)\n"				\
        	    prefetch "352(%1)\n"				\
		  " movaps 32(%1), %%xmm2\n"				\
		  " movaps 48(%1), %%xmm3\n"				\
		  " leal 64(%1),%1\n"					\
		  " movntps %%xmm2, 32(%0)\n"				\
		  " movntps %%xmm3, 48(%0)\n"				\
		  " decl %2\n"						\
		  " leal 64(%0),%0\n"					\
		  " jne 2b\n"						\
		  : "=&D"(to), "=&S"(from), "=&r"(dummy)		\
		  : "0" (to), "1" (from), "2" (lcnt)			\
		  : "memory", "cc");					\
	      PDEBUG(ErrorF("Jong-SSE_CPY(prefetch,from,to,dummy,lcnt)-2...\n")); \
    }

#define SSE64_CPY(prefetch,from,to,dummy,lcnt)				\
    if((ULong) from & 15) {						\
	__asm__ __volatile__ (						\
		"1:\n"							\
		    prefetch "320(%1)\n"				\
		  " movups (%1), %%xmm0\n"				\
		  " movups 16(%1), %%xmm1\n"				\
		  " movntps %%xmm0, (%0)\n"				\
		  " movntps %%xmm1, 16(%0)\n"				\
		    prefetch "352(%1)\n"				\
		  " movups 32(%1), %%xmm2\n"				\
		  " movups 48(%1), %%xmm3\n"				\
		  " leaq 64(%1),%1\n"					\
		  " movntps %%xmm2, 32(%0)\n"				\
		  " movntps %%xmm3, 48(%0)\n"				\
		  " decl %2\n"						\
		  " leaq 64(%0),%0\n"					\
		  " jne 1b\n"						\
		  : "=&D"(to), "=&S"(from), "=&r"(dummy)		\
		  : "0" (to), "1" (from), "2" (lcnt)			\
		  : "memory", "cc"); 					\
	      PDEBUG(ErrorF("Jong-SSE64_CPY(prefetch,from,to,dummy,lcnt)-1...\n")); \
    } else {								\
	__asm__ __volatile__ (						\
		"2:\n"							\
		    prefetch "320(%1)\n"				\
		  " movaps (%1), %%xmm0\n"				\
		  " movaps 16(%1), %%xmm1\n"				\
		  " movntps %%xmm0, (%0)\n"				\
		  " movntps %%xmm1, 16(%0)\n"				\
        	    prefetch "352(%1)\n"				\
		  " movaps 32(%1), %%xmm2\n"				\
		  " movaps 48(%1), %%xmm3\n"				\
		  " leaq 64(%1),%1\n"					\
		  " movntps %%xmm2, 32(%0)\n"				\
		  " movntps %%xmm3, 48(%0)\n"				\
		  " decl %2\n"						\
		  " leaq 64(%0),%0\n"					\
		  " jne 2b\n"						\
		  : "=&D"(to), "=&S"(from), "=&r"(dummy)		\
		  : "0" (to), "1" (from), "2" (lcnt)			\
		  : "memory", "cc");					\
	      PDEBUG(ErrorF("Jong-SSE64_CPY(prefetch,from,to,dummy,lcnt)-2...\n")); \
    }

#define MMXEXT_CPY(prefetch,from,to,dummy,lcnt)				\
    __asm__ __volatile__ (						\
		  ".p2align 4,,7\n"					\
		 "1:\n"							\
		    prefetch "320(%1)\n"				\
		  " movq (%1), %%mm0\n"					\
		  " movq 8(%1), %%mm1\n"				\
		  " movq 16(%1), %%mm2\n"				\
		  " movq 24(%1), %%mm3\n"				\
		  " movntq %%mm0, (%0)\n"				\
		  " movntq %%mm1, 8(%0)\n"				\
		  " movntq %%mm2, 16(%0)\n"				\
		  " movntq %%mm3, 24(%0)\n"				\
		    prefetch "352(%1)\n"				\
		  " movq 32(%1), %%mm0\n"				\
		  " movq 40(%1), %%mm1\n"				\
		  " movq 48(%1), %%mm2\n"				\
		  " movq 56(%1), %%mm3\n"				\
		  " leal 64(%1),%1\n"					\
		  " movntq %%mm0, 32(%0)\n"				\
		  " movntq %%mm1, 40(%0)\n"				\
		  " movntq %%mm2, 48(%0)\n"				\
		  " movntq %%mm3, 56(%0)\n"				\
		  " decl %2\n"						\
		  " leal 64(%0),%0\n"					\
		  " jne 1b\n"						\
		  : "=&D"(to), "=&S"(from), "=&r"(dummy)		\
		  : "0" (to), "1" (from), "2" (lcnt) 			\
		  : "memory", "cc");	\
	      PDEBUG(ErrorF("Jong-MMXEXT_CPY(prefetch,from,to,dummy,lcnt)...\n")); 


#define PREFETCH_FUNC(prefix,itype,ptype,begin,fence,small)		\
									\
/*    static void prefix##_memcpy(UChar *to,		*/		\
    void prefix##_memcpy(UChar *to,				\
				const UChar *from,			\
				int size)				\
    {									\
	int lcnt = size >> 6;						\
	int rest = size & 63;						\
	register int dummy;						\
								\
	PREFETCH1(ptype##_PREFETCH,from);				\
	begin;								\
    PDEBUG(ErrorF("Jong-After-begin()...\n"));	\
	if(lcnt) {							\
	   PDEBUG(ErrorF("Jong-Before-SSE_CPY()...\n"));		\
	   itype##_CPY(ptype##_PREFETCH,from,to,dummy,lcnt);		\
	   PDEBUG(ErrorF("Jong-After-SSE_CPY()...\n"));		\
	}								\
	if(rest) {							\
	   PDEBUG(ErrorF("Jong-Before-PREFETCH2()...\n"));	\
	   PREFETCH2(ptype##_PREFETCH,from);				\
	   PDEBUG(ErrorF("Jong-After-PREFETCH2()...\n"));	\
	   small(to, from, rest);					\
	   PDEBUG(ErrorF("Jong-After-small...\n"));		\
	   PREFETCH3(ptype##_PREFETCH,from);				\
	   PDEBUG(ErrorF("Jong-After-PREFETCH3()...\n"));	\
	}								\
    PDEBUG(ErrorF("Jong-Before-fence...\n"));	\
	fence;								\
    PDEBUG(ErrorF("Jong-PREFETCH_FUNC(prefix,itype,ptype,begin,fence,small)-end...\n"));	\
    }

#define NOPREFETCH_FUNC(prefix,itype,begin,fence,small)			\
									\
    static void prefix##_memcpy(UChar *to,				\
				const UChar *from,			\
				int size)				\
    {									\
	int lcnt = size >> 6;						\
	int rest = size & 63;						\
	register int dummy;						\
									\
	begin;								\
	if(lcnt) {							\
	   itype##_CPY("#",from,to,dummy,lcnt);				\
	}								\
	if(rest) {							\
	   small(to, from, rest);					\
	}								\
	fence;								\
    PDEBUG(ErrorF("Jong-NOPREFETCH_FUNC(prefix,itype,ptype,begin,fence,small)...\n"));	\
    }

/* Other archs */

/* ... */


#if 0 /* Jong 01/15/2009; ignore at the moment */
/* Type for table for benchmark list */
typedef struct {
    vidCopyFunc  mFunc;
    char         *mName;
    unsigned int mycpuflag;
    int          grade;
    int 	 gradefrom;
    Bool         reqAlignment;
} XGIMCFuncData;
#endif

/************************************************************************/
/*                   libc memcpy() wrapper - generic                    */
/************************************************************************/
#define UChar  unsigned char

void XGI_libc_memcpy(UChar *dst, const UChar *src, int size)
{
    PDEBUG(ErrorF("Jong-XGI_libc_memcpy()...\n"));
    memcpy(dst, src, size);
}

/************************************************************************/
/* We only do all that stuff under gcc; no idea what other compilers 	*/
/* would do with our asm code.  					*/
/************************************************************************/

#ifndef __GNUC__

unsigned int XGIGetCPUFlags(ScrnInfoPtr pScrn)
{
    PDEBUG(ErrorF("Jong-XGIGetCPUFlags(ScrnInfoPtr pScrn)...\n"));
    return 0;
}

vidCopyFunc XGIVidCopyInit(ScreenPtr pScreen, vidCopyFunc *UMemCpy, Bool from)
{
    PDEBUG(ErrorF("Jong-XGIVidCopyInit()...\n"));
    *UMemCpy = XGI_libc_memcpy;
    return XGI_libc_memcpy;
}

vidCopyFunc XGIVidCopyGetDefault(void)
{
    PDEBUG(ErrorF("Jong-XGIVidCopyGetDefault()...\n"));
    return XGI_libc_memcpy;
}

#else /* ! Everything below is gcc specific ! */

/************************************************************************/
/*                    Definitions for archs and OSes                    */
/************************************************************************/

#undef XGI_checkosforsse
#undef XGI_canBenchmark
#undef XGI_haveProc
#undef XGI_haveBuiltInMC

/* Jong Lin */
#if defined(__arm__) 
void XGI_builtin_memcpy_arm(UChar *to, const UChar *from, int n)
{
    long d1,d2,d3;

    PDEBUG(ErrorF("XGI_builtin_memcpy_arm_begin-size=%d...\n", n));

    __asm__  __volatile__(
	" start: \n"
		   /* " stmfd sp!, {r0-r12}\n" */ /* Save some working registers */
	" blockcopy: \n"
		   " movs r3,r2, lsr #3\n" /* Number of eight word multiples */
		   " beq copywords\n" /* Less than eight words to move? */
		   " stmfd sp!, {r4-r11}\n" /* Save some working registers */
	" octcopy: \n"
		   " ldmia r1!, {r4-r11}\n" /* Load 8 words from the source */
		   " stmia r0!, {r4-r11}\n" /* Put them at the destination */
		   " subs r3, r3, #1\n" /* Decrement the counter */
		   " bne octcopy\n" /* ... copy more */
		   " ldmfd sp!, {r4-r11}\n" /* Don't need these now - restore */
	" copywords: \n"
		   " ands r2, r2, #7\n" /* Number of odd words to copy */
		   " beq stop\n" /* No words left to copy? */
	" wordcopy: \n"
		  " ldr r3, [r1], #4\n" /* Load a word from the source */
		  " str r3, [r0], #4\n" /* and store it to the destination */
		  " subs r2, r2, #1\n" /* Decrement the counter */
		  " bne wordcopy\n" /*  ... copy more */
	" stop: \n"
		  /* "ldmfd sp!, {r0-r12}\n" */ /* Don't need these now - restore */
		  : 
		  :"r" ((long) to), "r" ((long) from), "r" ((unsigned long) n)
		  : "memory", "r3");

}
#endif

#if defined(__i386__) /* ***************************************** i386 */

#define XGI_checkosforsse 	/* Does this cpu support sse and do we need to check os? */
#define XGI_canBenchmark	/* Can we perform a benchmark? */
#ifdef XGI_LINUX
#define XGI_haveProc		/* Do we have /proc/cpuinfo or similar? */
#endif
#define XGI_haveBuiltInMC	/* Is there a built-in memcpy for this arch? */

/* Built-in memcpy for i386 */
#define size_t	int
static __inline void * builtin_memcpy(void * to, const void * from, size_t n)
{
    int d1,d2,d3;

    __asm__ __volatile__(
		  " cld\n"
		  " shrl $1, %%ecx\n"
		  " jnc 1f\n"
		  " movsb\n"
		"1: shrl $1, %%ecx\n"
		  " jnc 2f\n"
		  " movsw\n"
		"2: rep ; movsl\n"
		  : "=&c" (d1), "=&D" (d2), "=&S" (d3)
		  : "0" (n), "1" ((long) to), "2" ((long) from)
		  : "memory", "cc");

    PDEBUG(ErrorF("Jong-__inline builtin_memcpy() for i386...\n"));
    return(to);
}

/* Alternative for 586: Unroll loop, copy 32 bytes at a time */
static void XGI_builtin_memcp2(UChar *to, const UChar *from, int n)
{
    int d1,d2,d3;

    __asm__ __volatile__(
		  " movl %%edi, %%eax\n"
		  " cmpl $32, %%ecx\n"
		  " cld\n"
		  " jbe 3f\n"
		  " negl %%eax\n"		/* Align dest */
		  " andl $3, %%eax\n"
		  " subl %%eax, %%ecx\n"
		  " xchgl %%eax, %%ecx\n"
		  " rep ; movsb\n"
		  " movl %%eax, %%ecx\n"
		  " subl $32, %%ecx\n"
		  " js 2f\n"
		  " movl (%%edi), %%eax\n"
		"1: movl 28(%%edi), %%edx\n"   	/* Trick: Read-ahead */
		  " subl $32, %%ecx\n"
		  " movl (%%esi), %%eax\n"
		  " movl 4(%%esi), %%edx\n"
		  " movl %%eax, (%%edi)\n"
		  " movl %%edx, 4(%%edi)\n"
		  " movl 8(%%esi), %%eax\n"
		  " movl 12(%%esi), %%edx\n"
		  " movl %%eax, 8(%%edi)\n"
		  " movl %%edx, 12(%%edi)\n"
		  " movl 16(%%esi), %%eax\n"
		  " movl 20(%%esi), %%edx\n"
		  " movl %%eax, 16(%%edi)\n"
		  " movl %%edx, 20(%%edi)\n"
		  " movl 24(%%esi), %%eax\n"
		  " movl 28(%%esi), %%edx\n"
		  " movl %%eax, 24(%%edi)\n"
		  " movl %%edx, 28(%%edi)\n"
		  " leal 32(%%esi), %%esi\n"
		  " leal 32(%%edi), %%edi\n"
		  " jns 1b\n"
		"2: addl $32, %%ecx\n"
		"3: rep ; movsb"
		  : "=&c" (d1), "=&D" (d2), "=&S" (d3)
		  : "0" (n), "1" ((long) to), "2" ((long) from)
		  : "eax", "edx", "memory", "cc");

    PDEBUG(ErrorF("Jong-XGI_builtin_memcp2()-copy 32 bytes at a time-586...\n"));
}

static unsigned int taketime(void)	/* get current time (for benchmarking) */
{
    unsigned int eax;

    __asm__ volatile (
		" pushl %%ebx\n"
		" cpuid\n"
		" rdtsc\n"
		" popl %%ebx\n"
		: "=a" (eax)
		: "0" (0)
		: "ecx", "edx", "cc");

    return(eax);
}

#elif defined(__AMD64__) || defined(__amd64__) || defined(__x86_64__) /***************** AMD64 */

#define XGI_checkosforsse	/* Does this cpu support sse and do we need to check os? */
#define XGI_canBenchmark	/* Can we perform a benchmark? */
#ifdef XGI_LINUX
#define XGI_haveProc		/* Do we have /proc/cpuinfo or similar? */
#endif
#define XGI_haveBuiltInMC	/* Is there a built-in memcpy for this arch? */

/* Built-in memcpy for AMD64 */
static __inline void * builtin_memcpy(void * to, const void * from, int n)
{
    long d1, d2, d3;

    __asm__ __volatile__ (
		" cld\n"
		" rep ; movsq\n"
		" movq %4, %%rcx\n"
		" rep ; movsb"
		: "=%c" (d1), "=&D" (d2), "=&S" (d3)
		: "0" ((ULong)(n >> 3)), "q" ((ULong)(n & 7)),
		  "1" ((long) to), "2" ((long) from)
		: "memory");

    PDEBUG(ErrorF("Jong-builtin_memcpy() for AMD64...\n"));
    return(to);
}

/* Alternative: Unroll loop, copy 32 bytes at a time */
static void XGI_builtin_memcp2(UChar *to, const UChar *from, int n)
{
    long d1,d2,d3;

    __asm__ __volatile__(
		  " movq %%rdi, %%rax\n"
		  " cmpq $32, %%rcx\n"
		  " cld\n"			/* Pipeline; no other flags but DF */
		  " jbe 1f\n"
		  " negq %%rax\n"		/* Align dest */
		  " andq $7, %%rax\n"
		  " subq %%rax, %%rcx\n"
		  " xchgq %%rax, %%rcx\n"
		  " rep ; movsb\n"
		  " movq %%rax, %%rcx\n"
		  " subq $32, %%rcx\n"
		  " js 2f\n"
		  ".p2align 4\n"
		"3: subq $32, %%rcx\n"
		  " movq (%%rsi), %%rax\n"
		  " movq 8(%%rsi), %%rdx\n"
		  " movq 16(%%rsi), %%r8\n"
		  " movq 24(%%rsi), %%r9\n"
		  " movq %%rax, (%%rdi)\n"
		  " movq %%rdx, 8(%%rdi)\n"
		  " movq %%r8, 16(%%rdi)\n"
		  " movq %%r9, 24(%%rdi)\n"
		  " leaq 32(%%rsi), %%rsi\n"
		  " leaq 32(%%rdi), %%rdi\n"
		  " jns 3b\n"
		"2: addq $32, %%rcx\n"
		"1: rep ; movsb"
		  : "=&c" (d1), "=&D" (d2), "=&S" (d3)
		  :"0" ((ULong) n), "1" ((long) to), "2" ((long) from)
		  : "rax", "rdx", "r8", "r9", "memory", "cc");

    PDEBUG(ErrorF("Jong-XGI_builtin_memcp2()-copy 32 bytes at a time-AMD...\n"));
}

static unsigned int taketime(void)	/* get current time (for benchmarking) */
{
    unsigned int eax;

    __asm__ volatile (
		" pushq %%rbx\n"
		" cpuid\n"
		" rdtsc\n"
		" popq %%rbx\n"
		: "=a" (eax)
		: "0" (0)
		: "rcx", "rdx", "cc");

    return(eax);
}

#else		/* **************************************** Other archs */

/* 1. Can we do a benchmark?		*/
/* #define XGI_canBenchmark		*/

/* 2. Do we have /proc filesystem or similar for CPU information? */
/* #define XGI_haveproc			*/

/* 3. Optional: build-in memcpy()	*/
/* #define XGI_haveBuiltInMC		*/
/* static __inline void * builtin_memcpy(void * to, const void * from, int n)
   {
   }
*/

/* 4. Function for getting current time (for benchmarking)  */
/* static unsigned int taketime(void)
   {
   }
*/

#endif

/************************************************************************/
/*                   Generic built-in memcpy wrapper                    */
/************************************************************************/

#ifdef XGI_haveBuiltInMC
static void XGI_builtin_memcpy(UChar *dst, const UChar *src, int size)
{
    PDEBUG(ErrorF("Jong-XGI_builtin_memcpy()...\n"));
    builtin_memcpy(dst, src, size);
}
#endif

#if 0 /* Jong 01/15/2009; ignore at the moment */
/************************************************************************/
/* Generic routines if Benchmark can be performed (all archs, all OSes) */
/************************************************************************/

#ifdef XGI_canBenchmark

/* Get time (unsigned int) */
static unsigned int time_function(vidCopyFunc mf, UChar *buf1, UChar *buf2, int size)
{
    unsigned int t1, t2;

    t1 = taketime();

    (*mf)(buf1, buf2, size);

    t2 = taketime();

    return((t1 <  t2) ? t2 - t1 : 0xFFFFFFFFU - (t1 - t2 - 1));
}

/* Allocate an area of offscreen FB memory (buf1), a simulated video
 * player buffer (buf2) and a pool of uninitialized "video" data (buf3).
 */
static void *
XGI_AllocBuffers(ScrnInfoPtr pScrn, UChar **buf1, UChar **buf2, UChar **buf3)
{
    XGIPtr pXGI = XGIPTR(pScrn);
    unsigned int offset;
    void *handle = NULL;

    if(!(offset = XGIAllocateFBMemory(pScrn, &handle, BUFFERSIZE + 31))) {
       return NULL;
    }
    (*buf1) = (UChar *)pXGI->FbBase + offset;
    (*buf1) = (UChar *)(((ULong)(*buf1) + 31) & ~31);

    if(!((*buf2) = (UChar *)malloc(BUFFERSIZE + 15))) {
       XGIFreeFBMemory(pScrn, &handle);
       return NULL;
    }

    if(!((*buf3) = (UChar *)malloc(BUFFERSIZE + 15))) {
       free((*buf2));
       XGIFreeFBMemory(pScrn, &handle);
       return NULL;
    }

    return handle;
}
#endif

/* Perform Benchmark */
static int XGI_BenchmarkMemcpy(ScrnInfoPtr pScrn, XGIMCFuncData *MCFunctions,
                               unsigned int myCPUflags, UChar *buf1, UChar *buf2,
			       UChar *buf3, char *frqBuf, double cpuFreq,
			       vidCopyFunc *UMemCpy, int *best2, Bool from)
{
    XGIMCFuncData *curData;
    int j = 0, bestSoFar = 0;
    unsigned int tmp1, tmp2, best = 0xFFFFFFFFU, sbest = 0xFFFFFFFFU;

    (*best2) = 0;

    /* Make probable buf1 and buf2 are not paged out by referencing them */
    XGI_libc_memcpy(buf1, buf2, BUFFERSIZE);

    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	       "Benchmarking %s RAM to %s RAM memory transfer methods:\n",
	       from ? "video" : "system",
	       from ? "system" : "video");

#ifdef TWDEBUG
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Benchmark: CPUFlags %x\n", myCPUflags);
#endif

    j = 0;
    while(MCFunctions[j].mFunc) {
	PDEBUG(ErrorF("Check function[%d]-%s...\n", j, MCFunctions[j].mName));

	curData = MCFunctions + j;

	if(myCPUflags & curData->mycpuflag) {

	   /* Simulate setup of the video buffer and copy result to framebuffer */
	   /* Do this 4 times to verify results */
	   if(!from) {
	      XGI_builtin_memcpy(buf2, buf3, BUFFERSIZE);
	      tmp1 = time_function(curData->mFunc, buf1, buf2, BUFFERSIZE);
	      XGI_builtin_memcpy(buf2, buf3, BUFFERSIZE);
	      tmp2 = time_function(curData->mFunc, buf1, buf2, BUFFERSIZE);
	      tmp1 = (tmp2 < tmp1) ? tmp2 : tmp1;
	      XGI_builtin_memcpy(buf2, buf3, BUFFERSIZE);
	      tmp2 = time_function(curData->mFunc, buf1, buf2, BUFFERSIZE);
	      tmp1 = (tmp2 < tmp1) ? tmp2 : tmp1;
	      XGI_builtin_memcpy(buf2, buf3, BUFFERSIZE);
	      tmp2 = time_function(curData->mFunc, buf1, buf2, BUFFERSIZE);
	      tmp1 = (tmp2 < tmp1) ? tmp2 : tmp1;
	   } else {
	      XGI_builtin_memcpy(buf3, buf2, BUFFERSIZE);
	      tmp1 = time_function(curData->mFunc, buf2, buf1, BUFFERSIZE);
	      XGI_builtin_memcpy(buf3, buf2, BUFFERSIZE);
	      tmp2 = time_function(curData->mFunc, buf2, buf1, BUFFERSIZE);
	      tmp1 = (tmp2 < tmp1) ? tmp2 : tmp1;
	      XGI_builtin_memcpy(buf3, buf2, BUFFERSIZE);
	      tmp2 = time_function(curData->mFunc, buf2, buf1, BUFFERSIZE);
	      tmp1 = (tmp2 < tmp1) ? tmp2 : tmp1;
	      XGI_builtin_memcpy(buf3, buf2, BUFFERSIZE);
	      tmp2 = time_function(curData->mFunc, buf2, buf1, BUFFERSIZE);
	      tmp1 = (tmp2 < tmp1) ? tmp2 : tmp1;
	   }

	   if((!frqBuf) || (tmp1 == 0)) {
	      xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
			   "\tChecked %s memcpy()... \t%u\n",curData->mName, tmp1);
	   } else {
	      xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
			   "\tChecked %s memcpy()... \t%.1f MiB/s\n",
			   curData->mName,
			   cpuFreq * 1.e6 * (double)BUFFERSIZE / ((double)(tmp1) * (double)(0x100000)));
	   }

	   if(tmp1 < best) {
	      best = tmp1;
	      bestSoFar = j;
		  PDEBUG(ErrorF("* bestSoFar is %d...\n", bestSoFar));
	   }

	   if(!curData->reqAlignment) {
	      if(tmp1 < sbest) {
	         sbest = tmp1;
	         (*best2) = j;
	      }
	   }

	}

	j++;
    }

    PDEBUG(ErrorF("***** The best is func(%d)-%s...\n", bestSoFar, MCFunctions[bestSoFar].mName));

	/* Jong 01/07/2009; test for performance */
	/* bestSoFar = 2; */ /* the worst case; buit-in-2 */
    return bestSoFar;
}

static vidCopyFunc XGI_GetBestByGrade(ScrnInfoPtr pScrn, XGIMCFuncData *MCFunctions,
			unsigned int myCPUflags, vidCopyFunc *UMemCpy, Bool from)
{
    int j = 0, best = -1, secondbest = -1, bestSoFar = 10, best2SoFar = 10;
    int grade;

    *UMemCpy = XGI_libc_memcpy;

    while(MCFunctions[j].mFunc) {
	if(myCPUflags & MCFunctions[j].mycpuflag) {
	   grade = from ? MCFunctions[j].gradefrom : MCFunctions[j].grade;
	   if(grade < bestSoFar) {
	      best = j;
	      bestSoFar = grade;
	   }
	   if(grade < best2SoFar) {
	      if(!MCFunctions[j].reqAlignment) {
	         secondbest = j;
		 best2SoFar = grade;
	      }
	   }
	}
	j++;
    }
    if(best >= 0) {
       xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		"Chose %s method for aligned data transfers %s video RAM\n",
		MCFunctions[best].mName,
		from ? "from" : "to");
       if(secondbest >= 0) {
          xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		"Chose %s method for unaligned data transfers %s video RAM\n",
		   MCFunctions[secondbest].mName,
		   from ? "from" : "to");
          *UMemCpy = MCFunctions[secondbest].mFunc;
       }
       return MCFunctions[best].mFunc;
    }

    return XGI_libc_memcpy;
}
#endif /* canBenchmark */

/**********************************************************************/
/*      Generic routines if /proc filesystem is available (Linux)     */
/**********************************************************************/

#ifdef XGI_haveProc
/* Linux: Read file (/proc/cpuinfo) into buffer */
static int XGI_ReadProc(char *buf, char *filename)
{
    FILE *cpuInfoFile;
    int count;

    if((cpuInfoFile = fopen(filename, "r")) == NULL) {
       return 0;
    }

    count = fread(buf, 1, CPUBUFFERSIZE, cpuInfoFile);
    if(ferror(cpuInfoFile)) {
       fclose(cpuInfoFile);
       return 0;
    }

    fclose(cpuInfoFile);

    if(count >= CPUBUFFERSIZE - 2) {
       return 0;
    }

    buf[count] = 0;

    return count;
}

/* Linux: Extract CPU speed from /proc/cpuinfo */
static char *XGI_GetCPUFreq(ScrnInfoPtr pScrn, char *buf, double *cpuFreq)
{
    char *frqBuf, *endBuf;

    (*cpuFreq) = 0.0;

    if((frqBuf = strstr(buf,"cpu MHz\t\t:"))) {
       frqBuf += 11;
       (*cpuFreq) = strtod(frqBuf, &endBuf);
       if(endBuf == frqBuf) frqBuf = NULL;
       if((*cpuFreq) < 10.0) frqBuf = NULL; /* sanity check */
       if(frqBuf) {
          xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "CPU frequency %.2fMhz\n", (*cpuFreq));
       }
    }

    return frqBuf;
}
#endif /* haveProc */

/**********************************************************************/
/*                      Arch-specific routines                        */
/**********************************************************************/

#ifdef XGI_checkosforsse   /* Common i386, AMD64  */

#ifdef XGICHECKOSSSE

#ifndef XFree86LOADER
#include <setjmp.h>
#endif

static jmp_buf sigill_return;

static void sigill_handler(void)
{
    longjmp(sigill_return, 1);
}
#endif

#if 0 /* Jong 01/15/2009; ignore at the moment */
static Bool CheckOSforSSE(ScrnInfoPtr pScrn)
{
#ifdef XGICHECKOSSSE  /* Check OS for SSE possible: */
    int signo = -1;

#ifdef XGIDGBMC
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Checking OS SSE support\n");
#endif

    xf86InterceptSigIll(&sigill_handler);

    if(setjmp(sigill_return)) {
       signo = 4;
    } else {
       __asm__ __volatile__ (" xorps %xmm0, %xmm0\n");
       /* __asm__ __volatile__ (" .byte 0xff\n"); */  /* For test */
    }

    xf86InterceptSigIll(NULL);

#ifdef XGIDGBMC
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "OS SSE support signal %d\n", signo);
#endif

    if(signo != -1) {
       xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
		"OS does not support SSE instructions\n");
    }

    return (signo >= 0) ? FALSE : TRUE;

#else  /* no check for SSE possible: */

    XGIPtr pXGI = XGIPTR(pScrn);

    xf86DrvMsg(pScrn->scrnIndex, pXGI->XvSSEMemcpy ? X_WARNING : X_INFO,
	"Checking OS for SSE support is not supported in this version of " XGIMYSERVERNAME "\n");

    if(pXGI->XvSSEMemcpy) {
       xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
	"If you get a signal 4 here, set the option \"UseSSE\" to \"off\".\n");
       return TRUE;
    } else {
       xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	"If your OS supports SSE, set the option \"UseSSE\" to \"on\".\n");
       return FALSE;
    }
#endif
}

#endif /* XGI_checkosforsse */
#endif

#ifdef __i386__   /* i386 specific *************************************/
#define ULong	unsigned long

PREFETCH_FUNC(XGI_sse,SSE,SSE,,FENCE,small_memcpy_i386)
PREFETCH_FUNC(XGI_mmxext,MMXEXT,SSE,EMMS,FENCEMMS,small_memcpy_i386)
PREFETCH_FUNC(XGI_now,MMX,NOW,FEMMS,FEMMS,small_memcpy_i386)
NOPREFETCH_FUNC(XGI_mmx,MMX,EMMS,EMMS,small_memcpy_i386)

#if 0 /* Jong 01/15/2009; ignore at the moment */
static XGIMCFuncData MCFunctions_i386[] = {
    {XGI_libc_memcpy,   "libc",      XGI_CPUFL_LIBC,  4,  4, FALSE},
    {XGI_builtin_memcpy,"built-in-1",XGI_CPUFL_BI,    5,  5, FALSE},
    {XGI_builtin_memcp2,"built-in-2",XGI_CPUFL_BI2,   6,  6, FALSE},
    {XGI_mmx_memcpy,    "MMX",       XGI_CPUFL_MMX,   3,  3, FALSE},
    {XGI_sse_memcpy,    "SSE",       XGI_CPUFL_SSE,   1,  0, TRUE},
    {XGI_now_memcpy,    "3DNow!",    XGI_CPUFL_3DNOW, 2,  2, FALSE},
    {XGI_mmxext_memcpy, "MMX2",      XGI_CPUFL_MMX2,  0,  1, FALSE},
    {NULL,              "",          0,              10, 10, FALSE}
};
#endif

#define Def_FL  (XGI_CPUFL_LIBC | XGI_CPUFL_BI | XGI_CPUFL_BI2)  /* Default methods */

#define cpuid(op, eax, ebx, ecx, edx) 		\
    __asm__ __volatile__ (			\
		" pushl %%ebx\n"		\
		" cpuid\n"			\
		" movl %%ebx, %1\n"		\
		" popl %%ebx\n"			\
		: "=a" (eax), "=r" (ebx), 	\
		  "=c" (ecx), "=d" (edx)	\
		: "a" (op)			\
		: "cc")

#if 0 /* Jong 01/15/2009; ignore at the moment */
static Bool cpuIDSupported(ScrnInfoPtr pScrn)
{
    int eax, ebx, ecx, edx;

    /* Check for cpuid instruction */
    __asm__ __volatile__ (
		" pushf\n"
		" popl %0\n"
		" movl %0, %1\n"
		" xorl $0x200000, %0\n"
		" push %0\n"
		" popf\n"
		" pushf\n"
		" popl %0\n"
		: "=a" (eax), "=c" (ecx)
		:
		: "cc");

    if(eax == ecx) {
       xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "CPU does not support CPUID instruction\n");
       return FALSE;
    }

    /* Check for cpuid level */
    cpuid(0x00000000, eax, ebx, ecx, edx);
    if(!eax) {
       return FALSE;
    }

    /* Check for RDTSC */
    cpuid(0x00000001, eax, ebx, ecx, edx);

    if(!(edx & 0x10)) {
       xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "CPU does not support RDTSC instruction\n");
       return FALSE;
    }

    return TRUE;
}

static unsigned int XGI_GetCpuFeatures(ScrnInfoPtr pScrn)
{
    unsigned int flags = 0, eax, ebx, ecx, edx;
    Bool IsAMD;

    /* Check if cpuid and rdtsc instructions are supported */
    if(!cpuIDSupported(pScrn)) {
       return 0;
    }

    cpuid(0x00000000, eax, ebx, ecx, edx);

    IsAMD = (ebx == 0x68747541) && (edx == 0x69746e65) && (ecx == 0x444d4163);

    cpuid(0x00000001, eax, ebx, ecx, edx);
    /* MMX */
    if(edx & 0x00800000) flags |= XGI_CPUFL_MMX;
    /* SSE, MMXEXT */
    if(edx & 0x02000000) flags |= (XGI_CPUFL_SSE | XGI_CPUFL_MMX2);
    /* SSE2 - don't need this one directly, set SSE instead */
    if(edx & 0x04000000) flags |= (XGI_CPUFL_SSE | XGI_CPUFL_SSE2);

    cpuid(0x80000000, eax, ebx, ecx, edx);
    if(eax >= 0x80000001) {
       cpuid(0x80000001, eax, ebx, ecx, edx);
       /* 3DNow! */
       if(edx & 0x80000000) flags |= XGI_CPUFL_3DNOW;
       /* AMD MMXEXT */
       if(IsAMD && (edx & 0x00400000)) flags |= XGI_CPUFL_MMX2;
    }

    return flags;
}
#endif

#elif defined(__AMD64__) || defined(__amd64__) || defined(__x86_64__) /* AMD64 specific ***** */

PREFETCH_FUNC(XGI_sse,SSE64,SSE,,FENCE,small_memcpy_amd64)

#if 0 /* Jong 01/15/2009; ignore at the moment */

static XGIMCFuncData MCFunctions_AMD64[] = {
    {XGI_libc_memcpy,   "libc",      XGI_CPUFL_LIBC, 2,  2, FALSE},
    {XGI_builtin_memcpy,"built-in-1",XGI_CPUFL_BI,   1,  1, FALSE},
    {XGI_builtin_memcp2,"built-in-2",XGI_CPUFL_BI2,  3,  3, FALSE},
    {XGI_sse_memcpy,    "SSE",       XGI_CPUFL_SSE,  0,  0, TRUE},
    {NULL,              "",          0,             10, 10, FALSE}
};

#define Def_FL  (XGI_CPUFL_LIBC | XGI_CPUFL_BI | XGI_CPUFL_BI2)

static unsigned int XGI_GetCpuFeatures(ScrnInfoPtr pScrn)
{
    return((unsigned int)(XGI_CPUFL_SSE|XGI_CPUFL_SSE2));
}
#endif

#else  /* Specific for other archs ******************************** */

/* Fill in here */
#if 0 /* Jong 01/15/2009; ignore at the moment */

#define Def_FL  (XGI_CPUFL_LIBC)

static unsigned int XGI_GetCpuFeatures(ScrnInfoPtr pScrn)
{
    return((unsigned int)(0));
}

#endif

#endif

/**********************************************************************/
/*     Benchmark the video copy routines and choose the fastest       */
/**********************************************************************/

#if 0 /* Jong 01/15/2009; ignore at the moment */
#ifdef XGI_canBenchmark
static vidCopyFunc
XGIVidCopyInitGen(ScreenPtr pScreen, XGIMCFuncData *MCFunctions, vidCopyFunc *UMemCpy, Bool from)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    XGIPtr pXGI = XGIPTR(pScrn);
    void *fbhandle = NULL;
    char  *frqBuf = NULL;
    UChar *buf1, *buf2, *buf3;
    double cpuFreq = 0.0;
    unsigned int myCPUflags = pXGI->CPUFlags | Def_FL;
    int best, secondbest;
#ifdef XGI_haveProc
    char buf[CPUBUFFERSIZE];
#endif

	PDEBUG(ErrorF("---XGIVidCopyInitGen()...begin\n"));
    *UMemCpy = XGI_libc_memcpy;

    /* Bail out if user disabled benchmarking */
    if(!pXGI->BenchMemCpy) {
	   PDEBUG(ErrorF("---return XGI_libc_memcpy() as vidCopyFunc...\n"));
       return XGI_libc_memcpy;
    }

#ifdef XGI_haveProc
    /* Read /proc/cpuinfo into buf */
    if(XGI_ReadProc(buf, "/proc/cpuinfo")) {

       /* Extract CPU frequency */
       frqBuf = XGI_GetCPUFreq(pScrn, buf, &cpuFreq);

    }
#endif

    /* Allocate buffers; buf1:LFB; buf2,buf3:system memory */
    if(!(fbhandle = XGI_AllocBuffers(pScrn, &buf1, &buf2, &buf3))) {
       xf86DrvMsg(pScrn->scrnIndex, X_INFO,
       		"Failed to allocate video RAM for video data transfer benchmark\n");
       return XGI_GetBestByGrade(pScrn, MCFunctions, myCPUflags, UMemCpy, from);
    }

    /* Perform Benchmark */
    PDEBUG(ErrorF("---Perform XGI_BenchmarkMemcpy()...\n"));
    best = XGI_BenchmarkMemcpy(pScrn, MCFunctions, myCPUflags, buf1,
    				(UChar *)(((unsigned long)buf2 + 15) & ~15),
				(UChar *)(((unsigned long)buf3 + 15) & ~15),
				frqBuf, cpuFreq, UMemCpy, &secondbest, from);

    /* Free buffers */
    XGIFreeFBMemory(pScrn, &fbhandle);
    free(buf2);
    free(buf3);

    xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
	       "Using %s method for aligned data transfers %s video RAM\n",
	       MCFunctions[best].mName,
	       from ? "from" : "to");

    xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
	       "Using %s method for unaligned data transfers %s video RAM\n",
	       MCFunctions[secondbest].mName,
	       from ? "from" : "to");

    return MCFunctions[best].mFunc;
}
#endif /* canBenchmark */
#endif

/**********************************************************************/
/* 		       main(): Get CPU capabilities		      */
/* 			    (called externally)			      */
/**********************************************************************/
#if 0 /* Jong 01/15/2009; ignore at the moment */

unsigned int
XGIGetCPUFlags(ScrnInfoPtr pScrn)
{
    unsigned int myCPUflags = XGI_GetCpuFeatures(pScrn);

#ifdef XGI_checkosforsse
    if(myCPUflags & (XGI_CPUFL_SSE | XGI_CPUFL_SSE2)) {

       /* Check if OS supports usage of SSE instructions */
       if(!(CheckOSforSSE(pScrn))) {
          myCPUflags &= ~(XGI_CPUFL_SSE | XGI_CPUFL_SSE2);
       }

    }
#endif

    return myCPUflags;
}

#endif

/**********************************************************************/
/*                       main(): XGIVidCopyInit()                     */
/*			    (called externally)			      */
/*		(XGIGetCPUFlags must be called before this one)       */
/**********************************************************************/

#if 0 /* Jong 01/15/2009; ignore at the moment */
vidCopyFunc XGIVidCopyInit(ScreenPtr pScreen, vidCopyFunc *UMemCpy, Bool from)
{
/* Jong 01/08/2009; test for performance */
#if defined(__i386__) && defined(XGI_canBenchmark)
	PDEBUG(ErrorF("XGIVidCopyInit()-i386...\n"));
    return(XGIVidCopyInitGen(pScreen, MCFunctions_i386, UMemCpy, from));
#elif (defined(__AMD64__) || defined(__amd64__) || defined(__x86_64__)) && defined(XGI_canBenchmark)
	PDEBUG(ErrorF("XGIVidCopyInit()-AMD64-x86_64...\n"));
    return(XGIVidCopyInitGen(pScreen, MCFunctions_AMD64, UMemCpy, from));
#else /* Other cases: Use libc memcpy() */
    *UMemCpy = XGI_libc_memcpy;
	PDEBUG(ErrorF("XGIVidCopyInit()-generic...\n"));
    return XGI_libc_memcpy;
#endif 
}
#endif

#if 0 /* Jong 01/15/2009; ignore at the moment */
vidCopyFunc XGIVidCopyGetDefault(void)
{
    return XGI_libc_memcpy;
}
#endif

#endif /* GNU C */