• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 5894307386

17 Aug 2023 06:00PM UTC coverage: 80.943% (-1.9%) from 82.857%
5894307386

push

github

web-flow
Merge pull request #1368 from pmienk/master

Update copyright date range, regenerate artifacts.

9752 of 12048 relevant lines covered (80.94%)

4744998.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/include/bitcoin/system/impl/math/bytes.ipp
1
/**
2
 * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#ifndef LIBBITCOIN_SYSTEM_MATH_BYTES_IPP
20
#define LIBBITCOIN_SYSTEM_MATH_BYTES_IPP
21

22
#include <bitcoin/system/define.hpp>
23
#include <bitcoin/system/math/cast.hpp>
24
#include <bitcoin/system/math/logarithm.hpp>
25
#include <bitcoin/system/math/sign.hpp>
26

27
namespace libbitcoin {
28
namespace system {
29

30
// Byte width.
31
// ----------------------------------------------------------------------------
32
// See also std::bit_width (C++20).
33

34
// Called by chain::compact (for validation).
35
template <typename Integral, if_unsigned_integer<Integral>>
36
constexpr size_t byte_width(Integral value) NOEXCEPT
338✔
37
{
38
    // (zero-based position of msb) + 7 / 8.
39
    // (bit_width(value) + 7) / 8
40
    // (ceilinged_log2(value) + 7) / 8
41
    return ceilinged_log256(value);
338✔
42
}
43

44
// Called by machine::number (for little-endian chunk sizing).
45
template <typename Integral, if_signed_integer<Integral>>
46
constexpr size_t byte_width(Integral value) NOEXCEPT
47
{
48
    return is_negative(value) ? sizeof(Integral) :
49
        byte_width(to_unsigned(value));
50
}
51

52
// Bit count to byte count.
53
// ----------------------------------------------------------------------------
54

55
template <typename Integer, if_unsigned_integer<Integer>>
56
constexpr Integer to_ceilinged_bytes(Integer bits) NOEXCEPT
57
{
58
    return ceilinged_divide(bits, byte_bits);
59
}
60

61
template <typename Integer, if_unsigned_integer<Integer>>
62
constexpr Integer to_floored_bytes(Integer bits) NOEXCEPT
100✔
63
{
64
    return floored_divide(bits, byte_bits);
65
}
66

67
// Byte of integral by logical index.
68
// ----------------------------------------------------------------------------
69

70
template <size_t Index, typename Byte, typename Integral,
71
    if_one_byte<Byte>,
72
    if_integral_integer<Integral>,
73
    if_lesser<Index, sizeof(Integral)>>
74
constexpr Byte byte(Integral value) NOEXCEPT
×
75
{
76
    constexpr auto byte_mask = unmask_right<Integral>(byte_bits);
77
    const auto byte = bit_and(shift_right(value, to_bits(Index)), byte_mask);
78
    return possible_narrow_and_sign_cast<Byte>(byte);
79
}
80

81
// Byte Negation.
82
// ----------------------------------------------------------------------------
83
// ****************************************************************************
84
// CONSENSUS: byte negation is consensus behavior, see machine::number.
85
// ****************************************************************************
86

87
template <typename Integer, if_integer<Integer>>
88
constexpr bool is_negated(Integer value) NOEXCEPT
674✔
89
{
90
    // Guard precludes zero appearing negated: ((0 % 8) + 1) == 1.
91
    return is_nonzero(value) && add1(bit_width(value) % byte_bits) == one;
742✔
92
};
93

94
template <typename Integer, if_integer<Integer>>
95
constexpr Integer to_negated(Integer value) NOEXCEPT
49✔
96
{
97
    return set_left(value);
98
}
99

100
template <typename Integer, if_signed_integer<Integer>>
101
constexpr Integer to_unnegated(Integer value) NOEXCEPT
68✔
102
{
103
    // negate(minimum) is undefined behavior, but precluded by clearing a bit.
104
    return !is_negated(value) ? value : negate(
68✔
105
        set_right(value, sub1(bit_width(value)), false));
68✔
106
}
107

108
} // namespace system
109
} // namespace libbitcoin
110

111
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc