aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa/src/mesa/x86/rtasm/x86sse.c
blob: 0c9ffe25fa29556c9251a6c0fe71736ab7d2ad21 (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
#if defined(USE_X86_ASM)

#include "imports.h"
#include "x86sse.h"

#define DISASSEM 0
#define X86_TWOB 0x0f

/* Emit bytes to the instruction stream:
 */
static void emit_1b( struct x86_function *p, GLbyte b0 )
{
   *(GLbyte *)(p->csr++) = b0;
}

static void emit_1i( struct x86_function *p, GLint i0 )
{
   *(GLint *)(p->csr) = i0;
   p->csr += 4;
}

static void disassem( struct x86_function *p, const char *fn )
{
#if DISASSEM && 0
   if (fn && fn != p->fn) {
      _mesa_printf("0x%x: %s\n", p->csr, fn);
      p->fn = fn;
   }
#endif
}

static void emit_1ub_fn( struct x86_function *p, GLubyte b0, const char *fn )
{
   disassem(p, fn);
   *(p->csr++) = b0;
}

static void emit_2ub_fn( struct x86_function *p, GLubyte b0, GLubyte b1, const char *fn )
{
   disassem(p, fn);
   *(p->csr++) = b0;
   *(p->csr++) = b1;
}

static void emit_3ub_fn( struct x86_function *p, GLubyte b0, GLubyte b1, GLubyte b2, const char *fn )
{
   disassem(p, fn);
   *(p->csr++) = b0;
   *(p->csr++) = b1;
   *(p->csr++) = b2;
}

#define emit_1ub(p, b0)         emit_1ub_fn(p, b0, __FUNCTION__)
#define emit_2ub(p, b0, b1)     emit_2ub_fn(p, b0, b1, __FUNCTION__)
#define emit_3ub(p, b0, b1, b2) emit_3ub_fn(p, b0, b1, b2, __FUNCTION__)



/* Build a modRM byte + possible displacement.  No treatment of SIB
 * indexing.  BZZT - no way to encode an absolute address.
 */
static void emit_modrm( struct x86_function *p, 
			struct x86_reg reg, 
			struct x86_reg regmem )
{
   GLubyte val = 0;
   
   assert(reg.mod == mod_REG);
   
   val |= regmem.mod << 6;     	/* mod field */
   val |= reg.idx << 3;		/* reg field */
   val |= regmem.idx;		/* r/m field */
   
   emit_1ub_fn(p, val, 0);

   /* Oh-oh we've stumbled into the SIB thing.
    */
   if (regmem.file == file_REG32 &&
       regmem.idx == reg_SP) {
      emit_1ub_fn(p, 0x24, 0);		/* simplistic! */
   }

   switch (regmem.mod) {
   case mod_REG:
   case mod_INDIRECT:
      break;
   case mod_DISP8:
      emit_1b(p, regmem.disp);
      break;
   case mod_DISP32:
      emit_1i(p, regmem.disp);
      break;
   default:
      assert(0);
      break;
   }
}


static void emit_modrm_noreg( struct x86_function *p,
			      GLuint op,
			      struct x86_reg regmem )
{
   struct x86_reg dummy = x86_make_reg(file_REG32, op);
   emit_modrm(p, dummy, regmem);
}

/* Many x86 instructions have two opcodes to cope with the situations
 * where the destination is a register or memory reference
 * respectively.  This function selects the correct opcode based on
 * the arguments presented.
 */
static void emit_op_modrm( struct x86_function *p,
			   GLubyte op_dst_is_reg, 
			   GLubyte op_dst_is_mem,
			   struct x86_reg dst,
			   struct x86_reg src )
{  
   switch (dst.mod) {
   case mod_REG:
      emit_1ub_fn(p, op_dst_is_reg, 0);
      emit_modrm(p, dst, src);
      break;
   case mod_INDIRECT:
   case mod_DISP32:
   case mod_DISP8:
      assert(src.mod == mod_REG);
      emit_1ub_fn(p, op_dst_is_mem, 0);
      emit_modrm(p, src, dst);
      break;
   default:
      assert(0);
      break;
   }
}







/* Create and manipulate registers and regmem values:
 */
struct x86_reg x86_make_reg( GLuint file,
			     GLuint idx )
{
   struct x86_reg reg;

   reg.file = file;
   reg.idx = idx;
   reg.mod = mod_REG;
   reg.disp = 0;

   return reg;
}

struct x86_reg x86_make_disp( struct x86_reg reg,
			      GLint disp )
{
   assert(reg.file == file_REG32);

   if (reg.mod == mod_REG)
      reg.disp = disp;
   else
      reg.disp += disp;

   if (reg.disp == 0)
      reg.mod = mod_INDIRECT;
   else if (reg.disp <= 127 && reg.disp >= -128)
      reg.mod = mod_DISP8;
   else
      reg.mod = mod_DISP32;

   return reg;
}

struct x86_reg x86_deref( struct x86_reg reg )
{
   return x86_make_disp(reg, 0);
}

struct x86_reg x86_get_base_reg( struct x86_reg reg )
{
   return x86_make_reg( reg.file, reg.idx );
}

GLubyte *x86_get_label( struct x86_function *p )
{
   return p->csr;
}



/***********************************************************************
 * x86 instructions
 */


void x86_jcc( struct x86_function *p,
	      GLuint cc,
	      GLubyte *label )
{
   GLint offset = label - (x86_get_label(p) + 2);
   
   if (offset <= 127 && offset >= -128) {
      emit_1ub(p, 0x70 + cc);
      emit_1b(p, (GLbyte) offset);
   }
   else {
      offset = label - (x86_get_label(p) + 6);
      emit_2ub(p, 0x0f, 0x80 + cc);
      emit_1i(p, offset);
   }
}

/* Always use a 32bit offset for forward jumps:
 */
GLubyte *x86_jcc_forward( struct x86_function *p,
			  GLuint cc )
{
   emit_2ub(p, 0x0f, 0x80 + cc);
   emit_1i(p, 0);
   return x86_get_label(p);
}

/* Fixup offset from forward jump:
 */
void x86_fixup_fwd_jump( struct x86_function *p,
			 GLubyte *fixup )
{
   *(int *)(fixup - 4) = x86_get_label(p) - fixup;
}

void x86_push( struct x86_function *p,
	       struct x86_reg reg )
{
   assert(reg.mod == mod_REG);
   emit_1ub(p, 0x50 + reg.idx);
   p->stack_offset += 4;
}

void x86_pop( struct x86_function *p,
	      struct x86_reg reg )
{
   assert(reg.mod == mod_REG);
   emit_1ub(p, 0x58 + reg.idx);
   p->stack_offset -= 4;
}

void x86_inc( struct x86_function *p,
	      struct x86_reg reg )
{
   assert(reg.mod == mod_REG);
   emit_1ub(p, 0x40 + reg.idx);
}

void x86_dec( struct x86_function *p,
	      struct x86_reg reg )
{
   assert(reg.mod == mod_REG);
   emit_1ub(p, 0x48 + reg.idx);
}

void x86_ret( struct x86_function *p )
{
   emit_1ub(p, 0xc3);
}

void x86_sahf( struct x86_function *p )
{
   emit_1ub(p, 0x9e);
}

void x86_mov( struct x86_function *p,
	      struct x86_reg dst,
	      struct x86_reg src )
{
   emit_op_modrm( p, 0x8b, 0x89, dst, src );
}

void x86_xor( struct x86_function *p,
	      struct x86_reg dst,
	      struct x86_reg src )
{
   emit_op_modrm( p, 0x33, 0x31, dst, src );
}

void x86_cmp( struct x86_function *p,
	      struct x86_reg dst,
	      struct x86_reg src )
{
   emit_op_modrm( p, 0x3b, 0x39, dst, src );
}

void x86_lea( struct x86_function *p,
	      struct x86_reg dst,
	      struct x86_reg src )
{
   emit_1ub(p, 0x8d);
   emit_modrm( p, dst, src );
}

void x86_test( struct x86_function *p,
	       struct x86_reg dst,
	       struct x86_reg src )
{
   emit_1ub(p, 0x85);
   emit_modrm( p, dst, src );
}



/***********************************************************************
 * SSE instructions
 */


void sse_movss( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, 0xF3, X86_TWOB);
   emit_op_modrm( p, 0x10, 0x11, dst, src );
}

