summaryrefslogtreecommitdiff
path: root/backend/src/libocl/script/ocl_convert.sh
blob: 31fdb6f58f54d6e5a1ae17fba7ef4573b888e877 (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
#! /bin/sh -e

echo '
/*
 * Copyright © 2012 - 2014 Intel Corporatio
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
'

if [ $1"a" = "-pa" ]; then
    echo "#ifndef __OCL_CONVERT_H__"
    echo "#define __OCL_CONVERT_H__"
    echo "#include \"ocl_types.h\""
    echo
else
    echo "#include \"ocl_convert.h\""
    echo "#include \"ocl_float.h\""
    echo "#include \"ocl_as.h\""
    echo "#include \"ocl_integer.h\""
    echo
fi

# Supported base types and their lengths
TYPES="long:8 ulong:8 int:4 uint:4 short:2 ushort:2 char:1 uchar:1 double:8 float:4 half:2"
# Supported vector lengths
VECTOR_LENGTHS="1 2 3 4 8 16"
ROUNDING_MODES="rte rtz rtp rtn"

# For all vector lengths and types, generate conversion functions
for vector_length in $VECTOR_LENGTHS; do
    if test $vector_length -eq 1; then
	for ftype in $TYPES; do
	    fbasetype=`IFS=:; set -- dummy $ftype; echo $2`
	    for ttype in $TYPES; do
		tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tbasetype convert_$tbasetype($fbasetype v);"
		else
		    echo "OVERLOADABLE $tbasetype convert_$tbasetype($fbasetype v) {"
		    echo "  return ($tbasetype)v;"
		    echo "}"
		    echo
		fi
	    done
	done
    else
	for ftype in $TYPES; do
	    fbasetype=`IFS=:; set -- dummy $ftype; echo $2`
	    for ttype in $TYPES; do
		tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
		if test $fbasetype = $tbasetype; then
		    if test $vector_length -gt 1; then
			fvectortype=$fbasetype$vector_length
			tvectortype=$tbasetype$vector_length
			if [ $1"a" = "-pa" ]; then
			    echo "OVERLOADABLE $tvectortype convert_$tvectortype($fvectortype v);"
			else
			    echo "OVERLOADABLE $tvectortype convert_$tvectortype($fvectortype v) { return v; }"
			fi
		    else
			if [ $1"a" = "-pa" ]; then
			    echo "OVERLOADABLE $tbasetype convert_$tbasetype($fbasetype v);"
			else
			    echo "OVERLOADABLE $tbasetype convert_$tbasetype($fbasetype v) { return v; }"
			fi
		    fi
		    continue
		fi
		fvectortype=$fbasetype$vector_length
		tvectortype=$tbasetype$vector_length
		construct="($tbasetype)(v.s0)"
		if test $vector_length -gt 1; then
		    construct="$construct, ($tbasetype)(v.s1)"
		fi
		if test $vector_length -gt 2; then
		    construct="$construct, ($tbasetype)(v.s2)"
		fi
		if test $vector_length -gt 3; then
		    construct="$construct, ($tbasetype)(v.s3)"
		fi
		if test $vector_length -gt 4; then
		    construct="$construct, ($tbasetype)(v.s4)"
		    construct="$construct, ($tbasetype)(v.s5)"
		    construct="$construct, ($tbasetype)(v.s6)"
		    construct="$construct, ($tbasetype)(v.s7)"
		fi
		if test $vector_length -gt 8; then
		    construct="$construct, ($tbasetype)(v.s8)"
		    construct="$construct, ($tbasetype)(v.s9)"
		    construct="$construct, ($tbasetype)(v.sA)"
		    construct="$construct, ($tbasetype)(v.sB)"
		    construct="$construct, ($tbasetype)(v.sC)"
		    construct="$construct, ($tbasetype)(v.sD)"
		    construct="$construct, ($tbasetype)(v.sE)"
		    construct="$construct, ($tbasetype)(v.sF)"
		fi

		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tvectortype convert_$tvectortype($fvectortype v);"
		else
		    echo "OVERLOADABLE $tvectortype convert_$tvectortype($fvectortype v) {"
		    echo "  return ($tvectortype)($construct);"
		    echo "}"
		    echo
		fi
	    done
	done
    fi
done

echo '
/* The sat cvt supported by HW. */
#define DEF(DSTTYPE, SRCTYPE) \
OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x);
DEF(char, uchar);
DEF(char, short);
DEF(char, ushort);
DEF(char, int);
DEF(char, uint);
DEF(char, float);
DEF(uchar, char);
DEF(uchar, short);
DEF(uchar, ushort);
DEF(uchar, int);
DEF(uchar, uint);
DEF(uchar, float);
DEF(short, ushort);
DEF(short, int);
DEF(short, uint);
DEF(short, float);
DEF(short, double);
DEF(ushort, short);
DEF(ushort, int);
DEF(ushort, uint);
DEF(ushort, float);
DEF(ushort, double);
DEF(int, uint);
DEF(int, float);
DEF(int, double);
DEF(uint, int);
DEF(uint, float);
DEF(uint, double);
DEF(char, half);
DEF(uchar, half);
DEF(short, half);
DEF(ushort, half);
DEF(int, half);
DEF(uint, half);
#undef DEF
'

