aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-04-05 13:46:09 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-04-05 13:46:09 +0300
commit42f89542550033a3f22700e6b0fd71627a252f96 (patch)
treeb92fe73c326cb631f6fbb0bc37bc1f653dcd2079
parent441ddf9277f878b83c8d093def51b27285353fed (diff)
downloadejit-42f89542550033a3f22700e6b0fd71627a252f96.tar.gz
ejit-42f89542550033a3f22700e6b0fd71627a252f96.zip
implement minr_d/maxr_d
m---------deps/lightening0
-rw-r--r--include/ejit/ejit.h6
-rw-r--r--src/common.h6
-rw-r--r--src/compile/compile.c46
-rw-r--r--src/ejit.c26
-rw-r--r--src/interp.c22
-rw-r--r--tests/maxr_d.c28
-rw-r--r--tests/maxr_f.c28
-rw-r--r--tests/minr_d.c28
-rw-r--r--tests/minr_f.c28
10 files changed, 218 insertions, 0 deletions
diff --git a/deps/lightening b/deps/lightening
-Subproject 6421af4db1570a6a5fc3a15f3bcb2601d854ed0
+Subproject 5a72993827bc2b1735a7611c0036640cdb01b93
diff --git a/include/ejit/ejit.h b/include/ejit/ejit.h
index b4b20c1..965103c 100644
--- a/include/ejit/ejit.h
+++ b/include/ejit/ejit.h
@@ -872,6 +872,12 @@ void ejit_truncr_d_64(struct ejit_func *s, struct ejit_gpr r0,
void ejit_sqrtr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1);
void ejit_sqrtr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1);
+void ejit_minr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2);
+void ejit_minr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2);
+
+void ejit_maxr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2);
+void ejit_maxr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2);
+
struct ejit_reloc ejit_bltr(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1);
struct ejit_reloc ejit_bner(struct ejit_func *s, struct ejit_gpr r0,
diff --git a/src/common.h b/src/common.h
index 661220b..69f1441 100644
--- a/src/common.h
+++ b/src/common.h
@@ -145,6 +145,12 @@ enum ejit_opcode {
EJIT_OP_SQRTR_F,
EJIT_OP_SQRTR_D,
+ EJIT_OP_MINR_F,
+ EJIT_OP_MINR_D,
+
+ EJIT_OP_MAXR_F,
+ EJIT_OP_MAXR_D,
+
EJIT_OP_EQR,
EJIT_OP_NER,
EJIT_OP_GTR,
diff --git a/src/compile/compile.c b/src/compile/compile.c
index 38c368f..dcf662b 100644
--- a/src/compile/compile.c
+++ b/src/compile/compile.c
@@ -1977,6 +1977,46 @@ static void resolve_relocs(jit_state_t *j, struct relocs *relocs, struct addrs *
}
}
+static void compile_maxr_f(struct ejit_func *f, jit_state_t *j,
+ struct ejit_insn i)
+{
+ jit_fpr_t r0 = getfpr(f, i.r0, 0);
+ jit_fpr_t r1 = getloc_f(f, j, i.r1, 1);
+ jit_fpr_t r2 = getloc_f(f, j, i.r2, 2);
+ jit_maxr_f(j, r0, r1, r2);
+ putloc_f(f, j, i.r0, r0);
+}
+
+static void compile_maxr_d(struct ejit_func *f, jit_state_t *j,
+ struct ejit_insn i)
+{
+ jit_fpr_t r0 = getfpr(f, i.r0, 0);
+ jit_fpr_t r1 = getloc_d(f, j, i.r1, 1);
+ jit_fpr_t r2 = getloc_d(f, j, i.r2, 2);
+ jit_maxr_d(j, r0, r1, r2);
+ putloc_d(f, j, i.r0, r0);
+}
+
+static void compile_minr_f(struct ejit_func *f, jit_state_t *j,
+ struct ejit_insn i)
+{
+ jit_fpr_t r0 = getfpr(f, i.r0, 0);
+ jit_fpr_t r1 = getloc_f(f, j, i.r1, 1);
+ jit_fpr_t r2 = getloc_f(f, j, i.r2, 2);
+ jit_minr_f(j, r0, r1, r2);
+ putloc_f(f, j, i.r0, r0);
+}
+
+static void compile_minr_d(struct ejit_func *f, jit_state_t *j,
+ struct ejit_insn i)
+{
+ jit_fpr_t r0 = getfpr(f, i.r0, 0);
+ jit_fpr_t r1 = getloc_d(f, j, i.r1, 1);
+ jit_fpr_t r2 = getloc_d(f, j, i.r2, 2);
+ jit_minr_d(j, r0, r1, r2);
+ putloc_d(f, j, i.r0, r0);
+}
+
static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena,
size_t size)
{
@@ -2143,6 +2183,12 @@ static size_t compile_fn_body(struct ejit_func *f, jit_state_t *j, void *arena,
case EJIT_OP_SQRTR_F: compile_sqrtr_f(f, j, i); break;
case EJIT_OP_SQRTR_D: compile_sqrtr_d(f, j, i); break;
+ case EJIT_OP_MINR_F: compile_minr_f(f, j, i); break;
+ case EJIT_OP_MINR_D: compile_minr_d(f, j, i); break;
+
+ case EJIT_OP_MAXR_F: compile_maxr_f(f, j, i); break;
+ case EJIT_OP_MAXR_D: compile_maxr_d(f, j, i); break;
+
case EJIT_OP_EQR: compile_eqr(f, j, i); break;
case EJIT_OP_EQR_F: compile_eqr_f(f, j, i); break;
case EJIT_OP_EQR_D: compile_eqr_d(f, j, i); break;
diff --git a/src/ejit.c b/src/ejit.c
index 75f6a6a..c997a01 100644
--- a/src/ejit.c
+++ b/src/ejit.c
@@ -901,31 +901,37 @@ void ejit_ret(struct ejit_func *s)
void ejit_retr(struct ejit_func *s, struct ejit_gpr r0)
{
+ assert(s->rtype != EJIT_FLOAT && s->rtype != EJIT_DOUBLE);
emit_insn_oxr(s, EJIT_OP_RETR, r0);
}
void ejit_retr_f(struct ejit_func *s, struct ejit_fpr f0)
{
+ assert(s->rtype == EJIT_FLOAT);
emit_insn_oxf(s, EJIT_OP_RETR_F, f0);
}
void ejit_retr_d(struct ejit_func *s, struct ejit_fpr f0)
{
+ assert(s->rtype == EJIT_DOUBLE);
emit_insn_oxf(s, EJIT_OP_RETR_D, f0);
}
void ejit_reti(struct ejit_func *s, int64_t i)
{
+ assert(s->rtype != EJIT_FLOAT && s->rtype != EJIT_DOUBLE);
emit_insn_oi(s, EJIT_OP_RETI, i);
}
void ejit_reti_f(struct ejit_func *s, float f)
{
+ assert(s->rtype == EJIT_FLOAT);
emit_insn_oF(s, EJIT_OP_RETI_F, f);
}
void ejit_reti_d(struct ejit_func *s, double f)
{
+ assert(s->rtype == EJIT_DOUBLE);
emit_insn_oD(s, EJIT_OP_RETI_D, f);
}
@@ -1382,6 +1388,26 @@ void ejit_sqrtr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1)
emit_insn_off(s, EJIT_OP_SQRTR_D, r0, r1);
}
+void ejit_minr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2)
+{
+ emit_insn_offf(s, EJIT_OP_MINR_F, r0, r1, r2);
+}
+
+void ejit_minr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2)
+{
+ emit_insn_offf(s, EJIT_OP_MINR_D, r0, r1, r2);
+}
+
+void ejit_maxr_f(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2)
+{
+ emit_insn_offf(s, EJIT_OP_MAXR_F, r0, r1, r2);
+}
+
+void ejit_maxr_d(struct ejit_func *s, struct ejit_fpr r0, struct ejit_fpr r1, struct ejit_fpr r2)
+{
+ emit_insn_offf(s, EJIT_OP_MAXR_D, r0, r1, r2);
+}
+
struct ejit_reloc ejit_bner(struct ejit_func *s, struct ejit_gpr r0,
struct ejit_gpr r1)
{
diff --git a/src/interp.c b/src/interp.c
index e7be77b..57dbfbe 100644
--- a/src/interp.c
+++ b/src/interp.c
@@ -150,6 +150,12 @@ union interp_ret ejit_run(struct ejit_func *f, size_t paramc, struct ejit_arg pa
[EJIT_OP_SQRTR_F] = &&SQRTR_F,
[EJIT_OP_SQRTR_D] = &&SQRTR_D,
+ [EJIT_OP_MINR_F] = &&MINR_F,
+ [EJIT_OP_MINR_D] = &&MINR_D,
+
+ [EJIT_OP_MAXR_F] = &&MAXR_F,
+ [EJIT_OP_MAXR_D] = &&MAXR_D,
+
[EJIT_OP_BNER] = &&BNER,
[EJIT_OP_BNEI] = &&BNEI,
[EJIT_OP_BNER_F] = &&BNER_F,
@@ -791,6 +797,22 @@ union interp_ret ejit_run(struct ejit_func *f, size_t paramc, struct ejit_arg pa
fpr[i.r0].f = sqrt(fpr[i.r1].f);
DISPATCH();
+ DO(MINR_F);
+ fpr[i.r0].f = fminf(fpr[i.r1].f, fpr[i.r2].f);
+ DISPATCH();
+
+ DO(MINR_D);
+ fpr[i.r0].d = fmin(fpr[i.r1].d, fpr[i.r2].d);
+ DISPATCH();
+
+ DO(MAXR_F);
+ fpr[i.r0].f = fmaxf(fpr[i.r1].f, fpr[i.r2].f);
+ DISPATCH();
+
+ DO(MAXR_D);
+ fpr[i.r0].d = fmax(fpr[i.r1].d, fpr[i.r2].d);
+ DISPATCH();
+
DO(SQRTR_D);
fpr[i.r0].d = sqrt(fpr[i.r1].d);
DISPATCH();
diff --git a/tests/maxr_d.c b/tests/maxr_d.c
new file mode 100644
index 0000000..3e35665
--- /dev/null
+++ b/tests/maxr_d.c
@@ -0,0 +1,28 @@
+#include <ejit/ejit.h>
+#include <assert.h>
+#include "do_jit.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argv;
+ bool do_jit = argc > 1;
+ struct ejit_operand operands[2] = {
+ EJIT_OPERAND_FPR(0, EJIT_TYPE(double)),
+ EJIT_OPERAND_FPR(1, EJIT_TYPE(double))
+ };
+
+ struct ejit_func *f = ejit_create_func(EJIT_TYPE(double), 2, operands);
+
+ ejit_maxr_d(f, EJIT_FPR(2), EJIT_FPR(0), EJIT_FPR(1));
+ ejit_retr_d(f, EJIT_FPR(2));
+
+ ejit_select_compile_func(f, 0, 3, EJIT_USE64(double), do_jit, true);
+
+ assert(erfd2(f, EJIT_ARG(42., double), EJIT_ARG(69., double)
+ ) == 69.);
+
+ assert(erfd2(f, EJIT_ARG(-42., double), EJIT_ARG(-69., double)
+ ) == -42.);
+
+ ejit_destroy_func(f);
+}
diff --git a/tests/maxr_f.c b/tests/maxr_f.c
new file mode 100644
index 0000000..581f867
--- /dev/null
+++ b/tests/maxr_f.c
@@ -0,0 +1,28 @@
+#include <ejit/ejit.h>
+#include <assert.h>
+#include "do_jit.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argv;
+ bool do_jit = argc > 1;
+ struct ejit_operand operands[2] = {
+ EJIT_OPERAND_FPR(0, EJIT_TYPE(float)),
+ EJIT_OPERAND_FPR(1, EJIT_TYPE(float))
+ };
+
+ struct ejit_func *f = ejit_create_func(EJIT_TYPE(float), 2, operands);
+
+ ejit_maxr_f(f, EJIT_FPR(2), EJIT_FPR(0), EJIT_FPR(1));
+ ejit_retr_f(f, EJIT_FPR(2));
+
+ ejit_select_compile_func(f, 0, 3, EJIT_USE64(float), do_jit, true);
+
+ assert(erff2(f, EJIT_ARG(42., float), EJIT_ARG(69., float)
+ ) == 69.);
+
+ assert(erff2(f, EJIT_ARG(-42., float), EJIT_ARG(-69., float)
+ ) == -42.);
+
+ ejit_destroy_func(f);
+}
diff --git a/tests/minr_d.c b/tests/minr_d.c
new file mode 100644
index 0000000..d0fb7c8
--- /dev/null
+++ b/tests/minr_d.c
@@ -0,0 +1,28 @@
+#include <ejit/ejit.h>
+#include <assert.h>
+#include "do_jit.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argv;
+ bool do_jit = argc > 1;
+ struct ejit_operand operands[2] = {
+ EJIT_OPERAND_FPR(0, EJIT_TYPE(double)),
+ EJIT_OPERAND_FPR(1, EJIT_TYPE(double))
+ };
+
+ struct ejit_func *f = ejit_create_func(EJIT_TYPE(double), 2, operands);
+
+ ejit_minr_d(f, EJIT_FPR(2), EJIT_FPR(0), EJIT_FPR(1));
+ ejit_retr_d(f, EJIT_FPR(2));
+
+ ejit_select_compile_func(f, 0, 3, EJIT_USE64(double), do_jit, true);
+
+ assert(erfd2(f, EJIT_ARG(42., double), EJIT_ARG(69., double)
+ ) == 42.);
+
+ assert(erfd2(f, EJIT_ARG(-42., double), EJIT_ARG(-69., double)
+ ) == -69.);
+
+ ejit_destroy_func(f);
+}
diff --git a/tests/minr_f.c b/tests/minr_f.c
new file mode 100644
index 0000000..b02ec06
--- /dev/null
+++ b/tests/minr_f.c
@@ -0,0 +1,28 @@
+#include <ejit/ejit.h>
+#include <assert.h>
+#include "do_jit.h"
+
+int main(int argc, char *argv[])
+{
+ (void)argv;
+ bool do_jit = argc > 1;
+ struct ejit_operand operands[2] = {
+ EJIT_OPERAND_FPR(0, EJIT_TYPE(float)),
+ EJIT_OPERAND_FPR(1, EJIT_TYPE(float))
+ };
+
+ struct ejit_func *f = ejit_create_func(EJIT_TYPE(float), 2, operands);
+
+ ejit_minr_f(f, EJIT_FPR(2), EJIT_FPR(0), EJIT_FPR(1));
+ ejit_retr_f(f, EJIT_FPR(2));
+
+ ejit_select_compile_func(f, 0, 3, EJIT_USE64(float), do_jit, true);
+
+ assert(erff2(f, EJIT_ARG(42., float), EJIT_ARG(69., float)
+ ) == 42.);
+
+ assert(erff2(f, EJIT_ARG(-42., float), EJIT_ARG(-69., float)
+ ) == -69.);
+
+ ejit_destroy_func(f);
+}