void sse_movaps( struct x86_function *p,
		 struct x86_reg dst,
		 struct x86_reg src )
{
   emit_1ub(p, X86_TWOB);
   emit_op_modrm( p, 0x28, 0x29, dst, src );
}

void sse_movups( struct x86_function *p,
		 struct x86_reg dst,
		 struct x86_reg src )
{
   emit_1ub(p, X86_TWOB);
   emit_op_modrm( p, 0x10, 0x11, dst, src );
}

void sse_movhps( struct x86_function *p,
		 struct x86_reg dst,
		 struct x86_reg src )
{
   assert(dst.mod != mod_REG || src.mod != mod_REG);
   emit_1ub(p, X86_TWOB);
   emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
}

void sse_movlps( struct x86_function *p,
		 struct x86_reg dst,
		 struct x86_reg src )
{
   assert(dst.mod != mod_REG || src.mod != mod_REG);
   emit_1ub(p, X86_TWOB);
   emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
}

void sse_maxps( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, X86_TWOB, 0x5F);
   emit_modrm( p, dst, src );
}

void sse_divss( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
   emit_modrm( p, dst, src );
}

void sse_minps( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, X86_TWOB, 0x5D);
   emit_modrm( p, dst, src );
}

void sse_subps( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, X86_TWOB, 0x5C);
   emit_modrm( p, dst, src );
}

