JanEric1
JanEric1
MModular
Created by JanEric1 on 11/10/2024 in #questions
Compile time facilities
I am trying to write a function that compares a SIMD variable against a a sequence of other SIMD vectors that are determined by a function parameter and I want the function to do as much as possible at compile time. Ideally so that at runtime the function just has a structure like
If variable == reference1:
return True
If variable == reference2:
return True
...
return False
If variable == reference1:
return True
If variable == reference2:
return True
...
return False
Currently I have it like this
fn _is_winner[size: Int, //, player: UInt8](board: SIMD[DType.uint8, size*size]) -> Bool:
var reference: SIMD[DType.uint8, board.size]
@parameter
for row in range(size):
reference = SIMD[DType.uint8, board.size](0)
@parameter
for col in range(size):
reference[row * size + col] = player
if (board & reference).reduce_bit_count() == size:
return True
...
return False
fn _is_winner[size: Int, //, player: UInt8](board: SIMD[DType.uint8, size*size]) -> Bool:
var reference: SIMD[DType.uint8, board.size]
@parameter
for row in range(size):
reference = SIMD[DType.uint8, board.size](0)
@parameter
for col in range(size):
reference[row * size + col] = player
if (board & reference).reduce_bit_count() == size:
return True
...
return False
But I am not sure if this does everything I want at compile time and if there are other things I can do to do that?
10 replies
MModular
Created by JanEric1 on 11/7/2024 in #questions
Most efficient way to check for tictactoe board win
I want to write a litle tictactoe game (with arbitrary board size) in Mojo and was thinking how to go the hardest on optimizing it (just for fun). I was thinking of using a SIMD to hold the board where 0 is empty, 1 is player X and 2 is player O. Then i can check for a full board like:
all(board != 0)
all(board != 0)
For checking for a win i thought i could just pregenerate the wincombinations at compile time:
(1,1,0,0)
(1,0,1,0)
(1,0,0,1)
(0,1,1,0)
(0,1,0,1)
(0,0,1,1)
(1,1,0,0)
(1,0,1,0)
(1,0,0,1)
(0,1,1,0)
(0,1,0,1)
(0,0,1,1)
for a 2x2 board and then run
(board & reference).reduce_bit_count() == size
(board & reference).reduce_bit_count() == size
against all of these. But i am not sure how i can pregenerate all of these references at compile time.
7 replies