add vm_borrowchain_loop (8-bit ripple-borrow subtractor); vm_sample_count 402 -> 403

Result: {"status":"keep","vm_sample_count":403,"total_semantic_cases":4050,"manifest_samples":435}
This commit is contained in:
yusufcanislek
2026-04-26 14:47:45 +03:00
parent c4dfd494e6
commit 8a4e7bd4aa
2 changed files with 111 additions and 0 deletions
@@ -861,6 +861,27 @@
{ "inputs": { "RCX": 128 }, "expected": 128, "label": "a=0x80,b=0" }
]
},
{
"name": "vm_borrowchain_loop",
"symbol": "vm_borrowchain_loop_target",
"patterns": [
"xor",
"and",
"or"
],
"semantic": [
{ "inputs": { "RCX": 0 }, "expected": 0, "label": "a=0 b=0: 0-0" },
{ "inputs": { "RCX": 1 }, "expected": 1, "label": "a=1 b=0: 1-0=1" },
{ "inputs": { "RCX": 256 }, "expected": 511, "label": "a=0 b=1: -1 = 0xFF, borrow set" },
{ "inputs": { "RCX": 65535 }, "expected": 0, "label": "a=0xFF b=0xFF: 0" },
{ "inputs": { "RCX": 65280 }, "expected": 257, "label": "a=0 b=0xFF: -0xFF mod 256 = 1, borrow" },
{ "inputs": { "RCX": 255 }, "expected": 255, "label": "a=0xFF b=0: 0xFF" },
{ "inputs": { "RCX": 43605 }, "expected": 427, "label": "a=0x55 b=0xAA: 0xAB + borrow" },
{ "inputs": { "RCX": 21930 }, "expected": 85, "label": "a=0xAA b=0x55: 0x55" },
{ "inputs": { "RCX": 33023 }, "expected": 127, "label": "a=0xFF b=0x80: 0x7F" },
{ "inputs": { "RCX": 32640 }, "expected": 1, "label": "a=0x80 b=0x7F: 0x01" }
]
},
{
"name": "vm_prefix_sum_loop",
"symbol": "vm_prefix_sum_loop_target",
@@ -0,0 +1,90 @@
/* PC-state VM running an 8-bit ripple-borrow subtractor bit-by-bit.
* Lift target: vm_borrowchain_loop_target.
* Goal: complete the carrychain pair with a sequential subtractor where
* each iteration's borrow depends on the previous bit's borrow. Inputs
* a = x & 0xFF and b = (x >> 8) & 0xFF, output is (a-b) mod 2^8 packed
* as low byte | (final_borrow << 8). Distinct from vm_carrychain_loop
* (which adds): the borrow propagation chain has the opposite signal
* pattern.
*
* bs = ba XOR bb XOR borrow // sum bit of full subtractor
* new_borrow = (NOT ba AND bb) OR (borrow AND NOT(ba XOR bb))
*/
#include <stdio.h>
enum BcVmPc {
BC2_LOAD = 0,
BC2_INIT = 1,
BC2_CHECK = 2,
BC2_BODY_BA = 3,
BC2_BODY_BB = 4,
BC2_BODY_SUM = 5,
BC2_BODY_NB = 6,
BC2_BODY_OR = 7,
BC2_BODY_INC = 8,
BC2_PACK = 9,
BC2_HALT = 10,
};
__declspec(noinline)
int vm_borrowchain_loop_target(int x) {
int a = 0;
int b = 0;
int i = 0;
int borrow = 0;
int result = 0;
int ba = 0;
int bb = 0;
int bs = 0;
int nb = 0;
int xor_ab = 0;
int pc = BC2_LOAD;
while (1) {
if (pc == BC2_LOAD) {
a = x & 0xFF;
b = (x >> 8) & 0xFF;
i = 0;
borrow = 0;
result = 0;
pc = BC2_INIT;
} else if (pc == BC2_INIT) {
pc = BC2_CHECK;
} else if (pc == BC2_CHECK) {
pc = (i < 8) ? BC2_BODY_BA : BC2_PACK;
} else if (pc == BC2_BODY_BA) {
ba = (a >> i) & 1;
pc = BC2_BODY_BB;
} else if (pc == BC2_BODY_BB) {
bb = (b >> i) & 1;
pc = BC2_BODY_SUM;
} else if (pc == BC2_BODY_SUM) {
xor_ab = ba ^ bb;
bs = xor_ab ^ borrow;
pc = BC2_BODY_NB;
} else if (pc == BC2_BODY_NB) {
nb = ((~ba) & bb & 1) | (borrow & ((~xor_ab) & 1));
pc = BC2_BODY_OR;
} else if (pc == BC2_BODY_OR) {
result = result | (bs << i);
borrow = nb;
pc = BC2_BODY_INC;
} else if (pc == BC2_BODY_INC) {
i = i + 1;
pc = BC2_CHECK;
} else if (pc == BC2_PACK) {
result = result | (borrow << 8);
pc = BC2_HALT;
} else if (pc == BC2_HALT) {
return result;
} else {
return -1;
}
}
}
int main(void) {
printf("vm_borrowchain_loop(0x0001)=%d vm_borrowchain_loop(0xAA55)=%d\n",
vm_borrowchain_loop_target(0x0001), vm_borrowchain_loop_target(0xAA55));
return 0;
}