void sse_mulps( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, X86_TWOB, 0x59);
   emit_modrm( p, dst, src );
}

void sse_addps( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, X86_TWOB, 0x58);
   emit_modrm( p, dst, src );
}

void sse_addss( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_3ub(p, 0xF3, X86_TWOB, 0x58);
   emit_modrm( p, dst, src );
}

void sse_andps( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, X86_TWOB, 0x54);
   emit_modrm( p, dst, src );
}


void sse_rsqrtss( struct x86_function *p,
		  struct x86_reg dst,
		  struct x86_reg src )
{
   emit_3ub(p, 0xF3, X86_TWOB, 0x52);
   emit_modrm( p, dst, src );

}

void sse_movhlps( struct x86_function *p,
		  struct x86_reg dst,
		  struct x86_reg src )
{
   assert(dst.mod == mod_REG && src.mod == mod_REG);
   emit_2ub(p, X86_TWOB, 0x12);
   emit_modrm( p, dst, src );
}

void sse_movlhps( struct x86_function *p,
		  struct x86_reg dst,
		  struct x86_reg src )
{
   assert(dst.mod == mod_REG && src.mod == mod_REG);
   emit_2ub(p, X86_TWOB, 0x16);
   emit_modrm( p, dst, src );
}


void sse_cvtps2pi( struct x86_function *p,
		   struct x86_reg dst,
		   struct x86_reg src )
{
   assert(dst.file == file_MMX && 
	  (src.file == file_XMM || src.mod != mod_REG));

   p->need_emms = 1;

   emit_2ub(p, X86_TWOB, 0x2d);
   emit_modrm( p, dst, src );
}


/* Shufps can also be used to implement a reduced swizzle when dest ==
 * arg0.
 */
void sse_shufps( struct x86_function *p,
		 struct x86_reg dest,
		 struct x86_reg arg0,
		 GLubyte shuf) 
{
   emit_2ub(p, X86_TWOB, 0xC6);
   emit_modrm(p, dest, arg0);
   emit_1ub(p, shuf); 
}

void sse_cmpps( struct x86_function *p,
		struct x86_reg dest,
		struct x86_reg arg0,
		GLubyte cc) 
{
   emit_2ub(p, X86_TWOB, 0xC2);
   emit_modrm(p, dest, arg0);
   emit_1ub(p, cc); 
}

/***********************************************************************
 * SSE2 instructions
 */

/**
 * Perform a reduced swizzle:
 */
void sse2_pshufd( struct x86_function *p,
		  struct x86_reg dest,
		  struct x86_reg arg0,
		  GLubyte shuf) 
{
   emit_3ub(p, 0x66, X86_TWOB, 0x70);
   emit_modrm(p, dest, arg0);
   emit_1ub(p, shuf); 
}

void sse2_cvtps2dq( struct x86_function *p,
		    struct x86_reg dst,
		    struct x86_reg src )
{
   emit_3ub(p, 0x66, X86_TWOB, 0x5B);
   emit_modrm( p, dst, src );
}

void sse2_packssdw( struct x86_function *p,
		    struct x86_reg dst,
		    struct x86_reg src )
{
   emit_3ub(p, 0x66, X86_TWOB, 0x6B);
   emit_modrm( p, dst, src );
}

void sse2_packsswb( struct x86_function *p,
		    struct x86_reg dst,
		    struct x86_reg src )
{
   emit_3ub(p, 0x66, X86_TWOB, 0x63);
   emit_modrm( p, dst, src );
}

void sse2_packuswb( struct x86_function *p,
		    struct x86_reg dst,
		    struct x86_reg src )
{
   emit_3ub(p, 0x66, X86_TWOB, 0x67);
   emit_modrm( p, dst, src );
}

void sse2_rcpss( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_3ub(p, 0xF3, X86_TWOB, 0x53);
   emit_modrm( p, dst, src );
}

void sse2_movd( struct x86_function *p,
		struct x86_reg dst,
		struct x86_reg src )
{
   emit_2ub(p, 0x66, X86_TWOB);
   emit_op_modrm( p, 0x6e, 0x7e, dst, src );
}




/***********************************************************************
 * x87 instructions
 */
void x87_fist( struct x86_function *p, struct x86_reg dst )
{
   emit_1ub(p, 0xdb);
   emit_modrm_noreg(p, 2, dst);
}

void x87_fistp( struct x86_function *p, struct x86_reg dst )
{
   emit_1ub(p, 0xdb);
   emit_modrm_noreg(p, 3, dst);
}

void x87_fldz( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xee);
}


void x87_fldcw( struct x86_function *p, struct x86_reg arg )
{
   assert(arg.file == file_REG32);
   assert(arg.mod != mod_REG);
   emit_1ub(p, 0xd9);
   emit_modrm_noreg(p, 5, arg);
}

void x87_fld1( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xe8);
}

void x87_fldl2e( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xea);
}

void x87_fldln2( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xed);
}

void x87_fwait( struct x86_function *p )
{
   emit_1ub(p, 0x9b);
}

void x87_fnclex( struct x86_function *p )
{
   emit_2ub(p, 0xdb, 0xe2);
}

void x87_fclex( struct x86_function *p )
{
   x87_fwait(p);
   x87_fnclex(p);
}


static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
			  GLubyte dst0ub0,
			  GLubyte dst0ub1,
			  GLubyte arg0ub0,
			  GLubyte arg0ub1,
			  GLubyte argmem_noreg)
{
   assert(dst.file == file_x87);

   if (arg.file == file_x87) {
      if (dst.idx == 0) 
	 emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
      else if (arg.idx == 0) 
	 emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
      else
	 assert(0);
   }
   else if (dst.idx == 0) {
      assert(arg.file = file_REG32);
      emit_1ub(p, 0xd8);
      emit_modrm_noreg(p, argmem_noreg, arg);
   }
   else
      assert(0);
}

void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
{
   x87_arith_op(p, dst, arg, 
		0xd8, 0xc8,
		0xdc, 0xc8,
		4);
}

void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
{
   x87_arith_op(p, dst, arg, 
		0xd8, 0xe0,
		0xdc, 0xe8,
		4);
}

void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
{
   x87_arith_op(p, dst, arg, 
		0xd8, 0xe8,
		0xdc, 0xe0,
		5);
}

void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
{
   x87_arith_op(p, dst, arg, 
		0xd8, 0xc0,
		0xdc, 0xc0,
		0);
}

void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
{
   x87_arith_op(p, dst, arg, 
		0xd8, 0xf0,
		0xdc, 0xf8,
		6);
}

void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
{
   x87_arith_op(p, dst, arg, 
		0xd8, 0xf8,
		0xdc, 0xf0,
		7);
}

void x87_fmulp( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_x87);
   assert(dst.idx >= 1);
   emit_2ub(p, 0xde, 0xc8+dst.idx);
}

void x87_fsubp( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_x87);
   assert(dst.idx >= 1);
   emit_2ub(p, 0xde, 0xe8+dst.idx);
}

void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_x87);
   assert(dst.idx >= 1);
   emit_2ub(p, 0xde, 0xe0+dst.idx);
}

void x87_faddp( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_x87);
   assert(dst.idx >= 1);
   emit_2ub(p, 0xde, 0xc0+dst.idx);
}

void x87_fdivp( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_x87);
   assert(dst.idx >= 1);
   emit_2ub(p, 0xde, 0xf8+dst.idx);
}

void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_x87);
   assert(dst.idx >= 1);
   emit_2ub(p, 0xde, 0xf0+dst.idx);
}

void x87_fucom( struct x86_function *p, struct x86_reg arg )
{
   assert(arg.file == file_x87);
   emit_2ub(p, 0xdd, 0xe0+arg.idx);
}

void x87_fucomp( struct x86_function *p, struct x86_reg arg )
{
   assert(arg.file == file_x87);
   emit_2ub(p, 0xdd, 0xe8+arg.idx);
}

void x87_fucompp( struct x86_function *p )
{
   emit_2ub(p, 0xda, 0xe9);
}

void x87_fxch( struct x86_function *p, struct x86_reg arg )
{
   assert(arg.file == file_x87);
   emit_2ub(p, 0xd9, 0xc8+arg.idx);
}

void x87_fabs( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xe1);
}

void x87_fchs( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xe0);
}

void x87_fcos( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xff);
}


void x87_fprndint( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xfc);
}

void x87_fscale( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xfd);
}

void x87_fsin( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xfe);
}

void x87_fsincos( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xfb);
}

void x87_fsqrt( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xfa);
}

void x87_fxtract( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xf4);
}

/* st0 = (2^st0)-1
 *
 * Restrictions: -1.0 <= st0 <= 1.0
 */
void x87_f2xm1( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xf0);
}

/* st1 = st1 * log2(st0);
 * pop_stack;
 */
void x87_fyl2x( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xf1);
}

/* st1 = st1 * log2(st0 + 1.0);
 * pop_stack;
 *
 * A fast operation, with restrictions: -.29 < st0 < .29 
 */
void x87_fyl2xp1( struct x86_function *p )
{
   emit_2ub(p, 0xd9, 0xf9);
}


void x87_fld( struct x86_function *p, struct x86_reg arg )
{
   if (arg.file == file_x87) 
      emit_2ub(p, 0xd9, 0xc0 + arg.idx);
   else {
      emit_1ub(p, 0xd9);
      emit_modrm_noreg(p, 0, arg);
   }
}

