java - Calculating Carry Flag -
i writing x86 interpreter in java , have python script tests implementations of x86 instructions against real counterparts using nasm. according test flags set correctly, besides carry flag. interesting part is:
long result; switch (op) { case add: result = math.abs(x) + math.abs(y); if (result > u_max) registers.carry_flag = true; else registers.carry_flag = false; break;
where u_max 4294967295l (all 32 bits set).
answers found didn't realize carry , overflow 2 different things. so, how can implement carry flag correctly in java?
all absolute value business unnecessary , frankly confusing , has edge-case math.abs(integer.min_value)
negative (not weird when down it, doesn't code expects it), can calculate carry in simpler ways .
for example using old "result unsigned less 1 operand". result of course x + y
(as int
), signedness irrelevant addition. java 8, can use integer.compareunsigned, otherwise can use identity:
x <u y = (x ^ integer.min_value) <s (y ^ integer.min_value)
where <u
, <s
unsigned less , signed less than, respectively.
you calculate (x & 0xffffffffl) + (y & 0xffffffffl)
, use both result (cast int
) , carry flag (the 33th bit).
Comments
Post a Comment