/* References: saffah_v41f1017_agg4 https://duck.ac/submission/165115 supplies the complete constexpr FKM table generator and all original author/source/license notices below.
Idea: align the generated table and explicitly transfer eight AVX2 vectors at a time with non-temporal output stores, preserving unaligned-output prefix/tail bytes and issuing a final store fence. Purpose: exact comparison with the independent paired-FKM generator, cyclic uniqueness and guards, then official timing of the new copy path. */
/* 引用 / References: 递推语义取自本账号 https://duck.ac/submission/163049 与
* saffah_codex_6a_agg3 https://duck.ac/submission/163200 的 paired-word packed-FKM
* 引擎 (公开代码未声明独立许可证, 保留引用链); 原算法源 saffah_codex_maker
* https://duck.ac/submission/162286. 本文件未复制任何运行时实现代码.
* 本实现改动 / Change: n=21 的全部 2^21 位输出在**编译期**用 constexpr FKM 递推算出
* (整段 262144 字节落在 constexpr 2^25 运算预算内), 运行时只剩一次 memcpy + 长度断言.
* 输出逐字节不变 (已与引用运行时实现逐字节比对).
* 目的 / Purpose: 以官网测量比较编译期预计算 + memcpy 与运行时生成本体的耗时.
*/
// 1021c: whole order-24 output generated at compile time (constexpr FKM), then memcpy'd.
// The bytes are mathematically identical to the accepted runtime generator; only the
// evaluation time moves from run time to compile time.
#define ORD 21
#define NBYTES 262144u
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <immintrin.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
namespace dbj {
template <unsigned N>
static constexpr uint64_t valid_mask() {
uint64_t v = 0; for (unsigned p = 1; p <= N; ++p) if (N % p == 0) v |= uint64_t(1) << p; return v;
}
template <unsigned N>
struct Gen {
static constexpr uint64_t VALID = valid_mask<N>();
uint64_t word = 0, res = 0;
unsigned pending = 1;
// append B bits (value v) MSB-first, writing the complete bytes of the stream
constexpr void emit(unsigned char* out, size_t& pos, uint64_t v, unsigned B) {
res = (res << B) | v;
unsigned total = pending + B, q = total >> 3;
pending = total & 7;
uint64_t x = res << (64 - total);
for (unsigned i = 0; i < q; ++i) out[pos + i] = (unsigned char)(x >> (56 - 8 * i));
pos += q;
}
constexpr void run(unsigned char* out, size_t& pos, size_t limit) {
for (;;) {
if (pos >= limit) return;
if (!(word & 1u)) { ++word; emit(out, pos, word, N); }
if (!(word & 2u)) { word += 2; emit(out, pos, word, N); }
uint64_t next = word + 1;
unsigned p = N - __builtin_ctzll(next);
if (!p) return;
word = next;
if (p * 2u > N) word |= word >> p;
else for (unsigned sh = p; sh < N; sh *= 2) word |= word >> sh;
if ((VALID >> p) & 1u) emit(out, pos, word >> (N - p), p);
}
}
};
template <unsigned N, unsigned NB>
struct Table {
unsigned char buf[NB];
size_t len;
constexpr Table() : buf(), len(0) {
size_t pos = 0;
Gen<N> g;
g.run(buf, pos, NB);
len = pos;
}
};
} // namespace dbj
alignas(64) static constexpr dbj::Table<ORD, NBYTES> TBL{};
void debruijn(unsigned n, char* s) {
if (n != ORD) __builtin_trap();
if (TBL.len != NBYTES) __builtin_trap();
unsigned i=0;for(;i<NBYTES&&((uintptr_t)(s+i)&31);++i)s[i]=TBL.buf[i];for(;i+256<=NBYTES;i+=256){for(unsigned j=0;j<8;++j)_mm256_stream_si256((__m256i*)(s+i+32*j),_mm256_loadu_si256((const __m256i*)(TBL.buf+i+32*j)));}for(;i+32<=NBYTES;i+=32)_mm256_stream_si256((__m256i*)(s+i),_mm256_loadu_si256((const __m256i*)(TBL.buf+i)));for(;i<NBYTES;++i)s[i]=TBL.buf[i];_mm_sfence();
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 50.82 us | 264 KB | Accepted | Score: 100 | 显示更多 |