-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
runtime_intrinsics.c
1723 lines (1594 loc) · 64.5 KB
/
runtime_intrinsics.c
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
// This file is a part of Julia. License is MIT: https://julialang.org/license
// This is in implementation of the Julia intrinsic functions against boxed types
// excluding the native function call interface (ccall, llvmcall)
//
// this file assumes a little-endian processor, although that isn't too hard to fix
// it also assumes two's complement negative numbers, which might be a bit harder to fix
#include "APInt-C.h"
#include "julia.h"
#include "julia_internal.h"
#include "llvm-version.h"
const unsigned int host_char_bit = 8;
// float16 conversion helpers
static inline float half_to_float(uint16_t ival) JL_NOTSAFEPOINT
{
uint32_t sign = (ival & 0x8000) >> 15;
uint32_t exp = (ival & 0x7c00) >> 10;
uint32_t sig = (ival & 0x3ff) >> 0;
uint32_t ret;
if (exp == 0) {
if (sig == 0) {
sign = sign << 31;
ret = sign | exp | sig;
}
else {
int n_bit = 1;
uint16_t bit = 0x0200;
while ((bit & sig) == 0) {
n_bit = n_bit + 1;
bit = bit >> 1;
}
sign = sign << 31;
exp = ((-14 - n_bit + 127) << 23);
sig = ((sig & (~bit)) << n_bit) << (23 - 10);
ret = sign | exp | sig;
}
}
else if (exp == 0x1f) {
if (sig == 0) { // Inf
if (sign == 0)
ret = 0x7f800000;
else
ret = 0xff800000;
}
else // NaN
ret = 0x7fc00000 | (sign << 31) | (sig << (23 - 10));
}
else {
sign = sign << 31;
exp = ((exp - 15 + 127) << 23);
sig = sig << (23 - 10);
ret = sign | exp | sig;
}
float fret;
memcpy(&fret, &ret, sizeof(float));
return fret;
}
// float to half algorithm from:
// "Fast Half Float Conversion" by Jeroen van der Zijp
// ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf
//
// With adjustments for round-to-nearest, ties to even.
static uint16_t basetable[512] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0400, 0x0800, 0x0c00, 0x1000, 0x1400, 0x1800, 0x1c00, 0x2000,
0x2400, 0x2800, 0x2c00, 0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x4400, 0x4800, 0x4c00,
0x5000, 0x5400, 0x5800, 0x5c00, 0x6000, 0x6400, 0x6800, 0x6c00, 0x7000, 0x7400, 0x7800,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00, 0x7c00,
0x7c00, 0x7c00, 0x7c00, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8400, 0x8800, 0x8c00, 0x9000, 0x9400,
0x9800, 0x9c00, 0xa000, 0xa400, 0xa800, 0xac00, 0xb000, 0xb400, 0xb800, 0xbc00, 0xc000,
0xc400, 0xc800, 0xcc00, 0xd000, 0xd400, 0xd800, 0xdc00, 0xe000, 0xe400, 0xe800, 0xec00,
0xf000, 0xf400, 0xf800, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00,
0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00, 0xfc00};
static uint8_t shifttable[512] = {
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f,
0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0x0d, 0x0d, 0x0d, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x0d, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13,
0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0d};
static inline uint16_t float_to_half(float param) JL_NOTSAFEPOINT
{
uint32_t f;
memcpy(&f, ¶m, sizeof(float));
if (isnan(param)) {
uint32_t t = 0x8000 ^ (0x8000 & ((uint16_t)(f >> 0x10)));
return t ^ ((uint16_t)(f >> 0xd));
}
int i = ((f & ~0x007fffff) >> 23);
uint8_t sh = shifttable[i];
f &= 0x007fffff;
// If `val` is subnormal, the tables are set up to force the
// result to 0, so the significand has an implicit `1` in the
// cases we care about.
f |= 0x007fffff + 0x1;
uint16_t h = (uint16_t)(basetable[i] + ((f >> sh) & 0x03ff));
// round
// NOTE: we maybe should ignore NaNs here, but the payload is
// getting truncated anyway so "rounding" it might not matter
int nextbit = (f >> (sh - 1)) & 1;
if (nextbit != 0 && (h & 0x7C00) != 0x7C00) {
// Round halfway to even or check lower bits
if ((h & 1) == 1 || (f & ((1 << (sh - 1)) - 1)) != 0)
h += UINT16_C(1);
}
return h;
}
static inline uint16_t double_to_half(double param) JL_NOTSAFEPOINT
{
float temp = (float)param;
uint32_t tempi;
memcpy(&tempi, &temp, sizeof(temp));
// if Float16(res) is subnormal
if ((tempi&0x7fffffffu) < 0x38800000u) {
// shift so that the mantissa lines up where it would for normal Float16
uint32_t shift = 113u-((tempi & 0x7f800000u)>>23u);
if (shift<23u) {
tempi |= 0x00800000; // set implicit bit
tempi >>= shift;
}
}
// if we are halfway between 2 Float16 values
if ((tempi & 0x1fffu) == 0x1000u) {
memcpy(&tempi, &temp, sizeof(temp));
// adjust the value by 1 ULP in the direction that will make Float16(temp) give the right answer
tempi += (fabs(temp) < fabs(param)) - (fabs(param) < fabs(temp));
memcpy(&temp, &tempi, sizeof(temp));
}
return float_to_half(temp);
}
// x86-specific helpers for emulating the (B)Float16 ABI
#if defined(_CPU_X86_) || defined(_CPU_X86_64_)
#include <xmmintrin.h>
__attribute__((unused)) static inline __m128 return_in_xmm(uint16_t input) JL_NOTSAFEPOINT {
__m128 xmm_output;
asm (
"movd %[input], %%xmm0\n\t"
"movss %%xmm0, %[xmm_output]\n\t"
: [xmm_output] "=x" (xmm_output)
: [input] "r" ((uint32_t)input)
: "xmm0"
);
return xmm_output;
}
__attribute__((unused)) static inline uint16_t take_from_xmm(__m128 xmm_input) JL_NOTSAFEPOINT {
uint32_t output;
asm (
"movss %[xmm_input], %%xmm0\n\t"
"movd %%xmm0, %[output]\n\t"
: [output] "=r" (output)
: [xmm_input] "x" (xmm_input)
: "xmm0"
);
return (uint16_t)output;
}
#endif
// float16 conversion API
// for use in APInt and other soft-float ABIs (i.e. without the ABI shenanigans from below)
JL_DLLEXPORT uint16_t julia_float_to_half(float param) {
return float_to_half(param);
}
JL_DLLEXPORT uint16_t julia_double_to_half(double param) {
return double_to_half(param);
}
JL_DLLEXPORT float julia_half_to_float(uint16_t param) {
return half_to_float(param);
}
// starting with GCC 12 and Clang 15, we have _Float16 on most platforms
// (but not on Windows; this may be a bug in the MSYS2 GCC compilers)
#if ((defined(__GNUC__) && __GNUC__ > 11) || \
(defined(__clang__) && __clang_major__ > 14)) && \
!defined(_CPU_PPC64_) && !defined(_CPU_PPC_) && \
!defined(_OS_WINDOWS_)
#define FLOAT16_TYPE _Float16
#define FLOAT16_TO_UINT16(x) (*(uint16_t*)&(x))
#define FLOAT16_FROM_UINT16(x) (*(_Float16*)&(x))
// on older compilers, we need to emulate the platform-specific ABI
#elif defined(_CPU_X86_) || (defined(_CPU_X86_64_) && !defined(_OS_WINDOWS_))
// on x86, we can use __m128; except on Windows where x64 calling
// conventions expect to pass __m128 by reference.
#define FLOAT16_TYPE __m128
#define FLOAT16_TO_UINT16(x) take_from_xmm(x)
#define FLOAT16_FROM_UINT16(x) return_in_xmm(x)
#elif defined(_CPU_PPC64_) || defined(_CPU_PPC_)
// on PPC, pass Float16 as if it were an integer, similar to the old x86 ABI
// before _Float16
#define FLOAT16_TYPE uint16_t
#define FLOAT16_TO_UINT16(x) (x)
#define FLOAT16_FROM_UINT16(x) (x)
#else
// otherwise, pass using floating-point calling conventions
#define FLOAT16_TYPE float
#define FLOAT16_TO_UINT16(x) ((uint16_t)*(uint32_t*)&(x))
#define FLOAT16_FROM_UINT16(x) ({ uint32_t tmp = (uint32_t)(x); *(float*)&tmp; })
#endif
JL_DLLEXPORT float julia__gnu_h2f_ieee(FLOAT16_TYPE param)
{
uint16_t param16 = FLOAT16_TO_UINT16(param);
return half_to_float(param16);
}
JL_DLLEXPORT FLOAT16_TYPE julia__gnu_f2h_ieee(float param)
{
uint16_t res = float_to_half(param);
return FLOAT16_FROM_UINT16(res);
}
JL_DLLEXPORT FLOAT16_TYPE julia__truncdfhf2(double param)
{
uint16_t res = double_to_half(param);
return FLOAT16_FROM_UINT16(res);
}
// bfloat16 conversion helpers
static inline uint16_t float_to_bfloat(float param) JL_NOTSAFEPOINT
{
if (isnan(param))
return 0x7fc0;
uint32_t bits = *((uint32_t*) ¶m);
// round to nearest even
bits += 0x7fff + ((bits >> 16) & 1);
return (uint16_t)(bits >> 16);
}
static inline uint16_t double_to_bfloat(double param) JL_NOTSAFEPOINT
{
float temp = (float)param;
uint32_t tempi;
memcpy(&tempi, &temp, sizeof(temp));
// bfloat16 uses the same exponent as float32, so we don't need special handling
// for subnormals when truncating float64 to bfloat16.
// if we are halfway between 2 bfloat16 values
if ((tempi & 0x1ffu) == 0x100u) {
// adjust the value by 1 ULP in the direction that will make bfloat16(temp) give the right answer
tempi += (fabs(temp) < fabs(param)) - (fabs(param) < fabs(temp));
memcpy(&temp, &tempi, sizeof(temp));
}
return float_to_bfloat(temp);
}
static inline float bfloat_to_float(uint16_t param) JL_NOTSAFEPOINT
{
uint32_t bits = ((uint32_t)param) << 16;
float result;
memcpy(&result, &bits, sizeof(result));
return result;
}
// bfloat16 conversion API
// for use in APInt (without the ABI shenanigans from below)
uint16_t julia_float_to_bfloat(float param) {
return float_to_bfloat(param);
}
float julia_bfloat_to_float(uint16_t param) {
return bfloat_to_float(param);
}
// starting with GCC 13 and Clang 17, we have __bf16 on most platforms
// (but not on Windows; this may be a bug in the MSYS2 GCC compilers)
#if ((defined(__GNUC__) && __GNUC__ > 12) || \
(defined(__clang__) && __clang_major__ > 16)) && \
!defined(_CPU_PPC64_) && !defined(_CPU_PPC_) && \
!defined(_OS_WINDOWS_)
#define BFLOAT16_TYPE __bf16
#define BFLOAT16_TO_UINT16(x) (*(uint16_t*)&(x))
#define BFLOAT16_FROM_UINT16(x) (*(__bf16*)&(x))
// on older compilers, we need to emulate the platform-specific ABI.
// for more details, see similar code above that deals with Float16.
#elif defined(_CPU_X86_) || (defined(_CPU_X86_64_) && !defined(_OS_WINDOWS_))
#define BFLOAT16_TYPE __m128
#define BFLOAT16_TO_UINT16(x) take_from_xmm(x)
#define BFLOAT16_FROM_UINT16(x) return_in_xmm(x)
#elif defined(_CPU_PPC64_) || defined(_CPU_PPC_)
#define BFLOAT16_TYPE uint16_t
#define BFLOAT16_TO_UINT16(x) (x)
#define BFLOAT16_FROM_UINT16(x) (x)
#else
#define BFLOAT16_TYPE float
#define BFLOAT16_TO_UINT16(x) ((uint16_t)*(uint32_t*)&(x))
#define BFLOAT16_FROM_UINT16(x) ({ uint32_t tmp = (uint32_t)(x); *(float*)&tmp; })
#endif
JL_DLLEXPORT BFLOAT16_TYPE julia__truncsfbf2(float param) JL_NOTSAFEPOINT
{
uint16_t res = float_to_bfloat(param);
return BFLOAT16_FROM_UINT16(res);
}
JL_DLLEXPORT BFLOAT16_TYPE julia__truncdfbf2(double param) JL_NOTSAFEPOINT
{
uint16_t res = double_to_bfloat(param);
return BFLOAT16_FROM_UINT16(res);
}
// run time version of bitcast intrinsic
JL_DLLEXPORT jl_value_t *jl_bitcast(jl_value_t *ty, jl_value_t *v)
{
JL_TYPECHK(bitcast, datatype, ty);
if (!jl_is_concrete_type(ty) || !jl_is_primitivetype(ty))
jl_error("bitcast: target type not a leaf primitive type");
if (!jl_is_primitivetype(jl_typeof(v)))
jl_error("bitcast: value not a primitive type");
if (jl_datatype_size(jl_typeof(v)) != jl_datatype_size(ty))
jl_error("bitcast: argument size does not match size of target type");
if (ty == jl_typeof(v))
return v;
if (ty == (jl_value_t*)jl_bool_type)
return *(uint8_t*)jl_data_ptr(v) & 1 ? jl_true : jl_false;
return jl_new_bits(ty, jl_data_ptr(v));
}
// run time version of pointerref intrinsic (warning: i is not rooted)
JL_DLLEXPORT jl_value_t *jl_pointerref(jl_value_t *p, jl_value_t *i, jl_value_t *align)
{
JL_TYPECHK(pointerref, pointer, p);
JL_TYPECHK(pointerref, long, i)
JL_TYPECHK(pointerref, long, align);
jl_value_t *ety = jl_tparam0(jl_typeof(p));
if (ety == (jl_value_t*)jl_any_type) {
jl_value_t **pp = (jl_value_t**)(jl_unbox_long(p) + (jl_unbox_long(i)-1)*sizeof(void*));
return *pp;
}
else {
if (!is_valid_intrinsic_elptr(ety))
jl_error("pointerref: invalid pointer");
size_t nb = LLT_ALIGN(jl_datatype_size(ety), jl_datatype_align(ety));
char *pp = (char*)jl_unbox_long(p) + (jl_unbox_long(i)-1)*nb;
return jl_new_bits(ety, pp);
}
}
// run time version of pointerset intrinsic (warning: x is not gc-rooted)
JL_DLLEXPORT jl_value_t *jl_pointerset(jl_value_t *p, jl_value_t *x, jl_value_t *i, jl_value_t *align)
{
JL_TYPECHK(pointerset, pointer, p);
JL_TYPECHK(pointerset, long, i);
JL_TYPECHK(pointerset, long, align);
jl_value_t *ety = jl_tparam0(jl_typeof(p));
if (ety == (jl_value_t*)jl_any_type) {
jl_value_t **pp = (jl_value_t**)(jl_unbox_long(p) + (jl_unbox_long(i)-1)*sizeof(void*));
*pp = x;
}
else {
if (!is_valid_intrinsic_elptr(ety))
jl_error("pointerset: invalid pointer");
if (jl_typeof(x) != ety)
jl_type_error("pointerset", ety, x);
size_t elsz = jl_datatype_size(ety);
size_t nb = LLT_ALIGN(elsz, jl_datatype_align(ety));
char *pp = (char*)jl_unbox_long(p) + (jl_unbox_long(i)-1)*nb;
memcpy(pp, x, elsz);
}
return p;
}
JL_DLLEXPORT jl_value_t *jl_atomic_pointerref(jl_value_t *p, jl_value_t *order)
{
JL_TYPECHK(atomic_pointerref, pointer, p);
JL_TYPECHK(atomic_pointerref, symbol, order)
(void)jl_get_atomic_order_checked((jl_sym_t*)order, 1, 0);
jl_value_t *ety = jl_tparam0(jl_typeof(p));
char *pp = (char*)jl_unbox_long(p);
if (ety == (jl_value_t*)jl_any_type) {
return jl_atomic_load((_Atomic(jl_value_t*)*)pp);
}
else {
if (!is_valid_intrinsic_elptr(ety))
jl_error("atomic_pointerref: invalid pointer");
size_t nb = jl_datatype_size(ety);
if ((nb & (nb - 1)) != 0 || nb > MAX_POINTERATOMIC_SIZE)
jl_error("atomic_pointerref: invalid pointer for atomic operation");
return jl_atomic_new_bits(ety, pp);
}
}
JL_DLLEXPORT jl_value_t *jl_atomic_pointerset(jl_value_t *p, jl_value_t *x, jl_value_t *order)
{
JL_TYPECHK(atomic_pointerset, pointer, p);
JL_TYPECHK(atomic_pointerset, symbol, order);
(void)jl_get_atomic_order_checked((jl_sym_t*)order, 0, 1);
jl_value_t *ety = jl_tparam0(jl_typeof(p));
char *pp = (char*)jl_unbox_long(p);
if (ety == (jl_value_t*)jl_any_type) {
jl_atomic_store((_Atomic(jl_value_t*)*)pp, x);
}
else {
if (!is_valid_intrinsic_elptr(ety))
jl_error("atomic_pointerset: invalid pointer");
if (jl_typeof(x) != ety)
jl_type_error("atomic_pointerset", ety, x);
size_t nb = jl_datatype_size(ety);
if ((nb & (nb - 1)) != 0 || nb > MAX_POINTERATOMIC_SIZE)
jl_error("atomic_pointerset: invalid pointer for atomic operation");
jl_atomic_store_bits(pp, x, nb);
}
return p;
}
JL_DLLEXPORT jl_value_t *jl_atomic_pointerswap(jl_value_t *p, jl_value_t *x, jl_value_t *order)
{
JL_TYPECHK(atomic_pointerswap, pointer, p);
JL_TYPECHK(atomic_pointerswap, symbol, order);
(void)jl_get_atomic_order_checked((jl_sym_t*)order, 1, 1);
jl_value_t *ety = jl_tparam0(jl_typeof(p));
jl_value_t *y;
char *pp = (char*)jl_unbox_long(p);
if (ety == (jl_value_t*)jl_any_type) {
y = jl_atomic_exchange((_Atomic(jl_value_t*)*)pp, x);
}
else {
if (!is_valid_intrinsic_elptr(ety))
jl_error("atomic_pointerswap: invalid pointer");
if (jl_typeof(x) != ety)
jl_type_error("atomic_pointerswap", ety, x);
size_t nb = jl_datatype_size(ety);
if ((nb & (nb - 1)) != 0 || nb > MAX_POINTERATOMIC_SIZE)
jl_error("atomic_pointerswap: invalid pointer for atomic operation");
y = jl_atomic_swap_bits(ety, pp, x, nb);
}
return y;
}
JL_DLLEXPORT jl_value_t *jl_atomic_pointermodify(jl_value_t *p, jl_value_t *f, jl_value_t *x, jl_value_t *order)
{
JL_TYPECHK(atomic_pointermodify, pointer, p);
JL_TYPECHK(atomic_pointermodify, symbol, order)
(void)jl_get_atomic_order_checked((jl_sym_t*)order, 1, 1);
jl_value_t *ety = jl_tparam0(jl_typeof(p));
char *pp = (char*)jl_unbox_long(p);
jl_value_t *expected;
if (ety == (jl_value_t*)jl_any_type) {
expected = jl_atomic_load((_Atomic(jl_value_t*)*)pp);
}
else {
if (!is_valid_intrinsic_elptr(ety))
jl_error("atomic_pointermodify: invalid pointer");
size_t nb = jl_datatype_size(ety);
if ((nb & (nb - 1)) != 0 || nb > MAX_POINTERATOMIC_SIZE)
jl_error("atomic_pointermodify: invalid pointer for atomic operation");
expected = jl_atomic_new_bits(ety, pp);
}
jl_value_t **args;
JL_GC_PUSHARGS(args, 2);
args[0] = expected;
while (1) {
args[1] = x;
jl_value_t *y = jl_apply_generic(f, args, 2);
args[1] = y;
if (ety == (jl_value_t*)jl_any_type) {
if (jl_atomic_cmpswap((_Atomic(jl_value_t*)*)pp, &expected, y))
break;
}
else {
//if (!is_valid_intrinsic_elptr(ety)) // handled by jl_atomic_pointerref earlier
// jl_error("atomic_pointermodify: invalid pointer");
if (jl_typeof(y) != ety)
jl_type_error("atomic_pointermodify", ety, y);
size_t nb = jl_datatype_size(ety);
if (jl_atomic_bool_cmpswap_bits(pp, expected, y, nb))
break;
expected = jl_atomic_new_bits(ety, pp);
}
args[0] = expected;
jl_gc_safepoint();
}
// args[0] == expected (old)
// args[1] == y (new)
jl_datatype_t *rettyp = jl_apply_modify_type(ety);
JL_GC_PROMISE_ROOTED(rettyp); // (JL_ALWAYS_LEAFTYPE)
args[0] = jl_new_struct(rettyp, args[0], args[1]);
JL_GC_POP();
return args[0];
}
JL_DLLEXPORT jl_value_t *jl_atomic_pointerreplace(jl_value_t *p, jl_value_t *expected, jl_value_t *x, jl_value_t *success_order_sym, jl_value_t *failure_order_sym)
{
JL_TYPECHK(atomic_pointerreplace, pointer, p);
JL_TYPECHK(atomic_pointerreplace, symbol, success_order_sym);
JL_TYPECHK(atomic_pointerreplace, symbol, failure_order_sym);
enum jl_memory_order success_order = jl_get_atomic_order_checked((jl_sym_t*)success_order_sym, 1, 1);
enum jl_memory_order failure_order = jl_get_atomic_order_checked((jl_sym_t*)failure_order_sym, 1, 0);
if (failure_order > success_order)
jl_atomic_error("atomic_pointerreplace: invalid atomic ordering");
// TODO: filter other invalid orderings
jl_value_t *ety = jl_tparam0(jl_typeof(p));
if (!is_valid_intrinsic_elptr(ety))
jl_error("atomic_pointerreplace: invalid pointer");
char *pp = (char*)jl_unbox_long(p);
jl_datatype_t *rettyp = jl_apply_cmpswap_type(ety);
JL_GC_PROMISE_ROOTED(rettyp); // (JL_ALWAYS_LEAFTYPE)
jl_value_t *result = NULL;
JL_GC_PUSH1(&result);
if (ety == (jl_value_t*)jl_any_type) {
result = expected;
int success;
while (1) {
success = jl_atomic_cmpswap((_Atomic(jl_value_t*)*)pp, &result, x);
if (success || !jl_egal(result, expected))
break;
}
result = jl_new_struct(rettyp, result, success ? jl_true : jl_false);
}
else {
if (jl_typeof(x) != ety)
jl_type_error("atomic_pointerreplace", ety, x);
size_t nb = jl_datatype_size(ety);
if ((nb & (nb - 1)) != 0 || nb > MAX_POINTERATOMIC_SIZE)
jl_error("atomic_pointerreplace: invalid pointer for atomic operation");
int isptr = jl_field_isptr(rettyp, 0);
jl_task_t *ct = jl_current_task;
result = jl_gc_alloc(ct->ptls, isptr ? nb : jl_datatype_size(rettyp), isptr ? ety : (jl_value_t*)rettyp);
int success = jl_atomic_cmpswap_bits((jl_datatype_t*)ety, result, pp, expected, x, nb);
if (isptr) {
jl_value_t *z = jl_gc_alloc(ct->ptls, jl_datatype_size(rettyp), rettyp);
*(jl_value_t**)z = result;
result = z;
nb = sizeof(jl_value_t*);
}
*((uint8_t*)result + nb) = success ? 1 : 0;
}
JL_GC_POP();
return result;
}
JL_DLLEXPORT jl_value_t *jl_atomic_fence(jl_value_t *order_sym)
{
JL_TYPECHK(fence, symbol, order_sym);
enum jl_memory_order order = jl_get_atomic_order_checked((jl_sym_t*)order_sym, 1, 1);
if (order > jl_memory_order_monotonic)
jl_fence();
return jl_nothing;
}
JL_DLLEXPORT jl_value_t *jl_cglobal(jl_value_t *v, jl_value_t *ty)
{
JL_TYPECHK(cglobal, type, ty);
JL_GC_PUSH1(&v);
jl_value_t *rt =
ty == (jl_value_t*)jl_nothing_type ? (jl_value_t*)jl_voidpointer_type : // a common case
(jl_value_t*)jl_apply_type1((jl_value_t*)jl_pointer_type, ty);
JL_GC_PROMISE_ROOTED(rt); // (JL_ALWAYS_LEAFTYPE)
if (!jl_is_concrete_type(rt))
jl_error("cglobal: type argument not concrete");
if (jl_is_tuple(v) && jl_nfields(v) == 1)
v = jl_fieldref(v, 0);
if (jl_is_pointer(v)) {
v = jl_bitcast(rt, v);
JL_GC_POP();
return v;
}
char *f_lib = NULL;
if (jl_is_tuple(v) && jl_nfields(v) > 1) {
jl_value_t *t1 = jl_fieldref(v, 1);
if (jl_is_symbol(t1))
f_lib = jl_symbol_name((jl_sym_t*)t1);
else if (jl_is_string(t1))
f_lib = jl_string_data(t1);
else
JL_TYPECHK(cglobal, symbol, t1)
v = jl_fieldref(v, 0);
}
char *f_name = NULL;
if (jl_is_symbol(v))
f_name = jl_symbol_name((jl_sym_t*)v);
else if (jl_is_string(v))
f_name = jl_string_data(v);
else
JL_TYPECHK(cglobal, symbol, v)
if (!f_lib)
f_lib = (char*)jl_dlfind(f_name);
void *ptr;
jl_dlsym(jl_get_library(f_lib), f_name, &ptr, 1);
jl_value_t *jv = jl_gc_alloc(jl_current_task->ptls, sizeof(void*), rt);
*(void**)jl_data_ptr(jv) = ptr;
JL_GC_POP();
return jv;
}
JL_DLLEXPORT jl_value_t *jl_cglobal_auto(jl_value_t *v) {
return jl_cglobal(v, (jl_value_t*)jl_nothing_type);
}
static inline char signbitbyte(void *a, unsigned bytes) JL_NOTSAFEPOINT
{
// sign bit of an signed number of n bytes, as a byte
return (((signed char*)a)[bytes - 1] < 0) ? ~0 : 0;
}
static inline char usignbitbyte(void *a, unsigned bytes) JL_NOTSAFEPOINT
{
// sign bit of an unsigned number
return 0;
}
static inline unsigned select_by_size(unsigned sz) JL_NOTSAFEPOINT
{
/* choose the right sized function specialization */
switch (sz) {
default: return 0;
case 1: return 1;
case 2: return 2;
case 4: return 3;
case 8: return 4;
case 16: return 5;
}
}
#define SELECTOR_FUNC(intrinsic) \
typedef intrinsic##_t select_##intrinsic##_t[6]; \
static inline intrinsic##_t select_##intrinsic(unsigned sz, const select_##intrinsic##_t list) JL_NOTSAFEPOINT \
{ \
intrinsic##_t thunk = list[select_by_size(sz)]; \
if (!thunk) thunk = list[0]; \
return thunk; \
}
#define fp_select(a, func) \
sizeof(a) <= sizeof(float) ? func##f((float)a) : func(a)
#define fp_select2(a, b, func) \
sizeof(a) <= sizeof(float) ? func##f(a, b) : func(a, b)
// fast-function generators //
// integer input
// OP::Function macro(input)
// name::unique string
// nbits::number of bits
// c_type::c_type corresponding to nbits
#define un_iintrinsic_ctype(OP, name, nbits, c_type) \
static inline void jl_##name##nbits(unsigned runtime_nbits, void *pa, void *pr) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
*(c_type*)pr = OP(a); \
}
// integer input, unsigned output
// OP::Function macro(input)
// name::unique string
// nbits::number of bits
// c_type::c_type corresponding to nbits
#define uu_iintrinsic_ctype(OP, name, nbits, c_type) \
static inline unsigned jl_##name##nbits(unsigned runtime_nbits, void *pa) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
return OP(a); \
}
// floating point
// OP::Function macro(output pointer, input)
// name::unique string
// nbits::number of bits in the *input*
// c_type::c_type corresponding to nbits
#define un_fintrinsic_ctype(OP, name, c_type) \
static inline void name(unsigned osize, jl_value_t *ty, void *pa, void *pr) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
OP(ty, (c_type*)pr, a); \
}
#define un_fintrinsic_half(OP, name) \
static inline void name(unsigned osize, jl_value_t *ty, void *pa, void *pr) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
float A = half_to_float(a); \
if (osize == 16) { \
float R; \
OP(ty, &R, A); \
*(uint16_t*)pr = float_to_half(R); \
} else { \
OP(ty, (uint16_t*)pr, A); \
} \
}
#define un_fintrinsic_bfloat(OP, name) \
static inline void name(unsigned osize, jl_value_t *ty, void *pa, void *pr) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
float A = bfloat_to_float(a); \
if (osize == 16) { \
float R; \
OP(ty, &R, A); \
*(uint16_t*)pr = float_to_bfloat(R); \
} else { \
OP(ty, (uint16_t*)pr, A); \
} \
}
// float or integer inputs
// OP::Function macro(inputa, inputb)
// name::unique string
// nbits::number of bits
// c_type::c_type corresponding to nbits
#define bi_intrinsic_ctype(OP, name, nbits, c_type) \
static void jl_##name##nbits(unsigned runtime_nbits, void *pa, void *pb, void *pr) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
c_type b = *(c_type*)pb; \
*(c_type*)pr = (c_type)OP(a, b); \
}
#define bi_intrinsic_half(OP, name) \
static void jl_##name##16(unsigned runtime_nbits, void *pa, void *pb, void *pr) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
uint16_t b = *(uint16_t*)pb; \
float A = half_to_float(a); \
float B = half_to_float(b); \
runtime_nbits = 16; \
float R = OP(A, B); \
*(uint16_t*)pr = float_to_half(R); \
*(uint16_t*)pr = float_to_half(R); \
}
#define bi_intrinsic_bfloat(OP, name) \
static void jl_##name##bf16(unsigned runtime_nbits, void *pa, void *pb, void *pr) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
uint16_t b = *(uint16_t*)pb; \
float A = bfloat_to_float(a); \
float B = bfloat_to_float(b); \
runtime_nbits = 16; \
float R = OP(A, B); \
*(uint16_t*)pr = float_to_bfloat(R); \
}
// float or integer inputs, bool output
// OP::Function macro(inputa, inputb)
// name::unique string
// nbits::number of bits
// c_type::c_type corresponding to nbits
#define bool_intrinsic_ctype(OP, name, nbits, c_type) \
static int jl_##name##nbits(unsigned runtime_nbits, void *pa, void *pb) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
c_type b = *(c_type*)pb; \
return OP(a, b); \
}
#define bool_intrinsic_half(OP, name) \
static int jl_##name##16(unsigned runtime_nbits, void *pa, void *pb) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
uint16_t b = *(uint16_t*)pb; \
float A = half_to_float(a); \
float B = half_to_float(b); \
runtime_nbits = 16; \
return OP(A, B); \
}
#define bool_intrinsic_bfloat(OP, name) \
static int jl_##name##bf16(unsigned runtime_nbits, void *pa, void *pb) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
uint16_t b = *(uint16_t*)pb; \
float A = bfloat_to_float(a); \
float B = bfloat_to_float(b); \
runtime_nbits = 16; \
return OP(A, B); \
}
// integer inputs, with precondition test
// OP::Function macro(inputa, inputb)
// name::unique string
// nbits::number of bits
// c_type::c_type corresponding to nbits
#define checked_intrinsic_ctype(CHECK_OP, OP, name, nbits, c_type) \
static int jl_##name##nbits(unsigned runtime_nbits, void *pa, void *pb, void *pr) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
c_type b = *(c_type*)pb; \
*(c_type*)pr = (c_type)OP(a, b); \
return CHECK_OP(c_type, a, b); \
}
// float inputs
// OP::Function macro(inputa, inputb, inputc)
// name::unique string
// nbits::number of bits
// c_type::c_type corresponding to nbits
#define ter_intrinsic_ctype(OP, name, nbits, c_type) \
static void jl_##name##nbits(unsigned runtime_nbits, void *pa, void *pb, void *pc, void *pr) JL_NOTSAFEPOINT \
{ \
c_type a = *(c_type*)pa; \
c_type b = *(c_type*)pb; \
c_type c = *(c_type*)pc; \
*(c_type*)pr = (c_type)OP(a, b, c); \
}
#define ter_intrinsic_half(OP, name) \
static void jl_##name##16(unsigned runtime_nbits, void *pa, void *pb, void *pc, void *pr) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
uint16_t b = *(uint16_t*)pb; \
uint16_t c = *(uint16_t*)pc; \
float A = half_to_float(a); \
float B = half_to_float(b); \
float C = half_to_float(c); \
runtime_nbits = 16; \
float R = OP(A, B, C); \
*(uint16_t*)pr = float_to_half(R); \
*(uint16_t*)pr = float_to_half(R); \
}
#define ter_intrinsic_bfloat(OP, name) \
static void jl_##name##bf16(unsigned runtime_nbits, void *pa, void *pb, void *pc, void *pr) JL_NOTSAFEPOINT \
{ \
uint16_t a = *(uint16_t*)pa; \
uint16_t b = *(uint16_t*)pb; \
uint16_t c = *(uint16_t*)pc; \
float A = bfloat_to_float(a); \
float B = bfloat_to_float(b); \
float C = bfloat_to_float(c); \
runtime_nbits = 16; \
float R = OP(A, B, C); \
*(uint16_t*)pr = float_to_bfloat(R); \
}
// unary operator generator //
typedef void (*intrinsic_1_t)(unsigned, void*, void*);
SELECTOR_FUNC(intrinsic_1)
#define un_iintrinsic(name, u) \
JL_DLLEXPORT jl_value_t *jl_##name(jl_value_t *a) \
{ \
return jl_iintrinsic_1(a, #name, u##signbitbyte, jl_intrinsiclambda_ty1, name##_list); \
}
#define un_iintrinsic_fast(LLVMOP, OP, name, u) \
un_iintrinsic_ctype(OP, name, 8, u##int##8_t) \
un_iintrinsic_ctype(OP, name, 16, u##int##16_t) \
un_iintrinsic_ctype(OP, name, 32, u##int##32_t) \
un_iintrinsic_ctype(OP, name, 64, u##int##64_t) \
static const select_intrinsic_1_t name##_list = { \
LLVMOP, \
jl_##name##8, \
jl_##name##16, \
jl_##name##32, \
jl_##name##64, \
}; \
un_iintrinsic(name, u)
#define un_iintrinsic_slow(LLVMOP, name, u) \
static const select_intrinsic_1_t name##_list = { \
LLVMOP \
}; \
un_iintrinsic(name, u)
typedef unsigned (*intrinsic_u1_t)(unsigned, void*);
SELECTOR_FUNC(intrinsic_u1)
#define uu_iintrinsic(name, u) \
JL_DLLEXPORT jl_value_t *jl_##name(jl_value_t *a) \
{ \
return jl_iintrinsic_1(a, #name, u##signbitbyte, jl_intrinsiclambda_u1, name##_list); \
}
#define uu_iintrinsic_fast(LLVMOP, OP, name, u) \
uu_iintrinsic_ctype(OP, name, 8, u##int##8_t) \
uu_iintrinsic_ctype(OP, name, 16, u##int##16_t) \
uu_iintrinsic_ctype(OP, name, 32, u##int##32_t) \
uu_iintrinsic_ctype(OP, name, 64, u##int##64_t) \
static const select_intrinsic_u1_t name##_list = { \
LLVMOP, \
jl_##name##8, \
jl_##name##16, \
jl_##name##32, \
jl_##name##64, \
}; \
uu_iintrinsic(name, u)
#define uu_iintrinsic_slow(LLVMOP, name, u) \
static const select_intrinsic_u1_t name##_list = { \
LLVMOP \
}; \
uu_iintrinsic(name, u)
static inline
jl_value_t *jl_iintrinsic_1(jl_value_t *a, const char *name,
char (*getsign)(void*, unsigned),
jl_value_t *(*lambda1)(jl_value_t*, void*, unsigned, unsigned, const void*), const void *list)
{
jl_value_t *ty = jl_typeof(a);
if (!jl_is_primitivetype(ty))
jl_errorf("%s: value is not a primitive type", name);
void *pa = jl_data_ptr(a);
unsigned isize = jl_datatype_size(jl_typeof(a));
unsigned isize2 = next_power_of_two(isize);
unsigned osize = jl_datatype_size(ty);
unsigned osize2 = next_power_of_two(osize);
if (isize2 > osize2)
osize2 = isize2;
if (osize2 > isize || isize2 > isize) {
/* if needed, round type up to a real c-type and set/clear the unused bits */
void *pa2;
pa2 = alloca(osize2);
/* TODO: this memcpy assumes little-endian,
* for big-endian, need to align the copy to the other end */ \
memcpy(pa2, pa, isize);
memset((char*)pa2 + isize, getsign(pa, isize), osize2 - isize);