diff --git a/bit.asm b/bit.asm index f015315..3cba7a6 100644 --- a/bit.asm +++ b/bit.asm @@ -70,3 +70,25 @@ pub popc: add r4, r4, r2 and r1, r4, 0xFF ; mask out end result in lowest byte jmp r13 + +; Calculates the parity of the value provided in the r1 register +; i.e. 0 means even bits set, 1 means odd bits set +; Based on Stanford's BitHacks +; Clobbers r2 +pub parity: + ; v ^= v >> 16; + lsr r2, r1, 16 + xor r1, r1, r2 + ; v ^= v >> 8; + lsr r2, r1, 8 + xor r1, r1, r2 + ; v ^= v >> 4; + lsr r2, r1, 4 + xor r1, r1, r2 + ; v &= 0xf; + and r1, r1, 0x0F + ; return (0x6996 >> v) & 1; + mov r2, 0x6996 + lsr r1, r2, r1 + and r1, r1, 0x01 + jmp r13 \ No newline at end of file diff --git a/imath.asm b/imath.asm index 3262321..90056f0 100644 --- a/imath.asm +++ b/imath.asm @@ -13,4 +13,50 @@ pub mul_low: jge mull_loop mov r1, r3 + jmp r13 + +; Calculates the absolute value of the value provided in the r1 register +; Based on Stanford's BitHacks +; Clobbers r2 +pub abs: ; SHOULD BE INLINED + ; mask = v >> 31 + asr r2, r1, 31 + ; v + mask + add r1, r1, r2 + ; return (v + mask) ^ mask + xor r1, r1, r2 + jmp r13 + +; Calculates the minimum value of the two values provided in the r1 and r2 registers +; Based on Stanford's BitHacks +; Clobbers flags +pub min: ; SHOULD BE INLINED + ; x < y + cmp r1, r2 + lsr flags, flags, 2 + ; -(x < y) + neg flags, flags + ; x ^ y + xor r1, r1, r2 + ; (x ^ y) & -(x < y) + and r1, r1, flags + ; return y ^ ((x ^ y) & -(x < y)) + xor r1, r2, r1 + jmp r13 + +; Calculates the maximum value of the two values provided in the r1 and r2 registers +; Based on Stanford's BitHacks +; Clobbers r2 and flags +pub max: ; SHOULD BE INLINED + ; x < y + cmp r1, r2 + lsr flags, flags, 2 + ; -(x < y) + neg flags, flags + ; x ^ y + xor r2, r1, r2 + ; (x ^ y) & -(x < y) + and r2, r2, flags + ; return x ^ ((x ^ y) & -(x < y)) + xor r1, r1, r2 jmp r13 \ No newline at end of file