Skip to content

Commit 69fb82d

Browse files
authored
float32 otherlib (#2492)
1 parent dd07e23 commit 69fb82d

File tree

32 files changed

+2828
-252
lines changed

32 files changed

+2828
-252
lines changed

dune

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@
347347
%{dep:middle_end/flambda2/import/flambda2_import.a}
348348
%{dep:middle_end/flambda2/ui/flambda2_ui.a}
349349
%{dep:middle_end/flambda2/algorithms/flambda2_algorithms.a}
350+
%{dep:middle_end/flambda2/numbers/floats/flambda2_floats.a}
350351
%{dep:middle_end/flambda2/numbers/flambda2_numbers.a}
351352
%{dep:middle_end/flambda2/kinds/flambda2_kinds.a}
352353
%{dep:middle_end/flambda2/identifiers/flambda2_identifiers.a}
@@ -383,6 +384,7 @@
383384
%{dep:middle_end/flambda2/import/flambda2_import.cma}
384385
%{dep:middle_end/flambda2/ui/flambda2_ui.cma}
385386
%{dep:middle_end/flambda2/algorithms/flambda2_algorithms.cma}
387+
%{dep:middle_end/flambda2/numbers/floats/flambda2_floats.cma}
386388
%{dep:middle_end/flambda2/numbers/flambda2_numbers.cma}
387389
%{dep:middle_end/flambda2/kinds/flambda2_kinds.cma}
388390
%{dep:middle_end/flambda2/identifiers/flambda2_identifiers.cma}
@@ -419,6 +421,7 @@
419421
%{dep:middle_end/flambda2/import/flambda2_import.cmxa}
420422
%{dep:middle_end/flambda2/ui/flambda2_ui.cmxa}
421423
%{dep:middle_end/flambda2/algorithms/flambda2_algorithms.cmxa}
424+
%{dep:middle_end/flambda2/numbers/floats/flambda2_floats.cmxa}
422425
%{dep:middle_end/flambda2/numbers/flambda2_numbers.cmxa}
423426
%{dep:middle_end/flambda2/kinds/flambda2_kinds.cmxa}
424427
%{dep:middle_end/flambda2/identifiers/flambda2_identifiers.cmxa}
@@ -462,12 +465,12 @@
462465
(external/gc-timings/libgc_timings_stubs.a
463466
as
464467
compiler-libs/libgc_timings_stubs_native.a)
465-
(middle_end/flambda2/numbers/libflambda2_numbers_stubs.a
468+
(middle_end/flambda2/numbers/floats/libflambda2_floats_stubs.a
466469
as
467-
compiler-libs/libflambda2_numbers_stubs.a)
468-
(middle_end/flambda2/numbers/libflambda2_numbers_stubs.a
470+
compiler-libs/libflambda2_floats_stubs.a)
471+
(middle_end/flambda2/numbers/floats/libflambda2_floats_stubs.a
469472
as
470-
compiler-libs/libflambda2_numbers_stubs_native.a)
473+
compiler-libs/libflambda2_floats_stubs_native.a)
471474
; for special_dune compat
472475
(ocamloptcomp_with_flambda2.cma as compiler-libs/ocamloptcomp.cma)
473476
(ocamloptcomp_with_flambda2.cmxa as compiler-libs/ocamloptcomp.cmxa)

middle_end/flambda2/numbers/dune

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@
77
(backend bisect_ppx))
88
(flags
99
(:standard -principal -open Flambda2_algorithms -open Flambda2_ui))
10-
(foreign_stubs
11-
(language c)
12-
(names float32_stubs)
13-
(flags
14-
((:include %{project_root}/oc_cflags.sexp)
15-
(:include %{project_root}/sharedlib_cflags.sexp)
16-
(:include %{project_root}/oc_cppflags.sexp))))
1710
(ocamlopt_flags
1811
(:standard -O3 -open Int_replace_polymorphic_compare))
19-
(libraries ocamlcommon flambda2_algorithms flambda2_ui))
20-
21-
(install
22-
(files
23-
(dllflambda2_numbers_stubs.so as stublibs/dllflambda2_numbers_stubs.so))
24-
(section lib)
25-
(package ocaml))
12+
(libraries ocamlcommon flambda2_algorithms flambda2_ui flambda2_floats))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(include_subdirs unqualified)
2+
3+
(library
4+
(name flambda2_floats)
5+
(wrapped true)
6+
(instrumentation
7+
(backend bisect_ppx))
8+
(flags
9+
(:standard -principal))
10+
(foreign_stubs
11+
(language c)
12+
(names float32_stubs)
13+
(flags
14+
((:include %{project_root}/oc_cflags.sexp)
15+
(:include %{project_root}/sharedlib_cflags.sexp)
16+
(:include %{project_root}/oc_cppflags.sexp))))
17+
(ocamlopt_flags
18+
(:standard -O3)))
19+
20+
(install
21+
(files
22+
(dllflambda2_floats_stubs.so as stublibs/dllflambda2_floats_stubs.so))
23+
(section lib)
24+
(package ocaml))

