The commands listed in Table 4-4 perform basic arithmetic with items on the data stack.
Table 4-4 Arithmetic Functions|
Command |
Stack Diagram |
Description |
|---|---|---|
| + |
( nu1 nu2 -- sum ) |
Add nu1 + nu2. |
| - |
( nu1 nu2 -- diff ) |
Subtract nu1 - nu2. |
| * |
( nu1 nu2 -- prod ) |
Multiply nu1 * nu2. |
| / |
( n1 n2 -- quot ) |
Divide n1 by n2; remainder is discarded. |
| /mod |
( n1 n2 -- rem quot ) |
Remainder, quotient of n1 / n2. |
| << |
( x1 u -- x2 ) |
Synonym for lshift. |
| >> |
( x1 u -- x2 ) |
Synonym for rshift. |
| >>a |
( x1 u -- x2 ) |
Arithmetic right-shift x1 by u bits. |
| */ |
( n1 n2 n3 -- quot ) |
n1 * n2 / n3. |
| */mod |
( n1 n2 n3 -- rem quot ) |
Remainder, quotient of n1 * n2 / n3. |
| 1+ |
( nu1 -- nu2 ) |
Add 1. |
| 1- |
( nu1 -- nu2 ) |
Subtract 1. |
| 2* |
( nu1 -- nu2 ) |
Multiply by 2. |
| 2+ |
( nu1 -- nu2 ) |
Add 2. |
| 2- |
( nu1 -- nu2 ) |
Subtract 2. |
| 2/ |
( nu1 -- nu2 ) |
Divide by 2. |
| abs |
( n -- u ) |
Absolute value. |
| aligned |
( n1 -- n1 | a-addr) |
Round n1 up to the next multiple of 4. |
| and |
( n1 n2 -- n3 ) |
Bitwise logical AND. |
| bounds |
( startaddr len -- endaddr startaddr ) |
Convert startaddr len to endaddr startaddr for do loop. |
| bljoin |
( b.low b2 b3 b.hi -- quad ) |
Join four bytes to form a 32-bit quadword. |
| bwjoin |
( b.low b.hi -- word ) |
Join two bytes to form a 16-bit word. |
| d+ |
(d1 d2 -- d.sum ) |
Add two 64-bit numbers. |
| d- |
(d1 d2 --d.diff ) |
Subtract two 64-bit numbers. |
| even |
( n -- n | n+1 ) |
Round to nearest even integer >= n. |
| fm/mod |
( d n -- rem quot ) |
Divide d by n. |
| invert |
( x1 -- x2 ) |
Invert all bits of x1. |
| lbflip |
( quad1 -- quad2 ) |
Swap the bytes within a 32-bit quadword |
| lbsplit |
( quad -- b.low b2 b3 b.hi ) |
Split a 32-bit quadword into four bytes. |
| lwflip |
( quad1 -- quad2 ) |
Swap halves of a 32-bit quadword. |
| lwsplit |
( quad -- w.low w.hi ) |
Split a 32-bit quadword into two 16-bit words. |
| lshift |
( x1 u -- x2 ) |
Left-shift x1 by u bits. Zero-fill low bits. |
| max |
( n1 n2 -- n3 ) |
n3 is maximum of n1 and n2. |
| min |
( n1 n2 -- n3 ) |
n3 is minimum of n1 and n2. |
| mod |
( n1 n2 -- rem ) |
Remainder of n1 / n2. |
| negate |
( n1 -- n2 ) |
Change the sign of n1. |
| not |
( x1 -- x2 ) |
Synonym for invert. |
| or |
( n1 n2 -- n3 ) |
Bitwise logical OR. |
| rshift |
( x1 u -- x2 ) |
Right-shift x1 by u bits. Zero-fill high bits. |
| s>d |
( n1 -- d1 ) |
Convert a number to a double number. |
| sm/rem |
( d n -- rem quot ) |
Divide d by n, symmetric division. |
| u2/ |
( x1 -- x2 ) |
Logical right shift 1 bit; zero shifted into high bit. |
| u* |
(u1 u2 -- uprod ) |
Multiply 2 unsigned numbers yielding an unsigned product. |
| u/mod |
( u1 u2 -- urem uquot ) |
Divide unsigned 32-bit number by an unsigned 32-bit number; yield 32-bit remainder and quotient. |
| um* |
( u1 u2 -- ud ) |
Multiply 2 unsigned 32-bit numbers; yield unsigned double number product. |
| um/mod |
( ud u -- urem uprod ) |
Divide ud by u. |
| wbflip |
( word1 -- word2 ) |
Swap the bytes within a 16-bit word. |
| wbsplit |
( word -- b.low b.hi ) |
Split 16-bit word into two bytes. |
| wljoin |
( w.low w.hi -- quad ) |
Join two words to form a quadword. |
| xor |
( x1 x2 --x3 ) |
Bitwise exclusive OR. |