Added tests for stdlib math functions.
[vvhitespace] / stdlib / math.pvvs
CommitLineData
3625ff3a
AT
1#ifndef VVS_STDLIB_MATH
2#define VVS_STDLIB_MATH
3
2612f47f 4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a
AT
5@ Name:
6@ random (10000)
2612f47f 7@ Description:
b15f1da5
AT
8@ Returns a kinda-random number.
9@ Before using for the first time, seed heap[0] with a value.
2612f47f
AT
10@ Call Stack:
11@ empty
12@ Return Stack:
13@ random number <-- TOS
14@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
15NSSVTSSSSN | Mark: 10000 (random)
b15f1da5
AT
16
17@ Generate the next seed value
18SSSSN | PUSH 0 (ptr)
19TTT | LOAD
20SSSTSSSSSTTTSSSTTSSTSSTTTSSTTSTTSTN | PUSH 1103515245
21TSSN | MULTIPLY
22SSSTTSSSSSSTTTSSTN | PUSH 12345
23TSSS | ADD
24
25@ Store the next seed value but keep a copy on the stack.
26SNS | DUP
27SSSSN | PUSH 0 (ptr)
28SNT | SWAP
29TTS | STORE
30
31@ Calculate the random number and return.
32SSSTSSSSSSSSSSSSSSSSN | PUSH 65536
33TSTS | DIVIDE
34SSSTSSSSSSSSSSSSSSSN | PUSH 32768
35TSTT | MODULO
2612f47f
AT
36NTN | RTS
37
3625ff3a 38@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a
AT
39@ Name:
40@ abs (10001)
3625ff3a 41@ Description:
bb21580a 42@ Returns the absolute value of its argument
3625ff3a
AT
43@ Call Stack:
44@ signed number <-- TOS
45@ Return Stack:
46@ abs(signed number) <-- TOS
47@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
48NSSVTSSSTN | Mark: 10001 (absolute value)
7359501c
AT
49
50@ Catch -(2^63) as a special case since its absolute value will overflow
51@ a twos-complement 64-bit word. Return zero as though the absolute value
52@ overflowed to the bottom of the non-negative integers rather than
53@ overflowing back to the most negative integer.
54SNS | DUP
55SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63)
56TSST | SUBTRACT
57NTSSSSTSSSTSSSSSSTSN | BRZ > 00010001 00000010
58
59@ Handle all the other numbers.
3625ff3a
AT
60SNS | DUP
61NTTSSSTSSSTSSSSSSSSN | BMI > 00010001 00000000
62NSNSSSTSSSTSSSSSSSTN | JMP > 00010001 00000001
63NSSVSSSTSSSTSSSSSSSSN | Mark: 00010001 00000000
64SSTTN | PUSH -1
65TSSN | MULTIPLY
66NSSVSSSTSSSTSSSSSSSTN | Mark: 00010001 00000001
67NTN | RTS
68
7359501c
AT
69@ Special case: Push 0 and return.
70NSSVSSSTSSSTSSSSSSTSN | Mark: 00010001 00000010
71SNN | DROP
72SSSSN | PUSH 0
73NTN | RTS
74
3625ff3a 75#endif