abs: remove support for floats. Use floats.fabs() instead.

this solves: can't use abs() etc in pipe expression because return type depends on argument type
This commit is contained in:
Irmen de Jong 2022-04-14 00:38:31 +02:00
parent 220246278a
commit 7dbff5b9e6
12 changed files with 48 additions and 31 deletions

View File

@ -1146,7 +1146,6 @@ internal class BuiltinFunctionsAsmGen(private val program: Program,
when (dt) {
in ByteDatatypes -> asmgen.out(" jsr prog8_lib.abs_b_stack")
in WordDatatypes -> asmgen.out(" jsr prog8_lib.abs_w_stack")
DataType.FLOAT -> asmgen.out(" jsr floats.abs_f_stack")
else -> throw AssemblyError("weird type")
}
} else {
@ -1159,10 +1158,6 @@ internal class BuiltinFunctionsAsmGen(private val program: Program,
asmgen.out(" jsr prog8_lib.abs_w_into_AY")
assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, false, scope, program, asmgen), RegisterOrPair.AY)
}
DataType.FLOAT -> {
asmgen.out(" jsr floats.abs_f_fac1")
assignAsmGen.assignFAC1float(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.FAC1, true, scope, program, asmgen))
}
else -> throw AssemblyError("weird type")
}
}

View File

@ -1265,7 +1265,7 @@ internal class AssignmentAsmGen(private val program: Program,
TargetStorageKind.MEMORY -> throw AssemblyError("can't assign float to mem byte")
TargetStorageKind.REGISTER -> {
if (target.register!! != RegisterOrPair.FAC1)
throw AssemblyError("can't assign Fac1 float to another fac register")
throw AssemblyError("can't assign Fac1 float to another register")
}
TargetStorageKind.STACK -> asmgen.out(" jsr floats.push_fac1")
}

View File

@ -188,6 +188,18 @@ sub pow(float value, float power) -> float {
}}
}
sub fabs(float value) -> float {
%asm {{
phx
lda #<value
ldy #>value
jsr MOVFM
jsr ABS
plx
rts
}}
}
%asminclude "library:c128/floats.asm"
%asminclude "library:c64/floats_funcs.asm"

View File

@ -211,6 +211,18 @@ sub pow(float value, float power) -> float {
}}
}
sub fabs(float value) -> float {
%asm {{
phx
lda #<value
ldy #>value
jsr MOVFM
jsr ABS
plx
rts
}}
}
%asminclude "library:c64/floats.asm"
%asminclude "library:c64/floats_funcs.asm"

View File

@ -1,19 +1,6 @@
; --- floating point builtin functions
abs_f_stack .proc
; -- push abs(AY) on stack
jsr floats.MOVFM
jsr floats.ABS
jmp push_fac1
.pend
abs_f_fac1 .proc
; -- FAC1 = abs(AY)
jsr floats.MOVFM
jmp floats.ABS
.pend
func_atan_stack .proc
jsr func_atan_fac1
jmp push_fac1

View File

@ -200,6 +200,18 @@ sub pow(float value, float power) -> float {
}}
}
sub fabs(float value) -> float {
%asm {{
phx
lda #<value
ldy #>value
jsr MOVFM
jsr ABS
plx
rts
}}
}
%asminclude "library:c64/floats.asm"
%asminclude "library:c64/floats_funcs.asm"

View File