middle_end/flambda2/numbers/float32_stubs.c renamed to middle_end/flambda2/numbers/floats/float32_stubs.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ value compiler_float32_to_float_boxed(value i)
193193
static int compiler_float32_of_hex(const char * s, const char * end, float * res)
194194
{
195195
/* See caml_float_of_hex */
196-
int64_t m = 0; /* the mantissa - top 24 bits at most */
196+
int64_t m = 0; /* the mantissa - top 60 bits at most */
197197
int n_bits = 0; /* total number of bits read */
198198
int m_bits = 0; /* number of bits in mantissa */
199199
int x_bits = 0; /* number of bits after mantissa */
@@ -240,12 +240,12 @@ static int compiler_float32_of_hex(const char * s, const char * end, float * res
240240
else return -1; /* bad digit */
241241
n_bits += 4;
242242
if (d == 0 && m == 0) break; /* leading zeros are skipped */
243-
if (m_bits < 24) {
243+
if (m_bits < 60) {
244244
/* There is still room in m. Add this digit to the mantissa. */
245245
m = (m << 4) + d;
246246
m_bits += 4;
247247
} else {
248-
/* We've already collected 24 significant bits in m.
248+
/* We've already collected 60 significant bits in m.
249249
Now all we care about is whether there is a nonzero bit
250250
after. In this case, round m to odd so that the later
251251
rounding of m to FP produces the correct result. */
@@ -258,7 +258,7 @@ static int compiler_float32_of_hex(const char * s, const char * end, float * res
258258
}
259259
if (n_bits == 0) return -1;
260260
/* Convert mantissa to FP. We use a signed conversion because we can
261-
(m has 24 bits at most) and because it is faster
261+
(m has 60 bits at most) and because it is faster
262262
on several architectures. */
263263
f = (float) (int64_t) m;
264264
/* Adjust exponent to take decimal point and extra digits into account */
@@ -334,3 +334,15 @@ CAMLprim value compiler_float32_format(value fmt, value arg)
334334

335335
return res;
336336
}
337+
338+
// These replace the OCaml runtime versions for use under ocaml/ in the dune build.
339+
// They must have the same name as in the runtime because building ocaml/ with the
340+
// upstream build system calls it by name.
341+
342+
CAMLweakdef value caml_float32_of_string(value vs) {
343+
return compiler_float32_of_string(vs);
344+
}
345+
346+
CAMLweakdef value caml_format_float32(value fmt, value arg) {
347+
return compiler_float32_format(fmt, arg);
348+
}

middle_end/flambda2/numbers/numeric_types.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ end)
320320

321321
module Float32_by_bit_pattern = Float_by_bit_pattern_gen (struct
322322
include Int32
323-
module F32 = Float32
323+
module F32 = Flambda2_floats.Float32
324324

325325
let of_string str = F32.to_bits (F32.of_string str)
326326

ocaml/boot/ocamllex

1.45 KB
Binary file not shown.

ocaml/bytecomp/bytegen.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,8 @@ let rec comp_expr stack_info env exp sz cont =
868868
end
869869
| Lprim((Popaque _ | Pobj_magic _), [arg], _) ->
870870
comp_expr stack_info env arg sz cont
871-
| Lprim((Pbox_float (Pfloat64, _) | Punbox_float Pfloat64), [arg], _) ->
871+
| Lprim((Pbox_float ((Pfloat64 | Pfloat32), _)
872+
| Punbox_float (Pfloat64 | Pfloat32)), [arg], _) ->
872873
comp_expr stack_info env arg sz cont
873874
| Lprim((Pbox_int _ | Punbox_int _), [arg], _) ->
874875
comp_expr stack_info env arg sz cont

ocaml/bytecomp/symtable.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,15 @@ let output_primitive_table outchan =
144144

145145
(* Translate structured constants *)
146146

147+
(* We cannot use the [float32] type in the compiler, so we represent it as an
148+
opaque [Obj.t]. This is sufficient for interfacing with the runtime. *)
149+
external float32_of_string : string -> Obj.t = "caml_float32_of_string"
150+
147151
let rec transl_const = function
148152
Const_base(Const_int i) -> Obj.repr i
149153
| Const_base(Const_char c) -> Obj.repr c
150154
| Const_base(Const_string (s, _, _)) -> Obj.repr s
151-
| Const_base(Const_float32 f)
155+
| Const_base(Const_float32 f) -> float32_of_string f
152156
| Const_base(Const_float f)
153157
| Const_base(Const_unboxed_float f) -> Obj.repr (float_of_string f)
154158
| Const_base(Const_int32 i)

ocaml/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
(run cat %{input-file})))
6868
parser)))
6969
(library_flags -linkall)
70+
(libraries flambda2_floats)
7071
(modules_without_implementation
7172
annot asttypes cmo_format outcometree parsetree debug_event solver_intf mode_intf)
7273
(modules
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
alpha.mli
22
alpha.ml
3+
float32.mli
4+
float32.ml

ocaml/otherlibs/alpha/alpha.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Float32 = Float32

ocaml/otherlibs/alpha/alpha.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Float32 = Float32

ocaml/otherlibs/alpha/dune

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@
4141
(alpha.cmxs as alpha/alpha.cmxs)
4242
(alpha.cma as alpha/alpha.cma)
4343
(alpha.mli as alpha/alpha.mli)
44+
(float32.mli as alpha/float32.mli)
4445
(.alpha.objs/byte/alpha.cmi as alpha/alpha.cmi)
4546
(.alpha.objs/byte/alpha.cmt as alpha/alpha.cmt)
4647
(.alpha.objs/byte/alpha.cmti as alpha/alpha.cmti)
47-
(.alpha.objs/native/alpha.cmx as alpha/alpha.cmx))
48+
(.alpha.objs/native/alpha.cmx as alpha/alpha.cmx)
49+
(.alpha.objs/byte/float32.cmi as alpha/float32.cmi)
50+
(.alpha.objs/byte/float32.cmt as alpha/float32.cmt)
51+
(.alpha.objs/byte/float32.cmti as alpha/float32.cmti)
52+
(.alpha.objs/native/float32.cmx as alpha/float32.cmx))
4853
(section lib)
4954
(package ocaml))

0 commit comments

Comments
 (0)