Skip to content

Runtime helpers for 128-bit vectors #1897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ocaml/runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ BYTECODE_C_SOURCES := $(addsuffix .c, \
interp misc stacks fix_code startup_aux startup_byt freelist major_gc \
minor_gc memory alloc roots_byt globroots fail_byt signals \
signals_byt printexc backtrace_byt backtrace compare ints eventlog \
floats str array io extern intern hash sys meta parsing gc_ctrl md5 obj \
floats simd str array io extern intern hash sys meta parsing gc_ctrl md5 obj \
lexing callback debugger weak compact finalise custom dynlink \
afl $(UNIX_OR_WIN32) bigarray main memprof domain \
skiplist codefrag)

NATIVE_C_SOURCES := $(addsuffix .c, \
startup_aux startup_nat main fail_nat roots_nat signals \
signals_nat misc freelist major_gc minor_gc memory alloc compare ints \
floats str array io extern intern hash sys parsing gc_ctrl eventlog md5 obj \
floats simd str array io extern intern hash sys parsing gc_ctrl eventlog md5 obj \
lexing $(UNIX_OR_WIN32) printexc callback weak compact finalise custom \
globroots backtrace_nat backtrace dynlink_nat debugger meta \
dynlink clambda_checks afl bigarray \
Expand Down
46 changes: 46 additions & 0 deletions ocaml/runtime/caml/simd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Max Slater, Jane Street */
/* */
/* Copyright 2023 Jane Street Group LLC */
/* */
/* 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. */
/* */
/**************************************************************************/

/* SIMD vector instruction support */

#ifndef CAML_SIMD_H
#define CAML_SIMD_H

#include "mlvalues.h"

#if defined(_M_IX86_FP) || defined(__SSE2__) || defined(__SSE3__) || \
defined(__SSSE3__) || defined(__SSE4_1__) || defined(__SSE4_2__)
#define ARCH_SSE2
#endif

#if defined(__AVX__) || defined(__AVX2__)
#define ARCH_AVX
#endif

#ifdef ARCH_SSE2
#include <emmintrin.h>

#define Vec128_val(v) _mm_loadu_ps((const float*)Bp_val(v))
#define Vec128_vald(v) _mm_loadu_pd((const double*)Bp_val(v))
#define Vec128_vali(v) _mm_loadu_si128((const __m128i*)Bp_val(v))
#define Store_vec128_val(v,x) _mm_storeu_ps((float*)Bp_val(v), x)
#define Store_vec128_vald(v,x) _mm_storeu_pd((double*)Bp_val(v), x)
#define Store_vec128_vali(v,x) _mm_storeu_si128((__m128i*)Bp_val(v), x)

CAMLextern value caml_copy_vec128(__m128);
CAMLextern value caml_copy_vec128i(__m128i);
CAMLextern value caml_copy_vec128d(__m128d);
#endif

#endif /* CAML_SIMD_H */
40 changes: 40 additions & 0 deletions ocaml/runtime/simd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Max Slater, Jane Street */
/* */
/* Copyright 2023 Jane Street Group LLC */
/* */
/* 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

#include "caml/alloc.h"
#include "caml/simd.h"

#ifdef ARCH_SSE2

CAMLexport value caml_copy_vec128(__m128 v) {
value res = caml_alloc_small(2, Abstract_tag);
Store_vec128_val(res, v);
return res;
}

CAMLexport value caml_copy_vec128i(__m128i v) {
value res = caml_alloc_small(2, Abstract_tag);
Store_vec128_vali(res, v);
return res;
}

CAMLexport value caml_copy_vec128d(__m128d v) {
value res = caml_alloc_small(2, Abstract_tag);
Store_vec128_vald(res, v);
return res;
}

#endif
13 changes: 4 additions & 9 deletions tests/simd/stubs.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/simd.h>
#include <smmintrin.h>
#include <emmintrin.h>
#include <assert.h>

int64_t vec128_low_int64(__m128i v)
Expand All @@ -24,15 +22,12 @@ __m128i vec128_of_int64s(int64_t low, int64_t high)
CAMLprim value boxed_combine(value v0, value v1)
{
CAMLparam2(v0, v1);
CAMLlocal1(res);

__m128i l = _mm_loadu_si128((__m128i*)v0);
__m128i r = _mm_loadu_si128((__m128i*)v1);
__m128i l = Vec128_vali(v0);
__m128i r = Vec128_vali(v1);
__m128i result = _mm_add_epi64(l, r);
res = caml_alloc_small(2, Abstract_tag);
_mm_storeu_si128((__m128i*)res, result);

CAMLreturn(res);
CAMLreturn(caml_copy_vec128i(result));
}

__m128i lots_of_vectors(
Expand Down