Skip to content

Commit d892a4f

Browse files
committed
runtime fns
1 parent f939dd5 commit d892a4f

File tree

4 files changed

+122
-10
lines changed

4 files changed

+122
-10
lines changed

ocaml/runtime/array.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@ static const mlsize_t mlsize_t_max = -1;
3030

3131
/* Unboxed arrays */
3232

33-
static int no_polymorphic_compare(value v1, value v2)
33+
int no_polymorphic_compare(value v1, value v2)
3434
{
3535
caml_failwith("Polymorphic comparison is not permitted for unboxed arrays");
3636
}
3737

38-
static intnat no_polymorphic_hash(value v)
38+
intnat no_polymorphic_hash(value v)
3939
{
4040
caml_failwith("Polymorphic hash is not permitted for unboxed arrays");
4141
}
4242

43-
static void unboxed_array_serialize(value v, uintnat* bsize_32,
44-
uintnat* bsize_64)
43+
void unboxed_array_serialize(value v, uintnat* bsize_32, uintnat* bsize_64)
4544
{
4645
caml_failwith("Marshalling is not yet implemented for unboxed arrays");
4746
}
4847

49-
static uintnat unboxed_array_deserialize(void* dst)
48+
uintnat unboxed_array_deserialize(void* dst)
5049
{
5150
caml_failwith("Marshalling is not yet implemented for unboxed arrays");
5251
}

ocaml/runtime/float32.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <math.h>
2626
#include <float.h>
2727
#include <limits.h>
28+
#include <string.h>
2829

2930
#include "caml/alloc.h"
3031
#include "caml/fail.h"
@@ -540,3 +541,59 @@ CAMLprim value caml_float32_of_string(value vs)
540541
caml_failwith("float32_of_string");
541542
return Val_unit; /* not reached */
542543
}
544+
545+
/* Defined in array.c */
546+
547+
extern int no_polymorphic_compare(value v1, value v2);
548+
extern intnat no_polymorphic_hash(value v);
549+
extern void unboxed_array_serialize(value v, uintnat* bsize_32, uintnat* bsize_64);
550+
extern uintnat unboxed_array_deserialize(void* dst);
551+
extern value caml_make_vect(value len, value init);
552+
553+
CAMLexport const struct custom_operations caml_unboxed_float32_array_ops[2] = {
554+
{ "_unboxed_float32_even_array",
555+
custom_finalize_default,
556+
no_polymorphic_compare,
557+
no_polymorphic_hash,
558+
unboxed_array_serialize,
559+
unboxed_array_deserialize,
560+
custom_compare_ext_default,
561+
custom_fixed_length_default },
562+
{ "_unboxed_float32_odd_array",
563+
custom_finalize_default,
564+
no_polymorphic_compare,
565+
no_polymorphic_hash,
566+
unboxed_array_serialize,
567+
unboxed_array_deserialize,
568+
custom_compare_ext_default,
569+
custom_fixed_length_default },
570+
};
571+
572+
CAMLprim value caml_make_unboxed_float32_vect(value len)
573+
{
574+
/* This is only used on 64-bit targets. */
575+
576+
mlsize_t num_elements = Long_val(len);
577+
if (num_elements > Max_wosize) caml_invalid_argument("Array.make");
578+
579+
/* [num_fields] does not include the custom operations field. */
580+
mlsize_t num_fields = (num_elements + 1) / 2;
581+
582+
return caml_alloc_custom(&caml_unboxed_float32_array_ops[num_elements % 2],
583+
num_fields * sizeof(value), 0, 0);
584+
}
585+
586+
CAMLprim value caml_make_unboxed_float32_vect_bytecode(value len)
587+
{
588+
return caml_make_vect(len, caml_copy_float32(0.0f));
589+
}
590+
591+
CAMLprim value caml_unboxed_float32_vect_blit(value a1, value ofs1, value a2,
592+
value ofs2, value n)
593+
{
594+
// Need to skip the custom_operations field
595+
memmove((float *)((uintnat *)a2 + 1) + Long_val(ofs2),
596+
(float *)((uintnat *)a1 + 1) + Long_val(ofs1),
597+
Long_val(n) * sizeof(float));
598+
return Val_unit;
599+
}

