Skip to content

Commit 83eedd8

Browse files
committed
Optimize bit reverse and unary minus oparations
1 parent 5babc4c commit 83eedd8

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

include/slimcpplib/long_math.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ using half_t = typename half_type<type_t>::type;
119119
// standalone routines
120120
////////////////////////////////////////////////////////////////////////////////////////////////////
121121

122+
// make array from specified array
123+
124+
template<typename type_t, uint_t size, typename func_t>
125+
constexpr std::array<type_t, size> make_array(const std::array<type_t, size>& arr, const func_t& func);
126+
template<typename type_t, uint_t size, typename func_t, uint_t... idx>
127+
constexpr std::array<type_t, size> make_array(const std::array<type_t, size>& arr, const func_t& func, std::integer_sequence<uint_t, idx...>);
128+
122129
// extract low half of unsigned integer
123130

124131
template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
@@ -183,6 +190,24 @@ constexpr type_t divr2(type_t value1_hi, type_t value1_lo, type_t value2, std::o
183190
// standalone routines
184191
////////////////////////////////////////////////////////////////////////////////////////////////////
185192

193+
template<typename type_t, uint_t size, typename func_t>
194+
constexpr std::array<type_t, size> make_array(const std::array<type_t, size>& arr, const func_t& func)
195+
{
196+
return make_array(arr, func, std::make_integer_sequence<uint_t, size>{});
197+
}
198+
199+
200+
201+
////////////////////////////////////////////////////////////////////////////////////////////////////
202+
template<typename type_t, uint_t size, typename func_t, uint_t... idx>
203+
constexpr std::array<type_t, size> make_array(const std::array<type_t, size>& arr, const func_t& func, std::integer_sequence<uint_t, idx...>)
204+
{
205+
return std::array<type_t, size>{ (func(idx, arr[idx]))... };
206+
}
207+
208+
209+
210+
////////////////////////////////////////////////////////////////////////////////////////////////////
186211
template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int>>
187212
constexpr type_t half_lo(type_t value) noexcept
188213
{

include/slimcpplib/long_uint.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,9 @@ constexpr bool long_uint_t<native_t, size>::operator>=(const long_uint_t& that)
336336
template<typename native_t, uint_t size>
337337
constexpr long_uint_t<native_t, size> long_uint_t<native_t, size>::operator~() const noexcept
338338
{
339-
long_uint_t result;
340-
341-
result.digits[0] = ~digits[0];
342-
343-
for (uint_t n = 1; n < std::size(digits); ++n)
344-
result.digits[n] = ~digits[n];
345-
346-
return result;
339+
return make_array(digits, [](uint_t /*idx*/, native_t value) {
340+
return ~value;
341+
});
347342
}
348343

349344

@@ -579,17 +574,11 @@ constexpr long_uint_t<native_t, size> long_uint_t<native_t, size>::operator--(in
579574
template<typename native_t, uint_t size>
580575
constexpr long_uint_t<native_t, size> long_uint_t<native_t, size>::operator-() const noexcept
581576
{
582-
long_uint_t negative = *this;
583-
584577
bool borrow = true;
585578

586-
for (uint_t n = 0; n < std::size(negative.digits); ++n)
587-
negative.digits[n] = subb<native_t>(negative.digits[n], 0, borrow);
588-
589-
for (uint_t n = 0; n < std::size(negative.digits); ++n)
590-
negative.digits[n] = ~negative.digits[n];
591-
592-
return negative;
579+
return make_array(digits, [&borrow](uint_t /*idx*/, native_t value) {
580+
return ~subb<native_t>(value, 0, borrow);
581+
});
593582
}
594583

595584

0 commit comments

Comments
 (0)