removed 16 bits sin/cos routines from math library (sin16, sin16r etc)

This commit is contained in:
Irmen de Jong 2022-09-22 12:42:17 +02:00
parent 4f8fb32136
commit 97aa91c75e
6 changed files with 29 additions and 193 deletions

View File

@ -974,6 +974,9 @@ internal class AssignmentAsmGen(private val program: Program,
} else if(valueDt in ByteDatatypes && targetDt in ByteDatatypes) { } else if(valueDt in ByteDatatypes && targetDt in ByteDatatypes) {
// byte to byte, just assign // byte to byte, just assign
assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD) assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD)
} else if(valueDt in ByteDatatypes && targetDt in WordDatatypes) {
// byte to word, assign and zero the msb
TODO("cast byte to word: $value")
} }
else else
throw AssemblyError("can't cast $valueDt to $targetDt, this should have been checked in the astchecker") throw AssemblyError("can't cast $valueDt to $targetDt, this should have been checked in the astchecker")

View File

@ -37,60 +37,6 @@ _sinecos8 .char trunc(127.0 * sin(range(256+64) * rad(360.0/256.0)))
}} }}
} }
asmsub sin16(ubyte angle @A) -> word @AY {
%asm {{
tay
lda _sinecos8lo,y
pha
lda _sinecos8hi,y
tay
pla
rts
_ := trunc(32767.0 * sin(range(256+64) * rad(360.0/256.0)))
_sinecos8lo .byte <_
_sinecos8hi .byte >_
}}
}
asmsub cos16(ubyte angle @A) -> word @AY {
%asm {{
tay
lda sin16._sinecos8lo+64,y
pha
lda sin16._sinecos8hi+64,y
tay
pla
rts
}}
}
asmsub sin16u(ubyte angle @A) -> uword @AY {
%asm {{
tay
lda _sinecos8ulo,y
pha
lda _sinecos8uhi,y
tay
pla
rts
_ := trunc(32768.0 + 32767.5 * sin(range(256+64) * rad(360.0/256.0)))
_sinecos8ulo .byte <_
_sinecos8uhi .byte >_
}}
}
asmsub cos16u(ubyte angle @A) -> uword @AY {
%asm {{
tay
lda sin16u._sinecos8ulo+64,y
pha
lda sin16u._sinecos8uhi+64,y
tay
pla
rts
}}
}
asmsub sinr8u(ubyte radians @A) clobbers(Y) -> ubyte @A { asmsub sinr8u(ubyte radians @A) clobbers(Y) -> ubyte @A {
%asm {{ %asm {{
tay tay
@ -125,57 +71,4 @@ _sinecosR8 .char trunc(127.0 * sin(range(180+45) * rad(360.0/180.0)))
}} }}
} }
asmsub sinr16(ubyte radians @A) -> word @AY {
%asm {{
tay
lda _sinecosR8lo,y
pha
lda _sinecosR8hi,y
tay
pla
rts
_ := trunc(32767.0 * sin(range(180+45) * rad(360.0/180.0)))
_sinecosR8lo .byte <_
_sinecosR8hi .byte >_
}}
}
asmsub cosr16(ubyte radians @A) -> word @AY {
%asm {{
tay
lda sinr16._sinecosR8lo+45,y
pha
lda sinr16._sinecosR8hi+45,y
tay
pla
rts
}}
}
asmsub sinr16u(ubyte radians @A) -> uword @AY {
%asm {{
tay
lda _sinecosR8ulo,y
pha
lda _sinecosR8uhi,y
tay
pla
rts
_ := trunc(32768.0 + 32767.5 * sin(range(180+45) * rad(360.0/180.0)))
_sinecosR8ulo .byte <_
_sinecosR8uhi .byte >_
}}
}
asmsub cosr16u(ubyte radians @A) -> uword @AY {
%asm {{
tay
lda sinr16u._sinecosR8ulo+45,y
pha
lda sinr16u._sinecosR8uhi+45,y
tay
pla
rts
}}
}
} }

