1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5#include <assert.h>
6#include "life.h"
7#include "_cgo_export.h"
8
9const int MYCONST = 0;
10
11// Do the actual manipulation of the life board in C. This could be
12// done easily in Go, we are just using C for demonstration
13// purposes.
14void
15Step(int x, int y, int *a, int *n)
16{
17 struct GoStart_return r;
18
19 // Use Go to start 4 goroutines each of which handles 1/4 of the
20 // board.
21 r = GoStart(0, x, y, 0, x / 2, 0, y / 2, a, n);
22 assert(r.r0 == 0 && r.r1 == 100); // test multiple returns
23 r = GoStart(1, x, y, x / 2, x, 0, y / 2, a, n);
24 assert(r.r0 == 1 && r.r1 == 101); // test multiple returns
25 GoStart(2, x, y, 0, x / 2, y / 2, y, a, n);
26 GoStart(3, x, y, x / 2, x, y / 2, y, a, n);
27 GoWait(0);
28 GoWait(1);
29 GoWait(2);
30 GoWait(3);
31}
32
33// The actual computation. This is called in parallel.
34void
35DoStep(int xdim, int ydim, int xstart, int xend, int ystart, int yend, int *a, int *n)
36{
37 int x, y, c, i, j;
38
39 for(x = xstart; x < xend; x++) {
40 for(y = ystart; y < yend; y++) {
41 c = 0;
42 for(i = -1; i <= 1; i++) {
43 for(j = -1; j <= 1; j++) {
44 if(x+i >= 0 && x+i < xdim &&
45 y+j >= 0 && y+j < ydim &&
46 (i != 0 || j != 0))
47 c += a[(x+i)*xdim + (y+j)] != 0;
48 }
49 }
50 if(c == 3 || (c == 2 && a[x*xdim + y] != 0))
51 n[x*xdim + y] = 1;
52 else
53 n[x*xdim + y] = 0;
54 }
55 }
56}
View as plain text