if [ $1"a" = "-pa" ]; then
    echo "#define DEF(DSTTYPE, SRCTYPE, MIN, MAX)  OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x);"
else
    echo '
#define DEF(DSTTYPE, SRCTYPE, MIN, MAX) \
OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x) { \
  x = x >= MAX ? MAX : x; \
  return x <= MIN ? (DSTTYPE)MIN : (DSTTYPE)x; \
}
'
fi

echo '
DEF(char, long, -128, 127);
DEF(uchar, long, 0, 255);
DEF(short, long, -32768, 32767);
DEF(ushort, long, 0, 65535);
DEF(int, long, -0x7fffffff-1, 0x7fffffff);
DEF(uint, long, 0, 0xffffffffu);
#undef DEF
'

if [ $1"a" = "-pa" ]; then
    echo "
#define DEF(DSTTYPE, SRCTYPE, SRC_MIN, SRC_MAX, DST_MIN, DST_MAX) \
OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x);"
else
    echo '
//convert float to long/ulong must take care of overflow, if overflow the value is undef.
#define DEF(DSTTYPE, SRCTYPE, SRC_MIN, SRC_MAX, DST_MIN, DST_MAX) \
OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x) { \
  DSTTYPE y = x >= SRC_MAX ? DST_MAX : (DSTTYPE)x; \
  return x <= SRC_MIN ? DST_MIN : y; \
}
'
fi

echo '
DEF(long, float, -0x1.0p63, 0x1.0p63, 0x8000000000000000, 0x7fffffffffffffff);
DEF(ulong, float, 0, 0x1.0p64, 0, 0xffffffffffffffff);
DEF(char, double, -0x1.0p7, 0x1.0p7,  0x80, 0x7F);
DEF(uchar, double, 0, 0x1.0p8, 0, 0xFF);
DEF(long, double, -0x1.0p63, 0x1.0p63, 0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
DEF(ulong, double, 0, 0x1.0p64, 0, 0xFFFFFFFFFFFFFFFF);
#undef DEF
'

if [ $1"a" = "-pa" ]; then
    echo "#define DEF(DSTTYPE, SRCTYPE, MAX) OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x);"
else
    echo '
#define DEF(DSTTYPE, SRCTYPE, MAX) \
OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x) { \
  return x >= MAX ? (DSTTYPE)MAX : x; \
}
'
fi

echo '
DEF(char, ulong, 127);
DEF(uchar, ulong, 255);
DEF(short, ulong, 32767);
DEF(ushort, ulong, 65535);
DEF(int, ulong, 0x7fffffff);
DEF(uint, ulong, 0xffffffffu);
#undef DEF
'

if [ $1"a" = "-pa" ]; then
    echo  "OVERLOADABLE long convert_long_sat(ulong x);"
else
    echo '
OVERLOADABLE long convert_long_sat(ulong x) {
  ulong MAX = 0x7ffffffffffffffful;
  return x >= MAX ? MAX : x;
}
'
fi

if [ $1"a" = "-pa" ]; then
    echo "#define DEF(DSTTYPE, SRCTYPE) OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x);"
else
    echo '
#define DEF(DSTTYPE, SRCTYPE) \
  OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x) { \
  return x <= 0 ? 0 : x; \
}
'
fi

echo '
  DEF(ushort, char);
  DEF(uint, char);
  DEF(uint, short);
  DEF(ulong, char);
  DEF(ulong, short);
  DEF(ulong, int);
  DEF(ulong, long);
  #undef DEF
'

if [ $1"a" = "-pa" ]; then
    echo "#define DEF(DSTTYPE, SRCTYPE) OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x);"
else
    echo '
#define DEF(DSTTYPE, SRCTYPE) \
  OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat(SRCTYPE x) { \
  return x; \
}
'
fi

echo '
DEF(char, char);
DEF(uchar, uchar);
DEF(short, char);
DEF(short, uchar);
DEF(short, short);
DEF(ushort, uchar);
DEF(ushort, ushort);
DEF(int, char);
DEF(int, uchar);
DEF(int, short);
DEF(int, ushort);
DEF(int, int);
DEF(uint, uchar);
DEF(uint, ushort);
DEF(uint, uint);
DEF(long, char);
DEF(long, uchar);
DEF(long, short);
DEF(long, ushort);
DEF(long, int);
DEF(long, uint);
DEF(long, long);
DEF(ulong, uchar);
DEF(ulong, ushort);
DEF(ulong, uint);
DEF(ulong, ulong);
#undef DEF
'

