glue_memory_impl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // -*- C++ -*-
  2. //===-- glue_memory_impl.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. #ifndef _PSTL_GLUE_MEMORY_IMPL_H
  10. #define _PSTL_GLUE_MEMORY_IMPL_H
  11. #include "utils.h"
  12. #include "algorithm_fwd.h"
  13. namespace std
  14. {
  15. // [uninitialized.copy]
  16. template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator>
  17. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  18. uninitialized_copy(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result)
  19. {
  20. typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
  21. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
  22. typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
  23. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
  24. const auto __is_parallel =
  25. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  26. const auto __is_vector =
  27. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  28. return __pstl::__internal::__invoke_if_else(
  29. std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
  30. [&]() {
  31. return __pstl::__internal::__pattern_walk2_brick(
  32. std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
  33. [__is_vector](_InputIterator __begin, _InputIterator __end, _ForwardIterator __res) {
  34. return __pstl::__internal::__brick_copy(__begin, __end, __res, __is_vector);
  35. },
  36. __is_parallel);
  37. },
  38. [&]() {
  39. return __pstl::__internal::__pattern_walk2(std::forward<_ExecutionPolicy>(__exec), __first, __last,
  40. __result,
  41. [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
  42. ::new (std::addressof(__val2)) _ValueType2(__val1);
  43. },
  44. __is_vector, __is_parallel);
  45. });
  46. }
  47. template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator>
  48. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  49. uninitialized_copy_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result)
  50. {
  51. typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
  52. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
  53. typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
  54. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
  55. const auto __is_parallel =
  56. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  57. const auto __is_vector =
  58. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  59. return __pstl::__internal::__invoke_if_else(
  60. std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
  61. [&]() {
  62. return __pstl::__internal::__pattern_walk2_brick_n(
  63. std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
  64. [__is_vector](_InputIterator __begin, _Size __sz, _ForwardIterator __res) {
  65. return __pstl::__internal::__brick_copy_n(__begin, __sz, __res, __is_vector);
  66. },
  67. __is_parallel);
  68. },
  69. [&]() {
  70. return __pstl::__internal::__pattern_walk2_n(std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
  71. [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
  72. ::new (std::addressof(__val2)) _ValueType2(__val1);
  73. },
  74. __is_vector, __is_parallel);
  75. });
  76. }
  77. // [uninitialized.move]
  78. template <class _ExecutionPolicy, class _InputIterator, class _ForwardIterator>
  79. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  80. uninitialized_move(_ExecutionPolicy&& __exec, _InputIterator __first, _InputIterator __last, _ForwardIterator __result)
  81. {
  82. typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
  83. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
  84. typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
  85. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
  86. const auto __is_parallel =
  87. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  88. const auto __is_vector =
  89. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  90. return __pstl::__internal::__invoke_if_else(
  91. std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
  92. [&]() {
  93. return __pstl::__internal::__pattern_walk2_brick(
  94. std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
  95. [__is_vector](_InputIterator __begin, _InputIterator __end, _ForwardIterator __res) {
  96. return __pstl::__internal::__brick_copy(__begin, __end, __res, __is_vector);
  97. },
  98. __is_parallel);
  99. },
  100. [&]() {
  101. return __pstl::__internal::__pattern_walk2(
  102. std::forward<_ExecutionPolicy>(__exec), __first, __last, __result,
  103. [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
  104. ::new (std::addressof(__val2)) _ValueType2(std::move(__val1));
  105. },
  106. __is_vector, __is_parallel);
  107. });
  108. }
  109. template <class _ExecutionPolicy, class _InputIterator, class _Size, class _ForwardIterator>
  110. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  111. uninitialized_move_n(_ExecutionPolicy&& __exec, _InputIterator __first, _Size __n, _ForwardIterator __result)
  112. {
  113. typedef typename iterator_traits<_InputIterator>::value_type _ValueType1;
  114. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType2;
  115. typedef typename iterator_traits<_InputIterator>::reference _ReferenceType1;
  116. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType2;
  117. const auto __is_parallel =
  118. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  119. const auto __is_vector =
  120. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _InputIterator, _ForwardIterator>(__exec);
  121. return __pstl::__internal::__invoke_if_else(
  122. std::integral_constant < bool, std::is_trivial<_ValueType1>::value&& std::is_trivial<_ValueType2>::value > (),
  123. [&]() {
  124. return __pstl::__internal::__pattern_walk2_brick_n(
  125. std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
  126. [__is_vector](_InputIterator __begin, _Size __sz, _ForwardIterator __res) {
  127. return __pstl::__internal::__brick_copy_n(__begin, __sz, __res, __is_vector);
  128. },
  129. __is_parallel);
  130. },
  131. [&]() {
  132. return __pstl::__internal::__pattern_walk2_n(std::forward<_ExecutionPolicy>(__exec), __first, __n, __result,
  133. [](_ReferenceType1 __val1, _ReferenceType2 __val2) {
  134. ::new (std::addressof(__val2))
  135. _ValueType2(std::move(__val1));
  136. },
  137. __is_vector, __is_parallel);
  138. });
  139. }
  140. // [uninitialized.fill]
  141. template <class _ExecutionPolicy, class _ForwardIterator, class _Tp>
  142. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
  143. uninitialized_fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
  144. {
  145. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  146. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  147. const auto __is_parallel =
  148. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  149. const auto __is_vector =
  150. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  151. __pstl::__internal::__invoke_if_else(
  152. std::is_arithmetic<_ValueType>(),
  153. [&]() {
  154. __pstl::__internal::__pattern_walk_brick(
  155. std::forward<_ExecutionPolicy>(__exec), __first, __last,
  156. [&__value, &__is_vector](_ForwardIterator __begin, _ForwardIterator __end) {
  157. __pstl::__internal::__brick_fill(__begin, __end, _ValueType(__value), __is_vector);
  158. },
  159. __is_parallel);
  160. },
  161. [&]() {
  162. __pstl::__internal::__pattern_walk1(
  163. std::forward<_ExecutionPolicy>(__exec), __first, __last,
  164. [&__value](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(__value); }, __is_vector,
  165. __is_parallel);
  166. });
  167. }
  168. template <class _ExecutionPolicy, class _ForwardIterator, class _Size, class _Tp>
  169. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  170. uninitialized_fill_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n, const _Tp& __value)
  171. {
  172. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  173. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  174. const auto __is_parallel =
  175. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  176. const auto __is_vector =
  177. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  178. return __pstl::__internal::__invoke_if_else(
  179. std::is_arithmetic<_ValueType>(),
  180. [&]() {
  181. return __pstl::__internal::__pattern_walk_brick_n(
  182. std::forward<_ExecutionPolicy>(__exec), __first, __n,
  183. [&__value, &__is_vector](_ForwardIterator __begin, _Size __count) {
  184. return __pstl::__internal::__brick_fill_n(__begin, __count, _ValueType(__value), __is_vector);
  185. },
  186. __is_parallel);
  187. },
  188. [&]() {
  189. return __pstl::__internal::__pattern_walk1_n(
  190. std::forward<_ExecutionPolicy>(__exec), __first, __n,
  191. [&__value](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(__value); }, __is_vector,
  192. __is_parallel);
  193. });
  194. }
  195. // [specialized.destroy]
  196. template <class _ExecutionPolicy, class _ForwardIterator>
  197. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
  198. destroy(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
  199. {
  200. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  201. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  202. const auto __is_parallel =
  203. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  204. const auto __is_vector =
  205. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  206. __pstl::__internal::__invoke_if_not(std::is_trivially_destructible<_ValueType>(), [&]() {
  207. __pstl::__internal::__pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last,
  208. [](_ReferenceType __val) { __val.~_ValueType(); }, __is_vector,
  209. __is_parallel);
  210. });
  211. }
  212. template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
  213. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  214. destroy_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n)
  215. {
  216. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  217. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  218. const auto __is_parallel =
  219. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  220. const auto __is_vector =
  221. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  222. return __pstl::__internal::__invoke_if_else(
  223. std::is_trivially_destructible<_ValueType>(), [&]() { return std::next(__first, __n); },
  224. [&]() {
  225. return __pstl::__internal::__pattern_walk1_n(std::forward<_ExecutionPolicy>(__exec), __first, __n,
  226. [](_ReferenceType __val) { __val.~_ValueType(); }, __is_vector,
  227. __is_parallel);
  228. });
  229. }
  230. // [uninitialized.construct.default]
  231. template <class _ExecutionPolicy, class _ForwardIterator>
  232. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
  233. uninitialized_default_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
  234. {
  235. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  236. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  237. const auto __is_parallel =
  238. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  239. const auto __is_vector =
  240. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  241. __pstl::__internal::__invoke_if_not(std::is_trivial<_ValueType>(), [&]() {
  242. __pstl::__internal::__pattern_walk1(std::forward<_ExecutionPolicy>(__exec), __first, __last,
  243. [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType; },
  244. __is_vector, __is_parallel);
  245. });
  246. }
  247. template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
  248. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  249. uninitialized_default_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n)
  250. {
  251. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  252. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  253. const auto __is_parallel =
  254. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  255. const auto __is_vector =
  256. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  257. return __pstl::__internal::__invoke_if_else(
  258. std::is_trivial<_ValueType>(), [&]() { return std::next(__first, __n); },
  259. [&]() {
  260. return __pstl::__internal::__pattern_walk1_n(
  261. std::forward<_ExecutionPolicy>(__exec), __first, __n,
  262. [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType; }, __is_vector, __is_parallel);
  263. });
  264. }
  265. // [uninitialized.construct.value]
  266. template <class _ExecutionPolicy, class _ForwardIterator>
  267. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void>
  268. uninitialized_value_construct(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last)
  269. {
  270. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  271. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  272. const auto __is_parallel =
  273. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  274. const auto __is_vector =
  275. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  276. __pstl::__internal::__invoke_if_else(
  277. std::is_trivial<_ValueType>(),
  278. [&]() {
  279. __pstl::__internal::__pattern_walk_brick(std::forward<_ExecutionPolicy>(__exec), __first, __last,
  280. [__is_vector](_ForwardIterator __begin, _ForwardIterator __end) {
  281. __pstl::__internal::__brick_fill(__begin, __end, _ValueType(),
  282. __is_vector);
  283. },
  284. __is_parallel);
  285. },
  286. [&]() {
  287. __pstl::__internal::__pattern_walk1(
  288. std::forward<_ExecutionPolicy>(__exec), __first, __last,
  289. [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(); }, __is_vector, __is_parallel);
  290. });
  291. }
  292. template <class _ExecutionPolicy, class _ForwardIterator, class _Size>
  293. __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator>
  294. uninitialized_value_construct_n(_ExecutionPolicy&& __exec, _ForwardIterator __first, _Size __n)
  295. {
  296. typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
  297. typedef typename iterator_traits<_ForwardIterator>::reference _ReferenceType;
  298. const auto __is_parallel =
  299. __pstl::__internal::__is_parallelization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  300. const auto __is_vector =
  301. __pstl::__internal::__is_vectorization_preferred<_ExecutionPolicy, _ForwardIterator>(__exec);
  302. return __pstl::__internal::__invoke_if_else(
  303. std::is_trivial<_ValueType>(),
  304. [&]() {
  305. return __pstl::__internal::__pattern_walk_brick_n(std::forward<_ExecutionPolicy>(__exec), __first, __n,
  306. [__is_vector](_ForwardIterator __begin, _Size __count) {
  307. return __pstl::__internal::__brick_fill_n(
  308. __begin, __count, _ValueType(), __is_vector);
  309. },
  310. __is_parallel);
  311. },
  312. [&]() {
  313. return __pstl::__internal::__pattern_walk1_n(
  314. std::forward<_ExecutionPolicy>(__exec), __first, __n,
  315. [](_ReferenceType __val) { ::new (std::addressof(__val)) _ValueType(); }, __is_vector, __is_parallel);
  316. });
  317. }
  318. } // namespace std
  319. #endif /* _PSTL_GLUE_MEMORY_IMPL_H */