1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 // Checkstyle: stop
24 
25 
26 package org.graalvm.compiler.jtt.hotpath;
27 
28 import java.util.Random;
29 
30 import org.junit.Test;
31 
32 import org.graalvm.compiler.jtt.JTTTest;
33 
34 /*
35  */
36 public class HP_life extends JTTTest {
37 
test(int generations)38     public static int test(int generations) {
39         reset();
40         for (int i = 0; i < generations; ++i) {
41             step();
42         }
43         int sum = 0;
44         for (int row = 0; row < rows; ++row) {
45             for (int col = 0; col < cols; ++col) {
46                 boolean value = cell(row, col);
47                 sum += (row * 15223242 + col * 21623234) ^ ((value ? 1 : 0) * 15323142);
48             }
49         }
50         return sum;
51     }
52 
53     private static final int rows = 20;
54     private static final int cols = 20;
55     private static boolean cells[] = new boolean[rows * cols];
56 
cell(int row, int col)57     private static boolean cell(int row, int col) {
58         return ((row >= 0) && (row < rows) && (col >= 0) && (col < cols) && cells[row * cols + col]);
59     }
60 
step()61     private static boolean step() {
62         boolean next[] = new boolean[rows * cols];
63         boolean changed = false;
64         for (int row = rows - 1; row >= 0; --row) {
65             int row_offset = row * cols;
66             for (int col = cols - 1; col >= 0; --col) {
67                 int count = 0;
68                 if (cell(row - 1, col - 1)) {
69                     count++;
70                 }
71                 if (cell(row - 1, col)) {
72                     count++;
73                 }
74                 if (cell(row - 1, col + 1)) {
75                     count++;
76                 }
77                 if (cell(row, col - 1)) {
78                     count++;
79                 }
80                 if (cell(row, col + 1)) {
81                     count++;
82                 }
83                 if (cell(row + 1, col - 1)) {
84                     count++;
85                 }
86                 if (cell(row + 1, col)) {
87                     count++;
88                 }
89                 if (cell(row + 1, col + 1)) {
90                     count++;
91                 }
92                 boolean old_state = cells[row_offset + col];
93                 boolean new_state = (!old_state && count == 3) || (old_state && (count == 2 || count == 3));
94                 if (!changed && new_state != old_state) {
95                     changed = true;
96                 }
97                 next[row_offset + col] = new_state;
98             }
99         }
100         cells = next;
101         return changed;
102     }
103 
reset()104     private static void reset() {
105         Random random = new Random(0);
106         boolean cells2[] = HP_life.cells;
107         for (int offset = 0; offset < cells2.length; ++offset) {
108             cells2[offset] = random.nextDouble() > 0.5;
109         }
110     }
111 
112     @Test
run0()113     public void run0() throws Throwable {
114         runTest("test", 5);
115     }
116 
117 }
118