# for half to long
if [ $1"a" = "-pa" ]; then
    echo '
       OVERLOADABLE long convert_long_sat(half x);
       OVERLOADABLE ulong convert_ulong_sat(half x);
       '
else
    echo '
union _type_half_and_ushort {
  half hf;
  ushort us;
};
OVERLOADABLE long convert_long_sat(half x) {
  union _type_half_and_ushort u;
  u.hf = x;
  if (u.us == 0x7C00) // +inf
    return 0x7FFFFFFFFFFFFFFF;
  if (u.us == 0xFC00) // -inf
    return 0x8000000000000000;

  return (long)x;
}
OVERLOADABLE ulong convert_ulong_sat(half x) {
  union _type_half_and_ushort u;
  u.hf = x;
  if (u.us == 0x7C00) // +inf
    return 0xFFFFFFFFFFFFFFFF;

  if (x < (half)0.0) {
    return 0;
  }
  return (ulong)x;
}'
fi


# vector convert_DSTTYPE_sat function
for vector_length in $VECTOR_LENGTHS; do
    if test $vector_length -eq 1; then continue; fi

    for ftype in $TYPES; do
	fbasetype=`IFS=:; set -- dummy $ftype; echo $2`
	#if test $fbasetype = "double"; then continue; fi

	for ttype in $TYPES; do
	    tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	    if test $tbasetype = "double" -o $tbasetype = "float" -o $tbasetype = "half" ; then continue; fi

	    fvectortype=$fbasetype$vector_length
	    tvectortype=$tbasetype$vector_length
	    conv="convert_${tbasetype}_sat"

	    construct="$conv(v.s0)"
	    if test $vector_length -gt 1; then
		construct="$construct, $conv(v.s1)"
	    fi
	    if test $vector_length -gt 2; then
		construct="$construct, $conv(v.s2)"
	    fi
	    if test $vector_length -gt 3; then
		construct="$construct, $conv(v.s3)"
	    fi
	    if test $vector_length -gt 4; then
		construct="$construct, $conv(v.s4)"
		construct="$construct, $conv(v.s5)"
		construct="$construct, $conv(v.s6)"
		construct="$construct, $conv(v.s7)"
	    fi
	    if test $vector_length -gt 8; then
		construct="$construct, $conv(v.s8)"
		construct="$construct, $conv(v.s9)"
		construct="$construct, $conv(v.sA)"
		construct="$construct, $conv(v.sB)"
		construct="$construct, $conv(v.sC)"
		construct="$construct, $conv(v.sD)"
		construct="$construct, $conv(v.sE)"
		construct="$construct, $conv(v.sF)"
	    fi

	    if [ $1"a" = "-pa" ]; then
		echo "OVERLOADABLE $tvectortype convert_${tvectortype}_sat($fvectortype v);"
	    else
		echo "OVERLOADABLE $tvectortype convert_${tvectortype}_sat($fvectortype v) {"
		echo "  return ($tvectortype)($construct);"
		echo "}"
		echo
	    fi
	done
    done
done

