Use intrinsics, if possible

This commit is contained in:
Adrian Conlon 2024-08-05 20:32:34 +01:00
parent bb6bcb9e70
commit 90c887d169
2 changed files with 9 additions and 25 deletions

View File

@ -56,40 +56,24 @@ namespace EightBit
public static int DemoteNibble(byte value) => HighNibble(value); public static int DemoteNibble(byte value) => HighNibble(value);
public static int CountBits(int value) public static int CountBits(uint value)
{ {
var count = 0; return System.Numerics.BitOperations.PopCount(value);
while (value != 0)
{
++count;
value &= value - 1;
}
return count;
} }
public static int CountBits(byte value) => CountBits((int)value); public static bool EvenParity(uint value)
{
return CountBits(value) % 2 == 0;
}
public static bool EvenParity(int value) => CountBits(value) % 2 == 0; public static int FindFirstSet(uint value)
public static bool EvenParity(byte value) => EvenParity((int)value);
public static int FindFirstSet(int value)
{ {
if (value == 0) if (value == 0)
{ {
return 0; return 0;
} }
var test = 1; return System.Numerics.BitOperations.TrailingZeroCount(value) + 1;
var result = 1;
while ((value & test) == 0)
{
test <<= 1;
++result;
}
return result;
} }
} }
} }

View File

@ -66,7 +66,7 @@ namespace EightBit.GameBoy
var interruptEnable = this.Bus.Peek(IoRegisters.BASE + IoRegisters.IE); var interruptEnable = this.Bus.Peek(IoRegisters.BASE + IoRegisters.IE);
var interruptFlags = this.bus.IO.Peek(IoRegisters.IF); var interruptFlags = this.bus.IO.Peek(IoRegisters.IF);
var masked = interruptEnable & interruptFlags; var masked = (byte)(interruptEnable & interruptFlags);
if (masked != 0) if (masked != 0)
{ {
if (this.IME) if (this.IME)