dynarmic/src/common/u128.cpp

96 lines
2.2 KiB
C++
Raw Normal View History

/* This file is part of the dynarmic project.
* Copyright (c) 2018 MerryMage
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2 or any later version.
*/
#include <array>
#include "common/common_types.h"
#include "common/u128.h"
namespace Dynarmic {
2018-07-23 15:57:29 +01:00
u128 Multiply64To128(u64 a, u64 b) {
const u32 a0 = static_cast<u32>(a);
const u32 b0 = static_cast<u32>(b);
const u32 a1 = static_cast<u32>(a >> 32);
const u32 b1 = static_cast<u32>(b >> 32);
// result = (c2 << 64) + (c1 << 32) + c0
// c2 = a1 * b1
// c1 = a1 * b0 + a0 * b1
// c0 = a0 * b0
// noting that these operations may overflow
const u64 c0 = static_cast<u64>(a0) * b0;
const u64 c1_0 = static_cast<u64>(a1) * b0;
const u64 c1_1 = static_cast<u64>(a0) * b1;
const u64 c2 = static_cast<u64>(a1) * b1;
const u64 c1 = c1_0 + c1_1;
const u64 c1_overflow = c1 < c1_0;
const u64 lower = c0 + (c1 << 32);
const u64 lower_overflow = lower < c0;
const u64 upper = lower_overflow + (c1 >> 32) + (c1_overflow << 32) + c2;
u128 result;
result.lower = lower;
result.upper = upper;
return result;
}
u128 operator<<(u128 operand, int amount) {
if (amount < 0) {
return operand >> -amount;
}
if (amount == 0) {
return operand;
}
if (amount < 64) {
u128 result;
result.lower = (operand.lower << amount);
result.upper = (operand.upper << amount) | (operand.lower >> (64 - amount));
return result;
}
if (amount < 128) {
u128 result;
result.upper = operand.lower << (amount - 64);
return result;
}
return {};
}
u128 operator>>(u128 operand, int amount) {
if (amount < 0) {
return operand << -amount;
}
if (amount == 0) {
return operand;
}
if (amount < 64) {
u128 result;
result.lower = (operand.lower >> amount) | (operand.upper << (64 - amount));
result.upper = (operand.upper >> amount);
return result;
}
if (amount < 128) {
u128 result;
result.lower = operand.upper >> (amount - 64);
return result;
}
return {};
}
} // namespace Dynarmic