diff --git a/codeCore/src/prog8/code/core/BuiltinFunctions.kt b/codeCore/src/prog8/code/core/BuiltinFunctions.kt index bb7f8cfe6..380fef668 100644 --- a/codeCore/src/prog8/code/core/BuiltinFunctions.kt +++ b/codeCore/src/prog8/code/core/BuiltinFunctions.kt @@ -68,7 +68,6 @@ class FSignature(val pure: Boolean, // does it have side effects? } } - val BuiltinFunctions: Map = mapOf( // this set of function have no return value and operate in-place: "rol" to FSignature(false, listOf(FParam("item", arrayOf(DataType.UBYTE, DataType.UWORD))), null), @@ -85,7 +84,7 @@ val BuiltinFunctions: Map = mapOf( // normal functions follow: "sizeof" to FSignature(true, listOf(FParam("object", DataType.values())), DataType.UBYTE), "sgn" to FSignature(true, listOf(FParam("value", NumericDatatypesNoBool)), DataType.BYTE), - "sqrtw" to FSignature(true, listOf(FParam("value", arrayOf(DataType.UWORD))), DataType.UBYTE), + "sqrt" to FSignature(true, listOf(FParam("value", arrayOf(DataType.UWORD))), DataType.UBYTE), "divmod" to FSignature(false, listOf(FParam("number", arrayOf(DataType.UBYTE)), FParam("divident", arrayOf(DataType.UBYTE)), FParam("division", arrayOf(DataType.UBYTE)), FParam("remainder", arrayOf(DataType.UBYTE))), null), "divmodw" to FSignature(false, listOf(FParam("number", arrayOf(DataType.UWORD)), FParam("divident", arrayOf(DataType.UWORD)), FParam("division", arrayOf(DataType.UWORD)), FParam("remainder", arrayOf(DataType.UWORD))), null), "any" to FSignature(true, listOf(FParam("values", ArrayDatatypes)), DataType.UBYTE), diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 972aa2d64..601ff6541 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -36,7 +36,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, "abs" -> funcAbs(fcall, resultToStack, resultRegister, sscope) "any", "all" -> funcAnyAll(fcall, resultToStack, resultRegister, sscope) "sgn" -> funcSgn(fcall, resultToStack, resultRegister, sscope) - "sqrtw" -> funcSqrtw(fcall, resultToStack, resultRegister, sscope) + "sqrt" -> funcSqrt(fcall, resultToStack, resultRegister, sscope) "divmod" -> funcDivmod(fcall) "divmodw" -> funcDivmodW(fcall) "rol" -> funcRol(fcall) @@ -300,12 +300,12 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, asmgen.translateNormalAssignment(assign, fcall.definingISub()) } - private fun funcSqrtw(fcall: PtBuiltinFunctionCall, resultToStack: Boolean, resultRegister: RegisterOrPair?, scope: IPtSubroutine?) { + private fun funcSqrt(fcall: PtBuiltinFunctionCall, resultToStack: Boolean, resultRegister: RegisterOrPair?, scope: IPtSubroutine?) { translateArguments(fcall, scope) if(resultToStack) - asmgen.out(" jsr prog8_lib.func_sqrtw_stack") + asmgen.out(" jsr prog8_lib.func_sqrt16_stack") else { - asmgen.out(" jsr prog8_lib.func_sqrtw_into_A") + asmgen.out(" jsr prog8_lib.func_sqrt16_into_A") assignAsmGen.assignRegisterByte(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, false, fcall.position, scope, asmgen), CpuRegister.A, false) } } diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt index 823ebd086..b35c1966f 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt @@ -17,7 +17,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe "abs" -> funcAbs(call) "cmp" -> funcCmp(call) "sgn" -> funcSgn(call) - "sqrtw" -> funcSqrtw(call) + "sqrt" -> funcSqrt(call) "divmod" -> funcDivmod(call, IRDataType.BYTE) "divmodw" -> funcDivmod(call, IRDataType.WORD) "pop" -> funcPop(call) @@ -212,15 +212,37 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe return ExpressionCodeResult(result, vmDt, resultReg, -1) } - private fun funcSqrtw(call: PtBuiltinFunctionCall): ExpressionCodeResult { + private fun funcSqrt(call: PtBuiltinFunctionCall): ExpressionCodeResult { val result = mutableListOf() val tr = exprGen.translateExpression(call.args.single()) - addToResult(result, tr, tr.resultReg, -1) - val resultReg = codeGen.registers.nextFree() - result += IRCodeChunk(null, null).also { - it += IRInstruction(Opcode.SQRT, IRDataType.WORD, reg1=resultReg, reg2=tr.resultReg) + val dt = call.args[0].type + when(dt) { + DataType.UBYTE -> { + addToResult(result, tr, tr.resultReg, -1) + val resultReg = codeGen.registers.nextFree() + result += IRCodeChunk(null, null).also { + it += IRInstruction(Opcode.SQRT, IRDataType.BYTE, reg1=resultReg, reg2=tr.resultReg) + } + return ExpressionCodeResult(result, IRDataType.BYTE, resultReg, -1) + } + DataType.UWORD -> { + addToResult(result, tr, tr.resultReg, -1) + val resultReg = codeGen.registers.nextFree() + result += IRCodeChunk(null, null).also { + it += IRInstruction(Opcode.SQRT, IRDataType.WORD, reg1=resultReg, reg2=tr.resultReg) + } + return ExpressionCodeResult(result, IRDataType.WORD, resultReg, -1) + } + DataType.FLOAT -> { + addToResult(result, tr, -1, tr.resultFpReg) + val resultFpReg = codeGen.registers.nextFreeFloat() + result += IRCodeChunk(null, null).also { + it += IRInstruction(Opcode.SQRT, IRDataType.FLOAT, fpReg1 = resultFpReg, reg2 = tr.resultFpReg) + } + return ExpressionCodeResult(result, IRDataType.FLOAT, -1, resultFpReg) + } + else -> throw AssemblyError("invalid dt for sqrt") } - return ExpressionCodeResult(result, IRDataType.WORD, resultReg, -1) } private fun funcPop(call: PtBuiltinFunctionCall): ExpressionCodeResult { diff --git a/compiler/res/prog8lib/floats_functions.p8 b/compiler/res/prog8lib/floats_functions.p8 index 9e57ad017..3cddb66c4 100644 --- a/compiler/res/prog8lib/floats_functions.p8 +++ b/compiler/res/prog8lib/floats_functions.p8 @@ -128,7 +128,7 @@ sub log2(float value) -> float { }} } -sub sqrt(float value) -> float { +sub sqrtf(float value) -> float { %asm {{ lda #value diff --git a/compiler/res/prog8lib/prog8_funcs.asm b/compiler/res/prog8lib/prog8_funcs.asm index 4478cf293..5728d0bc1 100644 --- a/compiler/res/prog8lib/prog8_funcs.asm +++ b/compiler/res/prog8lib/prog8_funcs.asm @@ -203,14 +203,14 @@ func_sign_w_stack .proc rts .pend -func_sqrtw_stack .proc - jsr func_sqrtw_into_A +func_sqrt16_stack .proc + jsr func_sqrt16_into_A sta P8ESTACK_LO,x dex rts .pend -func_sqrtw_into_A .proc +func_sqrt16_into_A .proc ; integer square root from http://6502org.wikidot.com/software-math-sqrt sta P8ZP_SCRATCH_W1 sty P8ZP_SCRATCH_W1+1 diff --git a/compiler/res/prog8lib/virtual/floats.p8 b/compiler/res/prog8lib/virtual/floats.p8 index f310f7d47..e9840c505 100644 --- a/compiler/res/prog8lib/virtual/floats.p8 +++ b/compiler/res/prog8lib/virtual/floats.p8 @@ -82,9 +82,9 @@ sub log2(float value) -> float { }} } -sub sqrt(float value) -> float { +sub sqrtf(float value) -> float { %ir {{ - loadm.f fr0,floats.sqrt.value + loadm.f fr0,floats.sqrtf.value sqrt.f fr0,fr0 returnr.f fr0 }} diff --git a/compiler/src/prog8/compiler/BuiltinFunctions.kt b/compiler/src/prog8/compiler/BuiltinFunctions.kt index 1ce314673..bb9208918 100644 --- a/compiler/src/prog8/compiler/BuiltinFunctions.kt +++ b/compiler/src/prog8/compiler/BuiltinFunctions.kt @@ -16,7 +16,7 @@ internal val constEvaluatorsForBuiltinFuncs: Map "len" to ::builtinLen, "sizeof" to ::builtinSizeof, "sgn" to ::builtinSgn, - "sqrtw" to { a, p, prg -> oneIntArgOutputInt(a, p, prg) { sqrt(it.toDouble()) } }, + "sqrt" to { a, p, prg -> oneIntArgOutputInt(a, p, prg) { sqrt(it.toDouble()) } }, "any" to { a, p, prg -> collectionArg(a, p, prg, ::builtinAny) }, "all" to { a, p, prg -> collectionArg(a, p, prg, ::builtinAll) }, "lsb" to { a, p, prg -> oneIntArgOutputInt(a, p, prg) { x: Int -> (x and 255).toDouble() } }, diff --git a/compiler/test/arithmetic/builtins.p8 b/compiler/test/arithmetic/builtins.p8 index 3e08b7b95..57c55e11b 100644 --- a/compiler/test/arithmetic/builtins.p8 +++ b/compiler/test/arithmetic/builtins.p8 @@ -298,10 +298,10 @@ main { uw = 50000 - ub = sqrtw(uw) + ub = sqrt(uw) txt.print_ub(ub) txt.nl() - ub = zero+sqrtw(uw)*1+zero + ub = zero+sqrt(uw)*1+zero txt.print_ub(ub) txt.nl() diff --git a/compiler/test/ast/TestIntermediateAst.kt b/compiler/test/ast/TestIntermediateAst.kt index ed30b7c7e..75170f530 100644 --- a/compiler/test/ast/TestIntermediateAst.kt +++ b/compiler/test/ast/TestIntermediateAst.kt @@ -21,7 +21,7 @@ class TestIntermediateAst: FunSpec({ ubyte cc ubyte[] array = [1,2,3] cc = 11 in array - cc = sqrtw(lsb(cc)) + cc = sqrt(lsb(cc)) } } """ diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index 035166097..7f4359827 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -269,7 +269,7 @@ sin(x) If you want a fast integer sine, have a look at examples/cx16/sincos.p8 that contains various lookup tables generated by the 64tass assembler. -sqrt(x) +sqrtf(x) Floating point Square root. To do the reverse, squaring a floating point number, just write ``x*x``. diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 8617abbbb..5b952a8bd 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -774,10 +774,10 @@ max (x, y) sgn (x) Get the sign of the value. Result is -1, 0 or 1 (negative, zero, positive). -sqrtw (w) +sqrt (w) 16 bit unsigned integer Square root. Result is unsigned byte. To do the reverse, squaring an integer, just write ``x*x``. - Floating point square root has its own function `floats.sqrt()` + Floating point square root has its own function `floats.sqrtf()` divmod (number, divident, division, remainder) Performs division and remainder calculation in a single call. This is faster than using separate '/' and '%' calculations. diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 4b6dc73df..533dc9f43 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -5,8 +5,8 @@ For 9.0 major changes ^^^^^^^^^^^^^^^^^^^^^ - DONE: added min() max() builtin functions - DONE: added 'cbm' block in the syslib module that now contains all CBM compatible kernal routines and variables -- DONE: rename sqrt16() to just sqrtw() -- add "polymorphism" of min() and max() to several other builtin functions as well! Fix docs. +- DONE: rename sqrt16() to just sqrt(), rename floats.sqrt() to floats.sqrtf() +- add "polymorphism" of min() and max() to several other builtin functions as well (abs, divmod, pop, push) Fix docs. - 6502 codegen: see if we can let for loops skip the loop if startvar>endvar, without adding a lot of code size/duplicating the loop condition. It is documented behavior to now loop 'around' $00 but it's too easy to forget about! Lot of work because of so many special cases in ForLoopsAsmgen..... diff --git a/examples/cx16/circles.p8 b/examples/cx16/circles.p8 index ce0901fdf..bf9778377 100644 --- a/examples/cx16/circles.p8 +++ b/examples/cx16/circles.p8 @@ -66,7 +66,7 @@ main { word dy = y as word - circle_y[cix] uword sqx = dx*dx as uword uword sqy = dy*dy as uword - return sqrtw(sqx + sqy) + return sqrt(sqx + sqy) } ; sub distance(ubyte cix) -> uword { diff --git a/examples/test.p8 b/examples/test.p8 index f93070c84..48fc47100 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,40 +5,12 @@ main { sub start() { - ubyte v1 = 11 - ubyte v2 = 88 - byte v1s = 22 - byte v2s = -99 - uword w1 = 1111 - uword w2 = 8888 - word w1s = 2222 - word w2s = -9999 - - float f1 = 1111.1 - float f2 = -999.9 - - floats.print_f(floats.minf(f1, f2)) - txt.spc() - floats.print_f(floats.maxf(f1, f2)) + ubyte bb = 199 + txt.print_ub(sqrt(bb)) txt.nl() - - txt.print_uw(min(v1, v2)) - txt.spc() - txt.print_w(min(v1s, v2s)) - txt.spc() - txt.print_uw(max(v1, v2)) - txt.spc() - txt.print_w(max(v1s, v2s)) - txt.nl() - - txt.print_uw(min(w1, w2)) - txt.spc() - txt.print_w(min(w1s, w2s)) - txt.spc() - txt.print_uw(max(w1, w2)) - txt.spc() - txt.print_w(max(w1s, w2s)) + float fl = 199.99 + floats.print_f(floats.sqrtf(fl)) txt.nl() } } diff --git a/examples/textelite.p8 b/examples/textelite.p8 index 0febbb749..ed0912386 100644 --- a/examples/textelite.p8 +++ b/examples/textelite.p8 @@ -817,7 +817,7 @@ planet { else ay=y-py ay /= 2 - ubyte d = sqrtw(ax*ax + ay*ay) + ubyte d = sqrt(ax*ax + ay*ay) if d>63 return 255 return d*4 diff --git a/examples/vm/textelite.p8 b/examples/vm/textelite.p8 index aecfa9d5f..6f1f83d7f 100644 --- a/examples/vm/textelite.p8 +++ b/examples/vm/textelite.p8 @@ -744,7 +744,7 @@ planet { else ay=y-py ay /= 2 - ubyte d = sqrtw(ax*ax + ay*ay) + ubyte d = sqrt(ax*ax + ay*ay) if d>63 return 255 return d*4 diff --git a/syntax-files/IDEA/Prog8.xml b/syntax-files/IDEA/Prog8.xml index c248f7e4d..2427bc6db 100644 --- a/syntax-files/IDEA/Prog8.xml +++ b/syntax-files/IDEA/Prog8.xml @@ -14,10 +14,10 @@ - + - \ No newline at end of file +