View File

@ -19,7 +19,7 @@ math {
$0f, $11, $12, $14, $15, $17, $19, $1b, $1d, $1f, $21, $23, $25, $28, $2a, $2c, $0f, $11, $12, $14, $15, $17, $19, $1b, $1d, $1f, $21, $23, $25, $28, $2a, $2c,
$2f, $31, $34, $36, $39, $3b, $3e, $41, $43, $46, $49, $4c, $4f, $52, $55, $58, $2f, $31, $34, $36, $39, $3b, $3e, $41, $43, $46, $49, $4c, $4f, $52, $55, $58,
$5a, $5d, $61, $64, $67, $6a, $6d, $70, $73, $76, $79, $7c] $5a, $5d, $61, $64, $67, $6a, $6d, $70, $73, $76, $79, $7c]
return sintab[angle] return sintab[angle]
} }
sub cos8u(ubyte angle) -> ubyte { sub cos8u(ubyte angle) -> ubyte {
@ -40,7 +40,7 @@ math {
$bc, $be, $c1, $c4, $c6, $c9, $cb, $ce, $d0, $d3, $d5, $d7, $da, $dc, $de, $e0, $bc, $be, $c1, $c4, $c6, $c9, $cb, $ce, $d0, $d3, $d5, $d7, $da, $dc, $de, $e0,
$e2, $e4, $e6, $e8, $ea, $eb, $ed, $ee, $f0, $f1, $f3, $f4, $f5, $f6, $f8, $f9, $e2, $e4, $e6, $e8, $ea, $eb, $ed, $ee, $f0, $f1, $f3, $f4, $f5, $f6, $f8, $f9,
$fa, $fa, $fb, $fc, $fd, $fd, $fe, $fe, $fe, $ff, $ff, $ff ] $fa, $fa, $fb, $fc, $fd, $fd, $fe, $fe, $fe, $ff, $ff, $ff ]
return costab[angle] return costab[angle]
} }
sub sin8(ubyte angle) -> byte { sub sin8(ubyte angle) -> byte {
@ -51,22 +51,6 @@ math {
return 42 ; TODO return 42 ; TODO
} }
sub sin16(ubyte angle) -> word {
return 4242 ; TODO
}
sub cos16(ubyte angle) -> word {
return 4242 ; TODO
}
sub sin16u(ubyte angle) -> uword {
return 4242 ; TODO
}
sub cos16u(ubyte angle) -> uword {
return 4242 ; TODO
}
sub sinr8u(ubyte radians) -> ubyte { sub sinr8u(ubyte radians) -> ubyte {
ubyte[180] sintab = [ ubyte[180] sintab = [
$80, $84, $88, $8d, $80, $84, $88, $8d,
@ -109,19 +93,4 @@ math {
return 42 ; TODO return 42 ; TODO
} }
sub sinr16(ubyte radians) -> word {
return 4242 ; TODO
}
sub cosr16(ubyte radians) -> word {
return 4242 ; TODO
}
sub sinr16u(ubyte radians) -> uword {
return 4242 ; TODO
}
sub cosr16u(ubyte radians) -> uword {
return 4242 ; TODO
}
} }

View File

@ -273,7 +273,7 @@ math
Low level math routines. You should not normally have to bother with this directly. Low level math routines. You should not normally have to bother with this directly.
The compiler needs it to implement most of the math operations in your programs. The compiler needs it to implement most of the math operations in your programs.
However there's a bunch of integer trig functions in here too that use lookup tables However there's a bunch of 8-bit integer trig functions in here too that use lookup tables
to quickly calculate sine and cosines. Usually a custom lookup table is the way to go if your to quickly calculate sine and cosines. Usually a custom lookup table is the way to go if your
application needs this, but perhaps the provided ones can be of service too: application needs this, but perhaps the provided ones can be of service too:
@ -283,12 +283,6 @@ sin8u(x)
sin8(x) sin8(x)
Fast 8-bit byte sine of angle 0..255, result is in range -127..127 Fast 8-bit byte sine of angle 0..255, result is in range -127..127
sin16u(x)
Fast 16-bit uword sine of angle 0..255, result is in range 0..65535
sin16(x)
Fast 16-bit word sine of angle 0..255, result is in range -32767..32767
sinr8u(x) sinr8u(x)
Fast 8-bit ubyte sine of angle 0..179 (each is a 2 degree step), result is in range 0..255 Fast 8-bit ubyte sine of angle 0..179 (each is a 2 degree step), result is in range 0..255
Angles 180..255 will yield a garbage result! Angles 180..255 will yield a garbage result!
@ -297,27 +291,12 @@ sinr8(x)
Fast 8-bit byte sine of angle 0..179 (each is a 2 degree step), result is in range -127..127 Fast 8-bit byte sine of angle 0..179 (each is a 2 degree step), result is in range -127..127
Angles 180..255 will yield a garbage result! Angles 180..255 will yield a garbage result!
sinr16u(x)
Fast 16-bit uword sine of angle 0..179 (each is a 2 degree step), result is in range 0..65535
Angles 180..255 will yield a garbage result!
sinr16(x)
Fast 16-bit word sine of angle 0..179 (each is a 2 degree step), result is in range -32767..32767
Angles 180..255 will yield a garbage result!
cos8u(x) cos8u(x)
Fast 8-bit ubyte cosine of angle 0..255, result is in range 0..255 Fast 8-bit ubyte cosine of angle 0..255, result is in range 0..255
cos8(x) cos8(x)
Fast 8-bit byte cosine of angle 0..255, result is in range -127..127 Fast 8-bit byte cosine of angle 0..255, result is in range -127..127
cos16u(x)
Fast 16-bit uword cosine of angle 0..255, result is in range 0..65535
cos16(x)
Fast 16-bit word cosine of angle 0..255, result is in range -32767..32767
cosr8u(x) cosr8u(x)
Fast 8-bit ubyte cosine of angle 0..179 (each is a 2 degree step), result is in range 0..255 Fast 8-bit ubyte cosine of angle 0..179 (each is a 2 degree step), result is in range 0..255
Angles 180..255 will yield a garbage result! Angles 180..255 will yield a garbage result!
@ -326,14 +305,6 @@ cosr8(x)
Fast 8-bit byte cosine of angle 0..179 (each is a 2 degree step), result is in range -127..127 Fast 8-bit byte cosine of angle 0..179 (each is a 2 degree step), result is in range -127..127
Angles 180..255 will yield a garbage result! Angles 180..255 will yield a garbage result!
cosr16u(x)
Fast 16-bit uword cosine of angle 0..179 (each is a 2 degree step), result is in range 0..65535
Angles 180..255 will yield a garbage result!
cosr16(x)
Fast 16-bit word cosine of angle 0..179 (each is a 2 degree step), result is in range -32767..32767
Angles 180..255 will yield a garbage result!
cx16logo cx16logo
-------- --------

View File

@ -4,6 +4,7 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- vm: implement remaining sin/cos functions in virtual/math.p8 and merge tables - vm: implement remaining sin/cos functions in virtual/math.p8 and merge tables
- fix "can't cast BYTE to UWORD this should have been checked in the astchecker" crash
... ...

View File

@ -35,21 +35,21 @@ main {
graphics.line(256,0,256,height-1) graphics.line(256,0,256,height-1)
ubyte pixelyb ubyte pixelyb
uword pixelyw
ubyte pixelxb ubyte pixelxb
byte pixelys
for pixelxb in 0 to 255 { for pixelxb in 0 to 255 {
pixelyb = math.cos8u(pixelxb) / 2 pixelyb = math.cos8u(pixelxb) / 2
graphics.plot(pixelxb, pixelyb) graphics.plot(pixelxb, pixelyb+20)
pixelyb = math.sin8u(pixelxb) / 2 pixelyb = math.sin8u(pixelxb) / 2
graphics.plot(pixelxb, pixelyb) graphics.plot(pixelxb, pixelyb+20)
} }
for pixelxb in 0 to 255 { for pixelxb in 0 to 255 {
pixelyw = math.cos16u(pixelxb) / 1024 + 120 pixelys = math.cos8(pixelxb) / 2
graphics.plot(pixelxb, pixelyw) graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error
pixelyw = math.sin16u(pixelxb) / 1024 + 120 pixelys = math.sin8(pixelxb) / 2
graphics.plot(pixelxb, pixelyw) graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error
} }
} }
@ -57,21 +57,21 @@ main {
graphics.line(180,0,180,height-1) graphics.line(180,0,180,height-1)
ubyte pixelyb ubyte pixelyb
uword pixelyw
ubyte pixelxb ubyte pixelxb
byte pixelys
for pixelxb in 0 to 179 { for pixelxb in 0 to 179 {
pixelyb = math.cosr8u(pixelxb) / 2 pixelyb = math.cosr8u(pixelxb) / 2
graphics.plot(pixelxb, pixelyb) graphics.plot(pixelxb, pixelyb+20)
pixelyb = math.sinr8u(pixelxb) / 2 pixelyb = math.sinr8u(pixelxb) / 2
graphics.plot(pixelxb, pixelyb) graphics.plot(pixelxb, pixelyb+20)
} }
for pixelxb in 0 to 179 { for pixelxb in 0 to 179 {
pixelyw = math.cosr16u(pixelxb) / 1024 + 120 pixelys = math.cosr8(pixelxb) / 2
graphics.plot(pixelxb, pixelyw) graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error
pixelyw = math.sinr16u(pixelxb) / 1024 + 120 pixelys = math.sinr8(pixelxb) / 2
graphics.plot(pixelxb, pixelyw) graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error
} }
} }
@ -80,7 +80,7 @@ main {
uword pixelxw uword pixelxw
ubyte r ubyte r
; circles with "degrees" from 0 to 255 ; circle with "degrees" from 0 to 255
for r in 0 to 255 { for r in 0 to 255 {
pixelxw = ((math.sin8(r)/2 as word) + 80) as uword pixelxw = ((math.sin8(r)/2 as word) + 80) as uword
pixelyb = (math.cos8(r)/2 + height/2) as ubyte pixelyb = (math.cos8(r)/2 + height/2) as ubyte
@ -88,12 +88,12 @@ main {
} }
for r in 0 to 255 { for r in 0 to 255 {
pixelxw = (math.sin16(r)/1024 + 80) as uword pixelxw = math.sin8u(r)/2
pixelyb = (math.cos16(r)/1024 + height/2) as ubyte pixelyb = math.cos8u(r)/2
graphics.plot(pixelxw, pixelyb) graphics.plot(pixelxw + 16, pixelyb+50)
} }
; circles with half-degrees from 0 to 179 (=full degrees 0..358 with steps of 2 degrees) ; circle with half-degrees from 0 to 179 (=full degrees 0..358 with steps of 2 degrees)
for r in 0 to 179 { for r in 0 to 179 {
pixelxw = (math.sinr8(r) as word /2 + 220) as uword pixelxw = (math.sinr8(r) as word /2 + 220) as uword
pixelyb = (math.cosr8(r)/2 + height/2) as ubyte pixelyb = (math.cosr8(r)/2 + height/2) as ubyte
@ -101,10 +101,9 @@ main {
} }
for r in 0 to 179 { for r in 0 to 179 {
pixelxw = (math.sinr16(r) as word /1024 + 220) as uword pixelxw = math.sinr8u(r)/2
pixelyb = (math.cosr16(r)/1024 + height/2) as ubyte pixelyb = math.cosr8u(r)/2
graphics.plot(pixelxw, pixelyb) graphics.plot(pixelxw + 156, pixelyb+50)
} }
} }
} }