fix bench8 examples

This commit is contained in:
Irmen de Jong 2023-09-05 22:59:36 +02:00
parent b570bdaed7
commit 0bbbb12ed2
10 changed files with 136 additions and 47 deletions

View File

@ -141,7 +141,6 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({
listOf( listOf(
"animals", "animals",
"balls", "balls",
"bsieve",
"cube3d", "cube3d",
"cube3d-float", "cube3d-float",
"cube3d-gfx", "cube3d-gfx",

View File

@ -1,11 +1,7 @@
TODO TODO
==== ====
- examples/bench8/crc16 produces wrong answer (expected ffd0 in 4.6 sec) - fix ir/vm when running bench8/sieve-bit, it produces wrong result
- examples/bench8/crc32 produces wrong answer (expected e1fa84c6 in 40.1 sec)
- add bench8/sieve and sieve-bit ports
- prefix prog8 subroutines with p8s_ instead of p8_ to not let them clash with variables in the asm?? - prefix prog8 subroutines with p8s_ instead of p8_ to not let them clash with variables in the asm??
- [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... - [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....

View File

@ -1,11 +1,12 @@
%import textio %import textio
%import floats
main { main {
sub crc16(uword data, uword length) -> uword { sub crc16(uword data, uword length) -> uword {
uword crc = 0 uword crc = 0
repeat length { repeat length {
crc ^= @(data) << $0008 crc ^= mkword(@(data), 0)
repeat 8 { repeat 8 {
if crc & $8000 if crc & $8000
crc = (crc<<1)^$1021 crc = (crc<<1)^$1021
@ -23,8 +24,8 @@ main {
uword crc = crc16($e000, $2000) uword crc = crc16($e000, $2000)
txt.print_uwhex(crc, true) txt.print_uwhex(crc, true)
txt.nl() txt.nl()
txt.print_uw(cbm.RDTIM16()) floats.print_f(cbm.RDTIM16() / 60.0)
txt.print(" jiffies") txt.print(" seconds")
sys.wait(300) sys.wait(9999)
} }
} }

View File

@ -1,4 +1,5 @@
%import textio %import textio
%import floats
main { main {
sub crc32(uword data, uword length) { sub crc32(uword data, uword length) {
@ -7,7 +8,7 @@ main {
cx16.r0 = 0 cx16.r0 = 0
cx16.r1 = 0 cx16.r1 = 0
repeat length { repeat length {
cx16.r0 ^= @(data) << $0008 cx16.r0 ^= mkword(@(data), 0)
repeat 8 { repeat 8 {
if cx16.r0 & $8000 { if cx16.r0 & $8000 {
sys.clear_carry() sys.clear_carry()
@ -35,8 +36,8 @@ main {
txt.print_uwhex(cx16.r0, true) txt.print_uwhex(cx16.r0, true)
txt.print_uwhex(cx16.r1, false) txt.print_uwhex(cx16.r1, false)
txt.nl() txt.nl()
txt.print_uw(cbm.RDTIM16()) floats.print_f(cbm.RDTIM16() / 60.0)
txt.print(" jiffies") txt.print(" seconds")
sys.wait(300) sys.wait(9999)
} }
} }

View File

@ -1,4 +1,5 @@
%import textio %import textio
%import floats
main { main {
@ -23,8 +24,8 @@ main {
ubyte crc = crc8($e000, $2000) ubyte crc = crc8($e000, $2000)
txt.print_ubhex(crc, true) txt.print_ubhex(crc, true)
txt.nl() txt.nl()
txt.print_uw(cbm.RDTIM16()) floats.print_f(cbm.RDTIM16() / 60.0)
txt.print(" jiffies") txt.print(" seconds")
sys.wait(300) sys.wait(9999)
} }
} }

View File

@ -35,8 +35,8 @@ main {
floats.print_f(res) floats.print_f(res)
txt.nl() txt.nl()
txt.print_uw(cbm.RDTIM16()) floats.print_f(cbm.RDTIM16() / 60.0)
txt.print(" jiffies") txt.print(" seconds")
sys.wait(300) sys.wait(9999)
} }
} }

View File

@ -0,0 +1,64 @@
%import textio
%import floats
main {
const ubyte N_ITER = 4
const uword SIZE = 16000
uword @zp flags_ptr = memory("flags", SIZE/8+1, $100)
ubyte[] bitv = [ $01, $02, $04, $08, $10, $20, $40, $80 ]
sub start() {
txt.print_ub(N_ITER)
txt.print(" iterations, calculating... (expecting 3431)\n")
cbm.SETTIM(0, 0, 0)
uword prime_count
repeat N_ITER {
prime_count = sieve()
}
txt.print_uw(prime_count)
txt.print(" primes\n")
float time = cbm.RDTIM16() as float / 60.0
floats.print_f(time)
txt.print(" sec total = ")
floats.print_f(time/N_ITER)
txt.print(" sec per iteration\n")
sys.wait(9999)
}
sub check_flag(uword idx) -> ubyte
{
return flags_ptr[idx/8] & bitv[lsb(idx)&7]
}
sub clear_flag(uword idx)
{
flags_ptr[idx/8] &= ~bitv[lsb(idx)&7]
}
sub sieve() -> uword {
uword prime
uword k
uword count=0
uword i
sys.memset(flags_ptr, SIZE/8+1, $ff)
for i in 0 to SIZE-1 {
if check_flag(i) {
prime = i*2 + 3
k = i + prime
while k < SIZE {
clear_flag(k)
k += prime
}
; txt.print_uw(prime)
; txt.spc()
count++
}
}
return count
}
}

View File

@ -1,7 +1,5 @@
%import textio %import textio
%import floats %import floats
%zeropage basicsafe
%option no_sysinit
; The "Byte Sieve" test. https://en.wikipedia.org/wiki/Byte_Sieve ; The "Byte Sieve" test. https://en.wikipedia.org/wiki/Byte_Sieve
; Note: this program can be compiled for multiple target systems. ; Note: this program can be compiled for multiple target systems.
@ -9,7 +7,6 @@
main { main {
sub start() { sub start() {
cbm.SETTIM(0, 0, 0)
const ubyte ITERS = 10 const ubyte ITERS = 10
uword count uword count
@ -22,15 +19,17 @@ main {
txt.print_ub(ITERS) txt.print_ub(ITERS)
txt.print(" iterations, calculating...\n") txt.print(" iterations, calculating...\n")
cbm.SETTIM(0, 0, 0)
repeat ITERS { repeat ITERS {
sys.memset(flags_ptr, SIZEPL, 1) sys.memset(flags_ptr, SIZEPL, 1)
count = 0 count = 0
for i in 0 to SIZEPL-1 { for i in 0 to SIZEPL-1 {
if @(flags_ptr+i) { if flags_ptr[i] {
prime = i + i + 3 prime = i*2 + 3
k = i + prime k = i + prime
while k <= SIZEPL-1 { while k < SIZEPL {
@(flags_ptr + k) = false flags_ptr[k] = false
k += prime k += prime
} }
; txt.print_uw(prime) ; txt.print_uw(prime)
@ -48,5 +47,6 @@ main {
txt.print(" sec total = ") txt.print(" sec total = ")
floats.print_f(time/ITERS) floats.print_f(time/ITERS)
txt.print(" sec per iteration\n") txt.print(" sec per iteration\n")
sys.wait(9999)
} }
} }

View File

@ -2,27 +2,54 @@
%import floats %import floats
%zeropage basicsafe %zeropage basicsafe
main { ; TODO fix VM : produces wrong number of primes (and varies too, so it uses uninitialized memory somewhere)
float[4] array
sub testpow(float x) -> float {
return 1.234 main {
} const uword SIZE = 16000
uword @zp flags_ptr = memory("flags", SIZE/8+1, $100)
ubyte[] bitv = [ $01, $02, $04, $08, $10, $20, $40, $80 ]
sub start() { sub start() {
float res txt.print("calculating... (expecting 3431): ")
ubyte j = 2 txt.print_uw(sieve())
res += testpow(1.234) txt.print(" primes\n")
floats.print_f(res) }
txt.nl()
floats.print_f(array[j])
txt.nl()
txt.nl()
array[j] += testpow(1.234) sub check_flag(uword idx) -> ubyte
floats.print_f(res) {
txt.nl() ubyte mask = bitv[lsb(idx)&7]
floats.print_f(array[j]) ubyte flag = flags_ptr[idx/8]
txt.nl() return flag & mask
}
sub clear_flag(uword idx)
{
ubyte mask = bitv[lsb(idx)&7]
ubyte flag = flags_ptr[idx/8]
flag &= ~mask
flags_ptr[idx/8] = flag
}
sub sieve() -> uword {
uword prime
uword k
uword count=0
uword i
sys.memset(flags_ptr, SIZE/8+1, $ff)
for i in 0 to SIZE-1 {
if check_flag(i) {
prime = i*2 + 3
k = i + prime
while k < SIZE {
clear_flag(k)
k += prime
}
count++
}
}
return count
} }
} }

View File

@ -17,11 +17,11 @@ main {
sys.memset(flags_ptr, SIZEPL, 1) sys.memset(flags_ptr, SIZEPL, 1)
count = 0 count = 0
for i in 0 to SIZEPL-1 { for i in 0 to SIZEPL-1 {
if @(flags_ptr+i) { if flags_ptr[i] {
prime = i + i + 3 prime = i + i + 3
k = i + prime k = i + prime
while k <= SIZEPL-1 { while k <= SIZEPL-1 {
@(flags_ptr + k) = false flags_ptr[k] = false
k += prime k += prime
} }
txt.print_uw(prime) txt.print_uw(prime)