X-Git-Url: http://git.subgeniuskitty.com/vvhitespace/.git/blobdiff_plain/f969012f20e1d28118c4589dcc3d6ef689214533..d5a0b33775da13d1589252f75f2f86b81ffff599:/stdlib/math.pvvs diff --git a/stdlib/math.pvvs b/stdlib/math.pvvs index a72203f..eac0181 100644 --- a/stdlib/math.pvvs +++ b/stdlib/math.pvvs @@ -1,3 +1,6 @@ +@ (c) 2020 Aaron Taylor +@ See LICENSE.txt file for copyright and license details. + #ifndef VVS_STDLIB_MATH #define VVS_STDLIB_MATH @@ -45,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)