|
| 1 | +//---------------------------------------------------------------------------// |
| 2 | +// Copyright (c) 2020 Madhur Chauhan |
| 3 | +// Copyright (c) 2020 John Maddock |
| 4 | +// Copyright (c) 2024 Andrey Nefedov <[email protected]> |
| 5 | +// |
| 6 | +// Distributed under the Boost Software License, Version 1.0 |
| 7 | +// See accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt |
| 9 | +//---------------------------------------------------------------------------// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <climits> |
| 14 | +#include <cstdint> |
| 15 | +#include <type_traits> |
| 16 | +#include "nil/crypto3/multiprecision/detail/force_inline.hpp" |
| 17 | +#include "nil/crypto3/multiprecision/detail/intel_intrinsics.hpp" |
| 18 | + |
| 19 | +namespace nil::crypto3::multiprecision::detail { |
| 20 | + template<typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> |
| 21 | + constexpr std::uint8_t addcarry_constexpr(std::uint8_t carry, T a, T b, T* p_result) { |
| 22 | + T r = a + b + carry; |
| 23 | + *p_result = r; |
| 24 | + return r < a || (r == a && carry); |
| 25 | + } |
| 26 | + |
| 27 | + template<typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> |
| 28 | + constexpr std::uint8_t subborrow_constexpr(std::uint8_t borrow, T a, T b, |
| 29 | + T* p_result) { |
| 30 | + T r = a - b - borrow; |
| 31 | + *p_result = r; |
| 32 | + return r > a || (r == a && borrow); |
| 33 | + } |
| 34 | +} // namespace nil::crypto3::multiprecision::detail |
| 35 | + |
| 36 | +#ifdef NIL_CO3_MP_HAS_INTRINSICS |
| 37 | + |
| 38 | +namespace nil::crypto3::multiprecision::detail { |
| 39 | + static_assert(std::is_same_v<std::uint8_t, unsigned char>); |
| 40 | + |
| 41 | + template<typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> |
| 42 | + NIL_CO3_MP_FORCEINLINE constexpr std::uint8_t addcarry(std::uint8_t carry, T a, T b, |
| 43 | + T* p_result) { |
| 44 | + if (!std::is_constant_evaluated()) { |
| 45 | + if constexpr (sizeof(T) * CHAR_BIT == 64) { |
| 46 | + return _addcarry_u64(carry, a, b, |
| 47 | + reinterpret_cast<unsigned long long*>(p_result)); |
| 48 | + } else if constexpr (sizeof(T) * CHAR_BIT == 32) { |
| 49 | + return _addcarry_u32(carry, a, b, |
| 50 | + reinterpret_cast<unsigned int*>(p_result)); |
| 51 | + } else { |
| 52 | + return addcarry_constexpr(carry, a, b, p_result); |
| 53 | + } |
| 54 | + } |
| 55 | + return addcarry_constexpr(carry, a, b, p_result); |
| 56 | + } |
| 57 | + |
| 58 | + template<typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> |
| 59 | + NIL_CO3_MP_FORCEINLINE constexpr std::uint8_t subborrow(std::uint8_t borrow, T a, T b, |
| 60 | + T* p_result) { |
| 61 | + if (!std::is_constant_evaluated()) { |
| 62 | + if constexpr (sizeof(T) * CHAR_BIT == 64) { |
| 63 | + return _subborrow_u64(borrow, a, b, |
| 64 | + reinterpret_cast<unsigned long long*>(p_result)); |
| 65 | + } else if constexpr (sizeof(T) * CHAR_BIT == 32) { |
| 66 | + return _subborrow_u32(borrow, a, b, |
| 67 | + reinterpret_cast<unsigned int*>(p_result)); |
| 68 | + } else { |
| 69 | + return subborrow_constexpr(borrow, a, b, p_result); |
| 70 | + } |
| 71 | + } |
| 72 | + return subborrow_constexpr(borrow, a, b, p_result); |
| 73 | + } |
| 74 | +} // namespace nil::crypto3::multiprecision::detail |
| 75 | + |
| 76 | +#else |
| 77 | + |
| 78 | +#ifndef NIL_CO3_MP_DISABLE_INTRINSICS |
| 79 | +#warning "x86 intrinsics are not available, addcarry and subborrow optimizations disabled" |
| 80 | +#endif |
| 81 | + |
| 82 | +namespace nil::crypto3::multiprecision::detail { |
| 83 | + template<typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> |
| 84 | + NIL_CO3_MP_FORCEINLINE constexpr std::uint8_t addcarry(std::uint8_t carry, T a, T b, |
| 85 | + T* p_result) { |
| 86 | + return addcarry_constexpr(carry, a, b, p_result); |
| 87 | + } |
| 88 | + |
| 89 | + template<typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> |
| 90 | + NIL_CO3_MP_FORCEINLINE constexpr std::uint8_t subborrow(std::uint8_t borrow, T a, T b, |
| 91 | + T* p_result) { |
| 92 | + return subborrow_constexpr(borrow, a, b, p_result); |
| 93 | + } |
| 94 | +} // namespace nil::crypto3::multiprecision::detail |
| 95 | + |
| 96 | +#endif |
0 commit comments