if [ $1"a" != "-pa" ]; then
echo '
CONST float __gen_ocl_rndz(float x) __asm("llvm.trunc" ".f32");
CONST float __gen_ocl_rnde(float x) __asm("llvm.rint" ".f32");
CONST float __gen_ocl_rndu(float x) __asm("llvm.ceil" ".f32");
CONST float __gen_ocl_rndd(float x) __asm("llvm.floor" ".f32");
OVERLOADABLE float __convert_float_rtz(long x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  long l = u.f;
  if((l > x && x > 0) || x >= 0x7fffffc000000000 ||
    (l < x && x < 0)) {
    u.u -= 1;
  }
  return u.f;
}
OVERLOADABLE float __convert_float_rtp(long x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  long l = u.f;  //can not use u.f < x
  if(l < x && x < 0x7fffffc000000000) {
    if(x > 0)
      u.u = u.u + 1;
    else
      u.u = u.u - 1;
  }
  return u.f;
}
OVERLOADABLE float __convert_float_rtn(long x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  long l = u.f;  //avoid overflow
  if(l > x || x >= 0x7fffffc000000000) {
    if(x > 0)
      u.u = u.u - 1;
    else
      u.u = u.u + 1;
  }
  return u.f;
}
OVERLOADABLE float __convert_float_rtz(ulong x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  ulong l = u.f;
  if(l > x  || x >= 0xffffff8000000000)
    u.u -= 1;
  return u.f;
}
OVERLOADABLE float __convert_float_rtp(ulong x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  ulong l = u.f;  //can not use u.f < x
  if(l < x && x < 0xffffff8000000000)
    u.u = u.u + 1;
  return u.f;
}
OVERLOADABLE float __convert_float_rtn(ulong x)
{
  return __convert_float_rtz(x);
}
OVERLOADABLE float __convert_float_rtz(int x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  long i = u.f;
  if((i > x && x > 0) ||
    (i < x && x < 0)) {
    u.u -= 1;
  }
  return u.f;
}
OVERLOADABLE float __convert_float_rtp(int x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  int i = u.f;
  if(i < x) {
    if(x > 0)
      u.u += 1;
    else
      u.u -= 1;
  }
  return u.f;
}
OVERLOADABLE float __convert_float_rtn(int x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  long i = u.f;  //avoid overflow
  if(i > x) {
    if(x > 0)
      u.u = u.u - 1;
    else
      u.u = u.u + 1;
  }
  return u.f;
}
OVERLOADABLE float __convert_float_rtz(uint x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  ulong i = u.f;
  if(i > x)
    u.u -= 1;
  return u.f;
}
OVERLOADABLE float __convert_float_rtp(uint x)
{
  union {
    uint u;
    float f;
  } u;
  u.f = x;
  uint i = u.f;
  if(i < x)
    u.u += 1;
  return u.f;
}
OVERLOADABLE float __convert_float_rtn(uint x)
{
    return __convert_float_rtz(x);
}

OVERLOADABLE char convert_char_rtn(double x)
{
	return (char)convert_int_rtn(x);
}

OVERLOADABLE uchar convert_uchar_rtn(double x)
{
	return (uchar)convert_uint_rtn(x);
}

OVERLOADABLE short convert_short_rtn(double x)
{
	return (short)convert_int_rtn(x);
}

OVERLOADABLE ushort convert_ushort_rtn(double x)
{
	return (ushort)convert_uint_rtn(x);
}

OVERLOADABLE int convert_int_rtn(double x)
{
	int ret, iexp ;
	long lval = as_long(x);
	if((lval & DF_ABS_MASK) == 0) return 0;

	uint sign = (lval & DF_SIGN_MASK) >> DF_SIGN_OFFSET;
	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	ulong ma = (lval &DF_MAN_MASK);

	long intPart = (ma |DF_IMPLICITE_ONE)>> (DF_EXP_OFFSET -exp);
	ret = (int)intPart;
	iexp = (exp < 0) ? 0:exp;
	ret = (exp < 0) ? 0:ret;
	ret = sign ? -ret:ret;
	long mask = (1L << (DF_EXP_OFFSET -iexp)) - 1;
	ret = ((ma & mask) || (exp < 0)) ? ret -sign:ret;

	return ret;
}

OVERLOADABLE uint convert_uint_rtn(double x)
{
	uint ret, iexp ;
	long lval = as_long(x);
	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	long ma = (lval &DF_MAN_MASK);

	long intPart = (ma |DF_IMPLICITE_ONE)>> (DF_EXP_OFFSET -exp);
	ret = (int)intPart;
	ret = (exp < 0) ? 0:ret;

	return ret;
}

OVERLOADABLE long convert_long_rtn(double x)
{
	int iexp;
	long ret;
	ulong lval = as_ulong(x);
	uint sign = (lval & DF_SIGN_MASK) >> DF_SIGN_OFFSET;
	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	ulong ma = (lval &DF_MAN_MASK);

	ulong ldata = ma |DF_IMPLICITE_ONE;
	uint shift = abs(exp -DF_EXP_OFFSET);
	ulong ldataL = ldata << shift;
	ulong ldataR = ldata >> shift;
	ret    = (exp > DF_EXP_OFFSET) ? ldataL:ldataR;
	ret    = (exp >= 0) ? ret:0;
	iexp = (exp >= 0) ? exp:0;

	ret = sign ? -ret:ret;
	shift = convert_uint_sat(DF_EXP_OFFSET - iexp);
	long mask = (1L << shift) - 1;
	ret = ((ma & mask) || (exp < 0)) ? ret -sign:ret;
	ret = (lval & DF_ABS_MASK) ? ret:0;

	return ret;
}

OVERLOADABLE ulong convert_ulong_rtn(double x)
{
	int iexp;
	long ret;
	long lval = as_long(x);
	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	long ma = (lval &DF_MAN_MASK);

	ulong ldata = ma |DF_IMPLICITE_ONE;
	int shift = abs(exp -DF_EXP_OFFSET);
	ulong ldataL = ldata << shift;
	ulong ldataR = ldata >> shift;
	ret    = (exp > DF_EXP_OFFSET) ? ldataL:ldataR;
	ret    = (exp >= 0) ? ret:0;
	iexp = (exp >= 0) ? exp:0;
	ret    = (lval & DF_ABS_MASK) ? ret:0;

	return ret;
}

