From ca1d6bff560d5850133fdd9f9e154a0b3fce23eb Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 18 Dec 2018 02:02:19 +0100 Subject: [PATCH] log functions --- .../src/prog8/functions/BuiltinFunctions.kt | 1 - compiler/src/prog8/stackvm/StackVm.kt | 2 -- docs/source/programming.rst | 5 +-- prog8lib/prog8lib.p8 | 34 ++++++++++++------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/compiler/src/prog8/functions/BuiltinFunctions.kt b/compiler/src/prog8/functions/BuiltinFunctions.kt index 0f00ac894..97e097c3a 100644 --- a/compiler/src/prog8/functions/BuiltinFunctions.kt +++ b/compiler/src/prog8/functions/BuiltinFunctions.kt @@ -34,7 +34,6 @@ val BuiltinFunctions = mapOf( "atan" to FunctionSignature(true, listOf(BuiltinFunctionParam("rads", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, Math::atan) }, "ln" to FunctionSignature(true, listOf(BuiltinFunctionParam("value", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, Math::log) }, "log2" to FunctionSignature(true, listOf(BuiltinFunctionParam("value", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, ::log2) }, - "log10" to FunctionSignature(true, listOf(BuiltinFunctionParam("value", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, Math::log10) }, "sqrt" to FunctionSignature(true, listOf(BuiltinFunctionParam("value", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, Math::sqrt) }, "rad" to FunctionSignature(true, listOf(BuiltinFunctionParam("value", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, Math::toRadians) }, "deg" to FunctionSignature(true, listOf(BuiltinFunctionParam("value", setOf(DataType.FLOAT))), DataType.FLOAT) { a, p, n, h -> oneDoubleArg(a, p, n, h, Math::toDegrees) }, diff --git a/compiler/src/prog8/stackvm/StackVm.kt b/compiler/src/prog8/stackvm/StackVm.kt index 9109cb585..521a42a9b 100644 --- a/compiler/src/prog8/stackvm/StackVm.kt +++ b/compiler/src/prog8/stackvm/StackVm.kt @@ -33,7 +33,6 @@ enum class Syscall(val callNr: Short) { FUNC_ATAN(72), FUNC_LN(73), FUNC_LOG2(74), - FUNC_LOG10(75), FUNC_SQRT(76), FUNC_RAD(77), FUNC_DEG(78), @@ -1513,7 +1512,6 @@ class StackVm(private var traceOutputFile: String?) { Syscall.FUNC_ATAN -> evalstack.push(Value(DataType.FLOAT, atan(evalstack.pop().numericValue().toDouble()))) Syscall.FUNC_LN -> evalstack.push(Value(DataType.FLOAT, ln(evalstack.pop().numericValue().toDouble()))) Syscall.FUNC_LOG2 -> evalstack.push(Value(DataType.FLOAT, log2(evalstack.pop().numericValue().toDouble()))) - Syscall.FUNC_LOG10 -> evalstack.push(Value(DataType.FLOAT, log10(evalstack.pop().numericValue().toDouble()))) Syscall.FUNC_SQRT -> evalstack.push(Value(DataType.FLOAT, sqrt(evalstack.pop().numericValue().toDouble()))) Syscall.FUNC_RAD -> evalstack.push(Value(DataType.FLOAT, Math.toRadians(evalstack.pop().numericValue().toDouble()))) Syscall.FUNC_DEG -> evalstack.push(Value(DataType.FLOAT, Math.toDegrees(evalstack.pop().numericValue().toDouble()))) diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 4984830b7..11c1f8487 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -502,14 +502,11 @@ atan(x) Arctangent. ln(x) - Natural logarithm (base E). + Natural logarithm (base e). log2(x) Base 2 logarithm. -log10(x) - Base 10 logarithm. - sqrt(x) Square root. diff --git a/prog8lib/prog8lib.p8 b/prog8lib/prog8lib.p8 index 86c7f772a..5d57b666e 100644 --- a/prog8lib/prog8lib.p8 +++ b/prog8lib/prog8lib.p8 @@ -861,23 +861,32 @@ func_tan .proc .pend func_atan .proc - rts - .warn "atan not implemented" + ; -- push atan(f) back onto stack + jsr pop_float_fac1 + stx SCRATCH_ZPREGX + jsr c64.ATN + jmp push_fac1_as_result .pend func_ln .proc - rts - .warn "ln not implemented" + ; -- push ln(f) back onto stack + jsr pop_float_fac1 + stx SCRATCH_ZPREGX + jsr c64.LOG + jmp push_fac1_as_result .pend func_log2 .proc - rts - .warn "log2 not implemented" - .pend - -func_log10 .proc - rts - .warn "log10 not implemented" + ; -- push log base 2, ln(f)/ln(2), back onto stack + jsr pop_float_fac1 + stx SCRATCH_ZPREGX + jsr c64.LOG + jsr c64.MOVEF + lda #c64.FL_LOG2 + jsr c64.MOVFM + jsr c64.FDIVT + jmp push_fac1_as_result .pend func_sqrt .proc @@ -909,7 +918,7 @@ func_deg .proc _one_over_pi_div_180 .byte 134, 101, 46, 224, 211 ; 1 / (pi * 180) .pend -func_round .proc ; @todo check outcome vs floor/ceil +func_round .proc jsr pop_float_fac1 stx SCRATCH_ZPREGX jsr c64.FADDH @@ -920,6 +929,7 @@ func_round .proc ; @todo check outcome vs floor/ceil sta ESTACK_LO,x dex rts + .warn "round check outcome" .pend func_floor .proc ; @todo check outcome vs round/ceil