aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.3.1/gcc/config/sh/ushmedia.h
blob: 66493c94cdfc39dc8bc58df3683d00f273f5d988 (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
/* Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc.

This file is part of GCC.

GCC 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, or (at your option)
any later version.

GCC 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 GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

/* As a special exception, if you include this header file into source
   files compiled by GCC, this header file does not by itself cause
   the resulting executable to be covered by the GNU General Public
   License.  This exception does not however invalidate any other
   reasons why the executable file might be covered by the GNU General
   Public License.  */

/* ushmedia.h: Intrinsics corresponding to SHmedia instructions that
   may be executed in both user and privileged mode.  */

#ifndef _USHMEDIA_H
#define _USHMEDIA_H

#if __SHMEDIA__
#if ! __SH4_NO_FPU
typedef float __GCC_FV __attribute__ ((vector_size (4 * sizeof (float))));
typedef float __GCC_MTRX __attribute__ ((vector_size (16 * sizeof (float))));
#endif

static __inline unsigned long long
sh_media_MABS_L (unsigned long long mm)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_absv2si2 ((v2si) mm);
}

static __inline unsigned long long
sh_media_MABS_W (unsigned long long mm)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_absv4hi2 ((v4hi) mm);
}

static __inline unsigned long long
sh_media_MADD_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_addv2si3 ((v2si) mm, (v2si) mn);
}

static __inline unsigned long long
sh_media_MADD_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_addv4hi3 ((v4hi) mm, (v4hi) mn);
}

static __inline unsigned long long
sh_media_MADDS_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_ssaddv2si3 ((v2si) mm, (v2si) mn);
}

static __inline unsigned long long
sh_media_MADDS_UB (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_usaddv8qi3 ((v8qi) mm, (v8qi) mn);
}

static __inline unsigned long long
sh_media_MADDS_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_ssaddv4hi3 ((v4hi) mm, (v4hi) mn);
}

static __inline unsigned long long
sh_media_MCMPEQ_B (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MCMPEQ_B ((v8qi) mm,
							   (v8qi) mn);
}

static __inline unsigned long long
sh_media_MCMPEQ_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MCMPEQ_L ((v2si) mm,
							   (v2si) mn);
}

static __inline unsigned long long
sh_media_MCMPEQ_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MCMPEQ_W ((v4hi) mm,
							   (v4hi) mn);
}

static __inline unsigned long long
sh_media_MCMPGT_UB (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MCMPGT_UB ((v8qi) mm,
							   (v8qi) mn);
}

static __inline unsigned long long
sh_media_MCMPGT_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MCMPGT_L ((v2si) mm,
							   (v2si) mn);
}

static __inline unsigned long long
sh_media_MCMPGT_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MCMPGT_W ((v4hi) mm,
							   (v4hi) mn);
}

#define sh_media_MCMV __builtin_sh_media_MCMV

static __inline unsigned long long
sh_media_MCNVS_LW (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));
  typedef unsigned int uv2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MCNVS_LW ((v2si) mm,
							   (uv2si) mn);
}

static __inline unsigned long long
sh_media_MCNVS_WB (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MCNVS_WB ((v4hi) mm,
							   (v4hi) mn);
}

static __inline unsigned long long
sh_media_MCNVS_WUB (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MCNVS_WUB ((v4hi) mm,
							    (v4hi) mn);
}

static __inline unsigned long long
sh_media_MEXTR1 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR1 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MEXTR2 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR2 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MEXTR3 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR3 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MEXTR4 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR4 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MEXTR5 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR5 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MEXTR6 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR6 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MEXTR7 (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MEXTR7 ((v8qi) mm,
							 (v8qi) mn);
}

static __inline unsigned long long
sh_media_MMACFX_WL (unsigned long long mm, unsigned long long mn,
		    unsigned long long mw)
{
  typedef float v2hi __attribute__ ((mode(V2HI)));
  typedef float v2si __attribute__ ((mode(V2SI)));
  typedef unsigned int uv2si __attribute__ ((mode(V2SI)));

  long mm_l = (long) mm;
  long mn_l = (long) mn;

  return ((unsigned long long)
    __builtin_sh_media_MMACFX_WL ((v2hi) mm_l, (v2hi) mn_l,
				  (uv2si) mw));
}