@ -93,7 +93,7 @@ private val functionSignatures: List<FSignature> = listOf(
FSignature("reverse" , false, listOf(FParam("array", ArrayDatatypes)), null),
// cmp returns a status in the carry flag, but not a proper return value
FSignature("cmp" , false, listOf(FParam("value1", IntegerDatatypes), FParam("value2", NumericDatatypes)), null),
FSignature("abs" , true, listOf(FParam("value", NumericDatatypes)), DataType.UWORD, ::builtinAbs),
FSignature("abs" , true, listOf(FParam("value", IntegerDatatypes)), DataType.UWORD, ::builtinAbs),
FSignature("len" , true, listOf(FParam("values", IterableDatatypes)), DataType.UWORD, ::builtinLen),
// normal functions follow:
FSignature("sizeof" , true, listOf(FParam("object", DataType.values())), DataType.UBYTE, ::builtinSizeof),
@ -203,15 +203,14 @@ private fun collectionArg(args: List<Expression>, position: Position, program: P
}
private fun builtinAbs(args: List<Expression>, position: Position, program: Program): NumericLiteral {
// 1 arg, type = float or int, result type= isSameAs as argument type
// 1 arg, type = int, result type= uword
if(args.size!=1)
throw SyntaxError("abs requires one numeric argument", position)
throw SyntaxError("abs requires one integer argument", position)
val constval = args[0].constValue(program) ?: throw NotConstArgumentException()
return when (constval.type) {
in IntegerDatatypes -> numericLiteral(abs(constval.number.toInt()), args[0].position)
DataType.FLOAT -> numericLiteral(abs(constval.number), args[0].position)
else -> throw SyntaxError("abs requires one numeric argument", position)
else -> throw SyntaxError("abs requires one integer argument", position)
}
}

View File

@ -69,7 +69,7 @@ Language features
- Floating point math also supported if the target system provides floating point library routines (C64 and Cx16 both do).
- Strings can contain escaped characters but also many symbols directly if they have a petscii equivalent, such as "♠♥♣♦π▚●○╳". Characters like ^, _, \\, {, } and | are also accepted and converted to the closest petscii equivalents.
- High-level code optimizations, such as const-folding, expression and statement simplifications/rewriting.
- Many built-in functions, such as ``sin``, ``cos``, ``rnd``, ``abs``, ``min``, ``max``, ``sqrt``, ``msb``, ``rol``, ``ror``, ``swap``, ``sort`` and ``reverse``
- Many built-in functions, such as ``sin``, ``cos``, ``rnd``, ``abs``, ``sqrt``, ``msb``, ``rol``, ``ror``, ``swap``, ``sort`` and ``reverse``
- Programs can be run multiple times without reloading because of automatic variable (re)initializations.
- Supports the sixteen 'virtual' 16-bit registers R0 .. R15 from the Commander X16, also on the other machines.
- If you only use standard kernal and core prog8 library routines, it is possible to compile the *exact same program* for different machines (just change the compilation target flag)!

View File

@ -192,7 +192,8 @@ Provides string manipulation routines.
floats
------
Provides definitions for the ROM/kernal subroutines and utility routines dealing with floating
point variables. This includes ``print_f``, the routine used to print floating point numbers.
point variables. This includes ``print_f``, the routine used to print floating point numbers,
``fabs`` to get the absolute value of a floating point number, and some others.
graphics

View File

@ -736,7 +736,7 @@ Math
^^^^
abs(x)
Absolute value.
Absolute value of an integer. For floating point numbers, use ``floats.fabs()`` instead.
atan(x)
Arctangent.

View File

@ -3,8 +3,7 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- abs: remove support for floats. Just use floats.ABS()
- ... this will solve: can't use abs() etc in pipe expression because return type depends on argument type
- "999 as ubyte" won't be const folded away and gives compiler crash
- pipe operator: allow non-unary function calls in the pipe that specify the other argument(s) in the calls.
- createAssemblyAndAssemble(): make it possible to actually get rid of the VarDecl nodes by fixing the rest of the code mentioned there.
- allow "xxx" * constexpr (where constexpr is not a number literal), now gives expression error not same type

View File

@ -7,8 +7,8 @@
main {
sub start() {
ubyte qq = 99 as ubyte |> abs() |> abs()
txt.print_ub(qq)
uword qq = 999 as ubyte |> abs() |> abs()
txt.print_uw(qq)
txt.nl()
; uword other = $fe4a