Skip to content

Commit

Permalink
Add log
Browse files Browse the repository at this point in the history
  • Loading branch information
fikovnik committed Dec 4, 2024
1 parent f3ae399 commit 75c93fd
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 4 deletions.
1 change: 1 addition & 0 deletions client/rsh/src/bc2c/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SEXP R_LOGIC2_OPS[] = {X_LOGIC2_OPS};
Value Rsh_NilValue;
Value Rsh_UnboundValue;
SEXP NOT_OP;
SEXP LOG_OP;
SEXP BC2C_CALL_TRAMPOLINE_SXP;

#include "runtime_impl.h"
44 changes: 43 additions & 1 deletion client/rsh/src/bc2c/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ typedef SEXP (*Rsh_closure)(SEXP, SEXP);
JIT_DECL Value Rsh_NilValue;
JIT_DECL Value Rsh_UnboundValue;
JIT_DECL SEXP NOT_OP;
JIT_DECL SEXP LOG_OP;
JIT_DECL SEXP BC2C_CALL_TRAMPOLINE_SXP;

#ifdef RSH_TESTS
Expand Down Expand Up @@ -2298,7 +2299,6 @@ static INLINE void Rsh_Or2nd(Value *v2, Value v1, SEXP call) {
// v2 is the result of Rsh_And1St
Value v1l = fixup_scalar_logical(v1, call, "'y'", "||");
R_Visible = TRUE;
// Therefore:
// The first argument is FALSE or NA.
// If the second argument is not FALSE then its value is the result.
// If the second argument is FALSE, then the first argument's value is the
Expand All @@ -2309,4 +2309,46 @@ static INLINE void Rsh_Or2nd(Value *v2, Value v1, SEXP call) {
R_Visible = TRUE;
}

static INLINE void Rsh_Log(Value *val, SEXP call, SEXP rho) {
if (VAL_IS_DBL(*val)) {
double d = VAL_DBL(*val);
double r = R_log(d);
if (ISNAN(r) && ISNAN(d)) {
r = d;
} else {
Rf_warningcall(call, R_MSG_NA);
}
R_Visible = TRUE;
*val = DBL_TO_VAL(r);
} else {
SEXP args = CONS_NR(val_as_sexp(*val), R_NilValue);
R_Visible = TRUE;
*val = SXP_TO_VAL(do_log_builtin(call, LOG_OP, args, rho));
}
}

static INLINE void Rsh_LogBase(Value *val, Value base, SEXP call, SEXP rho) {
if (VAL_IS_DBL(*val) && VAL_IS_DBL(base)) {
double d = VAL_DBL(*val);
double b = VAL_DBL(base);
double r = R_logbase(d, b);
if (ISNAN(r)) {
if (ISNAN(d)) {
r = d;
} else if (ISNAN(b)) {
r = b;
} else {
Rf_warningcall(call, R_MSG_NA);
}
}
R_Visible = TRUE;
*val = DBL_TO_VAL(r);
} else {
SEXP args = CONS_NR(val_as_sexp(base), R_NilValue);
args = CONS_NR(val_as_sexp(*val), args);
R_Visible = TRUE;
*val = SXP_TO_VAL(do_log_builtin(call, LOG_OP, args, rho));
}
}

#endif // RUNTIME_H
2 changes: 2 additions & 0 deletions client/rsh/src/bc2c/runtime_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SEXP Rsh_pc_reset() {
}
#endif

// TODO: is the preserving needed?
#define LOAD_R_BUILTIN(target, name) \
do { \
target = PROTECT(R_Primitive(name)); \
Expand Down Expand Up @@ -85,6 +86,7 @@ JIT_DEF SEXP Rsh_initialize_runtime(void) {
Rsh_NilValue = SXP_TO_VAL(R_NilValue);
Rsh_UnboundValue = SXP_TO_VAL(Rsh_UnboundValue);
LOAD_R_BUILTIN(NOT_OP, "!");
LOAD_R_BUILTIN(LOG_OP, "log");

#ifdef RSH_TESTS
BC2C_CALL_TRAMPOLINE_SXP = Rf_mkString("Rsh_call_trampoline");
Expand Down
14 changes: 14 additions & 0 deletions client/rsh/src/bc2c/runtime_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ SEXP R_compact_intrange(R_xlen_t n1, R_xlen_t n2);
SEXP do_seq_along(SEXP call, SEXP op, SEXP args, SEXP rho);
SEXP do_seq_len(SEXP call, SEXP op, SEXP args, SEXP rho);
R_varloc_t R_findVarLoc(SEXP rho, SEXP symbol);
SEXP do_log_builtin(SEXP call, SEXP op, SEXP args, SEXP env);

// from arithmetic.h
static INLINE double R_log(double x) {
return x > 0 ? log(x) : x == 0 ? R_NegInf : R_NaN;
}

static INLINE double R_logbase(double x, double base) {
if (base == 10)
return x > 0 ? log10(x) : x == 0 ? R_NegInf : R_NaN;
if (base == 2)
return x > 0 ? log2(x) : x == 0 ? R_NegInf : R_NaN;
return R_log(x) / R_log(base);
}

static INLINE SEXP Rsh_get_dim_attr(SEXP v) {
SEXP attr = ATTRIB(v);
Expand Down
2 changes: 1 addition & 1 deletion external/R
Submodule R updated 1 files
+1 −1 src/main/arithmetic.c
1 change: 0 additions & 1 deletion server/src/main/java/org/prlprg/bc/BCCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.function.Supplier;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import org.prlprg.bc.BcInstr.*;
import org.prlprg.bc.BcInstr.Add;
import org.prlprg.bc.BcInstr.And;
import org.prlprg.bc.BcInstr.And1st;
Expand Down
4 changes: 4 additions & 0 deletions server/src/main/java/org/prlprg/bc/BcInstr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1197,13 +1197,17 @@ public BcOp op() {
}
}

@StackEffect(pop=1, push=1)
@NeedsRho
record Log(ConstPool.Idx<LangSXP> ast) implements BcInstr {
@Override
public BcOp op() {
return BcOp.LOG;
}
}

@StackEffect(pop=2, push=1)
@NeedsRho
record LogBase(ConstPool.Idx<LangSXP> ast) implements BcInstr {
@Override
public BcOp op() {
Expand Down
4 changes: 3 additions & 1 deletion server/src/main/java/org/prlprg/bc2c/BC2CCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ private void compile(BcInstr instr, int pc) {
BcOp.AND1ST,
BcOp.AND2ND,
BcOp.OR1ST,
BcOp.OR2ND);
BcOp.OR2ND,
BcOp.LOG,
BcOp.LOGBASE);

private void checkSupported(BcInstr instr) {
if (!SUPPORTED_OPS.contains(instr.op())) {
Expand Down
11 changes: 11 additions & 0 deletions server/src/test/java/org/prlprg/bc2c/BC2CCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@ public void testScalarBooleanOperations(BC2CSnapshot snapshot) {
snapshot.verify("a <- NA; b <- NA; a || b");
}

@Test
public void testLog(BC2CSnapshot snapshot) {
snapshot.verify("log(1)");
snapshot.verify("log(c(1,10,100, NA))");
snapshot.verify("log(2, 2)");
snapshot.verify("log(c(1,10,100, NA), 2)");
snapshot.verify("log(10, 10)");
snapshot.verify("log(c(1,10,100, NA), 10)");
snapshot.verify("log(5, 5)");
snapshot.verify("log(c(1,10,100, NA), 5)");
}
// API

private TestResultCheck fastArith() {
Expand Down
Binary file not shown.

0 comments on commit 75c93fd

Please sign in to comment.