This commit is contained in:
Irmen de Jong 2023-04-29 20:59:05 +02:00
parent 025bf900a5
commit 319079de7a
17 changed files with 58 additions and 65 deletions

View File

@ -68,7 +68,6 @@ class FSignature(val pure: Boolean, // does it have side effects?
}
}
val BuiltinFunctions: Map<String, FSignature> = 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<String, FSignature> = 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),

View File

@ -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)
}
}

View File

@ -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<IRCodeChunkBase>()
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 {

View File

@ -128,7 +128,7 @@ sub log2(float value) -> float {
}}
}
sub sqrt(float value) -> float {
sub sqrtf(float value) -> float {
%asm {{
lda #<value
ldy #>value

View File

@ -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

View File

@ -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
}}

View File

@ -16,7 +16,7 @@ internal val constEvaluatorsForBuiltinFuncs: Map<String, ConstExpressionCaller>
"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() } },

View File

@ -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()

View File

@ -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))
}
}
"""

View File

@ -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``.

View File

@ -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.

View File

@ -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.....

View File

@ -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 {

View File

@ -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()
}
}

View File

@ -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

View File

@ -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

View File

@ -14,10 +14,10 @@
<keywords keywords="&amp;;-&gt;;@;and;as;asmsub;break;clobbers;do;downto;else;false;for;goto;if;if_cc;if_cs;if_eq;if_mi;if_ne;if_neg;if_nz;if_pl;if_pos;if_vc;if_vs;if_z;in;inline;not;or;repeat;return;romsub;step;sub;to;true;unroll;until;when;while;xor;~" ignore_case="false" />
<keywords2 keywords="%address;%asm;%asmbinary;%asminclude;%breakpoint;%import;%ir;%launcher;%option;%output;%zeropage;%zpreserved;iso:;petscii:;sc:" />
<keywords3 keywords="@requirezp;@shared;@zp;bool;byte;const;float;str;ubyte;uword;void;word" />
<keywords4 keywords="abs;all;any;callfar;callram;callrom;cmp;len;lsb;max;memory;min;mkword;msb;peek;peekw;poke;pokew;pop;popw;push;pushw;reverse;rol;rol2;ror;ror2;rrestore;rrestorex;rsave;rsavex;sgn;sizeof;sort;sqrt16;swap;|&gt;" />
<keywords4 keywords="abs;all;any;callfar;callram;callrom;cmp;len;lsb;max;memory;min;mkword;msb;peek;peekw;poke;pokew;pop;popw;push;pushw;reverse;rol;rol2;ror;ror2;rrestore;rrestorex;rsave;rsavex;sgn;sizeof;sort;sqrt;sqrt16;swap;|&gt;" />
</highlighting>
<extensionMap>
<mapping ext="p8" />
<mapping ext="prog8" />
</extensionMap>
</filetype>
</filetype>