static __inline unsigned long long
sh_media_MMACNFX_WL (unsigned long long mm, unsigned long long mn,
		     unsigned long long mw)
{
  typedef float v2hi __attribute__ ((mode(V2HI)));
  typedef float v2si __attribute__ ((mode(V2SI)));
  typedef unsigned int uv2si __attribute__ ((mode(V2SI)));

  long mm_l = (long) mm;
  long mn_l = (long) mn;

  return ((unsigned long long)
    __builtin_sh_media_MMACNFX_WL ((v2hi) mm_l, (v2hi) mn_l,
				   (uv2si) mw));
}

static __inline unsigned long long
sh_media_MMUL_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_mulv2si3 ((v2si) mm, (v2si) mn);
}

static __inline unsigned long long
sh_media_MMUL_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_mulv4hi3 ((v4hi) mm, (v4hi) mn);
}

static __inline unsigned long long
sh_media_MMULFX_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MMULFX_L ((v2si) mm,
							   (v2si) mn);
}

static __inline unsigned long long
sh_media_MMULFX_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MMULFX_W ((v4hi) mm,
							   (v4hi) mn);
}

static __inline unsigned long long
sh_media_MMULFXRP_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MMULFXRP_W ((v4hi) mm,
							     (v4hi) mn);
}

static __inline unsigned long long
sh_media_MMULHI_WL (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MMULHI_WL ((v4hi) mm,
							    (v4hi) mn);
}

static __inline unsigned long long
sh_media_MMULLO_WL (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MMULLO_WL ((v4hi) mm,
							    (v4hi) mn);
}

static __inline unsigned long long
sh_media_MMULSUM_WQ (unsigned long long mm, unsigned long long mn,
		     unsigned long long mw)
{
  typedef unsigned int uv4hi __attribute__ ((mode(V4HI)));

  return __builtin_sh_media_MMULSUM_WQ ((uv4hi) mm, (uv4hi) mn, mw);
}

static __inline unsigned long long
sh_media_MPERM_W (unsigned long long mm, unsigned int mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MPERM_W ((v4hi) mm, mn);
}

static __inline unsigned long long
sh_media_MSAD_UBQ (unsigned long long mm, unsigned long long mn,
		   unsigned long long mw)
{
  typedef unsigned int uv8qi __attribute__ ((mode(V8QI)));

  return __builtin_sh_media_MSAD_UBQ ((uv8qi) mm, (uv8qi) mn, mw);
}

static __inline unsigned long long
sh_media_MSHALDS_L (unsigned long long mm, unsigned int mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MSHALDS_L ((v2si) mm, mn);
}

static __inline unsigned long long
sh_media_MSHALDS_W (unsigned long long mm, unsigned int mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MSHALDS_W ((v4hi) mm, mn);
}

static __inline unsigned long long
sh_media_MSHARD_L (unsigned long long mm, unsigned int mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_ashrv2si3 ((v2si) mm, mn);
}

static __inline unsigned long long
sh_media_MSHARD_W (unsigned long long mm, unsigned int mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_ashrv4hi3 ((v4hi) mm, mn);
}

#define sh_media_MSHARDS_Q __builtin_sh_media_MSHARDS_Q

static __inline unsigned long long
sh_media_MSHFHI_B (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MSHFHI_B ((v8qi) mm,
							   (v8qi) mn);
}

static __inline unsigned long long
sh_media_MSHFHI_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MSHFHI_L ((v2si) mm,
							   (v2si) mn);
}

static __inline unsigned long long
sh_media_MSHFHI_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MSHFHI_W ((v4hi) mm,
							   (v4hi) mn);
}

static __inline unsigned long long
sh_media_MSHFLO_B (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_MSHFLO_B ((v8qi) mm,
							   (v8qi) mn);
}

static __inline unsigned long long
sh_media_MSHFLO_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sh_media_MSHFLO_L ((v2si) mm,
							   (v2si) mn);
}

static __inline unsigned long long
sh_media_MSHFLO_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sh_media_MSHFLO_W ((v4hi) mm,
							   (v4hi) mn);
}

static __inline unsigned long long
sh_media_MSHLLD_L (unsigned long long mm, unsigned int mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_ashlv2si3 ((v2si) mm, mn);
}

static __inline unsigned long long
sh_media_MSHLLD_W (unsigned long long mm, unsigned int mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_ashlv4hi3 ((v4hi) mm, mn);
}

static __inline unsigned long long
sh_media_MSHLRD_L (unsigned long long mm, unsigned int mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_lshrv2si3 ((v2si) mm, mn);
}

static __inline unsigned long long
sh_media_MSHLRD_W (unsigned long long mm, unsigned int mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_lshrv4hi3 ((v4hi) mm, mn);
}

