From d5a0b33775da13d1589252f75f2f86b81ffff599 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Tue, 31 Mar 2020 01:22:00 -0700 Subject: [PATCH] Added old `random` function back as `fastrand`. --- stdlib/README.md | 1 + stdlib/math.pvvs | 36 +++++++++++++++++++++++++++++++++ stdlib_tests/5004_fastrand.pvvs | 10 +++++++++ stdlib_tests/vv_test.py | 1 + 4 files changed, 48 insertions(+) create mode 100644 stdlib_tests/5004_fastrand.pvvs diff --git a/stdlib/README.md b/stdlib/README.md index 6f12f4a..6c26770 100644 --- a/stdlib/README.md +++ b/stdlib/README.md @@ -95,6 +95,7 @@ header comment for each function to learn the call and return stack. 10000 ----- random (math.pvvs) 10001 ----- absolute value (math.pvvs) 10010 ----- greatest common divisor (math.pvvs) + 10011 ----- fastrand (math.pvvs) 011xxx - heap functions 11000 ----- memset (heap.pvvs) 11001 ----- memcpy (heap.pvvs) diff --git a/stdlib/math.pvvs b/stdlib/math.pvvs index 90a3666..eac0181 100644 --- a/stdlib/math.pvvs +++ b/stdlib/math.pvvs @@ -48,6 +48,42 @@ SNT | SWAP TTS | STORE NTN | RTS +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Name: +@ fastrand (10011) +@ Description: +@ Returns a pseudo-random number. +@ Probably not as good as `random`, but much faster since it doesn't use XOR. +@ Based on the POSIX.1-2001 example for random(). +@ Before using for the first time, seed heap[0] with a value. +@ Call Stack: +@ empty +@ Return Stack: +@ random number <-- TOS +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +NSSVTSSTTN | Mark: 10011 (fastrand) + +@ Generate the next seed value +SSSSN | PUSH 0 (ptr) +TTT | LOAD +SSSTSSSSSTTTSSSTTSSTSSTTTSSTTSTTSTN | PUSH 1103515245 +TSSN | MULTIPLY +SSSTTSSSSSSTTTSSTN | PUSH 12345 +TSSS | ADD + +@ Store the next seed value but keep a copy on the stack. +SNS | DUP +SSSSN | PUSH 0 (ptr) +SNT | SWAP +TTS | STORE + +@ Calculate the random number and return. +SSSTSSSSSSSSSSSSSSSSN | PUSH 65536 +TSTS | DIVIDE +SSSTSSSSSSSSSSSSSSSN | PUSH 32768 +TSTT | MODULO +NTN | RTS + @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ Name: @ abs (10001) diff --git a/stdlib_tests/5004_fastrand.pvvs b/stdlib_tests/5004_fastrand.pvvs new file mode 100644 index 0000000..a66670b --- /dev/null +++ b/stdlib_tests/5004_fastrand.pvvs @@ -0,0 +1,10 @@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ +@ This test intentionally blank. +@ +@ If there comes a day when the tests are extended beyond testing basic +@ functionality, include fastrand in the new tests. +@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +NNN | Die diff --git a/stdlib_tests/vv_test.py b/stdlib_tests/vv_test.py index 34d8a26..7db107b 100755 --- a/stdlib_tests/vv_test.py +++ b/stdlib_tests/vv_test.py @@ -46,6 +46,7 @@ tests = [ ['5001_abs', '', '+1+1+0+0'], ['5002_random', '', ''], ['5003_gcd', '', '+0+4+4+3+3+3'], + ['5004_fastrand', '', ''], ['6001_printstackstring', '', 'test'], ['6002_printheapstring', '', 'test'], ['6003_printnumbersign', '', '+-'], -- 2.20.1