% This file is part of the Stanford GraphBase (c) Stanford University 1992
\def\title{MULTIPLY}
@i boilerplate.w %<< legal stuff: PLEASE READ IT BEFORE MAKING ANY CHANGES!
\prerequisite{GB\_\thinspace GATES}
@* Introduction. This demonstration program uses graphs
constructed by the |prod| procedure in the |gb_gates| module to produce
an interactive program called \.{multiply}, which multiplies and divides
small numbers the slow way (i.e., by simulating the behavior of
a logical circuit, one gate at a time).
The program assumes that \UNIX\ conventions are being used. Some code in
sections listed under `\UNIX\ dependencies' in the index may need to change
if this program is ported to other operating systems.
\def\<#1>{$\langle${\rm#1}$\rangle$}
To run the program under \UNIX, say `\.{multiply} $m$ $n$ [|seed|]', where
$m$ and $n$ are the sizes of the numbers to be multiplied, in bits,
and where |seed| is given if and only if you want the multiplier
to be a special-purpose circuit for multiplying a given $m$-bit
number by a randomly chosen $n$-bit constant.
The program will prompt you for two numbers (or for just one, if the
random constant option has been selected), and it will use the gate
network to compute their product. Then it will ask for more input, and so on.
@ We use the data types \&{Vertex}, \&{Arc}, and \&{Graph} defined
in |gb_graph|.
@f Vertex int
@f Arc int
@f Graph int
@ Here is the general layout of this program, as seen by the \Cee\ compiler:
@^UNIX dependencies@>
@p
#include "gb_graph.h" /* the standard GraphBase data structures */
#include "gb_gates.h" /* routines for gate graphs */
@#
@@;
@@;
main(argc,argv)
int argc; /* the number of command-line arguments */
char *argv[]; /* an array of strings containing those arguments */
{
@;
@;
@;
if (seed<0) /* no seed given */
printf("Here I am, ready to multiply %d-bit numbers by %d-bit numbers.\n",
m,n);
else {
g=partial_gates(g,m,0,seed,buffer);
if (g) {
@;
printf("OK, I'm ready to multiply any %d-bit number by %s.\n",m,y);
} else { /* there was enough memory to make the original |g|, but
not enough to reduce it; this probably can't happen,
but who knows? */
printf("Sorry, I couldn't process the graph (trouble code %d)!\n",
panic_code);
return -9;
}
}
printf("(I'm simulating a logic circuit with %d gates, depth %d.)\n",
g->n,depth(g));
while(1) {
@;
@