introduce bitloops

This commit is contained in:
mike
2026-01-14 09:14:03 +01:00
parent bd52bd0ef0
commit afd4a7f53c
4 changed files with 45 additions and 45 deletions

View File

@@ -99,9 +99,11 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
for (int dr1 = -2; dr1 <= 2; dr1++)
for (int dr2 = -2; dr2 <= 2; dr2++)
for (int dc1 = -2; dc1 <= 2; dc1++)
for (int dc2 = -2; dc2 <= 2; dc2++)
MUTATE_RI[i][k++] = Grid.offset(clamp(Grid.r(i) + dr1 + dr2, 0, R - 1),
clamp(Grid.c(i) + dc1 + dc2, 0, C - 1));
for (int dc2 = -2; dc2 <= 2; dc2++) {
val ti = IT[i];
MUTATE_RI[i][k++] = Grid.offset(clamp(ti.r() + dr1 + dr2, 0, R - 1),
clamp(ti.c() + dc1 + dc2, 0, C - 1));
}
}
}
@@ -280,14 +282,12 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
final byte[] g;
public long lo, hi;
public static int r(int offset) { return offset & 7; }
public static int c(int offset) { return offset >>> 3; }
static int offset(int r, int c) { return r | (c << 3); }
/// the pos will never target a clue
public byte letter32At(int pos) { return g[pos]; }
void setLetter(int idx, byte ch) { g[idx] = ch; }
void clearletter(int idx) { g[idx] = DASH; }
void setLetter(int idx, byte ch) { g[idx] = ch; }
private void clearletter(int idx) { g[idx] = DASH; }
void undoPlace(long maskLo, long maskHi) {
for (long b = maskLo; b != 0; b &= b - 1) clearletter(Long.numberOfTrailingZeros(b));
for (long b = maskHi; b != 0; b &= b - 1) clearletter(64 | Long.numberOfTrailingZeros(b));
@@ -305,12 +305,11 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
static long pack(int index, byte[] b) { return pack(b) | ((long) index << 40); }
static long pack(byte[] b) {
long w = 0;
for (var i = 0; i < b.length; i++) w |= ((long) b[i] & 63) << (i * 5);
for (var i = 0; i < b.length; i++) w |= ((long) b[i] & 31) << (i * 5);
return w;
}
static public long from(int index, String word) { return pack(index, word.getBytes(US_ASCII)); }
static byte byteAt(long word, int idx) { return (byte) ((word >>> (idx * 5)) & 0b11111 | B64); }// word[]; }
static int intAt(long word, int idx) { return (int) (((word >>> (idx * 5))) & 0b11111); }// word[]; }
static byte byteAt(long word, int idx) { return (byte) ((word >>> (idx * 5)) & 0b11111); }
static String[] clue(long w) { return CsvIndexService.clues(unpackIndex(w)); }
static int simpel(long w) { return CsvIndexService.simpel(unpackIndex(w)); }
static int length(long word) { return ((63 - Long.numberOfLeadingZeros(word & LETTER_MASK)) / 5) + 1; }
@@ -338,7 +337,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
entry.words().add(lemma);
for (var i = 0; i < L; i++) {
var letter = Lemma.intAt(lemma, i) - 1;
var letter = Lemma.byteAt(lemma, i) - 1;
if (letter < 0 || letter >= 26) throw new RuntimeException("Illegal letter: " + letter + " in word " + lemma);
entry.pos()[i][letter].add(idx);
}
@@ -724,12 +723,14 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
static long patternForSlot(Grid grid, Slot s) {
if ((s.lo & ~grid.lo) == 0 && (s.hi & ~grid.hi) == 0) return 0;
/*if ((s.lo & ~grid.lo) == 0 && (s.hi & ~grid.hi) == 0) {
return 0;
}*/
long p = 0;
if (s.increasing()) {
for (long b = s.lo & ~grid.lo; b != 0; b &= b - 1) {
int idx = Long.numberOfTrailingZeros(b);
int val = grid.g[idx] & 31;
int val = grid.g[idx];
if (val != 0) {
int i = Long.bitCount(s.lo & ((1L << idx) - 1));
p |= ((long) (i * 26 + val)) << (i << 3);
@@ -738,7 +739,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
int offset = Long.bitCount(s.lo);
for (long b = s.hi & ~grid.hi; b != 0; b &= b - 1) {
int idx = Long.numberOfTrailingZeros(b);
int val = grid.g[64 | idx] & 31;
int val = grid.g[64 | idx];
if (val != 0) {
int i = offset + Long.bitCount(s.hi & ((1L << idx) - 1));
p |= ((long) (i * 26 + val)) << (i << 3);
@@ -748,7 +749,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
int offset = Long.bitCount(s.hi);
for (long b = s.hi & ~grid.hi; b != 0; b &= b - 1) {
int idx = Long.numberOfTrailingZeros(b);
int val = grid.g[64 | idx] & 31;
int val = grid.g[64 | idx];
if (val != 0) {
int i = Long.bitCount(s.hi & ~((1L << idx) | ((1L << idx) - 1)));
p |= ((long) (i * 26 + val)) << (i << 3);
@@ -756,7 +757,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
for (long b = s.lo & ~grid.lo; b != 0; b &= b - 1) {
int idx = Long.numberOfTrailingZeros(b);
int val = grid.g[idx] & 31;
int val = grid.g[idx];
if (val != 0) {
int i = offset + Long.bitCount(s.lo & ~((1L << idx) | ((1L << idx) - 1)));
p |= ((long) (i * 26 + val)) << (i << 3);