Skip to content

Commit

Permalink
Improved multiple bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvov1 committed Jan 7, 2024
1 parent 5c1bf9d commit edffd70
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 452 deletions.
95 changes: 64 additions & 31 deletions Aesi.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace {
* @enum AesiCMP
* @brief Analogue of STD::Strong_ordering since CUDA does not support <=> operator
*/
enum class AesiCMP { equal = 0, less = 1, greater = 2, equivalent = 3 };
enum AesiCMP { equal = 0, less = 1, greater = 2, equivalent = 3 };
}

/**
Expand All @@ -57,13 +57,13 @@ class Aesi final {
* @brief Block line of the number
*/
using blockLine = std::array<block, blocksNumber>;
blockLine blocks;
blockLine blocks {};

/**
* @enum Aesi::Sign
* @brief Specifies sign of the number. Should be Positive, Negative or Zero
*/
enum Sign { Zero = 0, Positive = 1, Negative = 2 } sign;
enum Sign { Zero = 0, Positive = 1, Negative = 2 } sign { Zero };
/* ----------------------------------------------------------------------- */


Expand Down Expand Up @@ -647,15 +647,19 @@ class Aesi final {
switch (sign) {
case Zero:
switch (other.sign) {
case Zero: return AesiCMP::equal;
case Positive: return AesiCMP::less;
case Negative: return AesiCMP::greater;
default: return AesiCMP::equivalent;
case Zero:
return AesiCMP::equal;
case Positive:
return AesiCMP::less;
case Negative:
return AesiCMP::greater;
default:
return AesiCMP::equivalent;
}
case Positive:
switch (other.sign) {
case Positive: {
for(long long i = blocksNumber; i >= 0; --i) {
for(long long i = blocksNumber - 1; i >= 0; --i) {
const block thisBlock = blocks[i], otherBlock = other.blocks[i];
if(thisBlock != 0) {
if(thisBlock > otherBlock)
Expand All @@ -669,13 +673,15 @@ class Aesi final {
return AesiCMP::equal;
}
case Zero:
case Negative: return AesiCMP::greater;
default: return AesiCMP::equivalent;
case Negative:
return AesiCMP::greater;
default:
return AesiCMP::equivalent;
}
case Negative:
switch (other.sign) {
case Negative: {
for(long long i = blocksNumber; i >= 0; --i) {
for(long long i = blocksNumber - 1; i >= 0; --i) {
const block thisBlock = blocks[i], otherBlock = other.blocks[i];
if(thisBlock != 0) {
if(thisBlock > otherBlock)
Expand All @@ -689,10 +695,13 @@ class Aesi final {
return AesiCMP::equal;
}
case Zero:
case Positive: return AesiCMP::less;
default: return AesiCMP::equivalent;
case Positive:
return AesiCMP::less;
default:
return AesiCMP::equivalent;
}
default: return AesiCMP::equivalent;
default:
return AesiCMP::equivalent;
}
};

Expand All @@ -714,10 +723,14 @@ class Aesi final {
*/
gpu constexpr auto operator<=>(const Aesi& other) const noexcept -> std::strong_ordering {
switch(this->compareTo(other)) {
case AesiCMP::less: return std::strong_ordering::less;
case AesiCMP::greater: return std::strong_ordering::greater;
case AesiCMP::equal: return std::strong_ordering::equal;
default: return std::strong_ordering::equivalent;
case AesiCMP::less:
return std::strong_ordering::less;
case AesiCMP::greater:
return std::strong_ordering::greater;
case AesiCMP::equal:
return std::strong_ordering::equal;
default:
return std::strong_ordering::equivalent;
}
};
#endif
Expand Down Expand Up @@ -1049,7 +1062,6 @@ class Aesi final {
}
/* ----------------------------------------------------------------------- */


/**
* @brief Cast from Aesi to built-in integral types
* @param Type integral_type TEMPLATE
Expand All @@ -1072,18 +1084,32 @@ class Aesi final {
* @note This method is used in all manipulations between numbers of different precision. Using this method is not recommended,
* cause it leads to redundant copying and may be slow
*/
template <std::size_t newBitness> requires (newBitness != bitness) [[nodiscard]]
gpu constexpr auto precisionCast() const noexcept -> Aesi<newBitness> {
Aesi<newBitness> result = 0;

long long startBlock = (blocksNumber < (newBitness / blockBitLength) ? blocksNumber - 1 : (newBitness / blockBitLength) - 1);
for(; startBlock >= 0; --startBlock) {
result <<= blockBitLength;
result |= blocks[startBlock];
}
template <std::size_t newBitness> requires (newBitness != bitness)
constexpr auto precisionCast() const noexcept -> Aesi<newBitness> {
Aesi<newBitness> result (blocks);
if(sign == Negative) return -result; return result;
}

if(sign == Negative) result *= -1;
return result;
// template <std::size_t newBitness> requires (newBitness != bitness) [[nodiscard]]
// gpu constexpr auto precisionCast() const noexcept -> Aesi<newBitness> {
// Aesi<newBitness> result = 0;
//
// long long startBlock = (blocksNumber < (newBitness / blockBitLength) ? blocksNumber - 1 : (newBitness / blockBitLength) - 1);
// for(; startBlock >= 0; --startBlock) {
// result <<= blockBitLength;
// result |= blocks[startBlock];
// }
//
// if(sign == Negative) result *= -1;
// return result;
// }

template <std::size_t length>
constexpr explicit Aesi(const std::array<block, length>& data) noexcept {
/* FIXME: Remove this function, change precision cast to use bitness. */
for(std::size_t i = 0; i < blocksNumber && i < length; ++i)
blocks[i] = data[i]; sign = Positive;
/* FIXME */
}

/**
Expand Down Expand Up @@ -1198,6 +1224,7 @@ class Aesi final {
return base;
} (flags & std::ios::basefield, ss, flags & std::ios::showbase);

// if(true /*base == 16*/) {
if(base == 16) {
long long iter = value.blocks.size() - 1;
for(; value.blocks[iter] == 0 && iter >= 0; --iter);
Expand All @@ -1209,7 +1236,7 @@ class Aesi final {
}
} else {
/* Well, here we use a pre-calculated magic number to ratio the length of numbers in decimal or octal notation according to bitness.
* It is 2.95-98 for octal and 3.2 for decimal. */
* * It is 2.95-98 for octal and 3.2 for decimal. */
constexpr auto bufferSize = static_cast<std::size_t>(static_cast<double>(bitness) / 2.95);
Char buffer [bufferSize] {}; std::size_t filled = 0;

Expand All @@ -1220,6 +1247,12 @@ class Aesi final {
copy = quotient;
}

if constexpr (std::is_same_v<Char, char>) {
std::cout << "Filled: " << filled << std::endl << "Buffer: " << buffer << std::endl;
} else {
std::wcout << L"Filled: " << filled << std::endl << L"Buffer: " << buffer << std::endl;
}

for(; filled > 0; --filled)
ss << buffer[filled - 1];
}
Expand Down
Binary file modified test/benchmarks/measures.db
Binary file not shown.
2 changes: 1 addition & 1 deletion test/bitwise/bitwise_and.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ TEST(Bitwise, AND) {
Aesi512 y56 = 6039725184101265805; EXPECT_EQ(y56 & 0, 0); Aesi512 y57 = 2310906701521457706; EXPECT_EQ(y57 & 0, 0);
Aesi512 y58 = 5729261014028876522; EXPECT_EQ(y58 & 0, 0); Aesi512 y59 = -7400848010369953160; EXPECT_EQ(y59 & 0, 0);

Logging::addRecord("Bitwise-AND",
Logging::addRecord("Bitwise_AND",
std::chrono::system_clock::to_time_t(timeStart),
(std::chrono::system_clock::now() - timeStart).count());;
}
Expand Down
Loading

0 comments on commit edffd70

Please sign in to comment.