OVERLOADABLE float  convert_float_rtn(double x)
{
	int ret, tmp;
	long lval = as_long(x);
	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	int sign = (lval & DF_SIGN_MASK)?1:0;

	ret = sign;
	ret = (lval & DF_ABS_MASK) ? ret:0;
	tmp = 0x1000000 >> (23 - (exp + 149));
	ret = (exp < -126) ? tmp:ret;
	long ma = (lval &DF_MAN_MASK);
	tmp = (lval & DF_MAN_MASK) ? SF_NAN:SF_POSITIVE_INF;
	ret = (exp == 1024) ? tmp:ret;
	tmp = SF_POSITIVE_INF;
	ret = (exp > 127) ? tmp:ret;
	ret = (lval & DF_ABS_MASK) ? ret:0;
	tmp = ((exp + 127) << 23) |convert_int(ma >> 29);
	if((ma & 0x1FFFFFFF) && sign) tmp += 1;
	ret = ((exp >= -126) && (exp <= 127)) ? tmp:ret;
	ret |= (sign << 31);
	float ftemp = as_float(ret);

	return ftemp;
}

OVERLOADABLE long convert_long_rte(double x)
{
	long lval = as_long(x);
	long ret = convert_ulong_rte(x);
	ret = (lval & DF_SIGN_MASK) ? -ret:ret;

	return ret;
}

OVERLOADABLE ulong convert_ulong_rte(double x)
{
	ulong ret;
	long lval = as_long(x);
	if((lval & DF_ABS_MASK) == 0) return 0;

	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	long ma = (lval & DF_MAN_MASK);
	uint shift = abs(exp -DF_EXP_OFFSET);
	long absVal = (ma |DF_IMPLICITE_ONE);
	ret = absVal << shift;
	long tmp = absVal >> shift;
	int lastBit = tmp & 0x1;
	long mask = (1L << shift) - 1;
	long roundBit = (1L << (shift -1));
	long diff = (mask & absVal) - roundBit;
	tmp = (diff == 0) ? tmp + lastBit:tmp;
	tmp = (diff > 0) ? tmp+1:tmp;

	ret = (exp < 52) ? tmp:ret;
	ret = (exp < -1) ? 0:ret;

	return ret;
}

OVERLOADABLE ulong convert_ulong_rtp(double x)
{
	int iexp ;
	ulong ret = 1;
	long lval = as_long(x);

	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	long ma = (lval & DF_MAN_MASK);
	int shift = abs(exp - 52);
	ret = (ma |DF_IMPLICITE_ONE)<< shift;
	ulong tmp = (ma |DF_IMPLICITE_ONE)>> shift;
	long mask = (1L << shift) - 1;
	tmp = (mask & ma) ? tmp+1:tmp;

	ret = (exp > 52) ? ret:tmp;
	ret = (exp < 0) ? 1:ret;
	ret = (lval & DF_SIGN_MASK) ? 0:ret;
	ret = (lval & DF_ABS_MASK) ? ret:0;

	return ret;
}

OVERLOADABLE long convert_long_rtp(double x)
{
	int iexp ;
	ulong ret;
	long lval = as_long(x);

	int exp = ((lval & DF_EXP_MASK) >> DF_EXP_OFFSET) - DF_EXP_BIAS;
	long ma = (lval & DF_MAN_MASK);
	int shift = abs(exp - 52);
	ret = (ma |DF_IMPLICITE_ONE)<< shift;
	ulong tmp = (ma |DF_IMPLICITE_ONE)>> shift;
	long mask = (1L << shift) - 1;
	int sign = ((lval & DF_SIGN_MASK) >> DF_SIGN_OFFSET);
	tmp = ((mask & ma) && !sign) ? tmp+1:tmp;

	ret = (exp > 52) ? ret:tmp;
	ret = (exp < 0) ? 1 -sign:ret;
	ret = (lval & DF_ABS_MASK) ? ret:0;
	ret = sign ? -ret:ret;

	return ret;
}
'
fi

# convert_u|char|short|int_rte(double x)
ITYPES="int:4 uint:4 short:2 ushort:2 char:1 uchar:1"
for ttype in $ITYPES; do
	tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	if [ $1"a" != "-pa" ]; then
	echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rte(double x)"
	echo "{ return ($tbasetype)convert_long_rte(x);}"
	fi
done