static __inline unsigned long long
sh_media_MSUB_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_subv2si3 ((v2si) mm, (v2si) mn);
}

static __inline unsigned long long
sh_media_MSUB_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_subv4hi3 ((v4hi) mm, (v4hi) mn);
}

static __inline unsigned long long
sh_media_MSUBS_L (unsigned long long mm, unsigned long long mn)
{
  typedef float v2si __attribute__ ((mode(V2SI)));

  return (unsigned long long) __builtin_sssubv2si3 ((v2si) mm, (v2si) mn);
}

static __inline unsigned long long
sh_media_MSUBS_UB (unsigned long long mm, unsigned long long mn)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_ussubv8qi3 ((v8qi) mm, (v8qi) mn);
}

static __inline unsigned long long
sh_media_MSUBS_W (unsigned long long mm, unsigned long long mn)
{
  typedef float v4hi __attribute__ ((mode(V4HI)));

  return (unsigned long long) __builtin_sssubv4hi3 ((v4hi) mm, (v4hi) mn);
}

#if ! __SH4_NOFPU__
/* Floating-point Intrinsics */

#define sh_media_FABS_D __builtin_fabs
#define sh_media_FABS_S __builtin_fabsf
#define sh_media_FCMPUN_D __builtin_isunordered
#define sh_media_FCMPUN_S __builtin_isunordered

static __inline float sh_media_FCOSA_S (float fg)
{
  union { int i; float f; } u;

  u.f = fg;
  return __builtin_sh_media_FCOSA_S (u.i);
}

static __inline float
sh_media_FGETSCR (void)
{ 
  float f;

  __asm volatile ("fgetscr %0" : "=f" (f));
  return f;
}

static __inline float
sh_media_FIPR_S (const void *fvg, const void *fvh)
{
  typedef float v4sf __attribute__ ((mode(V4SF)));
  v4sf vg = *(v4sf*) fvg;
  v4sf vh = *(v4sf*) fvh;

  return __builtin_sh_media_FIPR_S (vg, vh);
}

#if 0
/* This gives different results for -O0  */
static __inline float
sh_media_FMAC_S (float fg, float fh, float fq)
{
  return fg * fh + fq;
}
#else

#define sh_media_FMAC_S __builtin_sh_media_FMAC_S
#endif

static __inline long long
sh_media_FMOV_DQ (double dg)
{
  union { long long l; double d; } u;

  u.d = dg;
  return u.l;
}

static __inline float
sh_media_FMOV_LS (int mm)
{
  union { int i; float f; } u;

  u.i = mm;
  return u.f;
}

static __inline double
sh_media_FMOV_QD (long long mm)
{
  union { long long l; double d; } u;

  u.l = mm;
  return u.d;
}

static __inline int
sh_media_FMOV_SL (float fg)
{
  union { int i; float f; } u;

  u.f = fg;
  return u.i;
}

static __inline void
sh_media_FPUTSCR (float fg)
{ 
  __asm volatile ("fputscr %0" : : "f" (fg));
}

static __inline float sh_media_FSINA_S (float fg)
{
  union { int i; float f; } u;

  u.f = fg;
  return __builtin_sh_media_FSINA_S (u.i);
}

/* Can't use __builtin_sqrt / __builtin_sqrtf because they still implement
   error handling unless -ffast-math is used.  */
#define sh_media_FSQRT_D __builtin_sh_media_FSQRT_D
#define sh_media_FSQRT_S __builtin_sh_media_FSQRT_S
#define sh_media_FSRRA_S __builtin_sh_media_FSRRA_S

static __inline void
sh_media_FTRV_S (const void *mtrxg, const void *fvh, void *fvf)
{
  typedef float v16sf __attribute__ ((mode(V16SF)));
  typedef float v4sf __attribute__ ((mode(V4SF)));
  v16sf mtrx = *(v16sf*) mtrxg;
  v4sf vh = *(v4sf*) fvh;

  *(v4sf*) fvf = __builtin_sh_media_FTRV_S (mtrx, vh);
}
#endif /* ! __SH4_NOFPU__ */

/* Not implemented here: Control and Configuration intrinsics.  */
/* Misaligned Access Support intrinsics */

static __inline unsigned long long
sh_media_LDHI_L (void *p, int s)
{
  return __builtin_sh_media_LDHI_L ((char *)p + s);
}

static __inline unsigned long long
sh_media_LDHI_Q (void *p, int s)
{
  return __builtin_sh_media_LDHI_Q ((char *)p + s);
}

