79 lines
2.6 KiB
C++
79 lines
2.6 KiB
C++
/* XMRig
|
|
* Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
|
|
* Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* Additional permission under GNU GPL version 3 section 7
|
|
*
|
|
* If you modify this Program, or any covered work, by linking or combining
|
|
* it with OpenSSL (or a modified version of that library), containing parts
|
|
* covered by the terms of OpenSSL License and SSLeay License, the licensors
|
|
* of this Program grant you additional permission to convey the resulting work.
|
|
*/
|
|
|
|
#ifndef XMRIG_UMUL128_H
|
|
#define XMRIG_UMUL128_H
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
#ifdef XMRIG_64_BIT
|
|
# if defined(_MSC_VER)
|
|
# include <intrin.h>
|
|
# pragma intrinsic(_umul128)
|
|
# define xmrig_umul128 _umul128
|
|
# elif defined(__GNUC__)
|
|
static inline uint64_t xmrig_umul128(uint64_t a, uint64_t b, uint64_t* hi)
|
|
{
|
|
unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b;
|
|
*hi = r >> 64;
|
|
return (uint64_t) r;
|
|
}
|
|
# endif
|
|
#else
|
|
static inline uint64_t xmrig_umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) {
|
|
// multiplier = ab = a * 2^32 + b
|
|
// multiplicand = cd = c * 2^32 + d
|
|
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
|
|
uint64_t a = multiplier >> 32;
|
|
uint64_t b = multiplier & 0xFFFFFFFF;
|
|
uint64_t c = multiplicand >> 32;
|
|
uint64_t d = multiplicand & 0xFFFFFFFF;
|
|
|
|
//uint64_t ac = a * c;
|
|
uint64_t ad = a * d;
|
|
//uint64_t bc = b * c;
|
|
uint64_t bd = b * d;
|
|
|
|
uint64_t adbc = ad + (b * c);
|
|
uint64_t adbc_carry = adbc < ad ? 1 : 0;
|
|
|
|
// multiplier * multiplicand = product_hi * 2^64 + product_lo
|
|
uint64_t product_lo = bd + (adbc << 32);
|
|
uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
|
|
*product_hi = (a * c) + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;
|
|
|
|
return product_lo;
|
|
}
|
|
#endif
|
|
|
|
|
|
#if defined(XMRIG_LEGACY)
|
|
# define __umul128 xmrig_umul128
|
|
#endif
|
|
|
|
|
|
#endif // XMRIG_UMUL128_H
|