Skip to content

Commit 4a2c696

Browse files
committed
Fix shift argument type
1 parent c5bbc1e commit 4a2c696

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

include/slimcpplib/long_fixdiv.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class long_fixed_divider
5252
constexpr long_fixed_divider(const long_fixed_divider& that) noexcept = default;
5353
constexpr long_fixed_divider(long_fixed_divider&& that) noexcept = default;
5454
constexpr long_fixed_divider(const type_t& divider) noexcept;
55-
constexpr long_fixed_divider(const type_t& multiplier, const type_t& addition, const type_t& shift) noexcept;
55+
constexpr long_fixed_divider(const type_t& multiplier, const type_t& addition, const uint_t& shift) noexcept;
5656

5757
////////////////////////////////////////////////////////////////////////////////////////////////
5858
// public methods
@@ -64,6 +64,7 @@ class long_fixed_divider
6464
static constexpr long_fixed_divider create(const type_t& divider) noexcept;
6565
constexpr type_t divide(const type_t& dividend) const noexcept;
6666

67+
private:
6768
////////////////////////////////////////////////////////////////////////////////////////////////
6869
// data members
6970

@@ -78,9 +79,9 @@ class long_fixed_divider
7879
// standalone methods
7980
////////////////////////////////////////////////////////////////////////////////////////////////////
8081

81-
template <typename type_t>
82+
template<typename type_t>
8283
constexpr long_fixed_divider<type_t> make_fixed_divider(const type_t value) noexcept;
83-
template <typename type_t>
84+
template<typename type_t>
8485
constexpr type_t operator/(const type_t& dividend, const long_fixed_divider<type_t>& divider) noexcept;
8586

8687

@@ -99,7 +100,7 @@ constexpr long_fixed_divider<type_t>::long_fixed_divider(const type_t& divider)
99100
}
100101

101102
template<typename type_t>
102-
constexpr long_fixed_divider<type_t>::long_fixed_divider(const type_t& multiplier, const type_t& addition, const type_t& shift) noexcept
103+
constexpr long_fixed_divider<type_t>::long_fixed_divider(const type_t& multiplier, const type_t& addition, const uint_t& shift) noexcept
103104
: multiplier(std::move(multiplier))
104105
, addition(std::move(addition))
105106
, shift(std::move(shift))
@@ -111,6 +112,27 @@ constexpr long_fixed_divider<type_t>::long_fixed_divider(const type_t& multiplie
111112
////////////////////////////////////////////////////////////////////////////////////////////////////
112113
// public methods
113114

115+
template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
116+
constexpr uint_t nlz_costexpr(type_t value) noexcept
117+
{
118+
type_t mask = ~type_t(0) ^ ((~type_t(0)) / 2);
119+
uint_t count = 0;
120+
121+
for (uint_t n = 0; n < bit_count_v<type_t>; ++n) {
122+
123+
if ((value & mask) != 0)
124+
break;
125+
126+
mask /= 2;
127+
++count;
128+
}
129+
130+
return count;
131+
}
132+
133+
134+
135+
////////////////////////////////////////////////////////////////////////////////////////////////////
114136
template<typename type_t>
115137
constexpr long_fixed_divider<type_t> long_fixed_divider<type_t>::create(const type_t& divider) noexcept
116138
{
@@ -137,8 +159,8 @@ constexpr long_fixed_divider<type_t> long_fixed_divider<type_t>::create(const ty
137159

138160
if (divider == 1) {
139161

140-
multiplier = ~multiplier;
141-
addition = multiplier;
162+
multiplier = ~type_t(0);
163+
addition = ~type_t(0);
142164
shift = 0;
143165

144166
} else {
@@ -150,7 +172,7 @@ constexpr long_fixed_divider<type_t> long_fixed_divider<type_t>::create(const ty
150172

151173
return long_fixed_divider(multiplier, addition, shift);
152174
}
153-
175+
154176

155177

156178
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -159,10 +181,10 @@ constexpr type_t long_fixed_divider<type_t>::divide(const type_t& dividend) cons
159181
{
160182
type_t mul_lo = dividend;
161183
type_t mul_hi = mul(mul_lo, multiplier);
162-
163-
if (addition != 0)
164-
mul_hi += add(mul_lo, addition);
165-
184+
185+
//if (addition != 0)
186+
mul_hi += add(mul_lo, addition);
187+
166188
return mul_hi >>= shift;
167189
}
168190

0 commit comments

Comments
 (0)