Skip to content

Commit

Permalink
Merge pull request #1773 from Geod24/unused
Browse files Browse the repository at this point in the history
Remove SCP's dependency on utils/types.h

Reviewed-by: MonsieurNicolas
  • Loading branch information
latobarita authored Sep 1, 2018
2 parents 7586a2d + 11ae081 commit 551d1bd
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 144 deletions.
2 changes: 2 additions & 0 deletions Builds/VisualStudio/stellar-core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ exit /b 0
<ClCompile Include="..\..\src\util\Math.cpp" />
<ClCompile Include="..\..\src\util\NtpClient.cpp" />
<ClCompile Include="..\..\src\util\NtpWork.cpp" />
<ClCompile Include="..\..\src\util\numeric.cpp" />
<ClCompile Include="..\..\src\util\SecretValue.cpp" />
<ClCompile Include="..\..\src\util\StatusManager.cpp" />
<ClCompile Include="..\..\src\util\StatusManagerTest.cpp" />
Expand Down Expand Up @@ -721,6 +722,7 @@ exit /b 0
<ClInclude Include="..\..\src\util\NonCopyable.h" />
<ClInclude Include="..\..\src\util\NtpClient.h" />
<ClInclude Include="..\..\src\util\NtpWork.h" />
<ClInclude Include="..\..\src\util\numeric.h" />
<ClInclude Include="..\..\src\util\optional.h" />
<ClInclude Include="..\..\src\util\SecretValue.h" />
<ClInclude Include="..\..\src\util\SociNoWarnings.h" />
Expand Down
9 changes: 9 additions & 0 deletions Builds/VisualStudio/stellar-core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,9 @@
<ClCompile Include="..\..\src\ledger\LiabilitiesTests.cpp">
<Filter>ledger\tests</Filter>
</ClCompile>
<ClCompile Include="..\..\src\util\numeric.cpp">
<Filter>util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\ledger\LedgerManager.h">
Expand Down Expand Up @@ -1502,6 +1505,12 @@
<ClInclude Include="..\..\src\invariant\LiabilitiesMatchOffers.h">
<Filter>invariant</Filter>
</ClInclude>
<ClInclude Include="..\..\src\util\LogSlowExecution.h">
<Filter>util</Filter>
</ClInclude>
<ClInclude Include="..\..\src\util\numeric.h">
<Filter>util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\AUTHORS" />
Expand Down
1 change: 0 additions & 1 deletion src/scp/BallotProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "util/GlobalChecks.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "util/types.h"
#include "xdrpp/marshal.h"
#include <functional>

Expand Down
2 changes: 1 addition & 1 deletion src/scp/LocalNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "scp/QuorumSetUtils.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "util/types.h"
#include "util/numeric.h"
#include "xdrpp/marshal.h"
#include <algorithm>
#include <unordered_set>
Expand Down
1 change: 0 additions & 1 deletion src/scp/NominationProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "util/GlobalChecks.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "util/types.h"
#include "xdrpp/marshal.h"
#include <functional>

Expand Down
1 change: 0 additions & 1 deletion src/scp/SCPTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "simulation/Simulation.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "util/types.h"
#include "xdrpp/marshal.h"
#include "xdrpp/printer.h"

Expand Down
1 change: 0 additions & 1 deletion src/scp/Slot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "util/GlobalChecks.h"
#include "util/Logging.h"
#include "util/XDROperators.h"
#include "util/types.h"
#include "xdrpp/marshal.h"
#include <ctime>
#include <functional>
Expand Down
126 changes: 126 additions & 0 deletions src/util/numeric.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2014 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "util/numeric.h"
#include <cassert>
#include <stdexcept>

