From b61b7b8a122f3efc9a573777300648ab0eb2a50b Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Fri, 16 May 2025 07:45:24 -0400 Subject: check for equality instead of counting > 0 might be faster on certain cpus --- src/main.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index 0e8da4e..e6975be 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,4 @@ const SudokuSolver = struct { - const countEql = std.simd.countElementsWithValue; const join = std.simd.join; // Each cell exists in memory three times. // This makes it easy to compare a guess against all relevant cells simultaneously. @@ -29,7 +28,9 @@ const SudokuSolver = struct { const constraints = join(self.rows[row], join(self.cols[col], self.boxes[boxOf(row, col)])); inline for (1..10) |n| { // A number is a possible move iff it does not already exist in the current row, col, or box. - if (countEql(constraints, @intCast(n)) == 0) { + const n_vec: @Vector(@typeInfo(@TypeOf(constraints)).vector.len, u8) = @splat(n); + const equals = constraints == n_vec; + if (!@reduce(.Or, equals)) { cell_guesses.set(n - 1); } } @@ -95,7 +96,9 @@ const SudokuSolver = struct { fn isSolved(self: SudokuSolver) bool { for (self.rows) |row| { - if (countEql(row, 0) > 0) return false; + const z_vec: @Vector(@typeInfo(@TypeOf(row)).vector.len, u8) = @splat(0); + const equals = row == z_vec; + if (@reduce(.Or, equals)) return false; } return true; } -- cgit