commit bc926c4c872b10bbe89fb891230bb1612ce4ecb4
parent 4e2b671b09618b794e3508b97967ff924925d01e
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date: Fri, 9 Dec 2022 15:17:38 -0600
Switch to 64-bit numbers
Diffstat:
M | life.c | | | 71 | ++++++++++++++++++++++++++--------------------------------------------- |
1 file changed, 26 insertions(+), 45 deletions(-)
diff --git a/life.c b/life.c
@@ -11,30 +11,29 @@
#define WIDTH(x) sizeof(x[0]) * 8
#define MAX_ROWS 24
-static uint32_t glider[MAX_ROWS] = {0, 131072, 65536, 458752, 0, 0, 0, 0};
-static uint32_t modGlider[MAX_ROWS] = {524288, 262144, 1835008, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-static uint32_t vline[MAX_ROWS] = {0, 0, 131072, 131072, 131072, 0, 0, 0};
-static uint32_t what[MAX_ROWS] = {0, 0, 393216, 393216, 393216, 393216, 393216, 0};
-static uint32_t what2[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 262144, 0};
-static uint32_t what3[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 0, 0};
-static uint32_t what4[MAX_ROWS] = {0, 0, 983040, 0, 0, 0, 0, 0};
-static uint32_t gliderShootsImmortalFlower[MAX_ROWS] =
+static uint64_t glider[MAX_ROWS] = {0, 131072, 65536, 458752, 0, 0, 3, 3};
+static uint64_t modGlider[MAX_ROWS] = {524288, 262144, 1835008, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3};
+static uint64_t vline[MAX_ROWS] = {0, 0, 131072, 131072, 131072, 0, 0, 0};
+static uint64_t what[MAX_ROWS] = {0, 0, 393216, 393216, 393216, 393216, 393216, 0};
+static uint64_t what2[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 262144, 0};
+static uint64_t what3[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 0, 0};
+static uint64_t what4[MAX_ROWS] = {0, 0, 983040, 0, 0, 0, 0, 0};
+static uint64_t gliderShootsImmortalFlower[MAX_ROWS] =
{2097152, 1048576, 7340032, 0, 0, 0, 0, 0, 0, 0, 30720, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-static uint32_t hline[MAX_ROWS] = {0, 0, 114688, 0, 0, 0, 0, 0};
-static uint32_t sqtoad[MAX_ROWS] = {0, 786432, 786432, 196608, 196608, 0, 0, 0};
-/*ec printf('%x', float2nr(@"->str2nr(16)/pow(2,16)))*/
-static uint32_t gosper[MAX_ROWS] = { 0x00000200, 0x00000500, 0x00000686, 0x000006c6, 0x00000680, 0x00000500, 0x00000200, 0};
+static uint64_t hline[MAX_ROWS] = {0, 0, 114688, 0, 0, 0, 0, 0};
+static uint64_t sqtoad[MAX_ROWS] = {0, 786432, 786432, 196608, 196608, 0, 0, 0};
+static uint64_t gosper[MAX_ROWS] = {0x0000000800000000, 0x0000001400000000, 0x0030001a18000000, 0x0028001b18000000, 0x6604001a00000000, 0x6924001400000000, 0x0604000800000000, 0x0028000000000000, 0x0030000000000000};
#define BOARD gosper
-void print_bw(int x, int y, uint32_t ch) {
+void print_bw(int x, int y, uint64_t ch) {
tb_set_cell(x, y, ch, TB_BLACK, TB_WHITE);
}
-void print_bl(int x, int y, uint32_t ch) {
+void print_bl(int x, int y, uint64_t ch) {
tb_set_cell(x, y, ch, TB_BLACK, TB_BLACK);
}
-void print_board(uint32_t board[], int rows, int cols) {
+void print_board(uint64_t board[], int rows, int cols) {
int x, y;
print_bl(0, 0, 0x250f); //heavy box corner down and right
for(x=1; x <= cols*2; x++)
@@ -50,14 +49,10 @@ void print_board(uint32_t board[], int rows, int cols) {
print_bl(0, y, 0x2517); //heavy box corner up and right
for(y=0; y < rows; y++) {
-/* for(int n=cols - WIDTH(board); n-->0;) printf(" ");*/
- /* For a single byte for example, this value will be 1000 0000.*/
- uint32_t leftBit = 1 << (cols - 1);
- uint32_t bit=leftBit;
+ uint64_t leftBit = 1ULL << (cols - 1);
+ uint64_t bit=leftBit;
for(x=1; x <= cols*2; bit=bit>>1, x+=2) {
if (board[y] & bit) {
-/* print_bw(x, y+1, 0x257a);*/
-/* print_bw(x+1, y+1, 0x2578);*/
print_bw(x, y+1, ' ');
print_bw(x+1, y+1, ' ');
}
@@ -66,19 +61,19 @@ void print_board(uint32_t board[], int rows, int cols) {
tb_present();
}
-uint32_t *get_next_state(uint32_t board[], int rows, int cols) {
- uint32_t m[MAX_ROWS];
+uint64_t *get_next_state(uint64_t board[], int rows, int cols) {
+ uint64_t m[MAX_ROWS];
for(int r=0; r < rows; r++) {
- uint32_t newRow = board[r];
+ uint64_t newRow = board[r];
/* For a single byte for example, this value will be 1000 0000.*/
- uint32_t leftBit = (uint32_t)(1 << (cols - 1));
- for(uint32_t bit=(uint32_t)leftBit, col=0; col < cols; bit=leftBit>>++col) {
+ uint64_t leftBit = (uint64_t)(1ULL << (cols - 1));
+ for(uint64_t bit=(uint64_t)leftBit, col=0; col < cols; bit=leftBit>>++col) {
/* Get neighbors in previous row, current row, next row.
Ignore nonexistent rows. */
int neighbors = 0;
for(int a = -1; a <= 1; a++) {
if(r+a < 0 || r+a >= rows) continue;
- uint32_t row = board[r+a];
+ uint64_t row = board[r+a];
if(col > 0 && (row & bit<<1)) neighbors++;
/* Skip current cell */
if(a != 0 && (row & bit)) neighbors++;
@@ -101,33 +96,23 @@ uint32_t *get_next_state(uint32_t board[], int rows, int cols) {
}
m[r] = newRow;
}
-/* for(int r=0; r < rows; r++) {*/
-/* for(uint32_t bit, col=1; col <= cols; bit=128>>col++) {*/
-/* printf("%d ", m[r] & bit ? 1 : 0);*/
-/* }*/
-/* printf("\n");*/
-/* }*/
- static uint32_t tmp[MAX_ROWS];
+ static uint64_t tmp[MAX_ROWS];
memcpy(tmp, m, sizeof m);
return tmp;
}
-void play(uint32_t board[], int rows, int cols) {
+void play(uint64_t board[], int rows, int cols) {
struct tb_event ev;
tb_init();
-/* int rounds = 56;*/
-/* while(rounds-->0) {*/
while(1) {
tb_clear();
print_board(board, rows, cols);
- if (tb_poll_event(&ev) == TB_OK && ev.type == TB_EVENT_KEY)
+ if (tb_peek_event(&ev, 300) == TB_OK && ev.type == TB_EVENT_KEY)
if(ev.key == TB_KEY_CTRL_C)
return;
- uint32_t *l2 = get_next_state(board, rows, cols);
+ uint64_t *l2 = get_next_state(board, rows, cols);
memcpy(board, l2, (sizeof *l2) * rows );
}
-/* for(int i=0; i < rows; i++)*/
-/* printf("row %d:\t% 6x\t(%" PRIu32 ")\n", i, BOARD[i]);*/
}
int main() {
@@ -140,8 +125,4 @@ int main() {
play(BOARD, rows, cols);
tb_shutdown();
return 0;
-/* uint32_t *lp = (uint32_t*)malloc(5 * sizeof(uint32_t));*/
-/* uint32_t *mp = play(l);*/
-/* uint32_t *np = play(mp);*/
-/* play(np);*/
}