Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvov1 committed Sep 18, 2024
1 parent 34b909c commit ce75155
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
22 changes: 8 additions & 14 deletions Aesi.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class Aesi final {
* @param stringView String
*/
template <typename String, typename Char = typename String::value_type> requires (std::is_same_v<std::basic_string<Char>,
typename std::decay<String>::type> || std::is_same_v<std::basic_string_view<Char>, typename std::decay<String>::type>)
std::decay_t<String>> || std::is_same_v<std::basic_string_view<Char>, std::decay_t<String>>)
gpu constexpr Aesi(String&& stringView) noexcept : Aesi(stringView.data(), stringView.size()) {}

template <typename String, typename Char = typename String::value_type> requires (std::is_same_v<std::basic_string<Char>,
typename std::decay<String>::type> || std::is_same_v<std::basic_string_view<Char>, typename std::decay<String>::type>)
std::decay_t<String>> || std::is_same_v<std::basic_string_view<Char>, std::decay_t<String>>)
gpu constexpr Aesi(const String& stringView) noexcept : Aesi(stringView.data(), stringView.size()) {}

/**
Expand Down Expand Up @@ -250,8 +250,7 @@ class Aesi final {
}

if(sign == Sign::Positive) { /* Positive += Negative; */
const auto ratio = base.compareTo(addendum.base);
switch(ratio) {
switch(base.compareTo(addendum.base)) {
case Comparison::greater: {
base -= addendum.base;
return *this;
Expand All @@ -267,8 +266,7 @@ class Aesi final {
}
}
} else { /* Negative += Positive; */
const auto ratio = base.compareTo(addendum.base);
switch(ratio) {
switch(const auto ratio = base.compareTo(addendum.base)) {
case Comparison::greater: {
base -= addendum.base;
return *this;
Expand Down Expand Up @@ -312,8 +310,7 @@ class Aesi final {

if(sign == Sign::Positive) {
if(subtrahend.sign == Sign::Positive) { /* Positive -= Positive; */
const auto ratio = base.compareTo(subtrahend.base);
switch(ratio) {
switch(base.compareTo(subtrahend.base)) {
case Comparison::greater: {
base -= subtrahend.base;
return *this;
Expand All @@ -334,8 +331,7 @@ class Aesi final {
}
} else {
if(subtrahend.sign == Sign::Negative) { /* Negative -= Negative; */
const auto ratio = base.compareTo(subtrahend.base);
switch(ratio) {
switch(base.compareTo(subtrahend.base)) {
case Comparison::greater: {
base -= subtrahend.base;
return *this;
Expand Down Expand Up @@ -687,8 +683,7 @@ class Aesi final {
* @note Available from C++20 standard and further. Should almost never return Strong_ordering::Equivalent
*/
gpu constexpr auto operator<=>(const Aesi& other) const noexcept -> std::strong_ordering {
const auto ratio = this->compareTo(other);
switch(ratio) {
switch(this->compareTo(other)) {
case Comparison::less:
return std::strong_ordering::less;
case Comparison::greater:
Expand All @@ -708,8 +703,7 @@ class Aesi final {
*/
template <typename Object>
gpu constexpr auto operator<=>(const Object& other) const noexcept -> std::strong_ordering {
const auto ratio = this->compareTo(other);
switch(ratio) {
switch(this->compareTo(other)) {
case Comparison::less:
return std::strong_ordering::less;
case Comparison::greater:
Expand Down
43 changes: 22 additions & 21 deletions Aeu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/// @cond HIDE_INCLUDES
#include <iostream>
#include <cassert>
#include <vector>

#ifdef __CUDACC__
#define gpu __host__ __device__
Expand Down Expand Up @@ -220,11 +219,11 @@ class Aeu final {
* @details Constructs object from STD::Basic_String or STD::Basic_String_View. Accepts objects based on char or wchar_t
*/
template <typename String, typename Char = typename String::value_type> requires (std::is_same_v<std::basic_string<Char>,
typename std::decay<String>::type> || std::is_same_v<std::basic_string_view<Char>, typename std::decay<String>::type>)
std::decay_t<String>> || std::is_same_v<std::basic_string_view<Char>, std::decay_t<String>>)
gpu constexpr Aeu(String&& stringView) noexcept : Aeu(stringView.data(), stringView.size()) {}

template <typename String, typename Char = typename String::value_type> requires (std::is_same_v<std::basic_string<Char>,
typename std::decay<String>::type> || std::is_same_v<std::basic_string_view<Char>, typename std::decay<String>::type>)
std::decay_t<String>> || std::is_same_v<std::basic_string_view<Char>, std::decay_t<String>>)
gpu constexpr Aeu(const String& stringView) noexcept : Aeu(stringView.data(), stringView.size()) {}

#ifdef AESI_CRYPTOPP_INTEGRATION
Expand Down Expand Up @@ -794,8 +793,7 @@ class Aeu final {
* @note Available from C++20 standard and further. Should almost never return Strong_ordering::Equivalent
*/
gpu constexpr auto operator<=>(const Aeu& other) const noexcept -> std::strong_ordering {
const auto ratio = this->compareTo(other);
switch(ratio) {
switch(this->compareTo(other)) {
case Comparison::less:
return std::strong_ordering::less;
case Comparison::greater:
Expand All @@ -815,8 +813,7 @@ class Aeu final {
*/
template <typename Unsigned>
gpu constexpr auto operator<=>(const Unsigned& other) const noexcept -> std::strong_ordering {
const auto ratio = this->compareTo(other);
switch(ratio) {
switch(this->compareTo(other)) {
case Comparison::less:
return std::strong_ordering::less;
case Comparison::greater:
Expand Down Expand Up @@ -935,8 +932,7 @@ class Aeu final {
for(; lastBlock > 0 && blocks[lastBlock] == 0; --lastBlock) ;

for(int8_t byteN = sizeof(block) - 1; byteN >= 0; --byteN) {
const auto byte = (blocks[lastBlock] & (0xffU << (byteN * bitsInByte))) >> (byteN * bitsInByte);
if(byte)
if((blocks[lastBlock] & (0xffU << (byteN * bitsInByte))) >> (byteN * bitsInByte))
return lastBlock * sizeof(block) + byteN + 1;
}
return lastBlock * sizeof(block);
Expand All @@ -956,8 +952,7 @@ class Aeu final {
if(!byte) continue;

for(int8_t bitN = bitsInByte - 1; bitN >= 0; --bitN) {
const auto bit = (byte & (0x1u << bitN)) >> bitN;
if(bit)
if((byte & (0x1u << bitN)) >> bitN)
return (lastBlock * sizeof(block) + byteN) * bitsInByte + bitN + 1;
}
return ((lastBlock - 1) * sizeof(block) + byteN) * bitsInByte;
Expand Down Expand Up @@ -1071,7 +1066,7 @@ class Aeu final {
* @details Counts Bézout coefficients along with the greatest common divisor. Returns coefficients by reference
*/
gpu static constexpr auto gcd(const Aeu& first, const Aeu& second, Aeu& bezoutX, Aeu& bezoutY) noexcept -> Aeu {
Aeu gcd, tGcd, quotient, remainder;
Aeu gcd, quotient, remainder;

const auto ratio = first.compareTo(second);
if(ratio == Comparison::greater) {
Expand All @@ -1084,7 +1079,7 @@ class Aeu final {

bezoutX = 0u; bezoutY = 1u;
for(Aeu tX = 1u, tY = 0u; remainder != 0u; ) {
tGcd = gcd; gcd = remainder;
Aeu tGcd = gcd; gcd = remainder;

Aeu t = bezoutX; bezoutX = tX - quotient * bezoutX; tX = t;
t = bezoutY; bezoutY = tY - quotient * bezoutY; tY = t;
Expand All @@ -1106,7 +1101,7 @@ class Aeu final {
* @details Faster version, ignoring bezout coefficients
*/
gpu static constexpr auto gcd(const Aeu& first, const Aeu& second) noexcept -> Aeu {
Aeu gcd, tGcd, quotient, remainder;
Aeu gcd, quotient, remainder;

const auto ratio = first.compareTo(second);
if(ratio == Comparison::greater) {
Expand All @@ -1118,7 +1113,7 @@ class Aeu final {
}

for(Aeu tX = 1u, tY = 0u; remainder != 0u; ) {
tGcd = gcd; gcd = remainder;
Aeu tGcd = gcd; gcd = remainder;
divide(tGcd, gcd, quotient, remainder);
}

Expand Down Expand Up @@ -1232,23 +1227,29 @@ class Aeu final {
if (showBase && bufferSize > 3) {
if constexpr (base == 2) {
if constexpr (std::is_same_v<Char, char>) {
memcpy(buffer, "0b", 2 * sizeof(Char));
buffer[0] = '0'; buffer[1] = 'b';
// memcpy(buffer, "0b", 2 * sizeof(Char));
} else {
memcpy(buffer, L"0b", 2 * sizeof(Char));
buffer[0] = L'0'; buffer[1] = L'b';
// memcpy(buffer, L"0b", 2 * sizeof(Char));
}
position += 2;
} else if constexpr (base == 8) {
if constexpr (std::is_same_v<Char, char>) {
memcpy(buffer, "0o", 2 * sizeof(Char));
buffer[0] = '0'; buffer[1] = 'o';
// memcpy(buffer, "0o", 2 * sizeof(Char));
} else {
memcpy(buffer, L"0o", 2 * sizeof(Char));
buffer[0] = L'0'; buffer[1] = L'o';
// memcpy(buffer, L"0o", 2 * sizeof(Char));
}
position += 2;
} else if constexpr (base == 16) {
if constexpr (std::is_same_v<Char, char>) {
memcpy(buffer, "0x", 2 * sizeof(Char));
buffer[0] = '0'; buffer[1] = 'x';
// memcpy(buffer, "0x", 2 * sizeof(Char));
} else {
memcpy(buffer, L"0x", 2 * sizeof(Char));
buffer[0] = L'0'; buffer[1] = L'x';
// memcpy(buffer, L"0x", 2 * sizeof(Char));
}
position += 2;
}
Expand Down

0 comments on commit ce75155

Please sign in to comment.