Fixed many hardening issues
This commit is contained in:
16
data.cpp
16
data.cpp
@@ -52,8 +52,8 @@ static void test_argsort(const std::array<int32_t, N>& gaps, const asp::Array<T>
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static asp::Array<T> create_random_array(const size_t& n) noexcept {
|
||||
asp::Array<T> original(n);
|
||||
static inline asp::Array<T> create_random_array(const int64_t n) noexcept {
|
||||
asp::Array<T> original((size_t(n)));
|
||||
std::random_device rd;
|
||||
std::default_random_engine gen(rd());
|
||||
std::uniform_int_distribution<T> distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
||||
@@ -64,22 +64,20 @@ int32_t main(int32_t argc, char** argv) {
|
||||
asp::toolbox_unit_test();
|
||||
|
||||
using array_type = uint16_t;
|
||||
size_t N = (static_cast<size_t>(1)<<15) - 1;
|
||||
// size_t N = std::numeric_limits<array_type>::max();
|
||||
int64_t N = (static_cast<int64_t>(1)<<15) - 1;
|
||||
// int64_t N = std::numeric_limits<array_type>::max();
|
||||
if (argc > 2) {
|
||||
fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n");
|
||||
return EXIT_FAILURE;
|
||||
} else if (argc == 2)
|
||||
N = std::strtoul(argv[1], argv + strlen(argv[1]) + 1, 10);
|
||||
N = std::strtol(argv[1], argv + strlen(argv[1]) + 1, 10);
|
||||
|
||||
char title[64];
|
||||
sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * N * sizeof(array_type)).c_str());
|
||||
const std::array<int32_t, 3> gaps = { 48, -17, 25 };
|
||||
sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * uint64_t(N) * sizeof(array_type)).c_str());
|
||||
constexpr std::array<int32_t, 3> gaps = { 48, -17, 25 };
|
||||
asp::header(gaps, { title, "Time spent (ns)", "Formatted time spent" });
|
||||
|
||||
const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>(gaps, "Creating random array", create_random_array<array_type>, N);
|
||||
// const asp::Array<array_type> original = { 2, 5, 3, 0, 2, 3, 0, 3 };
|
||||
// asp::print(original);
|
||||
|
||||
test_sort(gaps, original, asp::bubble_sort<array_type>, "Bubble sort");
|
||||
test_argsort(gaps, original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)");
|
||||
|
45
toolbox.cpp
45
toolbox.cpp
@@ -1,24 +1,11 @@
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
|
||||
#include "toolbox.hpp"
|
||||
|
||||
namespace asp {
|
||||
/**
|
||||
* @brief Swap two given memory values
|
||||
*
|
||||
* @tparam T Type of memory placeholder
|
||||
* @param a Firat memory pointer
|
||||
* @param b Second memory pointer
|
||||
*/
|
||||
template<typename T>
|
||||
constexpr void swap(T* const a, T* const b) noexcept {
|
||||
const T temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert a given number to string
|
||||
*
|
||||
@@ -31,12 +18,12 @@ namespace asp {
|
||||
size_t i = 0;
|
||||
|
||||
for (; num > 0; num /= 10)
|
||||
str[offset + i++] = num % 10 + '0';
|
||||
str[offset + i++] = char(num % 10) + '0';
|
||||
|
||||
str[offset + i] = '\0';
|
||||
|
||||
for (size_t j = 0; j < i / 2; ++j)
|
||||
swap(str + offset + j, str + offset + i - j - 1);
|
||||
std::swap(str[offset + j], str[offset + i - j - 1]);
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -49,7 +36,7 @@ namespace asp {
|
||||
*/
|
||||
template<typename T>
|
||||
static constexpr uint64_t u64(const T var) noexcept {
|
||||
return static_cast<uint64_t>(var);
|
||||
return uint64_t(var);
|
||||
}
|
||||
|
||||
static constexpr const size_t STR_BUFFER_SIZE = 64;
|
||||
@@ -77,10 +64,10 @@ namespace asp {
|
||||
}
|
||||
|
||||
size_t j = 0;
|
||||
for(int8_t i = static_cast<int8_t>(format_prefix.size() - 1) * 10; i >= 0; i -= 10){
|
||||
for(uint8_t i = uint8_t(format_prefix.size() - 1) * 10; i > 0; i -= 10){
|
||||
const uint64_t nsi = n >> i;
|
||||
if(nsi > 0){
|
||||
const int8_t idx = i / 10;
|
||||
const uint8_t idx = i / 10;
|
||||
j += ullstr(nsi, j, s);
|
||||
for(int k = 0; format_prefix[idx][k] > 0; ++k)
|
||||
s[j++] = format_prefix[idx][k];
|
||||
@@ -88,6 +75,12 @@ namespace asp {
|
||||
n &= u64(-1) >> (64 - i);
|
||||
}
|
||||
}
|
||||
if(n > 0){
|
||||
j += ullstr(n, j, s);
|
||||
for(int k = 0; format_prefix[0][k] > 0; ++k)
|
||||
s[j++] = format_prefix[0][k];
|
||||
s[j++] = ' ';
|
||||
}
|
||||
|
||||
/* Remove trailing character */
|
||||
s[j - 1] = '\0';
|
||||
@@ -111,7 +104,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
uint64_t res;
|
||||
for (int8_t i = time_numbers.size() - 1; i >= 0; --i) {
|
||||
for (uint8_t i = time_numbers.size() - 1; i > 0; --i) {
|
||||
if (time >= time_numbers[i]) {
|
||||
res = time / time_numbers[i];
|
||||
time %= time_numbers[i];
|
||||
@@ -121,6 +114,14 @@ namespace asp {
|
||||
s[j++] = ' ';
|
||||
}
|
||||
}
|
||||
if (time >= time_numbers[0]) {
|
||||
res = time / time_numbers[0];
|
||||
time %= time_numbers[0];
|
||||
j += ullstr(res, j, s);
|
||||
for(int k = 0; time_formats[0][k] > 0; ++k)
|
||||
s[j++] = time_formats[0][k];
|
||||
s[j++] = ' ';
|
||||
}
|
||||
|
||||
/* Remove trailing character */
|
||||
s[j - 1] = '\0';
|
||||
|
@@ -90,7 +90,7 @@ namespace asp {
|
||||
const auto start = time();
|
||||
fnc(std::forward<Args>(args)...);
|
||||
const long long timespent = duration_ns(time() - start);
|
||||
formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str() });
|
||||
formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(uint64_t(timespent)).c_str() });
|
||||
}
|
||||
|
||||
template <typename T, typename F, size_t N, typename... Args>
|
||||
@@ -101,7 +101,7 @@ namespace asp {
|
||||
const auto start = time();
|
||||
const T res = fnc(std::forward<Args>(args)...);
|
||||
const long long timespent = duration_ns(time() - start);
|
||||
formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str() });
|
||||
formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(uint64_t(timespent)).c_str() });
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user