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.
7 #include "_cgo_export.h"
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
15 Step(int x
, int y
, int *a
, int *n
)
17 struct GoStart_return r
;
19 // Use Go to start 4 goroutines each of which handles 1/4 of the
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
);
33 // The actual computation. This is called in parallel.
35 DoStep(int xdim
, int ydim
, int xstart
, int xend
, int ystart
, int yend
, int *a
, int *n
)
39 for(x
= xstart
; x
< xend
; x
++) {
40 for(y
= ystart
; y
< yend
; y
++) {
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
&&
47 c
+= a
[(x
+i
)*xdim
+ (y
+j
)] != 0;
50 if(c
== 3 || (c
== 2 && a
[x
*xdim
+ y
] != 0))