Handelcexample

# 累積器

``````const dw = 8; /* Width of incoming data */
const nw=4;/*2^nw=Numberofinputs*/
const rw=dw+nw;/*Widthoftheresult*/
void main(chan (in) STDIN:dw, chan (out) STDOUT:rw)
{
int    data:dw;
int    accumulator:rw;
int    counter:nw;
accumulator,counter=0,0;
do {
STDIN?data;
accumulator=accumulator+(0@data);
counter=counter+1;
}while(counter!=0);

STDOUT!accumulator;
}```
```

Handel-C Hardware Compiler, Beta release:H163.11
After compilation:35 FFs, 210 gates,5 inverters;size 296
Afternetlist optimisations:34 FFs,97gates,5 inverters;size 148

# 長除法

``````/*Integer division by long-division method*/
const dw=16;
void main(chan (in) STDIN:dw, chan (out) STDOUT:dw)
{
int a,b,c:dw;
int bits:5;
while(1){
STDIN?a;
STDIN?b;
c,bits=0,1;
while(b.<=.a)
b,bits=b<<1,bits+1;
do par{
if(a.>=.b)
a,c=a-b,(c<<1)^1;
else
c=c<<1;
b,bits=b>>1,bits-1;
}while(bits!=0);
STDOUT!c;
}
}```
```

# 平方根 root.c

``````/*Square root without multiplication or division*/
const half_dw=8:4;
const dw=half_dw<<1;
const sq=(1<<(dw-2)):dw;
void main(chan (in) STDIN:dw, chan (out) STDOUT:dw)
{
int a,p,q,r;
int i;
while(1){
STDIN?a;
p,q,r=0,0,sq;
i=half_dw;
do{
if(p+q+r.<=.a)
i,r,q,p=i-1,r>>2,(q>>1)+r,p+q+r;
else
i,r,q=i-1,r>>2,(q>>1);
}while(i!=0);
STDOUT!q;
}
}```
```

# 平行佇列 — queue.c

``````/*Four place queue using internal communications*/
const dw=8;
void main(chan (in) c4:dw, chan (out) c0:dw)
{
int d0,d1,d2,d3;
chan c1,c2,c3;
void e0() { while(1) {c1?d0;c0!d0;}}
void e1() { while(1) {c2?d1;c1!d1;}}
void e2() { while(1) {c3?d2;c2!d2;}}
void e3() { while(1) {c4?d3;c3!d3;}}
par{
e0();e1();e2();e3();
}
}```
```