void x87_fst( struct x86_function *p, struct x86_reg dst )
{
   if (dst.file == file_x87) 
      emit_2ub(p, 0xdd, 0xd0 + dst.idx);
   else {
      emit_1ub(p, 0xd9);
      emit_modrm_noreg(p, 2, dst);
   }
}

void x87_fstp( struct x86_function *p, struct x86_reg dst )
{
   if (dst.file == file_x87) 
      emit_2ub(p, 0xdd, 0xd8 + dst.idx);
   else {
      emit_1ub(p, 0xd9);
      emit_modrm_noreg(p, 3, dst);
   }
}

void x87_fcom( struct x86_function *p, struct x86_reg dst )
{
   if (dst.file == file_x87) 
      emit_2ub(p, 0xd8, 0xd0 + dst.idx);
   else {
      emit_1ub(p, 0xd8);
      emit_modrm_noreg(p, 2, dst);
   }
}

void x87_fcomp( struct x86_function *p, struct x86_reg dst )
{
   if (dst.file == file_x87) 
      emit_2ub(p, 0xd8, 0xd8 + dst.idx);
   else {
      emit_1ub(p, 0xd8);
      emit_modrm_noreg(p, 3, dst);
   }
}


void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
{
   assert(dst.file == file_REG32);

   if (dst.idx == reg_AX &&
       dst.mod == mod_REG) 
      emit_2ub(p, 0xdf, 0xe0);
   else {
      emit_1ub(p, 0xdd);
      emit_modrm_noreg(p, 7, dst);
   }
}




/***********************************************************************
 * MMX instructions
 */

void mmx_emms( struct x86_function *p )
{
   assert(p->need_emms);
   emit_2ub(p, 0x0f, 0x77);
   p->need_emms = 0;
}

void mmx_packssdw( struct x86_function *p,
		   struct x86_reg dst,
		   struct x86_reg src )
{
   assert(dst.file == file_MMX && 
	  (src.file == file_MMX || src.mod != mod_REG));

   p->need_emms = 1;

   emit_2ub(p, X86_TWOB, 0x6b);
   emit_modrm( p, dst, src );
}

void mmx_packuswb( struct x86_function *p,
		   struct x86_reg dst,
		   struct x86_reg src )
{
   assert(dst.file == file_MMX && 
	  (src.file == file_MMX || src.mod != mod_REG));

   p->need_emms = 1;

   emit_2ub(p, X86_TWOB, 0x67);
   emit_modrm( p, dst, src );
}

void mmx_movd( struct x86_function *p,
	       struct x86_reg dst,
	       struct x86_reg src )
{
   p->need_emms = 1;
   emit_1ub(p, X86_TWOB);
   emit_op_modrm( p, 0x6e, 0x7e, dst, src );
}

void mmx_movq( struct x86_function *p,
	       struct x86_reg dst,
	       struct x86_reg src )
{
   p->need_emms = 1;
   emit_1ub(p, X86_TWOB);
   emit_op_modrm( p, 0x6f, 0x7f, dst, src );
}


/***********************************************************************
 * Helper functions
 */


/* Retreive a reference to one of the function arguments, taking into
 * account any push/pop activity:
 */
struct x86_reg x86_fn_arg( struct x86_function *p,
			   GLuint arg )
{
   return x86_make_disp(x86_make_reg(file_REG32, reg_SP), 
			p->stack_offset + arg * 4);	/* ??? */
}


void x86_init_func( struct x86_function *p )
{
   p->store = _mesa_exec_malloc(1024);
   p->csr = p->store;
}

void x86_release_func( struct x86_function *p )
{
   _mesa_exec_free(p->store);
}


void (*x86_get_func( struct x86_function *p ))(void)
{
   if (DISASSEM)
      _mesa_printf("disassemble %p %p\n", p->store, p->csr);
   return (void (*)())p->store;
}

#else

void x86sse_dummy( void )
{
}

#endif