brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.4 KiB · d03421b Raw
252 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___ALGORITHM_FIND_H11#define _LIBCPP___ALGORITHM_FIND_H12 13#include <__algorithm/find_segment_if.h>14#include <__algorithm/min.h>15#include <__algorithm/simd_utils.h>16#include <__algorithm/unwrap_iter.h>17#include <__bit/countr.h>18#include <__bit/invert_if.h>19#include <__config>20#include <__cstddef/size_t.h>21#include <__functional/identity.h>22#include <__fwd/bit_reference.h>23#include <__iterator/segmented_iterator.h>24#include <__string/constexpr_c_functions.h>25#include <__type_traits/enable_if.h>26#include <__type_traits/invoke.h>27#include <__type_traits/is_constant_evaluated.h>28#include <__type_traits/is_equality_comparable.h>29#include <__type_traits/is_integral.h>30#include <__type_traits/is_signed.h>31#include <__utility/move.h>32#include <limits>33 34#if _LIBCPP_HAS_WIDE_CHARACTERS35#  include <cwchar>36#endif37 38#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)39#  pragma GCC system_header40#endif41 42_LIBCPP_PUSH_MACROS43#include <__undef_macros>44 45_LIBCPP_BEGIN_NAMESPACE_STD46 47// generic implementation48template <class _Iter, class _Sent, class _Tp, class _Proj>49_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter50__find_loop(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {51  for (; __first != __last; ++__first)52    if (std::__invoke(__proj, *__first) == __value)53      break;54  return __first;55}56 57template <class _Iter, class _Sent, class _Tp, class _Proj>58_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter59__find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {60  return std::__find_loop(std::move(__first), std::move(__last), __value, __proj);61}62 63#if _LIBCPP_VECTORIZE_ALGORITHMS64template <class _Tp, class _Up>65[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI66_LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find_vectorized(_Tp* __first, _Tp* __last, _Up __value) {67  if (!__libcpp_is_constant_evaluated()) {68    constexpr size_t __unroll_count = 4;69    constexpr size_t __vec_size     = __native_vector_size<_Tp>;70    using __vec                     = __simd_vector<_Tp, __vec_size>;71 72    auto __orig_first = __first;73 74    auto __values = static_cast<__simd_vector<_Tp, __vec_size>>(__value); // broadcast the value75    while (static_cast<size_t>(__last - __first) >= __unroll_count * __vec_size) [[__unlikely__]] {76      __vec __lhs[__unroll_count];77 78      for (size_t __i = 0; __i != __unroll_count; ++__i)79        __lhs[__i] = std::__load_vector<__vec>(__first + __i * __vec_size);80 81      for (size_t __i = 0; __i != __unroll_count; ++__i) {82        if (auto __cmp_res = __lhs[__i] == __values; std::__any_of(__cmp_res)) {83          auto __offset = __i * __vec_size + std::__find_first_set(__cmp_res);84          return __first + __offset;85        }86      }87 88      __first += __unroll_count * __vec_size;89    }90 91    // check the remaining 0-3 vectors92    while (static_cast<size_t>(__last - __first) >= __vec_size) {93      if (auto __cmp_res = std::__load_vector<__vec>(__first) == __values; std::__any_of(__cmp_res)) {94        return __first + std::__find_first_set(__cmp_res);95      }96      __first += __vec_size;97    }98 99    if (__last - __first == 0)100      return __first;101 102    // Check if we can load elements in front of the current pointer. If that's the case load a vector at103    // (last - vector_size) to check the remaining elements104    if (static_cast<size_t>(__first - __orig_first) >= __vec_size) {105      __first = __last - __vec_size;106      return __first + std::__find_first_set(std::__load_vector<__vec>(__first) == __values);107    }108  }109 110  __identity __proj;111  return std::__find_loop(__first, __last, __value, __proj);112}113#endif114 115#ifndef _LIBCPP_CXX03_LANG116// trivially equality comparable implementations117template <118    class _Tp,119    class _Up,120    class _Proj,121    __enable_if_t<__is_identity<_Proj>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value, int> = 0>122_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj&) {123  if constexpr (sizeof(_Tp) == 1) {124    if (auto __ret = std::__constexpr_memchr(__first, __value, __last - __first))125      return __ret;126    return __last;127  }128#  if _LIBCPP_HAS_WIDE_CHARACTERS129  else if constexpr (sizeof(_Tp) == sizeof(wchar_t) && _LIBCPP_ALIGNOF(_Tp) >= _LIBCPP_ALIGNOF(wchar_t)) {130    if (auto __ret = std::__constexpr_wmemchr(__first, __value, __last - __first))131      return __ret;132    return __last;133  }134#  endif135#  if _LIBCPP_VECTORIZE_ALGORITHMS136  else if constexpr (is_integral<_Tp>::value) {137    return std::__find_vectorized(__first, __last, __value);138  }139#  endif140  else {141    __identity __proj;142    return std::__find_loop(__first, __last, __value, __proj);143  }144}145#endif146 147// TODO: This should also be possible to get right with different signedness148// cast integral types to allow vectorization149template <class _Tp,150          class _Up,151          class _Proj,152          __enable_if_t<__is_identity<_Proj>::value && !__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value &&153                            is_integral<_Tp>::value && is_integral<_Up>::value &&154                            is_signed<_Tp>::value == is_signed<_Up>::value,155                        int> = 0>156_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*157__find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {158  if (__value < numeric_limits<_Tp>::min() || __value > numeric_limits<_Tp>::max())159    return __last;160  return std::__find(__first, __last, _Tp(__value), __proj);161}162 163// __bit_iterator implementation164template <bool _ToFind, class _Cp, bool _IsConst>165_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>166__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {167  using _It            = __bit_iterator<_Cp, _IsConst>;168  using __storage_type = typename _It::__storage_type;169 170  const int __bits_per_word = _It::__bits_per_word;171  // do first partial word172  if (__first.__ctz_ != 0) {173    __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);174    __storage_type __dn    = std::min(__clz_f, __n);175    __storage_type __m     = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);176    __storage_type __b     = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;177    if (__b)178      return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));179    if (__n == __dn)180      return __first + __n;181    __n -= __dn;182    ++__first.__seg_;183  }184  // do middle whole words185  for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {186    __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);187    if (__b)188      return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));189  }190  // do last partial word191  if (__n > 0) {192    __storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);193    __storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;194    if (__b)195      return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));196  }197  return _It(__first.__seg_, static_cast<unsigned>(__n));198}199 200template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_identity<_Proj>::value, int> = 0>201inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>202__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {203  if (static_cast<bool>(__value))204    return std::__find_bool<true>(205        __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));206  return std::__find_bool<false>(207      __first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));208}209 210// segmented iterator implementation211 212template <class>213struct __find_segment;214 215template <class _SegmentedIterator,216          class _Tp,217          class _Proj,218          __enable_if_t<__is_segmented_iterator_v<_SegmentedIterator>, int> = 0>219_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _SegmentedIterator220__find(_SegmentedIterator __first, _SegmentedIterator __last, const _Tp& __value, _Proj& __proj) {221  return std::__find_segment_if(std::move(__first), std::move(__last), __find_segment<_Tp>(__value), __proj);222}223 224template <class _Tp>225struct __find_segment {226  const _Tp& __value_;227 228  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __find_segment(const _Tp& __value) : __value_(__value) {}229 230  template <class _InputIterator, class _Proj>231  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _InputIterator232  operator()(_InputIterator __first, _InputIterator __last, _Proj& __proj) const {233    return std::__rewrap_iter(234        __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value_, __proj));235  }236};237 238// public API239template <class _InputIterator, class _Tp>240[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator241find(_InputIterator __first, _InputIterator __last, const _Tp& __value) {242  __identity __proj;243  return std::__rewrap_iter(244      __first, std::__find(std::__unwrap_iter(__first), std::__unwrap_iter(__last), __value, __proj));245}246 247_LIBCPP_END_NAMESPACE_STD248 249_LIBCPP_POP_MACROS250 251#endif // _LIBCPP___ALGORITHM_FIND_H252