Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce equiv_uint_t helper #294

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
BasedOnStyle: LLVM
SortIncludes: false
SeparateDefinitionBlocks: Always
MaxEmptyLinesToKeep: 1
6 changes: 6 additions & 0 deletions benchmarks/apple_arm_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ struct performance_counters {
double branches;
double missed_branches;
double instructions;

performance_counters(uint64_t c, uint64_t b, uint64_t m, uint64_t i)
: cycles(c), branches(b), missed_branches(m), instructions(i) {}

performance_counters(double c, double b, double m, double i)
: cycles(c), branches(b), missed_branches(m), instructions(i) {}

performance_counters(double init)
: cycles(init), branches(init), missed_branches(init),
instructions(init) {}
Expand All @@ -67,6 +70,7 @@ struct performance_counters {
instructions -= other.instructions;
return *this;
}

inline performance_counters &min(const performance_counters &other) {
cycles = other.cycles < cycles ? other.cycles : cycles;
branches = other.branches < branches ? other.branches : branches;
Expand All @@ -77,6 +81,7 @@ struct performance_counters {
other.instructions < instructions ? other.instructions : instructions;
return *this;
}

inline performance_counters &operator+=(const performance_counters &other) {
cycles += other.cycles;
branches += other.branches;
Expand Down Expand Up @@ -920,6 +925,7 @@ static int kdebug_wait(usize timeout_ms, bool *suc) {
// -----------------------------------------------------------------------------

#define EVENT_NAME_MAX 8

typedef struct {
const char *alias; /// name for print
const char *names[EVENT_NAME_MAX]; /// name from pmc db
Expand Down
20 changes: 20 additions & 0 deletions benchmarks/event_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
struct event_count {
std::chrono::duration<double> elapsed;
std::vector<unsigned long long> event_counts;

event_count() : elapsed(0), event_counts{0, 0, 0, 0, 0} {}

event_count(const std::chrono::duration<double> _elapsed,
const std::vector<unsigned long long> _event_counts)
: elapsed(_elapsed), event_counts(_event_counts) {}

event_count(const event_count &other)
: elapsed(other.elapsed), event_counts(other.event_counts) {}

Expand All @@ -42,18 +45,23 @@ struct event_count {
double elapsed_sec() const {
return std::chrono::duration<double>(elapsed).count();
}

double elapsed_ns() const {
return std::chrono::duration<double, std::nano>(elapsed).count();
}

double cycles() const {
return static_cast<double>(event_counts[CPU_CYCLES]);
}

double instructions() const {
return static_cast<double>(event_counts[INSTRUCTIONS]);
}

double branches() const {
return static_cast<double>(event_counts[BRANCHES]);
}

double missed_branches() const {
return static_cast<double>(event_counts[MISSED_BRANCHES]);
}
Expand All @@ -63,6 +71,7 @@ struct event_count {
this->event_counts = other.event_counts;
return *this;
}

event_count operator+(const event_count &other) const {
return event_count(elapsed + other.elapsed,
{
Expand Down Expand Up @@ -98,10 +107,15 @@ struct event_aggregate {
}

double elapsed_sec() const { return total.elapsed_sec() / iterations; }

double elapsed_ns() const { return total.elapsed_ns() / iterations; }

double cycles() const { return total.cycles() / iterations; }

double instructions() const { return total.instructions() / iterations; }

double branches() const { return total.branches() / iterations; }

double missed_branches() const {
return total.missed_branches() / iterations;
}
Expand All @@ -113,18 +127,23 @@ struct event_collector {

#if defined(__linux__)
LinuxEvents<PERF_TYPE_HARDWARE> linux_events;

event_collector()
: linux_events(std::vector<int>{
PERF_COUNT_HW_CPU_CYCLES, PERF_COUNT_HW_INSTRUCTIONS,
PERF_COUNT_HW_BRANCH_INSTRUCTIONS, // Retired branch instructions
PERF_COUNT_HW_BRANCH_MISSES}) {}

bool has_events() { return linux_events.is_working(); }
#elif __APPLE__ && __aarch64__
performance_counters diff;

event_collector() : diff(0) { setup_performance_counters(); }

bool has_events() { return setup_performance_counters(); }
#else
event_collector() {}

bool has_events() { return false; }
#endif

Expand All @@ -138,6 +157,7 @@ struct event_collector {
#endif
start_clock = std::chrono::steady_clock::now();
}

inline event_count &end() {
const auto end_clock = std::chrono::steady_clock::now();
#if defined(__linux)
Expand Down
14 changes: 14 additions & 0 deletions include/fast_float/bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ template <uint16_t size> struct stackvec {
FASTFLOAT_DEBUG_ASSERT(index < length);
return data[index];
}

FASTFLOAT_CONSTEXPR14 const limb &operator[](size_t index) const noexcept {
FASTFLOAT_DEBUG_ASSERT(index < length);
return data[index];
}

// index from the end of the container
FASTFLOAT_CONSTEXPR14 const limb &rindex(size_t index) const noexcept {
FASTFLOAT_DEBUG_ASSERT(index < length);
Expand All @@ -72,14 +74,19 @@ template <uint16_t size> struct stackvec {
FASTFLOAT_CONSTEXPR14 void set_len(size_t len) noexcept {
length = uint16_t(len);
}

constexpr size_t len() const noexcept { return length; }

constexpr bool is_empty() const noexcept { return length == 0; }

constexpr size_t capacity() const noexcept { return size; }

// append item to vector, without bounds checking
FASTFLOAT_CONSTEXPR14 void push_unchecked(limb value) noexcept {
data[length] = value;
length++;
}

// append item to vector, returning if item was added
FASTFLOAT_CONSTEXPR14 bool try_push(limb value) noexcept {
if (len() < capacity()) {
Expand All @@ -89,12 +96,14 @@ template <uint16_t size> struct stackvec {
return false;
}
}

// add items to the vector, from a span, without bounds checking
FASTFLOAT_CONSTEXPR20 void extend_unchecked(limb_span s) noexcept {
limb *ptr = data + length;
std::copy_n(s.ptr, s.len(), ptr);
set_len(len() + s.len());
}

// try to add items to the vector, returning if items were added
FASTFLOAT_CONSTEXPR20 bool try_extend(limb_span s) noexcept {
if (len() + s.len() <= capacity()) {
Expand All @@ -104,6 +113,7 @@ template <uint16_t size> struct stackvec {
return false;
}
}

// resize the vector, without bounds checking
// if the new size is longer than the vector, assign value to each
// appended item.
Expand All @@ -119,6 +129,7 @@ template <uint16_t size> struct stackvec {
set_len(new_len);
}
}

// try to resize the vector, returning if the vector was resized.
FASTFLOAT_CONSTEXPR20 bool try_resize(size_t new_len, limb value) noexcept {
if (new_len > capacity()) {
Expand All @@ -128,6 +139,7 @@ template <uint16_t size> struct stackvec {
return true;
}
}

// check if any limbs are non-zero after the given index.
// this needs to be done in reverse order, since the index
// is relative to the most significant limbs.
Expand All @@ -140,6 +152,7 @@ template <uint16_t size> struct stackvec {
}
return false;
}

// normalize the big integer, so most-significant zero limbs are removed.
FASTFLOAT_CONSTEXPR14 void normalize() noexcept {
while (len() > 0 && rindex(0) == 0) {
Expand Down Expand Up @@ -423,6 +436,7 @@ struct bigint : pow5_tables<> {
stackvec<bigint_limbs> vec;

FASTFLOAT_CONSTEXPR20 bigint() : vec() {}

bigint(bigint const &) = delete;
bigint &operator=(bigint const &) = delete;
bigint(bigint &&) = delete;
Expand Down
4 changes: 3 additions & 1 deletion include/fast_float/digit_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ scientific_exponent(parsed_number_string_t<UC> &num) noexcept {
template <typename T>
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
to_extended(T value) noexcept {
using equiv_uint = typename binary_format<T>::equiv_uint;
using equiv_uint = equiv_uint_t<T>;
constexpr equiv_uint exponent_mask = binary_format<T>::exponent_mask();
constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
Expand Down Expand Up @@ -170,6 +170,7 @@ round_down(adjusted_mantissa &am, int32_t shift) noexcept {
}
am.power2 += shift;
}

template <typename UC>
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
skip_zeros(UC const *&first, UC const *last) noexcept {
Expand Down Expand Up @@ -213,6 +214,7 @@ is_truncated(UC const *first, UC const *last) noexcept {
}
return false;
}

template <typename UC>
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
is_truncated(span<UC const> s) noexcept {
Expand Down
1 change: 1 addition & 0 deletions include/fast_float/fast_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
from_chars(UC const *first, UC const *last, T &value, int base = 10) noexcept;

} // namespace fast_float

#include "parse_number.h"
#endif // FASTFLOAT_FAST_FLOAT_H
Loading