bsnes/higan/sfc/alt/ppu-balanced/render/addsub.cpp

22 lines
667 B
C++

//color addition / subtraction
//thanks go to blargg for the optimized algorithms
inline auto PPU::addsub(uint32 x, uint32 y, bool halve) -> uint16 {
if(!regs.color_mode) {
if(!halve) {
uint sum = x + y;
uint carry = (sum - ((x ^ y) & 0x0421)) & 0x8420;
return (sum - carry) | (carry - (carry >> 5));
} else {
return (x + y - ((x ^ y) & 0x0421)) >> 1;
}
} else {
uint diff = x - y + 0x8420;
uint borrow = (diff - ((x ^ y) & 0x8420)) & 0x8420;
if(!halve) {
return (diff - borrow) & (borrow - (borrow >> 5));
} else {
return (((diff - borrow) & (borrow - (borrow >> 5))) & 0x7bde) >> 1;
}
}
}