CommonFuncs: Generify rotation functions and move them to BitUtils.h

These are bit manipulation functions, so they belong within BitUtils.

This also gets rid of duplicated code and avoids relying on compiler
reserved names existing or not existing to determine whether or not we
define a set of functions.

Optimizers are smart enough in GCC and clang to transform the code to a
ROR or ROL instruction in the respective functions.
This commit is contained in:
Lioncash
2018-03-31 17:09:33 -04:00
parent 76e1a5b892
commit c3483a1823
7 changed files with 81 additions and 65 deletions

View File

@ -17,7 +17,7 @@
#include <windows.h>
#endif
#include "Common/CommonFuncs.h"
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
#include "Common/MsgHandler.h"
#include "Common/Swap.h"
@ -251,26 +251,26 @@ static void unscramble1(u32* addr, u32* val)
{
u32 tmp;
*val = _rotl(*val, 4);
*val = Common::RotateLeft(*val, 4);
tmp = ((*addr ^ *val) & 0xF0F0F0F0);
*addr ^= tmp;
*val = _rotr((*val ^ tmp), 0x14);
*val = Common::RotateRight((*val ^ tmp), 0x14);
tmp = ((*addr ^ *val) & 0xFFFF0000);
*addr ^= tmp;
*val = _rotr((*val ^ tmp), 0x12);
*val = Common::RotateRight((*val ^ tmp), 0x12);
tmp = ((*addr ^ *val) & 0x33333333);
*addr ^= tmp;
*val = _rotr((*val ^ tmp), 6);
*val = Common::RotateRight((*val ^ tmp), 6);
tmp = ((*addr ^ *val) & 0x00FF00FF);
*addr ^= tmp;
*val = _rotl((*val ^ tmp), 9);
*val = Common::RotateLeft((*val ^ tmp), 9);
tmp = ((*addr ^ *val) & 0xAAAAAAAA);
*addr = _rotl((*addr ^ tmp), 1);
*addr = Common::RotateLeft((*addr ^ tmp), 1);
*val ^= tmp;
}
@ -278,27 +278,27 @@ static void unscramble2(u32* addr, u32* val)
{
u32 tmp;
*val = _rotr(*val, 1);
*val = Common::RotateRight(*val, 1);
tmp = ((*addr ^ *val) & 0xAAAAAAAA);
*val ^= tmp;
*addr = _rotr((*addr ^ tmp), 9);
*addr = Common::RotateRight((*addr ^ tmp), 9);
tmp = ((*addr ^ *val) & 0x00FF00FF);
*val ^= tmp;
*addr = _rotl((*addr ^ tmp), 6);
*addr = Common::RotateLeft((*addr ^ tmp), 6);
tmp = ((*addr ^ *val) & 0x33333333);
*val ^= tmp;
*addr = _rotl((*addr ^ tmp), 0x12);
*addr = Common::RotateLeft((*addr ^ tmp), 0x12);
tmp = ((*addr ^ *val) & 0xFFFF0000);
*val ^= tmp;
*addr = _rotl((*addr ^ tmp), 0x14);
*addr = Common::RotateLeft((*addr ^ tmp), 0x14);
tmp = ((*addr ^ *val) & 0xF0F0F0F0);
*val ^= tmp;
*addr = _rotr((*addr ^ tmp), 4);
*addr = Common::RotateRight((*addr ^ tmp), 4);
}
static void decryptcode(const u32* seeds, u32* code)
@ -311,13 +311,13 @@ static void decryptcode(const u32* seeds, u32* code)
unscramble1(&addr, &val);
while (i < 32)
{
tmp = (_rotr(val, 4) ^ seeds[i++]);
tmp = (Common::RotateRight(val, 4) ^ seeds[i++]);
tmp2 = (val ^ seeds[i++]);
addr ^= (table6[tmp & 0x3F] ^ table4[(tmp >> 8) & 0x3F] ^ table2[(tmp >> 16) & 0x3F] ^
table0[(tmp >> 24) & 0x3F] ^ table7[tmp2 & 0x3F] ^ table5[(tmp2 >> 8) & 0x3F] ^
table3[(tmp2 >> 16) & 0x3F] ^ table1[(tmp2 >> 24) & 0x3F]);
tmp = (_rotr(addr, 4) ^ seeds[i++]);
tmp = (Common::RotateRight(addr, 4) ^ seeds[i++]);
tmp2 = (addr ^ seeds[i++]);
val ^= (table6[tmp & 0x3F] ^ table4[(tmp >> 8) & 0x3F] ^ table2[(tmp >> 16) & 0x3F] ^
table0[(tmp >> 24) & 0x3F] ^ table7[tmp2 & 0x3F] ^ table5[(tmp2 >> 8) & 0x3F] ^