1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <__algorithm/min.h>
10 #include <__algorithm/pstl_backends/cpu_backends/libdispatch.h>
11 #include <__config>
12 #include <dispatch/dispatch.h>
13 
14 _LIBCPP_BEGIN_NAMESPACE_STD
15 
16 namespace __par_backend::inline __libdispatch {
17 
18 void __dispatch_apply(size_t chunk_count, void* context, void (*func)(void* context, size_t chunk)) noexcept {
19   ::dispatch_apply_f(chunk_count, DISPATCH_APPLY_AUTO, context, func);
20 }
21 
22 __chunk_partitions __partition_chunks(ptrdiff_t element_count) noexcept {
23   __chunk_partitions partitions;
24   partitions.__chunk_count_      = std::max<ptrdiff_t>(1, element_count / 256);
25   partitions.__chunk_size_       = element_count / partitions.__chunk_count_;
26   partitions.__first_chunk_size_ = element_count - (partitions.__chunk_count_ - 1) * partitions.__chunk_size_;
27   if (partitions.__chunk_count_ == 0 && element_count > 0)
28     partitions.__chunk_count_ = 1;
29   return partitions;
30 }
31 
32 // NOLINTNEXTLINE(llvm-namespace-comment) // This is https://llvm.org/PR56804
33 } // namespace __par_backend::inline __libdispatch
34 
35 _LIBCPP_END_NAMESPACE_STD
36