Ethiopian multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|Forth}}: remove warning)
Line 1,088: Line 1,088:


=={{header|Forth}}==
=={{header|Forth}}==
Halve and double are standard words, spelled '''2/''' and '''2*''' respectively.
{{incorrect|Forth|No functions/subroutines defined to perform the three actions as specified in the task description}}
<lang forth>: e* ( x y -- x*y )
<lang forth>: even? ( n -- ? ) 1 and 0= ;
: e* ( x y -- x*y )
dup 0= if nip exit then
dup 0= if nip exit then
over 2* over 2/ recurse
over 2* over 2/ recurse
swap 1 and if + else nip then ;</lang>
swap even? if nip else + then ;</lang>


The author of Forth, Chuck Moore, designed a similar primitive into his MISC Forth microprocessors. The '''+*''' instruction is a multiply step: it adds S to T if A is odd, then shifts both A and T right one. The idea is that you only need to perform as many of these multiply steps as you have significant bits in the operand. (See his [http://www.colorforth.com/inst.htm core instruction set] for details.)
The author of Forth, Chuck Moore, designed a similar primitive into his MISC Forth microprocessors. The '''+*''' instruction is a multiply step: it adds S to T if A is odd, then shifts both A and T right one. The idea is that you only need to perform as many of these multiply steps as you have significant bits in the operand. (See his [http://www.colorforth.com/inst.htm core instruction set] for details.)