Skip to content

Commit

Permalink
Fix .fill, make it more efficient. Catch invalid byte order.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Jul 2, 2024
1 parent 4d5051c commit c6f26dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/FillExpression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ Expression FillExpression::create(const Expression& count, const Expression& val
auto actual_count = real_count.unsigned_value();
const auto real_value = *value.value();
auto encoding = Encoder(real_value);
std::string bytes;
auto bytes = std::string{};
encoding.encode(bytes, real_value);
auto result = std::string{};
result.reserve(actual_count * bytes.size());
for (uint64_t i = 0; i < actual_count; i++) {
encoding.encode(bytes, real_value);
result += bytes;
}
return Expression(Value(bytes));
return Expression(Value(result));
}
else {
return Expression(std::make_shared<FillExpression>(count, value));
Expand Down
29 changes: 21 additions & 8 deletions src/Int.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "Int.h"
#include "Exception.h"

#include <limits>
#include <iomanip>
#include <sstream>

size_t Int::minimum_byte_size(uint64_t value) {
if (value > std::numeric_limits<uint32_t>::max()) {
Expand Down Expand Up @@ -80,15 +83,25 @@ size_t Int::minimum_byte_size(int64_t value) {
}

void Int::encode(std::string &bytes, uint64_t value, uint64_t size, uint64_t byte_order) {
uint64_t mask = 10000000;
for (auto index = 0; index < 8; index += 1) {
uint64_t byte_index = (byte_order / mask) % 10 - 1;
mask /= 10;
if (byte_index >= size) {
continue;
if (size == 1) {
bytes += static_cast<char>(value & 0xff);
}
else {
uint64_t mask = 10000000;
for (auto index = 0; index < 8; index += 1) {
uint64_t byte_index = (byte_order / mask) % 10 - 1;
if (byte_index > 8) {
std::stringstream str;
str << "invalid byte order " << std::setfill('0') << std::setw(8) << byte_order;
throw Exception(str.str());
}
mask /= 10;
if (byte_index >= size) {
continue;
}
uint64_t byte = (value >> (byte_index * 8)) & 0xff;
bytes += static_cast<char>(byte);
}
uint64_t byte = (value >> (byte_index * 8)) & 0xff;
bytes += static_cast<char>(byte);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Linker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void Linker::link() {
program->import(library.get());
}

Target::set_current_target(target);
program->resolve_defaults();
program->evaluate();
program->evaluate();
Expand Down

0 comments on commit c6f26dc

Please sign in to comment.