Commit | Line | Data |
---|---|---|
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 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
15 | NSSVTSSSSN | Mark: 10000 (random) | |
b15f1da5 AT |
16 | |
17 | @ Generate the next seed value | |
18 | SSSSN | PUSH 0 (ptr) | |
19 | TTT | LOAD | |
20 | SSSTSSSSSTTTSSSTTSSTSSTTTSSTTSTTSTN | PUSH 1103515245 | |
21 | TSSN | MULTIPLY | |
22 | SSSTTSSSSSSTTTSSTN | PUSH 12345 | |
23 | TSSS | ADD | |
24 | ||
25 | @ Store the next seed value but keep a copy on the stack. | |
26 | SNS | DUP | |
27 | SSSSN | PUSH 0 (ptr) | |
28 | SNT | SWAP | |
29 | TTS | STORE | |
30 | ||
31 | @ Calculate the random number and return. | |
32 | SSSTSSSSSSSSSSSSSSSSN | PUSH 65536 | |
33 | TSTS | DIVIDE | |
34 | SSSTSSSSSSSSSSSSSSSN | PUSH 32768 | |
35 | TSTT | MODULO | |
2612f47f AT |
36 | NTN | 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 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | |
48 | NSSVTSSSTN | 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. | |
54 | SNS | DUP | |
55 | SSTTSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSN | -(2^63) | |
56 | TSST | SUBTRACT | |
57 | NTSSSSTSSSTSSSSSSTSN | BRZ > 00010001 00000010 | |
58 | ||
59 | @ Handle all the other numbers. | |
3625ff3a AT |
60 | SNS | DUP |
61 | NTTSSSTSSSTSSSSSSSSN | BMI > 00010001 00000000 | |
62 | NSNSSSTSSSTSSSSSSSTN | JMP > 00010001 00000001 | |
63 | NSSVSSSTSSSTSSSSSSSSN | Mark: 00010001 00000000 | |
64 | SSTTN | PUSH -1 | |
65 | TSSN | MULTIPLY | |
66 | NSSVSSSTSSSTSSSSSSSTN | Mark: 00010001 00000001 | |
67 | NTN | RTS | |
68 | ||
7359501c AT |
69 | @ Special case: Push 0 and return. |
70 | NSSVSSSTSSSTSSSSSSTSN | Mark: 00010001 00000010 | |
71 | SNN | DROP | |
72 | SSSSN | PUSH 0 | |
73 | NTN | RTS | |
74 | ||
3625ff3a | 75 | #endif |