# convert_u|char|short|int_rte(double x)
IUTYPES="ulong:8 uint:4 ushort:2 uchar:1"
for ttype in $IUTYPES; do
	tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	if [ $1"a" != "-pa" ]; then
	echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtz(double x)"
	echo "{ double lx = (x < 0) ? 0:x;"
	echo " return convert_${tbasetype}_rtn(lx);}"
	fi
done

ITYPES="long:8 int:4 short:2 char:1"
for ttype in $ITYPES; do
	tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	if [ $1"a" != "-pa" ]; then
	echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtz(double x)"
	echo "{ double lx = (x < 0) ? -x:x;"
	echo " $tbasetype tmp = convert_u${tbasetype}_rtn(lx);"
	echo "return (x < 0) ? -tmp:tmp;}"
	fi
done

# convert_u|char|short|int_rtp(double x)
IUTYPES="uint:4 ushort:2 uchar:1"
for ttype in $IUTYPES; do
	tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	if [ $1"a" != "-pa" ]; then
	echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtp(double x)"
	echo "{ return ($tbasetype)convert_ulong_rtp(x);}"
	fi
done

ITYPES="int:4 short:2 char:1"
for ttype in $ITYPES; do
	tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	if [ $1"a" != "-pa" ]; then
	echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtp(double x)"
	echo " { return ($tbasetype)convert_long_rtp(x);}"
	fi
done

# convert_DSTTYPE_ROUNDING function
for vector_length in $VECTOR_LENGTHS; do
    for ftype in $TYPES; do
	fbasetype=`IFS=:; set -- dummy $ftype; echo $2`
	#if test $fbasetype = "double"; then continue; fi

	for ttype in $TYPES; do
	    tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	    if test $tbasetype = "double"; then continue; fi

	    if test $vector_length -eq 1; then
		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rte($fbasetype x);"
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtz($fbasetype x);"
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtp($fbasetype x);"
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtn($fbasetype x);"
		else
		    if [ "$fbasetype" = "double" ]; then
		        continue;
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rte($fbasetype x)"
		    if test $fbasetype = "float" -a $tbasetype != "float"; then
			echo "{ return __gen_ocl_rnde(x); }"
		    else
			echo "{ return x; }"
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtz($fbasetype x)"
		    if test $fbasetype = "float" -a $tbasetype != "float"; then
			echo "{ return __gen_ocl_rndz(x); }"
		    elif [ "$fbasetype" = "int" -o "$fbasetype" = "uint" -o "$fbasetype" = "long" -o "$fbasetype" = "ulong" ] && [ "$tbasetype" = "float" ]; then
			echo "{ return __convert_${tbasetype}_rtz(x); }"
		    else
			echo "{ return x; }"
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtp($fbasetype x)"
		    if test $fbasetype = "float" -a $tbasetype != "float"; then
			echo "{ return __gen_ocl_rndu(x); }"
		    elif [ "$fbasetype" = "int" -o "$fbasetype" = "uint" -o "$fbasetype" = "long" -o "$fbasetype" = "ulong" ] && [ "$tbasetype" = "float" ]; then
			echo "{ return __convert_${tbasetype}_rtp(x); }"
		    else
			echo "{ return x; }"
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_rtn($fbasetype x)"
		    if test $fbasetype = "float" -a $tbasetype != "float"; then
			echo "{ return __gen_ocl_rndd(x); }"
		    elif [ "$fbasetype" = "int" -o "$fbasetype" = "uint" -o "$fbasetype" = "long" -o "$fbasetype" = "ulong" ] && [ "$tbasetype" = "float" ]; then
			echo "{ return __convert_${tbasetype}_rtn(x); }"
		    else
			echo "{ return x; }"
		    fi
		fi

		continue
	    fi

	    for rounding in $ROUNDING_MODES; do
		fvectortype=$fbasetype$vector_length
		tvectortype=$tbasetype$vector_length
		conv="convert_${tbasetype}_${rounding}"

		construct="$conv(v.s0)"
		if test $vector_length -gt 1; then
		    construct="$construct, $conv(v.s1)"
		fi
		if test $vector_length -gt 2; then
		    construct="$construct, $conv(v.s2)"
		fi
		if test $vector_length -gt 3; then
		    construct="$construct, $conv(v.s3)"
		fi
		if test $vector_length -gt 4; then
		    construct="$construct, $conv(v.s4)"
		    construct="$construct, $conv(v.s5)"
		    construct="$construct, $conv(v.s6)"
		    construct="$construct, $conv(v.s7)"
		fi
		if test $vector_length -gt 8; then
		    construct="$construct, $conv(v.s8)"
		    construct="$construct, $conv(v.s9)"
		    construct="$construct, $conv(v.sA)"
		    construct="$construct, $conv(v.sB)"
		    construct="$construct, $conv(v.sC)"
		    construct="$construct, $conv(v.sD)"
		    construct="$construct, $conv(v.sE)"
		    construct="$construct, $conv(v.sF)"
		fi

		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tvectortype convert_${tvectortype}_${rounding}($fvectortype v);"
		else
		    echo "OVERLOADABLE $tvectortype convert_${tvectortype}_${rounding}($fvectortype v) {"
		    echo "  return ($tvectortype)($construct);"
		    echo "}"
		    echo
		fi
	    done
	done
    done