namespace stellar
{
// calculates A*B/C when A*B overflows 64bits
bool
bigDivide(int64_t& result, int64_t A, int64_t B, int64_t C, Rounding rounding)
{
bool res;
assert((A >= 0) && (B >= 0) && (C > 0));
uint64_t r2;
res = bigDivide(r2, (uint64_t)A, (uint64_t)B, (uint64_t)C, rounding);
if (res)
{
res = r2 <= INT64_MAX;
result = r2;
}
return res;
}

bool
bigDivide(uint64_t& result, uint64_t A, uint64_t B, uint64_t C,
Rounding rounding)
{
// update when moving to (signed) int128
uint128_t a(A);
uint128_t b(B);
uint128_t c(C);
uint128_t x = rounding == ROUND_DOWN ? (a * b) / c : (a * b + c - 1) / c;

result = (uint64_t)x;

return (x <= UINT64_MAX);
}

int64_t
bigDivide(int64_t A, int64_t B, int64_t C, Rounding rounding)
{
int64_t res;
if (!bigDivide(res, A, B, C, rounding))
{
throw std::overflow_error("overflow while performing bigDivide");
}
return res;
}

bool
bigDivide(int64_t& result, uint128_t a, int64_t B, Rounding rounding)
{
assert(B > 0);

uint64_t r2;
bool res = bigDivide(r2, a, (uint64_t)B, rounding);
if (res)
{
res = r2 <= INT64_MAX;
result = r2;
}
return res;
}

bool
bigDivide(uint64_t& result, uint128_t a, uint64_t B, Rounding rounding)
{
assert(B != 0);

// update when moving to (signed) int128
uint128_t b(B);

// We need to handle the case a + b - 1 > UINT128_MAX separately if rounding
// up, since in this case a + b - 1 would overflow uint128_t. This is
// equivalent to a > UINT128_MAX - (b - 1), where b >= 1 by assumption.
// This is not a limitation of using uint128_t, since even if intermediate
// values could not overflow we would still find that
// (a + b - 1) / b
// > UINT128_MAX / b
// >= UINT128_MAX / UINT64_MAX
// = ((UINT64_MAX + 1) * (UINT64_MAX + 1) - 1) / UINT64_MAX
// = (UINT64_MAX * UINT64_MAX + 2 * UINT64_MAX) / UINT64_MAX
// = UINT64_MAX + 2
// which would have overflowed uint64_t anyway.
uint128_t const UINT128_MAX = ~uint128_0;
if ((rounding == ROUND_UP) && (a > UINT128_MAX - (b - 1)))
{
return false;
}

uint128_t x = rounding == ROUND_DOWN ? a / b : (a + b - 1) / b;

result = (uint64_t)x;

return (x <= UINT64_MAX);
}

int64_t
bigDivide(uint128_t a, int64_t B, Rounding rounding)
{
int64_t res;
if (!bigDivide(res, a, B, rounding))
{
throw std::overflow_error("overflow while performing bigDivide");
}
return res;
}

uint128_t
bigMultiply(uint64_t a, uint64_t b)
{
uint128_t A(a);
uint128_t B(b);
return A * B;
}

uint128_t
bigMultiply(int64_t a, int64_t b)
{
assert((a >= 0) && (b >= 0));
return bigMultiply((uint64_t)a, (uint64_t)b);
}
}
34 changes: 34 additions & 0 deletions src/util/numeric.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

// Copyright 2014 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "lib/util/uint128_t.h"
#include <cstdint>

namespace stellar
{
enum Rounding
{
ROUND_DOWN,
ROUND_UP
};

// calculates A*B/C when A*B overflows 64bits
int64_t bigDivide(int64_t A, int64_t B, int64_t C, Rounding rounding);
// no throw version, returns true if result is valid
bool bigDivide(int64_t& result, int64_t A, int64_t B, int64_t C,
Rounding rounding);

// no throw version, returns true if result is valid
bool bigDivide(uint64_t& result, uint64_t A, uint64_t B, uint64_t C,
Rounding rounding);

bool bigDivide(int64_t& result, uint128_t a, int64_t B, Rounding rounding);
bool bigDivide(uint64_t& result, uint128_t a, uint64_t B, Rounding rounding);
int64_t bigDivide(uint128_t a, int64_t B, Rounding rounding);

uint128_t bigMultiply(uint64_t a, uint64_t b);
uint128_t bigMultiply(int64_t a, int64_t b);
}
116 changes: 0 additions & 116 deletions src/util/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,122 +181,6 @@ addBalance(int64_t& balance, int64_t delta, int64_t maxBalance)
return true;
}

