Initial release of slime chunk search blog examples

This commit is contained in:
Yaossg 2026-11-20 22:11:27 +08:00
commit 057215c3a2
47 changed files with 40361 additions and 0 deletions

7
include/README.md Normal file
View file

@ -0,0 +1,7 @@
# 共用数学实现
- `slime/solver_core.hpp`:Java 整数回绕、48 位 Random 状态、精确史莱姆判定与模式验证。
- `slime/upper_interval.hpp`:输出差必要条件和高 13 位区间求交。
教学示例和搜索 CLI 共用这些实现。候选生成使用首次随机输出的普通分支;
最终验证处理 Java 的拒绝重抽,但不能补回此前漏掉的候选。

View file

@ -0,0 +1,97 @@
#pragma once
#include <cstddef>
#include <cstdint>
#if defined(__CUDACC__)
#define SLIME_HD __host__ __device__
#define SLIME_INLINE __forceinline__
#else
#define SLIME_HD
#define SLIME_INLINE inline
#endif
namespace slime {
constexpr uint64_t kJavaMultiplier = 0x5DEECE66DULL;
constexpr uint64_t kJavaAddend = 0xBULL;
constexpr uint64_t kMask48 = (1ULL << 48) - 1ULL;
constexpr uint64_t kSlimeXor = 987234911ULL;
constexpr uint64_t kCombinedXor = kSlimeXor ^ kJavaMultiplier;
constexpr int kLowerBits = 35;
constexpr int kUpperBits = 13;
constexpr uint32_t kUpperSize = 1U << kUpperBits;
constexpr uint32_t kUpperMask = kUpperSize - 1U;
constexpr uint32_t kHalfDeltaInverse = 4965U;
constexpr int kMaxPatternCells = 64;
constexpr uint32_t kX2Coeff = 4987142U;
constexpr uint32_t kXCoeff = 5947611U;
constexpr uint32_t kZ2Coeff = 4392871U;
constexpr uint32_t kZCoeff = 389711U;
enum Requirement : uint8_t {
kNonSlime = 0,
kSlime = 1,
};
struct CellTerm {
uint64_t term;
uint8_t requirement;
};
SLIME_HD SLIME_INLINE uint64_t javaIntAsLongBits(uint32_t value) {
return static_cast<uint64_t>(static_cast<int64_t>(static_cast<int32_t>(value)));
}
SLIME_HD SLIME_INLINE uint64_t coordinateTerm(int32_t chunkX, int32_t chunkZ) {
const uint32_t x = static_cast<uint32_t>(chunkX);
const uint32_t z = static_cast<uint32_t>(chunkZ);
return javaIntAsLongBits(x * x * kX2Coeff) + javaIntAsLongBits(x * kXCoeff) +
javaIntAsLongBits(z * z) * kZ2Coeff + javaIntAsLongBits(z * kZCoeff);
}
SLIME_HD SLIME_INLINE uint64_t coordinateXTerm(int32_t chunkX) {
const uint32_t x = static_cast<uint32_t>(chunkX);
return javaIntAsLongBits(x * x * kX2Coeff) + javaIntAsLongBits(x * kXCoeff);
}
SLIME_HD SLIME_INLINE uint64_t coordinateZTerm(int32_t chunkZ) {
const uint32_t z = static_cast<uint32_t>(chunkZ);
return javaIntAsLongBits(z * z) * kZ2Coeff + javaIntAsLongBits(z * kZCoeff);
}
SLIME_HD SLIME_INLINE uint64_t nextStateFromTerm(uint64_t seed48, uint64_t term) {
uint64_t state = ((seed48 + term) ^ kCombinedXor) & kMask48;
return (state * kJavaMultiplier + kJavaAddend) & kMask48;
}
SLIME_HD SLIME_INLINE bool exactIsSlimeFromTerm(uint64_t seed48, uint64_t term) {
uint64_t state = ((seed48 + term) ^ kCombinedXor) & kMask48;
for (;;) {
state = (state * kJavaMultiplier + kJavaAddend) & kMask48;
const uint32_t bits = static_cast<uint32_t>(state >> 17);
const uint32_t value = bits % 10U;
if (bits - value + 9U <= 0x7FFFFFFFU) {
return value == 0U;
}
}
}
SLIME_HD SLIME_INLINE bool verifyPattern(uint64_t seed48, const CellTerm *cells, int cellCount) {
for (int i = 0; i < cellCount; ++i) {
const bool slime = exactIsSlimeFromTerm(seed48, cells[i].term);
if (slime != (cells[i].requirement == kSlime)) {
return false;
}
}
return true;
}
SLIME_HD SLIME_INLINE uint32_t firstWithResidue(uint32_t start, uint32_t residue) {
return start + ((residue + 5U - (start % 5U)) % 5U);
}
} // namespace slime
#undef SLIME_HD
#undef SLIME_INLINE

