Implement 128 bits Unsigned Integer Multiplication and Division.
This commit is contained in:
parent
5b7ec71fb7
commit
3ea48e8ebe
3 changed files with 50 additions and 0 deletions
30
src/common/uint128.h
Normal file
30
src/common/uint128.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <cstring>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Common {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <intrin.h>
|
||||
|
||||
#pragma intrinsic(_umul128)
|
||||
#endif
|
||||
|
||||
inline u128 umul128(u64 a, u64 b) {
|
||||
#ifdef _MSC_VER
|
||||
u128 result;
|
||||
result[0] = _umul128(a, b, &result[1]);
|
||||
#else
|
||||
unsigned __int128 tmp = a;
|
||||
tmp *= b;
|
||||
u128 result;
|
||||
std::memcpy(&result, &tmp, sizeof(u128));
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<u64, u64> udiv128(u128 dividend, u64 divisor);
|
||||
|
||||
} // namespace Common
|
Loading…
Add table
Add a link
Reference in a new issue