mirror of
https://github.com/irmen/prog8.git
synced 2025-02-04 02:30:19 +00:00
add math.diff() and math.diffw()
This commit is contained in:
parent
0904712a00
commit
94f12732ab
@ -1,4 +1,6 @@
|
||||
; Internal Math library routines - always included by the compiler
|
||||
; note: some functions you might expect here are builtin functions,
|
||||
; such as abs, sqrt, clamp, min, max for example.
|
||||
|
||||
math {
|
||||
%option no_symbol_prefixing
|
||||
@ -445,4 +447,41 @@ log2_tab
|
||||
}}
|
||||
}
|
||||
|
||||
asmsub diff(ubyte v1 @A, ubyte v2 @Y) -> ubyte @A {
|
||||
; -- returns the (absolute) difference, or distance, between the two bytes
|
||||
%asm {{
|
||||
sty P8ZP_SCRATCH_REG
|
||||
sec
|
||||
sbc P8ZP_SCRATCH_REG
|
||||
bcs +
|
||||
eor #255
|
||||
inc a
|
||||
+ rts
|
||||
}}
|
||||
}
|
||||
|
||||
asmsub diffw(uword w1 @R0, uword w2 @AY) -> uword @AY {
|
||||
; -- returns the (absolute) difference, or distance, between the two words
|
||||
%asm {{
|
||||
sec
|
||||
sbc cx16.r0L
|
||||
sta cx16.r0L
|
||||
tya
|
||||
sbc cx16.r0H
|
||||
sta cx16.r0H
|
||||
bcs +
|
||||
eor #255
|
||||
sta cx16.r0H
|
||||
lda cx16.r0L
|
||||
eor #255
|
||||
inc a
|
||||
sta cx16.r0L
|
||||
bne +
|
||||
inc cx16.r0H
|
||||
+ lda cx16.r0L
|
||||
ldy cx16.r0H
|
||||
rts
|
||||
}}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
; Internal Math library routines - always included by the compiler
|
||||
; note: some functions you might expect here are builtin functions,
|
||||
; such as abs, sqrt, clamp, min, max for example.
|
||||
|
||||
math {
|
||||
|
||||
@ -277,4 +279,17 @@ math {
|
||||
returnr.w r0
|
||||
}}
|
||||
}
|
||||
|
||||
sub diff(ubyte b1, ubyte b2) -> ubyte {
|
||||
if b1>b2
|
||||
return b1-b2
|
||||
return b2-b1
|
||||
}
|
||||
|
||||
sub diffw(uword w1, uword w2) -> uword {
|
||||
if w1>w2
|
||||
return w1-w2
|
||||
return w2-w1
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -407,6 +407,13 @@ but perhaps the provided ones can be of service too.
|
||||
If you already know the quadrant and x/y deltas, calculate discrete direction between 0 and 23.
|
||||
This is a heavily optimized routine (small and fast).
|
||||
|
||||
``diff (ubyte b1, ubyte b2) -> ubyte``
|
||||
Returns the absolute difference, or distance, between the two byte values.
|
||||
(This routine is more efficient than doing a compare and a subtract separately, or using abs)
|
||||
|
||||
``diffw (uword w1, uword w2) -> uword``
|
||||
Returns the absolute difference, or distance, between the two word values.
|
||||
(This routine is more efficient than doing a compare and a subtract separately, or using abs)
|
||||
|
||||
|
||||
cx16logo
|
||||
|
@ -1,14 +1,37 @@
|
||||
%import textio
|
||||
%import math
|
||||
%option no_sysinit
|
||||
%zeropage basicsafe
|
||||
|
||||
main {
|
||||
sub start() {
|
||||
byte @shared bb = -44
|
||||
uword uw = 8888 + (bb as ubyte)
|
||||
txt.print_uw(uw) ; 9100
|
||||
ubyte ub1 = 12
|
||||
ubyte ub2 = 233
|
||||
|
||||
txt.print_ub(math.diff(ub1, ub2))
|
||||
txt.nl()
|
||||
txt.print_uw(8888 + (bb as ubyte)) ; 9100
|
||||
ub1 = 200
|
||||
ub2 = 90
|
||||
txt.print_ub(math.diff(ub1, ub2))
|
||||
txt.nl()
|
||||
ub1 = 144
|
||||
ub2 = 144
|
||||
txt.print_ub(math.diff(ub1, ub2))
|
||||
txt.nl()
|
||||
txt.nl()
|
||||
|
||||
|
||||
uword uw1 = 1200
|
||||
uword uw2 = 40000
|
||||
txt.print_uw(math.diffw(uw1, uw2))
|
||||
txt.nl()
|
||||
uw1 = 40000
|
||||
uw2 = 21000
|
||||
txt.print_uw(math.diffw(uw1, uw2))
|
||||
txt.nl()
|
||||
uw1 = 30000
|
||||
uw2 = 30000
|
||||
txt.print_uw(math.diffw(uw1, uw2))
|
||||
txt.nl()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user