Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/test ceilinged_multiply(), organize math/multiplication sources. #1457

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ test_libbitcoin_system_test_SOURCES = \
test/math/division.cpp \
test/math/limits.cpp \
test/math/logarithm.cpp \
test/math/multiplication.cpp \
test/math/overflow.cpp \
test/math/power.cpp \
test/math/rotate.cpp \
Expand Down Expand Up @@ -625,6 +626,7 @@ include_bitcoin_system_impl_math_HEADERS = \
include/bitcoin/system/impl/math/division.ipp \
include/bitcoin/system/impl/math/limits.ipp \
include/bitcoin/system/impl/math/logarithm.ipp \
include/bitcoin/system/impl/math/multiplication.ipp \
include/bitcoin/system/impl/math/overflow.ipp \
include/bitcoin/system/impl/math/power.ipp \
include/bitcoin/system/impl/math/rotate.ipp \
Expand Down Expand Up @@ -712,6 +714,7 @@ include_bitcoin_system_math_HEADERS = \
include/bitcoin/system/math/limits.hpp \
include/bitcoin/system/math/logarithm.hpp \
include/bitcoin/system/math/math.hpp \
include/bitcoin/system/math/multiplication.hpp \
include/bitcoin/system/math/overflow.hpp \
include/bitcoin/system/math/power.hpp \
include/bitcoin/system/math/rotate.hpp \
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ if (with-tests)
"../../test/math/division.cpp"
"../../test/math/limits.cpp"
"../../test/math/logarithm.cpp"
"../../test/math/multiplication.cpp"
"../../test/math/overflow.cpp"
"../../test/math/power.cpp"
"../../test/math/rotate.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
<ClCompile Include="..\..\..\..\test\math\division.cpp" />
<ClCompile Include="..\..\..\..\test\math\limits.cpp" />
<ClCompile Include="..\..\..\..\test\math\logarithm.cpp" />
<ClCompile Include="..\..\..\..\test\math\multiplication.cpp" />
<ClCompile Include="..\..\..\..\test\math\overflow.cpp" />
<ClCompile Include="..\..\..\..\test\math\power.cpp" />
<ClCompile Include="..\..\..\..\test\math\rotate.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@
<ClCompile Include="..\..\..\..\test\math\logarithm.cpp">
<Filter>src\math</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\math\multiplication.cpp">
<Filter>src\math</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\math\overflow.cpp">
<Filter>src\math</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\limits.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\logarithm.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\math.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\multiplication.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\overflow.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\power.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\rotate.hpp" />
Expand Down Expand Up @@ -550,6 +551,7 @@
<None Include="..\..\..\..\include\bitcoin\system\impl\math\division.ipp" />
<None Include="..\..\..\..\include\bitcoin\system\impl\math\limits.ipp" />
<None Include="..\..\..\..\include\bitcoin\system\impl\math\logarithm.ipp" />
<None Include="..\..\..\..\include\bitcoin\system\impl\math\multiplication.ipp" />
<None Include="..\..\..\..\include\bitcoin\system\impl\math\overflow.ipp" />
<None Include="..\..\..\..\include\bitcoin\system\impl\math\power.ipp" />
<None Include="..\..\..\..\include\bitcoin\system\impl\math\rotate.ipp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\math.hpp">
<Filter>include\bitcoin\system\math</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\multiplication.hpp">
<Filter>include\bitcoin\system\math</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\system\math\overflow.hpp">
<Filter>include\bitcoin\system\math</Filter>
</ClInclude>
Expand Down Expand Up @@ -1525,6 +1528,9 @@
<None Include="..\..\..\..\include\bitcoin\system\impl\math\logarithm.ipp">
<Filter>include\bitcoin\system\impl\math</Filter>
</None>
<None Include="..\..\..\..\include\bitcoin\system\impl\math\multiplication.ipp">
<Filter>include\bitcoin\system\impl\math</Filter>
</None>
<None Include="..\..\..\..\include\bitcoin\system\impl\math\overflow.ipp">
<Filter>include\bitcoin\system\impl\math</Filter>
</None>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
#include <bitcoin/system/math/limits.hpp>
#include <bitcoin/system/math/logarithm.hpp>
#include <bitcoin/system/math/math.hpp>
#include <bitcoin/system/math/multiplication.hpp>
#include <bitcoin/system/math/overflow.hpp>
#include <bitcoin/system/math/power.hpp>
#include <bitcoin/system/math/rotate.hpp>
Expand Down
54 changes: 54 additions & 0 deletions include/bitcoin/system/impl/math/multiplication.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_SYSTEM_MATH_MULTIPLICATION_IPP
#define LIBBITCOIN_SYSTEM_MATH_MULTIPLICATION_IPP

#include <bitcoin/system/define.hpp>
#include <bitcoin/system/math/cast.hpp>
#include <bitcoin/system/math/limits.hpp>
#include <bitcoin/system/math/overflow.hpp>

namespace libbitcoin {
namespace system {

template <typename Integer, if_integer<Integer>>
constexpr bool is_multiple(Integer product, Integer value) NOEXCEPT
{
return is_nonzero(value) ? is_zero(product % value) : is_zero(product);
}

template <typename Integer, if_integer<Integer>>
constexpr bool is_product(Integer product, Integer left,
Integer right) NOEXCEPT
{
return (is_zero(left) || is_zero(right)) ? is_zero(product) :
is_zero(product % left) && ((product / left) == right);
}

template <typename Unsigned, if_unsigned_integer<Unsigned>>
constexpr Unsigned ceilinged_multiply(Unsigned left, Unsigned right) NOEXCEPT
{
return is_multiply_overflow(left, right) ? maximum<Unsigned> :
depromote<Unsigned>(left * right);
}

} // namespace system
} // namespace libbitcoin

