Skip to content
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
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(XSIMD_ALGORITHM_BENCHMARKS
main.cpp
bench_math.cpp
bench_map.cpp
bench_of.cpp
)

add_executable(benchmark_xsimd_algorithm ${XSIMD_ALGORITHM_BENCHMARKS})
Expand Down
96 changes: 96 additions & 0 deletions benchmark/bench_of.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <algorithm>
#include <vector>

#include <benchmark/benchmark.h>

#include "xsimd_algorithm/stl/all_of.hpp"
#include "xsimd_algorithm/stl/any_of.hpp"
#include "xsimd_algorithm/stl/none_of.hpp"

#define BENCH_ARGS ->Arg(kSmall)->Arg(kMedium)->Arg(kLarge)

namespace
{
template <typename T>
std::vector<T> make_input(std::size_t n, T constant)
{
std::vector<T> v(n);
std::fill(v.begin(), v.end(), constant);
return v;
}

template <typename T>
void BM_std_all_of(benchmark::State& state)
{
const auto constant = T { 5 };

auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
for (auto _ : state)
{
auto result = std::all_of(src.begin(), src.end(), [constant](const auto& x) { return x == constant; });
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

template <typename T>
void BM_xsimd_all_of(benchmark::State& state)
{
const auto constant = T { 5 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
for (auto _ : state)
{
auto result = xsimd::all_of(src.begin(), src.end(), [constant](const auto& x) { return x == constant; });
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

template <typename T>
void BM_std_any_of(benchmark::State& state)
{
const auto constant = T { 5 };
const auto tested = T { 10 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
for (auto _ : state)
{
auto result = std::any_of(src.begin(), src.end(), [tested](const auto& x) { return x == tested; });
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

template <typename T>
void BM_xsimd_any_of(benchmark::State& state)
{
const auto constant = T { 5 };
const auto tested = T { 10 };
auto src = make_input<T>(static_cast<std::size_t>(state.range(0)), constant);
for (auto _ : state)
{
auto result = xsimd::any_of(src.begin(), src.end(), [tested](const auto& x) { return x == tested; });
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
}

static constexpr long long kSmall = 1LL << 12;
static constexpr long long kMedium = 1LL << 18;
static constexpr long long kLarge = 1LL << 22;

// all_of
BENCHMARK_TEMPLATE(BM_std_all_of, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_all_of, float)
BENCH_ARGS;
// any_of
BENCHMARK_TEMPLATE(BM_std_any_of, float)
BENCH_ARGS;
BENCHMARK_TEMPLATE(BM_xsimd_any_of, float)
BENCH_ARGS;

}
4 changes: 4 additions & 0 deletions include/xsimd_algorithm/algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#ifndef XSIMD_ALGORITHMS_HPP
#define XSIMD_ALGORITHMS_HPP

#include "xsimd_algorithm/stl/all_of.hpp"
#include "xsimd_algorithm/stl/any_of.hpp"
#include "xsimd_algorithm/stl/arange.hpp"
//#include "xsimd_algorithm/stl/none_of.hpp"
#include "xsimd_algorithm/stl/reduce.hpp"
#include "xsimd_algorithm/stl/transform.hpp"

Expand Down
87 changes: 87 additions & 0 deletions include/xsimd_algorithm/stl/all_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XSIMD_ALGORITHMS_ALL_OF_HPP
#define XSIMD_ALGORITHMS_ALL_OF_HPP

#include "xsimd/xsimd.hpp"

#include <iterator>

namespace xsimd
{
namespace detail
{
template <class Arch = default_arch, class ForwardIterator, class Predicate>
bool sequential_all_of(ForwardIterator first, ForwardIterator last, Predicate&& p) noexcept
{
for (; first != last; ++first)
{
if (!p(*first))
{
return false;
}
}
return true;
}
}

template <class Arch = default_arch, class ContiguousIterator, class Predicate>
bool all_of(ContiguousIterator first, ContiguousIterator last, Predicate&& p) noexcept
{
using value_type = typename std::decay<decltype(*first)>::type;
using batch_type = batch<value_type, Arch>;

const std::size_t size = static_cast<std::size_t>(std::distance(first, last));
constexpr std::size_t simd_size = batch_type::size;

// quick exit for small sizes
if (size < simd_size)
{
return detail::sequential_all_of(first, last, std::forward<Predicate>(p));
}

// prologue: process elements until the first aligned one
// compute alignment offsets
const auto* const ptr_begin = &(*first);
const std::size_t align_begin = xsimd::get_alignment_offset(ptr_begin, size, simd_size);
const std::size_t align_end = align_begin + ((size - align_begin) & ~(simd_size - 1));

const auto align_begin_it = std::next(first, align_begin);
const auto align_end_it = std::next(first, align_end);

const auto prologue_result = detail::sequential_all_of(first, align_begin_it, std::forward<Predicate>(p));
if (!prologue_result)
{
return false;
}
// main loop: process aligned elements
for (auto current = align_begin_it; current != align_end_it; std::advance(current, simd_size))
{
batch_type batch_val = batch_type::load_aligned(&(*current));
if (!xsimd::all(p(batch_val)))
{
return false;
}
}
// epilogue: process remaining elements
const auto epilogue_result = detail::sequential_all_of(align_end_it, last, std::forward<Predicate>(p));
if (!epilogue_result)
{
return false;
}

return true;
}

}

#endif // XSIMD_ALGORITHMS_ALL_OF_HPP
88 changes: 88 additions & 0 deletions include/xsimd_algorithm/stl/any_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XSIMD_ALGORITHMS_ANY_OF_HPP
#define XSIMD_ALGORITHMS_ANY_OF_HPP

#include "xsimd/xsimd.hpp"

#include <iterator>

namespace xsimd
{
namespace detail
{
template <class Arch = default_arch, class ForwardIterator, class Predicate>
bool sequential_any_of(ForwardIterator first, ForwardIterator last, Predicate&& p) noexcept
{
for (; first != last; ++first)
{
if (p(*first))
{
return true;
}
}
return false;
}
}

template <class Arch = default_arch, class ContiguousIterator, class Predicate>
bool any_of(ContiguousIterator first, ContiguousIterator last, Predicate&& p) noexcept
{
using value_type = typename std::decay<decltype(*first)>::type;
using batch_type = batch<value_type, Arch>;

const std::size_t size = static_cast<std::size_t>(std::distance(first, last));
constexpr std::size_t simd_size = batch_type::size;

// quick exit for small sizes
if (size < simd_size)
{
return detail::sequential_any_of(first, last, std::forward<Predicate>(p));
}

// prologue: process elements until the first aligned one
// compute alignment offsets
const auto* const ptr_begin = &(*first);
const std::size_t align_begin = xsimd::get_alignment_offset(ptr_begin, size, simd_size);
const std::size_t align_end = align_begin + ((size - align_begin) & ~(simd_size - 1));

const auto align_begin_it = std::next(first, align_begin);
const auto align_end_it = std::next(first, align_end);

const auto prologue_result = detail::sequential_any_of(first, align_begin_it, std::forward<Predicate>(p));
if (prologue_result)
{
return true;
}

// main loop: process aligned elements
for (auto current = align_begin_it; current != align_end_it; std::advance(current, simd_size))
{
batch_type batch_val = batch_type::load_aligned(&(*current));
if (xsimd::any(p(batch_val)))
{
return true;
}
}
// epilogue: process remaining elements
const auto epilogue_result = detail::sequential_any_of(align_end_it, last, std::forward<Predicate>(p));
if (epilogue_result)
{
return true;
}

return false;
}

}

#endif // XSIMD_ALGORITHMS_ANY_OF_HPP
31 changes: 31 additions & 0 deletions include/xsimd_algorithm/stl/none_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XSIMD_ALGORITHMS_NONE_OF_HPP
#define XSIMD_ALGORITHMS_NONE_OF_HPP

#include "xsimd/xsimd.hpp"
#include "xsimd_algorithm/stl/any_of.hpp"

#include <iterator>

namespace xsimd
{

template <class Arch = default_arch, class ContiguousIterator, class Predicate>
bool none_of(ContiguousIterator first, ContiguousIterator last, Predicate&& p) noexcept
{
return !any_of<Arch>(first, last, std::forward<Predicate>(p));
}

}

#endif // XSIMD_ALGORITHMS_NONE_OF_HPP
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ endif()

set(XSIMD_ALGORITHM_TESTS
main.cpp
test_all_of.cpp
test_any_of.cpp
test_none_of.cpp
test_arange.cpp
test_map.cpp
test_iterator.cpp
Expand Down
Loading
Loading