done

if [ $1"a" = "-pa" ]; then
        echo "#define DEF(DSTTYPE, SRCTYPE, SRC_MIN, SRC_MAX, DST_MIN, DST_MAX) \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rte(SRCTYPE x); \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rtz(SRCTYPE x); \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rtn(SRCTYPE x); \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rtp(SRCTYPE x);"
else
        echo '
            #define DEF(DSTTYPE, SRCTYPE, SRC_MIN, SRC_MAX, DST_MIN, DST_MAX) \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rte(SRCTYPE x) { \
              DSTTYPE y = convert_##DSTTYPE##_rte(x); \
              y = (x >= SRC_MAX) ? DST_MAX : y; \
              y = (x <= SRC_MIN) ? DST_MIN : y; \
              return y; \
            } \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rtz(SRCTYPE x) { \
              DSTTYPE y = convert_##DSTTYPE##_rtz(x); \
              y = (x >= SRC_MAX) ? DST_MAX : y; \
              y = (x <= SRC_MIN) ? DST_MIN : y; \
              return y; \
            } \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rtn(SRCTYPE x) { \
              DSTTYPE y = convert_##DSTTYPE##_rtn(x); \
              y = (x >= SRC_MAX) ? DST_MAX : y; \
              y = (x <= SRC_MIN) ? DST_MIN : y; \
              return y; \
            } \
            OVERLOADABLE DSTTYPE convert_ ## DSTTYPE ## _sat_rtp(SRCTYPE x) { \
              DSTTYPE y = convert_##DSTTYPE##_rtp(x); \
              y = (x >= SRC_MAX) ? DST_MAX : y; \
              y = (x <= SRC_MIN) ? DST_MIN : y; \
              return y; \
            }'
fi

