Generalize ConvertBits
This commit is contained in:
committed by
Jack Grigg
parent
6b759fb092
commit
ac70f76c5d
@@ -16,8 +16,8 @@ BOOST_AUTO_TEST_CASE(convertbits_deterministic)
|
|||||||
std::vector<unsigned char> input(32, i);
|
std::vector<unsigned char> input(32, i);
|
||||||
std::vector<unsigned char> data;
|
std::vector<unsigned char> data;
|
||||||
std::vector<unsigned char> output;
|
std::vector<unsigned char> output;
|
||||||
ConvertBits<8, 5, true>(data, input.begin(), input.end());
|
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, input.begin(), input.end());
|
||||||
ConvertBits<5, 8, false>(output, data.begin(), data.end());
|
ConvertBits<5, 8, false>([&](unsigned char c) { output.push_back(c); }, data.begin(), data.end());
|
||||||
BOOST_CHECK_EQUAL(data.size(), 52);
|
BOOST_CHECK_EQUAL(data.size(), 52);
|
||||||
BOOST_CHECK_EQUAL(output.size(), 32);
|
BOOST_CHECK_EQUAL(output.size(), 32);
|
||||||
BOOST_CHECK(input == output);
|
BOOST_CHECK(input == output);
|
||||||
@@ -27,8 +27,8 @@ BOOST_AUTO_TEST_CASE(convertbits_deterministic)
|
|||||||
std::vector<unsigned char> input(43, i);
|
std::vector<unsigned char> input(43, i);
|
||||||
std::vector<unsigned char> data;
|
std::vector<unsigned char> data;
|
||||||
std::vector<unsigned char> output;
|
std::vector<unsigned char> output;
|
||||||
ConvertBits<8, 5, true>(data, input.begin(), input.end());
|
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, input.begin(), input.end());
|
||||||
ConvertBits<5, 8, false>(output, data.begin(), data.end());
|
ConvertBits<5, 8, false>([&](unsigned char c) { output.push_back(c); }, data.begin(), data.end());
|
||||||
BOOST_CHECK_EQUAL(data.size(), 69);
|
BOOST_CHECK_EQUAL(data.size(), 69);
|
||||||
BOOST_CHECK_EQUAL(output.size(), 43);
|
BOOST_CHECK_EQUAL(output.size(), 43);
|
||||||
BOOST_CHECK(input == output);
|
BOOST_CHECK(input == output);
|
||||||
@@ -41,8 +41,8 @@ BOOST_AUTO_TEST_CASE(convertbits_random)
|
|||||||
auto input = libzcash::random_uint256();
|
auto input = libzcash::random_uint256();
|
||||||
std::vector<unsigned char> data;
|
std::vector<unsigned char> data;
|
||||||
std::vector<unsigned char> output;
|
std::vector<unsigned char> output;
|
||||||
ConvertBits<8, 5, true>(data, input.begin(), input.end());
|
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, input.begin(), input.end());
|
||||||
ConvertBits<5, 8, false>(output, data.begin(), data.end());
|
ConvertBits<5, 8, false>([&](unsigned char c) { output.push_back(c); }, data.begin(), data.end());
|
||||||
BOOST_CHECK_EQUAL(data.size(), 52);
|
BOOST_CHECK_EQUAL(data.size(), 52);
|
||||||
BOOST_CHECK_EQUAL(output.size(), 32);
|
BOOST_CHECK_EQUAL(output.size(), 32);
|
||||||
BOOST_CHECK(input == uint256(output));
|
BOOST_CHECK(input == uint256(output));
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
|
|||||||
|
|
||||||
/** Convert from one power-of-2 number base to another. */
|
/** Convert from one power-of-2 number base to another. */
|
||||||
template<int frombits, int tobits, bool pad, typename O, typename I>
|
template<int frombits, int tobits, bool pad, typename O, typename I>
|
||||||
bool ConvertBits(O& out, I it, I end) {
|
bool ConvertBits(const O& outfn, I it, I end) {
|
||||||
size_t acc = 0;
|
size_t acc = 0;
|
||||||
size_t bits = 0;
|
size_t bits = 0;
|
||||||
constexpr size_t maxv = (1 << tobits) - 1;
|
constexpr size_t maxv = (1 << tobits) - 1;
|
||||||
@@ -145,12 +145,12 @@ bool ConvertBits(O& out, I it, I end) {
|
|||||||
bits += frombits;
|
bits += frombits;
|
||||||
while (bits >= tobits) {
|
while (bits >= tobits) {
|
||||||
bits -= tobits;
|
bits -= tobits;
|
||||||
out.push_back((acc >> bits) & maxv);
|
outfn((acc >> bits) & maxv);
|
||||||
}
|
}
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
if (pad) {
|
if (pad) {
|
||||||
if (bits) out.push_back((acc << (tobits - bits)) & maxv);
|
if (bits) outfn((acc << (tobits - bits)) & maxv);
|
||||||
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
|
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user