annotate libgo/misc/cgo/life/c-life.c @ 133:420680fc7707

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