static __inline unsigned long long
sh_media_LDLO_L (void *p, int s)
{
  return __builtin_sh_media_LDLO_L ((char *)p + s);
}

static __inline unsigned long long
sh_media_LDLO_Q (void *p, int s)
{
  return __builtin_sh_media_LDLO_Q ((char *)p + s);
}

static __inline void
sh_media_STHI_L (void *p, int s, unsigned int mw)
{
  __builtin_sh_media_STHI_L ((char*)p + s, mw);
}

static __inline void
sh_media_STHI_Q (void *p, int s, unsigned long long mw)
{
  __builtin_sh_media_STHI_Q ((char*)p + s, mw);
}

static __inline void
sh_media_STLO_L (void *p, int s, unsigned int mw)
{
  __builtin_sh_media_STLO_L ((char*)p + s, mw);
}

static __inline void
sh_media_STLO_Q (void *p, int s, unsigned long long mw)
{
  __builtin_sh_media_STLO_Q ((char*)p + s, mw);
}

/* Miscellaneous intrinsics */

#define sh_media_NSB __builtin_sh_media_NSB

static __inline unsigned long long
sh_media_BYTEREV (unsigned long long mm)
{
  typedef float v8qi __attribute__ ((mode(V8QI)));

  return (unsigned long long) __builtin_sh_media_BYTEREV ((v8qi) mm);
}

__inline__ static unsigned long long
sh_media_CMVEQ (unsigned long long mm, unsigned long long mn, unsigned long long mw) __attribute__ ((always_inline));

__inline__ static unsigned long long
sh_media_CMVEQ (unsigned long long mm, unsigned long long mn, unsigned long long mw)
{
  return mm == 0 ? mn : mw;
}

__inline__ static unsigned long long
sh_media_CMVNE (unsigned long long mm, unsigned long long mn, unsigned long long mw) __attribute__ ((always_inline));

__inline__ static unsigned long long
sh_media_CMVNE (unsigned long long mm, unsigned long long mn, unsigned long long mw)
{
  return mm != 0 ? mn : mw;
}

static __inline long long
sh_media_ADDZ_L (unsigned int mm, unsigned int mn)
{
  return mm + mn;
}

/* NOP and Synchronization intrinsics not implemented here.  */

static __inline__ void sh_media_PREFO(void *mm, int s)
{
  __builtin_sh_media_PREFO (mm + s, 0, 0);
}

/* Event Handling intrinsics not implemented here.  */

/* Old asm stuff */

static __inline__
void
sh_media_NOP (void)
{
  __asm__ ("nop" : :);
}

__inline__ static
unsigned long long
sh_media_SWAP_Q (void *mm, long long mn, unsigned long long mw)
{
  unsigned long long res;
  unsigned long long *addr = (unsigned long long *)((char *)mm + mn);
  __asm__ ("swap.q	%m1, %0" : "=r" (res), "+o" (*addr) : "0" (mw));
  return res;
}

__inline__ static
void     
sh_media_SYNCI (void)
{
  __asm__ __volatile__ ("synci");
}

__inline__ static
void     
sh_media_SYNCO (void)
{
  __asm__ __volatile__ ("synco");
}

__inline__ static
void
sh_media_ALLOCO (void *mm, int s)
{
  __builtin_sh_media_ALLOCO (mm + s);
}

__inline__ static
void
sh_media_ICBI (void *mm, int s)
{
  __asm__ __volatile__ ("icbi	%m0" : : "o" (((char*)mm)[s]));
}

__inline__ static
void
sh_media_OCBI (void *mm, int s)
{
  __asm__ __volatile__ ("ocbi	%m0" : : "o" (((char*)mm)[s]));
}

__inline__ static
void
sh_media_OCBP (void *mm, int s)
{
  __asm__ __volatile__ ("ocbp	%m0" : : "o" (((char*)mm)[s]));
}

__inline__ static
void
sh_media_OCBWB (void *mm, int s)
{
  __asm__ __volatile__ ("ocbwb	%m0" : : "o" (((char*)mm)[s]));
}

__inline__ static
void
sh_media_PREFI (void *mm, int s)
{
  __asm__ __volatile__ ("prefi	%m0" : : "o" (((char*)mm)[s]));
}

__inline__ static
void
sh_media_BRK (void)
{
  __asm__ __volatile__ ("brk");
}

__inline__ static
void
sh_media_TRAPA (unsigned long long mm)
{
  __asm__ __volatile__ ("trapa	%%0" : : "r" (mm));
}

