X-Git-Url: http://git.subgeniuskitty.com/vvhitespace/.git/blobdiff_plain/37372ed0cccf20298fdefc4c3d1153eb58407e45..d5a0b33775da13d1589252f75f2f86b81ffff599:/stdlib/math.pvvs diff --git a/stdlib/math.pvvs b/stdlib/math.pvvs index 742522e..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 @@ -5,15 +8,61 @@ @ Name: @ random (10000) @ Description: -@ Returns a kinda-random number. -@ Before using for the first time, seed heap[0] with a value. +@ Returns a pseudo-random number. +@ Before using for the first time, seed heap[0] with a non-zero value. +@ This PRNG was taken from: https://en.wikipedia.org/wiki/Xorshift @ Call Stack: @ empty @ Return Stack: @ random number <-- TOS @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +#include NSSVTSSSSN | Mark: 10000 (random) +@ Fetch seed from heap[0]. +SSSSN | PUSH 0 (ptr) +TTT | LOAD + +@ Set TOS ^= TOS << 13 +SNS | DUP +SSSTTSTN | PUSH +13 +NSTTSTTSTN | JSR > 101101 (lshift) +NSTTSTSTTN | JSR > 101011 (xor) + +@ Set TOS ^= TOS >> 7 +SNS | DUP +SSSTTTN | PUSH +7 +NSTTSTTSSN | JSR > 101100 (rshift) +NSTTSTSTTN | JSR > 101011 (xor) + +@ Set TOS ^= TOS << 17 +SNS | DUP +SSSTSSSTN | PUSH +17 +NSTTSTTSTN | JSR > 101101 (lshift) +NSTTSTSTTN | JSR > 101011 (xor) + +@ Store a copy of the new seed at heap[0] and return. +SNS | DUP +SSSSN | PUSH 0 (ptr) +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