ocaml/runtime4/array.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@ static const mlsize_t mlsize_t_max = -1;
3030

3131
/* Unboxed arrays */
3232

33-
static int no_polymorphic_compare(value v1, value v2)
33+
int no_polymorphic_compare(value v1, value v2)
3434
{
3535
caml_failwith("Polymorphic comparison is not permitted for unboxed arrays");
3636
}
3737

38-
static intnat no_polymorphic_hash(value v)
38+
intnat no_polymorphic_hash(value v)
3939
{
4040
caml_failwith("Polymorphic hash is not permitted for unboxed arrays");
4141
}
4242

43-
static void unboxed_array_serialize(value v, uintnat* bsize_32,
44-
uintnat* bsize_64)
43+
void unboxed_array_serialize(value v, uintnat* bsize_32, uintnat* bsize_64)
4544
{
4645
caml_failwith("Marshalling is not yet implemented for unboxed arrays");
4746
}
4847

49-
static uintnat unboxed_array_deserialize(void* dst)
48+
uintnat unboxed_array_deserialize(void* dst)
5049
{
5150
caml_failwith("Marshalling is not yet implemented for unboxed arrays");
5251
}

ocaml/runtime4/float32.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <math.h>
2626
#include <float.h>
2727
#include <limits.h>
28+
#include <string.h>
2829

2930
#include "caml/alloc.h"
3031
#include "caml/fail.h"
@@ -540,3 +541,59 @@ CAMLprim value caml_float32_of_string(value vs)
540541
caml_failwith("float32_of_string");
541542
return Val_unit; /* not reached */
542543
}
544+
545+
/* Defined in array.c */
546+
547+
extern int no_polymorphic_compare(value v1, value v2);
548+
extern intnat no_polymorphic_hash(value v);
549+
extern void unboxed_array_serialize(value v, uintnat* bsize_32, uintnat* bsize_64);
550+
extern uintnat unboxed_array_deserialize(void* dst);
551+
extern value caml_make_vect(value len, value init);
552+
553+
CAMLexport struct custom_operations caml_unboxed_float32_array_ops[2] = {
554+
{ "_unboxed_float32_even_array",
555+
custom_finalize_default,
556+
no_polymorphic_compare,
557+
no_polymorphic_hash,
558+
unboxed_array_serialize,
559+
unboxed_array_deserialize,
560+
custom_compare_ext_default,
561+
custom_fixed_length_default },
562+
{ "_unboxed_float32_odd_array",
563+
custom_finalize_default,
564+
no_polymorphic_compare,
565+
no_polymorphic_hash,
566+
unboxed_array_serialize,
567+
unboxed_array_deserialize,
568+
custom_compare_ext_default,
569+
custom_fixed_length_default },
570+
};
571+
572+
CAMLprim value caml_make_unboxed_float32_vect(value len)
573+
{
574+
/* This is only used on 64-bit targets. */
575+
576+
mlsize_t num_elements = Long_val(len);
577+
if (num_elements > Max_wosize) caml_invalid_argument("Array.make");
578+
579+
/* [num_fields] does not include the custom operations field. */
580+
mlsize_t num_fields = (num_elements + 1) / 2;
581+
582+
return caml_alloc_custom(&caml_unboxed_float32_array_ops[num_elements % 2],
583+
num_fields * sizeof(value), 0, 0);
584+
}
585+
586+
CAMLprim value caml_make_unboxed_float32_vect_bytecode(value len)
587+
{
588+
return caml_make_vect(len, caml_copy_float32(0.0f));
589+
}
590+
591+
CAMLprim value caml_unboxed_float32_vect_blit(value a1, value ofs1, value a2,
592+
value ofs2, value n)
593+
{
594+
// Need to skip the custom_operations field
595+
memmove((float *)((uintnat *)a2 + 1) + Long_val(ofs2),
596+
(float *)((uintnat *)a1 + 1) + Long_val(ofs1),
597+
Long_val(n) * sizeof(float));
598+
return Val_unit;
599+
}

0 commit comments

Comments
 (0)