__inline__ static
short         
sh_media_unaligned_LD_W (void *p)
{
#if __LITTLE_ENDIAN__
  return (((unsigned char *)p)[0]
	  | (((short)((__signed__ char *)p)[1]) << 8));
#else
  return ((((short)((__signed__ char *)p)[0]) << 8)
	  | ((unsigned char *)p)[1]);
#endif
}

__inline__ static
unsigned short
sh_media_unaligned_LD_UW (void *p)
{
  unsigned char *addr = p;
#if __LITTLE_ENDIAN__
  return sh_media_MSHFLO_B (addr[0], addr[1]);
#else
  return sh_media_MSHFLO_B (addr[1], addr[0]);
#endif
}

/* We don't use the sh_media_LD* functions here because that turned out
   to impede constant propagation of the offsets into the ldhi / ldlo
   instructions.  */
__inline__ static
int           
sh_media_unaligned_LD_L (void *p)
{
#if __LITTLE_ENDIAN__
  return (__builtin_sh_media_LDHI_L ((char *)p + 3)
	  | __builtin_sh_media_LDLO_L (p));
#else
  return (__builtin_sh_media_LDLO_L ((char *)p + 3)
	  | __builtin_sh_media_LDHI_L (p));
#endif
}

__inline__ static
long long     
sh_media_unaligned_LD_Q (void *p)
{
#if __LITTLE_ENDIAN__
  return (__builtin_sh_media_LDHI_Q ((char *)p + 7)
	  | __builtin_sh_media_LDLO_Q (p));
#else
  return (__builtin_sh_media_LDLO_Q ((char *)p + 7)
	  | __builtin_sh_media_LDHI_Q (p));
#endif
}

__inline__ static
void
sh_media_unaligned_ST_W (void *p, unsigned int k)
{
  char *addr = p;
#if __LITTLE_ENDIAN__
  addr[0] = k;
  addr[1] = k >> 8;
#else
  addr[1] = k;
  addr[0] = k >> 8;
#endif
}

/* We don't use the sh_media_ST* functions here because that turned out
   to impede constant propagation of the offsets into the ldhi / ldlo
   instructions.  */
__inline__ static
void
sh_media_unaligned_ST_L (void *p, unsigned int k)
{
#if __LITTLE_ENDIAN__
  __builtin_sh_media_STHI_L (p + 3, k);
  __builtin_sh_media_STLO_L (p, k);
#else
  __builtin_sh_media_STLO_L (p + 3, k);
  __builtin_sh_media_STHI_L (p, k);
#endif
}

__inline__ static
void
sh_media_unaligned_ST_Q (void *p, unsigned long long k)
{
#if __LITTLE_ENDIAN__
  __builtin_sh_media_STHI_Q (p + 7, k);
  __builtin_sh_media_STLO_Q (p, k);
#else
  __builtin_sh_media_STLO_Q (p + 7, k);
  __builtin_sh_media_STHI_Q (p, k);
#endif
}

#if ! __SH4_NOFPU__
__inline__ static
void
sh_media_FVCOPY_S (const void *fvg, void *fvf)
{
  const __GCC_FV *g = fvg;
  __GCC_FV *f = fvf;
  *f = *g;
}

__inline__ static
void
sh_media_FVADD_S (const void *fvg, const void *fvh, void *fvf)
{
  const float *g = fvg, *h = fvh;
  float *f = fvf;
#if 1
  int i;

  for (i = 0; i < 4; i++)
    f[i] = g[i] + h[i];
#else
  f[0] = g[0] + h[0];
  f[1] = g[1] + h[1];
  f[2] = g[2] + h[2];
  f[3] = g[3] + h[3];
#endif
}

__inline__ static
void
sh_media_FVSUB_S (const void *fvg, const void *fvh, void *fvf)
{
  const float *g = fvg, *h = fvh;
  float *f = fvf;
#if 1
  int i;

  for (i = 0; i < 4; i++)
    f[i] = g[i] - h[i];
#else
  f[0] = g[0] - h[0];
  f[1] = g[1] - h[1];
  f[2] = g[2] - h[2];
  f[3] = g[3] - h[3];
#endif
}

__inline__ static
void
sh_media_FMTRXCOPY_S (const void *mtrxg, void *mtrxf)
{
  const __GCC_MTRX *g = mtrxg;
  __GCC_MTRX *f = mtrxf;
  *f = *g;
}

