Skip to content

Commit 9a1e406

Browse files
authored
Merge pull request #85 from thecppzoo/jp/expo-ai
Add Exponentiation via Associative Iteration
2 parents f28eb43 + a328d4a commit 9a1e406

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

inc/zoo/swar/associative_iteration.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,38 @@ constexpr auto multiplication_OverflowUnsafe_SpecificBitCount_deprecated(
451451
return product;
452452
}
453453

454+
template<int ActualBits, int NB, typename T>
455+
constexpr auto exponentiation_OverflowUnsafe_SpecificBitCount(
456+
SWAR<NB, T> x,
457+
SWAR<NB, T> exponent
458+
) {
459+
using S = SWAR<NB, T>;
460+
461+
auto operation = [](auto left, auto right, auto counts) {
462+
const auto mask = makeLaneMaskFromMSB(counts);
463+
const auto product =
464+
multiplication_OverflowUnsafe_SpecificBitCount<ActualBits>(left, right);
465+
return (product & mask) | (left & ~mask);
466+
};
467+
468+
// halver should work same as multiplication... i think...
469+
auto halver = [](auto counts) {
470+
auto msbCleared = counts & ~S{S::MostSignificantBit};
471+
return S{static_cast<T>(msbCleared.value() << 1)};
472+
};
473+
474+
exponent = S{static_cast<T>(exponent.value() << (NB - ActualBits))};
475+
return associativeOperatorIterated_regressive(
476+
x,
477+
S{meta::BitmaskMaker<T, 1, NB>().value}, // neutral is lane wise..
478+
exponent,
479+
S{S::MostSignificantBit},
480+
operation,
481+
ActualBits,
482+
halver
483+
);
484+
}
485+
454486
template<int NB, typename T>
455487
constexpr auto multiplication_OverflowUnsafe(
456488
SWAR<NB, T> multiplicand,

0 commit comments

Comments
 (0)