mirror of
https://github.com/irmen/prog8.git
synced 2025-01-10 20:30:23 +00:00
fix bench8 examples
This commit is contained in:
parent
b570bdaed7
commit
0bbbb12ed2
@ -141,7 +141,6 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({
|
||||
listOf(
|
||||
"animals",
|
||||
"balls",
|
||||
"bsieve",
|
||||
"cube3d",
|
||||
"cube3d-float",
|
||||
"cube3d-gfx",
|
||||
|
@ -1,11 +1,7 @@
|
||||
TODO
|
||||
====
|
||||
|
||||
- examples/bench8/crc16 produces wrong answer (expected ffd0 in 4.6 sec)
|
||||
- examples/bench8/crc32 produces wrong answer (expected e1fa84c6 in 40.1 sec)
|
||||
|
||||
- add bench8/sieve and sieve-bit ports
|
||||
|
||||
- fix ir/vm when running bench8/sieve-bit, it produces wrong result
|
||||
|
||||
- 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 ....
|
||||
|
@ -1,11 +1,12 @@
|
||||
%import textio
|
||||
%import floats
|
||||
|
||||
main {
|
||||
|
||||
sub crc16(uword data, uword length) -> uword {
|
||||
uword crc = 0
|
||||
repeat length {
|
||||
crc ^= @(data) << $0008
|
||||
crc ^= mkword(@(data), 0)
|
||||
repeat 8 {
|
||||
if crc & $8000
|
||||
crc = (crc<<1)^$1021
|
||||
@ -23,8 +24,8 @@ main {
|
||||
uword crc = crc16($e000, $2000)
|
||||
txt.print_uwhex(crc, true)
|
||||
txt.nl()
|
||||
txt.print_uw(cbm.RDTIM16())
|
||||
txt.print(" jiffies")
|
||||
sys.wait(300)
|
||||
floats.print_f(cbm.RDTIM16() / 60.0)
|
||||
txt.print(" seconds")
|
||||
sys.wait(9999)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
%import textio
|
||||
%import floats
|
||||
|
||||
main {
|
||||
sub crc32(uword data, uword length) {
|
||||
@ -7,7 +8,7 @@ main {
|
||||
cx16.r0 = 0
|
||||
cx16.r1 = 0
|
||||
repeat length {
|
||||
cx16.r0 ^= @(data) << $0008
|
||||
cx16.r0 ^= mkword(@(data), 0)
|
||||
repeat 8 {
|
||||
if cx16.r0 & $8000 {
|
||||
sys.clear_carry()
|
||||
@ -35,8 +36,8 @@ main {
|
||||
txt.print_uwhex(cx16.r0, true)
|
||||
txt.print_uwhex(cx16.r1, false)
|
||||
txt.nl()
|
||||
txt.print_uw(cbm.RDTIM16())
|
||||
txt.print(" jiffies")
|
||||
sys.wait(300)
|
||||
floats.print_f(cbm.RDTIM16() / 60.0)
|
||||
txt.print(" seconds")
|
||||
sys.wait(9999)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
%import textio
|
||||
%import floats
|
||||
|
||||
main {
|
||||
|
||||
@ -23,8 +24,8 @@ main {
|
||||
ubyte crc = crc8($e000, $2000)
|
||||
txt.print_ubhex(crc, true)
|
||||
txt.nl()
|
||||
txt.print_uw(cbm.RDTIM16())
|
||||
txt.print(" jiffies")
|
||||
sys.wait(300)
|
||||
floats.print_f(cbm.RDTIM16() / 60.0)
|
||||
txt.print(" seconds")
|
||||
sys.wait(9999)
|
||||
}
|
||||
}
|
||||
|
@ -35,8 +35,8 @@ main {
|
||||
|
||||
floats.print_f(res)
|
||||
txt.nl()
|
||||
txt.print_uw(cbm.RDTIM16())
|
||||
txt.print(" jiffies")
|
||||
sys.wait(300)
|
||||
floats.print_f(cbm.RDTIM16() / 60.0)
|
||||
txt.print(" seconds")
|
||||
sys.wait(9999)
|
||||
}
|
||||
}
|
||||
|
64
examples/bench8/sieve-bit.p8
Normal file
64
examples/bench8/sieve-bit.p8
Normal 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
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
%import textio
|
||||
%import floats
|
||||
%zeropage basicsafe
|
||||
%option no_sysinit
|
||||
|
||||
; The "Byte Sieve" test. https://en.wikipedia.org/wiki/Byte_Sieve
|
||||
; Note: this program can be compiled for multiple target systems.
|
||||
@ -9,7 +7,6 @@
|
||||
main {
|
||||
sub start() {
|
||||
|
||||
cbm.SETTIM(0, 0, 0)
|
||||
|
||||
const ubyte ITERS = 10
|
||||
uword count
|
||||
@ -22,15 +19,17 @@ main {
|
||||
txt.print_ub(ITERS)
|
||||
txt.print(" iterations, calculating...\n")
|
||||
|
||||
cbm.SETTIM(0, 0, 0)
|
||||
|
||||
repeat ITERS {
|
||||
sys.memset(flags_ptr, SIZEPL, 1)
|
||||
count = 0
|
||||
for i in 0 to SIZEPL-1 {
|
||||
if @(flags_ptr+i) {
|
||||
prime = i + i + 3
|
||||
if flags_ptr[i] {
|
||||
prime = i*2 + 3
|
||||
k = i + prime
|
||||
while k <= SIZEPL-1 {
|
||||
@(flags_ptr + k) = false
|
||||
while k < SIZEPL {
|
||||
flags_ptr[k] = false
|
||||
k += prime
|
||||
}
|
||||
; txt.print_uw(prime)
|
||||
@ -48,5 +47,6 @@ main {
|
||||
txt.print(" sec total = ")
|
||||
floats.print_f(time/ITERS)
|
||||
txt.print(" sec per iteration\n")
|
||||
sys.wait(9999)
|
||||
}
|
||||
}
|
@ -2,27 +2,54 @@
|
||||
%import floats
|
||||
%zeropage basicsafe
|
||||
|
||||
main {
|
||||
float[4] array
|
||||
; TODO fix VM : produces wrong number of primes (and varies too, so it uses uninitialized memory somewhere)
|
||||
|
||||
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() {
|
||||
float res
|
||||
ubyte j = 2
|
||||
res += testpow(1.234)
|
||||
floats.print_f(res)
|
||||
txt.nl()
|
||||
floats.print_f(array[j])
|
||||
txt.nl()
|
||||
txt.nl()
|
||||
txt.print("calculating... (expecting 3431): ")
|
||||
txt.print_uw(sieve())
|
||||
txt.print(" primes\n")
|
||||
}
|
||||
|
||||
array[j] += testpow(1.234)
|
||||
floats.print_f(res)
|
||||
txt.nl()
|
||||
floats.print_f(array[j])
|
||||
txt.nl()
|
||||
sub check_flag(uword idx) -> ubyte
|
||||
{
|
||||
ubyte mask = bitv[lsb(idx)&7]
|
||||
ubyte flag = flags_ptr[idx/8]
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ main {
|
||||
sys.memset(flags_ptr, SIZEPL, 1)
|
||||
count = 0
|
||||
for i in 0 to SIZEPL-1 {
|
||||
if @(flags_ptr+i) {
|
||||
if flags_ptr[i] {
|
||||
prime = i + i + 3
|
||||
k = i + prime
|
||||
while k <= SIZEPL-1 {
|
||||
@(flags_ptr + k) = false
|
||||
flags_ptr[k] = false
|
||||
k += prime
|
||||
}
|
||||
txt.print_uw(prime)
|
||||
|
Loading…
x
Reference in New Issue
Block a user