// calculates A*B/C when A*B overflows 64bits
bool
bigDivide(int64_t& result, int64_t A, int64_t B, int64_t C, Rounding rounding)
{
bool res;
assert((A >= 0) && (B >= 0) && (C > 0));
uint64_t r2;
res = bigDivide(r2, (uint64_t)A, (uint64_t)B, (uint64_t)C, rounding);
if (res)
{
res = r2 <= INT64_MAX;
result = r2;
}
return res;
}

bool
bigDivide(uint64_t& result, uint64_t A, uint64_t B, uint64_t C,
Rounding rounding)
{
// update when moving to (signed) int128
uint128_t a(A);
uint128_t b(B);
uint128_t c(C);
uint128_t x = rounding == ROUND_DOWN ? (a * b) / c : (a * b + c - 1) / c;

result = (uint64_t)x;

return (x <= UINT64_MAX);
}

int64_t
bigDivide(int64_t A, int64_t B, int64_t C, Rounding rounding)
{
int64_t res;
if (!bigDivide(res, A, B, C, rounding))
{
throw std::overflow_error("overflow while performing bigDivide");
}
return res;
}

bool
bigDivide(int64_t& result, uint128_t a, int64_t B, Rounding rounding)
{
assert(B > 0);

uint64_t r2;
bool res = bigDivide(r2, a, (uint64_t)B, rounding);
if (res)
{
res = r2 <= INT64_MAX;
result = r2;
}
return res;
}

bool
bigDivide(uint64_t& result, uint128_t a, uint64_t B, Rounding rounding)
{
assert(B != 0);

// update when moving to (signed) int128
uint128_t b(B);

// We need to handle the case a + b - 1 > UINT128_MAX separately if rounding
// up, since in this case a + b - 1 would overflow uint128_t. This is
// equivalent to a > UINT128_MAX - (b - 1), where b >= 1 by assumption.
// This is not a limitation of using uint128_t, since even if intermediate
// values could not overflow we would still find that
// (a + b - 1) / b
// > UINT128_MAX / b
// >= UINT128_MAX / UINT64_MAX
// = ((UINT64_MAX + 1) * (UINT64_MAX + 1) - 1) / UINT64_MAX
// = (UINT64_MAX * UINT64_MAX + 2 * UINT64_MAX) / UINT64_MAX
// = UINT64_MAX + 2
// which would have overflowed uint64_t anyway.
uint128_t const UINT128_MAX = ~uint128_0;
if ((rounding == ROUND_UP) && (a > UINT128_MAX - (b - 1)))
{
return false;
}

uint128_t x = rounding == ROUND_DOWN ? a / b : (a + b - 1) / b;

result = (uint64_t)x;

return (x <= UINT64_MAX);
}

int64_t
bigDivide(uint128_t a, int64_t B, Rounding rounding)
{
int64_t res;
if (!bigDivide(res, a, B, rounding))
{
throw std::overflow_error("overflow while performing bigDivide");
}
return res;
}

uint128_t
bigMultiply(uint64_t a, uint64_t b)
{
uint128_t A(a);
uint128_t B(b);
return A * B;
}

uint128_t
bigMultiply(int64_t a, int64_t b)
{
assert((a >= 0) && (b >= 0));
return bigMultiply((uint64_t)a, (uint64_t)b);
}

bool
iequals(std::string const& a, std::string const& b)
{
Expand Down
Loading

0 comments on commit 551d1bd

Please sign in to comment.