__inline__ static
void
sh_media_FMTRXADD_S (const void *mtrxg, const void *mtrxh, void *mtrxf)
{
  const __GCC_FV *g = mtrxg, *h = mtrxh;
  __GCC_FV *f = mtrxf;
#if 1
  int i;

  for (i = 0; i < 4; i++)
    sh_media_FVADD_S (&g[i], &h[i], &f[i]);
#else
  sh_media_FVADD_S (&g[0], &h[0], &f[0]);
  sh_media_FVADD_S (&g[1], &h[1], &f[1]);
  sh_media_FVADD_S (&g[2], &h[2], &f[2]);
  sh_media_FVADD_S (&g[3], &h[3], &f[3]);
#endif
}

__inline__ static
void
sh_media_FMTRXSUB_S (const void *mtrxg, const void *mtrxh, void *mtrxf)
{
  const __GCC_FV *g = mtrxg, *h = mtrxh;
  __GCC_FV *f = mtrxf;
#if 1
  int i;

  for (i = 0; i < 4; i++)
    sh_media_FVSUB_S (&g[i], &h[i], &f[i]);
#else
  sh_media_FVSUB_S (&g[0], &h[0], &f[0]);
  sh_media_FVSUB_S (&g[1], &h[1], &f[1]);
  sh_media_FVSUB_S (&g[2], &h[2], &f[2]);
  sh_media_FVSUB_S (&g[3], &h[3], &f[3]);
#endif
}

__inline__ static
void
sh_media_FTRVADD_S (const void *mtrxg, const void *fvh, const void *fvi, void *fvf)
{
  sh_media_FTRV_S (mtrxg, fvh, fvf);
  sh_media_FVADD_S (fvf, fvi, fvf);
}

__inline__ static
void
sh_media_FTRVSUB_S (const void *mtrxg, const void *fvh, const void *fvi, void *fvf)
{
  sh_media_FTRV_S (mtrxg, fvh, fvf);
  sh_media_FVSUB_S (fvf, fvi, fvf);
}

__inline__ static
void
sh_media_FMTRXMUL_S (const void *mtrxg, const void *mtrxh, void *mtrxf)
{
  const __GCC_FV *g = mtrxg;
  __GCC_FV *f = mtrxf;
#if 1
  int j;

  for (j = 0; j < 4; j++)
    sh_media_FTRV_S (mtrxh, &g[j], &f[j]);
#else
  sh_media_FTRV_S (mtrxh, &g[0], &f[0]);
  sh_media_FTRV_S (mtrxh, &g[1], &f[1]);
  sh_media_FTRV_S (mtrxh, &g[2], &f[2]);
  sh_media_FTRV_S (mtrxh, &g[3], &f[3]);
#endif
}

__inline__ static
void
sh_media_FMTRXMULADD_S (const void *mtrxg, const void *mtrxh, const void *mtrxi, void *mtrxf)
{
  const __GCC_FV *g = mtrxg, *i = mtrxi;
  __GCC_FV *f = mtrxf;
#if 1
  int j;

  for (j = 0; j < 4; j++)
    sh_media_FTRVADD_S (mtrxh, &g[j], &i[j], &f[j]);
#else
  sh_media_FTRVADD_S (mtrxh, &g[0], &i[0], &f[0]);
  sh_media_FTRVADD_S (mtrxh, &g[1], &i[1], &f[1]);
  sh_media_FTRVADD_S (mtrxh, &g[2], &i[2], &f[2]);
  sh_media_FTRVADD_S (mtrxh, &g[3], &i[3], &f[3]);
#endif
}

__inline__ static
void
sh_media_FMTRXMULSUB_S (const void *mtrxg, const void *mtrxh, const void *mtrxi, void *mtrxf)
{
  const __GCC_FV *g = mtrxg, *i = mtrxi;
  __GCC_FV *f = mtrxf;
#if 1
  int j;

  for (j = 0; j < 4; j++)
    sh_media_FTRVSUB_S (mtrxh, &g[j], &i[j], &f[j]);
#else
  sh_media_FTRVSUB_S (mtrxh, &g[0], &i[0], &f[0]);
  sh_media_FTRVSUB_S (mtrxh, &g[1], &i[1], &f[1]);
  sh_media_FTRVSUB_S (mtrxh, &g[2], &i[2], &f[2]);
  sh_media_FTRVSUB_S (mtrxh, &g[3], &i[3], &f[3]);
#endif
}
#endif /* ! __SH4_NOFPU__ */

#endif /* __SHMEDIA__ */

#endif /* _USHMEDIA_H */