@ (c) 2020 Aaron Taylor <ataylor at subgeniuskitty dot com>
@ See LICENSE.txt file for copyright and license details.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSSN | Mark: 10000 (random)
@ Fetch seed from heap[0].
NSTTSTTSTN | JSR > 101101 (lshift)
NSTTSTSTTN | JSR > 101011 (xor)
NSTTSTTSSN | JSR > 101100 (rshift)
NSTTSTSTTN | JSR > 101011 (xor)
NSTTSTTSTN | JSR > 101101 (lshift)
NSTTSTSTTN | JSR > 101011 (xor)
@ Store a copy of the new seed at heap[0] and return.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Returns the absolute value of its argument
@ abs(signed number) <-- TOS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSSTN | Mark: 10001 (absolute value)
@ Catch -(2^63) as a special case since its absolute value will overflow
@ a twos-complement 64-bit word. Return zero as though the absolute value
@ overflowed to the bottom of the non-negative integers rather than
@ overflowing back to the most negative integer.
SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63)
NTSSSSTSSSTSSSSSSTSN | BRZ > 00010001 00000010
@ Handle all the other numbers.
NTTSSSTSSSTSSSSSSSSN | BMI > 00010001 00000000
NSNSSSTSSSTSSSSSSSTN | JMP > 00010001 00000001
NSSVSSSTSSSTSSSSSSSSN | Mark: 00010001 00000000
NSSVSSSTSSSTSSSSSSSTN | Mark: 00010001 00000001
@ Special case: Push 0 and return.
NSSVSSSTSSSTSSSSSSTSN | Mark: 00010001 00000010
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Returns greatest common divisor of X and Y.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NSSVTSSTSN | Mark: 10010 (gcd)
@ Since 1 > -1, transform problem into gcd(abs(X),abs(Y)).
NSTTSSSTN | JSR > 10001 (abs)
NSTTSSSTN | JSR > 10001 (abs)
@ Verify neither operand is zero.
NTSSSSTSSTSSSSSSSSSN | BRZ > 00010010 00000000 (gcd:zero input)
NTSSSSTSSTSSSSSSSSSN | BRZ > 00010010 00000000 (gcd:zero input)
@ Verify X != Y and sort X,Y so the smaller is TOS.
NSTTTSSN | JSR > 1100 (deepdup)
NTTSSSTSSTSSSSSSSSTN | BMI > 00010010 00000001 (gcd:swap inputs)
NSNSSSTSSTSSSSSSSTSN | JMP > 00010010 00000010 (gcd:main loop)
NSSVSSSTSSTSSSSSSSSTN | MARK: 00010010 00000001 (gcd:swap inputs)
NSSVSSSTSSTSSSSSSSTSN | MARK: 00010010 00000010 (gcd:main loop)
NSTTSTSN | JSR > 1010 (stackrotate)
NTSSSSTSSTSSSSSSSTTN | BRZ > 00010010 00000011 (gcd:loop termination)
NSNSSSTSSTSSSSSSSTSN | JMP > 00010010 00000010 (gcd:main loop)
NSSVSSSTSSTSSSSSSSTTN | MARK: 00010010 00000011 (gcd:loop termination)
@ At least one operand was zero.
@ Since we define gcd(a,0) = a, return the other operand.
NSSVSSSTSSTSSSSSSSSSN | MARK: 00010010 00000000 (gcd:zero input)