diff --git a/include/boost/histogram/axis/regular.hpp b/include/boost/histogram/axis/regular.hpp index a5c7c124..2c098334 100644 --- a/include/boost/histogram/axis/regular.hpp +++ b/include/boost/histogram/axis/regular.hpp @@ -314,16 +314,18 @@ class regular : public iterator_mixin(z * size()); } } else { - if (z < 1) { - if (z >= 0) - return static_cast(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(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(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. diff --git a/include/boost/histogram/detail/fill_n.hpp b/include/boost/histogram/detail/fill_n.hpp index a282a403..6baf214a 100644 --- a/include/boost/histogram/detail/fill_n.hpp +++ b/include/boost/histogram/detail/fill_n.hpp @@ -105,7 +105,8 @@ struct index_visitor { template void linearize_element(std::false_type, pointer it, const T& x) const { // no axis is growing - linearize(*it, stride_, axis_, try_cast(x)); + linearize_branchless(*it, stride_, axis_, + try_cast(x)); } template @@ -215,11 +216,30 @@ void fill_n_storage(S& s, const Index idx, weight_type&& w, Ts&&... ps) noexc (void)std::initializer_list{(ps.second ? (++ps.first, 0) : 0)...}; } +template +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 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]; /* @@ -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::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)...); } } diff --git a/include/boost/histogram/detail/linearize.hpp b/include/boost/histogram/detail/linearize.hpp index bbee5a2a..091a1146 100644 --- a/include/boost/histogram/detail/linearize.hpp +++ b/include/boost/histogram/detail/linearize.hpp @@ -50,6 +50,29 @@ std::size_t linearize(Opts, optional_index& out, const std::size_t stride, return size + u + o; } +template +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 +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(0) - static_cast(!valid); + out.value = (out.value + static_cast(idx) * stride) | mask; + return size + u + o; +} + template std::size_t linearize(Index& out, const std::size_t stride, const Axis& ax, const Value& v) { @@ -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 +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::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.