echo '
    DEF(char, double, -0x1.0p7, 0x1.0p7-1,  0x80, 0x7F);
    DEF(uchar, double, 0, 0x1.0p8-1, 0, 0xFF);
    DEF(short, double, -0x1.0p15, 0x1.0p15-1, 0x8000, 0x7FFF);
    DEF(ushort, double, 0, 0x1.0p16-1, 0, 0xFFFF);
    DEF(int, double, -0x1.0p31, 0x1.0p31-1, 0x80000000, 0x7FFFFFFF);
    DEF(uint, double, 0, 0x1.0p32-1, 0, 0xFFFFFFFF);
    DEF(long, double, -0x1.0p63, 0x1.0p63, 0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
    DEF(ulong, double, 0, 0x1.0p64, 0, 0xFFFFFFFFFFFFFFFF);
    #undef DEF
    '

# convert_DSTTYPE_sat_ROUNDING function
for vector_length in $VECTOR_LENGTHS; do
    for ftype in $TYPES; do
	fbasetype=`IFS=:; set -- dummy $ftype; echo $2`
	#if test $fbasetype = "double"; then continue; fi

	for ttype in $TYPES; do
	    tbasetype=`IFS=:; set -- dummy $ttype; echo $2`
	    if test $tbasetype = "double" -o $tbasetype = "float" -o $tbasetype = "half" ; then continue; fi

	    if test $vector_length -eq 1; then
	        if test $fbasetype = "double"; then continue; fi
		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rte($fbasetype x);"
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rtz($fbasetype x);"
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rtp($fbasetype x);"
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rtn($fbasetype x);"
		else
		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rte($fbasetype x)"
		    if test $fbasetype = "float"; then
			echo "{ return convert_${tbasetype}_sat(__gen_ocl_rnde(x)); }"
		    else
			echo "{ return convert_${tbasetype}_sat(x); }"
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rtz($fbasetype x)"
		    if test $fbasetype = "float"; then
			echo "{ return convert_${tbasetype}_sat(__gen_ocl_rndz(x)); }"
		    else
			echo "{ return convert_${tbasetype}_sat(x); }"
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rtp($fbasetype x)"
		    if test $fbasetype = "float"; then
			echo "{ return convert_${tbasetype}_sat(__gen_ocl_rndu(x)); }"
		    else
			echo "{ return convert_${tbasetype}_sat(x); }"
		    fi

		    echo "OVERLOADABLE $tbasetype convert_${tbasetype}_sat_rtn($fbasetype x)"
		    if test $fbasetype = "float"; then
			echo "{ return convert_${tbasetype}_sat(__gen_ocl_rndd(x)); }"
		    else
			echo "{ return convert_${tbasetype}_sat(x); }"
		    fi
		fi
		continue
	    fi

	    for rounding in $ROUNDING_MODES; do
		fvectortype=$fbasetype$vector_length
		tvectortype=$tbasetype$vector_length
		conv="convert_${tbasetype}_sat_${rounding}"

		construct="$conv(v.s0)"
		if test $vector_length -gt 1; then
		    construct="$construct, $conv(v.s1)"
		fi
		if test $vector_length -gt 2; then
		    construct="$construct, $conv(v.s2)"
		fi
		if test $vector_length -gt 3; then
		    construct="$construct, $conv(v.s3)"
		fi
		if test $vector_length -gt 4; then
		    construct="$construct, $conv(v.s4)"
		    construct="$construct, $conv(v.s5)"
		    construct="$construct, $conv(v.s6)"
		    construct="$construct, $conv(v.s7)"
		fi
		if test $vector_length -gt 8; then
		    construct="$construct, $conv(v.s8)"
		    construct="$construct, $conv(v.s9)"
		    construct="$construct, $conv(v.sA)"
		    construct="$construct, $conv(v.sB)"
		    construct="$construct, $conv(v.sC)"
		    construct="$construct, $conv(v.sD)"
		    construct="$construct, $conv(v.sE)"
		    construct="$construct, $conv(v.sF)"
		fi

		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tvectortype convert_${tvectortype}_sat_${rounding}($fvectortype v);"
		else
		    echo "OVERLOADABLE $tvectortype convert_${tvectortype}_sat_${rounding}($fvectortype v) {"
		    echo "  return ($tvectortype)($construct);"
		    echo "}"
		    echo
		fi
	    done
	done
    done
done

# convert_double_roundingmode( int32, int16 ,int8)
ITYPES=" double:8 int:4 uint:4 short:2 ushort:2 char:1 uchar:1"
for vector_length in $VECTOR_LENGTHS; do
    for ftype in $ITYPES; do
	fbasetype=`IFS=:; set -- dummy $ftype; echo $2`

	    if test $vector_length -eq 1; then
		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE double convert_double_rte($fbasetype x);"
		    echo "OVERLOADABLE double convert_double_rtz($fbasetype x);"
		    echo "OVERLOADABLE double convert_double_rtp($fbasetype x);"
		    echo "OVERLOADABLE double convert_double_rtn($fbasetype x);"
		else
		    echo "OVERLOADABLE double convert_double_rte($fbasetype x)"
			echo "{ return convert_double(x); }"

		    echo "OVERLOADABLE double convert_double_rtz($fbasetype x)"
			echo "{ return convert_double(x); }"

		    echo "OVERLOADABLE double convert_double_rtp($fbasetype x)"
			echo "{ return convert_double(x); }"

		    echo "OVERLOADABLE double convert_double_rtn($fbasetype x)"
			echo "{ return convert_double(x); }"
		fi
		continue
	    fi

	    for rounding in $ROUNDING_MODES; do
		fvectortype=$fbasetype$vector_length
		tvectortype=double$vector_length
		conv="convert_double_${rounding}"

		construct="$conv(v.s0)"
		if test $vector_length -gt 1; then
		    construct="$construct, $conv(v.s1)"
		fi
		if test $vector_length -gt 2; then
		    construct="$construct, $conv(v.s2)"
		fi
		if test $vector_length -gt 3; then
		    construct="$construct, $conv(v.s3)"
		fi
		if test $vector_length -gt 4; then
		    construct="$construct, $conv(v.s4)"
		    construct="$construct, $conv(v.s5)"
		    construct="$construct, $conv(v.s6)"
		    construct="$construct, $conv(v.s7)"
		fi
		if test $vector_length -gt 8; then
		    construct="$construct, $conv(v.s8)"
		    construct="$construct, $conv(v.s9)"
		    construct="$construct, $conv(v.sA)"
		    construct="$construct, $conv(v.sB)"
		    construct="$construct, $conv(v.sC)"
		    construct="$construct, $conv(v.sD)"
		    construct="$construct, $conv(v.sE)"
		    construct="$construct, $conv(v.sF)"
		fi

		if [ $1"a" = "-pa" ]; then
		    echo "OVERLOADABLE $tvectortype convert_${tvectortype}_${rounding}($fvectortype v);"
		else
		    echo "OVERLOADABLE $tvectortype convert_${tvectortype}_${rounding}($fvectortype v) {"
		    echo "  return ($tvectortype)($construct);"
		    echo "}"
		    echo
		fi
	done
    done
done

if [ $1"a" = "-pa" ]; then
    echo "#endif /* __OCL_CONVERT_H__ */"
fi