├── .arcconfig ├── .clang-format ├── CMakeLists.txt ├── CREDITS.txt ├── LICENSE.txt ├── README.md ├── cmake └── ParallelSTLConfig.cmake.in ├── docs └── ReleaseNotes.rst ├── include ├── __pstl_algorithm ├── __pstl_config_site.in ├── __pstl_execution ├── __pstl_memory ├── __pstl_numeric └── pstl │ └── internal │ ├── algorithm_fwd.h │ ├── algorithm_impl.h │ ├── execution_defs.h │ ├── execution_impl.h │ ├── glue_algorithm_defs.h │ ├── glue_algorithm_impl.h │ ├── glue_execution_defs.h │ ├── glue_memory_defs.h │ ├── glue_memory_impl.h │ ├── glue_numeric_defs.h │ ├── glue_numeric_impl.h │ ├── memory_impl.h │ ├── numeric_fwd.h │ ├── numeric_impl.h │ ├── parallel_backend.h │ ├── parallel_backend_serial.h │ ├── parallel_backend_tbb.h │ ├── parallel_backend_utils.h │ ├── parallel_impl.h │ ├── pstl_config.h │ ├── unseq_backend_simd.h │ └── utils.h └── test ├── CMakeLists.txt ├── pstl ├── header_inclusion_order_algorithm_0.pass.cpp ├── header_inclusion_order_algorithm_1.pass.cpp ├── header_inclusion_order_memory_0.pass.cpp ├── header_inclusion_order_memory_1.pass.cpp ├── header_inclusion_order_numeric_0.pass.cpp ├── header_inclusion_order_numeric_1.pass.cpp └── version.pass.cpp ├── std ├── algorithms │ ├── alg.merge │ │ ├── inplace_merge.pass.cpp │ │ └── merge.pass.cpp │ ├── alg.modifying.operations │ │ ├── alg.copy │ │ │ └── copy_if.pass.cpp │ │ ├── alg.partitions │ │ │ ├── is_partitioned.pass.cpp │ │ │ ├── partition.pass.cpp │ │ │ └── partition_copy.pass.cpp │ │ ├── alg.reverse │ │ │ ├── reverse.pass.cpp │ │ │ └── reverse_copy.pass.cpp │ │ ├── copy_move.pass.cpp │ │ ├── fill.pass.cpp │ │ ├── generate.pass.cpp │ │ ├── remove.pass.cpp │ │ ├── remove_copy.pass.cpp │ │ ├── replace.pass.cpp │ │ ├── replace_copy.pass.cpp │ │ ├── rotate.pass.cpp │ │ ├── rotate_copy.pass.cpp │ │ ├── swap_ranges.pass.cpp │ │ ├── transform_binary.pass.cpp │ │ ├── transform_unary.pass.cpp │ │ ├── unique.pass.cpp │ │ └── unique_copy_equal.pass.cpp │ ├── alg.nonmodifying │ │ ├── adjacent_find.pass.cpp │ │ ├── all_of.pass.cpp │ │ ├── any_of.pass.cpp │ │ ├── count.pass.cpp │ │ ├── equal.pass.cpp │ │ ├── find.pass.cpp │ │ ├── find_end.pass.cpp │ │ ├── find_first_of.pass.cpp │ │ ├── find_if.pass.cpp │ │ ├── for_each.pass.cpp │ │ ├── mismatch.pass.cpp │ │ ├── none_of.pass.cpp │ │ ├── nth_element.pass.cpp │ │ └── search_n.pass.cpp │ └── alg.sorting │ │ ├── alg.heap.operations │ │ └── is_heap.pass.cpp │ │ ├── alg.lex.comparison │ │ └── lexicographical_compare.pass.cpp │ │ ├── alg.min.max │ │ └── minmax_element.pass.cpp │ │ ├── alg.set.operations │ │ ├── includes.pass.cpp │ │ └── set.pass.cpp │ │ ├── is_sorted.pass.cpp │ │ ├── partial_sort.pass.cpp │ │ ├── partial_sort_copy.pass.cpp │ │ └── sort.pass.cpp ├── lit.local.cfg ├── numerics │ └── numeric.ops │ │ ├── adjacent_difference.pass.cpp │ │ ├── reduce.pass.cpp │ │ ├── scan.pass.cpp │ │ ├── transform_reduce.pass.cpp │ │ └── transform_scan.pass.cpp └── utilities │ └── memory │ └── specialized.algorithms │ ├── uninitialized_construct.pass.cpp │ ├── uninitialized_copy_move.pass.cpp │ └── uninitialized_fill_destroy.pass.cpp └── support ├── pstl_test_config.h ├── stdlib ├── algorithm ├── execution ├── memory └── numeric └── utils.h /.arcconfig: -------------------------------------------------------------------------------- 1 | { 2 | "repository.callsign" : "PSTL", 3 | "conduit_uri" : "https://reviews.llvm.org/" 4 | } 5 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | 3 | Language: Cpp 4 | Standard: Cpp11 5 | 6 | IndentWidth: 4 7 | ColumnLimit: 120 8 | 9 | AlwaysBreakTemplateDeclarations: true 10 | AlwaysBreakAfterReturnType: All 11 | PointerAlignment: Left 12 | AllowShortIfStatementsOnASingleLine: false 13 | BreakBeforeBraces: Allman 14 | 15 | # Disable formatting options which may break tests. 16 | SortIncludes: false 17 | ReflowComments: false 18 | 19 | # Indent preprocessor directives 20 | IndentPPDirectives: AfterHash 21 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #===-- CMakeLists.txt ----------------------------------------------------===## 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 | cmake_minimum_required(VERSION 3.4.3) 9 | 10 | set(PARALLELSTL_VERSION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/include/pstl/internal/pstl_config.h") 11 | file(STRINGS "${PARALLELSTL_VERSION_FILE}" PARALLELSTL_VERSION_SOURCE REGEX "#define _PSTL_VERSION .*$") 12 | string(REGEX REPLACE "#define _PSTL_VERSION (.*)$" "\\1" PARALLELSTL_VERSION_SOURCE "${PARALLELSTL_VERSION_SOURCE}") 13 | math(EXPR VERSION_MAJOR "(${PARALLELSTL_VERSION_SOURCE} / 1000)") 14 | math(EXPR VERSION_MINOR "((${PARALLELSTL_VERSION_SOURCE} % 1000) / 10)") 15 | math(EXPR VERSION_PATCH "(${PARALLELSTL_VERSION_SOURCE} % 10)") 16 | 17 | project(ParallelSTL VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} LANGUAGES CXX) 18 | 19 | set(PSTL_PARALLEL_BACKEND "serial" CACHE STRING "Threading backend to use. Valid choices are 'serial' and 'tbb'. The default is 'serial'.") 20 | set(PSTL_HIDE_FROM_ABI_PER_TU OFF CACHE BOOL "Whether to constrain ABI-unstable symbols to each translation unit (basically, mark them with C's static keyword).") 21 | set(_PSTL_HIDE_FROM_ABI_PER_TU ${PSTL_HIDE_FROM_ABI_PER_TU}) # For __pstl_config_site 22 | 23 | if (NOT TBB_DIR) 24 | get_filename_component(PSTL_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) 25 | string(REPLACE pstl tbb TBB_DIR_NAME ${PSTL_DIR_NAME}) 26 | if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake") 27 | get_filename_component(TBB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${TBB_DIR_NAME}/cmake" ABSOLUTE) 28 | endif() 29 | endif() 30 | 31 | ############################################################################### 32 | # Setup the ParallelSTL library target 33 | ############################################################################### 34 | add_library(ParallelSTL INTERFACE) 35 | add_library(pstl::ParallelSTL ALIAS ParallelSTL) 36 | target_compile_features(ParallelSTL INTERFACE cxx_std_17) 37 | 38 | if (PSTL_PARALLEL_BACKEND STREQUAL "serial") 39 | message(STATUS "Parallel STL uses the serial backend") 40 | set(_PSTL_PAR_BACKEND_SERIAL ON) 41 | elseif (PSTL_PARALLEL_BACKEND STREQUAL "tbb") 42 | find_package(TBB 2018 REQUIRED tbb OPTIONAL_COMPONENTS tbbmalloc) 43 | message(STATUS "Parallel STL uses TBB ${TBB_VERSION} (interface version: ${TBB_INTERFACE_VERSION})") 44 | target_link_libraries(ParallelSTL INTERFACE TBB::tbb) 45 | set(_PSTL_PAR_BACKEND_TBB ON) 46 | else() 47 | message(FATAL_ERROR "Requested unknown Parallel STL backend '${PSTL_PARALLEL_BACKEND}'.") 48 | endif() 49 | 50 | set(PSTL_GENERATED_HEADERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated_headers") 51 | set(PSTL_CONFIG_SITE_PATH "${PSTL_GENERATED_HEADERS_DIR}/__pstl_config_site") 52 | configure_file("include/__pstl_config_site.in" 53 | "${PSTL_CONFIG_SITE_PATH}" 54 | @ONLY) 55 | 56 | target_include_directories(ParallelSTL 57 | INTERFACE 58 | $ 59 | $ 60 | $) 61 | 62 | ############################################################################### 63 | # Setup tests 64 | ############################################################################### 65 | enable_testing() 66 | add_subdirectory(test) 67 | 68 | ############################################################################### 69 | # Install the target and the associated CMake files 70 | ############################################################################### 71 | include(CMakePackageConfigHelpers) 72 | write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake" 73 | COMPATIBILITY ExactVersion) 74 | 75 | configure_file(cmake/ParallelSTLConfig.cmake.in 76 | "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake" 77 | @ONLY) 78 | 79 | install(TARGETS ParallelSTL 80 | EXPORT ParallelSTLTargets) 81 | install(EXPORT ParallelSTLTargets 82 | FILE ParallelSTLTargets.cmake 83 | NAMESPACE pstl:: 84 | DESTINATION lib/cmake/ParallelSTL) 85 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfig.cmake" 86 | "${CMAKE_CURRENT_BINARY_DIR}/ParallelSTLConfigVersion.cmake" 87 | DESTINATION lib/cmake/ParallelSTL) 88 | install(DIRECTORY include/ 89 | DESTINATION include) 90 | install(FILES "${PSTL_CONFIG_SITE_PATH}" 91 | DESTINATION include) 92 | 93 | add_custom_target(install-pstl 94 | COMMAND "${CMAKE_COMMAND}" -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" -DCOMPONENT=ParallelSTL) 95 | -------------------------------------------------------------------------------- /CREDITS.txt: -------------------------------------------------------------------------------- 1 | This file is a partial list of people who have contributed to the LLVM/pstl 2 | (Parallel STL) project. If you have contributed a patch or made some other 3 | contribution to LLVM/pstl, please submit a patch to this file to add yourself, 4 | and it will be done! 5 | 6 | The list is sorted by surname and formatted to allow easy grepping and 7 | beautification by scripts. The fields are: name (N), email (E), web-address 8 | (W), PGP key ID and fingerprint (P), description (D), and snail-mail address 9 | (S). 10 | 11 | N: Intel Corporation 12 | W: http://www.intel.com 13 | D: Created the initial implementation. 14 | 15 | N: Thomas Rodgers 16 | E: trodgers@redhat.com 17 | D: Identifier name transformation for inclusion in a Standard C++ library. 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Parallel STL 2 | 3 | Parallel STL is an implementation of the C++ standard library algorithms with support for execution policies, 4 | as specified in ISO/IEC 14882:2017 standard, commonly called C++17. The implementation also supports the unsequenced 5 | execution policy specified in Parallelism TS version 2 and proposed for the next version of the C++ standard in the 6 | C++ working group paper [P1001](https://wg21.link/p1001). 7 | Parallel STL offers efficient support for both parallel and vectorized execution of algorithms. For sequential 8 | execution, it relies on an available implementation of the C++ standard library. 9 | 10 | ## Prerequisites 11 | 12 | To use Parallel STL, you must have the following software installed: 13 | * C++ compiler with: 14 | * Support for C++11 15 | * Support for OpenMP* 4.0 SIMD constructs 16 | * Threading Building Blocks (TBB) which is available for download at https://github.com/01org/tbb/ 17 | 18 | ## Known issues and limitations 19 | 20 | * `unseq` and `par_unseq` policies only have effect with compilers that support `#pragma omp simd` or `#pragma simd`. 21 | * Parallel and vector execution is only supported for the algorithms if random access iterators are provided, 22 | while for other iterator types the execution will remain serial. 23 | * The following algorithms do not allow efficient SIMD execution: `includes`, `inplace_merge`, `merge`, `nth_element`, 24 | `partial_sort`, `partial_sort_copy`, `set_difference`, `set_intersection`, `set_symmetric_difference`, `set_union`, 25 | `sort`, `stable_partition`, `stable_sort`, `unique`. 26 | * The initial value type for `exclusive_scan`, `inclusive_scan`, `transform_exclusive_scan`, `transform_inclusive_scan` 27 | shall be DefaultConstructible. A default constructed-instance of the initial value type shall be the identity element 28 | for the specified binary operation. 29 | * For `max_element`, `min_element`, `minmax_element`, `partial_sort`, `partial_sort_copy`, `sort`, `stable_sort` 30 | the dereferenced value type of the provided iterators shall be DefaultConstructible. 31 | * For `remove`, `remove_if`, `unique` the dereferenced value type of the provided iterators shall be MoveConstructible. 32 | * The following algorithms require additional O(n) memory space for parallel execution: `copy_if`, `inplace_merge`, 33 | `partial_sort`, `partial_sort_copy`, `partition_copy`, `remove`, `remove_if`, `rotate`, `sort`, `stable_sort`, 34 | `unique`, `unique_copy`. 35 | 36 | -------------------------------------------------------------------------------- /cmake/ParallelSTLConfig.cmake.in: -------------------------------------------------------------------------------- 1 | #===-- ParallelSTLConfig.cmake.in ----------------------------------------===## 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(CMakeFindDependencyMacro) 10 | 11 | set(PSTL_PARALLEL_BACKEND "@PSTL_PARALLEL_BACKEND@") 12 | 13 | if (PSTL_PARALLEL_BACKEND STREQUAL "tbb") 14 | find_dependency(TBB REQUIRED tbb) 15 | endif() 16 | 17 | if (NOT TARGET pstl::ParallelSTL) 18 | include("${CMAKE_CURRENT_LIST_DIR}/ParallelSTLTargets.cmake") 19 | endif() 20 | -------------------------------------------------------------------------------- /docs/ReleaseNotes.rst: -------------------------------------------------------------------------------- 1 | ======================================= 2 | PSTL 10.0.0 (In-Progress) Release Notes 3 | ======================================= 4 | 5 | .. contents:: 6 | :local: 7 | :depth: 2 8 | 9 | Written by the `PSTL Team `_ 10 | 11 | .. warning:: 12 | 13 | These are in-progress notes for the upcoming pstl 10 release. 14 | Release notes for previous releases can be found on 15 | `the Download Page `_. 16 | 17 | Introduction 18 | ============ 19 | 20 | This document contains the release notes for the PSTL parallel algorithms 21 | library, part of the LLVM Compiler Infrastructure, release 10.0.0. Here we 22 | describe the status of the library in some detail, including major improvements 23 | from the previous release and new feature work. For the general LLVM release 24 | notes, see `the LLVM documentation `_. 25 | All LLVM releases may be downloaded from the `LLVM releases web site 26 | `_. 27 | 28 | Note that if you are reading this file from a source checkout or the main PSTL 29 | web page, this document applies to the *next* release, not the current one. 30 | To see the release notes for a specific release, please see the `releases 31 | page `_. 32 | 33 | What's New in PSTL 10.0.0? 34 | ========================== 35 | 36 | New Features 37 | ------------ 38 | 39 | API Changes 40 | ----------- 41 | -------------------------------------------------------------------------------- /include/__pstl_algorithm: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef __PSTL_ALGORITHM 11 | #define __PSTL_ALGORITHM 12 | 13 | #include 14 | 15 | #endif /* __PSTL_ALGORITHM */ 16 | -------------------------------------------------------------------------------- /include/__pstl_config_site.in: -------------------------------------------------------------------------------- 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 | #ifndef __PSTL_CONFIG_SITE 10 | #define __PSTL_CONFIG_SITE 11 | 12 | #cmakedefine _PSTL_PAR_BACKEND_SERIAL 13 | #cmakedefine _PSTL_PAR_BACKEND_TBB 14 | #cmakedefine _PSTL_HIDE_FROM_ABI_PER_TU 15 | 16 | #endif // __PSTL_CONFIG_SITE 17 | -------------------------------------------------------------------------------- /include/__pstl_execution: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef __PSTL_EXECUTION 11 | #define __PSTL_EXECUTION 12 | 13 | #include 14 | 15 | #endif /* __PSTL_EXECUTION */ 16 | -------------------------------------------------------------------------------- /include/__pstl_memory: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef __PSTL_MEMORY 11 | #define __PSTL_MEMORY 12 | 13 | #include 14 | 15 | #endif /* __PSTL_MEMORY */ 16 | -------------------------------------------------------------------------------- /include/__pstl_numeric: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef __PSTL_NUMERIC 11 | #define __PSTL_NUMERIC 12 | 13 | #include 14 | 15 | #endif /* __PSTL_NUMERIC */ 16 | -------------------------------------------------------------------------------- /include/pstl/internal/execution_defs.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_EXECUTION_POLICY_DEFS_H 11 | #define _PSTL_EXECUTION_POLICY_DEFS_H 12 | 13 | #include 14 | 15 | #include "pstl_config.h" 16 | 17 | _PSTL_HIDE_FROM_ABI_PUSH 18 | 19 | namespace __pstl 20 | { 21 | namespace execution 22 | { 23 | inline namespace v1 24 | { 25 | 26 | // 2.4, Sequential execution policy 27 | class sequenced_policy 28 | { 29 | public: 30 | // For internal use only 31 | static constexpr std::false_type 32 | __allow_unsequenced() 33 | { 34 | return std::false_type{}; 35 | } 36 | static constexpr std::false_type 37 | __allow_vector() 38 | { 39 | return std::false_type{}; 40 | } 41 | static constexpr std::false_type 42 | __allow_parallel() 43 | { 44 | return std::false_type{}; 45 | } 46 | }; 47 | 48 | // 2.5, Parallel execution policy 49 | class parallel_policy 50 | { 51 | public: 52 | // For internal use only 53 | static constexpr std::false_type 54 | __allow_unsequenced() 55 | { 56 | return std::false_type{}; 57 | } 58 | static constexpr std::false_type 59 | __allow_vector() 60 | { 61 | return std::false_type{}; 62 | } 63 | static constexpr std::true_type 64 | __allow_parallel() 65 | { 66 | return std::true_type{}; 67 | } 68 | }; 69 | 70 | // 2.6, Parallel+Vector execution policy 71 | class parallel_unsequenced_policy 72 | { 73 | public: 74 | // For internal use only 75 | static constexpr std::true_type 76 | __allow_unsequenced() 77 | { 78 | return std::true_type{}; 79 | } 80 | static constexpr std::true_type 81 | __allow_vector() 82 | { 83 | return std::true_type{}; 84 | } 85 | static constexpr std::true_type 86 | __allow_parallel() 87 | { 88 | return std::true_type{}; 89 | } 90 | }; 91 | 92 | class unsequenced_policy 93 | { 94 | public: 95 | // For internal use only 96 | static constexpr std::true_type 97 | __allow_unsequenced() 98 | { 99 | return std::true_type{}; 100 | } 101 | static constexpr std::true_type 102 | __allow_vector() 103 | { 104 | return std::true_type{}; 105 | } 106 | static constexpr std::false_type 107 | __allow_parallel() 108 | { 109 | return std::false_type{}; 110 | } 111 | }; 112 | 113 | // 2.8, Execution policy objects 114 | constexpr sequenced_policy seq{}; 115 | constexpr parallel_policy par{}; 116 | constexpr parallel_unsequenced_policy par_unseq{}; 117 | constexpr unsequenced_policy unseq{}; 118 | 119 | // 2.3, Execution policy type trait 120 | template 121 | struct is_execution_policy : std::false_type 122 | { 123 | }; 124 | 125 | template <> 126 | struct is_execution_policy<__pstl::execution::sequenced_policy> : std::true_type 127 | { 128 | }; 129 | template <> 130 | struct is_execution_policy<__pstl::execution::parallel_policy> : std::true_type 131 | { 132 | }; 133 | template <> 134 | struct is_execution_policy<__pstl::execution::parallel_unsequenced_policy> : std::true_type 135 | { 136 | }; 137 | template <> 138 | struct is_execution_policy<__pstl::execution::unsequenced_policy> : std::true_type 139 | { 140 | }; 141 | 142 | #if _PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT 143 | template 144 | constexpr bool is_execution_policy_v = __pstl::execution::is_execution_policy::value; 145 | #endif 146 | 147 | } // namespace v1 148 | } // namespace execution 149 | 150 | namespace __internal 151 | { 152 | template 153 | using __enable_if_execution_policy = 154 | typename std::enable_if<__pstl::execution::is_execution_policy::type>::value, 155 | T>::type; 156 | } // namespace __internal 157 | 158 | } // namespace __pstl 159 | 160 | _PSTL_HIDE_FROM_ABI_POP 161 | 162 | #endif /* _PSTL_EXECUTION_POLICY_DEFS_H */ 163 | -------------------------------------------------------------------------------- /include/pstl/internal/execution_impl.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_EXECUTION_IMPL_H 11 | #define _PSTL_EXECUTION_IMPL_H 12 | 13 | #include 14 | #include 15 | 16 | #include "pstl_config.h" 17 | #include "execution_defs.h" 18 | 19 | _PSTL_HIDE_FROM_ABI_PUSH 20 | 21 | namespace __pstl 22 | { 23 | namespace __internal 24 | { 25 | 26 | using namespace __pstl::execution; 27 | 28 | /* predicate */ 29 | 30 | template 31 | std::false_type __lazy_and(_Tp, std::false_type) 32 | { 33 | return std::false_type{}; 34 | } 35 | 36 | template 37 | inline _Tp 38 | __lazy_and(_Tp __a, std::true_type) 39 | { 40 | return __a; 41 | } 42 | 43 | template 44 | std::true_type __lazy_or(_Tp, std::true_type) 45 | { 46 | return std::true_type{}; 47 | } 48 | 49 | template 50 | inline _Tp 51 | __lazy_or(_Tp __a, std::false_type) 52 | { 53 | return __a; 54 | } 55 | 56 | /* iterator */ 57 | template 58 | struct __is_random_access_iterator 59 | { 60 | static constexpr bool value = __internal::__is_random_access_iterator<_IteratorType>::value && 61 | __internal::__is_random_access_iterator<_OtherIteratorTypes...>::value; 62 | typedef std::integral_constant type; 63 | }; 64 | 65 | template 66 | struct __is_random_access_iterator<_IteratorType> 67 | : std::is_same::iterator_category, std::random_access_iterator_tag> 68 | { 69 | }; 70 | 71 | /* policy */ 72 | template 73 | struct __policy_traits 74 | { 75 | }; 76 | 77 | template <> 78 | struct __policy_traits 79 | { 80 | typedef std::false_type allow_parallel; 81 | typedef std::false_type allow_unsequenced; 82 | typedef std::false_type allow_vector; 83 | }; 84 | 85 | template <> 86 | struct __policy_traits 87 | { 88 | typedef std::false_type allow_parallel; 89 | typedef std::true_type allow_unsequenced; 90 | typedef std::true_type allow_vector; 91 | }; 92 | 93 | template <> 94 | struct __policy_traits 95 | { 96 | typedef std::true_type allow_parallel; 97 | typedef std::false_type allow_unsequenced; 98 | typedef std::false_type allow_vector; 99 | }; 100 | 101 | template <> 102 | struct __policy_traits 103 | { 104 | typedef std::true_type allow_parallel; 105 | typedef std::true_type allow_unsequenced; 106 | typedef std::true_type allow_vector; 107 | }; 108 | 109 | template 110 | using __collector_t = 111 | typename __internal::__policy_traits::type>::__collector_type; 112 | 113 | template 114 | using __allow_vector = 115 | typename __internal::__policy_traits::type>::__allow_vector; 116 | 117 | template 118 | using __allow_unsequenced = 119 | typename __internal::__policy_traits::type>::__allow_unsequenced; 120 | 121 | template 122 | using __allow_parallel = 123 | typename __internal::__policy_traits::type>::__allow_parallel; 124 | 125 | template 126 | auto 127 | __is_vectorization_preferred(_ExecutionPolicy&& __exec) 128 | -> decltype(__internal::__lazy_and(__exec.__allow_vector(), 129 | typename __internal::__is_random_access_iterator<_IteratorTypes...>::type())) 130 | { 131 | return __internal::__lazy_and(__exec.__allow_vector(), 132 | typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()); 133 | } 134 | 135 | template 136 | auto 137 | __is_parallelization_preferred(_ExecutionPolicy&& __exec) 138 | -> decltype(__internal::__lazy_and(__exec.__allow_parallel(), 139 | typename __internal::__is_random_access_iterator<_IteratorTypes...>::type())) 140 | { 141 | return __internal::__lazy_and(__exec.__allow_parallel(), 142 | typename __internal::__is_random_access_iterator<_IteratorTypes...>::type()); 143 | } 144 | 145 | template 146 | struct __prefer_unsequenced_tag 147 | { 148 | static constexpr bool value = __internal::__allow_unsequenced::value && 149 | __internal::__is_random_access_iterator<_IteratorTypes...>::value; 150 | typedef std::integral_constant type; 151 | }; 152 | 153 | template 154 | struct __prefer_parallel_tag 155 | { 156 | static constexpr bool value = __internal::__allow_parallel::value && 157 | __internal::__is_random_access_iterator<_IteratorTypes...>::value; 158 | typedef std::integral_constant type; 159 | }; 160 | 161 | } // namespace __internal 162 | } // namespace __pstl 163 | 164 | _PSTL_HIDE_FROM_ABI_POP 165 | 166 | #endif /* _PSTL_EXECUTION_IMPL_H */ 167 | -------------------------------------------------------------------------------- /include/pstl/internal/glue_execution_defs.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_GLUE_EXECUTION_DEFS_H 11 | #define _PSTL_GLUE_EXECUTION_DEFS_H 12 | 13 | #include 14 | 15 | #include "execution_defs.h" 16 | #include "pstl_config.h" 17 | 18 | namespace std 19 | { 20 | // Type trait 21 | using __pstl::execution::is_execution_policy; 22 | #if _PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT 23 | # if __INTEL_COMPILER 24 | template 25 | constexpr bool is_execution_policy_v = is_execution_policy::value; 26 | # else 27 | using __pstl::execution::is_execution_policy_v; 28 | # endif 29 | #endif 30 | 31 | namespace execution 32 | { 33 | // Standard C++ policy classes 34 | using __pstl::execution::parallel_policy; 35 | using __pstl::execution::parallel_unsequenced_policy; 36 | using __pstl::execution::sequenced_policy; 37 | 38 | // Standard predefined policy instances 39 | using __pstl::execution::par; 40 | using __pstl::execution::par_unseq; 41 | using __pstl::execution::seq; 42 | 43 | // Implementation-defined names 44 | // Unsequenced policy is not yet standard, but for consistency 45 | // we include it into namespace std::execution as well 46 | using __pstl::execution::unseq; 47 | using __pstl::execution::unsequenced_policy; 48 | } // namespace execution 49 | } // namespace std 50 | 51 | #include "algorithm_impl.h" 52 | #include "numeric_impl.h" 53 | #include "parallel_backend.h" 54 | 55 | #endif /* _PSTL_GLUE_EXECUTION_DEFS_H */ 56 | -------------------------------------------------------------------------------- /include/pstl/internal/glue_memory_defs.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_GLUE_MEMORY_DEFS_H 11 | #define _PSTL_GLUE_MEMORY_DEFS_H 12 | 13 | #include "execution_defs.h" 14 | #include "pstl_config.h" 15 | 16 | _PSTL_HIDE_FROM_ABI_PUSH 17 | 18 | namespace std 19 | { 20 | 21 | // [uninitialized.copy] 22 | 23 | template 24 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 25 | uninitialized_copy(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result); 26 | 27 | template 28 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 29 | uninitialized_copy_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result); 30 | 31 | // [uninitialized.move] 32 | 33 | template 34 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 35 | uninitialized_move(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result); 36 | 37 | template 38 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 39 | uninitialized_move_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result); 40 | 41 | // [uninitialized.fill] 42 | 43 | template 44 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> 45 | uninitialized_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value); 46 | 47 | template 48 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 49 | uninitialized_fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, const _Tp& __value); 50 | 51 | // [specialized.destroy] 52 | 53 | template 54 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> 55 | destroy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); 56 | 57 | template 58 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 59 | destroy_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n); 60 | 61 | // [uninitialized.construct.default] 62 | 63 | template 64 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> 65 | uninitialized_default_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); 66 | 67 | template 68 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 69 | uninitialized_default_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n); 70 | 71 | // [uninitialized.construct.value] 72 | 73 | template 74 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> 75 | uninitialized_value_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last); 76 | 77 | template 78 | __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator> 79 | uninitialized_value_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n); 80 | 81 | } // namespace std 82 | 83 | _PSTL_HIDE_FROM_ABI_POP 84 | 85 | #endif /* _PSTL_GLUE_MEMORY_DEFS_H */ 86 | -------------------------------------------------------------------------------- /include/pstl/internal/memory_impl.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_MEMORY_IMPL_H 11 | #define _PSTL_MEMORY_IMPL_H 12 | 13 | #include 14 | 15 | #include "pstl_config.h" 16 | #include "unseq_backend_simd.h" 17 | 18 | _PSTL_HIDE_FROM_ABI_PUSH 19 | 20 | namespace __pstl 21 | { 22 | namespace __internal 23 | { 24 | 25 | //------------------------------------------------------------------------ 26 | // uninitialized_move 27 | //------------------------------------------------------------------------ 28 | 29 | template 30 | _OutputIterator 31 | __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, 32 | /*vector=*/std::false_type) noexcept 33 | { 34 | typedef typename std::iterator_traits<_OutputIterator>::value_type _ValueType2; 35 | for (; __first != __last; ++__first, ++__result) 36 | { 37 | ::new (std::addressof(*__result)) _ValueType2(std::move(*__first)); 38 | } 39 | return __result; 40 | } 41 | 42 | template 43 | _OutputIterator 44 | __brick_uninitialized_move(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, 45 | /*vector=*/std::true_type) noexcept 46 | { 47 | typedef typename std::iterator_traits<_OutputIterator>::value_type __ValueType2; 48 | typedef typename std::iterator_traits<_ForwardIterator>::reference _ReferenceType1; 49 | typedef typename std::iterator_traits<_OutputIterator>::reference _ReferenceType2; 50 | 51 | return __unseq_backend::__simd_walk_2( 52 | __first, __last - __first, __result, 53 | [](_ReferenceType1 __x, _ReferenceType2 __y) { ::new (std::addressof(__y)) __ValueType2(std::move(__x)); }); 54 | } 55 | 56 | } // namespace __internal 57 | } // namespace __pstl 58 | 59 | _PSTL_HIDE_FROM_ABI_POP 60 | 61 | #endif /* _PSTL_MEMORY_IMPL_H */ 62 | -------------------------------------------------------------------------------- /include/pstl/internal/parallel_backend.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_PARALLEL_BACKEND_H 11 | #define _PSTL_PARALLEL_BACKEND_H 12 | 13 | #include "pstl_config.h" 14 | 15 | #if defined(_PSTL_PAR_BACKEND_SERIAL) 16 | # include "parallel_backend_serial.h" 17 | namespace __pstl 18 | { 19 | namespace __par_backend = __serial_backend; 20 | } 21 | #elif defined(_PSTL_PAR_BACKEND_TBB) 22 | # include "parallel_backend_tbb.h" 23 | namespace __pstl 24 | { 25 | namespace __par_backend = __tbb_backend; 26 | } 27 | #else 28 | _PSTL_PRAGMA_MESSAGE("Parallel backend was not specified"); 29 | #endif 30 | 31 | #endif /* _PSTL_PARALLEL_BACKEND_H */ 32 | -------------------------------------------------------------------------------- /include/pstl/internal/parallel_backend_serial.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_PARALLEL_BACKEND_SERIAL_H 11 | #define _PSTL_PARALLEL_BACKEND_SERIAL_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "pstl_config.h" 20 | 21 | _PSTL_HIDE_FROM_ABI_PUSH 22 | 23 | namespace __pstl 24 | { 25 | namespace __serial_backend 26 | { 27 | 28 | template 29 | class __buffer 30 | { 31 | std::allocator<_Tp> __allocator_; 32 | _Tp* __ptr_; 33 | const std::size_t __buf_size_; 34 | __buffer(const __buffer&) = delete; 35 | void 36 | operator=(const __buffer&) = delete; 37 | 38 | public: 39 | __buffer(std::size_t __n) : __allocator_(), __ptr_(__allocator_.allocate(__n)), __buf_size_(__n) {} 40 | 41 | operator bool() const { return __ptr_ != nullptr; } 42 | _Tp* 43 | get() const 44 | { 45 | return __ptr_; 46 | } 47 | ~__buffer() { __allocator_.deallocate(__ptr_, __buf_size_); } 48 | }; 49 | 50 | inline void 51 | __cancel_execution() 52 | { 53 | } 54 | 55 | template 56 | void 57 | __parallel_for(_ExecutionPolicy&&, _Index __first, _Index __last, _Fp __f) 58 | { 59 | __f(__first, __last); 60 | } 61 | 62 | template 63 | _Value 64 | __parallel_reduce(_ExecutionPolicy&&, _Index __first, _Index __last, const _Value& __identity, 65 | const _RealBody& __real_body, const _Reduction&) 66 | { 67 | if (__first == __last) 68 | { 69 | return __identity; 70 | } 71 | else 72 | { 73 | return __real_body(__first, __last, __identity); 74 | } 75 | } 76 | 77 | template 78 | _Tp 79 | __parallel_transform_reduce(_ExecutionPolicy&&, _Index __first, _Index __last, _UnaryOp, _Tp __init, _BinaryOp, 80 | _Reduce __reduce) 81 | { 82 | return __reduce(__first, __last, __init); 83 | } 84 | 85 | template 86 | void 87 | __parallel_strict_scan(_ExecutionPolicy&&, _Index __n, _Tp __initial, _Rp __reduce, _Cp __combine, _Sp __scan, 88 | _Ap __apex) 89 | { 90 | _Tp __sum = __initial; 91 | if (__n) 92 | __sum = __combine(__sum, __reduce(_Index(0), __n)); 93 | __apex(__sum); 94 | if (__n) 95 | __scan(_Index(0), __n, __initial); 96 | } 97 | 98 | template 99 | _Tp 100 | __parallel_transform_scan(_ExecutionPolicy&&, _Index __n, _UnaryOp, _Tp __init, _BinaryOp, _Reduce, _Scan __scan) 101 | { 102 | return __scan(_Index(0), __n, __init); 103 | } 104 | 105 | template 106 | void 107 | __parallel_stable_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 108 | _LeafSort __leaf_sort, std::size_t = 0) 109 | { 110 | __leaf_sort(__first, __last, __comp); 111 | } 112 | 113 | template 115 | void 116 | __parallel_merge(_ExecutionPolicy&&, _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 117 | _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _RandomAccessIterator3 __outit, 118 | _Compare __comp, _LeafMerge __leaf_merge) 119 | { 120 | __leaf_merge(__first1, __last1, __first2, __last2, __outit, __comp); 121 | } 122 | 123 | template 124 | void 125 | __parallel_invoke(_ExecutionPolicy&&, _F1&& __f1, _F2&& __f2) 126 | { 127 | std::forward<_F1>(__f1)(); 128 | std::forward<_F2>(__f2)(); 129 | } 130 | 131 | } // namespace __serial_backend 132 | } // namespace __pstl 133 | 134 | _PSTL_HIDE_FROM_ABI_POP 135 | 136 | #endif /* _PSTL_PARALLEL_BACKEND_SERIAL_H */ 137 | -------------------------------------------------------------------------------- /include/pstl/internal/parallel_backend_utils.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_PARALLEL_BACKEND_UTILS_H 11 | #define _PSTL_PARALLEL_BACKEND_UTILS_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include "utils.h" 17 | 18 | #include "pstl_config.h" 19 | 20 | _PSTL_HIDE_FROM_ABI_PUSH 21 | 22 | namespace __pstl 23 | { 24 | 25 | namespace __utils 26 | { 27 | 28 | //! Destroy sequence [xs,xe) 29 | struct __serial_destroy 30 | { 31 | template 32 | void 33 | operator()(_RandomAccessIterator __zs, _RandomAccessIterator __ze) 34 | { 35 | typedef typename std::iterator_traits<_RandomAccessIterator>::value_type _ValueType; 36 | while (__zs != __ze) 37 | { 38 | --__ze; 39 | (*__ze).~_ValueType(); 40 | } 41 | } 42 | }; 43 | 44 | //! Merge sequences [__xs,__xe) and [__ys,__ye) to output sequence [__zs,(__xe-__xs)+(__ye-__ys)), using std::move 45 | struct __serial_move_merge 46 | { 47 | const std::size_t _M_nmerge; 48 | 49 | explicit __serial_move_merge(std::size_t __nmerge) : _M_nmerge(__nmerge) {} 50 | template 52 | void 53 | operator()(_RandomAccessIterator1 __xs, _RandomAccessIterator1 __xe, _RandomAccessIterator2 __ys, 54 | _RandomAccessIterator2 __ye, _RandomAccessIterator3 __zs, _Compare __comp, _MoveValueX __move_value_x, 55 | _MoveValueY __move_value_y, _MoveSequenceX __move_sequence_x, _MoveSequenceY __move_sequence_y) 56 | { 57 | constexpr bool __same_move_val = std::is_same<_MoveValueX, _MoveValueY>::value; 58 | constexpr bool __same_move_seq = std::is_same<_MoveSequenceX, _MoveSequenceY>::value; 59 | 60 | auto __n = _M_nmerge; 61 | assert(__n > 0); 62 | 63 | auto __nx = __xe - __xs; 64 | //auto __ny = __ye - __ys; 65 | _RandomAccessIterator3 __zs_beg = __zs; 66 | 67 | if (__xs != __xe) 68 | { 69 | if (__ys != __ye) 70 | { 71 | for (;;) 72 | { 73 | if (__comp(*__ys, *__xs)) 74 | { 75 | const auto __i = __zs - __zs_beg; 76 | if (__i < __nx) 77 | __move_value_x(__ys, __zs); 78 | else 79 | __move_value_y(__ys, __zs); 80 | ++__zs, --__n; 81 | if (++__ys == __ye) 82 | { 83 | break; 84 | } 85 | else if (__n == 0) 86 | { 87 | const auto __j = __zs - __zs_beg; 88 | if (__same_move_seq || __j < __nx) 89 | __zs = __move_sequence_x(__ys, __ye, __zs); 90 | else 91 | __zs = __move_sequence_y(__ys, __ye, __zs); 92 | break; 93 | } 94 | } 95 | else 96 | { 97 | const auto __i = __zs - __zs_beg; 98 | if (__same_move_val || __i < __nx) 99 | __move_value_x(__xs, __zs); 100 | else 101 | __move_value_y(__xs, __zs); 102 | ++__zs, --__n; 103 | if (++__xs == __xe) 104 | { 105 | const auto __j = __zs - __zs_beg; 106 | if (__same_move_seq || __j < __nx) 107 | __move_sequence_x(__ys, __ye, __zs); 108 | else 109 | __move_sequence_y(__ys, __ye, __zs); 110 | return; 111 | } 112 | else if (__n == 0) 113 | { 114 | const auto __j = __zs - __zs_beg; 115 | if (__same_move_seq || __j < __nx) 116 | { 117 | __zs = __move_sequence_x(__xs, __xe, __zs); 118 | __move_sequence_x(__ys, __ye, __zs); 119 | } 120 | else 121 | { 122 | __zs = __move_sequence_y(__xs, __xe, __zs); 123 | __move_sequence_y(__ys, __ye, __zs); 124 | } 125 | return; 126 | } 127 | } 128 | } 129 | } 130 | __ys = __xs; 131 | __ye = __xe; 132 | } 133 | const auto __i = __zs - __zs_beg; 134 | if (__same_move_seq || __i < __nx) 135 | __move_sequence_x(__ys, __ye, __zs); 136 | else 137 | __move_sequence_y(__ys, __ye, __zs); 138 | } 139 | }; 140 | 141 | } // namespace __utils 142 | } // namespace __pstl 143 | 144 | _PSTL_HIDE_FROM_ABI_POP 145 | 146 | #endif /* _PSTL_PARALLEL_BACKEND_UTILS_H */ 147 | -------------------------------------------------------------------------------- /include/pstl/internal/parallel_impl.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_PARALLEL_IMPL_H 11 | #define _PSTL_PARALLEL_IMPL_H 12 | 13 | #include "pstl_config.h" 14 | 15 | #include 16 | // This header defines the minimum set of parallel routines required to support Parallel STL, 17 | // implemented on top of Intel(R) Threading Building Blocks (Intel(R) TBB) library 18 | 19 | _PSTL_HIDE_FROM_ABI_PUSH 20 | 21 | namespace __pstl 22 | { 23 | namespace __internal 24 | { 25 | 26 | //------------------------------------------------------------------------ 27 | // parallel_find 28 | //----------------------------------------------------------------------- 29 | /** Return extremum value returned by brick f[i,j) for subranges [i,j) of [first,last) 30 | Each f[i,j) must return a value in [i,j). */ 31 | template 32 | _Index 33 | __parallel_find(_ExecutionPolicy&& __exec, _Index __first, _Index __last, _Brick __f, _Compare __comp, bool __b_first) 34 | { 35 | typedef typename std::iterator_traits<_Index>::difference_type _DifferenceType; 36 | const _DifferenceType __n = __last - __first; 37 | _DifferenceType __initial_dist = __b_first ? __n : -1; 38 | std::atomic<_DifferenceType> __extremum(__initial_dist); 39 | // TODO: find out what is better here: parallel_for or parallel_reduce 40 | __par_backend::__parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last, 41 | [__comp, __f, __first, &__extremum](_Index __i, _Index __j) { 42 | // See "Reducing Contention Through Priority Updates", PPoPP '13, for discussion of 43 | // why using a shared variable scales fairly well in this situation. 44 | if (__comp(__i - __first, __extremum)) 45 | { 46 | _Index __res = __f(__i, __j); 47 | // If not '__last' returned then we found what we want so put this to extremum 48 | if (__res != __j) 49 | { 50 | const _DifferenceType __k = __res - __first; 51 | for (_DifferenceType __old = __extremum; __comp(__k, __old); 52 | __old = __extremum) 53 | { 54 | __extremum.compare_exchange_weak(__old, __k); 55 | } 56 | } 57 | } 58 | }); 59 | return __extremum != __initial_dist ? __first + __extremum : __last; 60 | } 61 | 62 | //------------------------------------------------------------------------ 63 | // parallel_or 64 | //------------------------------------------------------------------------ 65 | //! Return true if brick f[i,j) returns true for some subrange [i,j) of [first,last) 66 | template 67 | bool 68 | __parallel_or(_ExecutionPolicy&& __exec, _Index __first, _Index __last, _Brick __f) 69 | { 70 | std::atomic __found(false); 71 | __par_backend::__parallel_for(std::forward<_ExecutionPolicy>(__exec), __first, __last, 72 | [__f, &__found](_Index __i, _Index __j) { 73 | if (!__found.load(std::memory_order_relaxed) && __f(__i, __j)) 74 | { 75 | __found.store(true, std::memory_order_relaxed); 76 | __par_backend::__cancel_execution(); 77 | } 78 | }); 79 | return __found; 80 | } 81 | 82 | } // namespace __internal 83 | } // namespace __pstl 84 | 85 | _PSTL_HIDE_FROM_ABI_POP 86 | 87 | #endif /* _PSTL_PARALLEL_IMPL_H */ 88 | -------------------------------------------------------------------------------- /include/pstl/internal/utils.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_UTILS_H 11 | #define _PSTL_UTILS_H 12 | 13 | #include 14 | #include 15 | 16 | _PSTL_HIDE_FROM_ABI_PUSH 17 | 18 | namespace __pstl 19 | { 20 | namespace __internal 21 | { 22 | 23 | template 24 | typename std::result_of<_Fp()>::type 25 | __except_handler(_Fp __f) 26 | { 27 | try 28 | { 29 | return __f(); 30 | } 31 | catch (const std::bad_alloc&) 32 | { 33 | throw; // re-throw bad_alloc according to the standard [algorithms.parallel.exceptions] 34 | } 35 | catch (...) 36 | { 37 | std::terminate(); // Good bye according to the standard [algorithms.parallel.exceptions] 38 | } 39 | } 40 | 41 | template 42 | void 43 | __invoke_if(std::true_type, _Fp __f) 44 | { 45 | __f(); 46 | } 47 | 48 | template 49 | void __invoke_if(std::false_type, _Fp) 50 | { 51 | } 52 | 53 | template 54 | void 55 | __invoke_if_not(std::false_type, _Fp __f) 56 | { 57 | __f(); 58 | } 59 | 60 | template 61 | void __invoke_if_not(std::true_type, _Fp) 62 | { 63 | } 64 | 65 | template 66 | typename std::result_of<_F1()>::type 67 | __invoke_if_else(std::true_type, _F1 __f1, _F2) 68 | { 69 | return __f1(); 70 | } 71 | 72 | template 73 | typename std::result_of<_F2()>::type 74 | __invoke_if_else(std::false_type, _F1, _F2 __f2) 75 | { 76 | return __f2(); 77 | } 78 | 79 | //! Unary operator that returns reference to its argument. 80 | struct __no_op 81 | { 82 | template 83 | _Tp&& 84 | operator()(_Tp&& __a) const 85 | { 86 | return std::forward<_Tp>(__a); 87 | } 88 | }; 89 | 90 | template 91 | class __reorder_pred 92 | { 93 | _Pred _M_pred; 94 | 95 | public: 96 | explicit __reorder_pred(_Pred __pred) : _M_pred(__pred) {} 97 | 98 | template 99 | bool 100 | operator()(_FTp&& __a, _STp&& __b) 101 | { 102 | return _M_pred(std::forward<_STp>(__b), std::forward<_FTp>(__a)); 103 | } 104 | }; 105 | 106 | //! Like a polymorphic lambda for pred(...,value) 107 | template 108 | class __equal_value_by_pred 109 | { 110 | const _Tp& _M_value; 111 | _Predicate _M_pred; 112 | 113 | public: 114 | __equal_value_by_pred(const _Tp& __value, _Predicate __pred) : _M_value(__value), _M_pred(__pred) {} 115 | 116 | template 117 | bool 118 | operator()(_Arg&& __arg) 119 | { 120 | return _M_pred(std::forward<_Arg>(__arg), _M_value); 121 | } 122 | }; 123 | 124 | //! Like a polymorphic lambda for ==value 125 | template 126 | class __equal_value 127 | { 128 | const _Tp& _M_value; 129 | 130 | public: 131 | explicit __equal_value(const _Tp& __value) : _M_value(__value) {} 132 | 133 | template 134 | bool 135 | operator()(_Arg&& __arg) const 136 | { 137 | return std::forward<_Arg>(__arg) == _M_value; 138 | } 139 | }; 140 | 141 | //! Logical negation of ==value 142 | template 143 | class __not_equal_value 144 | { 145 | const _Tp& _M_value; 146 | 147 | public: 148 | explicit __not_equal_value(const _Tp& __value) : _M_value(__value) {} 149 | 150 | template 151 | bool 152 | operator()(_Arg&& __arg) const 153 | { 154 | return !(std::forward<_Arg>(__arg) == _M_value); 155 | } 156 | }; 157 | 158 | template 159 | _ForwardIterator 160 | __cmp_iterators_by_values(_ForwardIterator __a, _ForwardIterator __b, _Compare __comp) 161 | { 162 | if (__a < __b) 163 | { // we should return closer iterator 164 | return __comp(*__b, *__a) ? __b : __a; 165 | } 166 | else 167 | { 168 | return __comp(*__a, *__b) ? __a : __b; 169 | } 170 | } 171 | 172 | } // namespace __internal 173 | } // namespace __pstl 174 | 175 | _PSTL_HIDE_FROM_ABI_POP 176 | 177 | #endif /* _PSTL_UTILS_H */ 178 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #===-- CMakeLists.txt ----------------------------------------------------===## 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 | # TODO(ldionne): This CMake testing infrastructure should be replaced with a 10 | # llvm-lit test suite. 11 | 12 | add_custom_target(pstl-build-tests 13 | COMMENT "Build all the pstl tests.") 14 | 15 | add_custom_target(check-pstl 16 | COMMAND "${CMAKE_CTEST_COMMAND}" --output-on-failure 17 | USES_TERMINAL 18 | DEPENDS pstl-build-tests 19 | COMMENT "Build and run all the unit tests.") 20 | 21 | add_library(test_stdlib INTERFACE) 22 | target_include_directories(test_stdlib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/support/stdlib") 23 | target_link_libraries(test_stdlib INTERFACE pstl::ParallelSTL) 24 | target_compile_options(test_stdlib INTERFACE -Wno-gnu-include-next) 25 | 26 | file(GLOB_RECURSE UNIT_TESTS "*.pass.cpp") 27 | foreach(_file IN LISTS UNIT_TESTS) 28 | file(RELATIVE_PATH _target "${CMAKE_CURRENT_SOURCE_DIR}" "${_file}") 29 | string(REPLACE ".cpp" "" _target "${_target}") 30 | string(REPLACE "/" "-" _target "${_target}") 31 | set(_target "pstl-${_target}") 32 | 33 | add_executable(${_target} EXCLUDE_FROM_ALL "${_file}") 34 | target_include_directories(${_target} PRIVATE "${CMAKE_CURRENT_LIST_DIR}") 35 | target_compile_options(${_target} PRIVATE -Wno-unused-local-typedef -Wno-unused-variable) 36 | target_link_libraries(${_target} PRIVATE test_stdlib) 37 | set_target_properties(${_target} PROPERTIES CXX_EXTENSIONS NO 38 | RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") 39 | add_test(${_target} "${CMAKE_CURRENT_BINARY_DIR}/${_target}") 40 | add_dependencies(pstl-build-tests ${_target}) 41 | endforeach() 42 | -------------------------------------------------------------------------------- /test/pstl/header_inclusion_order_algorithm_0.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- header_inclusion_order_algorithm_0.pass.cpp -----------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | int 18 | main() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/pstl/header_inclusion_order_algorithm_1.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- header_inclusion_order_algorithm_1.pass.cpp -----------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | int 18 | main() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/pstl/header_inclusion_order_memory_0.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- header_inclusion_order_memory_0.pass.cpp --------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | int 18 | main() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/pstl/header_inclusion_order_memory_1.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- header_inclusion_order_memory_1.pass.cpp --------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | int 18 | main() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/pstl/header_inclusion_order_numeric_0.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- header_inclusion_order_numeric_0.pass.cpp -------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | int 18 | main() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/pstl/header_inclusion_order_numeric_1.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- header_inclusion_order_numeric_0.pass.cpp -------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | int 18 | main() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/pstl/version.pass.cpp: -------------------------------------------------------------------------------- 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 10 | 11 | static_assert(_PSTL_VERSION == 10000); 12 | static_assert(_PSTL_VERSION_MAJOR == 10); 13 | static_assert(_PSTL_VERSION_MINOR == 00); 14 | static_assert(_PSTL_VERSION_PATCH == 0); 15 | 16 | int 17 | main() 18 | { 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.merge/merge.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- merge.pass.cpp ----------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | struct test_merge 23 | { 24 | template 26 | void 27 | operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, 28 | OutputIterator out_first, OutputIterator out_last, Compare comp) 29 | { 30 | using namespace std; 31 | { 32 | const auto res = merge(exec, first1, last1, first2, last2, out_first, comp); 33 | EXPECT_TRUE(res == out_last, "wrong return result from merge with predicate"); 34 | EXPECT_TRUE(is_sorted(out_first, res, comp), "wrong result from merge with predicate"); 35 | EXPECT_TRUE(includes(out_first, res, first1, last1, comp), "first sequence is not a part of result"); 36 | EXPECT_TRUE(includes(out_first, res, first2, last2, comp), "second sequence is not a part of result"); 37 | } 38 | { 39 | const auto res = merge(exec, first1, last1, first2, last2, out_first); 40 | EXPECT_TRUE(res == out_last, "wrong return result from merge"); 41 | EXPECT_TRUE(is_sorted(out_first, res), "wrong result from merge"); 42 | } 43 | } 44 | 45 | // for reverse iterators 46 | template 48 | void 49 | operator()(Policy&& exec, std::reverse_iterator first1, std::reverse_iterator last1, 50 | std::reverse_iterator first2, std::reverse_iterator last2, 51 | std::reverse_iterator out_first, std::reverse_iterator out_last, Compare) 52 | { 53 | using namespace std; 54 | typedef typename std::iterator_traits>::value_type T; 55 | const auto res = merge(exec, first1, last1, first2, last2, out_first, std::greater()); 56 | 57 | EXPECT_TRUE(res == out_last, "wrong return result from merge with predicate"); 58 | EXPECT_TRUE(is_sorted(out_first, res, std::greater()), "wrong result from merge with predicate"); 59 | EXPECT_TRUE(includes(out_first, res, first1, last1, std::greater()), 60 | "first sequence is not a part of result"); 61 | EXPECT_TRUE(includes(out_first, res, first2, last2, std::greater()), 62 | "second sequence is not a part of result"); 63 | } 64 | }; 65 | 66 | template 67 | void 68 | test_merge_by_type(Generator1 generator1, Generator2 generator2) 69 | { 70 | using namespace std; 71 | size_t max_size = 100000; 72 | Sequence in1(max_size, generator1); 73 | Sequence in2(max_size / 2, generator2); 74 | Sequence out(in1.size() + in2.size()); 75 | std::sort(in1.begin(), in1.end()); 76 | std::sort(in2.begin(), in2.end()); 77 | 78 | for (size_t size = 0; size <= max_size; size = size <= 16 ? size + 1 : size_t(3.1415 * size)) 79 | { 80 | invoke_on_all_policies(test_merge(), in1.cbegin(), in1.cbegin() + size, in2.data(), in2.data() + size / 2, 81 | out.begin(), out.begin() + 1.5 * size, std::less()); 82 | invoke_on_all_policies(test_merge(), in1.data(), in1.data() + size, in2.cbegin(), in2.cbegin() + size / 2, 83 | out.begin(), out.begin() + 3 * size / 2, std::less()); 84 | } 85 | } 86 | 87 | template 88 | struct test_non_const 89 | { 90 | template 91 | void 92 | operator()(Policy&& exec, InputIterator input_iter, OutputIterator out_iter) 93 | { 94 | merge(exec, input_iter, input_iter, input_iter, input_iter, out_iter, non_const(std::less())); 95 | } 96 | }; 97 | 98 | int 99 | main() 100 | { 101 | test_merge_by_type([](size_t v) { return (v % 2 == 0 ? v : -v) * 3; }, [](size_t v) { return v * 2; }); 102 | test_merge_by_type([](size_t v) { return float64_t(v); }, [](size_t v) { return float64_t(v - 100); }); 103 | 104 | #if !_PSTL_ICC_16_17_TEST_64_TIMEOUT 105 | test_merge_by_type>([](size_t v) { return Wrapper(v % 100); }, 106 | [](size_t v) { return Wrapper(v % 10); }); 107 | #endif 108 | 109 | test_algo_basic_double(run_for_rnd_fw>()); 110 | 111 | std::cout << done() << std::endl; 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- is_partitioned.pass.cpp -------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_one_policy 22 | { 23 | //dummy specialization by policy type, in case of broken configuration 24 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN 25 | 26 | template 27 | void 28 | operator()(pstl::execution::unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred) 29 | { 30 | } 31 | template 32 | void 33 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 begin1, Iterator1 end1, Predicate pred) 34 | { 35 | } 36 | #endif 37 | 38 | template 39 | void 40 | operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Predicate pred) 41 | { 42 | const bool expected = std::is_partitioned(begin1, end1, pred); 43 | const bool actual = std::is_partitioned(exec, begin1, end1, pred); 44 | EXPECT_TRUE(actual == expected, "wrong return result from is_partitioned"); 45 | } 46 | }; 47 | 48 | template 49 | void 50 | test(Predicate pred) 51 | { 52 | 53 | const std::size_t max_n = 1000000; 54 | Sequence in(max_n, [](std::size_t k) { return T(k); }); 55 | 56 | for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1)) 57 | { 58 | invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, pred); 59 | std::partition(in.begin(), in.begin() + n1, pred); 60 | invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, pred); 61 | } 62 | } 63 | 64 | template 65 | struct LocalWrapper 66 | { 67 | explicit LocalWrapper(std::size_t k) : my_val(k) {} 68 | 69 | private: 70 | T my_val; 71 | }; 72 | 73 | struct test_non_const 74 | { 75 | template 76 | void 77 | operator()(Policy&& exec, Iterator iter) 78 | { 79 | auto is_even = [&](float64_t v) { 80 | uint32_t i = (uint32_t)v; 81 | return i % 2 == 0; 82 | }; 83 | invoke_if(exec, [&]() { is_partitioned(exec, iter, iter, non_const(is_even)); }); 84 | } 85 | }; 86 | 87 | int 88 | main() 89 | { 90 | test([](const float64_t x) { return x < 0; }); 91 | test([](const int32_t x) { return x > 1000; }); 92 | test([](const uint16_t x) { return x % 5 < 3; }); 93 | #if !_PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN && !_PSTL_ICC_19_TEST_IS_PARTITIONED_RELEASE_BROKEN 94 | test>([](const LocalWrapper&) { return true; }); 95 | #endif 96 | 97 | test_algo_basic_single(run_for_rnd_fw()); 98 | 99 | std::cout << done() << std::endl; 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- partition_copy.pass.cpp -------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for stable_partition and partition_copy 13 | #include "support/pstl_test_config.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "support/utils.h" 22 | 23 | using namespace TestUtils; 24 | 25 | struct test_partition_copy 26 | { 27 | template 29 | void 30 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator true_first, OutputIterator, 31 | OutputIterator2 false_first, OutputIterator2, UnaryOp unary_op) 32 | { 33 | 34 | auto actual_ret = std::partition_copy(exec, first, last, true_first, false_first, unary_op); 35 | 36 | EXPECT_TRUE(std::distance(true_first, actual_ret.first) == std::count_if(first, last, unary_op), 37 | "partition_copy has wrong effect from true sequence"); 38 | EXPECT_TRUE(std::distance(false_first, actual_ret.second) == std::count_if(first, last, std::not_fn(unary_op)), 39 | "partition_copy has wrong effect from false sequence"); 40 | } 41 | 42 | //dummy specialization by iterator type and policy type, in case of broken configuration 43 | #if _PSTL_ICC_1800_TEST_MONOTONIC_RELEASE_64_BROKEN 44 | template 45 | void 46 | operator()(pstl::execution::unsequenced_policy, std::reverse_iterator first, 47 | std::reverse_iterator last, std::reverse_iterator true_first, 48 | std::reverse_iterator true_last, std::reverse_iterator false_first, 49 | OutputIterator2 false_last, UnaryOp unary_op) 50 | { 51 | } 52 | template 53 | void 54 | operator()(pstl::execution::parallel_unsequenced_policy, std::reverse_iterator first, 55 | std::reverse_iterator last, std::reverse_iterator true_first, 56 | std::reverse_iterator true_last, std::reverse_iterator false_first, 57 | OutputIterator2 false_last, UnaryOp unary_op) 58 | { 59 | } 60 | #endif 61 | }; 62 | 63 | template 64 | void 65 | test(UnaryPred pred) 66 | { 67 | 68 | const std::size_t max_size = 100000; 69 | Sequence in(max_size, [](std::size_t v) -> T { return T(v); }); 70 | Sequence actual_true(max_size); 71 | Sequence actual_false(max_size); 72 | for (std::size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : std::size_t(3.1415 * n)) 73 | { 74 | 75 | // for non-const input iterators 76 | invoke_on_all_policies(test_partition_copy(), in.begin(), in.begin() + n, actual_true.begin(), 77 | actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred); 78 | 79 | // for const input iterators 80 | invoke_on_all_policies(test_partition_copy(), in.cbegin(), in.cbegin() + n, actual_true.begin(), 81 | actual_true.begin() + n, actual_false.begin(), actual_false.begin() + n, pred); 82 | } 83 | } 84 | 85 | struct test_non_const 86 | { 87 | template 88 | void 89 | operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 90 | { 91 | auto is_even = [&](float64_t v) { 92 | uint32_t i = (uint32_t)v; 93 | return i % 2 == 0; 94 | }; 95 | 96 | partition_copy(exec, input_iter, input_iter, out_iter, out_iter, non_const(is_even)); 97 | } 98 | }; 99 | 100 | int 101 | main() 102 | { 103 | test([](const int32_t value) { return value % 2; }); 104 | 105 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN 106 | test([](const int32_t) { return true; }); 107 | #endif 108 | 109 | test([](const float64_t value) { return value > 2 << 6; }); 110 | test>([](const Wrapper& value) -> bool { return value.get_my_field() != nullptr; }); 111 | 112 | test_algo_basic_double(run_for_rnd_bi()); 113 | 114 | std::cout << done() << std::endl; 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/alg.reverse/reverse.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- reverse.pass.cpp --------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | struct test_one_policy 23 | { 24 | #if _PSTL_ICC_18_VC141_TEST_SIMD_LAMBDA_RELEASE_BROKEN || _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 25 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration 26 | template 27 | typename std::enable_if::value, void>::type 28 | operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 29 | Iterator2 actual_e) 30 | { 31 | } 32 | template 33 | typename std::enable_if::value, void>::type 34 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 35 | Iterator2 actual_e) 36 | { 37 | } 38 | #endif 39 | 40 | template 41 | typename std::enable_if::value>::type 42 | operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e) 43 | { 44 | using namespace std; 45 | 46 | copy(data_b, data_e, actual_b); 47 | 48 | reverse(exec, actual_b, actual_e); 49 | 50 | bool check = equal(data_b, data_e, reverse_iterator(actual_e)); 51 | 52 | EXPECT_TRUE(check, "wrong result of reverse"); 53 | } 54 | 55 | template 56 | typename std::enable_if::value>::type 57 | operator()(ExecutionPolicy&&, Iterator1, Iterator1, Iterator2, Iterator2) 58 | { 59 | } 60 | }; 61 | 62 | template 63 | void 64 | test() 65 | { 66 | const std::size_t max_len = 100000; 67 | 68 | Sequence actual(max_len); 69 | 70 | Sequence data(max_len, [](std::size_t i) { return T(i); }); 71 | 72 | for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 73 | { 74 | invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), 75 | actual.begin() + len); 76 | } 77 | } 78 | 79 | template 80 | struct wrapper 81 | { 82 | T t; 83 | wrapper() {} 84 | explicit wrapper(T t_) : t(t_) {} 85 | bool 86 | operator==(const wrapper& a) const 87 | { 88 | return t == a.t; 89 | } 90 | }; 91 | 92 | int 93 | main() 94 | { 95 | test(); 96 | test(); 97 | test(); 98 | #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN 99 | test>(); 100 | #endif 101 | 102 | std::cout << done() << std::endl; 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/alg.reverse/reverse_copy.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- reverse_copy.pass.cpp ---------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | template 23 | struct wrapper 24 | { 25 | T t; 26 | wrapper() {} 27 | explicit wrapper(T t_) : t(t_) {} 28 | wrapper& 29 | operator=(const T& t_) 30 | { 31 | t = t_; 32 | return *this; 33 | } 34 | bool 35 | operator==(const wrapper& t_) const 36 | { 37 | return t == t_.t; 38 | } 39 | }; 40 | 41 | template 42 | bool 43 | eq(const wrapper& a, const wrapper& b) 44 | { 45 | return a.t == b.t; 46 | } 47 | 48 | template 49 | bool 50 | eq(const T1& a, const T2& b) 51 | { 52 | return a == b; 53 | } 54 | 55 | // we need to save state here, because we need to test with different types of iterators 56 | // due to the caller invoke_on_all_policies does forcing modification passed iterator type to cover additional usage cases. 57 | template 58 | struct test_one_policy 59 | { 60 | Iterator data_b; 61 | Iterator data_e; 62 | test_one_policy(Iterator b, Iterator e) : data_b(b), data_e(e) {} 63 | 64 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 65 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration 66 | template 67 | typename std::enable_if::value, void>::type 68 | operator()(pstl::execution::unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e) 69 | { 70 | } 71 | template 72 | typename std::enable_if::value, void>::type 73 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 actual_b, Iterator1 actual_e) 74 | { 75 | } 76 | #endif 77 | 78 | template 79 | void 80 | operator()(ExecutionPolicy&& exec, Iterator1 actual_b, Iterator1 actual_e) 81 | { 82 | using namespace std; 83 | using T = typename iterator_traits::value_type; 84 | 85 | fill(actual_b, actual_e, T(-123)); 86 | Iterator1 actual_return = reverse_copy(exec, data_b, data_e, actual_b); 87 | 88 | EXPECT_TRUE(actual_return == actual_e, "wrong result of reverse_copy"); 89 | 90 | const auto n = std::distance(data_b, data_e); 91 | Sequence res(n); 92 | std::copy(std::reverse_iterator(data_e), std::reverse_iterator(data_b), res.begin()); 93 | 94 | EXPECT_EQ_N(res.begin(), actual_b, n, "wrong effect of reverse_copy"); 95 | } 96 | }; 97 | 98 | template 99 | void 100 | test() 101 | { 102 | typedef typename Sequence::iterator iterator_type; 103 | typedef typename Sequence::const_bidirectional_iterator cbi_iterator_type; 104 | 105 | const std::size_t max_len = 100000; 106 | 107 | Sequence actual(max_len); 108 | 109 | Sequence data(max_len, [](std::size_t i) { return T1(i); }); 110 | 111 | for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 112 | { 113 | invoke_on_all_policies(test_one_policy(data.begin(), data.begin() + len), actual.begin(), 114 | actual.begin() + len); 115 | invoke_on_all_policies(test_one_policy(data.cbibegin(), std::next(data.cbibegin(), len)), 116 | actual.begin(), actual.begin() + len); 117 | } 118 | } 119 | 120 | int 121 | main() 122 | { 123 | // clang-3.8 fails to correctly auto vectorize the loop in some cases of different types of container's elements, 124 | // for example: int32_t and int8_t. This issue isn't detected for clang-3.9 and newer versions. 125 | test(); 126 | test(); 127 | test(); 128 | test, wrapper>(); 129 | 130 | std::cout << done() << std::endl; 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/fill.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- fill.pass.cpp -----------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_fill 22 | { 23 | template 24 | bool 25 | check(It first, It last, const T& value) 26 | { 27 | for (; first != last; ++first) 28 | if (*first != value) 29 | return false; 30 | return true; 31 | } 32 | 33 | template 34 | void 35 | operator()(Policy&& exec, Iterator first, Iterator last, const T& value) 36 | { 37 | fill(first, last, T(value + 1)); // initialize memory with different value 38 | 39 | fill(exec, first, last, value); 40 | EXPECT_TRUE(check(first, last, value), "fill wrong result"); 41 | } 42 | }; 43 | 44 | struct test_fill_n 45 | { 46 | template 47 | bool 48 | check(It first, Size n, const T& value) 49 | { 50 | for (Size i = 0; i < n; ++i, ++first) 51 | if (*first != value) 52 | return false; 53 | return true; 54 | } 55 | 56 | template 57 | void 58 | operator()(Policy&& exec, Iterator first, Size n, const T& value) 59 | { 60 | fill_n(first, n, T(value + 1)); // initialize memory with different value 61 | 62 | const Iterator one_past_last = fill_n(exec, first, n, value); 63 | const Iterator expected_return = std::next(first, n); 64 | 65 | EXPECT_TRUE(expected_return == one_past_last, "fill_n should return Iterator to one past the element assigned"); 66 | EXPECT_TRUE(check(first, n, value), "fill_n wrong result"); 67 | 68 | //n == -1 69 | const Iterator res = fill_n(exec, first, -1, value); 70 | EXPECT_TRUE(res == first, "fill_n wrong result for n == -1"); 71 | } 72 | }; 73 | 74 | template 75 | void 76 | test_fill_by_type(std::size_t n) 77 | { 78 | Sequence in(n, [](std::size_t) -> T { return T(0); }); //fill with zeros 79 | T value = -1; 80 | 81 | invoke_on_all_policies(test_fill(), in.begin(), in.end(), value); 82 | invoke_on_all_policies(test_fill_n(), in.begin(), n, value); 83 | } 84 | 85 | int 86 | main() 87 | { 88 | 89 | const std::size_t N = 100000; 90 | 91 | for (std::size_t n = 0; n < N; n = n < 16 ? n + 1 : size_t(3.1415 * n)) 92 | { 93 | test_fill_by_type(n); 94 | test_fill_by_type(n); 95 | } 96 | 97 | std::cout << done() << std::endl; 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/generate.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- generate.pass.cpp -------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | template 23 | struct Generator_count 24 | { 25 | const T def_val = T(-1); 26 | T 27 | operator()() 28 | { 29 | return def_val; 30 | } 31 | T 32 | default_value() const 33 | { 34 | return def_val; 35 | } 36 | }; 37 | 38 | struct test_generate 39 | { 40 | template 41 | void 42 | operator()(Policy&& exec, Iterator first, Iterator last, Size n) 43 | { 44 | using namespace std; 45 | typedef typename std::iterator_traits::value_type T; 46 | 47 | // Try random-access iterator 48 | { 49 | Generator_count g; 50 | generate(exec, first, last, g); 51 | Size count = std::count(first, last, g.default_value()); 52 | EXPECT_TRUE(count == n, "generate wrong result for generate"); 53 | std::fill(first, last, T(0)); 54 | } 55 | 56 | { 57 | Generator_count g; 58 | const auto m = n / 2; 59 | auto actual_last = generate_n(exec, first, m, g); 60 | Size count = std::count(first, actual_last, g.default_value()); 61 | EXPECT_TRUE(count == m && actual_last == std::next(first, m), "generate_n wrong result for generate_n"); 62 | std::fill(first, actual_last, T(0)); 63 | } 64 | } 65 | }; 66 | 67 | template 68 | void 69 | test_generate_by_type() 70 | { 71 | for (size_t n = 0; n <= 100000; n = n < 16 ? n + 1 : size_t(3.1415 * n)) 72 | { 73 | Sequence in(n, [](size_t) -> T { return T(0); }); //fill by zero 74 | 75 | invoke_on_all_policies(test_generate(), in.begin(), in.end(), in.size()); 76 | } 77 | } 78 | 79 | template 80 | struct test_non_const 81 | { 82 | template 83 | void 84 | operator()(Policy&& exec, Iterator iter) 85 | { 86 | auto gen = []() { return T(0); }; 87 | 88 | generate(exec, iter, iter, non_const(gen)); 89 | generate_n(exec, iter, 0, non_const(gen)); 90 | } 91 | }; 92 | 93 | int 94 | main() 95 | { 96 | 97 | test_generate_by_type(); 98 | test_generate_by_type(); 99 | 100 | test_algo_basic_single(run_for_rnd_fw>()); 101 | 102 | std::cout << done() << std::endl; 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/remove_copy.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- remove_copy.pass.cpp ----------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct run_remove_copy 22 | { 23 | template 25 | void 26 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 27 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, const T& value, 28 | T trash) 29 | { 30 | // Cleaning 31 | std::fill_n(expected_first, n, trash); 32 | std::fill_n(out_first, n, trash); 33 | 34 | // Run copy_if 35 | auto i = std::remove_copy(first, last, expected_first, value); 36 | (void)i; 37 | auto k = std::remove_copy(exec, first, last, out_first, value); 38 | EXPECT_EQ_N(expected_first, out_first, n, "wrong remove_copy effect"); 39 | for (size_t j = 0; j < GuardSize; ++j) 40 | { 41 | ++k; 42 | } 43 | EXPECT_TRUE(out_last == k, "wrong return value from remove_copy"); 44 | } 45 | }; 46 | 47 | template 48 | void 49 | test(T trash, const T& value, Convert convert, bool check_weakness = true) 50 | { 51 | // Try sequences of various lengths. 52 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 53 | { 54 | // count is number of output elements, plus a handful 55 | // more for sake of detecting buffer overruns. 56 | size_t count = GuardSize; 57 | Sequence in(n, [&](size_t k) -> T { 58 | T x = convert(n ^ k); 59 | count += !(x == value) ? 1 : 0; 60 | return x; 61 | }); 62 | using namespace std; 63 | 64 | Sequence out(count, [=](size_t) { return trash; }); 65 | Sequence expected(count, [=](size_t) { return trash; }); 66 | if (check_weakness) 67 | { 68 | auto expected_result = remove_copy(in.cfbegin(), in.cfend(), expected.begin(), value); 69 | size_t m = expected_result - expected.begin(); 70 | EXPECT_TRUE(n / 4 <= m && m <= 3 * (n + 1) / 4, "weak test for remove_copy"); 71 | } 72 | invoke_on_all_policies(run_remove_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), 73 | expected.end(), count, value, trash); 74 | invoke_on_all_policies(run_remove_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), 75 | expected.end(), count, value, trash); 76 | } 77 | } 78 | 79 | int 80 | main() 81 | { 82 | 83 | test(-666.0, 8.5, [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); 84 | 85 | test(-666, 42, [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); }); 86 | 87 | test(Number(42, OddTag()), Number(2001, OddTag()), 88 | [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); }); 89 | std::cout << done() << std::endl; 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/replace.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- replace.pass.cpp --------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | // This class is needed to check the self-copying 22 | struct copy_int 23 | { 24 | int32_t value; 25 | int32_t copied_times = 0; 26 | constexpr explicit copy_int(int32_t val = 0) : value(val) {} 27 | 28 | constexpr copy_int& 29 | operator=(const copy_int& other) 30 | { 31 | if (&other == this) 32 | copied_times++; 33 | else 34 | { 35 | value = other.value; 36 | copied_times = other.copied_times; 37 | } 38 | return *this; 39 | } 40 | 41 | constexpr bool 42 | operator==(const copy_int& other) const 43 | { 44 | return (value == other.value); 45 | } 46 | }; 47 | 48 | template 49 | struct test_one_policy 50 | { 51 | std::size_t len; 52 | Iterator data_b; 53 | Iterator data_e; 54 | test_one_policy(Iterator data_, std::size_t len_) 55 | { 56 | len = len_; 57 | data_b = data_; 58 | data_e = std::next(data_b, len); 59 | } 60 | template 61 | void 62 | operator()(ExecutionPolicy&& exec, Iterator1 expected_b, Iterator1 expected_e, Iterator2 actual_b, 63 | Iterator2 actual_e, Predicate pred, const T& value, const T& old_value) 64 | { 65 | using namespace std; 66 | 67 | copy(data_b, data_e, expected_b); 68 | copy(data_b, data_e, actual_b); 69 | 70 | replace(expected_b, expected_e, old_value, value); 71 | replace(exec, actual_b, actual_e, old_value, value); 72 | 73 | EXPECT_TRUE((check(actual_b, actual_e)), "wrong result of self assignment check"); 74 | EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace"); 75 | 76 | copy(data_b, data_e, expected_b); 77 | copy(data_b, data_e, actual_b); 78 | 79 | replace_if(expected_b, expected_e, pred, value); 80 | replace_if(exec, actual_b, actual_e, pred, value); 81 | EXPECT_TRUE(equal(expected_b, expected_e, actual_b), "wrong result of replace_if"); 82 | } 83 | 84 | template 85 | bool check(Iterator1, Iterator1) 86 | { 87 | return true; 88 | } 89 | 90 | template 91 | typename std::enable_if::value, bool>::type_t 92 | check(Iterator1 b, Iterator1 e) 93 | { 94 | return std::all_of(b, e, [](const copy_int& elem) { return elem.copied_times == 0; }); 95 | } 96 | }; 97 | 98 | template 99 | void 100 | test(Pred pred) 101 | { 102 | typedef typename Sequence::iterator iterator_type; 103 | 104 | const std::size_t max_len = 100000; 105 | 106 | static constexpr T1 value = T1(0); 107 | static constexpr T1 new_value = T1(666); 108 | 109 | Sequence expected(max_len); 110 | Sequence actual(max_len); 111 | 112 | Sequence data(max_len, [](std::size_t i) { 113 | if (i % 3 == 2) 114 | { 115 | return T1(i); 116 | } 117 | else 118 | { 119 | return value; 120 | } 121 | }); 122 | 123 | for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 124 | { 125 | test_one_policy temp(data.begin(), len); 126 | 127 | invoke_on_all_policies(temp, expected.begin(), expected.begin() + len, actual.begin(), actual.begin() + len, 128 | pred, new_value, value); 129 | } 130 | } 131 | 132 | template 133 | struct test_non_const 134 | { 135 | template 136 | void 137 | operator()(Policy&& exec, Iterator iter) 138 | { 139 | auto is_even = [&](float64_t v) { 140 | uint32_t i = (uint32_t)v; 141 | return i % 2 == 0; 142 | }; 143 | invoke_if(exec, [&]() { replace_if(exec, iter, iter, non_const(is_even), T(0)); }); 144 | } 145 | }; 146 | 147 | int 148 | main() 149 | { 150 | test(__pstl::__internal::__equal_value(666)); 151 | test([](const uint16_t& elem) { return elem % 3 < 2; }); 152 | test([](const float64_t& elem) { return elem * elem - 3.5 * elem > 10; }); 153 | test([](const copy_int& val) { return val.value / 5 > 2; }); 154 | 155 | test_algo_basic_single(run_for_rnd_fw>()); 156 | 157 | std::cout << done() << std::endl; 158 | return 0; 159 | } 160 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/replace_copy.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- replace_copy.pass.cpp ---------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for replace_copy and replace_copy_if 13 | 14 | #include "support/pstl_test_config.h" 15 | 16 | #include 17 | #include 18 | 19 | #include "support/utils.h" 20 | 21 | using namespace TestUtils; 22 | 23 | struct test_replace_copy 24 | { 25 | template 27 | void 28 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 29 | OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred, 30 | const T& old_value, const T& new_value, T trash) 31 | { 32 | // Cleaning 33 | std::fill_n(expected_first, n, trash); 34 | std::fill_n(out_first, n, trash); 35 | // Run replace_copy 36 | auto i = std::replace_copy(first, last, expected_first, old_value, new_value); 37 | auto k = std::replace_copy(exec, first, last, out_first, old_value, new_value); 38 | EXPECT_EQ_N(expected_first, out_first, n, "wrong replace_copy effect"); 39 | EXPECT_TRUE(out_last == k, "wrong return value from replace_copy"); 40 | 41 | // Cleaning 42 | std::fill_n(expected_first, n, trash); 43 | std::fill_n(out_first, n, trash); 44 | // Run replace_copy_if 45 | i = replace_copy_if(first, last, expected_first, pred, new_value); 46 | k = replace_copy_if(exec, first, last, out_first, pred, new_value); 47 | EXPECT_EQ_N(expected_first, out_first, n, "wrong replace_copy_if effect"); 48 | EXPECT_TRUE(out_last == k, "wrong return value from replace_copy_if"); 49 | } 50 | }; 51 | 52 | template 53 | void 54 | test(T trash, const T& old_value, const T& new_value, Predicate pred, Convert convert) 55 | { 56 | // Try sequences of various lengths. 57 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 58 | { 59 | Sequence in(n, [&](size_t k) -> T { return convert(n ^ k); }); 60 | Sequence out(n, [=](size_t) { return trash; }); 61 | Sequence expected(n, [=](size_t) { return trash; }); 62 | 63 | invoke_on_all_policies(test_replace_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), 64 | expected.end(), out.size(), pred, old_value, new_value, trash); 65 | invoke_on_all_policies(test_replace_copy(), in.cbegin(), in.cend(), out.begin(), out.end(), expected.begin(), 66 | expected.end(), out.size(), pred, old_value, new_value, trash); 67 | } 68 | } 69 | 70 | template 71 | struct test_non_const 72 | { 73 | template 74 | void 75 | operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 76 | { 77 | auto is_even = [&](float64_t v) { 78 | uint32_t i = (uint32_t)v; 79 | return i % 2 == 0; 80 | }; 81 | 82 | invoke_if(exec, [&]() { replace_copy_if(exec, input_iter, input_iter, out_iter, non_const(is_even), T(0)); }); 83 | } 84 | }; 85 | 86 | int 87 | main() 88 | { 89 | 90 | test(-666.0, 8.5, 0.33, [](const float64_t& x) { return x * x <= 1024; }, 91 | [](size_t j) { return ((j + 1) % 7 & 2) != 0 ? 8.5 : float64_t(j % 32 + j); }); 92 | 93 | test(-666, 42, 99, [](const int32_t& x) { return x != 42; }, 94 | [](size_t j) { return ((j + 1) % 5 & 2) != 0 ? 42 : -1 - int32_t(j); }); 95 | 96 | #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN 97 | test(Number(42, OddTag()), Number(2001, OddTag()), Number(2017, OddTag()), IsMultiple(3, OddTag()), 98 | [](int32_t j) { return ((j + 1) % 3 & 2) != 0 ? Number(2001, OddTag()) : Number(j, OddTag()); }); 99 | #endif 100 | 101 | test_algo_basic_double(run_for_rnd_fw>()); 102 | 103 | std::cout << done() << std::endl; 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/rotate.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- rotate.pass.cpp ---------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | template 23 | struct wrapper 24 | { 25 | T t; 26 | int move_count; 27 | explicit wrapper(T t_) : t(t_), move_count(0) {} 28 | wrapper& 29 | operator=(const T& t_) 30 | { 31 | t = t_; 32 | return *this; 33 | } 34 | 35 | wrapper(const wrapper& a) : move_count(0) { t = a.t; } 36 | 37 | wrapper& 38 | operator=(wrapper& a) 39 | { 40 | t = a.t; 41 | return *this; 42 | } 43 | 44 | wrapper& 45 | operator=(wrapper&& a) 46 | { 47 | t = a.t; 48 | move_count += 1; 49 | return *this; 50 | } 51 | }; 52 | 53 | template 54 | struct compare 55 | { 56 | bool 57 | operator()(const T& a, const T& b) 58 | { 59 | return a == b; 60 | } 61 | }; 62 | 63 | template 64 | struct compare> 65 | { 66 | bool 67 | operator()(const wrapper& a, const wrapper& b) 68 | { 69 | return a.t == b.t; 70 | } 71 | }; 72 | #include 73 | 74 | struct test_one_policy 75 | { 76 | 77 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 78 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specializations to skip testing in case of broken configuration 79 | template 80 | void 81 | operator()(pstl::execution::unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b, 82 | Iterator actual_e, Size shift) 83 | { 84 | } 85 | template 86 | void 87 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b, 88 | Iterator actual_e, Size shift) 89 | { 90 | } 91 | #endif 92 | 93 | template 94 | void 95 | operator()(ExecutionPolicy&& exec, Iterator data_b, Iterator data_e, Iterator actual_b, Iterator actual_e, 96 | Size shift) 97 | { 98 | using namespace std; 99 | using T = typename iterator_traits::value_type; 100 | Iterator actual_m = std::next(actual_b, shift); 101 | 102 | copy(data_b, data_e, actual_b); 103 | Iterator actual_return = rotate(exec, actual_b, actual_m, actual_e); 104 | 105 | EXPECT_TRUE(actual_return == std::next(actual_b, std::distance(actual_m, actual_e)), "wrong result of rotate"); 106 | auto comparator = compare(); 107 | bool check = std::equal(actual_return, actual_e, data_b, comparator); 108 | check = check && std::equal(actual_b, actual_return, std::next(data_b, shift), comparator); 109 | 110 | EXPECT_TRUE(check, "wrong effect of rotate"); 111 | EXPECT_TRUE(check_move(exec, actual_b, actual_e, shift), "wrong move test of rotate"); 112 | } 113 | 114 | template 115 | typename std::enable_if< 116 | is_same_iterator_category::value && 117 | !std::is_same::value && 118 | std::is_same::value_type, wrapper>::value, 119 | bool>::type 120 | check_move(ExecutionPolicy&&, Iterator b, Iterator e, Size shift) 121 | { 122 | bool result = all_of(b, e, [](wrapper& a) { 123 | bool temp = a.move_count > 0; 124 | a.move_count = 0; 125 | return temp; 126 | }); 127 | return shift == 0 || result; 128 | } 129 | 130 | template 131 | typename std::enable_if< 132 | !(is_same_iterator_category::value && 133 | !std::is_same::value && 134 | std::is_same::value_type, wrapper>::value), 135 | bool>::type 136 | check_move(ExecutionPolicy&&, Iterator, Iterator, Size) 137 | { 138 | return true; 139 | } 140 | }; 141 | 142 | template 143 | void 144 | test() 145 | { 146 | const int32_t max_len = 100000; 147 | 148 | Sequence actual(max_len, [](std::size_t i) { return T(i); }); 149 | Sequence data(max_len, [](std::size_t i) { return T(i); }); 150 | 151 | for (int32_t len = 0; len < max_len; len = len <= 16 ? len + 1 : int32_t(3.1415 * len)) 152 | { 153 | int32_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1}; 154 | for (auto shift : shifts) 155 | { 156 | if (shift >= 0 && shift < len) 157 | { 158 | invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), 159 | actual.begin() + len, shift); 160 | } 161 | } 162 | } 163 | } 164 | 165 | int 166 | main() 167 | { 168 | test(); 169 | test>(); 170 | 171 | std::cout << done() << std::endl; 172 | return 0; 173 | } 174 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/rotate_copy.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- rotate_copy.pass.cpp ----------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | template 23 | struct wrapper; 24 | 25 | template 26 | bool 27 | compare(const wrapper& a, const wrapper& b) 28 | { 29 | return a.t == b.t; 30 | } 31 | 32 | template 33 | bool 34 | compare(const T& a, const T& b) 35 | { 36 | return a == b; 37 | } 38 | 39 | template 40 | struct wrapper 41 | { 42 | explicit wrapper(T t_) : t(t_) {} 43 | wrapper& 44 | operator=(const T& t_) 45 | { 46 | t = t_; 47 | return *this; 48 | } 49 | friend bool 50 | compare(const wrapper& a, const wrapper& b); 51 | 52 | private: 53 | T t; 54 | }; 55 | 56 | template 57 | struct comparator 58 | { 59 | using T1 = typename std::iterator_traits::value_type; 60 | using T2 = typename std::iterator_traits::value_type; 61 | bool 62 | operator()(T1 a, T2 b) 63 | { 64 | T temp = a; 65 | return compare(temp, b); 66 | } 67 | }; 68 | 69 | struct test_one_policy 70 | { 71 | 72 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 73 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration 74 | template 75 | typename std::enable_if::value, void>::type 76 | operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 77 | Iterator2 actual_e, std::size_t shift) 78 | { 79 | } 80 | template 81 | typename std::enable_if::value, void>::type 82 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 83 | Iterator2 actual_e, std::size_t shift) 84 | { 85 | } 86 | #endif 87 | 88 | template 89 | void 90 | operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, 91 | std::size_t shift) 92 | { 93 | using namespace std; 94 | using T = typename iterator_traits::value_type; 95 | Iterator1 data_m = std::next(data_b, shift); 96 | 97 | fill(actual_b, actual_e, T(-123)); 98 | Iterator2 actual_return = rotate_copy(exec, data_b, data_m, data_e, actual_b); 99 | 100 | EXPECT_TRUE(actual_return == actual_e, "wrong result of rotate_copy"); 101 | auto comparer = comparator(); 102 | bool check = std::equal(data_m, data_e, actual_b, comparer); 103 | check = check && std::equal(data_b, data_m, std::next(actual_b, std::distance(data_m, data_e)), comparer); 104 | 105 | EXPECT_TRUE(check, "wrong effect of rotate_copy"); 106 | } 107 | }; 108 | 109 | template 110 | void 111 | test() 112 | { 113 | 114 | const std::size_t max_len = 100000; 115 | 116 | Sequence actual(max_len, [](std::size_t i) { return T1(i); }); 117 | 118 | Sequence data(max_len, [](std::size_t i) { return T1(i); }); 119 | 120 | for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 121 | { 122 | std::size_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1}; 123 | for (std::size_t shift : shifts) 124 | { 125 | if (shift > 0 && shift < len) 126 | { 127 | invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), 128 | actual.begin() + len, shift); 129 | invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(), 130 | actual.begin() + len, shift); 131 | } 132 | } 133 | } 134 | } 135 | 136 | int 137 | main() 138 | { 139 | test(); 140 | test(); 141 | test(); 142 | test, wrapper>(); 143 | 144 | std::cout << done() << std::endl; 145 | return 0; 146 | } 147 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/swap_ranges.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- swap_ranges.pass.cpp ----------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | template 23 | struct wrapper 24 | { 25 | T t; 26 | std::size_t number_of_swaps = 0; 27 | wrapper() {} 28 | explicit wrapper(T t_) : t(t_) {} 29 | template 30 | void 31 | operator=(const U& b) 32 | { 33 | t = b; 34 | } 35 | bool 36 | operator==(const wrapper& a) const 37 | { 38 | return t == a.t; 39 | } 40 | }; 41 | 42 | template 43 | void 44 | swap(wrapper& a, wrapper& b) 45 | { 46 | std::swap(a.t, b.t); 47 | a.number_of_swaps++; 48 | b.number_of_swaps++; 49 | } 50 | 51 | template 52 | struct check_swap 53 | { 54 | bool 55 | operator()(T&) 56 | { 57 | return true; 58 | } 59 | }; 60 | 61 | template 62 | struct check_swap> 63 | { 64 | bool 65 | operator()(wrapper& a) 66 | { 67 | bool temp = (a.number_of_swaps == 1); 68 | a.number_of_swaps = 0; 69 | return temp; 70 | } 71 | }; 72 | 73 | struct test_one_policy 74 | { 75 | template 76 | void 77 | operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e) 78 | { 79 | using namespace std; 80 | using T_ref = typename iterator_traits::reference; 81 | using T = typename iterator_traits::value_type; 82 | 83 | iota(data_b, data_e, 0); 84 | iota(actual_b, actual_e, std::distance(data_b, data_e)); 85 | 86 | Iterator2 actual_return = swap_ranges(exec, data_b, data_e, actual_b); 87 | bool check_return = (actual_return == actual_e); 88 | EXPECT_TRUE(check_return, "wrong result of swap_ranges"); 89 | if (check_return) 90 | { 91 | std::size_t i = 0; 92 | bool check = all_of(actual_b, actual_e, [&i](T_ref a) { return a == T(i++); }) && 93 | all_of(data_b, data_e, [&i](T_ref a) { return a == T(i++); }); 94 | 95 | EXPECT_TRUE(check, "wrong effect of swap_ranges"); 96 | 97 | if (check) 98 | { 99 | bool swap_check = 100 | all_of(data_b, data_e, check_swap()) && all_of(actual_b, actual_e, check_swap()); 101 | EXPECT_TRUE(swap_check, "wrong effect of swap_ranges swap check"); 102 | } 103 | } 104 | } 105 | }; 106 | 107 | template 108 | void 109 | test() 110 | { 111 | const std::size_t max_len = 100000; 112 | 113 | Sequence data(max_len); 114 | Sequence actual(max_len); 115 | 116 | for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 117 | { 118 | invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), 119 | actual.begin() + len); 120 | } 121 | } 122 | 123 | int 124 | main() 125 | { 126 | test>(); 127 | test>(); 128 | test(); 129 | test(); 130 | 131 | std::cout << done() << std::endl; 132 | return 0; 133 | } 134 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/transform_binary.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- transform_binary.pass.cpp -----------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | template 22 | class TheOperation 23 | { 24 | Out val; 25 | 26 | public: 27 | TheOperation(Out v) : val(v) {} 28 | Out 29 | operator()(const In1& x, const In2& y) const 30 | { 31 | return Out(val + x - y); 32 | } 33 | }; 34 | 35 | template 36 | void 37 | check_and_reset(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator out_first) 38 | { 39 | typedef typename std::iterator_traits::value_type Out; 40 | typename std::iterator_traits::difference_type k = 0; 41 | for (; first1 != last1; ++first1, ++first2, ++out_first, ++k) 42 | { 43 | // check 44 | Out expected = Out(1.5) + *first1 - *first2; 45 | Out actual = *out_first; 46 | if (std::is_floating_point::value) 47 | { 48 | EXPECT_TRUE((expected > actual ? expected - actual : actual - expected) < 1e7, 49 | "wrong value in output sequence"); 50 | } 51 | else 52 | { 53 | EXPECT_EQ(expected, actual, "wrong value in output sequence"); 54 | } 55 | // reset 56 | *out_first = k % 7 != 4 ? 7 * k - 5 : 0; 57 | } 58 | } 59 | 60 | struct test_one_policy 61 | { 62 | template 64 | void 65 | operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2, 66 | OutputIterator out_first, OutputIterator, BinaryOp op) 67 | { 68 | auto result = std::transform(exec, first1, last1, first2, out_first, op); 69 | (void)result; 70 | check_and_reset(first1, last1, first2, out_first); 71 | } 72 | }; 73 | 74 | template 75 | void 76 | test(Predicate pred) 77 | { 78 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 79 | { 80 | Sequence in1(n, [](size_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; }); 81 | Sequence in2(n, [](size_t k) { return k % 7 != 2 ? 5 * k - 5 : 0; }); 82 | 83 | Sequence out(n, [](size_t) { return -1; }); 84 | 85 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.begin(), in2.end(), out.begin(), 86 | out.end(), pred); 87 | invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cend(), in2.cbegin(), in2.cend(), out.begin(), 88 | out.end(), pred); 89 | } 90 | } 91 | 92 | template 93 | struct test_non_const 94 | { 95 | template 96 | void 97 | operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 98 | { 99 | invoke_if(exec, [&]() { 100 | InputIterator input_iter2 = input_iter; 101 | transform(exec, input_iter, input_iter, input_iter2, out_iter, non_const(std::plus())); 102 | }); 103 | } 104 | }; 105 | 106 | int 107 | main() 108 | { 109 | //const operator() 110 | test(TheOperation(1)); 111 | test(TheOperation(1.5)); 112 | //non-const operator() 113 | test(non_const(TheOperation(1.5))); 114 | test(non_const(TheOperation(1.5))); 115 | //lambda 116 | test([](const int8_t& x, const float64_t& y) { return int8_t(int8_t(1.5) + x - y); }); 117 | 118 | test_algo_basic_double(run_for_rnd_fw>()); 119 | 120 | std::cout << done() << std::endl; 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.modifying.operations/transform_unary.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- transform_unary.pass.cpp ------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | template 22 | void 23 | check_and_reset(InputIterator first, InputIterator last, OutputIterator out_first) 24 | { 25 | typedef typename std::iterator_traits::value_type Out; 26 | typename std::iterator_traits::difference_type k = 0; 27 | for (; first != last; ++first, ++out_first, ++k) 28 | { 29 | // check 30 | Out expected = 1 - *first; 31 | Out actual = *out_first; 32 | EXPECT_EQ(expected, actual, "wrong value in output sequence"); 33 | // reset 34 | *out_first = k % 7 != 4 ? 7 * k - 5 : 0; 35 | } 36 | } 37 | 38 | struct test_one_policy 39 | { 40 | template 41 | void 42 | operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, 43 | OutputIterator out_last, UnaryOp op) 44 | { 45 | auto orr = std::transform(exec, first, last, out_first, op); 46 | EXPECT_TRUE(out_last == orr, "transform returned wrong iterator"); 47 | check_and_reset(first, last, out_first); 48 | } 49 | }; 50 | 51 | template 52 | void 53 | test() 54 | { 55 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 56 | { 57 | Sequence in(n, [](int32_t k) { return k % 5 != 1 ? 3 * k - 7 : 0; }); 58 | 59 | Sequence out(n); 60 | 61 | const auto flip = Complement(1); 62 | invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), out.begin(), out.end(), flip); 63 | invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), out.begin(), out.end(), flip); 64 | } 65 | } 66 | 67 | template 68 | struct test_non_const 69 | { 70 | template 71 | void 72 | operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) 73 | { 74 | invoke_if(exec, [&]() { transform(exec, input_iter, input_iter, out_iter, non_const(std::negate())); }); 75 | } 76 | }; 77 | 78 | int 79 | main() 80 | { 81 | test(); 82 | test(); 83 | test(); 84 | test(); 85 | test(); 86 | 87 | test_algo_basic_double(run_for_rnd_fw>()); 88 | 89 | std::cout << done() << std::endl; 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/adjacent_find.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- adjacent_find.pass.cpp --------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_adjacent_find 22 | { 23 | template 24 | void 25 | operator()(Policy&& exec, Iterator first, Iterator last, Pred pred) 26 | { 27 | using namespace std; 28 | 29 | auto k = std::adjacent_find(first, last, pred); 30 | auto i = adjacent_find(exec, first, last, pred); 31 | EXPECT_TRUE(i == k, "wrong return value from adjacent_find with predicate"); 32 | 33 | i = adjacent_find(exec, first, last); 34 | EXPECT_TRUE(i == k, "wrong return value from adjacent_find without predicate"); 35 | } 36 | }; 37 | 38 | template 39 | void 40 | test_adjacent_find_by_type() 41 | { 42 | 43 | size_t counts[] = {2, 3, 500}; 44 | for (size_t c = 0; c < const_size(counts); ++c) 45 | { 46 | 47 | for (size_t e = 0; e < (counts[c] >= 64 ? 64 : (counts[c] == 2 ? 1 : 2)); ++e) 48 | { 49 | Sequence in(counts[c], [](size_t v) -> T { return T(v); }); //fill 0...n 50 | in[e] = in[e + 1] = -1; //make an adjacent pair 51 | 52 | auto i = std::adjacent_find(in.cbegin(), in.cend(), std::equal_to()); 53 | EXPECT_TRUE(i == in.cbegin() + e, "std::adjacent_find returned wrong result"); 54 | 55 | invoke_on_all_policies(test_adjacent_find(), in.begin(), in.end(), std::equal_to()); 56 | invoke_on_all_policies(test_adjacent_find(), in.cbegin(), in.cend(), std::equal_to()); 57 | } 58 | } 59 | 60 | //special cases: size=0, size=1; 61 | for (size_t expect = 0; expect < 1; ++expect) 62 | { 63 | Sequence in(expect, [](size_t v) -> T { return T(v); }); //fill 0...n 64 | auto i = std::adjacent_find(in.cbegin(), in.cend(), std::equal_to()); 65 | EXPECT_TRUE(i == in.cbegin() + expect, "std::adjacent_find returned wrong result"); 66 | 67 | invoke_on_all_policies(test_adjacent_find(), in.begin(), in.end(), std::equal_to()); 68 | invoke_on_all_policies(test_adjacent_find(), in.cbegin(), in.cend(), std::equal_to()); 69 | } 70 | 71 | //special cases: 72 | Sequence a1 = {5, 5, 5, 6, 7, 8, 9}; 73 | invoke_on_all_policies(test_adjacent_find(), a1.begin(), a1.end(), std::equal_to()); 74 | invoke_on_all_policies(test_adjacent_find(), a1.begin() + 1, a1.end(), std::equal_to()); 75 | 76 | invoke_on_all_policies(test_adjacent_find(), a1.cbegin(), a1.cend(), std::equal_to()); 77 | invoke_on_all_policies(test_adjacent_find(), a1.cbegin() + 1, a1.cend(), std::equal_to()); 78 | 79 | Sequence a2 = {5, 6, 7, 8, 9, 9}; 80 | invoke_on_all_policies(test_adjacent_find(), a2.begin(), a2.end(), std::equal_to()); 81 | invoke_on_all_policies(test_adjacent_find(), a2.begin(), a2.end() - 1, std::equal_to()); 82 | 83 | invoke_on_all_policies(test_adjacent_find(), a2.cbegin(), a2.cend(), std::equal_to()); 84 | invoke_on_all_policies(test_adjacent_find(), a2.cbegin(), a2.cend() - 1, std::equal_to()); 85 | 86 | Sequence a3 = {5, 6, 6, 6, 7, 9, 9, 9, 9}; 87 | invoke_on_all_policies(test_adjacent_find(), a3.begin(), a3.end(), std::equal_to()); 88 | 89 | invoke_on_all_policies(test_adjacent_find(), a3.cbegin(), a3.cend(), std::equal_to()); 90 | } 91 | 92 | template 93 | struct test_non_const 94 | { 95 | template 96 | void 97 | operator()(Policy&& exec, Iterator iter) 98 | { 99 | adjacent_find(exec, iter, iter, non_const(std::equal_to())); 100 | } 101 | }; 102 | 103 | int 104 | main() 105 | { 106 | 107 | test_adjacent_find_by_type(); 108 | test_adjacent_find_by_type(); 109 | 110 | test_algo_basic_single(run_for_rnd_bi>()); 111 | 112 | std::cout << done() << std::endl; 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/all_of.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- all_of.pass.cpp ---------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | /* 20 | TODO: consider implementing the following tests for a better code coverage 21 | - correctness 22 | - bad input argument (if applicable) 23 | - data corruption around/of input and output 24 | - correctly work with nested parallelism 25 | - check that algorithm does not require anything more than is described in its requirements section 26 | */ 27 | 28 | using namespace TestUtils; 29 | 30 | struct test_all_of 31 | { 32 | template 33 | void 34 | operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) 35 | { 36 | 37 | auto actualr = std::all_of(exec, begin, end, pred); 38 | EXPECT_EQ(expected, actualr, "result for all_of"); 39 | } 40 | }; 41 | 42 | template 43 | struct Parity 44 | { 45 | bool parity; 46 | 47 | public: 48 | Parity(bool parity_) : parity(parity_) {} 49 | bool 50 | operator()(T value) const 51 | { 52 | return (size_t(value) ^ parity) % 2 == 0; 53 | } 54 | }; 55 | 56 | template 57 | void 58 | test(size_t bits) 59 | { 60 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 61 | { 62 | 63 | // Sequence of odd values 64 | Sequence in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); 65 | 66 | // Even value, or false when T is bool. 67 | T spike(2 * HashBits(n, bits - 1)); 68 | Sequence inCopy(in); 69 | 70 | invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity(1), true); 71 | invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity(1), true); 72 | EXPECT_EQ(in, inCopy, "all_of modified input sequence"); 73 | if (n > 0) 74 | { 75 | // Sprinkle in a miss 76 | in[2 * n / 3] = spike; 77 | invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity(1), false); 78 | invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity(1), false); 79 | 80 | // Sprinkle in a few more misses 81 | in[n / 2] = spike; 82 | in[n / 3] = spike; 83 | invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity(1), false); 84 | invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity(1), false); 85 | } 86 | } 87 | } 88 | 89 | struct test_non_const 90 | { 91 | template 92 | void 93 | operator()(Policy&& exec, Iterator iter) 94 | { 95 | auto is_even = [&](float64_t v) { 96 | uint32_t i = (uint32_t)v; 97 | return i % 2 == 0; 98 | }; 99 | all_of(exec, iter, iter, non_const(is_even)); 100 | } 101 | }; 102 | 103 | int 104 | main() 105 | { 106 | test(8 * sizeof(int32_t)); 107 | test(8 * sizeof(uint16_t)); 108 | test(53); 109 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 110 | test(1); 111 | #endif 112 | 113 | test_algo_basic_single(run_for_rnd_fw()); 114 | 115 | std::cout << done() << std::endl; 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/any_of.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- any_of.pass.cpp ---------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | /* 20 | TODO: consider implementing the following tests for a better code coverage 21 | - correctness 22 | - bad input argument (if applicable) 23 | - data corruption around/of input and output 24 | - correctly work with nested parallelism 25 | - check that algorithm does not require anything more than is described in its requirements section 26 | */ 27 | 28 | using namespace TestUtils; 29 | 30 | struct test_any_of 31 | { 32 | template 33 | void 34 | operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) 35 | { 36 | 37 | auto actualr = std::any_of(exec, begin, end, pred); 38 | EXPECT_EQ(expected, actualr, "result for any_of"); 39 | } 40 | }; 41 | 42 | template 43 | void 44 | test(size_t bits) 45 | { 46 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 47 | { 48 | 49 | // Sequence of odd values 50 | Sequence in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); 51 | 52 | // Even value, or false when T is bool. 53 | T spike(2 * HashBits(n, bits - 1)); 54 | Sequence inCopy(in); 55 | 56 | invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to(spike), false); 57 | invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to(spike), false); 58 | EXPECT_EQ(in, inCopy, "any_of modified input sequence"); 59 | if (n > 0) 60 | { 61 | // Sprinkle in a hit 62 | in[2 * n / 3] = spike; 63 | invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to(spike), true); 64 | invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to(spike), true); 65 | 66 | // Sprinkle in a few more hits 67 | in[n / 2] = spike; 68 | in[n / 3] = spike; 69 | invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to(spike), true); 70 | invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to(spike), true); 71 | } 72 | } 73 | } 74 | 75 | struct test_non_const 76 | { 77 | template 78 | void 79 | operator()(Policy&& exec, Iterator iter) 80 | { 81 | auto is_even = [&](float64_t v) { 82 | uint32_t i = (uint32_t)v; 83 | return i % 2 == 0; 84 | }; 85 | any_of(exec, iter, iter, non_const(is_even)); 86 | } 87 | }; 88 | 89 | int 90 | main() 91 | { 92 | test(8 * sizeof(int32_t)); 93 | test(8 * sizeof(uint16_t)); 94 | test(53); 95 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 96 | test(1); 97 | #endif 98 | 99 | test_algo_basic_single(run_for_rnd_fw()); 100 | 101 | std::cout << done() << std::endl; 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/count.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- count.pass.cpp ----------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for count and count_if 13 | #include "support/pstl_test_config.h" 14 | 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | struct test_count 23 | { 24 | template 25 | void 26 | operator()(Policy&& exec, Iterator first, Iterator last, T needle) 27 | { 28 | auto expected = std::count(first, last, needle); 29 | auto result = std::count(exec, first, last, needle); 30 | EXPECT_EQ(expected, result, "wrong count result"); 31 | } 32 | }; 33 | 34 | struct test_count_if 35 | { 36 | template 37 | void 38 | operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred) 39 | { 40 | auto expected = std::count_if(first, last, pred); 41 | auto result = std::count_if(exec, first, last, pred); 42 | EXPECT_EQ(expected, result, "wrong count_if result"); 43 | } 44 | }; 45 | 46 | template 47 | class IsEqual 48 | { 49 | T value; 50 | 51 | public: 52 | IsEqual(T value_, OddTag) : value(value_) {} 53 | bool 54 | operator()(const T& x) const 55 | { 56 | return x == value; 57 | } 58 | }; 59 | 60 | template 61 | void 62 | test(T needle, Predicate pred, Convert convert) 63 | { 64 | // Try sequences of various lengths. 65 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 66 | { 67 | Sequence in(n, [=](size_t k) -> In { 68 | // Sprinkle "42" and "50" early, so that short sequences have non-zero count. 69 | return convert((n - k - 1) % 3 == 0 ? 42 : (n - k - 2) % 5 == 0 ? 50 : 3 * (int(k) % 1000 - 500)); 70 | }); 71 | invoke_on_all_policies(test_count(), in.begin(), in.end(), needle); 72 | invoke_on_all_policies(test_count_if(), in.begin(), in.end(), pred); 73 | 74 | invoke_on_all_policies(test_count(), in.cbegin(), in.cend(), needle); 75 | invoke_on_all_policies(test_count_if(), in.cbegin(), in.cend(), pred); 76 | } 77 | } 78 | 79 | struct test_non_const 80 | { 81 | template 82 | void 83 | operator()(Policy&& exec, Iterator iter) 84 | { 85 | auto is_even = [&](float64_t v) { 86 | uint32_t i = (uint32_t)v; 87 | return i % 2 == 0; 88 | }; 89 | count_if(exec, iter, iter, non_const(is_even)); 90 | } 91 | }; 92 | 93 | int 94 | main() 95 | { 96 | test(42, IsEqual(50, OddTag()), [](int32_t j) { return j; }); 97 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN 98 | test(42, [](const int32_t&) { return true; }, [](int32_t j) { return j; }); 99 | #endif 100 | test(42, IsEqual(50, OddTag()), [](int32_t j) { return float64_t(j); }); 101 | test(Number(42, OddTag()), IsEqual(Number(50, OddTag()), OddTag()), 102 | [](int32_t j) { return Number(j, OddTag()); }); 103 | 104 | test_algo_basic_single(run_for_rnd_fw()); 105 | 106 | std::cout << done() << std::endl; 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/equal.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- equal.pass.cpp ----------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | #define CPP14_ENABLED 0 22 | 23 | struct UserType 24 | { 25 | size_t key; 26 | float32_t f; 27 | float64_t d; 28 | int32_t i; 29 | 30 | bool 31 | operator()(UserType a, UserType b) 32 | { 33 | return a.key < b.key; 34 | } 35 | bool 36 | operator<(UserType a) 37 | { 38 | return a.key < key; 39 | } 40 | bool 41 | operator>=(UserType a) 42 | { 43 | return a.key <= key; 44 | } 45 | bool 46 | operator<=(UserType a) 47 | { 48 | return a.key >= key; 49 | } 50 | bool 51 | operator==(UserType a) 52 | { 53 | return a.key == key; 54 | } 55 | bool 56 | operator==(UserType a) const 57 | { 58 | return a.key == key; 59 | } 60 | bool 61 | operator!=(UserType a) 62 | { 63 | return a.key != key; 64 | } 65 | UserType operator!() 66 | { 67 | UserType tmp; 68 | tmp.key = !key; 69 | return tmp; 70 | } 71 | friend std::ostream& 72 | operator<<(std::ostream& stream, const UserType a) 73 | { 74 | stream << a.key; 75 | return stream; 76 | } 77 | 78 | UserType() : key(-1), f(0.0f), d(0.0), i(0) {} 79 | UserType(size_t Number) : key(Number), f(0.0f), d(0.0), i(0) {} 80 | UserType& 81 | operator=(const UserType& other) 82 | { 83 | key = other.key; 84 | return *this; 85 | } 86 | UserType(const UserType& other) : key(other.key), f(other.f), d(other.d), i(other.i) {} 87 | UserType(UserType&& other) : key(other.key), f(other.f), d(other.d), i(other.i) 88 | { 89 | other.key = -1; 90 | other.f = 0.0f; 91 | other.d = 0.0; 92 | other.i = 0; 93 | } 94 | }; 95 | 96 | struct test_one_policy 97 | { 98 | template 99 | void 100 | operator()(ExecutionPolicy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2, bool is_true_equal) 101 | { 102 | using namespace std; 103 | 104 | auto expected = equal(first1, last1, first2); 105 | auto actual = equal(exec, first1, last1, first2); 106 | EXPECT_EQ(expected, actual, "result for equal for random-access iterator, checking against std::equal()"); 107 | 108 | // testing bool 109 | EXPECT_TRUE(is_true_equal == actual, "result for equal for random-access iterator, bool"); 110 | 111 | //add C++14 equal symantics tests 112 | //add more cases for inCopy size less than in 113 | #if CPP14_ENABLED 114 | auto actualr14 = std::equal(in.cbegin(), in.cend(), inCopy.cbegin(), inCopy.cend()); 115 | EXPECT_EQ(expected, actualr14, "result for equal for random-access iterator"); 116 | #endif 117 | } 118 | }; 119 | 120 | template 121 | void 122 | test(size_t bits) 123 | { 124 | for (size_t n = 1; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 125 | { 126 | 127 | // Sequence of odd values 128 | Sequence in(n, [bits](size_t k) { return T(2 * HashBits(k, bits - 1) ^ 1); }); 129 | Sequence inCopy(in); 130 | 131 | invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), inCopy.begin(), true); 132 | invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), inCopy.cbegin(), true); 133 | 134 | // testing bool !equal() 135 | inCopy[0] = !inCopy[0]; 136 | invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), inCopy.begin(), false); 137 | invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), inCopy.cbegin(), false); 138 | } 139 | } 140 | 141 | template 142 | struct test_non_const 143 | { 144 | template 145 | void 146 | operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) 147 | { 148 | equal(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to())); 149 | } 150 | }; 151 | 152 | int 153 | main() 154 | { 155 | 156 | test(8 * sizeof(int32_t)); 157 | test(8 * sizeof(uint16_t)); 158 | test(53); 159 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 160 | test(1); 161 | #endif 162 | test(256); 163 | 164 | test_algo_basic_double(run_for_rnd_fw>()); 165 | 166 | std::cout << done() << std::endl; 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/find.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- find.pass.cpp -----------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for find 13 | #include "support/pstl_test_config.h" 14 | 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | struct test_find 23 | { 24 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 25 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 26 | template 27 | void 28 | operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Value value) 29 | { 30 | } 31 | template 32 | void 33 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Value value) 34 | { 35 | } 36 | #endif 37 | 38 | template 39 | void 40 | operator()(Policy&& exec, Iterator first, Iterator last, Value value) 41 | { 42 | auto i = std::find(first, last, value); 43 | auto j = find(exec, first, last, value); 44 | EXPECT_TRUE(i == j, "wrong return value from find"); 45 | } 46 | }; 47 | 48 | template 49 | void 50 | test(Value value, Hit hit, Miss miss) 51 | { 52 | // Try sequences of various lengths. 53 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 54 | { 55 | Sequence in(n, [&](size_t k) -> T { return miss(n ^ k); }); 56 | // Try different find positions, including not found. 57 | // By going backwards, we can add extra matches that are *not* supposed to be found. 58 | // The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. 59 | for (size_t m = n; m > 0; m *= 0.6) 60 | { 61 | if (m < n) 62 | in[m] = hit(n ^ m); 63 | invoke_on_all_policies(test_find(), in.begin(), in.end(), value); 64 | invoke_on_all_policies(test_find(), in.cbegin(), in.cend(), value); 65 | } 66 | } 67 | } 68 | 69 | // Type defined for sake of checking that std::find works with asymmetric ==. 70 | class Weird 71 | { 72 | Number value; 73 | 74 | public: 75 | friend bool 76 | operator==(Number x, Weird y) 77 | { 78 | return x == y.value; 79 | } 80 | Weird(int32_t val, OddTag) : value(val, OddTag()) {} 81 | }; 82 | 83 | int 84 | main() 85 | { 86 | // Note that the "hit" and "miss" functions here avoid overflow issues. 87 | test(Weird(42, OddTag()), [](int32_t) { return Number(42, OddTag()); }, // hit 88 | [](int32_t j) { return Number(j == 42 ? 0 : j, OddTag()); }); // miss 89 | 90 | // Test with value that is equal to two different bit patterns (-0.0 and 0.0) 91 | test(-0.0, [](int32_t j) { return j & 1 ? 0.0 : -0.0; }, // hit 92 | [](int32_t j) { return j == 0 ? ~j : j; }); // miss 93 | 94 | std::cout << done() << std::endl; 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/find_end.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- find_end.pass.cpp -------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_one_policy 22 | { 23 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 24 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 25 | template 26 | void 27 | operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, 28 | Predicate pred) 29 | { 30 | } 31 | template 32 | void 33 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, 34 | Predicate pred) 35 | { 36 | } 37 | #endif 38 | 39 | template 40 | void 41 | operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred) 42 | { 43 | using namespace std; 44 | // For find_end 45 | { 46 | auto expected = find_end(b, e, bsub, esub, pred); 47 | auto actual = find_end(exec, b, e, bsub, esub); 48 | EXPECT_TRUE(actual == expected, "wrong return result from find_end"); 49 | 50 | actual = find_end(exec, b, e, bsub, esub, pred); 51 | EXPECT_TRUE(actual == expected, "wrong return result from find_end with a predicate"); 52 | } 53 | 54 | // For search 55 | { 56 | auto expected = search(b, e, bsub, esub, pred); 57 | auto actual = search(exec, b, e, bsub, esub); 58 | EXPECT_TRUE(actual == expected, "wrong return result from search"); 59 | 60 | actual = search(exec, b, e, bsub, esub, pred); 61 | EXPECT_TRUE(actual == expected, "wrong return result from search with a predicate"); 62 | } 63 | } 64 | }; 65 | 66 | template 67 | void 68 | test(const std::size_t bits) 69 | { 70 | 71 | const std::size_t max_n1 = 1000; 72 | const std::size_t max_n2 = (max_n1 * 10) / 8; 73 | Sequence in(max_n1, [bits](std::size_t) { return T(2 * HashBits(max_n1, bits - 1) ^ 1); }); 74 | Sequence sub(max_n2, [bits](std::size_t) { return T(2 * HashBits(max_n1, bits - 1)); }); 75 | for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) 76 | { 77 | std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8}; 78 | std::size_t res[] = {0, 1, n1 / 2, n1}; 79 | for (auto n2 : sub_n) 80 | { 81 | for (auto r : res) 82 | { 83 | std::size_t i = r, isub = 0; 84 | for (; i < n1 & isub < n2; ++i, ++isub) 85 | in[i] = sub[isub]; 86 | invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, sub.begin(), sub.begin() + n2, 87 | std::equal_to()); 88 | invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, sub.cbegin(), 89 | sub.cbegin() + n2, std::equal_to()); 90 | } 91 | } 92 | } 93 | } 94 | 95 | template 96 | struct test_non_const 97 | { 98 | template 99 | void 100 | operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) 101 | { 102 | invoke_if(exec, [&]() { 103 | find_end(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to())); 104 | search(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to())); 105 | }); 106 | } 107 | }; 108 | 109 | int 110 | main() 111 | { 112 | test(8 * sizeof(int32_t)); 113 | test(8 * sizeof(uint16_t)); 114 | test(53); 115 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 116 | test(1); 117 | #endif 118 | 119 | test_algo_basic_double(run_for_rnd_fw>()); 120 | 121 | std::cout << done() << std::endl; 122 | return 0; 123 | } 124 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/find_first_of.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- find_first_of.pass.cpp --------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_one_policy 22 | { 23 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 24 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 25 | template 26 | void 27 | operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, 28 | Predicate pred) 29 | { 30 | } 31 | template 32 | void 33 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, 34 | Predicate pred) 35 | { 36 | } 37 | #endif 38 | 39 | template 40 | void 41 | operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred) 42 | { 43 | using namespace std; 44 | Iterator1 expected = find_first_of(b, e, bsub, esub, pred); 45 | Iterator1 actual = find_first_of(exec, b, e, bsub, esub, pred); 46 | EXPECT_TRUE(actual == expected, "wrong return result from find_first_of with a predicate"); 47 | 48 | expected = find_first_of(b, e, bsub, esub); 49 | actual = find_first_of(exec, b, e, bsub, esub); 50 | EXPECT_TRUE(actual == expected, "wrong return result from find_first_of"); 51 | } 52 | }; 53 | 54 | template 55 | void 56 | test(Predicate pred) 57 | { 58 | 59 | const std::size_t max_n1 = 1000; 60 | const std::size_t max_n2 = (max_n1 * 10) / 8; 61 | Sequence in1(max_n1, [](std::size_t) { return T(1); }); 62 | Sequence in2(max_n2, [](std::size_t) { return T(0); }); 63 | for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) 64 | { 65 | std::size_t sub_n[] = {0, 1, n1 / 3, n1, (n1 * 10) / 8}; 66 | for (const auto n2 : sub_n) 67 | { 68 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.data(), in2.data() + n2, pred); 69 | 70 | in2[n2 / 2] = T(1); 71 | invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.data(), in2.data() + n2, 72 | pred); 73 | 74 | if (n2 >= 3) 75 | { 76 | in2[2 * n2 / 3] = T(1); 77 | invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.begin(), 78 | in2.begin() + n2, pred); 79 | in2[2 * n2 / 3] = T(0); 80 | } 81 | in2[n2 / 2] = T(0); 82 | } 83 | } 84 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n1 / 10, in1.data(), 85 | in1.data() + max_n1 / 10, pred); 86 | } 87 | 88 | template 89 | struct test_non_const 90 | { 91 | template 92 | void 93 | operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) 94 | { 95 | invoke_if(exec, [&]() { 96 | find_first_of(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to())); 97 | }); 98 | } 99 | }; 100 | 101 | int 102 | main() 103 | { 104 | test(std::equal_to()); 105 | test(std::not_equal_to()); 106 | test([](const float64_t x, const float64_t y) { return x * x == y * y; }); 107 | 108 | test_algo_basic_double(run_for_rnd_fw>()); 109 | 110 | std::cout << done() << std::endl; 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/find_if.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- find_if.pass.cpp --------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for find_if and find_if_not 13 | #include "support/pstl_test_config.h" 14 | 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | struct test_find_if 23 | { 24 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 25 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 26 | template 27 | void 28 | operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Predicate pred, 29 | NotPredicate not_pred) 30 | { 31 | } 32 | template 33 | void 34 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Predicate pred, 35 | NotPredicate not_pred) 36 | { 37 | } 38 | #endif 39 | 40 | template 41 | void 42 | operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred, NotPredicate not_pred) 43 | { 44 | auto i = std::find_if(first, last, pred); 45 | auto j = find_if(exec, first, last, pred); 46 | EXPECT_TRUE(i == j, "wrong return value from find_if"); 47 | auto i_not = find_if_not(exec, first, last, not_pred); 48 | EXPECT_TRUE(i_not == i, "wrong return value from find_if_not"); 49 | } 50 | }; 51 | 52 | template 53 | void 54 | test(Predicate pred, Hit hit, Miss miss) 55 | { 56 | auto not_pred = [pred](T x) { return !pred(x); }; 57 | // Try sequences of various lengths. 58 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 59 | { 60 | Sequence in(n, [&](size_t k) -> T { return miss(n ^ k); }); 61 | // Try different find positions, including not found. 62 | // By going backwards, we can add extra matches that are *not* supposed to be found. 63 | // The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. 64 | for (size_t m = n; m > 0; m *= 0.6) 65 | { 66 | if (m < n) 67 | in[m] = hit(n ^ m); 68 | invoke_on_all_policies(test_find_if(), in.begin(), in.end(), pred, not_pred); 69 | invoke_on_all_policies(test_find_if(), in.cbegin(), in.cend(), pred, not_pred); 70 | } 71 | } 72 | } 73 | 74 | struct test_non_const 75 | { 76 | template 77 | void 78 | operator()(Policy&& exec, Iterator iter) 79 | { 80 | auto is_even = [&](float64_t v) { 81 | uint32_t i = (uint32_t)v; 82 | return i % 2 == 0; 83 | }; 84 | 85 | invoke_if(exec, [&]() { 86 | find_if(exec, iter, iter, non_const(is_even)); 87 | find_if_not(exec, iter, iter, non_const(is_even)); 88 | }); 89 | } 90 | }; 91 | 92 | int 93 | main() 94 | { 95 | #if !_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN 96 | // Note that the "hit" and "miss" functions here avoid overflow issues. 97 | test(IsMultiple(5, OddTag()), [](int32_t j) { return Number(j - j % 5, OddTag()); }, // hit 98 | [](int32_t j) { return Number(j % 5 == 0 ? j ^ 1 : j, OddTag()); }); // miss 99 | #endif 100 | 101 | // Try type for which algorithm can really be vectorized. 102 | test([](float32_t x) { return x >= 0; }, [](float32_t j) { return j * j; }, 103 | [](float32_t j) { return -1 - j * j; }); 104 | 105 | test_algo_basic_single(run_for_rnd_fw()); 106 | 107 | std::cout << done() << std::endl; 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/for_each.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- for_each.pass.cpp -------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | template 22 | struct Gen 23 | { 24 | Type 25 | operator()(std::size_t k) 26 | { 27 | return Type(k % 5 != 1 ? 3 * k - 7 : 0); 28 | }; 29 | }; 30 | 31 | template 32 | struct Flip 33 | { 34 | int32_t val; 35 | Flip(int32_t y) : val(y) {} 36 | T 37 | operator()(T& x) const 38 | { 39 | return x = val - x; 40 | } 41 | }; 42 | 43 | struct test_one_policy 44 | { 45 | template 46 | void 47 | operator()(Policy&& exec, Iterator first, Iterator last, Iterator expected_first, Iterator expected_last, Size n) 48 | { 49 | typedef typename std::iterator_traits::value_type T; 50 | 51 | // Try for_each 52 | std::for_each(expected_first, expected_last, Flip(1)); 53 | for_each(exec, first, last, Flip(1)); 54 | EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each"); 55 | 56 | // Try for_each_n 57 | std::for_each_n(std::execution::seq, expected_first, n, Flip(1)); 58 | for_each_n(exec, first, n, Flip(1)); 59 | EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each_n"); 60 | } 61 | }; 62 | 63 | template 64 | void 65 | test() 66 | { 67 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 68 | { 69 | Sequence inout(n, Gen()); 70 | Sequence expected(n, Gen()); 71 | invoke_on_all_policies(test_one_policy(), inout.begin(), inout.end(), expected.begin(), expected.end(), 72 | inout.size()); 73 | } 74 | } 75 | 76 | struct test_non_const 77 | { 78 | template 79 | void 80 | operator()(Policy&& exec, Iterator iter) 81 | { 82 | invoke_if(exec, [&]() { 83 | auto f = [](typename std::iterator_traits::reference x) { x = x + 1; }; 84 | 85 | for_each(exec, iter, iter, non_const(f)); 86 | for_each_n(exec, iter, 0, non_const(f)); 87 | }); 88 | } 89 | }; 90 | 91 | int 92 | main() 93 | { 94 | test(); 95 | test(); 96 | test(); 97 | 98 | test_algo_basic_single(run_for_rnd_fw()); 99 | 100 | std::cout << done() << std::endl; 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/mismatch.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- mismatch.pass.cpp -------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_mismatch 22 | { 23 | template 24 | void 25 | operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2) 26 | { 27 | using namespace std; 28 | typedef typename iterator_traits::value_type T; 29 | { 30 | const auto expected = std::mismatch(first1, last1, first2, std::equal_to()); 31 | const auto res3 = mismatch(exec, first1, last1, first2, std::equal_to()); 32 | EXPECT_TRUE(expected == res3, "wrong return result from mismatch"); 33 | const auto res4 = mismatch(exec, first1, last1, first2); 34 | EXPECT_TRUE(expected == res4, "wrong return result from mismatch"); 35 | } 36 | } 37 | template 38 | void 39 | operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) 40 | { 41 | using namespace std; 42 | typedef typename iterator_traits::value_type T; 43 | { 44 | const auto expected = mismatch(std::execution::seq, first1, last1, first2, last2, std::equal_to()); 45 | const auto res1 = mismatch(exec, first1, last1, first2, last2, std::equal_to()); 46 | EXPECT_TRUE(expected == res1, "wrong return result from mismatch"); 47 | const auto res2 = mismatch(exec, first1, last1, first2, last2); 48 | EXPECT_TRUE(expected == res2, "wrong return result from mismatch"); 49 | } 50 | } 51 | }; 52 | 53 | template 54 | void 55 | test_mismatch_by_type() 56 | { 57 | using namespace std; 58 | for (size_t size = 0; size <= 100000; size = size <= 16 ? size + 1 : size_t(3.1415 * size)) 59 | { 60 | const T val = T(-1); 61 | Sequence in(size, [](size_t v) -> T { return T(v % 100); }); 62 | { 63 | Sequence in2(in); 64 | invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin(), in2.end()); 65 | invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin()); 66 | 67 | const size_t min_size = 3; 68 | if (size > min_size) 69 | { 70 | const size_t idx_for_1 = size / min_size; 71 | in[idx_for_1] = val, in[idx_for_1 + 1] = val, in[idx_for_1 + 2] = val; 72 | invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin(), in2.end()); 73 | invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin()); 74 | } 75 | 76 | const size_t idx_for_2 = 500; 77 | if (size >= idx_for_2 - 1) 78 | { 79 | in2[size / idx_for_2] = val; 80 | invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend()); 81 | invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin()); 82 | } 83 | } 84 | { 85 | Sequence in2(100, [](size_t v) -> T { return T(v); }); 86 | invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin(), in.end()); 87 | // We can't call std::mismatch with semantic below when size of second sequence less than size of first sequence 88 | if (in2.size() <= in.size()) 89 | invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin()); 90 | 91 | const size_t idx = 97; 92 | in2[idx] = val; 93 | in2[idx + 1] = val; 94 | invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend()); 95 | if (in.size() <= in2.size()) 96 | invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin()); 97 | } 98 | { 99 | Sequence in2({}); 100 | invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin(), in.end()); 101 | 102 | invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend()); 103 | if (in.size() == 0) 104 | invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin()); 105 | } 106 | } 107 | } 108 | 109 | template 110 | struct test_non_const 111 | { 112 | template 113 | void 114 | operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) 115 | { 116 | mismatch(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::less())); 117 | } 118 | }; 119 | 120 | int 121 | main() 122 | { 123 | 124 | test_mismatch_by_type(); 125 | test_mismatch_by_type(); 126 | test_mismatch_by_type>(); 127 | 128 | test_algo_basic_double(run_for_rnd_fw>()); 129 | 130 | std::cout << done() << std::endl; 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/none_of.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- none_of.pass.cpp --------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | /* 20 | TODO: consider implementing the following tests for a better code coverage 21 | - correctness 22 | - bad input argument (if applicable) 23 | - data corruption around/of input and output 24 | - correctly work with nested parallelism 25 | - check that algorithm does not require anything more than is described in its requirements section 26 | */ 27 | 28 | using namespace TestUtils; 29 | 30 | struct test_none_of 31 | { 32 | template 33 | void 34 | operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) 35 | { 36 | 37 | auto actualr = std::none_of(exec, begin, end, pred); 38 | EXPECT_EQ(expected, actualr, "result for none_of"); 39 | } 40 | }; 41 | 42 | template 43 | void 44 | test(size_t bits) 45 | { 46 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 47 | { 48 | 49 | // Sequence of odd values 50 | Sequence in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); 51 | 52 | // Even value, or false when T is bool. 53 | T spike(2 * HashBits(n, bits - 1)); 54 | 55 | invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to(spike), true); 56 | invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to(spike), true); 57 | if (n > 0) 58 | { 59 | // Sprinkle in a hit 60 | in[2 * n / 3] = spike; 61 | invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to(spike), false); 62 | invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to(spike), false); 63 | 64 | // Sprinkle in a few more hits 65 | in[n / 3] = spike; 66 | in[n / 2] = spike; 67 | invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to(spike), false); 68 | invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to(spike), false); 69 | } 70 | } 71 | } 72 | 73 | struct test_non_const 74 | { 75 | template 76 | void 77 | operator()(Policy&& exec, Iterator iter) 78 | { 79 | auto is_even = [&](float64_t v) { 80 | uint32_t i = (uint32_t)v; 81 | return i % 2 == 0; 82 | }; 83 | none_of(exec, iter, iter, non_const(is_even)); 84 | } 85 | }; 86 | 87 | int 88 | main() 89 | { 90 | test(8 * sizeof(int32_t)); 91 | test(8 * sizeof(uint16_t)); 92 | test(53); 93 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 94 | test(1); 95 | #endif 96 | 97 | test_algo_basic_single(run_for_rnd_fw()); 98 | 99 | std::cout << done() << std::endl; 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.nonmodifying/search_n.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- search_n.pass.cpp -------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_one_policy 22 | { 23 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 24 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 25 | template 26 | void 27 | operator()(pstl::execution::unsequenced_policy, Iterator b, Iterator e, Size count, const T& value, Predicate pred) 28 | { 29 | } 30 | template 31 | void 32 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator b, Iterator e, Size count, const T& value, 33 | Predicate pred) 34 | { 35 | } 36 | #endif 37 | 38 | template 39 | void 40 | operator()(ExecutionPolicy&& exec, Iterator b, Iterator e, Size count, const T& value, Predicate pred) 41 | { 42 | using namespace std; 43 | auto expected = search_n(b, e, count, value, pred); 44 | auto actual = search_n(exec, b, e, count, value); 45 | EXPECT_TRUE(actual == expected, "wrong return result from search_n"); 46 | 47 | actual = search_n(exec, b, e, count, value, pred); 48 | EXPECT_TRUE(actual == expected, "wrong return result from search_n with a predicate"); 49 | } 50 | }; 51 | 52 | template 53 | void 54 | test() 55 | { 56 | 57 | const std::size_t max_n1 = 100000; 58 | const T value = T(1); 59 | for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1)) 60 | { 61 | std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8}; 62 | std::size_t res[] = {0, 1, n1 / 2, n1}; 63 | for (auto n2 : sub_n) 64 | { 65 | // Some of standard libraries return "first" in this case. We return "last" according to the standard 66 | if (n2 == 0) 67 | { 68 | continue; 69 | } 70 | for (auto r : res) 71 | { 72 | Sequence in(n1, [](std::size_t) { return T(0); }); 73 | std::size_t i = r, isub = 0; 74 | for (; i < n1 & isub < n2; ++i, ++isub) 75 | in[i] = value; 76 | 77 | invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, n2, value, std::equal_to()); 78 | invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, n2, value, std::equal_to()); 79 | } 80 | } 81 | } 82 | } 83 | 84 | template 85 | struct test_non_const 86 | { 87 | template 88 | void 89 | operator()(Policy&& exec, Iterator iter) 90 | { 91 | invoke_if(exec, [&]() { search_n(exec, iter, iter, 0, T(0), non_const(std::equal_to())); }); 92 | } 93 | }; 94 | 95 | int 96 | main() 97 | { 98 | test(); 99 | test(); 100 | test(); 101 | #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 102 | test(); 103 | #endif 104 | 105 | test_algo_basic_single(run_for_rnd_fw>()); 106 | 107 | std::cout << done() << std::endl; 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.sorting/alg.heap.operations/is_heap.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- is_heap.pass.cpp --------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for is_heap, is_heap_until 13 | #include "support/pstl_test_config.h" 14 | 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | #include 20 | 21 | using namespace TestUtils; 22 | 23 | struct WithCmpOp 24 | { 25 | int32_t _first; 26 | int32_t _second; 27 | WithCmpOp() : _first(0), _second(0){}; 28 | explicit WithCmpOp(int32_t x) : _first(x), _second(x){}; 29 | bool 30 | operator<(const WithCmpOp& rhs) const 31 | { 32 | return this->_first < rhs._first; 33 | } 34 | }; 35 | 36 | struct test_is_heap 37 | { 38 | #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 39 | _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 40 | template 41 | typename std::enable_if::value, void>::type 42 | operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Predicate pred) 43 | { 44 | } 45 | template 46 | typename std::enable_if::value, void>::type 47 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Predicate pred) 48 | { 49 | } 50 | #endif 51 | 52 | template 53 | typename std::enable_if::value, void>::type 54 | operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred) 55 | { 56 | using namespace std; 57 | // is_heap 58 | { 59 | bool expected = is_heap(first, last); 60 | bool actual = is_heap(exec, first, last); 61 | EXPECT_TRUE(expected == actual, "wrong return value from is_heap"); 62 | } 63 | // is_heap with predicate 64 | { 65 | bool expected = is_heap(first, last, pred); 66 | bool actual = is_heap(exec, first, last, pred); 67 | EXPECT_TRUE(expected == actual, "wrong return value from is_heap with predicate"); 68 | } 69 | // is_heap_until 70 | { 71 | Iterator expected = is_heap_until(first, last); 72 | Iterator actual = is_heap_until(exec, first, last); 73 | EXPECT_TRUE(expected == actual, "wrong return value from is_heap_until"); 74 | } 75 | // is_heap_until with predicate 76 | { 77 | const Iterator expected = is_heap_until(first, last, pred); 78 | const auto y = std::distance(first, expected); 79 | const Iterator actual = is_heap_until(exec, first, last, pred); 80 | const auto x = std::distance(first, actual); 81 | EXPECT_TRUE(expected == actual, "wrong return value from is_heap_until with predicate"); 82 | EXPECT_EQ(x, y, "both iterators should be the same distance away from 'first'"); 83 | } 84 | } 85 | 86 | // is_heap, is_heap_until works only with random access iterators 87 | template 88 | typename std::enable_if::value, void>::type 89 | operator()(Policy&&, Iterator, Iterator, Predicate) 90 | { 91 | } 92 | }; 93 | 94 | template 95 | void 96 | test_is_heap_by_type(Comp comp) 97 | { 98 | using namespace std; 99 | 100 | const size_t max_size = 100000; 101 | for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 102 | { 103 | Sequence in(n, [](size_t v) -> T { return T(v); }); 104 | 105 | invoke_on_all_policies(test_is_heap(), in.begin(), in.end(), comp); 106 | 107 | std::make_heap(in.begin(), in.begin() + n / 4, comp); 108 | invoke_on_all_policies(test_is_heap(), in.cbegin(), in.cend(), comp); 109 | 110 | std::make_heap(in.begin(), in.begin() + n / 3, comp); 111 | invoke_on_all_policies(test_is_heap(), in.begin(), in.end(), comp); 112 | 113 | std::make_heap(in.begin(), in.end(), comp); 114 | invoke_on_all_policies(test_is_heap(), in.cbegin(), in.cend(), comp); 115 | } 116 | 117 | Sequence in(max_size / 10, [](size_t) -> T { return T(1); }); 118 | invoke_on_all_policies(test_is_heap(), in.begin(), in.end(), comp); 119 | } 120 | 121 | template 122 | struct test_non_const 123 | { 124 | template 125 | void 126 | operator()(Policy&& exec, Iterator iter) 127 | { 128 | invoke_if(exec, [&]() { 129 | is_heap(exec, iter, iter, non_const(std::less())); 130 | is_heap_until(exec, iter, iter, non_const(std::less())); 131 | }); 132 | } 133 | }; 134 | 135 | int 136 | main() 137 | { 138 | test_is_heap_by_type(std::greater()); 139 | test_is_heap_by_type(std::less()); 140 | test_is_heap_by_type([](uint64_t x, uint64_t y) { return x % 100 < y % 100; }); 141 | 142 | test_algo_basic_single(run_for_rnd>()); 143 | 144 | std::cout << done() << std::endl; 145 | return 0; 146 | } 147 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.sorting/alg.set.operations/includes.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- includes.pass.cpp -------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "support/utils.h" 20 | 21 | using namespace TestUtils; 22 | 23 | template 24 | struct Num 25 | { 26 | T val; 27 | explicit Num(const T& v) : val(v) {} 28 | 29 | //for "includes" checks 30 | template 31 | bool 32 | operator<(const Num& v1) const 33 | { 34 | return val < v1.val; 35 | } 36 | 37 | //The types Type1 and Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to both of them 38 | template 39 | operator Num() const 40 | { 41 | return Num((T1)val); 42 | } 43 | }; 44 | 45 | struct test_one_policy 46 | { 47 | template 48 | typename std::enable_if::value, void>::type 49 | operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, 50 | Compare comp) 51 | { 52 | 53 | auto expect_res = std::includes(first1, last1, first2, last2, comp); 54 | auto res = std::includes(exec, first1, last1, first2, last2, comp); 55 | 56 | EXPECT_TRUE(expect_res == res, "wrong result for includes"); 57 | } 58 | 59 | template 60 | typename std::enable_if::value, void>::type 61 | operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare) 62 | { 63 | } 64 | }; 65 | 66 | template 67 | void 68 | test_includes(Compare compare) 69 | { 70 | 71 | const std::size_t n_max = 1000000; 72 | 73 | // The rand()%(2*n+1) encourages generation of some duplicates. 74 | std::srand(42); 75 | 76 | for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 77 | { 78 | for (std::size_t m = 0; m < n_max; m = m <= 16 ? m + 1 : size_t(2.71828 * m)) 79 | { 80 | //prepare the input ranges 81 | Sequence in1(n, [](std::size_t k) { return rand() % (2 * k + 1); }); 82 | Sequence in2(m, [](std::size_t k) { return rand() % (k + 1); }); 83 | 84 | std::sort(in1.begin(), in1.end(), compare); 85 | std::sort(in2.begin(), in2.end(), compare); 86 | 87 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), compare); 88 | 89 | //test w/ non constant predicate 90 | if (n < 5 && m < 5) 91 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), 92 | non_const(compare)); 93 | } 94 | } 95 | } 96 | 97 | int 98 | main() 99 | { 100 | 101 | test_includes(std::less<>()); 102 | test_includes, Num>([](const Num& x, const Num& y) { return x < y; }); 103 | std::cout << done() << std::endl; 104 | 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.sorting/is_sorted.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- is_sorted.pass.cpp ------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_is_sorted 22 | { 23 | template 24 | void 25 | operator()(Policy&& exec, Iterator first, Iterator last, bool exam) 26 | { 27 | using namespace std; 28 | typedef typename std::iterator_traits::value_type T; 29 | 30 | //try random-access iterator 31 | bool res = is_sorted(exec, first, last); 32 | EXPECT_TRUE(exam == res, "is_sorted wrong result for random-access iterator"); 33 | auto iexam = is_sorted_until(first, last); 34 | auto ires = is_sorted_until(exec, first, last); 35 | EXPECT_TRUE(iexam == ires, "is_sorted_until wrong result for random-access iterator"); 36 | 37 | //try random-access iterator with a predicate 38 | res = is_sorted(exec, first, last, std::less()); 39 | EXPECT_TRUE(exam == res, "is_sorted wrong result for random-access iterator"); 40 | iexam = is_sorted_until(first, last, std::less()); 41 | ires = is_sorted_until(exec, first, last, std::less()); 42 | EXPECT_TRUE(iexam == ires, "is_sorted_until wrong result for random-access iterator"); 43 | } 44 | }; 45 | 46 | template 47 | void 48 | test_is_sorted_by_type() 49 | { 50 | 51 | Sequence in(99999, [](size_t v) -> T { return T(v); }); //fill 0..n 52 | 53 | invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); 54 | invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); 55 | 56 | in[in.size() / 2] = -1; 57 | invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); 58 | invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); 59 | 60 | in[1] = -1; 61 | invoke_on_all_policies(test_is_sorted(), in.begin(), in.end(), std::is_sorted(in.begin(), in.end())); 62 | invoke_on_all_policies(test_is_sorted(), in.cbegin(), in.cend(), std::is_sorted(in.begin(), in.end())); 63 | 64 | //an empty container 65 | Sequence in0(0); 66 | invoke_on_all_policies(test_is_sorted(), in0.begin(), in0.end(), std::is_sorted(in0.begin(), in0.end())); 67 | invoke_on_all_policies(test_is_sorted(), in0.cbegin(), in0.cend(), std::is_sorted(in0.begin(), in0.end())); 68 | 69 | //non-descending order 70 | Sequence in1(9, [](size_t) -> T { return T(0); }); 71 | invoke_on_all_policies(test_is_sorted(), in1.begin(), in1.end(), std::is_sorted(in1.begin(), in1.end())); 72 | invoke_on_all_policies(test_is_sorted(), in1.cbegin(), in1.cend(), std::is_sorted(in1.begin(), in1.end())); 73 | } 74 | 75 | template 76 | struct test_non_const 77 | { 78 | template 79 | void 80 | operator()(Policy&& exec, Iterator iter) 81 | { 82 | is_sorted(exec, iter, iter, std::less()); 83 | is_sorted_until(exec, iter, iter, std::less()); 84 | } 85 | }; 86 | 87 | int 88 | main() 89 | { 90 | 91 | test_is_sorted_by_type(); 92 | test_is_sorted_by_type(); 93 | 94 | test_is_sorted_by_type>(); 95 | 96 | test_algo_basic_single(run_for_rnd_fw>()); 97 | 98 | std::cout << done() << std::endl; 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /test/std/algorithms/alg.sorting/partial_sort.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- partial_sort.pass.cpp ---------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "support/utils.h" 19 | 20 | using namespace TestUtils; 21 | 22 | static std::atomic count_val; 23 | static std::atomic count_comp; 24 | 25 | template 26 | struct Num 27 | { 28 | T val; 29 | 30 | Num() { ++count_val; } 31 | Num(T v) : val(v) { ++count_val; } 32 | Num(const Num& v) : val(v.val) { ++count_val; } 33 | Num(Num&& v) : val(v.val) { ++count_val; } 34 | ~Num() { --count_val; } 35 | Num& 36 | operator=(const Num& v) 37 | { 38 | val = v.val; 39 | return *this; 40 | } 41 | operator T() const { return val; } 42 | bool 43 | operator<(const Num& v) const 44 | { 45 | ++count_comp; 46 | return val < v.val; 47 | } 48 | }; 49 | 50 | struct test_brick_partial_sort 51 | { 52 | template 53 | typename std::enable_if::value, 54 | void>::type 55 | operator()(Policy&& exec, InputIterator first, InputIterator last, InputIterator exp_first, InputIterator exp_last, 56 | Compare compare) 57 | { 58 | 59 | typedef typename std::iterator_traits::value_type T; 60 | 61 | // The rand()%(2*n+1) encourages generation of some duplicates. 62 | std::srand(42); 63 | const std::size_t n = last - first; 64 | for (std::size_t k = 0; k < n; ++k) 65 | { 66 | first[k] = T(rand() % (2 * n + 1)); 67 | } 68 | std::copy(first, last, exp_first); 69 | 70 | for (std::size_t p = 0; p < n; p = p <= 16 ? p + 1 : std::size_t(31.415 * p)) 71 | { 72 | auto m1 = first + p; 73 | auto m2 = exp_first + p; 74 | 75 | std::partial_sort(exp_first, m2, exp_last, compare); 76 | count_comp = 0; 77 | std::partial_sort(exec, first, m1, last, compare); 78 | EXPECT_EQ_N(exp_first, first, p, "wrong effect from partial_sort"); 79 | 80 | //checking upper bound number of comparisons; O(p*(last-first)log(middle-first)); where p - number of threads; 81 | if (m1 - first > 1) 82 | { 83 | #ifdef _DEBUG 84 | # if defined(_PSTL_PAR_BACKEND_TBB) 85 | auto p = tbb::this_task_arena::max_concurrency(); 86 | # else 87 | auto p = 1; 88 | # endif 89 | auto complex = std::ceil(n * std::log(float32_t(m1 - first))); 90 | if (count_comp > complex * p) 91 | { 92 | std::cout << "complexity exceeded" << std::endl; 93 | } 94 | #endif // _DEBUG 95 | } 96 | } 97 | } 98 | 99 | template 100 | typename std::enable_if::value, 101 | void>::type 102 | operator()(Policy&&, InputIterator, InputIterator, InputIterator, InputIterator, Compare) 103 | { 104 | } 105 | }; 106 | 107 | template 108 | void 109 | test_partial_sort(Compare compare) 110 | { 111 | 112 | const std::size_t n_max = 100000; 113 | Sequence in(n_max); 114 | Sequence exp(n_max); 115 | for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 116 | { 117 | invoke_on_all_policies(test_brick_partial_sort(), in.begin(), in.begin() + n, exp.begin(), exp.begin() + n, 118 | compare); 119 | } 120 | } 121 | 122 | template 123 | struct test_non_const 124 | { 125 | template 126 | void 127 | operator()(Policy&& exec, Iterator iter) 128 | { 129 | partial_sort(exec, iter, iter, iter, non_const(std::less())); 130 | } 131 | }; 132 | 133 | int 134 | main() 135 | { 136 | count_val = 0; 137 | 138 | test_partial_sort>([](Num x, Num y) { return x < y; }); 139 | 140 | EXPECT_TRUE(count_val == 0, "cleanup error"); 141 | 142 | test_partial_sort( 143 | [](int32_t x, int32_t y) { return x > y; }); // Reversed so accidental use of < will be detected. 144 | 145 | test_algo_basic_single(run_for_rnd>()); 146 | 147 | std::cout << done() << std::endl; 148 | return 0; 149 | } 150 | -------------------------------------------------------------------------------- /test/std/lit.local.cfg: -------------------------------------------------------------------------------- 1 | if 'parallel-algorithms' not in config.available_features: 2 | config.unsupported = True 3 | -------------------------------------------------------------------------------- /test/std/numerics/numeric.ops/reduce.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- reduce.pass.cpp ---------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_long_forms_for_one_policy 22 | { 23 | template 24 | void 25 | operator()(Policy&& exec, Iterator first, Iterator last, T init, BinaryOp binary, T expected) 26 | { 27 | T result_r = std::reduce(exec, first, last, init, binary); 28 | EXPECT_EQ(expected, result_r, "bad result from reduce(exec, first, last, init, binary_op)"); 29 | } 30 | }; 31 | 32 | template 33 | void 34 | test_long_form(T init, BinaryOp binary_op, F f) 35 | { 36 | // Try sequences of various lengths 37 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 38 | { 39 | T expected(init); 40 | Sequence in(n, [n, f](size_t k) { return f((int32_t(k ^ n) % 1000 - 500)); }); 41 | for (size_t k = 0; k < n; ++k) 42 | expected = binary_op(expected, in[k]); 43 | 44 | using namespace std; 45 | 46 | T result = transform_reduce_serial(in.cfbegin(), in.cfend(), init, binary_op, [](const T& t) { return t; }); 47 | EXPECT_EQ(expected, result, "bad result from reduce(first, last, init, binary_op_op)"); 48 | 49 | invoke_on_all_policies(test_long_forms_for_one_policy(), in.begin(), in.end(), init, binary_op, expected); 50 | invoke_on_all_policies(test_long_forms_for_one_policy(), in.cbegin(), in.cend(), init, binary_op, expected); 51 | } 52 | } 53 | 54 | struct test_two_short_forms 55 | { 56 | 57 | #if _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN //dummy specialization by policy type, in case of broken configuration 58 | template 59 | void 60 | operator()(pstl::execution::parallel_policy, Iterator first, Iterator last, Sum init, Sum expected) 61 | { 62 | } 63 | template 64 | void 65 | operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Sum init, Sum expected) 66 | { 67 | } 68 | #endif 69 | 70 | template 71 | void 72 | operator()(Policy&& exec, Iterator first, Iterator last, Sum init, Sum expected) 73 | { 74 | using namespace std; 75 | 76 | Sum r0 = init + reduce(exec, first, last); 77 | EXPECT_EQ(expected, r0, "bad result from reduce(exec, first, last)"); 78 | 79 | Sum r1 = reduce(exec, first, last, init); 80 | EXPECT_EQ(expected, r1, "bad result from reduce(exec, first, last, init)"); 81 | } 82 | }; 83 | 84 | // Test forms of reduce(...) that omit the binary_op or init operands. 85 | void 86 | test_short_forms() 87 | { 88 | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 89 | { 90 | Sum init(42, OddTag()); 91 | Sum expected(init); 92 | Sequence in(n, [n](size_t k) { return Sum((int32_t(k ^ n) % 1000 - 500), OddTag()); }); 93 | for (size_t k = 0; k < n; ++k) 94 | expected = expected + in[k]; 95 | invoke_on_all_policies(test_two_short_forms(), in.begin(), in.end(), init, expected); 96 | invoke_on_all_policies(test_two_short_forms(), in.cbegin(), in.cend(), init, expected); 97 | } 98 | } 99 | 100 | int 101 | main() 102 | { 103 | // Test for popular types 104 | test_long_form(42, std::plus(), [](int32_t x) { return x; }); 105 | test_long_form(42.0, std::plus(), [](float64_t x) { return x; }); 106 | 107 | // Test for strict types 108 | test_long_form(Number(42, OddTag()), Add(OddTag()), [](int32_t x) { return Number(x, OddTag()); }); 109 | 110 | // Short forms are just facade for long forms, so just test with a single type. 111 | test_short_forms(); 112 | std::cout << done() << std::endl; 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /test/std/numerics/numeric.ops/transform_reduce.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- transform_reduce.pass.cpp -----------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | // Equal for all types 22 | template 23 | static bool 24 | Equal(T x, T y) 25 | { 26 | return x == y; 27 | } 28 | 29 | // Functor for xor-operation for modeling binary operations in inner_product 30 | class XOR 31 | { 32 | public: 33 | template 34 | T 35 | operator()(const T& left, const T& right) const 36 | { 37 | return left ^ right; 38 | } 39 | }; 40 | 41 | // Model of User-defined class 42 | class MyClass 43 | { 44 | public: 45 | int32_t my_field; 46 | MyClass() { my_field = 0; } 47 | MyClass(int32_t in) { my_field = in; } 48 | MyClass(const MyClass& in) { my_field = in.my_field; } 49 | 50 | friend MyClass 51 | operator+(const MyClass& x, const MyClass& y) 52 | { 53 | return MyClass(x.my_field + y.my_field); 54 | } 55 | friend MyClass 56 | operator-(const MyClass& x) 57 | { 58 | return MyClass(-x.my_field); 59 | } 60 | friend MyClass operator*(const MyClass& x, const MyClass& y) { return MyClass(x.my_field * y.my_field); } 61 | bool 62 | operator==(const MyClass& in) 63 | { 64 | return my_field == in.my_field; 65 | } 66 | }; 67 | 68 | template 69 | void 70 | CheckResults(const T& expected, const T& in) 71 | { 72 | EXPECT_TRUE(Equal(expected, in), "wrong result of transform_reduce"); 73 | } 74 | 75 | // We need to check correctness only for "int" (for example) except cases 76 | // if we have "floating-point type"-specialization 77 | void 78 | CheckResults(const float32_t&, const float32_t&) 79 | { 80 | } 81 | 82 | // Test for different types and operations with different iterators 83 | struct test_transform_reduce 84 | { 85 | template 87 | void 88 | operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2, 89 | T init, BinaryOperation1 opB1, BinaryOperation2 opB2, UnaryOp opU) 90 | { 91 | 92 | auto expectedB = std::inner_product(first1, last1, first2, init, opB1, opB2); 93 | auto expectedU = transform_reduce_serial(first1, last1, init, opB1, opU); 94 | T resRA = std::transform_reduce(exec, first1, last1, first2, init, opB1, opB2); 95 | CheckResults(expectedB, resRA); 96 | resRA = std::transform_reduce(exec, first1, last1, init, opB1, opU); 97 | CheckResults(expectedU, resRA); 98 | } 99 | }; 100 | 101 | template 102 | void 103 | test_by_type(T init, BinaryOperation1 opB1, BinaryOperation2 opB2, UnaryOp opU, Initializer initObj) 104 | { 105 | 106 | std::size_t maxSize = 100000; 107 | Sequence in1(maxSize, initObj); 108 | Sequence in2(maxSize, initObj); 109 | 110 | for (std::size_t n = 0; n < maxSize; n = n < 16 ? n + 1 : size_t(3.1415 * n)) 111 | { 112 | invoke_on_all_policies(test_transform_reduce(), in1.begin(), in1.begin() + n, in2.begin(), in2.begin() + n, 113 | init, opB1, opB2, opU); 114 | invoke_on_all_policies(test_transform_reduce(), in1.cbegin(), in1.cbegin() + n, in2.cbegin(), in2.cbegin() + n, 115 | init, opB1, opB2, opU); 116 | } 117 | } 118 | 119 | int 120 | main() 121 | { 122 | test_by_type(42, std::plus(), std::multiplies(), std::negate(), 123 | [](std::size_t) -> int32_t { return int32_t(rand() % 1000); }); 124 | test_by_type(0, [](const int64_t& a, const int64_t& b) -> int64_t { return a | b; }, XOR(), 125 | [](const int64_t& x) -> int64_t { return x * 2; }, 126 | [](std::size_t) -> int64_t { return int64_t(rand() % 1000); }); 127 | test_by_type( 128 | 1.0f, std::multiplies(), [](const float32_t& a, const float32_t& b) -> float32_t { return a + b; }, 129 | [](const float32_t& x) -> float32_t { return x + 2; }, [](std::size_t) -> float32_t { return rand() % 1000; }); 130 | test_by_type(MyClass(), std::plus(), std::multiplies(), std::negate(), 131 | [](std::size_t) -> MyClass { return MyClass(rand() % 1000); }); 132 | 133 | std::cout << done() << std::endl; 134 | return 0; 135 | } 136 | -------------------------------------------------------------------------------- /test/std/utilities/memory/specialized.algorithms/uninitialized_construct.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- uninitialized_construct.pass.cpp ----------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | // Tests for uninitialized_default_construct, uninitialized_default_construct_n, 13 | // uninitialized_value_construct, uninitialized_value_construct_n 14 | 15 | #include "support/pstl_test_config.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "support/utils.h" 21 | 22 | using namespace TestUtils; 23 | 24 | // function of checking correctness for uninitialized.construct.value 25 | template 26 | bool 27 | IsCheckValueCorrectness(Iterator begin, Iterator end) 28 | { 29 | for (; begin != end; ++begin) 30 | { 31 | if (*begin != T()) 32 | { 33 | return false; 34 | } 35 | } 36 | return true; 37 | } 38 | 39 | struct test_uninit_construct 40 | { 41 | template 42 | void 43 | operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial=*/std::false_type) 44 | { 45 | typedef typename std::iterator_traits::value_type T; 46 | // it needs for cleaning memory that was filled by default constructors in unique_ptr p(new T[n]) 47 | // and for cleaning memory after last calling of uninitialized_value_construct_n. 48 | // It is important for non-trivial types 49 | std::destroy_n(exec, begin, n); 50 | 51 | // reset counter of constructors 52 | T::SetCount(0); 53 | // run algorithm 54 | std::uninitialized_default_construct(exec, begin, end); 55 | // compare counter of constructors to length of container 56 | EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct"); 57 | // destroy objects for testing new algorithms on same memory 58 | std::destroy(exec, begin, end); 59 | 60 | std::uninitialized_default_construct_n(exec, begin, n); 61 | EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct_n"); 62 | std::destroy_n(exec, begin, n); 63 | 64 | std::uninitialized_value_construct(exec, begin, end); 65 | EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct"); 66 | std::destroy(exec, begin, end); 67 | 68 | std::uninitialized_value_construct_n(exec, begin, n); 69 | EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct_n"); 70 | } 71 | 72 | template 73 | void 74 | operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial=*/std::true_type) 75 | { 76 | typedef typename std::iterator_traits::value_type T; 77 | 78 | std::uninitialized_default_construct(exec, begin, end); 79 | std::destroy(exec, begin, end); 80 | 81 | std::uninitialized_default_construct_n(exec, begin, n); 82 | std::destroy_n(exec, begin, n); 83 | 84 | std::uninitialized_value_construct(exec, begin, end); 85 | // check correctness for uninitialized.construct.value 86 | EXPECT_TRUE(IsCheckValueCorrectness(begin, end), "wrong uninitialized_value_construct"); 87 | std::destroy(exec, begin, end); 88 | 89 | std::uninitialized_value_construct_n(exec, begin, n); 90 | EXPECT_TRUE(IsCheckValueCorrectness(begin, end), "wrong uninitialized_value_construct_n"); 91 | std::destroy_n(exec, begin, n); 92 | } 93 | }; 94 | 95 | template 96 | void 97 | test_uninit_construct_by_type() 98 | { 99 | std::size_t N = 100000; 100 | for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 101 | { 102 | std::unique_ptr p(new T[n]); 103 | invoke_on_all_policies(test_uninit_construct(), p.get(), std::next(p.get(), n), n, std::is_trivial()); 104 | } 105 | } 106 | 107 | int 108 | main() 109 | { 110 | 111 | // for user-defined types 112 | #if !_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN 113 | test_uninit_construct_by_type>(); 114 | test_uninit_construct_by_type>>(); 115 | #endif 116 | 117 | // for trivial types 118 | test_uninit_construct_by_type(); 119 | test_uninit_construct_by_type(); 120 | 121 | std::cout << done() << std::endl; 122 | return 0; 123 | } 124 | -------------------------------------------------------------------------------- /test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- uninitialized_fill_destroy.pass.cpp -------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | // UNSUPPORTED: c++98, c++03, c++11, c++14 11 | 12 | #include "support/pstl_test_config.h" 13 | 14 | #include 15 | #include 16 | 17 | #include "support/utils.h" 18 | 19 | using namespace TestUtils; 20 | 21 | struct test_uninitialized_fill_destroy 22 | { 23 | template 24 | void 25 | operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::false_type) 26 | { 27 | using namespace std; 28 | { 29 | T::SetCount(0); 30 | uninitialized_fill(exec, first, last, in); 31 | size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); 32 | EXPECT_TRUE(n == count, "wrong work of uninitialized_fill"); 33 | destroy(exec, first, last); 34 | EXPECT_TRUE(T::Count() == 0, "wrong work of destroy"); 35 | } 36 | 37 | { 38 | auto res = uninitialized_fill_n(exec, first, n, in); 39 | EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n"); 40 | size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); 41 | EXPECT_TRUE(n == count, "wrong work of uninitialized_fill_n"); 42 | destroy_n(exec, first, n); 43 | EXPECT_TRUE(T::Count() == 0, "wrong work of destroy_n"); 44 | } 45 | } 46 | template 47 | void 48 | operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::true_type) 49 | { 50 | using namespace std; 51 | { 52 | destroy(exec, first, last); 53 | uninitialized_fill(exec, first, last, in); 54 | size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); 55 | EXPECT_EQ(n, count, "wrong work of uninitialized:_fill"); 56 | } 57 | { 58 | destroy_n(exec, first, n); 59 | auto res = uninitialized_fill_n(exec, first, n, in); 60 | size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); 61 | EXPECT_EQ(n, count, "wrong work of uninitialized_fill_n"); 62 | EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n"); 63 | } 64 | } 65 | }; 66 | 67 | template 68 | void 69 | test_uninitialized_fill_destroy_by_type() 70 | { 71 | std::size_t N = 100000; 72 | for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 73 | { 74 | std::unique_ptr p(new T[n]); 75 | invoke_on_all_policies(test_uninitialized_fill_destroy(), p.get(), std::next(p.get(), n), T(), n, 76 | std::is_trivial()); 77 | } 78 | } 79 | 80 | int 81 | main() 82 | { 83 | // for trivial types 84 | test_uninitialized_fill_destroy_by_type(); 85 | test_uninitialized_fill_destroy_by_type(); 86 | 87 | // for user-defined types 88 | test_uninitialized_fill_destroy_by_type>(); 89 | test_uninitialized_fill_destroy_by_type>(); 90 | std::cout << done() << std::endl; 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /test/support/pstl_test_config.h: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- pstl_test_config.h ------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _PSTL_TEST_config_H 11 | #define _PSTL_TEST_config_H 12 | 13 | #if defined(_MSC_VER) && defined(_DEBUG) 14 | # define _SCL_SECURE_NO_WARNINGS //to prevent the compilation warning. Microsoft STL implementation has specific checking of an iterator range in DEBUG mode for the containers from the standard library. 15 | #endif 16 | 17 | #ifndef __clang__ 18 | # define _PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN \ 19 | (__x86_64 && !_DEBUG && __INTEL_COMPILER && __INTEL_COMPILER <= 1700 && !__APPLE__) 20 | # define _PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN \ 21 | (!_DEBUG && __INTEL_COMPILER && \ 22 | (__INTEL_COMPILER < 1800 || (__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE < 1))) 23 | # define _PSTL_ICC_1800_TEST_MONOTONIC_RELEASE_64_BROKEN \ 24 | (__x86_64 && !_DEBUG && __INTEL_COMPILER && __INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE < 1) 25 | # define _PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN \ 26 | (__i386__ && !_DEBUG && __INTEL_COMPILER >= 1700 && __INTEL_COMPILER < 1800 && __APPLE__) 27 | # define _PSTL_ICC_18_VC141_TEST_SIMD_LAMBDA_RELEASE_BROKEN \ 28 | (!_DEBUG && __INTEL_COMPILER >= 1800 && __INTEL_COMPILER < 1900 && _MSC_VER == 1910) 29 | # define _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN \ 30 | (_M_IX86 && _DEBUG && __INTEL_COMPILER >= 1700 && __INTEL_COMPILER < 1800 && _MSC_VER >= 1900) 31 | # define _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN \ 32 | (_M_IX86 && _DEBUG && __INTEL_COMPILER >= 1600 && __INTEL_COMPILER < 1700 && _MSC_VER == 1900) 33 | # define _PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN \ 34 | (((_M_X64 && _MSC_VER == 1900) || __x86_64) && !_DEBUG && __INTEL_COMPILER < 1700) 35 | # define _PSTL_ICC_16_17_TEST_64_TIMEOUT (__x86_64 && __INTEL_COMPILER && __INTEL_COMPILER < 1800 && !__APPLE__) 36 | # define _PSTL_ICC_18_TEST_EARLY_EXIT_MONOTONIC_RELEASE_BROKEN \ 37 | (!_DEBUG && __INTEL_COMPILER && __INTEL_COMPILER == 1800) 38 | # define _PSTL_CLANG_TEST_BIG_OBJ_DEBUG_32_BROKEN \ 39 | (__i386__ && PSTL_USE_DEBUG && __clang__ && _PSTL_CLANG_VERSION <= 90000) 40 | # define _PSTL_ICC_16_17_18_TEST_UNIQUE_MASK_RELEASE_BROKEN \ 41 | (!_DEBUG && __INTEL_COMPILER && \ 42 | (__INTEL_COMPILER < 1800 || (__INTEL_COMPILER == 1800 && __INTEL_COMPILER_UPDATE < 3))) 43 | # define _PSTL_ICC_18_TEST_EARLY_EXIT_AVX_RELEASE_BROKEN \ 44 | (!_DEBUG && __INTEL_COMPILER == 1800 && __AVX__ && !__AVX2__ && !__AVX512__) 45 | # define _PSTL_ICC_19_TEST_IS_PARTITIONED_RELEASE_BROKEN \ 46 | (!PSTL_USE_DEBUG && (__linux__ || __APPLE__) && __INTEL_COMPILER == 1900) 47 | # define _PSTL_ICL_19_VC14_VC141_TEST_SCAN_RELEASE_BROKEN \ 48 | (__INTEL_COMPILER == 1900 && _MSC_VER >= 1900 && _MSC_VER <= 1910) 49 | # define _PSTL_ICC_19_TEST_SIMD_UDS_WINDOWS_RELEASE_BROKEN (__INTEL_COMPILER == 1900 && _MSC_VER && !_DEBUG) 50 | #endif // !__clang__ 51 | 52 | #endif /* _PSTL_TEST_config_H */ 53 | -------------------------------------------------------------------------------- /test/support/stdlib/algorithm: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- algorithm ---------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _TEST_SUPPORT_STDLIB_ALGORITHM 11 | #define _TEST_SUPPORT_STDLIB_ALGORITHM 12 | 13 | #include_next 14 | 15 | #include 16 | 17 | #if _PSTL_EXECUTION_POLICIES_DEFINED 18 | // If has already been included, pull in implementations 19 | # include 20 | #else 21 | // Otherwise just pull in forward declarations 22 | # include 23 | # define _PSTL_ALGORITHM_FORWARD_DECLARED 1 24 | #endif 25 | 26 | #endif /* _TEST_SUPPORT_STDLIB_ALGORITHM */ 27 | -------------------------------------------------------------------------------- /test/support/stdlib/execution: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- execution ---------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _TEST_SUPPORT_STDLIB_EXECUTION 11 | #define _TEST_SUPPORT_STDLIB_EXECUTION 12 | 13 | // #include_next // None of the standard libraries PSTL is built on top have the header yet. 14 | 15 | #include 16 | #include 17 | 18 | #define _PSTL_EXECUTION_POLICIES_DEFINED 1 19 | 20 | #if _PSTL_ALGORITHM_FORWARD_DECLARED 21 | # include 22 | #endif 23 | 24 | #if _PSTL_MEMORY_FORWARD_DECLARED 25 | # include 26 | #endif 27 | 28 | #if _PSTL_NUMERIC_FORWARD_DECLARED 29 | # include 30 | #endif 31 | 32 | #if _PSTL_CPP17_EXECUTION_POLICIES_PRESENT 33 | _PSTL_PRAGMA_MESSAGE_POLICIES("The execution policies are defined in the namespace __pstl::execution") 34 | #else 35 | # include 36 | _PSTL_PRAGMA_MESSAGE_POLICIES( 37 | "The execution policies are injected into the standard namespace std::execution") 38 | #endif 39 | 40 | //TODO: __pstl::execution namespace is injected into the pstl::execution namespace when the implementation is not a part of 41 | // standard C++ library 42 | namespace pstl 43 | { 44 | namespace execution 45 | { 46 | using namespace __pstl::execution; 47 | } 48 | } // namespace pstl 49 | 50 | #endif /* _TEST_SUPPORT_STDLIB_EXECUTION */ 51 | -------------------------------------------------------------------------------- /test/support/stdlib/memory: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- memory ------------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _TEST_SUPPORT_STDLIB_MEMORY 11 | #define _TEST_SUPPORT_STDLIB_MEMORY 12 | 13 | #include_next 14 | 15 | #include 16 | 17 | #if _PSTL_EXECUTION_POLICIES_DEFINED 18 | // If has already been included, pull in implementations 19 | # include 20 | #else 21 | // Otherwise just pull in forward declarations 22 | # include 23 | # define _PSTL_MEMORY_FORWARD_DECLARED 1 24 | #endif 25 | 26 | #endif /* _TEST_SUPPORT_STDLIB_MEMORY */ 27 | -------------------------------------------------------------------------------- /test/support/stdlib/numeric: -------------------------------------------------------------------------------- 1 | // -*- C++ -*- 2 | //===-- numeric -----------------------------------------------------------===// 3 | // 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 | // See https://llvm.org/LICENSE.txt for license information. 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 | // 8 | //===----------------------------------------------------------------------===// 9 | 10 | #ifndef _TEST_SUPPORT_STDLIB_NUMERIC 11 | #define _TEST_SUPPORT_STDLIB_NUMERIC 12 | 13 | #include_next 14 | 15 | #include 16 | 17 | #if _PSTL_EXECUTION_POLICIES_DEFINED 18 | // If has already been included, pull in implementations 19 | # include 20 | #else 21 | // Otherwise just pull in forward declarations 22 | # include 23 | # define _PSTL_NUMERIC_FORWARD_DECLARED 1 24 | #endif 25 | 26 | #endif /* _TEST_SUPPORT_STDLIB_NUMERIC */ 27 | --------------------------------------------------------------------------------