Added tests for stdlib math functions.
[vvhitespace] / stdlib / math.pvvs
index 0df09ed..3876c6e 100644 (file)
@@ -2,26 +2,61 @@
 #define VVS_STDLIB_MATH
 
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 #define VVS_STDLIB_MATH
 
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   random (10000)
 @ Description:
 @ Description:
-@   This function returns a random IEEE-vetted number per RFC 1149.5.
+@   Returns a kinda-random number.
+@   Before using for the first time, seed heap[0] with a value.
 @ Call Stack:
 @   empty
 @ Return Stack:
 @   random number  <-- TOS
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NSSVTSSSSN              | Mark: 10000 (random)
 @ Call Stack:
 @   empty
 @ Return Stack:
 @   random number  <-- TOS
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NSSVTSSSSN              | Mark: 10000 (random)
-SSSTSSN                 | PUSH +4
+
+@ 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
 
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NTN                     | RTS
 
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+@ Name:
+@   abs (10001)
 @ Description:
 @ Description:
-@   This function returns the absolute value of its argument
+@   Returns the absolute value of its argument
 @ Call Stack:
 @   signed number  <-- TOS
 @ Return Stack:
 @   abs(signed number)  <-- TOS
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 NSSVTSSSTN              | Mark: 10001 (absolute value)
 @ Call Stack:
 @   signed number  <-- TOS
 @ Return Stack:
 @   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.
+SNS                     | DUP
+SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63)
+TSST                    | SUBTRACT
+NTSSSSTSSSTSSSSSSTSN    | BRZ > 00010001 00000010
+
+@ Handle all the other numbers.
 SNS                     | DUP
 NTTSSSTSSSTSSSSSSSSN    | BMI > 00010001 00000000
 NSNSSSTSSSTSSSSSSSTN    | JMP > 00010001 00000001
 SNS                     | DUP
 NTTSSSTSSSTSSSSSSSSN    | BMI > 00010001 00000000
 NSNSSSTSSSTSSSSSSSTN    | JMP > 00010001 00000001
@@ -31,4 +66,10 @@ TSSN                    | MULTIPLY
 NSSVSSSTSSSTSSSSSSSTN   | Mark: 00010001 00000001
 NTN                     | RTS
 
 NSSVSSSTSSSTSSSSSSSTN   | Mark: 00010001 00000001
 NTN                     | RTS
 
+@ Special case: Push 0 and return.
+NSSVSSSTSSSTSSSSSSTSN   | Mark: 00010001 00000010
+SNN                     | DROP
+SSSSN                   | PUSH 0
+NTN                     | RTS
+
 #endif
 #endif