introduce bitloops

This commit is contained in:
mike
2026-01-12 08:28:48 +01:00
parent f8f1a67a61
commit 9c1be77d76
2 changed files with 22 additions and 22 deletions

View File

@@ -72,10 +72,8 @@ public record SwedishGenerator(Rng rng) {
static final byte B64 = (byte) 64; static final byte B64 = (byte) 64;
static final byte B48 = (byte) 48; static final byte B48 = (byte) 48;
// Directions for '1'..'6' // Directions for '1'..'6'
static final nbrs_16[] OFFSETS = Neighbors9x8.OFFSETS; static final long[] OFFSETS_D_IDX = Neighbors9x8.OFFSET_D_IDX;
static final nbrs_16[] OFFSETS_FOUR = Neighbors9x8.OFFSETS_FOUR; static final nbrs_16[] OFFSETS_FOUR = Neighbors9x8.OFFSETS_FOUR;
static final nbrs_8[] nbrs8 = Neighbors9x8.nbrs8;
static final nbrs_8[] nbrs4 = Neighbors9x8.nbrs4;
static final rci[] IT = Neighbors9x8.IT; static final rci[] IT = Neighbors9x8.IT;
static final long[] INBR8_PACKEDT = Neighbors9x8.NBR8_PACKED; static final long[] INBR8_PACKEDT = Neighbors9x8.NBR8_PACKED;
static final int[][] MUTATE_RI = new int[SIZE][625]; static final int[][] MUTATE_RI = new int[SIZE][625];
@@ -175,7 +173,7 @@ public record SwedishGenerator(Rng rng) {
this.hi = hi; this.hi = hi;
} }
static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); } static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); }
int digitAt(int index) { return g[index] & ~48; } int digitAt(int index) { return g[index] & 7; }
public static int r(int offset) { return offset & 7; } public static int r(int offset) { return offset & 7; }
public static int c(int offset) { return offset >>> 3; } public static int c(int offset) { return offset >>> 3; }
static int offset(int r, int c) { return r | (c << 3); } static int offset(int r, int c) { return r | (c << 3); }
@@ -266,7 +264,7 @@ public record SwedishGenerator(Rng rng) {
int intAt(int idx) { return (int) (((word >>> (idx * 5))) & 0b11111); }// word[]; } int intAt(int idx) { return (int) (((word >>> (idx * 5))) & 0b11111); }// word[]; }
@Override public int hashCode() { return unpackIndex(word); } @Override public int hashCode() { return unpackIndex(word); }
@Override public boolean equals(Object o) { return (o == this) || (o instanceof Lemma l && l.word == word); } @Override public boolean equals(Object o) { return (o == this) || (o instanceof Lemma l && l.word == word); }
static String[] clue(long w) { return CsvIndexService.clues(unpackIndex(w)); } static String[] clue(long w) { return CsvIndexService.clues(unpackIndex(w)); }
static int simpel(long w) { return CsvIndexService.simpel(unpackIndex(w)); } static int simpel(long w) { return CsvIndexService.simpel(unpackIndex(w)); }
int length() { return Lemma.length(word); } int length() { return Lemma.length(word); }
static int length(long word) { static int length(long word) {
@@ -344,7 +342,6 @@ public record SwedishGenerator(Rng rng) {
} }
} }
static record Slot(int key, long packedPos) { static record Slot(int key, long packedPos) {
static final int BIT_FOR_DIR = 3; static final int BIT_FOR_DIR = 3;
@@ -364,7 +361,7 @@ public record SwedishGenerator(Rng rng) {
private static void processSlot(Grid grid, SlotVisitor visitor, int idx) { private static void processSlot(Grid grid, SlotVisitor visitor, int idx) {
var d = grid.digitAt(idx); var d = grid.digitAt(idx);
var packed = OFFSETS[d].path()[idx]; var packed = OFFSETS_D_IDX[(d - 1) | (idx << 2)];
long packedPos = 0L; long packedPos = 0L;
int k = 0; int k = 0;
for (long n = (packed >>> 56), offset = 0L, iidx; k < n; k++, offset += 7L) { for (long n = (packed >>> 56), offset = 0L, iidx; k < n; k++, offset += 7L) {
@@ -399,10 +396,11 @@ public record SwedishGenerator(Rng rng) {
for (long bits = (i == 0 ? lo_cl : hi_cl); bits != X; bits &= bits - 1) { for (long bits = (i == 0 ? lo_cl : hi_cl); bits != X; bits &= bits - 1) {
int clueIdx = i | Long.numberOfTrailingZeros(bits); int clueIdx = i | Long.numberOfTrailingZeros(bits);
var d = grid.digitAt(clueIdx); var d = grid.digitAt(clueIdx);
var nbrs16 = OFFSETS[d]; long packed = OFFSETS_D_IDX[(d - 1) | (clueIdx << 2)];
long packed = nbrs16.path()[clueIdx]; //if (OFFSETS_D_IDX[(d-1) * SIZE + clueIdx] != packed) throw new IllegalStateException("Invalid path index for digit " + d + " at clue " + clueIdx);
int n = (int) (packed >>> 56) * 7, k, idx;
var horiz = Slot.horiz(d) ? covH : covV; int n = (int) (packed >>> 56) * 7, k, idx;
var horiz = Slot.horiz(d) ? covH : covV;
for (k = 0; k < n; k += 7) { for (k = 0; k < n; k += 7) {
idx = (int) ((packed >>> (k)) & 0x7F); idx = (int) ((packed >>> (k)) & 0x7F);
if (grid.isClue(idx)) break; if (grid.isClue(idx)) break;
@@ -554,7 +552,7 @@ public record SwedishGenerator(Rng rng) {
for (var hi = out.hi; hi != X; hi &= hi - 1L) clearClues(out, 64 + Long.numberOfTrailingZeros(hi)); for (var hi = out.hi; hi != X; hi &= hi - 1L) clearClues(out, 64 + Long.numberOfTrailingZeros(hi));
return out; return out;
} }
public static void clearClues(Grid out, int idx) { if (!out.hasRoomForClue(OFFSETS[out.digitAt(idx)].path()[idx])) out.clearClue(idx); } public static void clearClues(Grid out, int idx) { if (!out.hasRoomForClue(OFFSETS_D_IDX[(out.digitAt(idx) - 1) | (idx << 2)])) out.clearClue(idx); }
Grid hillclimb(Grid start, int limit) { Grid hillclimb(Grid start, int limit) {
var best = start; var best = start;

View File

@@ -11,10 +11,11 @@ import static puzzle.SwedishGenerator.*;
public class SwedishGeneratorTest { public class SwedishGeneratorTest {
static final byte D_BYTE_2 = 50;
@Test @Test
void testPatternForSlotAllLetters() { void testPatternForSlotAllLetters() {
var grid = new Grid(new byte[]{ 65, 66, 67 }); // A B C var grid = new Grid(new byte[]{ 65, 66, 67 }); // A B C
var slot = Slot.from(18, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); var slot = Slot.from(18, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3);
long pattern = patternForSlot(grid, slot); long pattern = patternForSlot(grid, slot);
assertEquals(1 | (2 << 5) | (3 << 10), pattern); assertEquals(1 | (2 << 5) | (3 << 10), pattern);
@@ -22,8 +23,8 @@ public class SwedishGeneratorTest {
@Test @Test
void testPatternForSlotMixed() { void testPatternForSlotMixed() {
var grid = new Grid(new byte[]{ 65, DASH, 67 }); // A - C var grid = new Grid(new byte[]{ 65, DASH, 67 }); // A - C
var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3);
long pattern = patternForSlot(grid, slot); long pattern = patternForSlot(grid, slot);
assertEquals(1 | (0 << 5) | (3 << 10), pattern); assertEquals(1 | (0 << 5) | (3 << 10), pattern);
@@ -31,8 +32,8 @@ public class SwedishGeneratorTest {
@Test @Test
void testPatternForSlotAllDashes() { void testPatternForSlotAllDashes() {
var grid = new Grid(new byte[]{ DASH, DASH, DASH }); // - - - var grid = new Grid(new byte[]{ DASH, DASH, DASH }); // - - -
var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3);
long pattern = patternForSlot(grid, slot); long pattern = patternForSlot(grid, slot);
assertEquals(0, pattern); assertEquals(0, pattern);
@@ -40,8 +41,8 @@ public class SwedishGeneratorTest {
@Test @Test
void testPatternForSlotSingleLetter() { void testPatternForSlotSingleLetter() {
var grid = new Grid(new byte[]{ 65, DASH, DASH }); // A - - var grid = new Grid(new byte[]{ 65, DASH, DASH }); // A - -
var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3);
long pattern = patternForSlot(grid, slot); long pattern = patternForSlot(grid, slot);
assertEquals(1, pattern); assertEquals(1, pattern);
@@ -135,7 +136,7 @@ public class SwedishGeneratorTest {
// key = (r << 8) | (c << 4) | d // key = (r << 8) | (c << 4) | d
var offset = Grid.offset(2, 3); var offset = Grid.offset(2, 3);
System.out.println("[DEBUG_LOG] Grid.offset(2, 3) = " + offset); System.out.println("[DEBUG_LOG] Grid.offset(2, 3) = " + offset);
var key = (offset << Slot.BIT_FOR_DIR) | 5; var key = (offset << Slot.BIT_FOR_DIR) | 5;
System.out.println("[DEBUG_LOG] key = " + key); System.out.println("[DEBUG_LOG] key = " + key);
long packedPos = 0; long packedPos = 0;
// pos 0: (2, 5) // pos 0: (2, 5)
@@ -273,7 +274,8 @@ public class SwedishGeneratorTest {
assertTrue(f1 >= 1_000_000_000L); assertTrue(f1 >= 1_000_000_000L);
// Add a slot // Add a slot
grid.setClue(0, OFFSETS[2].dbyte()); //var dbyte = OFFSETS[2].dbyte();
grid.setClue(0, D_BYTE_2);
var f2 = gen.maskFitness(grid); var f2 = gen.maskFitness(grid);
assertTrue(f2 < f1); assertTrue(f2 < f1);
} }