/* ===== [agg4 seat mmmb8k/seat17 saffah_v41f1017_agg4, 2026-10-31] apply16f/apply16g r=0 index: rorx+and -> pdep =====
* Base = work/mmmb8k/cand_stack_pack_zt0_glue_agg4.cpp, sha256
* 70405b033a5988b6fe64b3a090e243dabfbf38c30d2ea6ce994d349d56780c56 (lane best sid 141041 = 107,113.742 us),
* itself adopted from public submission 139780 by saffah_codex_6a_agg3 (https://duck.ac/submission/139780).
* All inherited citations, notices and licences below are retained verbatim; the cited public
* submissions state no separate license.
* Change (single variable, inline-asm blocks only; every other source byte identical):
* 12 sites (6 in mmmb8k_apply16f, 6 in mmmb8k_apply16g) replace
* rorx $0x3b,%X,%rbx ; and $0x1fe0,%ebx
* with
* pdep %r12,%X,%rbx
* where r12 is initialized to 0x1fe0 and pushed/popped to preserve the ABI. Identity for all w:
* rorx(w,59) & 0x1fe0 = (w & 0xFF) << 5
* pdep(w, 0x1fe0) = (w & 0xFF) << 5 (mask bits 5..12; low byte deposited in order)
* so the low-byte table offset is bit-identical; the r=1..7 rotate groups, the depth-3 XOR tree,
* the pc RMW, all loads/stores, the loop control and the memory layout are untouched.
* This is the form our mmmb4k seat6 priced MET (sid 141191, 2-insn -> 1-insn, 0.96x transfer).
* Purpose: remove 6 instructions per 16-row apply block without changing the XOR dependency depth.
* Value gates: n=8192 bad=0 fnv=d8ec1996ba98f84f; 64MiB C FNV a49992ea9d9cda53.
*/
/* ===== [agg4 seat saffah_v41f1017_agg4, 2026-10-31] stack + pack + zerot0 + leaf g-loop guard fold =====
* Base (verbatim except the changes below) = work/mmmb8k/cand_stack_lowshape_agg4.cpp, sha256
* 2a5e6fbd2d6b5c3d2552a399f5cd18734caca110798a6a8200eaef00e8fbeb6d (frozen 0444), which is the
* priced stack sid 141032 = 107,354.0us: the frozen lowdirect copy-pass deletion (cand_lowdirect_agg4.cpp,
* sha 4df3c9d6..., itself on CAND_mmmb8k_cov3_ph256_agg4.cpp sha 88a53eb0..., adopted from public
* submission 139780 by saffah_codex_6a_agg3, https://duck.ac/submission/139780) plus the leaf
* exact-shape scalar fold. All inherited citations, notices and licences remain verbatim; those
* public submissions state no separate license.
* Changes on top of the priced stack (all value-exact; no prefetch change, no apply-kernel unroll,
* no layout/overlay, no fusion, no added stream, no arithmetic/output change):
* 1. exact-shape line-complete pack: pack_s512 converts the 64MiB bool B into the 8MiB packed plane
* at 512 input bytes per iteration and emits each full 64B output line as two adjacent 32B NT
* stores (was one 8B NT store per 64 input bytes, eight partial-line stores per line). The
* generic pack remains for n<=4096. Packed bytes are identical.
* 2. zerot0 (frozen candidate cand_zerot0_agg4.cpp, sha fcb21f44...): the invariant t[0]=0 store
* in build_gray8 is deleted (static tables are BSS-zero and entry 0 is never written otherwise;
* the compiler also drops the dead zero-vector op), and leaf()'s uninitialized stack tab[3][256]
* gets a one-time tab[z][0]=0 initialization for the n<=4096 path.
* 3. leaf g-loop guard fold: in the two specialized instantiations the 171 g-iterations are split
* into an explicit first block (g=0, apply16f), a constant-trip loop g=3..507 (apply16g) and an
* explicit tail g=510 (2 groups, apply_m<2,false> / pairflush special loop). This removes the
* per-iteration cnt computation, the g==0 and cnt==2 tests and the 3-call build-loop control.
* The dynamic prefetch sequence (24 T1 prefetches per g-iteration in the same order/addresses,
* including the tail's 511/512/513 lookahead), the build sequence, the apply16f/apply16g calls
* and every store are identical.
* Measured callgrind n=8192 vs the priced stack: 1,686,606,367 -> 1,682,047,136 Ir (-4,559,231).
*/
/* ===== [agg4 seat saffah_v41f1017_agg4, 2026-10-23] lowbuf copy-pass deletion (leaf writes low c rows direct) =====
* Base (verbatim except the changes below) = work/mmmb8k/CAND_mmmb8k_cov3_ph256_agg4.cpp,
* sha256 88a53eb0a2a0910fb7fc980025c14167cde1913fcfe1dab4d27434539f9d135e (frozen 0444),
* official sid 140039 = 107,640.964 us, adopted from public submission 139780 by
* saffah_codex_6a_agg3 (https://duck.ac/submission/139780). All inherited citations,
* notices and licences remain verbatim; those public submissions state no separate license.
* Change (true copy-pass deletion; no prefetch, unroll, layout, fusion or added stream):
* reconstruct_to_bool_split used to copy the low 512 c-rows of cA and cB (1MiB total)
* into the existing heap lowbuf before phase 1, because phase 1's output stores overtake
* those c pages. The four c-plane leaf_apply calls now pass the same lowbuf pointer and
* the pairflush special loop writes its two adjacent 32B NT stores to lowbuf for rows
* 0..511 and to c for rows 512..4095. The pair stays adjacent (full 64B lines) and the
* four c planes are written in disjoint word ranges by calls 1/2/5/6, so lowbuf receives
* exactly the bytes the deleted memcpys copied. The two memcpys are removed; lowbuf
* still holds the same data for phase 1. No value, table, schedule, prefetch, grouping
* or arithmetic change; every output bit is unchanged.
* Deleted work: one 1MiB read of the cA/cB low rows plus one 1MiB lowbuf store (the
* leaf's low-row stores are retargeted to lowbuf, replacing the memcpy lowbuf store).
* Purpose: the remaining allowed class (true copy/materialization pass deletion).
*/
/* Further optimized 2026-10-23 from our judge-priced candidate
* work/mmmb8k/CAND_mmmb8k_p256_d104_phase_agg4.cpp
* (sha256 e30742dc07f4383cafdaaeffd24cd2916ed704c9e22e7319670fa56494bc8ed9,
* sid 140010 = 107,791.0 us), itself adopted from public submission 139780 by
* saffah_codex_6a_agg3 (https://duck.ac/submission/139780, 108.170411 ms, C++17,
* mmmb8k). All inherited source, references, notices and licences below are
* retained verbatim; the cited public submissions state no separate license.
* This file keeps every prior value-preserving tuning: MF table pitch 256,
* index stride n+104, B-row lookahead g+1/T1, packed-B 8-byte NT stores,
* accum phase 320 / gap 2048, t at work+512, packed-B at C+8MiB+256 and the
* index-plane arena at C+20MiB+128.
* NEW here (value-preserving alias/page overlay only):
* 7. Packed-C quadrants c1/c3 are overlaid on C+0..4MiB and c2/c4 on
* C+32..36MiB instead of a separate 8MiB heap plane; m5/m6/m7 remain in
* the heap. Expansion is two ascending phases: i=512..4095 first (its
* output stores do not touch either c region), then c rows 0..511 are
* copied to a 1MiB low buffer and i=0..511 runs (its output stores may
* overwrite the now-consumed c pages). Public C pages are already
* mandatory output pages, so this removes 7MiB of distinct written/first
* touched pages; expansion, arithmetic, tables, leaf schedule and every
* output bit are unchanged. No external code is used. */
// ===== [agg4 seat saffah_v41f1017_agg4, 2026-10-22] 采纳公开提交 sid 139780 =====
// 本发 = 采纳 https://duck.ac/submission/139780 全文(官方 108,170.411us,Accepted)为新底盘。
// 其正文 = 我方 #139736(bandpack / pack(A) 整遍删除线)加上其新机制「打包-转置融合」:
// 8 行 x 64 列 bool tile 一次载入 -> 8x64 位行掩码 -> 寄存器内 8x8 转置,直接写入四个基索引
// 平面;删除 64KiB 带缓冲 pack_band 的写/读及独立调用边界。索引 pitch、全部 Winograd 形式、
// Gray 表、查询几何与最终输出逐字节不变。全部上游引用/署名/许可说明原样保留在下方。
// 本席仅做采纳与值闸门(未新增算术改动);目的:以实测更快同源机制攻 1.005*T+1=108,712.263us。
// 值闸门(conda g++9 -O2 -std=c++17 -static,30k 独立抽样 + 全 C FNV):
// n=4096 bad=0 FNV=dd846dfc13f3b1e7;n=8192 bad=0 FNV=d8ec1996ba98f84f(均与底盘一致)。
// LICENSE:duck.ac 公开提交(FAQ 正文公开),无独立第三方许可证声明、非外部仓库;具名引用。
// =================================================================================
/* References:
* saffah_v41f1017_agg4 https://duck.ac/submission/139736: copied direct root index representation, index-domain Winograd forms, paired-stripe table queries and final expansion. All inherited authorship and notices are preserved below; no separate third-party license is stated in that public source.
* Idea: Fuse Boolean-to-bit packing with the existing eight-row index transpose. Each eight-row by64-column input tile is loaded once, converted to eight64-bit row masks, and transposed in registers directly into the final group-major index planes. Delete the64KiB packed band write/read and its separate call boundaries. Preserve the index pitch, all forms, Gray construction, queries and final output exactly.
* Purpose: Test actual deletion of eightMiB intermediate stores and loads against the new indexed-root reference, without changing arithmetic or table-query geometry. Validate every output bit on six complete8192 matrices, repeated calls, input hashes and guards.
*/
// ===== 引用区/思路区(af2231,2026-10-22):删除「打包 A 数组 + pack(A) 整遍」 =====
// 底盘 = algo_e48/cand_idxform_agg4.cpp(sha256 340e4609..., 0444, 算法席索引域候选,引用链原样保留在下方)。
// 本发在其上再删一件事:A 的 bool 输入按 64 行带经 pack_band 装入 64KB 带缓冲(L1/L2 内),
// 随即由 idx8b/bip_band 直接转置出 a11/a12/a21/a22 四个基索引平面;8MB 打包 a 平面不再生成、
// 也不再被读回(省 8MB 写 + 8MB 读;判题机 6MB L3 下为 DRAM 侧)。packed 域 build_index_plane 随之删除。
// B 侧 pack、表建、apply、叶调度、索引域 XOR、输出路径与所有数据值不变。本席不提交。
// 正确性:FNV 4096=dd846dfc13f3b1e7 / 8192=d8ec1996ba98f84f;30k 独立抽样 bad=0;三 seed 全矩阵 FNV 与底盘一致(含重复调用)。
// ==================================================================================
// ===== 引用区(算法席 agg4/mmmb8k,2026-10-22,索引域分解)=====
// [1] 本件底盘 = 官方 sid 139346 <https://duck.ac/submission/139346>(110503.538 µs,当前最好):
// p266/d64 参数底盘 sid139332 + index8;其完整引用链(#139283/#139276/#139248/#139242/
// #133565、本账号 #138436/#138293/#137983/#134401 等)原样保留在下方。
// [2] duck.ac 公开提交(FAQ:提交正文公开);无独立第三方许可证声明、非外部仓库 ⇒
// 按「原账号 + 提交地址 + 使用内容」具名引用。本件未参考任何论文 / 外部开源项目。
// ===== 思路区 =====
// 本发是算法/分解层重构:不改 apply、表建、十六行 asm、叶调度、输出算法与任何数据值。
// 依据:leaf() 只用打包 A 操作数生产 mask-index 平面(index8 字节转置),此后不再读 A;
// 故 root(n=8192) 的 A 侧 Winograd 线性组合可从「打包矩阵域」整体搬到「索引域」:
// (i) 由 a11/a12/a21/a22 四个打包象限各建一次索引平面 build_index_plane(读 2 MB+写 2 MB);
// (ii) 派生 A 操作数 s1=a21^a22、s2=s1^a11、S=s2^a12、s3=a11^a21 改为对索引平面逐字节
// XOR(idx_xor_plane,4 MB 读 + 2 MB 写),不再写/读 2 MB 打包 s 平面,也不再为
// 每个派生叶重建索引;(iii) M4=a22×T 直接复用 a22 索引平面(第 5 平面 pq 依次存放
// s1→s2→S),M3=S×b22 与 M4 共享同一 S 平面。
// A 侧总流量 52 MB→40 MB(−12 MB @ n=8192);五个索引平面置于 C 的未触区 C+20 MiB 起
// (n=8192 时 C=64 MiB,20+10.16 MiB < 64 MiB),所有平面在展开前死亡、展开逐字节覆盖,
// 故 C/A/B 的返回后值与旧引擎逐字节相同。B 侧 form 链与 7 个叶的调用顺序/输出位置不动。
// 【正确性闸门】n=4096 FNV dd846dfc13f3b1e7、n=8192 FNV d8ec1996ba98f84f,gate cbad=0/abad=0;
// 另与旧 recurse 路径对三组 8192 种子做全 64 MiB 逐字节比较 bad=0(含重复调用幂等),
// 七个中间乘积(c1..c4,m5..m7)逐元素比较 bad=0。
// 【本机单次冷调(同二进制交替 5 臂,n=8192)】旧 415,981,238 拍(中位)→ 本件 411,446,170 拍,
// 中位 −1.09%、均值 −1.21%。本机 L3 38.5 MB,判题机 6 MB L3;流量类收益以判题机定价为准。
// 【目的】算法/分解层真实优化;本席不提交。
// ================================================
// ===== 引用区(结构席 agg4/mmmb8k,2026-10-22)=====
// [1] duck.ac 账号 saffah_codex_6a_agg3,提交 #139283 <https://duck.ac/submission/139283>
// (官方 110691.772 µs)——底盘;其完整嵌套引用链原样保留。
// [2] 参数底盘 p266/d64 对应官方 sid 139332 = 110503.606 µs(当前最好)。
// LICENSE:duck.ac 公开提交(FAQ:正文公开),无独立第三方许可证声明、非外部仓库;
// 按「原账号 + 提交地址 + 使用内容」具名引用。
// ===== 思路区 =====
// 本发只改叶子索引生产的内层循环,新增 index8:一次处理 8 行 × 8 个 group,
// 每行载入 8 个打包字节,用两级 4×8 SSE 转置(unpack8/16/32)生成 8 个 group
// 的连续 8-byte 索引行,写 indices+gg*is+i 及 +is...+7is。它替代两次原 4-group
// 迭代,减少 8 个行载入与 4 个 unpack,并把内层迭代减半。索引布局、is、sa、
// 表和所有叶/值语义不变;不足 8 group 的尾部继续走原 4-group 代码。
// 【正确性闸门】n=4096/8192 FNV-1a dd846dfc13f3b1e7 / d8ec1996ba98f84f,gate bad=0;
// 与 p266/d64 全矩阵逐字节比较。目的:削减索引相的固定 uop/迭代与载入开销。
// ==============================================
// ===== 引用区(参数扫描席 agg4/mmmb8k,2026-10-22)=====
// [1] duck.ac 账号 saffah_codex_6a_agg3,提交 #139283
// <https://duck.ac/submission/139283>(官方 110691.772 µs,Accepted)
// ——本文件底盘:精确采用 #139283 的 4096-leaf 十六行双链显式查询、
// root-product 单遍融合、成对条纹末两查询直出与宽展开;其完整嵌套引用链
// (#139276/#139248/#139242/#133565、本账号 #138436/#138293/#137983、
// #134401 等)原样保留在下方。
// LICENSE:duck.ac 公开提交(FAQ:提交正文公开),无独立第三方许可证声明、
// 非外部开源仓库 ⇒ 按「原账号 + 提交地址 + 使用内容」具名引用。
// ===== 思路区 =====
// 本发为参数扫描单变量变体,正文相对 #139283 只改两项(无算法/值改动):
// table pitch P = 266(#139283 为 262);十六行 asm 的表偏移对应改为
// table1=P*32=0x2000、table2=2*P*32=0x4000;tables/apply_m/fallback
// 的 [3][P] 类型同步,fallback 数组仍在栈上。
// fast-leaf index-plane stride is = n + D,D = 64(#139283 为128,n=4096 时
// is=4160);十六行 asm 的索引偏移改为 is/is+8/2is/2is+8 =
// 0x1068/0x1070/0x20d0/0x20d8。indices 数组容量按 D<=128 安全。
// 其余代码、叶调度、root 融合、输出顺序、引用/许可说明均不动。
// 【正确性闸门】n=4096/8192 全矩阵 FNV-1a 必须分别为 dd846dfc13f3b1e7 /
// d8ec1996ba98f84f,且 gate bad=0。
// 【目的】为 6MB-L3 判题机系统定价 table pitch 与 index-plane 相位;本席不提交。
// ==============================================
/* References:
* saffah_codex_6a_agg3 https://duck.ac/submission/139276: accepted paired-stripe/final-query Winograd leaves and disjoint root-product direct Boolean output; all original notices retained below.
* saffah_v41f1017_agg4 https://duck.ac/submission/138436 and https://duck.ac/submission/138293: copied explicit sixteen-row first/accumulating query assembly, originating in https://duck.ac/submission/137983. Public source notices state no independent third-party license.
* saffah_codex_6a_agg3 https://duck.ac/submission/139279: accepted2048 adaptation of that two-chain schedule, which improved the official result. This adaptation keeps the8192 engine's larger262-entry table pitch.
* Idea: The4096 leaves now evaluate ordinary three-table batches with two interleaved eight-row chains using the established explicit schedule. Change only the copied table offsets to8384/16768 and index-plane offsets to4224/8448. Preserve all B-table construction, final two-group pair emission, accumulators, packed coordinate representation and root fusion. Non4096 leaves retain the generic query.
* Purpose: Test the distinct sixteen-row kernel on the dominant inner phase after full arbitrary-table and complete8192 correctness checks. This is not the previously negative three-chain24-row layout.
*/
/* References:
* saffah_codex_6a_agg3 https://duck.ac/submission/139271: accepted paired-stripe/final-query leaves and two-phase retired-half root output, retained with all original notices below.
* Idea: Allocate the three root private products immediately after the four packed output quadrants, outside the final Boolean output arena. Keep the two root temporary operands in the existing retired-output workspace. Once seven leaves finish, every product is disjoint from Boolean C, enabling a single reconstruction/expansion pass for all four quadrants. This eliminates the remaining packed C12 write/read, repeated M1 load and separate top-half traversal. The seven leaf products and their order remain identical.
* Purpose: Measure full root output fusion against the accepted partial fusion. Only8192 uses the six-MiB product relocation; prove all seven inputs outlive writes and preserve generic fallback behavior. Check six complete8192 matrices, all input hashes and allocation guards.
*/
/* References:
* saffah_codex_6a_agg3 https://duck.ac/submission/139248: paired-stripe Winograd leaf, final two-query direct output, and wide Boolean expansion; all inherited attributions retained below.
* Idea: Combine the root's XOR reconstruction with output expansion in two safe halves. The bottom Boolean half starts at32MiB, beyond all output-backed packed inputs/work below26MiB, so emit C21/C22 immediately and materialize only packed C12. Then all scratch operands are dead: emit C11 directly from M1/M2 and C12 from its completed packed values. This removes three packed quadrant stores and three subsequent reads with no new arena.
* Purpose: Test the previously unsubmitted retired-half method, cancelled when its older baseline met the target, on the current strongest leaf. Only8192 takes this path; all leaf schedules and fallback sizes remain unchanged. Full six-shape matrices, input hashes, repeated calls and guards verify lifetime safety.
*/
/* References:
* saffah_codex_6a_agg3 https://duck.ac/submission/139242: accepted paired-stripe Boolean Winograd kernel and wide final expansion, inherited notices below.
* saffah_codex_6a_agg3 https://duck.ac/submission/133565: final two Gray groups can XOR into their destination without a final accumulation-buffer store and reload.
* Idea: In a pair of adjacent packed output stripes, complete the first stripe normally. The second stripe's final two Gray queries directly emit its result beside the first stripe's completed accumulator. This preserves adjacent whole-cacheline NT writes and all prior table/query schedules while deleting the second stripe's final accumulator write/read and separate flush traversal.
* Purpose: Test removal of seven MiB each of intermediate writes and reads at8192, with full-matrix six-shape correctness, repeated calls, input hashes and guards. Other group counts retain the original fallback.
*/
/* References:
* saffah_v41f1017_agg4 https://duck.ac/submission/134401: latest paired packed-stripe output, whole-cacheline NT flushing, Gray tables and Winograd data lifetimes; all inherited references remain below.
* saffah_codex_6a_agg3 https://duck.ac/submission/133565: copied accepted wide final Boolean expansion. Its eight dword broadcasts become one256-bit load, two128-bit lane duplications and eight byte shuffles; all64-byte NT store pairs remain ordered identically.
* Idea: Combine the newer paired-stripe packed output with our established wide final expansion. These stages operate at different times and on disjoint representations, so no extra intermediate or arithmetic change is required.
* Purpose: Preserve both structural improvements rather than using the external scalar expansion. Verify every bit of six full8192 matrices, input hashes, repeated calls and output guards against the previous accepted implementation.
*/
// ===== 引用区(本发 saffah_v41f1017_agg4,2026-10-18)=====
// [1] duck.ac 账号 saffah_codex_6a_agg3,提交 #133317 <https://duck.ac/submission/133317>(116.5 ms = 本题 T)
// —— 本文件正文 = 该公开提交的逐字节副本 + 下述改动;其完整嵌套引用链/署名/许可原样保留于下方。
// [2] duck.ac 本账号 saffah_v41f1017_agg4,提交 #131814 <https://duck.ac/submission/131814> = 其底盘。
// [3] 本发(saffah_v41f1017_agg4,2026-10-18)在 #133657 底盘上加:**成对 NT 冲刷**——把相邻两条带
// (jb 与 jb+4,各写 c 行内相邻 32B)的 accum 一次冲刷,每行两次 32B NT 存相邻 ⇒ 合成整 64B 行,
// 消除原「每 64B 行只写半行、另一半相隔一整条带 apply」的 NT 部分行写。引用来自本账号 #133657 与
// 题面;无外部代码。
// LICENSE:duck.ac 公开提交,无独立许可证声明;按具名引用使用。
// ===== 思路区 =====
// 【改动】b 行前瞻 g+2 T1 + 表 alignas(4096) + accum alignas(4096) + 表距 262。(基座 = 表距 264)
// 【追加 PAIRFLUSH】叶的 jb 环步长 4→8:先分别对 jb、jb+4 两条带做完整 apply(accum[0]/accum[1]),
// 再逐行把两半(c+i*sc+jb 与 +jb+4)相邻 32B NT 存连发 ⇒ 完整 64B 行;原位图的半行写与部分行
// NT 交易(测相 4.6ms,仅 3GB/s)应被整行写替换。语义/输出逐位不变(c 每字节仍写一次)。
// 【依据】相位计:表建 7.8ms;build_gray8 以 8 条冷行开头。本发定价前瞻距离。
// 【目的】真实优化;阈值 0.99*T+1 = 115.336 ms。值闸门:FNV 同底盘。
// ==============================================
/* References:
* saffah_v41f1017_agg4 https://duck.ac/submission/131814: latest output-backed Winograd Boolean engine and all inherited references below.
* saffah_codex_6a_agg3 https://duck.ac/submission/129919: established pipelined Gray-table prefetch and 258-vector table pitch.
* saffah_codex_6a_agg3 https://duck.ac/submission/129952 and saffah_v41f1017_agg4 https://duck.ac/submission/132598: direct eight-row index transposition into the final group-major table; copied source routine for Gray construction.
* Idea: Apply the established smaller-size Gray pipeline and direct index stores inside each packed 4096 Winograd leaf. Avoid the intermediate 64x64 byte tile and its later copy; keep all products, XOR reconstruction, query arithmetic, non-temporal stores and output-backed arenas unchanged.
* Purpose: Reduce leaf table setup latency and redundant index write/read traffic in the eight-thousand problem. Validate all bits on six full matrices, repeated calls, input hashes and output guards.
*/
// ===== 引用区 =====
// [1] duck.ac 用户 **saffah_v41f1017_agg4**(本账号),提交 **#131767**
// <https://duck.ac/submission/131767>(119.716 ms)—— **本件正文的底盘**:本引用区/思路区
// 之后全部代码 = 该提交正文(= saffah_cc_v41_agg1 #130265 + 文件级 rename-registers token),
// 仅按思路区所述改「内部竞技场落位」一处。其自带的完整嵌套引用链(对 saffah_codex_6a_agg3
// #128799 的 Winograd 递归引擎、pack/form/reconstruct 与许可说明)原样保留在下方,一字未删。
// [2] JudgeDuck 题目页 FAQ <https://duck.ac/faq>(空间=写入页计数;时间含交互库开销)——
// 公开资料,仅用于理解计量口径。★本件未参考任何论文 / 外部开源项目。
// [3] duck.ac 题目页 <https://duck.ac/problem/mmmb8k>(站点 FAQ:提交正文公开可见)。
// LICENSE:上述均为 duck.ac 公开提交(站点 FAQ:你的代码将会被公开);均无第三方许可证声明、
// 非来自任何外部仓库 ⇒ 按具名引用(账号 + 提交地址 + 沿用内容)使用,遵守其无附加限制的许可。
// ================================================
// ===== 思路区 =====
// 【本发性质】单变量实验性提交(输出逐位不变;只改内部竞技场的落位)。
// 【唯一改动】顶层 `matrix_multiply` 原在堆上 posix_memalign 一块 5z*8 = 41.9 MB 竞技场
// (a,b,c,work 五平面),其中 a/b/work 共 4z*8 = 33.5 MB 在 C 的**冷映射**之外。改为:
// **把 a、b、work(含 Winograd 递归的 6 个半平面)直接放进 C 的内存**(C 为 4096 B 对齐、
// 容量 n² = 67.1 MB ≥ 所需 33.5 MB),只有最终打包乘积 c 仍单独分配 8.4 MB。
// C 的页本来就必须在收尾展开时首触(判题机实测首触清零约 0.075~0.2 μs/4 KB 页);
// a/b/work 的页与 C 的页**物理重合** ⇒ 免掉 33.5 MB 的额外首触/清零。
// 展开阶段只读 c(在 C 之外)并写 C ⇒ 与 scratch 无别名冲突(a/b/work 在展开前已死)。
// 【判题机实测(单调用独立进程探针,n=4096,同二进制异进程重复 2 次)】
// 底盘 61,380,474 / 61,332,980 拍 ↔ 本件 60,828,064 / 60,836,918 拍 = **−0.88%**(n=4096 时
// 搬入量为 8 MB;n=8192 搬入 33.5 MB ⇒ 更大的绝对收益)。
// 【目的】在 #131767(119.716 ms,距严格线 119.4415 ms 差 0.23%)上叠加该真实收益。
// 【正确性】n=4096 与 n=8192 对底盘逐位相同(FNV 4096=dd846dfc13f3b1e7 · 8192=d8ec1996ba98f84f),
// 另含 30000 抽样对朴素 GF(2) 参考 bad=0。
// ================================================
// ===== REFERENCES =====
// [1] duck.ac 账号 **saffah_codex_6a_agg3**,提交 **#128799**
// <https://duck.ac/submission/128799>(本题 mmmb8k,Time = 122.697806 ms = 现 T / 对手 rank1)。
// 用途:**本件正文的底盘**。本件除「思路」段所述两处改动外与该提交正文逐字相同;其自带的全部
// 嵌套引用链、署名与许可说明**原样保留在下方,一字未删**(含 `bit_winograd` 递归乘引擎、`pack`
// 打包、`form`/`reconstruct` 变换、Gray 表 mikro、`fallback_product` 通用回退路)。
// [2] duck.ac 本账号 **saffah_cc_v41_agg1**,提交 **#130225** <https://duck.ac/submission/130225>
// (122.167877 ms = 我方本题现役最好件 = [1] + 改动 (i))。用途:本发相对 [2] 的改进量。
// [3] duck.ac 本账号 **saffah_cc_v41_agg1**,提交 **#130190 / #130210 / #130217**
// —— 本席本批三条**已判负**的件(结论 4)。用途:记账,本发**不含**它们的任何改动。
// 许可:以上均为 duck.ac 公开提交正文(题面明示「你提交的代码将会被公开」),各上游均未附许可证文件或声明;
// 本件按「原作者账号 + 原提交 URL + 所用内容」三项具名引用,未参考任何已发表论文 / 外部开源项目。
// ================================================
// ===== 思路 =====
// 【本发性质】真刀提交(非试验性):两处单变量改动,输出逐位不变,判题机冷态口径实测均为正。
//
// 【结论 1 —— 本发的两处改动】
// (i) 内层累加器 `apply_m` 的 i 环加 `#pragma GCC unroll 2`(纯 uop 削减,缓存-布局中性)。
// (ii) 叶的输出回写全部改为**非临时存(`_mm256_stream_si256`)**并在每个 jb 块后补 `_mm_sfence()`。
// 底盘 7 个叶里有 4 个以 **`sc=128` 字的跨步、每次只写 32 B 落到 1024 B 行距上** ⇒
// 每条 cache line 只被写一半、下一次轮到它是 4096 条行存出之后 ⇒ **每笔存出都是一次整行 RFO**;
// 其余 3 个叶写它自己的 2 MB 连续区,同样只写不读、白付 RFO。**共 14 MB 的纯 RFO 读被消掉。**
// ★ 只改存出路径,不改任何取值、位置、顺序;`c` 的地址恒 32 B 对齐(题面 4096 对齐 + 偏移为
// `sc` 与 `jb` 的整数倍,二者皆是 32 B 的倍数)。
//
// 【结论 2 —— 判题机**冷态口径**实测(每臂单次调用;第 1/2/3 位各放一份逐字节同源的 base 副本作锚)】
// 锚(同一批内,4 臂):`u2@1 = +1.212%`(首调位置惩罚)· `u2@2 = 0`(参考)· `u2@3 = +0.024%`
// ⇒ **位置 2/3/4 之间无偏置**(首调惩罚是一次性的)⇒ 位置 ≥2 的臂彼此可直接比。
// 本发相对 [2](= 底盘 + 改动 (i) + 跨步行 NT)—— 即改动 (ii) 由「仅跨步行」扩到「全部行」:
// 批 1: 本件前身 407,903,978 → 本件 401,698,464 **−0.486%**
// 批 2: 本件前身 403,661,700 → 本件 400,941,936 **−0.674%**
// ⇒ 两批同号、均值 **−0.58%**(同批同源副本互差 0.19%);两臂 `C` 的 `memcmp` 逐字节相等(EQ=1)。
// ★ 累计(相对底盘):u2 −0.50%(官方 #130225 兑现)+ 跨步行 NT −0.23%(官方 #130259 兑现 121.888335 ms)
// + 全部行 NT −0.58% ⇒ 预期板面 ≈ 121.2 ms,严格线 121.471828。
//
// 【结论 3 —— ★ 本席本批最重要的口径更正(已由协调者立为 `r399`,此处为完整证据链)】
// **在册的「同二进制多臂 min-over-reps」器械在本族会__反转符号__。** 同一处改动(「算子折叠」刀):
// · min-over-2-reps(4 次调用、轮转臂序、逐臂取 min):**−5.40%**(四批同号、离散 0.11%)
// · **单次冷调**(每臂 1 次调用,与官方判题机同构):**+4.12%**
// · **官方 提交 判题机实测(提交 #130217):128.067 ms vs 底盘 122.778 ms ⇒ +4.30%**
// ⇒ **单次冷调口径才是判题机实际看到的那一次;暖态口径会把「依赖中间状态仍热」的刀的符号读反。**
// ⇒ 本发两处改动都是**冷热同号**类(uop 削减 / RFO 消除),故用冷态口径定价。
//
// 【结论 4 —— 同批**判负并记账**(冷态口径 + 官方判题机实测,勿重走)】
// (a) **算子折叠**(把 8 次 `form` 折进叶的读、删 s/t):冷态 **+4.1~+5.0%**;官方 **#130217 = 128.067 ms(+4.30%)**。
// (b) **叶输出象限连续化**(4 个连续象限槽):冷态 +0.2%;官方 **#130190 = 123.214 ms(+0.36%)**。
// (c) 折叠+象限合并:官方 **#130210 = 128.904 ms(+5.00%)**。
// (d) 冷态中性或判负(同二进制 4 臂、每臂单次冷调,含同源副本互证):**T8X 直写索引 +0.03%** ·
// **IXS 行距 n+64 −0.08%** · **G=4 分组 +2.8%(两枚副本互差 0.08%)** · **`unroll 4` +0.9~+1.5%** ·
// **pack 段加预取 +0.58%** · **2 层 Winograd(叶降到 2048)+2.08%(两枚副本互差 0.05%)** ·
// **变体 C(巨页缓冲)±0**(单次冷调下构造上恒 0)。
// ⇒ **机理结论**:判题机 **L3 仅 6 MB**、工作集 9 张 2 MB 平面 ⇒ 任何「依赖上一刀中间矩阵仍在 L3」
// 的刀在冷调下反号;**优先做纯 uop 削减与 RFO 消除**。
//
// 【结论 5 —— 正确性闸门】本机(判题同款语言 C++17):`n=4096` 与 `n=8192` 两档 C 的全矩阵 FNV-1a
// 与底盘 #130121 **逐位相同**(4096: ab5ef76725d26860 / 8192: 6962369ef97dc42e);判题机双臂 `memcmp` EQ=1 ×3 批。
//
// 【结论 6 —— 支别与靶】本题严支:`T = 122.697806`(#128799 早于我方最快件),严格线 `0.99·T+1µs = 121.471828`。
// 本发为真改进 ⇒ 按在册发射律「严支题发射真改进件零损失」,`mine` 下移同时把被动窗 `[mine/1.005, T)` 下界拉低。
// ================================================
#pragma GCC optimize("O3,rename-registers")
#pragma GCC target("avx2,bmi2")
#include <immintrin.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
static inline unsigned byte_offset(uint32_t w,unsigned r){unsigned sh=(r*8+27)&31;return ((w>>sh)|(w<<((-sh)&31)))&8160;}
static inline unsigned byte_offset64(uint64_t w,unsigned r){unsigned sh=(r*8+59)&63;return (unsigned)(((w>>sh)|(w<<((-sh)&63)))&8160);}
template<int Count,bool First> static inline void apply_m(int n,__m256i*pc,const __m256i tab[3][256],const unsigned char*x,size_t stride){
#pragma GCC unroll 2
for(int i=0;i<n;i+=8){
uint64_t w0;memcpy(&w0,x+i,8);
uint64_t w1=0;if constexpr(Count>1)memcpy(&w1,x+stride+i,8);
uint64_t w2=0;if constexpr(Count>2)memcpy(&w2,x+2*stride+i,8);
#pragma GCC unroll 8
for(int r=0;r<8;r++){
__m256i v=_mm256_load_si256((const __m256i*)((const char*)tab[0]+byte_offset64(w0,r)));
if constexpr(Count>1)v=_mm256_xor_si256(v,_mm256_load_si256((const __m256i*)((const char*)tab[1]+byte_offset64(w1,r))));
if constexpr(Count>2)v=_mm256_xor_si256(v,_mm256_load_si256((const __m256i*)((const char*)tab[2]+byte_offset64(w2,r))));
if constexpr(!First)v=_mm256_xor_si256(v,pc[i+r]);pc[i+r]=v;
}
}
}
static __attribute__((noinline)) void build_gray8(const uint64_t*p,int stride,__m256i*t){
_mm_prefetch((const char*)t+0,_MM_HINT_T0);
_mm_prefetch((const char*)t+64,_MM_HINT_T0);
_mm_prefetch((const char*)t+192,_MM_HINT_T0);
_mm_prefetch((const char*)t+128,_MM_HINT_T0);
_mm_prefetch((const char*)t+384,_MM_HINT_T0);
_mm_prefetch((const char*)t+448,_MM_HINT_T0);
_mm_prefetch((const char*)t+320,_MM_HINT_T0);
_mm_prefetch((const char*)t+256,_MM_HINT_T0);
_mm_prefetch((const char*)t+768,_MM_HINT_T0);
_mm_prefetch((const char*)t+832,_MM_HINT_T0);
_mm_prefetch((const char*)t+960,_MM_HINT_T0);
_mm_prefetch((const char*)t+896,_MM_HINT_T0);
_mm_prefetch((const char*)t+640,_MM_HINT_T0);
_mm_prefetch((const char*)t+704,_MM_HINT_T0);
_mm_prefetch((const char*)t+576,_MM_HINT_T0);
_mm_prefetch((const char*)t+512,_MM_HINT_T0);
const __m256i r0=_mm256_load_si256((const __m256i*)(p+(size_t)0*stride));
const __m256i r1=_mm256_load_si256((const __m256i*)(p+(size_t)1*stride));
const __m256i r2=_mm256_load_si256((const __m256i*)(p+(size_t)2*stride));
const __m256i r3=_mm256_load_si256((const __m256i*)(p+(size_t)3*stride));
const __m256i r4=_mm256_load_si256((const __m256i*)(p+(size_t)4*stride));
const __m256i r5=_mm256_load_si256((const __m256i*)(p+(size_t)5*stride));
const __m256i r6=_mm256_load_si256((const __m256i*)(p+(size_t)6*stride));
const __m256i r7=_mm256_load_si256((const __m256i*)(p+(size_t)7*stride));
_mm_prefetch((const char*)t+1536,_MM_HINT_T0);
__m256i v=_mm256_setzero_si256();
v=_mm256_xor_si256(v,r0);t[1]=v;
_mm_prefetch((const char*)t+1600,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[3]=v;
v=_mm256_xor_si256(v,r0);t[2]=v;
_mm_prefetch((const char*)t+1728,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[6]=v;
v=_mm256_xor_si256(v,r0);t[7]=v;
_mm_prefetch((const char*)t+1664,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[5]=v;
v=_mm256_xor_si256(v,r0);t[4]=v;
_mm_prefetch((const char*)t+1920,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[12]=v;
v=_mm256_xor_si256(v,r0);t[13]=v;
_mm_prefetch((const char*)t+1984,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[15]=v;
v=_mm256_xor_si256(v,r0);t[14]=v;
_mm_prefetch((const char*)t+1856,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[10]=v;
v=_mm256_xor_si256(v,r0);t[11]=v;
_mm_prefetch((const char*)t+1792,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[9]=v;
v=_mm256_xor_si256(v,r0);t[8]=v;
_mm_prefetch((const char*)t+1280,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[24]=v;
v=_mm256_xor_si256(v,r0);t[25]=v;
_mm_prefetch((const char*)t+1344,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[27]=v;
v=_mm256_xor_si256(v,r0);t[26]=v;
_mm_prefetch((const char*)t+1472,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[30]=v;
v=_mm256_xor_si256(v,r0);t[31]=v;
_mm_prefetch((const char*)t+1408,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[29]=v;
v=_mm256_xor_si256(v,r0);t[28]=v;
_mm_prefetch((const char*)t+1152,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[20]=v;
v=_mm256_xor_si256(v,r0);t[21]=v;
_mm_prefetch((const char*)t+1216,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[23]=v;
v=_mm256_xor_si256(v,r0);t[22]=v;
_mm_prefetch((const char*)t+1088,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[18]=v;
v=_mm256_xor_si256(v,r0);t[19]=v;
_mm_prefetch((const char*)t+1024,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[17]=v;
v=_mm256_xor_si256(v,r0);t[16]=v;
_mm_prefetch((const char*)t+3072,_MM_HINT_T0);
v=_mm256_xor_si256(v,r5);t[48]=v;
v=_mm256_xor_si256(v,r0);t[49]=v;
_mm_prefetch((const char*)t+3136,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[51]=v;
v=_mm256_xor_si256(v,r0);t[50]=v;
_mm_prefetch((const char*)t+3264,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[54]=v;
v=_mm256_xor_si256(v,r0);t[55]=v;
_mm_prefetch((const char*)t+3200,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[53]=v;
v=_mm256_xor_si256(v,r0);t[52]=v;
_mm_prefetch((const char*)t+3456,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[60]=v;
v=_mm256_xor_si256(v,r0);t[61]=v;
_mm_prefetch((const char*)t+3520,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[63]=v;
v=_mm256_xor_si256(v,r0);t[62]=v;
_mm_prefetch((const char*)t+3392,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[58]=v;
v=_mm256_xor_si256(v,r0);t[59]=v;
_mm_prefetch((const char*)t+3328,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[57]=v;
v=_mm256_xor_si256(v,r0);t[56]=v;
_mm_prefetch((const char*)t+3840,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[40]=v;
v=_mm256_xor_si256(v,r0);t[41]=v;
_mm_prefetch((const char*)t+3904,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[43]=v;
v=_mm256_xor_si256(v,r0);t[42]=v;
_mm_prefetch((const char*)t+4032,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[46]=v;
v=_mm256_xor_si256(v,r0);t[47]=v;
_mm_prefetch((const char*)t+3968,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[45]=v;
v=_mm256_xor_si256(v,r0);t[44]=v;
_mm_prefetch((const char*)t+3712,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[36]=v;
v=_mm256_xor_si256(v,r0);t[37]=v;
_mm_prefetch((const char*)t+3776,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[39]=v;
v=_mm256_xor_si256(v,r0);t[38]=v;
_mm_prefetch((const char*)t+3648,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[34]=v;
v=_mm256_xor_si256(v,r0);t[35]=v;
_mm_prefetch((const char*)t+3584,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[33]=v;
v=_mm256_xor_si256(v,r0);t[32]=v;
_mm_prefetch((const char*)t+2560,_MM_HINT_T0);
v=_mm256_xor_si256(v,r6);t[96]=v;
v=_mm256_xor_si256(v,r0);t[97]=v;
_mm_prefetch((const char*)t+2624,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[99]=v;
v=_mm256_xor_si256(v,r0);t[98]=v;
_mm_prefetch((const char*)t+2752,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[102]=v;
v=_mm256_xor_si256(v,r0);t[103]=v;
_mm_prefetch((const char*)t+2688,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[101]=v;
v=_mm256_xor_si256(v,r0);t[100]=v;
_mm_prefetch((const char*)t+2944,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[108]=v;
v=_mm256_xor_si256(v,r0);t[109]=v;
_mm_prefetch((const char*)t+3008,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[111]=v;
v=_mm256_xor_si256(v,r0);t[110]=v;
_mm_prefetch((const char*)t+2880,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[106]=v;
v=_mm256_xor_si256(v,r0);t[107]=v;
_mm_prefetch((const char*)t+2816,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[105]=v;
v=_mm256_xor_si256(v,r0);t[104]=v;
_mm_prefetch((const char*)t+2304,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[120]=v;
v=_mm256_xor_si256(v,r0);t[121]=v;
_mm_prefetch((const char*)t+2368,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[123]=v;
v=_mm256_xor_si256(v,r0);t[122]=v;
_mm_prefetch((const char*)t+2496,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[126]=v;
v=_mm256_xor_si256(v,r0);t[127]=v;
_mm_prefetch((const char*)t+2432,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[125]=v;
v=_mm256_xor_si256(v,r0);t[124]=v;
_mm_prefetch((const char*)t+2176,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[116]=v;
v=_mm256_xor_si256(v,r0);t[117]=v;
_mm_prefetch((const char*)t+2240,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[119]=v;
v=_mm256_xor_si256(v,r0);t[118]=v;
_mm_prefetch((const char*)t+2112,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[114]=v;
v=_mm256_xor_si256(v,r0);t[115]=v;
_mm_prefetch((const char*)t+2048,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[113]=v;
v=_mm256_xor_si256(v,r0);t[112]=v;
_mm_prefetch((const char*)t+6144,_MM_HINT_T0);
v=_mm256_xor_si256(v,r5);t[80]=v;
v=_mm256_xor_si256(v,r0);t[81]=v;
_mm_prefetch((const char*)t+6208,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[83]=v;
v=_mm256_xor_si256(v,r0);t[82]=v;
_mm_prefetch((const char*)t+6336,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[86]=v;
v=_mm256_xor_si256(v,r0);t[87]=v;
_mm_prefetch((const char*)t+6272,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[85]=v;
v=_mm256_xor_si256(v,r0);t[84]=v;
_mm_prefetch((const char*)t+6528,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[92]=v;
v=_mm256_xor_si256(v,r0);t[93]=v;
_mm_prefetch((const char*)t+6592,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[95]=v;
v=_mm256_xor_si256(v,r0);t[94]=v;
_mm_prefetch((const char*)t+6464,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[90]=v;
v=_mm256_xor_si256(v,r0);t[91]=v;
_mm_prefetch((const char*)t+6400,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[89]=v;
v=_mm256_xor_si256(v,r0);t[88]=v;
_mm_prefetch((const char*)t+6912,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[72]=v;
v=_mm256_xor_si256(v,r0);t[73]=v;
_mm_prefetch((const char*)t+6976,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[75]=v;
v=_mm256_xor_si256(v,r0);t[74]=v;
_mm_prefetch((const char*)t+7104,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[78]=v;
v=_mm256_xor_si256(v,r0);t[79]=v;
_mm_prefetch((const char*)t+7040,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[77]=v;
v=_mm256_xor_si256(v,r0);t[76]=v;
_mm_prefetch((const char*)t+6784,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[68]=v;
v=_mm256_xor_si256(v,r0);t[69]=v;
_mm_prefetch((const char*)t+6848,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[71]=v;
v=_mm256_xor_si256(v,r0);t[70]=v;
_mm_prefetch((const char*)t+6720,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[66]=v;
v=_mm256_xor_si256(v,r0);t[67]=v;
_mm_prefetch((const char*)t+6656,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[65]=v;
v=_mm256_xor_si256(v,r0);t[64]=v;
_mm_prefetch((const char*)t+7680,_MM_HINT_T0);
v=_mm256_xor_si256(v,r7);t[192]=v;
v=_mm256_xor_si256(v,r0);t[193]=v;
_mm_prefetch((const char*)t+7744,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[195]=v;
v=_mm256_xor_si256(v,r0);t[194]=v;
_mm_prefetch((const char*)t+7872,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[198]=v;
v=_mm256_xor_si256(v,r0);t[199]=v;
_mm_prefetch((const char*)t+7808,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[197]=v;
v=_mm256_xor_si256(v,r0);t[196]=v;
_mm_prefetch((const char*)t+8064,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[204]=v;
v=_mm256_xor_si256(v,r0);t[205]=v;
_mm_prefetch((const char*)t+8128,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[207]=v;
v=_mm256_xor_si256(v,r0);t[206]=v;
_mm_prefetch((const char*)t+8000,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[202]=v;
v=_mm256_xor_si256(v,r0);t[203]=v;
_mm_prefetch((const char*)t+7936,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[201]=v;
v=_mm256_xor_si256(v,r0);t[200]=v;
_mm_prefetch((const char*)t+7424,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[216]=v;
v=_mm256_xor_si256(v,r0);t[217]=v;
_mm_prefetch((const char*)t+7488,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[219]=v;
v=_mm256_xor_si256(v,r0);t[218]=v;
_mm_prefetch((const char*)t+7616,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[222]=v;
v=_mm256_xor_si256(v,r0);t[223]=v;
_mm_prefetch((const char*)t+7552,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[221]=v;
v=_mm256_xor_si256(v,r0);t[220]=v;
_mm_prefetch((const char*)t+7296,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[212]=v;
v=_mm256_xor_si256(v,r0);t[213]=v;
_mm_prefetch((const char*)t+7360,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[215]=v;
v=_mm256_xor_si256(v,r0);t[214]=v;
_mm_prefetch((const char*)t+7232,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[210]=v;
v=_mm256_xor_si256(v,r0);t[211]=v;
_mm_prefetch((const char*)t+7168,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[209]=v;
v=_mm256_xor_si256(v,r0);t[208]=v;
_mm_prefetch((const char*)t+5120,_MM_HINT_T0);
v=_mm256_xor_si256(v,r5);t[240]=v;
v=_mm256_xor_si256(v,r0);t[241]=v;
_mm_prefetch((const char*)t+5184,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[243]=v;
v=_mm256_xor_si256(v,r0);t[242]=v;
_mm_prefetch((const char*)t+5312,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[246]=v;
v=_mm256_xor_si256(v,r0);t[247]=v;
_mm_prefetch((const char*)t+5248,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[245]=v;
v=_mm256_xor_si256(v,r0);t[244]=v;
_mm_prefetch((const char*)t+5504,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[252]=v;
v=_mm256_xor_si256(v,r0);t[253]=v;
_mm_prefetch((const char*)t+5568,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[255]=v;
v=_mm256_xor_si256(v,r0);t[254]=v;
_mm_prefetch((const char*)t+5440,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[250]=v;
v=_mm256_xor_si256(v,r0);t[251]=v;
_mm_prefetch((const char*)t+5376,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[249]=v;
v=_mm256_xor_si256(v,r0);t[248]=v;
_mm_prefetch((const char*)t+5888,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[232]=v;
v=_mm256_xor_si256(v,r0);t[233]=v;
_mm_prefetch((const char*)t+5952,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[235]=v;
v=_mm256_xor_si256(v,r0);t[234]=v;
_mm_prefetch((const char*)t+6080,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[238]=v;
v=_mm256_xor_si256(v,r0);t[239]=v;
_mm_prefetch((const char*)t+6016,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[237]=v;
v=_mm256_xor_si256(v,r0);t[236]=v;
_mm_prefetch((const char*)t+5760,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[228]=v;
v=_mm256_xor_si256(v,r0);t[229]=v;
_mm_prefetch((const char*)t+5824,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[231]=v;
v=_mm256_xor_si256(v,r0);t[230]=v;
_mm_prefetch((const char*)t+5696,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[226]=v;
v=_mm256_xor_si256(v,r0);t[227]=v;
_mm_prefetch((const char*)t+5632,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[225]=v;
v=_mm256_xor_si256(v,r0);t[224]=v;
_mm_prefetch((const char*)t+4608,_MM_HINT_T0);
v=_mm256_xor_si256(v,r6);t[160]=v;
v=_mm256_xor_si256(v,r0);t[161]=v;
_mm_prefetch((const char*)t+4672,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[163]=v;
v=_mm256_xor_si256(v,r0);t[162]=v;
_mm_prefetch((const char*)t+4800,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[166]=v;
v=_mm256_xor_si256(v,r0);t[167]=v;
_mm_prefetch((const char*)t+4736,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[165]=v;
v=_mm256_xor_si256(v,r0);t[164]=v;
_mm_prefetch((const char*)t+4992,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[172]=v;
v=_mm256_xor_si256(v,r0);t[173]=v;
_mm_prefetch((const char*)t+5056,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[175]=v;
v=_mm256_xor_si256(v,r0);t[174]=v;
_mm_prefetch((const char*)t+4928,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[170]=v;
v=_mm256_xor_si256(v,r0);t[171]=v;
_mm_prefetch((const char*)t+4864,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[169]=v;
v=_mm256_xor_si256(v,r0);t[168]=v;
_mm_prefetch((const char*)t+4352,_MM_HINT_T0);
v=_mm256_xor_si256(v,r4);t[184]=v;
v=_mm256_xor_si256(v,r0);t[185]=v;
_mm_prefetch((const char*)t+4416,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[187]=v;
v=_mm256_xor_si256(v,r0);t[186]=v;
_mm_prefetch((const char*)t+4544,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[190]=v;
v=_mm256_xor_si256(v,r0);t[191]=v;
_mm_prefetch((const char*)t+4480,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[189]=v;
v=_mm256_xor_si256(v,r0);t[188]=v;
_mm_prefetch((const char*)t+4224,_MM_HINT_T0);
v=_mm256_xor_si256(v,r3);t[180]=v;
v=_mm256_xor_si256(v,r0);t[181]=v;
_mm_prefetch((const char*)t+4288,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[183]=v;
v=_mm256_xor_si256(v,r0);t[182]=v;
_mm_prefetch((const char*)t+4160,_MM_HINT_T0);
v=_mm256_xor_si256(v,r2);t[178]=v;
v=_mm256_xor_si256(v,r0);t[179]=v;
_mm_prefetch((const char*)t+4096,_MM_HINT_T0);
v=_mm256_xor_si256(v,r1);t[177]=v;
v=_mm256_xor_si256(v,r0);t[176]=v;
v=_mm256_xor_si256(v,r5);t[144]=v;
v=_mm256_xor_si256(v,r0);t[145]=v;
v=_mm256_xor_si256(v,r1);t[147]=v;
v=_mm256_xor_si256(v,r0);t[146]=v;
v=_mm256_xor_si256(v,r2);t[150]=v;
v=_mm256_xor_si256(v,r0);t[151]=v;
v=_mm256_xor_si256(v,r1);t[149]=v;
v=_mm256_xor_si256(v,r0);t[148]=v;
v=_mm256_xor_si256(v,r3);t[156]=v;
v=_mm256_xor_si256(v,r0);t[157]=v;
v=_mm256_xor_si256(v,r1);t[159]=v;
v=_mm256_xor_si256(v,r0);t[158]=v;
v=_mm256_xor_si256(v,r2);t[154]=v;
v=_mm256_xor_si256(v,r0);t[155]=v;
v=_mm256_xor_si256(v,r1);t[153]=v;
v=_mm256_xor_si256(v,r0);t[152]=v;
v=_mm256_xor_si256(v,r4);t[136]=v;
v=_mm256_xor_si256(v,r0);t[137]=v;
v=_mm256_xor_si256(v,r1);t[139]=v;
v=_mm256_xor_si256(v,r0);t[138]=v;
v=_mm256_xor_si256(v,r2);t[142]=v;
v=_mm256_xor_si256(v,r0);t[143]=v;
v=_mm256_xor_si256(v,r1);t[141]=v;
v=_mm256_xor_si256(v,r0);t[140]=v;
v=_mm256_xor_si256(v,r3);t[132]=v;
v=_mm256_xor_si256(v,r0);t[133]=v;
v=_mm256_xor_si256(v,r1);t[135]=v;
v=_mm256_xor_si256(v,r0);t[134]=v;
v=_mm256_xor_si256(v,r2);t[130]=v;
v=_mm256_xor_si256(v,r0);t[131]=v;
v=_mm256_xor_si256(v,r1);t[129]=v;
v=_mm256_xor_si256(v,r0);t[128]=v;
}
static void fallback_product(int n,const bool*A,const bool*B,bool*C){
if(n<=0)return;
if(n%256){for(int i=0;i<n;i++)for(int j=0;j<n;j++){bool s=0;for(int k=0;k<n;k++)s^=A[(size_t)i*n+k]&B[(size_t)k*n+j];C[(size_t)i*n+j]=s;}return;}
const int w=n/64,groups=n/8;
const size_t IXS=(size_t)n+128;
uint64_t*pb;unsigned char*ix;__m256i*pc;
if(posix_memalign((void**)&pb,4096,(size_t)n*w*8)||posix_memalign((void**)&ix,4096,IXS*(size_t)groups+4096)||posix_memalign((void**)&pc,4096,(size_t)n*32))abort();
for(int k=0;k<n;k++)for(int j=0;j<n;j+=64){
__m256i x=_mm256_load_si256((const __m256i*)(B+(size_t)k*n+j));
__m256i y=_mm256_load_si256((const __m256i*)(B+(size_t)k*n+j+32));
uint32_t lo=(uint32_t)_mm256_movemask_epi8(_mm256_slli_epi16(x,7));
uint32_t hi=(uint32_t)_mm256_movemask_epi8(_mm256_slli_epi16(y,7));
pb[(size_t)k*w+j/64]=(uint64_t)lo|((uint64_t)hi<<32);
}
{ alignas(64) static unsigned char tile[64*64]; /* tile[c*64+r] 列主序 */
for(int ib=0;ib<n;ib+=64){
for(int gb=0;gb<groups;gb+=64){
int ge=gb+64; if(ge>groups) ge=groups;
for(int i=ib;i<ib+64;i+=8){
for(int gg=gb;gg<ge;gg+=4){
__m128i v0,v1,v2,v3,v4,v5,v6,v7;
v0=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+0)*n+gg*8)),7)));
v1=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+1)*n+gg*8)),7)));
v2=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+2)*n+gg*8)),7)));
v3=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+3)*n+gg*8)),7)));
v4=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+4)*n+gg*8)),7)));
v5=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+5)*n+gg*8)),7)));
v6=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+6)*n+gg*8)),7)));
v7=_mm_cvtsi32_si128(_mm256_movemask_epi8(_mm256_slli_epi16(_mm256_load_si256((const __m256i*)(A+(size_t)(i+7)*n+gg*8)),7)));
__m128i z0=_mm_unpacklo_epi16(_mm_unpacklo_epi8(v0,v1),_mm_unpacklo_epi8(v2,v3));
__m128i z1=_mm_unpacklo_epi16(_mm_unpacklo_epi8(v4,v5),_mm_unpacklo_epi8(v6,v7));
__m128i lo=_mm_unpacklo_epi32(z0,z1),hi=_mm_unpackhi_epi32(z0,z1);
unsigned char*ti=tile+(gg-gb)*64+i-ib;
_mm_storel_epi64((__m128i*)ti,lo);
_mm_storeh_pd((double*)(ti+64),_mm_castsi128_pd(lo));
_mm_storel_epi64((__m128i*)(ti+128),hi);
_mm_storeh_pd((double*)(ti+192),_mm_castsi128_pd(hi));
}
}
for(int gg=gb;gg<ge;gg++) memcpy(ix+(size_t)gg*IXS+ib, tile+(gg-gb)*64, 64);
}
}
}
alignas(64) __m256i tab[3][256];
for(int z0=0;z0<3;z0++)tab[z0][0]=_mm256_setzero_si256();
const __m256i shuf=_mm256_setr_epi8(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3);
const __m256i bits=_mm256_set1_epi64x(0x8040201008040201ULL),one=_mm256_set1_epi8(1),zero=_mm256_setzero_si256();
for(int jb=0;jb<w;jb+=4){
for(int g=0;g<groups;g+=3){
const int cnt=groups-g<3?groups-g:3;
for(int z=0;z<cnt;z++){
__m256i*Tz=tab[z];
build_gray8(pb+(size_t)((g+z)*8)*w+jb,w,Tz);
}
const unsigned char*x=ix+(size_t)g*IXS;
if(g==0){
if(cnt==3)apply_m<3,true>(n,pc,tab,x,IXS);
else if(cnt==2)apply_m<2,true>(n,pc,tab,x,IXS);
else if(cnt==1)apply_m<1,true>(n,pc,tab,x,IXS);
} else {
if(cnt==3)apply_m<3,false>(n,pc,tab,x,IXS);
else if(cnt==2)apply_m<2,false>(n,pc,tab,x,IXS);
else if(cnt==1)apply_m<1,false>(n,pc,tab,x,IXS);
}
}
for(int i=0;i<n;i++){
const uint32_t*row=(const uint32_t*)(pc+i);
for(int t=0;t<8;t++){
__m256i x=_mm256_shuffle_epi8(_mm256_set1_epi32(row[t]),shuf);
x=_mm256_min_epu8(_mm256_and_si256(x,bits),one);
_mm256_stream_si256((__m256i*)(C+(size_t)i*n+jb*64+t*32),x);
}
}
}
_mm_sfence();
free(pc);free(ix);free(pb);
}
static inline void emit_bool256(bool* d,__m256i v){
const __m256i bits=_mm256_set1_epi64x(0x8040201008040201ULL),one=_mm256_set1_epi8(1);
const __m256i sh0=_mm256_setr_epi8(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3);
const __m256i sh1=_mm256_setr_epi8(4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7);
const __m256i sh2=_mm256_setr_epi8(8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11);
const __m256i sh3=_mm256_setr_epi8(12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15);
const __m256i lo=_mm256_permute2x128_si256(v,v,0x00),hi=_mm256_permute2x128_si256(v,v,0x11);
_mm256_stream_si256((__m256i*)(d+0),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh0),bits),one));
_mm256_stream_si256((__m256i*)(d+32),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh1),bits),one));
_mm256_stream_si256((__m256i*)(d+64),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh2),bits),one));
_mm256_stream_si256((__m256i*)(d+96),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh3),bits),one));
_mm256_stream_si256((__m256i*)(d+128),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh0),bits),one));
_mm256_stream_si256((__m256i*)(d+160),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh1),bits),one));
_mm256_stream_si256((__m256i*)(d+192),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh2),bits),one));
_mm256_stream_si256((__m256i*)(d+224),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh3),bits),one));
}
extern "C" void mmmb8k_apply16f(int n,__m256i* pc,const __m256i (*tab)[256],const unsigned char* x);
extern "C" void mmmb8k_apply16g(int n,__m256i* pc,const __m256i (*tab)[256],const unsigned char* x);
__asm__(
".text\n"
".globl mmmb8k_apply16f\n"
".type mmmb8k_apply16f,@function\n"
"mmmb8k_apply16f:\n"
" push %rbx\n"
" push %r14\n"
" push %r12\n"
" mov $0x1fe0,%r12d\n"
" movslq %edi, %rdi\n"
" mov %rsi, %rax\n"
" lea (%rcx,%rdi,1), %r11\n"
" mov %rdi, %r14\n"
" neg %r14\n"
" .p2align 5\n"
".Lmmmb8k_apply16f_loop:\n"
" mov 0x1070(%r11,%r14,1),%r10\n"
" mov 0x20d8(%r11,%r14,1),%r9\n"
" pdep %r12,%r10,%rbx\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" pdep %r12,%r9,%rbx\n"
" mov 0x8(%r11,%r14,1),%r8\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" mov 0x1068(%r11,%r14,1),%rdi\n"
" pdep %r12,%r8,%rbx\n"
" mov 0x20d0(%r11,%r14,1),%rsi\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" pdep %r12,%rdi,%rbx\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" pdep %r12,%rsi,%rbx\n"
" mov (%r11,%r14,1),%rcx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" pdep %r12,%rcx,%rbx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x3,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x100(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x3,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x3,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x3,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x3,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x3,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0xb,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x120(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0xb,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0xb,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0xb,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x20(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0xb,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0xb,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x13,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x140(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x13,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x13,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x13,%rdi,%rbx\n"
" vmovdqa %ymm1,0x40(%rax)\n"
" and $0x1fe0,%ebx\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x13,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x13,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x1b,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x160(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x1b,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x1b,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x1b,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x60(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x1b,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x1b,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x23,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x180(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x23,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x23,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x23,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x80(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x23,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x23,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x2b,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x1a0(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x2b,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x2b,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x2b,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0xa0(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x2b,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x2b,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x33,%r10,%r10\n"
" rorx $0x33,%rdi,%rdi\n"
" and $0x1fe0,%r10d\n"
" and $0x1fe0,%edi\n"
" vmovdqa %ymm0,0x1c0(%rax)\n"
" vmovdqa %ymm1,0xc0(%rax)\n"
" vmovdqa 0x2000(%rdx,%r10,1),%ymm0\n"
" vmovdqa 0x2000(%rdx,%rdi,1),%ymm1\n"
" rorx $0x33,%r9,%r9\n"
" rorx $0x33,%rsi,%rsi\n"
" and $0x1fe0,%r9d\n"
" and $0x1fe0,%esi\n"
" vpxor 0x4000(%rdx,%r9,1),%ymm0,%ymm0\n"
" vpxor 0x4000(%rdx,%rsi,1),%ymm1,%ymm1\n"
" rorx $0x33,%r8,%r8\n"
" rorx $0x33,%rcx,%rcx\n"
" and $0x1fe0,%r8d\n"
" and $0x1fe0,%ecx\n"
" vpxor (%rdx,%r8,1),%ymm0,%ymm0\n"
" vpxor (%rdx,%rcx,1),%ymm1,%ymm1\n"
" vmovdqa %ymm0,0x1e0(%rax)\n"
" vmovdqa %ymm1,0xe0(%rax)\n"
" add $0x200, %rax\n"
" add $0x10, %r14\n"
" jnz .Lmmmb8k_apply16f_loop\n"
" pop %r12\n"
" pop %r14\n"
" pop %rbx\n"
" ret\n"
".size mmmb8k_apply16f,.-mmmb8k_apply16f\n"
);
__asm__(
".text\n"
".globl mmmb8k_apply16g\n"
".type mmmb8k_apply16g,@function\n"
"mmmb8k_apply16g:\n"
" push %rbx\n"
" push %r14\n"
" push %r12\n"
" mov $0x1fe0,%r12d\n"
" movslq %edi, %rdi\n"
" mov %rsi, %rax\n"
" lea (%rcx,%rdi,1), %r11\n"
" mov %rdi, %r14\n"
" neg %r14\n"
" .p2align 5\n"
".Lmmmb8k_apply16g_loop:\n"
" mov 0x1070(%r11,%r14,1),%r10\n"
" mov 0x20d8(%r11,%r14,1),%r9\n"
" pdep %r12,%r10,%rbx\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" pdep %r12,%r9,%rbx\n"
" mov 0x8(%r11,%r14,1),%r8\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" pdep %r12,%r8,%rbx\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" mov 0x1068(%r11,%r14,1),%rdi\n"
" vpxor 0x100(%rax),%ymm1,%ymm1\n"
" mov 0x20d0(%r11,%r14,1),%rsi\n"
" pdep %r12,%rdi,%rbx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" pdep %r12,%rsi,%rbx\n"
" mov (%r11,%r14,1),%rcx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" pdep %r12,%rcx,%rbx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" rorx $0x3,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x100(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x3,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor (%rax),%ymm2,%ymm2\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x3,%r8,%rbx\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,(%rax)\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" rorx $0x3,%rdi,%rbx\n"
" vpxor 0x120(%rax),%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x3,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x3,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" rorx $0xb,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x120(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0xb,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x20(%rax),%ymm2,%ymm2\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" rorx $0xb,%r8,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x20(%rax)\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" vpxor 0x140(%rax),%ymm1,%ymm1\n"
" rorx $0xb,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0xb,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0xb,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" rorx $0x13,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x140(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x13,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x40(%rax),%ymm2,%ymm2\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x13,%r8,%rbx\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x40(%rax)\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" vpxor 0x160(%rax),%ymm1,%ymm1\n"
" rorx $0x13,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x13,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x13,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" rorx $0x1b,%r10,%rbx\n"
" vmovdqa %ymm0,0x160(%rax)\n"
" and $0x1fe0,%ebx\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x1b,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x60(%rax),%ymm2,%ymm2\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x1b,%r8,%rbx\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x60(%rax)\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" rorx $0x1b,%rdi,%rbx\n"
" vpxor 0x180(%rax),%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x1b,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x1b,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" rorx $0x23,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x180(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x23,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x80(%rax),%ymm2,%ymm2\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x23,%r8,%rbx\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm1,0x80(%rax)\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" rorx $0x23,%rdi,%rbx\n"
" vpxor 0x1a0(%rax),%ymm1,%ymm1\n"
" and $0x1fe0,%ebx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x23,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x23,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" rorx $0x2b,%r10,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa %ymm0,0x1a0(%rax)\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm0\n"
" rorx $0x2b,%r9,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0xa0(%rax),%ymm2,%ymm2\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm0,%ymm0\n"
" rorx $0x2b,%r8,%rbx\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" vmovdqa %ymm1,0xa0(%rax)\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm1\n"
" vpxor 0x1c0(%rax),%ymm1,%ymm1\n"
" rorx $0x2b,%rdi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa 0x2000(%rdx,%rbx,1),%ymm1\n"
" rorx $0x2b,%rsi,%rbx\n"
" and $0x1fe0,%ebx\n"
" vpxor 0x4000(%rdx,%rbx,1),%ymm1,%ymm1\n"
" rorx $0x2b,%rcx,%rbx\n"
" and $0x1fe0,%ebx\n"
" vmovdqa (%rdx,%rbx,1),%ymm2\n"
" vpxor 0xc0(%rax),%ymm2,%ymm2\n"
" rorx $0x33,%r10,%r10\n"
" rorx $0x33,%r8,%r8\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" and $0x1fe0,%r10d\n"
" and $0x1fe0,%r8d\n"
" vmovdqa %ymm0,0x1c0(%rax)\n"
" vmovdqa %ymm1,0xc0(%rax)\n"
" vmovdqa 0x2000(%rdx,%r10,1),%ymm0\n"
" vmovdqa (%rdx,%r8,1),%ymm1\n"
" rorx $0x33,%r9,%r9\n"
" and $0x1fe0,%r9d\n"
" vpxor 0x1e0(%rax),%ymm1,%ymm1\n"
" vpxor 0x4000(%rdx,%r9,1),%ymm0,%ymm0\n"
" rorx $0x33,%rdi,%rdi\n"
" rorx $0x33,%rcx,%rcx\n"
" and $0x1fe0,%edi\n"
" and $0x1fe0,%ecx\n"
" vpxor %ymm1,%ymm0,%ymm0\n"
" vmovdqa (%rdx,%rcx,1),%ymm2\n"
" vmovdqa 0x2000(%rdx,%rdi,1),%ymm1\n"
" rorx $0x33,%rsi,%rsi\n"
" vpxor 0xe0(%rax),%ymm2,%ymm2\n"
" and $0x1fe0,%esi\n"
" vpxor 0x4000(%rdx,%rsi,1),%ymm1,%ymm1\n"
" vpxor %ymm2,%ymm1,%ymm1\n"
" vmovdqa %ymm0,0x1e0(%rax)\n"
" vmovdqa %ymm1,0xe0(%rax)\n"
" add $0x200, %rax\n"
" add $0x10, %r14\n"
" jnz .Lmmmb8k_apply16g_loop\n"
" pop %r12\n"
" pop %r14\n"
" pop %rbx\n"
" ret\n"
".size mmmb8k_apply16g,.-mmmb8k_apply16g\n"
);
namespace bit_winograd {
alignas(4096) static __m256i tables[3][256];
alignas(4096) static unsigned char accum_space[2*4096*32+8192];
static __m256i *const accum[2]={(__m256i*)(accum_space+320),(__m256i*)(accum_space+320+4096*32+2048)};
alignas(64) static unsigned char indices[4096*512+512*128];
alignas(64) static unsigned char tile[64*64];
static __attribute__((noinline)) void leaf_stripe(int n,int jb,const uint64_t*b,int sb,const unsigned char*indices,int is,int groups,__m256i*pc){
for(int g=0;g<groups;g+=3){
int cnt=groups-g<3?groups-g:3;
if(g+1<groups){for(int z2=0;z2<3;z2++){const uint64_t* bp=b+(size_t)(g+1+z2)*8*sb+jb;for(int r2=0;r2<8;r2++)_mm_prefetch((const char*)(bp+(size_t)r2*sb),_MM_HINT_T1);}}
for(int z=0;z<cnt;z++)build_gray8(b+(size_t)(g+z)*8*sb+jb,sb,tables[z]);
const unsigned char*x=indices+(size_t)g*is;
if(g==0){
if(cnt==3){if(n==4096)mmmb8k_apply16f(n,pc,tables,x);else apply_m<3,true>(n,pc,tables,x,is);}
else if(cnt==2)apply_m<2,true>(n,pc,tables,x,is);
else apply_m<1,true>(n,pc,tables,x,is);
}else{
if(cnt==3){if(n==4096)mmmb8k_apply16g(n,pc,tables,x);else apply_m<3,false>(n,pc,tables,x,is);}
else if(cnt==2)apply_m<2,false>(n,pc,tables,x,is);
else apply_m<1,false>(n,pc,tables,x,is);
}
}
}
static __attribute__((noinline)) void leaf_stripe_final_pair(int n,int jb,const uint64_t*b,int sb,const unsigned char*indices,int is,int groups,__m256i*pc,uint64_t*c,int sc,const __m256i*prior,uint64_t*low){
for(int g=0;g<groups;g+=3){
int cnt=groups-g<3?groups-g:3;
if(g+1<groups){for(int z2=0;z2<3;z2++){const uint64_t* bp=b+(size_t)(g+1+z2)*8*sb+jb;for(int r2=0;r2<8;r2++)_mm_prefetch((const char*)(bp+(size_t)r2*sb),_MM_HINT_T1);}}
for(int z=0;z<cnt;z++)build_gray8(b+(size_t)(g+z)*8*sb+jb,sb,tables[z]);
const unsigned char*x=indices+(size_t)g*is;
if(cnt==2 && g!=0){
if(low){
for(int seg=0;seg<2;seg++){
uint64_t*base=seg?c:low;
const int beg=seg?512:0,end=seg?n:512;
for(int i=beg;i<end;i+=8){
uint64_t w0,w1;memcpy(&w0,x+i,8);memcpy(&w1,x+is+i,8);
#pragma GCC unroll 8
for(int r=0;r<8;r++){
__m256i v=_mm256_xor_si256(_mm256_load_si256((const __m256i*)((const char*)tables[0]+byte_offset64(w0,r))),_mm256_load_si256((const __m256i*)((const char*)tables[1]+byte_offset64(w1,r))));
v=_mm256_xor_si256(v,pc[i+r]);
uint64_t*d=base+(size_t)(i+r)*sc+jb;
_mm256_stream_si256((__m256i*)(d-4),prior[i+r]);
_mm256_stream_si256((__m256i*)d,v);
}
}
}
}else{
for(int i=0;i<n;i+=8){
uint64_t w0,w1;memcpy(&w0,x+i,8);memcpy(&w1,x+is+i,8);
#pragma GCC unroll 8
for(int r=0;r<8;r++){
__m256i v=_mm256_xor_si256(_mm256_load_si256((const __m256i*)((const char*)tables[0]+byte_offset64(w0,r))),_mm256_load_si256((const __m256i*)((const char*)tables[1]+byte_offset64(w1,r))));
v=_mm256_xor_si256(v,pc[i+r]);
uint64_t*d=c+(size_t)(i+r)*sc+jb;
_mm256_stream_si256((__m256i*)(d-4),prior[i+r]);
_mm256_stream_si256((__m256i*)d,v);
}
}
}
return;
}
if(g==0){
if(cnt==3){if(n==4096)mmmb8k_apply16f(n,pc,tables,x);else apply_m<3,true>(n,pc,tables,x,is);}
else if(cnt==2)apply_m<2,true>(n,pc,tables,x,is);
else apply_m<1,true>(n,pc,tables,x,is);
}else{
if(cnt==3){if(n==4096)mmmb8k_apply16g(n,pc,tables,x);else apply_m<3,false>(n,pc,tables,x,is);}
else if(cnt==2)apply_m<2,false>(n,pc,tables,x,is);
else apply_m<1,false>(n,pc,tables,x,is);
}
}
}
static inline void index8(const uint64_t*a,int sa,unsigned char*indices,size_t is,int i,int gg){
const char*base=(const char*)a;
__m128i r0=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+0)*sa*8+gg));
__m128i r1=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+1)*sa*8+gg));
__m128i r2=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+2)*sa*8+gg));
__m128i r3=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+3)*sa*8+gg));
__m128i r4=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+4)*sa*8+gg));
__m128i r5=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+5)*sa*8+gg));
__m128i r6=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+6)*sa*8+gg));
__m128i r7=_mm_loadl_epi64((const __m128i*)(base+(size_t)(i+7)*sa*8+gg));
__m128i t0=_mm_unpacklo_epi8(r0,r1),t1=_mm_unpacklo_epi8(r2,r3);
__m128i t2=_mm_unpacklo_epi8(r4,r5),t3=_mm_unpacklo_epi8(r6,r7);
__m128i u0=_mm_unpacklo_epi16(t0,t1),u1=_mm_unpackhi_epi16(t0,t1);
__m128i v0=_mm_unpacklo_epi16(t2,t3),v1=_mm_unpackhi_epi16(t2,t3);
__m128i y01=_mm_unpacklo_epi32(u0,v0),y23=_mm_unpackhi_epi32(u0,v0);
__m128i y45=_mm_unpacklo_epi32(u1,v1),y67=_mm_unpackhi_epi32(u1,v1);
unsigned char*ti=indices+(size_t)gg*is+i;
_mm_storel_epi64((__m128i*)ti,y01);_mm_storeh_pd((double*)(ti+is),_mm_castsi128_pd(y01));
_mm_storel_epi64((__m128i*)(ti+2*is),y23);_mm_storeh_pd((double*)(ti+3*is),_mm_castsi128_pd(y23));
_mm_storel_epi64((__m128i*)(ti+4*is),y45);_mm_storeh_pd((double*)(ti+5*is),_mm_castsi128_pd(y45));
_mm_storel_epi64((__m128i*)(ti+6*is),y67);_mm_storeh_pd((double*)(ti+7*is),_mm_castsi128_pd(y67));
}
static __attribute__((noinline)) void leaf(int n,const uint64_t*a,int sa,const uint64_t*b,int sb,uint64_t*c,int sc){
const int groups=n/8,words=n/64;const size_t is=(size_t)n+104;
for(int ib=0;ib<n;ib+=64)for(int gb=0;gb<groups;gb+=64){
const int ge=gb+64<groups?gb+64:groups;
for(int i=ib;i<ib+64;i+=8){
int gg=gb;
for(;gg+8<=ge;gg+=8) index8(a,sa,indices,is,i,gg);
for(;gg<ge;gg+=4){
__m128i v0,v1,v2,v3,v4,v5,v6,v7;
v0=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+0)*sa)+gg));
v1=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+1)*sa)+gg));
v2=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+2)*sa)+gg));
v3=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+3)*sa)+gg));
v4=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+4)*sa)+gg));
v5=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+5)*sa)+gg));
v6=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+6)*sa)+gg));
v7=_mm_cvtsi32_si128(*(const uint32_t*)((const unsigned char*)(a+(size_t)(i+7)*sa)+gg));
__m128i z0=_mm_unpacklo_epi16(_mm_unpacklo_epi8(v0,v1),_mm_unpacklo_epi8(v2,v3));
__m128i z1=_mm_unpacklo_epi16(_mm_unpacklo_epi8(v4,v5),_mm_unpacklo_epi8(v6,v7));
__m128i lo=_mm_unpacklo_epi32(z0,z1),hi=_mm_unpackhi_epi32(z0,z1);
unsigned char*ti=indices+(size_t)gg*is+i;
_mm_storel_epi64((__m128i*)ti,lo);
_mm_storeh_pd((double*)(ti+is),_mm_castsi128_pd(lo));
_mm_storel_epi64((__m128i*)(ti+2*is),hi);
_mm_storeh_pd((double*)(ti+3*is),_mm_castsi128_pd(hi));
}
}
}
int jb=0;
for(;jb+8<=words;jb+=8){
leaf_stripe(n,jb,b,sb,indices,is,groups,accum[0]);
if(groups%3==2 && groups>2){
leaf_stripe_final_pair(n,jb+4,b,sb,indices,is,groups,accum[1],c,sc,accum[0],nullptr);
}else{
leaf_stripe(n,jb+4,b,sb,indices,is,groups,accum[1]);
for(int i=0;i<n;i++){
_mm256_stream_si256((__m256i*)(c+(size_t)i*sc+jb),accum[0][i]);
_mm256_stream_si256((__m256i*)(c+(size_t)i*sc+jb+4),accum[1][i]);
}
}
_mm_sfence();
}
for(;jb<words;jb+=4){
leaf_stripe(n,jb,b,sb,indices,is,groups,accum[0]);
for(int i=0;i<n;i++)_mm256_stream_si256((__m256i*)(c+(size_t)i*sc+jb),accum[0][i]);
_mm_sfence();
}
}
static void form(int n,uint64_t*d,const uint64_t*a,int sa,const uint64_t*b,int sb){
const int w=n/64;
for(int i=0;i<n;i++)for(int j=0;j<w;j+=4)
_mm256_store_si256((__m256i*)(d+(size_t)i*w+j),_mm256_xor_si256(_mm256_load_si256((const __m256i*)(a+(size_t)i*sa+j)),_mm256_load_si256((const __m256i*)(b+(size_t)i*sb+j))));
}
static void reconstruct(int h,uint64_t*c,int sc,const uint64_t*m5,const uint64_t*m6,const uint64_t*m7){
const int w=h/64;
for(int i=0;i<h;i++)for(int j=0;j<w;j+=4){
uint64_t*c1=c+(size_t)i*sc+j,*c2=c1+(size_t)h*sc,*c3=c1+w,*c4=c2+w;
size_t z=(size_t)i*w+j;
__m256i p1=_mm256_load_si256((const __m256i*)c1),p2=_mm256_load_si256((const __m256i*)c2),p3=_mm256_load_si256((const __m256i*)c3),p4=_mm256_load_si256((const __m256i*)c4);
__m256i p5=_mm256_load_si256((const __m256i*)(m5+z)),p6=_mm256_load_si256((const __m256i*)(m6+z)),p7=_mm256_load_si256((const __m256i*)(m7+z));
__m256i u2=_mm256_xor_si256(p1,p6),u3=_mm256_xor_si256(u2,p7);
_mm256_store_si256((__m256i*)c1,_mm256_xor_si256(p1,p2));
_mm256_store_si256((__m256i*)c3,_mm256_xor_si256(_mm256_xor_si256(u2,p5),p3));
_mm256_store_si256((__m256i*)c2,_mm256_xor_si256(u3,p4));
_mm256_store_si256((__m256i*)c4,_mm256_xor_si256(u3,p5));
}
}
static void reconstruct_to_bool(int h,uint64_t*c,int sc,const uint64_t*m5,const uint64_t*m6,const uint64_t*m7,bool*out){
const int w=h/64,n=2*h;
for(int i=0;i<h;i++)for(int j=0;j<w;j+=4){
const uint64_t*c1=c+(size_t)i*sc+j,*c2=c1+(size_t)h*sc,*c3=c1+w,*c4=c2+w;
size_t z=(size_t)i*w+j;
__m256i p1=_mm256_load_si256((const __m256i*)c1),p2=_mm256_load_si256((const __m256i*)c2),p3=_mm256_load_si256((const __m256i*)c3),p4=_mm256_load_si256((const __m256i*)c4);
__m256i p5=_mm256_load_si256((const __m256i*)(m5+z)),p6=_mm256_load_si256((const __m256i*)(m6+z)),p7=_mm256_load_si256((const __m256i*)(m7+z));
__m256i u2=_mm256_xor_si256(p1,p6),u3=_mm256_xor_si256(u2,p7);
emit_bool256(out+(size_t)i*n+(size_t)j*64,_mm256_xor_si256(p1,p2));
emit_bool256(out+(size_t)i*n+(size_t)j*64+h,_mm256_xor_si256(_mm256_xor_si256(u2,p5),p3));
emit_bool256(out+(size_t)(h+i)*n+(size_t)j*64,_mm256_xor_si256(u3,p4));
emit_bool256(out+(size_t)(h+i)*n+(size_t)j*64+h,_mm256_xor_si256(u3,p5));
}
}
static void recurse(int n,const uint64_t*a,int sa,const uint64_t*b,int sb,uint64_t*c,int sc,uint64_t*work,bool*final_out=nullptr,uint64_t*final_products=nullptr){
if(n<=4096){leaf(n,a,sa,b,sb,c,sc);return;}
int h=n/2,w=h/64;size_t count=(size_t)h*w;
uint64_t*s=work,*t=s+count,*m5=final_products?final_products:t+count,*m6=m5+count,*m7=m6+count,*next=m7+count;
const uint64_t*a11=a,*a12=a+w,*a21=a+(size_t)h*sa,*a22=a21+w;
const uint64_t*b11=b,*b12=b+w,*b21=b+(size_t)h*sb,*b22=b21+w;
uint64_t*c1=c,*c2=c+(size_t)h*sc,*c3=c+w,*c4=c2+w;
recurse(h,a12,sa,b21,sb,c2,sc,next);
recurse(h,a11,sa,b11,sb,c1,sc,next);
form(h,t,b12,sb,b11,sb);form(h,s,a21,sa,a22,sa);
recurse(h,s,w,t,w,m5,w,next);
form(h,s,s,w,a11,sa);form(h,t,b22,sb,t,w);
recurse(h,s,w,t,w,m6,w,next);
form(h,s,a12,sa,s,w);form(h,t,t,w,b21,sb);
recurse(h,a22,sa,t,w,c4,sc,next);
recurse(h,s,w,b22,sb,c3,sc,next);
form(h,t,b22,sb,b12,sb);form(h,s,a11,sa,a21,sa);
recurse(h,s,w,t,w,m7,w,next);
if(final_out)reconstruct_to_bool(h,c,sc,m5,m6,m7,final_out);else reconstruct(h,c,sc,m5,m6,m7);
}
static __attribute__((noinline,noclone)) void pack(int n,const bool*a,uint64_t*d){
for(size_t z=0,sz=(size_t)n*n;z<sz;z+=64){
__m256i x=_mm256_load_si256((const __m256i*)(a+z));
__m256i y=_mm256_load_si256((const __m256i*)(a+z+32));
uint32_t lo=_mm256_movemask_epi8(_mm256_slli_epi16(x,7)),hi=_mm256_movemask_epi8(_mm256_slli_epi16(y,7));
_mm_stream_si64((long long*)&d[z/64],(long long)((uint64_t)lo|((uint64_t)hi<<32)));
}
_mm_sfence();
}
}
// Adapted 2026-11-07 solely for generic n=16384/32768 and isolated benchmarking.
// Source: saffah_v41f1017_agg4, https://duck.ac/submission/141194 .
// Exact generic Winograd/leaf arithmetic, source notices and kernel assembly retained.
static __attribute__((noinline,noclone)) void expand_raw(int n,const uint64_t*c,bool*C){
const size_t count=(size_t)n*n/64;
const __m256i bits=_mm256_set1_epi64x(0x8040201008040201ULL),one=_mm256_set1_epi8(1);
const __m256i sh0=_mm256_setr_epi8(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3);
const __m256i sh1=_mm256_setr_epi8(4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7);
const __m256i sh2=_mm256_setr_epi8(8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11);
const __m256i sh3=_mm256_setr_epi8(12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15);
for(size_t z=0;z<count;z+=4){
const __m256i v=_mm256_load_si256((const __m256i*)(c+z));
const __m256i lo=_mm256_permute2x128_si256(v,v,0x00),hi=_mm256_permute2x128_si256(v,v,0x11);
bool* d=C+z*64;
_mm256_stream_si256((__m256i*)(d+0),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh0),bits),one));
_mm256_stream_si256((__m256i*)(d+32),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh1),bits),one));
_mm256_stream_si256((__m256i*)(d+64),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh2),bits),one));
_mm256_stream_si256((__m256i*)(d+96),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(lo,sh3),bits),one));
_mm256_stream_si256((__m256i*)(d+128),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh0),bits),one));
_mm256_stream_si256((__m256i*)(d+160),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh1),bits),one));
_mm256_stream_si256((__m256i*)(d+192),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh2),bits),one));
_mm256_stream_si256((__m256i*)(d+224),_mm256_min_epu8(_mm256_and_si256(_mm256_shuffle_epi8(hi,sh3),bits),one));
}
_mm_sfence();
}
static __attribute__((noinline,noclone)) void multiply_ready(int n,const uint64_t*a,const uint64_t*b,uint64_t*c,uint64_t*work,bool*C){
bit_winograd::recurse(n,a,n/64,b,n/64,c,n/64,work);
expand_raw(n,c,C);
}
__attribute__((noinline,noclone)) void matrix_multiply(int n,const bool*A,const bool*B,bool*C){
if(n<4096 || (n&(n-1))){fallback_product(n,A,B,C);return;}
const size_t count=(size_t)n*n/64;
uint64_t*c=nullptr;if(posix_memalign((void**)&c,4096,count*8))abort();
uint64_t*a=(uint64_t*)C,*b=a+count,*work=b+count;
bit_winograd::pack(n,A,a);bit_winograd::pack(n,B,b);
multiply_ready(n,a,b,c,work,C);
free(c);
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 724.684 ms | 290 MB + 360 KB | Accepted | Score: 100 | 显示更多 |