View file

@ -0,0 +1,87 @@
#pragma once
#include "solver_core.hpp"
#if defined(__CUDACC__)
#define SLIME_INTERVAL_HD __host__ __device__
#define SLIME_INTERVAL_INLINE __forceinline__
#else
#define SLIME_INTERVAL_HD
#define SLIME_INTERVAL_INLINE inline
#endif
namespace slime {
namespace sr = slime;
// Positive (slime) constraints only; positions may be sparse.
// Each surviving residue has a single interval. Keeping the first cell's
// two residues makes intersection constant-space. Check negative cells on emit.
template <class Emit>
SLIME_INTERVAL_HD SLIME_INTERVAL_INLINE void solveIntervals(uint64_t low, const sr::CellTerm *cells,
int count, Emit &emit) {
low &= (1ULL << kLowerBits) - 1;
uint32_t q = uint32_t(sr::nextStateFromTerm(low, cells[0].term) >> 17);
if (q & 1U)
return;
uint32_t b = q >> 18;
uint32_t r = q & ((1U << 18) - 1);
uint32_t threshold = kUpperSize - b;
uint32_t residue[2] = {(r % 5 + 5 - b % 5) % 5, 0};
residue[1] = (residue[0] + 2) % 5;
uint32_t lo[2] = {0, threshold}, hi[2] = {threshold, kUpperSize};
unsigned active = 0;
for (int k = 0; k < 2; ++k)
if (sr::firstWithResidue(lo[k], residue[k]) < hi[k])
active |= 1U << k;
for (int i = 1; i < count && active; ++i) {
q = uint32_t(sr::nextStateFromTerm(low, cells[i].term) >> 17);
if (q & 1U)
return;
b = q >> 18;
r = q & ((1U << 18) - 1);
threshold = kUpperSize - b;
const uint32_t before = (r % 5 + 5 - b % 5) % 5;
const uint32_t after = (before + 2) % 5;
for (int k = 0; k < 2; ++k)
if (active & (1U << k)) {
if (residue[k] == before)
hi[k] = hi[k] < threshold ? hi[k] : threshold;
else if (residue[k] == after)
lo[k] = lo[k] > threshold ? lo[k] : threshold;
else {
active &= ~(1U << k);
continue;
}
if (sr::firstWithResidue(lo[k], residue[k]) >= hi[k])
active &= ~(1U << k);
}
}
for (int k = 0; k < 2; ++k)
if (active & (1U << k)) {
for (uint32_t t = sr::firstWithResidue(lo[k], residue[k]); t < hi[k]; t += 5) {
const uint64_t seed =
low + (uint64_t((kHalfDeltaInverse * t) & kUpperMask) << kLowerBits);
if (sr::verifyPattern(seed, cells, count))
emit(seed);
}
}
}
// A cheap necessary condition before high-bit interval solving.
// Adding the common high-bit shift can wrap q_i-q_0 once: delta mod 10 is 0 or 8.
SLIME_INTERVAL_HD SLIME_INTERVAL_INLINE bool
differenceFilter(uint64_t low, const sr::CellTerm *cells, int count) {
const uint32_t q0 = uint32_t(sr::nextStateFromTerm(low, cells[0].term) >> 17);
for (int i = 1; i < count; ++i) {
const uint32_t qi = uint32_t(sr::nextStateFromTerm(low, cells[i].term) >> 17);
const uint32_t delta = (qi - q0) & 0x7fffffffU;
const uint32_t r = delta % 10;
if (r != 0 && r != 8)
return false;
}
return true;
}
} // namespace slime
#undef SLIME_INTERVAL_HD
#undef SLIME_INTERVAL_INLINE