forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloat32.c
915 lines (796 loc) · 28.2 KB
/
float32.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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* Max Slater, Jane Street */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
/* Needed for uselocale */
#define _XOPEN_SOURCE 700
/* Needed for strtof_l */
#define _GNU_SOURCE
#include <math.h>
#include <float.h>
#include <limits.h>
#include <string.h>
#include "caml/alloc.h"
#include "caml/bigarray.h"
#include "caml/fail.h"
#include "caml/custom.h"
#include "caml/float32.h"
#include "caml/memory.h"
#include "caml/intext.h"
#include "caml/mlvalues.h"
#if defined(HAS_LOCALE) || defined(__MINGW32__)
#if defined(HAS_LOCALE_H) || defined(__MINGW32__)
#include <locale.h>
#endif
#if defined(HAS_XLOCALE_H)
#include <xlocale.h>
#endif
#if defined(_MSC_VER)
#ifndef locale_t
#define locale_t _locale_t
#endif
#ifndef freelocale
#define freelocale _free_locale
#endif
#ifndef strtof_l
#define strtof_l _strtof_l
#endif
#endif
#endif /* defined(HAS_LOCALE) */
CAML_STATIC_ASSERT(sizeof(float) == sizeof(int32_t));
#define Max_custom_array_wosize (Max_wosize - 1)
#define Max_unboxed_float32_array_wosize (Max_custom_array_wosize * (sizeof(intnat) / sizeof(float)))
intnat caml_float32_compare_unboxed(float f, float g)
{
/* If one or both of f and g is NaN, order according to the convention
NaN = NaN and NaN < x for all other floats x. */
/* This branchless implementation is from GPR#164.
Note that [f == f] if and only if f is not NaN.
We expand each subresult of the expression to
avoid sign-extension on 64bit. GPR#2250. */
intnat res =
(intnat)(f > g) - (intnat)(f < g) + (intnat)(f == f) - (intnat)(g == g);
return res;
}
static int float32_cmp(value v1, value v2)
{
return caml_float32_compare_unboxed(Float32_val(v1), Float32_val(v2));
}
static intnat float32_hash(value v)
{
union {
float f;
uint32_t i;
} u;
uint32_t n;
u.f = Float32_val(v); n = u.i;
/* Normalize NaNs */
if ((n & 0x7F800000) == 0x7F800000 && (n & 0x007FFFFF) != 0) {
n = 0x7F800001;
}
/* Normalize -0 into +0 */
else if (n == 0x80000000) {
n = 0;
}
return n;
}
static uintnat float32_deserialize(void *dst)
{
*((float *)dst) = caml_deserialize_float_4();
return 4;
}
static void float32_serialize(value v, uintnat *bsize_32,
uintnat *bsize_64)
{
caml_serialize_float_4(Float32_val(v));
*bsize_32 = *bsize_64 = 4;
}
static const struct custom_fixed_length float32_length = {4, 4};
CAMLexport struct custom_operations caml_float32_ops = {
"_f32",
custom_finalize_default,
float32_cmp,
float32_hash,
float32_serialize,
float32_deserialize,
custom_compare_ext_default,
&float32_length
};
CAMLexport value caml_copy_float32(float f)
{
value res = caml_alloc_custom(&caml_float32_ops, 4, 0, 1);
Float32_val(res) = f;
return res;
}
CAMLprim value caml_float32_of_float(value d)
{
return caml_copy_float32((float)Double_val(d));
}
CAMLprim value caml_float_of_float32(value f)
{
return caml_copy_double((double)Float32_val(f));
}
CAMLprim value caml_int_of_float32(value f)
{
return Val_long((intnat)Float32_val(f));
}
CAMLprim value caml_float32_of_int(value n)
{
return caml_copy_float32((float)Long_val(n));
}
CAMLprim value caml_neg_float32(value f)
{
return caml_copy_float32(-Float32_val(f));
}
CAMLprim value caml_abs_float32(value f)
{
return caml_copy_float32(fabsf(Float32_val(f)));
}
CAMLprim value caml_add_float32(value f, value g)
{
return caml_copy_float32(Float32_val(f) + Float32_val(g));
}
CAMLprim value caml_sub_float32(value f, value g)
{
return caml_copy_float32(Float32_val(f) - Float32_val(g));
}
CAMLprim value caml_mul_float32(value f, value g)
{
return caml_copy_float32(Float32_val(f) * Float32_val(g));
}
CAMLprim value caml_div_float32(value f, value g)
{
return caml_copy_float32(Float32_val(f) / Float32_val(g));
}
CAMLprim value caml_sqrt_float32(value f)
{
return caml_copy_float32(sqrtf(Float32_val(f)));
}
CAMLprim value caml_float32_compare(value vf, value vg)
{
return Val_int(caml_float32_compare_unboxed(Float32_val(vf), Float32_val(vg)));
}
#define DEFINE_NAN_CMP(op) \
(value f, value g) \
{ \
return Val_bool(Float32_val(f) op Float32_val(g)); \
}
CAMLprim value caml_eq_float32 DEFINE_NAN_CMP(==)
CAMLprim value caml_neq_float32 DEFINE_NAN_CMP(!=)
CAMLprim value caml_le_float32 DEFINE_NAN_CMP(<=)
CAMLprim value caml_lt_float32 DEFINE_NAN_CMP(<)
CAMLprim value caml_ge_float32 DEFINE_NAN_CMP(>=)
CAMLprim value caml_gt_float32 DEFINE_NAN_CMP(>)
#define DEFINE_BYTE_UNOP(op) \
(value f) \
{ \
return caml_copy_float32(op(Float32_val(f))); \
}
CAMLprim value caml_sqrt_float32_bytecode DEFINE_BYTE_UNOP(sqrtf)
CAMLprim value caml_cbrt_float32_bytecode DEFINE_BYTE_UNOP(cbrtf)
CAMLprim value caml_exp_float32_bytecode DEFINE_BYTE_UNOP(expf)
CAMLprim value caml_exp2_float32_bytecode DEFINE_BYTE_UNOP(exp2f)
CAMLprim value caml_log_float32_bytecode DEFINE_BYTE_UNOP(logf)
CAMLprim value caml_log10_float32_bytecode DEFINE_BYTE_UNOP(log10f)
CAMLprim value caml_log2_float32_bytecode DEFINE_BYTE_UNOP(log2f)
CAMLprim value caml_expm1_float32_bytecode DEFINE_BYTE_UNOP(expm1f)
CAMLprim value caml_log1p_float32_bytecode DEFINE_BYTE_UNOP(log1pf)
CAMLprim value caml_cos_float32_bytecode DEFINE_BYTE_UNOP(cosf)
CAMLprim value caml_sin_float32_bytecode DEFINE_BYTE_UNOP(sinf)
CAMLprim value caml_tan_float32_bytecode DEFINE_BYTE_UNOP(tanf)
CAMLprim value caml_acos_float32_bytecode DEFINE_BYTE_UNOP(acosf)
CAMLprim value caml_asin_float32_bytecode DEFINE_BYTE_UNOP(asinf)
CAMLprim value caml_atan_float32_bytecode DEFINE_BYTE_UNOP(atanf)
CAMLprim value caml_cosh_float32_bytecode DEFINE_BYTE_UNOP(coshf)
CAMLprim value caml_sinh_float32_bytecode DEFINE_BYTE_UNOP(sinhf)
CAMLprim value caml_tanh_float32_bytecode DEFINE_BYTE_UNOP(tanhf)
CAMLprim value caml_acosh_float32_bytecode DEFINE_BYTE_UNOP(acoshf)
CAMLprim value caml_asinh_float32_bytecode DEFINE_BYTE_UNOP(asinhf)
CAMLprim value caml_atanh_float32_bytecode DEFINE_BYTE_UNOP(atanhf)
CAMLprim value caml_erf_float32_bytecode DEFINE_BYTE_UNOP(erff)
CAMLprim value caml_erfc_float32_bytecode DEFINE_BYTE_UNOP(erfcf)
CAMLprim value caml_trunc_float32_bytecode DEFINE_BYTE_UNOP(truncf)
CAMLprim value caml_round_float32_bytecode DEFINE_BYTE_UNOP(roundf)
CAMLprim value caml_ceil_float32_bytecode DEFINE_BYTE_UNOP(ceilf)
CAMLprim value caml_floor_float32_bytecode DEFINE_BYTE_UNOP(floorf)
#define DEFINE_BYTE_BINOP(op) \
(value f, value g) \
{ \
return caml_copy_float32(op(Float32_val(f),Float32_val(g))); \
}
CAMLprim value caml_atan2_float32_bytecode DEFINE_BYTE_BINOP(atan2f)
CAMLprim value caml_hypot_float32_bytecode DEFINE_BYTE_BINOP(hypotf)
CAMLprim value caml_nextafter_float32_bytecode DEFINE_BYTE_BINOP(nextafterf)
CAMLprim value caml_copysign_float32_bytecode DEFINE_BYTE_BINOP(copysignf)
CAMLprim value caml_fmod_float32_bytecode DEFINE_BYTE_BINOP(fmodf)
CAMLprim value caml_power_float32_bytecode DEFINE_BYTE_BINOP(powf)
CAMLprim value caml_fma_float32_bytecode(value f, value g, value h)
{
return caml_copy_float32(fmaf(Float32_val(f), Float32_val(g), Float32_val(h)));
}
float caml_float32_of_int64(int64_t i) {
return (float)i;
}
CAMLprim value caml_float32_of_int64_bytecode(value i) {
return caml_copy_float32(caml_float32_of_int64(Int64_val(i)));
}
int64_t caml_float32_to_int64(float f) {
return (int64_t)f;
}
CAMLprim value caml_float32_to_int64_bytecode(value f) {
return caml_copy_int64(caml_float32_to_int64(Float32_val(f)));
}
float caml_float32_of_bits(int32_t bits)
{
union { float f; int32_t i; } u;
u.i = bits;
return u.f;
}
CAMLprim value caml_float32_of_bits_bytecode(value bits)
{
return caml_copy_float32(caml_float32_of_bits(Int32_val(bits)));
}
int32_t caml_float32_to_bits(float f)
{
union { float f; int32_t i; } u;
u.f = f;
return u.i;
}
CAMLprim value caml_float32_to_bits_bytecode(value f)
{
return caml_copy_int32(caml_float32_to_bits(Float32_val(f)));
}
float caml_ldexp_float32(float f, intnat i)
{
return ldexpf(f, (int)i);
}
CAMLprim value caml_ldexp_float32_bytecode(value f, value i)
{
return caml_copy_float32(caml_ldexp_float32(Float32_val(f), Int_val(i)));
}
float caml_simd_float32_min(float x, float y) {
return x < y ? x : y;
}
CAMLprim value caml_simd_float32_min_bytecode(value x, value y) {
return Float32_val(x) < Float32_val(y) ? x : y;
}
float caml_simd_float32_max(float x, float y) {
return x > y ? x : y;
}
CAMLprim value caml_simd_float32_max_bytecode(value x, value y) {
return Float32_val(x) > Float32_val(y) ? x : y;
}
int64_t caml_simd_cast_float32_int64(float f)
{
return llrintf(f);
}
CAMLprim value caml_simd_cast_float32_int64_bytecode(value f)
{
return caml_copy_int64(caml_simd_cast_float32_int64(Float32_val(f)));
}
float caml_simd_float32_round_current(float f) {
return rintf(f);
}
CAMLprim value caml_simd_float32_round_current_bytecode(value f) {
return caml_copy_float32(caml_simd_float32_round_current(Float32_val(f)));
}
float caml_simd_float32_round_neg_inf(float f) {
return floorf(f);
}
CAMLprim value caml_simd_float32_round_neg_inf_bytecode(value f) {
return caml_copy_float32(caml_simd_float32_round_neg_inf(Float32_val(f)));
}
float caml_simd_float32_round_pos_inf(float f) {
return ceilf(f);
}
CAMLprim value caml_simd_float32_round_pos_inf_bytecode(value f) {
return caml_copy_float32(caml_simd_float32_round_pos_inf(Float32_val(f)));
}
float caml_simd_float32_round_towards_zero(float f) {
return truncf(f);
}
CAMLprim value caml_simd_float32_round_towards_zero_bytecode(value f) {
return caml_copy_float32(caml_simd_float32_round_towards_zero(Float32_val(f)));
}
enum { FP_normal, FP_subnormal, FP_zero, FP_infinite, FP_nan };
value caml_classify_float32(float vf)
{
union { float f; uint32_t i; } u;
uint32_t n;
uint32_t e;
u.f = vf;
n = u.i << 1; /* shift sign bit off */
if (n == 0) return Val_int(FP_zero);
e = n >> 24; /* extract exponent */
if (e == 0) return Val_int(FP_subnormal);
if (e == 0xff) {
if (n << 8 == 0) /* shift exponent off */
return Val_int(FP_infinite);
else
return Val_int(FP_nan);
}
return Val_int(FP_normal);
}
CAMLprim value caml_classify_float32_bytecode(value f)
{
return caml_classify_float32(Float32_val(f));
}
value caml_signbit_float32(float f)
{
return Val_bool(signbit(f));
}
CAMLprim value caml_signbit_float32_bytecode(value f)
{
return caml_signbit_float32(Float32_val(f));
}
CAMLprim value caml_frexp_float32(value f)
{
CAMLparam0 ();
CAMLlocal1 (mantissa);
value res;
int exponent;
mantissa = caml_copy_float32(frexpf(Float32_val(f), &exponent));
res = caml_alloc_small(2, 0);
Field(res, 0) = mantissa;
Field(res, 1) = Val_int(exponent);
CAMLreturn (res);
}
CAMLprim value caml_modf_float32(value f)
{
CAMLparam0 ();
CAMLlocal2 (quo, rem);
value res;
float frem;
quo = caml_copy_float32(modff(Float32_val(f), &frem));
rem = caml_copy_float32(frem);
res = caml_alloc_small(2, 0);
Field(res, 0) = quo;
Field(res, 1) = rem;
CAMLreturn (res);
}
/* The functions on bytes, strings, and bigstrings (ba_uint8) are only used
in bytecode builds. Otherwise, the flambda-backend compiler translates
the corresponding primitives directly to load/store instructions. */
CAMLprim value caml_string_getf32(value str, value index)
{
#ifdef ARCH_BIG_ENDIAN
caml_failwith(
"Raw float32 load/store is not supported on big-endian architectures.");
#else
intnat idx = Long_val(index);
if (idx < 0 || idx + 3 >= caml_string_length(str)) caml_array_bound_error();
float res = *(float*)&Byte_u(str, idx);
return caml_copy_float32(res);
#endif
}
CAMLprim value caml_bytes_getf32(value str, value index)
{
return caml_string_getf32(str, index);
}
CAMLprim value caml_bytes_setf32(value str, value index, value newval)
{
#ifdef ARCH_BIG_ENDIAN
caml_failwith(
"Raw float32 load/store is not supported on big-endian architectures.");
#else
intnat idx = Long_val(index);
if (idx < 0 || idx + 3 >= caml_string_length(str)) caml_array_bound_error();
*(float*)&Byte_u(str, idx) = Float32_val(newval);
return Val_unit;
#endif
}
CAMLprim value caml_ba_uint8_getf32(value vb, value vind)
{
#ifdef ARCH_BIG_ENDIAN
caml_failwith(
"Raw float32 load/store is not supported on big-endian architectures.");
#else
intnat idx = Long_val(vind);
struct caml_ba_array * b = Caml_ba_array_val(vb);
if (idx < 0 || idx >= b->dim[0] - 3) caml_array_bound_error();
float res = *(float*)&Byte_u(b->data, idx);
return caml_copy_float32(res);
#endif
}
CAMLprim value caml_ba_uint8_setf32(value vb, value vind, value newval)
{
#ifdef ARCH_BIG_ENDIAN
caml_failwith(
"Raw float32 load/store is not supported on big-endian architectures.");
#else
intnat idx = Long_val(vind);
struct caml_ba_array * b = Caml_ba_array_val(vb);
if (idx < 0 || idx >= b->dim[0] - 3) caml_array_bound_error();
*(float*)&Byte_u(b->data, idx) = Float32_val(newval);
return Val_unit;
#endif
}
CAMLprim value caml_ba_uint8_getf32_indexed_by_int64(value array, value index);
CAMLprim value caml_ba_uint8_getf32_indexed_by_int32(value array, value index);
CAMLprim value caml_ba_uint8_getf32_indexed_by_nativeint(value array, value index);
CAMLprim value caml_ba_uint8_setf32_indexed_by_int64(value array, value index,
value newval);
CAMLprim value caml_ba_uint8_setf32_indexed_by_int32(value array, value index,
value newval);
CAMLprim value caml_ba_uint8_setf32_indexed_by_nativeint(value array, value index,
value newval);
CAMLprim value caml_string_getf32_indexed_by_int64(value array, value index);
CAMLprim value caml_string_getf32_indexed_by_int32(value array, value index);
CAMLprim value caml_string_getf32_indexed_by_nativeint(value array, value index);
CAMLprim value caml_bytes_getf32_indexed_by_int64(value array, value index);
CAMLprim value caml_bytes_getf32_indexed_by_int32(value array, value index);
CAMLprim value caml_bytes_getf32_indexed_by_nativeint(value array, value index);
CAMLprim value caml_bytes_setf32_indexed_by_int64(value array, value index,
value newval);
CAMLprim value caml_bytes_setf32_indexed_by_int32(value array, value index,
value newval);
CAMLprim value caml_bytes_setf32_indexed_by_nativeint(value array, value index,
value newval);
#define Float32_get_index_by(name, container, index_type, val_func) \
CAMLprim value caml_##container##_getf32_indexed_by_##name(value vb, \
value vind) \
{ \
index_type idx = val_func(vind); \
if (idx != Long_val(Val_long(idx))) caml_array_bound_error(); \
return caml_##container##_getf32(vb, Val_long(idx)); \
}
#define Float32_set_index_by(name, container, index_type, val_func) \
CAMLprim value caml_##container##_setf32_indexed_by_##name(value vb, \
value vind, \
value newval) \
{ \
index_type idx = val_func(vind); \
if (idx != Long_val(Val_long(idx))) caml_array_bound_error(); \
return caml_##container##_setf32(vb, Val_long(idx), newval); \
}
#define Float32_access_index_by(name, index_type, val_func) \
Float32_get_index_by(name, ba_uint8, index_type, val_func) \
Float32_get_index_by(name, string, index_type, val_func) \
Float32_get_index_by(name, bytes, index_type, val_func) \
Float32_set_index_by(name, ba_uint8, index_type, val_func) \
Float32_set_index_by(name, bytes, index_type, val_func)
Float32_access_index_by(int64, int64_t, Int64_val)
Float32_access_index_by(int32, int32_t, Int32_val)
Float32_access_index_by(nativeint, intnat, Nativeint_val)
/* Defined in bigarray.c */
CAMLextern intnat caml_ba_offset(struct caml_ba_array * b, intnat * index);
static value caml_ba_float32_get_aux(value vb, value * vind, int nind)
{
struct caml_ba_array * b = Caml_ba_array_val(vb);
intnat index[CAML_BA_MAX_NUM_DIMS];
int i;
intnat offset;
/* Check number of indices = number of dimensions of array
(maybe not necessary if ML typing guarantees this) */
if (nind != b->num_dims)
caml_invalid_argument("Float32.Bigarray.get: wrong number of indices");
/* Compute offset and check bounds */
for (i = 0; i < b->num_dims; i++) index[i] = Long_val(vind[i]);
offset = caml_ba_offset(b, index);
/* Perform read */
switch ((b->flags) & CAML_BA_KIND_MASK) {
case CAML_BA_FLOAT32:
return caml_copy_float32(((float *) b->data)[offset]);
default:
caml_invalid_argument("Float32.Bigarray.get: wrong kind");
}
}
CAMLprim value caml_ba_float32_get_1(value vb, value vind1)
{
return caml_ba_float32_get_aux(vb, &vind1, 1);
}
CAMLprim value caml_ba_float32_get_2(value vb, value vind1, value vind2)
{
value vind[2];
vind[0] = vind1;
vind[1] = vind2;
return caml_ba_float32_get_aux(vb, vind, 2);
}
CAMLprim value caml_ba_float32_get_3(value vb, value vind1, value vind2,
value vind3)
{
value vind[3];
vind[0] = vind1;
vind[1] = vind2;
vind[2] = vind3;
return caml_ba_float32_get_aux(vb, vind, 3);
}
static value caml_ba_float32_set_aux(value vb, value * vind, intnat nind,
value newval)
{
struct caml_ba_array * b = Caml_ba_array_val(vb);
intnat index[CAML_BA_MAX_NUM_DIMS];
int i;
intnat offset;
/* Check number of indices = number of dimensions of array
(maybe not necessary if ML typing guarantees this) */
if (nind != b->num_dims)
caml_invalid_argument("Float32.Bigarray.set: wrong number of indices");
/* Compute offset and check bounds */
for (i = 0; i < b->num_dims; i++) index[i] = Long_val(vind[i]);
offset = caml_ba_offset(b, index);
/* Perform write */
switch (b->flags & CAML_BA_KIND_MASK) {
case CAML_BA_FLOAT32:
((float *) b->data)[offset] = Float32_val(newval);
break;
default:
caml_invalid_argument("Float32.Bigarray.get: wrong kind");
}
return Val_unit;
}
CAMLprim value caml_ba_float32_set_1(value vb, value vind1, value newval)
{
return caml_ba_float32_set_aux(vb, &vind1, 1, newval);
}
CAMLprim value caml_ba_float32_set_2(value vb, value vind1, value vind2,
value newval)
{
value vind[2];
vind[0] = vind1;
vind[1] = vind2;
return caml_ba_float32_set_aux(vb, vind, 2, newval);
}
CAMLprim value caml_ba_float32_set_3(value vb, value vind1, value vind2,
value vind3, value newval)
{
value vind[3];
vind[0] = vind1;
vind[1] = vind2;
vind[2] = vind3;
return caml_ba_float32_set_aux(vb, vind, 3, newval);
}
/*
OCaml runtime itself doesn't call setlocale, i.e. it is using
standard "C" locale by default, but it is possible that
third-party code loaded into process does.
*/
#ifdef HAS_LOCALE
extern locale_t caml_locale;
#endif
#if defined(_MSC_VER) || defined(__MINGW32__)
/* there is no analogue to uselocale in MSVC so just set locale for thread */
#define USE_LOCALE setlocale(LC_NUMERIC,"C")
#define RESTORE_LOCALE do {} while(0)
#elif defined(HAS_LOCALE)
#define USE_LOCALE locale_t saved_locale = uselocale(caml_locale)
#define RESTORE_LOCALE uselocale(saved_locale)
#else
#define USE_LOCALE do {} while(0)
#define RESTORE_LOCALE do {} while(0)
#endif
CAMLprim value caml_format_float32(value fmt, value arg)
{
/* See caml_format_float */
value res;
float f = Float32_val(arg);
#ifdef HAS_BROKEN_PRINTF
if (isfinite(f)) {
#endif
USE_LOCALE;
res = caml_alloc_sprintf(String_val(fmt), f);
RESTORE_LOCALE;
#ifdef HAS_BROKEN_PRINTF
} else {
if (isnan(f)) {
res = caml_copy_string("nan");
} else {
if (f > 0)
res = caml_copy_string("inf");
else
res = caml_copy_string("-inf");
}
}
#endif
return res;
}
static int caml_float32_of_hex(const char * s, const char * end, float * res)
{
/* See caml_float_of_hex */
int64_t m = 0; /* the mantissa - top 60 bits at most */
int n_bits = 0; /* total number of bits read */
int m_bits = 0; /* number of bits in mantissa */
int x_bits = 0; /* number of bits after mantissa */
int dec_point = -1; /* bit count corresponding to decimal point */
/* -1 if no decimal point seen */
int exp = 0; /* exponent */
char * p; /* for converting the exponent */
float f;
while (s < end) {
char c = *s++;
switch (c) {
case '.':
if (dec_point >= 0) return -1; /* multiple decimal points */
dec_point = n_bits;
break;
case 'p': case 'P': {
long e;
if (*s == 0) return -1; /* nothing after exponent mark */
e = strtol(s, &p, 10);
if (p != end) return -1; /* ill-formed exponent */
/* Handle exponents larger than int by returning 0/infinity directly.
Mind that INT_MIN/INT_MAX are included in the test so as to capture
the overflow case of strtol on Win64 -- long and int have the same
size there. */
if (e <= INT_MIN) {
*res = 0.f;
return 0;
}
else if (e >= INT_MAX) {
*res = m == 0 ? 0.f : HUGE_VALF;
return 0;
}
/* regular exponent value */
exp = e;
s = p; /* stop at next loop iteration */
break;
}
default: { /* Nonzero digit */
int d;
if (c >= '0' && c <= '9') d = c - '0';
else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
else return -1; /* bad digit */
n_bits += 4;
if (d == 0 && m == 0) break; /* leading zeros are skipped */
if (m_bits < 60) {
/* There is still room in m. Add this digit to the mantissa. */
m = (m << 4) + d;
m_bits += 4;
} else {
/* We've already collected 60 significant bits in m.
Now all we care about is whether there is a nonzero bit
after. In this case, round m to odd so that the later
rounding of m to FP produces the correct result. */
if (d != 0) m |= 1; /* round to odd */
x_bits += 4;
}
break;
}
}
}
if (n_bits == 0) return -1;
/* Convert mantissa to FP. We use a signed conversion because we can
(m has 60 bits at most) and because it is faster
on several architectures. */
f = (float) (int64_t) m;
/* Adjust exponent to take decimal point and extra digits into account */
{
int adj = x_bits;
if (dec_point >= 0) adj = adj + (dec_point - n_bits);
/* saturated addition exp + adj */
if (adj > 0 && exp > INT_MAX - adj)
exp = INT_MAX;
else if (adj < 0 && exp < INT_MIN - adj)
exp = INT_MIN;
else
exp = exp + adj;
}
/* Apply exponent if needed */
if (exp != 0) f = ldexpf(f, exp);
/* Done! */
*res = f;
return 0;
}
CAMLprim value caml_float32_of_string(value vs)
{
/* See caml_float_of_string */
char parse_buffer[64];
char * buf, * dst, * end;
const char *src;
mlsize_t len;
int sign;
float f;
/* Remove '_' characters before conversion */
len = caml_string_length(vs);
buf = len < sizeof(parse_buffer) ? parse_buffer : caml_stat_alloc(len + 1);
src = String_val(vs);
dst = buf;
while (len--) {
char c = *src++;
if (c != '_') *dst++ = c;
}
*dst = 0;
if (dst == buf) goto error;
/* Check for hexadecimal FP constant */
src = buf;
sign = 1;
if (*src == '-') { sign = -1; src++; }
else if (*src == '+') { src++; };
if (src[0] == '0' && (src[1] == 'x' || src[1] == 'X')) {
/* Convert using our hexadecimal FP parser */
if (caml_float32_of_hex(src + 2, dst, &f) == -1) goto error;
if (sign < 0) f = -f;
} else {
/* Convert using strtof, which is available when strtod is. */
#if defined(HAS_STRTOD_L) && defined(HAS_LOCALE)
f = strtof_l((const char *) buf, &end, caml_locale);
#else
USE_LOCALE;
f = strtof((const char *) buf, &end);
RESTORE_LOCALE;
#endif /* HAS_STRTOD_L */
if (end != dst) goto error;
}
if (buf != parse_buffer) caml_stat_free(buf);
return caml_copy_float32(f);
error:
if (buf != parse_buffer) caml_stat_free(buf);
caml_failwith("float32_of_string");
return Val_unit; /* not reached */
}
/* Defined in array.c */
CAMLextern int caml_unboxed_array_no_polymorphic_compare(value v1, value v2);
CAMLextern intnat caml_unboxed_array_no_polymorphic_hash(value v);
CAMLextern void caml_unboxed_array_serialize(value v, uintnat* bsize_32, uintnat* bsize_64);
CAMLextern uintnat caml_unboxed_array_deserialize(void* dst);
CAMLextern value caml_make_vect(value len, value init);
CAMLexport struct custom_operations caml_unboxed_float32_array_ops[2] = {
{ "_unboxed_float32_even_array",
custom_finalize_default,
caml_unboxed_array_no_polymorphic_compare,
caml_unboxed_array_no_polymorphic_hash,
caml_unboxed_array_serialize,
caml_unboxed_array_deserialize,
custom_compare_ext_default,
custom_fixed_length_default },
{ "_unboxed_float32_odd_array",
custom_finalize_default,
caml_unboxed_array_no_polymorphic_compare,
caml_unboxed_array_no_polymorphic_hash,
caml_unboxed_array_serialize,
caml_unboxed_array_deserialize,
custom_compare_ext_default,
custom_fixed_length_default },
};
static value caml_make_unboxed_float32_vect0(value len, int local)
{
/* This is only used on 64-bit targets. */
mlsize_t num_elements = Long_val(len);
if (num_elements > Max_unboxed_float32_array_wosize) caml_invalid_argument("Array.make");
/* [num_fields] does not include the custom operations field. */
mlsize_t num_fields = num_elements / 2 + num_elements % 2;
struct custom_operations* ops =
&caml_unboxed_float32_array_ops[num_elements % 2];
if (local)
return caml_alloc_custom_local(ops, num_fields * sizeof(value), 0, 0);
else
return caml_alloc_custom(ops, num_fields * sizeof(value), 0, 0);
}
CAMLprim value caml_make_unboxed_float32_vect(value len)
{
return caml_make_unboxed_float32_vect0(len, 0);
}
CAMLprim value caml_make_local_unboxed_float32_vect(value len)
{
return caml_make_unboxed_float32_vect0(len, 1);
}
CAMLprim value caml_make_unboxed_float32_vect_bytecode(value len)
{
return caml_make_vect(len, caml_copy_float32(0.0f));
}
CAMLprim value caml_unboxed_float32_vect_blit(value a1, value ofs1, value a2,
value ofs2, value n)
{
// Need to skip the custom_operations field
memmove((float *)((uintnat *)a2 + 1) + Long_val(ofs2),
(float *)((uintnat *)a1 + 1) + Long_val(ofs1),
Long_val(n) * sizeof(float));
return Val_unit;
}
CAMLprim value caml_is_boot_compiler(value v) {
(void)v;
return Val_false;
}