Skip to content
Draft
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
18 changes: 10 additions & 8 deletions include/boost/histogram/axis/regular.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,18 @@ class regular : public iterator_mixin<regular<Value, Transform, MetaData, Option
return static_cast<index_type>(z * size());
}
} else {
if (z < 1) {
if (z >= 0)
return static_cast<index_type>(z * size());
else
return -1;
}
// Branch-free so that random data does not mispredict. The clamped value
// keeps the conversion to int in range for any input. fmin(NaN, 1) is 1,
// so NaN maps to size(); the order of fmin and fmax matters.
const bool below = z < 0;
const auto zc = std::fmax(std::fmin(z, decltype(z){1}), decltype(z){0});
index_type i = static_cast<index_type>(zc * size());
i = below ? -1 : i;
// upper edge of last bin is inclusive if overflow bin is not present
if (!options_type::test(option::overflow) && z == 1) return size() - 1;
if (!options_type::test(option::overflow)) i -= static_cast<index_type>(z == 1);
return i; // size() if x is NaN
}
return size(); // also returned if x is NaN
return size();
}

/// Returns index and shift (if axis has grown) for the passed argument.
Expand Down
31 changes: 28 additions & 3 deletions include/boost/histogram/detail/fill_n.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ struct index_visitor {
template <class T>
void linearize_element(std::false_type, pointer it, const T& x) const {
// no axis is growing
linearize(*it, stride_, axis_, try_cast<value_type, std::invalid_argument>(x));
linearize_branchless(*it, stride_, axis_,
try_cast<value_type, std::invalid_argument>(x));
}

template <class T>
Expand Down Expand Up @@ -215,11 +216,30 @@ void fill_n_storage(S& s, const Index idx, weight_type<T>&& w, Ts&&... ps) noexc
(void)std::initializer_list<int>{(ps.second ? (++ps.first, 0) : 0)...};
}

template <class Index>
std::size_t compact_indices(std::false_type, Index*, const std::size_t n) noexcept {
return n;
}

// Move the valid indices to the front and return their number. The storage loop
// then has no data-dependent branch, which mispredicts on partly out-of-range data.
inline std::size_t compact_indices(std::true_type, optional_index* indices,
const std::size_t n) noexcept {
std::size_t m = 0;
for (std::size_t i = 0; i < n; ++i) {
const optional_index idx = indices[i];
indices[m] = idx;
m += is_valid(idx);
}
return m;
}

// general Nd treatment
template <class Index, class S, class A, class T, class... Ts>
void fill_n_nd(const std::size_t offset, S& storage, A& axes, const std::size_t vsize,
const T* values, Ts&&... ts) {
constexpr std::size_t buffer_size = 1ul << 14;
// 32 kB of indices; a larger buffer spills out of L1 and slows the passes below
constexpr std::size_t buffer_size = 1ul << 12;
Index indices[buffer_size];

/*
Expand Down Expand Up @@ -254,8 +274,13 @@ void fill_n_nd(const std::size_t offset, S& storage, A& axes, const std::size_t
const std::size_t n = (std::min)(buffer_size, vsize - start);
// fill buffer of indices...
fill_n_indices(indices, start, n, offset, storage, axes, values);
// ...drop invalid indices without branching, if no other arrays are passed...
const std::size_t m =
compact_indices(mp11::mp_bool<(sizeof...(Ts) == 0 &&
std::is_same<Index, optional_index>::value)>{},
indices, n);
// ...and fill corresponding storage cells
for (auto&& idx : make_span(indices, n))
for (auto&& idx : make_span(indices, m))
fill_n_storage(storage, idx, std::forward<Ts>(ts)...);
}
}
Expand Down
31 changes: 31 additions & 0 deletions include/boost/histogram/detail/linearize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ std::size_t linearize(Opts, optional_index& out, const std::size_t stride,
return size + u + o;
}

template <class Opts>
std::size_t linearize_branchless(Opts opts, std::size_t& out, const std::size_t stride,
const axis::index_type size,
const axis::index_type idx) {
return linearize(opts, out, stride, size, idx);
}

// Used by fill_n, where data that is partly out of range mispredicts the checks.
// A mask of all ones is also invalid_index, so an invalid input stays invalid.
template <class Opts>
std::size_t linearize_branchless(Opts, optional_index& out, const std::size_t stride,
const axis::index_type size,
const axis::index_type idx) {
constexpr bool u = Opts::test(axis::option::underflow);
constexpr bool o = Opts::test(axis::option::overflow);
assert(idx >= -1);
assert(idx < size + 1);
const bool valid = (u | (idx >= 0)) & (o | (idx < size)) & is_valid(out);
const auto mask = static_cast<std::size_t>(0) - static_cast<std::size_t>(!valid);
out.value = (out.value + static_cast<std::size_t>(idx) * stride) | mask;
return size + u + o;
}

template <class Index, class Axis, class Value>
std::size_t linearize(Index& out, const std::size_t stride, const Axis& ax,
const Value& v) {
Expand All @@ -59,6 +82,14 @@ std::size_t linearize(Index& out, const std::size_t stride, const Axis& ax,
return linearize(opts, out, stride, ax.size(), axis::traits::index(ax, v));
}

template <class Index, class Axis, class Value>
std::size_t linearize_branchless(Index& out, const std::size_t stride, const Axis& ax,
const Value& v) {
constexpr auto opts = axis::traits::get_options<Axis>{} &
(axis::option::underflow | axis::option::overflow);
return linearize_branchless(opts, out, stride, ax.size(), axis::traits::index(ax, v));
}

/**
Must be used when axis is potentially growing. Also works for non-growing axis.

Expand Down
Loading