From 97aa91c75efad477c80e7c64fca28b9582c5d463 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 22 Sep 2022 12:42:17 +0200 Subject: [PATCH] removed 16 bits sin/cos routines from math library (sin16, sin16r etc) --- .../cpu6502/assignment/AssignmentAsmGen.kt | 3 + compiler/res/prog8lib/math.p8 | 107 ------------------ compiler/res/prog8lib/virtual/math.p8 | 35 +----- docs/source/libraries.rst | 31 +---- docs/source/todo.rst | 1 + examples/cx16/sincos.p8 | 45 ++++---- 6 files changed, 29 insertions(+), 193 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index e17594500..af0b418c5 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -974,6 +974,9 @@ internal class AssignmentAsmGen(private val program: Program, } else if(valueDt in ByteDatatypes && targetDt in ByteDatatypes) { // byte to byte, just assign 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 throw AssemblyError("can't cast $valueDt to $targetDt, this should have been checked in the astchecker") diff --git a/compiler/res/prog8lib/math.p8 b/compiler/res/prog8lib/math.p8 index 97d2e4f1e..7564760d2 100644 --- a/compiler/res/prog8lib/math.p8 +++ b/compiler/res/prog8lib/math.p8 @@ -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 { %asm {{ 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 - }} - } } diff --git a/compiler/res/prog8lib/virtual/math.p8 b/compiler/res/prog8lib/virtual/math.p8 index b8b107e11..00882523a 100644 --- a/compiler/res/prog8lib/virtual/math.p8 +++ b/compiler/res/prog8lib/virtual/math.p8 @@ -19,7 +19,7 @@ math { $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, $5a, $5d, $61, $64, $67, $6a, $6d, $70, $73, $76, $79, $7c] - return sintab[angle] + return sintab[angle] } 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, $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 ] - return costab[angle] + return costab[angle] } sub sin8(ubyte angle) -> byte { @@ -51,22 +51,6 @@ math { 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 { ubyte[180] sintab = [ $80, $84, $88, $8d, @@ -109,19 +93,4 @@ math { 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 - } } diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index 4e3dbb0d8..7b29748ac 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -273,7 +273,7 @@ math 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. -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 application needs this, but perhaps the provided ones can be of service too: @@ -283,12 +283,6 @@ sin8u(x) sin8(x) 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) 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! @@ -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 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) Fast 8-bit ubyte cosine of angle 0..255, result is in range 0..255 cos8(x) 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) 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! @@ -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 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 -------- diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 499a55bd8..6d8c92413 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,6 +4,7 @@ TODO For next release ^^^^^^^^^^^^^^^^ - 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 ... diff --git a/examples/cx16/sincos.p8 b/examples/cx16/sincos.p8 index 9e2f339d6..c29dd4537 100644 --- a/examples/cx16/sincos.p8 +++ b/examples/cx16/sincos.p8 @@ -35,21 +35,21 @@ main { graphics.line(256,0,256,height-1) ubyte pixelyb - uword pixelyw ubyte pixelxb + byte pixelys for pixelxb in 0 to 255 { pixelyb = math.cos8u(pixelxb) / 2 - graphics.plot(pixelxb, pixelyb) + graphics.plot(pixelxb, pixelyb+20) pixelyb = math.sin8u(pixelxb) / 2 - graphics.plot(pixelxb, pixelyb) + graphics.plot(pixelxb, pixelyb+20) } for pixelxb in 0 to 255 { - pixelyw = math.cos16u(pixelxb) / 1024 + 120 - graphics.plot(pixelxb, pixelyw) - pixelyw = math.sin16u(pixelxb) / 1024 + 120 - graphics.plot(pixelxb, pixelyw) + pixelys = math.cos8(pixelxb) / 2 + graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error + pixelys = math.sin8(pixelxb) / 2 + 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) ubyte pixelyb - uword pixelyw ubyte pixelxb + byte pixelys for pixelxb in 0 to 179 { pixelyb = math.cosr8u(pixelxb) / 2 - graphics.plot(pixelxb, pixelyb) + graphics.plot(pixelxb, pixelyb+20) pixelyb = math.sinr8u(pixelxb) / 2 - graphics.plot(pixelxb, pixelyb) + graphics.plot(pixelxb, pixelyb+20) } for pixelxb in 0 to 179 { - pixelyw = math.cosr16u(pixelxb) / 1024 + 120 - graphics.plot(pixelxb, pixelyw) - pixelyw = math.sinr16u(pixelxb) / 1024 + 120 - graphics.plot(pixelxb, pixelyw) + pixelys = math.cosr8(pixelxb) / 2 + graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error + pixelys = math.sinr8(pixelxb) / 2 + graphics.plot(pixelxb, pixelys+90 as ubyte as uword) ; TODO fix weird cast error } } @@ -80,7 +80,7 @@ main { uword pixelxw ubyte r - ; circles with "degrees" from 0 to 255 + ; circle with "degrees" from 0 to 255 for r in 0 to 255 { pixelxw = ((math.sin8(r)/2 as word) + 80) as uword pixelyb = (math.cos8(r)/2 + height/2) as ubyte @@ -88,12 +88,12 @@ main { } for r in 0 to 255 { - pixelxw = (math.sin16(r)/1024 + 80) as uword - pixelyb = (math.cos16(r)/1024 + height/2) as ubyte - graphics.plot(pixelxw, pixelyb) + pixelxw = math.sin8u(r)/2 + pixelyb = math.cos8u(r)/2 + 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 { pixelxw = (math.sinr8(r) as word /2 + 220) as uword pixelyb = (math.cosr8(r)/2 + height/2) as ubyte @@ -101,10 +101,9 @@ main { } for r in 0 to 179 { - pixelxw = (math.sinr16(r) as word /1024 + 220) as uword - pixelyb = (math.cosr16(r)/1024 + height/2) as ubyte - graphics.plot(pixelxw, pixelyb) + pixelxw = math.sinr8u(r)/2 + pixelyb = math.cosr8u(r)/2 + graphics.plot(pixelxw + 156, pixelyb+50) } - } }