版本比较
Darling Hold My Hand
正文
增加 41 行删除 41 行
148 行未改动全部展开14915015115215315315415515615715815916016115916016116216316416316416516616716616716816917017113 行未改动18518618718818918919019119119219319419419519619619719819920020120220220320420520620720720820921021121221321421421521621721821821922022022122222222322422522622622722822922923023123223332 行未改动2662672682692702702712722732742742752762772785 行未改动28428528628728828828929029129229329429529629729829829930030030130230230330430530630730730830931031031131231331431431531631731731831931932032132232232332432532613 行未改动34034134234334434534434534634734734834934935035135235335435535635735735835936036136136236336436536536636736836937037137237337437337437537637737810 行未改动148 行未改动全部展开}
// 5 寄存器减法:引入了恢复标志位 (restore_op2),保证连等式运算的安全性void macro_sub_5(string out, string op1, string op2, string carry, string scratch, bool restore_op2 = false) { gen_minus_1(scratch, carry, out); gen_minus_1(scratch, carry, out); xor_r(op2, scratch); // op2 = ~op2 gen_const(1, carry, scratch, out); // carry = 1 xor_r(out, out); for(int j = 0; j < 32; ++j) { mov_r(scratch, op1); xor_r(scratch, op2); shl_r(scratch, 31); shr_r(scratch, 31); xor_r(scratch, carry); shr_r(out, 1); shl_r(scratch, 31); xor_r(out, scratch); xor_r(scratch, carry); shr_r(out, 1); shl_r(scratch, 31); xor_r(out, scratch);
mov_r(scratch, op1); xor_r(scratch, op2); shl_r(scratch, 31); shr_r(scratch, 31); mul_r(scratch, carry); mul_r(scratch, carry);
mov_r(carry, op1); mul_r(carry, op2); shl_r(carry, 31); shr_r(carry, 31); xor_r(carry, scratch); xor_r(carry, scratch);
mov_r(scratch, op1); shl_r(scratch, 31); shr_r(op1, 1); xor_r(op1, scratch); mov_r(scratch, op2); shl_r(scratch, 31); shr_r(op2, 1); xor_r(op2, scratch); } if (restore_op2) {13 行未改动
// FPU 级终极展开void compile_task_sin() { cout << "; ====== FPU Ultimate Precision: 65536 * sin(X) ======\n";
// 1. 保护与提取: 分离符号位与绝对值 |x| mov_r("a", "x"); shr_r("a", 31); mov_r("a", "x"); shr_r("a", 31); mov_r("b", "x"); shl_r("b", 1); shr_r("b", 1); mov_r("x", "b"); shl_r("a", 31); xor_r("x", "a"); // X = |x| 寄生 sign
// 2. T1 = 1 - x^2/20 mov_r("b", "x"); shl_r("b", 1); shr_r("b", 1); mov_r("b", "x"); shl_r("b", 1); shr_r("b", 1); approx_mul_var("c", "b", "b", "d"); // C = x^2 gen_const(3277, "d", "a", "y"); // D = 1/20 mul_r("c", "d"); shr_r("c", 16); // 全精度直接乘! C = x^2 / 20 gen_const(65536, "d", "a", "y"); // D = 1.0 macro_sub_5("a", "d", "c", "b", "y", false); // A = T1
// 3. T2 = x^2 * T1 mov_r("b", "x"); shl_r("b", 1); shr_r("b", 1); approx_mul_var("c", "b", "b", "d"); // 重新算出精确的 x^2 approx_mul_var("a", "a", "c", "d"); // A = x^2 * T1 = T2
// 4. T3 = 1 - T2/6 gen_const(10923, "d", "b", "c"); // D = 1/6 mul_r("a", "d"); shr_r("a", 16); // 全精度乘法! A = T2 / 6 mov_r("c", "a"); // C = T2 / 6 gen_const(65536, "d", "a", "b"); // D = 1.0 macro_sub_5("a", "d", "c", "b", "y", false); // A = 1 - T2/6 = T3
// 5. sin(|x|) = |x| * T3 mov_r("b", "x"); shl_r("b", 1); shr_r("b", 1); approx_mul_var("c", "a", "b", "d"); // C = sin(|x|) 算完了!
// 6. 神级符号恢复 (数学掩码选择网络) xor_r("d", "d"); xor_r("d", "d"); macro_sub_5("y", "d", "c", "a", "b", true); // Y = -sin(|x|). 关键:必须开启 true 以便保住 C 原值!
gen_const(1, "d", "a", "b"); // D = 1 mov_r("a", "x"); shr_r("a", 31); // A = sign (1 or 0) mov_r("b", "a"); xor_r("b", "d"); // B = !sign (0 or 1)
mul_r("y", "a"); // 选出负半轴结果 mul_r("c", "b"); // 选出正半轴结果
xor_r("a", "a"); xor_r("a", "y"); xor_r("a", "c"); // A = 最终输出! submit("a");}
32 行未改动 for(int j = 0; j < 32; ++j) { mov_r(scratch, "y"); shr_r(scratch, j); shl_r(scratch, 31); shr_r(scratch, 31); mul_r(scratch, dest); xor_r(dest, scratch); mov_r(scratch, "y"); shr_r(scratch, j); shl_r(scratch, 31); shr_r(scratch, 31); xor_r(dest, scratch);
mov_r(scratch, "x"); shr_r(scratch, j); shl_r(scratch, 31); shr_r(scratch, 31); mul_r(scratch, dest); xor_r(dest, scratch); mov_r(scratch, "x"); shr_r(scratch, j); shl_r(scratch, 31); shr_r(scratch, 31); xor_r(dest, scratch);
mov_r(scratch, "b"); shr_r(scratch, j); shl_r(scratch, 31); shr_r(scratch, 31); mul_r(scratch, dest); xor_r(dest, scratch); mov_r(scratch, "b"); shr_r(scratch, j); shl_r(scratch, 31); shr_r(scratch, 31); xor_r(dest, scratch); }5 行未改动 xor_r("a", "a"); for(int i = 0; i < 32; ++i) { // 1. 左移 A 和 X mov_r("d", "x"); shr_r("d", 31); shl_r("x", 1); shl_r("a", 1); xor_r("a", "d");
// 2. 仅扫描原始 Y 造 -1 取反 (此时 Y 绝对非零) xor_r("b", "b"); for(int j=0; j<32; ++j) { mov_r("c", "y"); shr_r("c", j); shl_r("c", 31); shr_r("c", 31); mul_r("c", "b"); xor_r("b", "c"); mov_r("c", "y"); shr_r("c", j); shl_r("c", 31); shr_r("c", 31); xor_r("b", "c"); } expand_minus_1("b", "c"); xor_r("y", "b"); // Y 变成了 ~Y
// 3. 初始 Carry 设为 1 (极其巧妙,直接复用刚才的 -1) mov_r("c", "b"); shl_r("c", 31); shr_r("c", 31); mov_r("c", "b"); shl_r("c", 31); shr_r("c", 31); xor_r("b", "b"); // B 作为 Sum 初始化为 0
// 4. 位串行加法器 (A + ~Y + 1) for(int j = 0; j < 32; ++j) { mov_r("d", "a"); xor_r("d", "y"); shl_r("d", 31); shr_r("d", 31); xor_r("d", "c"); shr_r("b", 1); shl_r("d", 31); xor_r("b", "d"); // Sum 入 B
mov_r("d", "a"); xor_r("d", "y"); shl_r("d", 31); shr_r("d", 31); mul_r("d", "c"); mov_r("c", "a"); mul_r("c", "y"); shl_r("c", 31); shr_r("c", 31); xor_r("c", "d"); // Carry 更新
mov_r("d", "a"); shl_r("d", 31); shr_r("a", 1); xor_r("a", "d"); mov_r("d", "y"); shl_r("d", 31); shr_r("y", 1); xor_r("y", "d"); }
// 5. 核心:C 此时就是精确无误的 Carry Out (商位)!直接写入 X xor_r("x", "c");
// 6. 恢复 Y (调用三体扫描,彻底杜绝全零热寂) gen_1_YXB("c", "d"); gen_1_YXB("c", "d"); expand_minus_1("c", "d"); xor_r("y", "c");
// 7. 有条件恢复 A (无需分支,纯数学选择) mov_r("c", "x"); shl_r("c", 31); shr_r("c", 31); // 提取商位 0 或 1 mov_r("d", "a"); xor_r("d", "b"); // D = A ^ B mul_r("d", "c"); // 若商位是1, D = A^B; 若商位是0, D = 013 行未改动 mul_r("c", "b"); xor_r("b", "c"); mov_r("c", "x"); shr_r("c", j); shl_r("c", 31); shr_r("c", 31); xor_r("b", "c"); } mov_r("y", "b"); shl_r("y", i); // 游标 T mov_r("d", "a"); xor_r("d", "y");
mov_r("d", "a"); xor_r("d", "y"); mov_r("b", "d"); mul_r("b", "d"); // B = T^2
expand_minus_1("b", "c"); // 将 B 取反 (~T^2)
xor_r("c", "c"); for(int j = 0; j < 32; ++j) { mov_r("d", "x"); shr_r("d", j); shl_r("d", 31); shr_r("d", 31); mul_r("d", "c"); xor_r("c", "d"); mov_r("d", "x"); shr_r("d", j); shl_r("d", 31); shr_r("d", 31); xor_r("c", "d"); } // C = Initial Carry = 1
for(int j = 0; j < 32; ++j) { mov_r("d", "x"); xor_r("d", "b"); shl_r("d", 31); shr_r("d", 31); mul_r("d", "c"); mov_r("c", "x"); mul_r("c", "b"); shl_r("c", 31); shr_r("c", 31); xor_r("c", "d"); // Next Carry
mov_r("d", "x"); shl_r("d", 31); shr_r("x", 1); xor_r("x", "d"); mov_r("d", "b"); shl_r("d", 31); shr_r("b", 1); xor_r("b", "d"); }
// C 此时就是最终判断位 (1 表示够减) xor_r("b", "b"); for(int j = 0; j < 32; ++j) { mov_r("d", "x"); shr_r("d", j); shl_r("d", 31); shr_r("d", 31); mul_r("d", "b"); xor_r("b", "d"); mov_r("d", "x"); shr_r("d", j); shl_r("d", 31); shr_r("d", 31); xor_r("b", "d"); } mov_r("y", "b"); shl_r("y", i); mov_r("y", "b"); shl_r("y", i);
mov_r("d", "y"); mul_r("d", "c"); xor_r("a", "d"); } submit("a");10 行未改动