8#ifndef BOTAN_UTIL_MUL128_H_
9#define BOTAN_UTIL_MUL128_H_
11#include <botan/types.h>
13#if defined(BOTAN_BUILD_COMPILER_IS_MSVC) && defined(BOTAN_TARGET_CPU_HAS_NATIVE_64BIT)
16 #pragma intrinsic(__umulh)
18 #pragma intrinsic(_umul128)
24#if defined(__SIZEOF_INT128__) && defined(BOTAN_TARGET_CPU_HAS_NATIVE_64BIT)
25 #define BOTAN_TARGET_HAS_NATIVE_UINT128
29 typedef unsigned int uint128_t __attribute__((mode(TI)));
31 typedef unsigned __int128 uint128_t;
38inline void mul64x64_128(uint64_t a, uint64_t b, uint64_t* lo, uint64_t* hi)
40#if defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
42 const uint128_t r =
static_cast<uint128_t
>(a) * b;
43 *hi = (r >> 64) & 0xFFFFFFFFFFFFFFFF;
44 *lo = (r ) & 0xFFFFFFFFFFFFFFFF;
46#elif defined(BOTAN_BUILD_COMPILER_IS_MSVC) && defined(BOTAN_TARGET_CPU_HAS_NATIVE_64BIT)
51 *lo = _umul128(a, b, hi);
54#elif defined(BOTAN_USE_GCC_INLINE_ASM) && defined(BOTAN_TARGET_ARCH_IS_X86_64)
56 :
"=d" (*hi),
"=a" (*lo)
60#elif defined(BOTAN_USE_GCC_INLINE_ASM) && defined(BOTAN_TARGET_ARCH_IS_PPC64)
74 const size_t HWORD_BITS = 32;
75 const uint32_t HWORD_MASK = 0xFFFFFFFF;
77 const uint32_t a_hi = (a >> HWORD_BITS);
78 const uint32_t a_lo = (a & HWORD_MASK);
79 const uint32_t b_hi = (b >> HWORD_BITS);
80 const uint32_t b_lo = (b & HWORD_MASK);
82 uint64_t x0 =
static_cast<uint64_t
>(a_hi) * b_hi;
83 uint64_t x1 =
static_cast<uint64_t
>(a_lo) * b_hi;
84 uint64_t x2 =
static_cast<uint64_t
>(a_hi) * b_lo;
85 uint64_t x3 =
static_cast<uint64_t
>(a_lo) * b_lo;
88 x2 += x3 >> HWORD_BITS;
94 x0 +=
static_cast<uint64_t
>(
static_cast<bool>(x2 < x1)) << HWORD_BITS;
96 *hi = x0 + (x2 >> HWORD_BITS);
97 *lo = ((x2 & HWORD_MASK) << HWORD_BITS) + (x3 & HWORD_MASK);
void mul64x64_128(uint64_t a, uint64_t b, uint64_t *lo, uint64_t *hi)