#endif
14 changes: 0 additions & 14 deletions include/bitcoin/system/impl/math/overflow.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,6 @@ constexpr bool is_divide_overflow(Dividend, Divisor divisor) NOEXCEPT
return is_zero(divisor);
}

template <typename Integer, if_integer<Integer>>
constexpr bool is_multiple(Integer product, Integer value) NOEXCEPT
{
return is_nonzero(value) ? is_zero(product % value) : is_zero(product);
}

template <typename Integer, if_integer<Integer>>
constexpr bool is_product(Integer product, Integer left,
Integer right) NOEXCEPT
{
return (is_zero(left) || is_zero(right)) ? is_zero(product) :
is_zero(product % left) && ((product / left) == right);
}

// power/log
// ----------------------------------------------------------------------------
// TODO: type-constrain Exponent by Base/Value to preclude value overflow.
Expand Down
32 changes: 17 additions & 15 deletions include/bitcoin/system/math/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,32 @@
#include <bitcoin/system/math/functional.hpp>
#include <bitcoin/system/math/limits.hpp>
#include <bitcoin/system/math/logarithm.hpp>
#include <bitcoin/system/math/multiplication.hpp>
#include <bitcoin/system/math/overflow.hpp>
#include <bitcoin/system/math/power.hpp>
#include <bitcoin/system/math/rotate.hpp>
#include <bitcoin/system/math/sign.hpp>

// Inclusion dependencies:
// cast ->
// sign -> cast
// rotate -> cast
// byteswap -> cast
// functional -> cast, rotate
// division -> sign
// overflow -> sign, cast
// bits -> sign, cast, logarithm (for bit_width(clog2))
// bytes -> sign, cast, logarithm (for byte_width(clog256))
// limits -> sign, cast, [power] (for sized domain limits)
// power -> sign, cast, overflow, bits (for shift optimization)
// logarithm -> sign, cast, overflow, division (for ceiling/floor opts)
// addition -> sign, cast, overflow, limits (for ceiling/floor opts)
// multiply -> sign, cast, overflow, limits (for ceiling/floor opts)
// cast ->
// sign -> cast
// rotate -> cast
// byteswap -> cast
// functional -> cast, rotate
// division -> sign
// overflow -> sign, cast
// bits -> sign, cast, logarithm (for bit_width(clog2))
// bytes -> sign, cast, logarithm (for byte_width(clog256))
// limits -> sign, cast, [power] (for sized domain limits)
// power -> sign, cast, overflow, bits (for shift optimization)
// logarithm -> sign, cast, overflow, division (for ceiling/floor opts)
// addition -> sign, cast, overflow, limits (for ceiling/floor opts)
// multiplication -> cast, overflow, limits (for ceiling opts)

// sign/cast/overflow should not call any other math libs and are safe from
// all others. bits/bytes should otherwise call only log. Otherwise only:
// addition/multiply->limits, limits->power, power->bits, logarithm->division.
// addition/multiplication->limits, limits->power, power->bits,
// logarithm->division.

// TODO: create type constraints for unitx/unitx_t<> optimization.
// TODO: verify non-integral ops do not unnecessarily copy unitx/unitx_t<>.
Expand Down
48 changes: 48 additions & 0 deletions include/bitcoin/system/math/multiplication.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_SYSTEM_MATH_MULTIPLICATION_HPP
#define LIBBITCOIN_SYSTEM_MATH_MULTIPLICATION_HPP

#include <bitcoin/system/define.hpp>

namespace libbitcoin {
namespace system {

/// Safely determine whether product is a whole mutiple of value.
template <typename Integer,
if_integer<Integer> = true>
constexpr bool is_multiple(Integer product, Integer value) NOEXCEPT;

/// Safely determine whether product is left * right.
template <typename Integer,
if_integer<Integer> = true>
constexpr bool is_product(Integer product, Integer left,
Integer right) NOEXCEPT;

/// Integer maximum if would overflow, otherwise the product.
template <typename Unsigned,
if_unsigned_integer<Unsigned> = true>
constexpr Unsigned ceilinged_multiply(Unsigned left, Unsigned right) NOEXCEPT;

} // namespace system
} // namespace libbitcoin

#include <bitcoin/system/impl/math/multiplication.ipp>

#endif
13 changes: 1 addition & 12 deletions include/bitcoin/system/math/overflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ constexpr bool is_subtract_overflow(Unsigned left, Unsigned right) NOEXCEPT;

/// multiply/divide.
/// ---------------------------------------------------------------------------
/// Multiply is limited to integrals, as it requires domain bounds.
/// Multiply is limited to integrals, as it requires domain bounds.

/// True if multiply would overflow Integer domain.
template <typename Unsigned,
Expand All @@ -66,17 +66,6 @@ template <typename Dividend, typename Divisor,
if_integer<Dividend> = true, if_integer<Divisor> = true>
constexpr bool is_divide_overflow(Dividend dividend, Divisor divisor) NOEXCEPT;

/// Safely determine whether product is a whole mutiple of value.
template <typename Integer,
if_integer<Integer> = true>
constexpr bool is_multiple(Integer product, Integer value) NOEXCEPT;

/// Safely determine whether product is left * right.
template <typename Integer,
if_integer<Integer> = true>
constexpr bool is_product(Integer product, Integer left,
Integer right) NOEXCEPT;

/// power/log.
/// ---------------------------------------------------------------------------
/// These are not limited to integral as they do not require domain bounds.
Expand Down
Loading
Loading