1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_FILL_N_H 10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_FILL_N_H 11fe6060f1SDimitry Andric 12*0fca6ea1SDimitry Andric #include <__algorithm/min.h> 13fe6060f1SDimitry Andric #include <__config> 14*0fca6ea1SDimitry Andric #include <__fwd/bit_reference.h> 15fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h> 16*0fca6ea1SDimitry Andric #include <__memory/pointer_traits.h> 17bdd1243dSDimitry Andric #include <__utility/convert_to_integral.h> 18fe6060f1SDimitry Andric 19fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20fe6060f1SDimitry Andric # pragma GCC system_header 21fe6060f1SDimitry Andric #endif 22fe6060f1SDimitry Andric 23*0fca6ea1SDimitry Andric _LIBCPP_PUSH_MACROS 24*0fca6ea1SDimitry Andric #include <__undef_macros> 25*0fca6ea1SDimitry Andric 26fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 27fe6060f1SDimitry Andric 2861cfbce3SDimitry Andric // fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset. 2961cfbce3SDimitry Andric 30fe6060f1SDimitry Andric template <class _OutputIterator, class _Size, class _Tp> 31cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator 32*0fca6ea1SDimitry Andric __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value); 33*0fca6ea1SDimitry Andric 34*0fca6ea1SDimitry Andric template <bool _FillVal, class _Cp> 35*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void 36*0fca6ea1SDimitry Andric __fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { 37*0fca6ea1SDimitry Andric using _It = __bit_iterator<_Cp, false>; 38*0fca6ea1SDimitry Andric using __storage_type = typename _It::__storage_type; 39*0fca6ea1SDimitry Andric 40*0fca6ea1SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 41*0fca6ea1SDimitry Andric // do first partial word 42*0fca6ea1SDimitry Andric if (__first.__ctz_ != 0) { 43*0fca6ea1SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 44*0fca6ea1SDimitry Andric __storage_type __dn = std::min(__clz_f, __n); 45*0fca6ea1SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 46*0fca6ea1SDimitry Andric if (_FillVal) 47*0fca6ea1SDimitry Andric *__first.__seg_ |= __m; 48*0fca6ea1SDimitry Andric else 49*0fca6ea1SDimitry Andric *__first.__seg_ &= ~__m; 50*0fca6ea1SDimitry Andric __n -= __dn; 51*0fca6ea1SDimitry Andric ++__first.__seg_; 52*0fca6ea1SDimitry Andric } 53*0fca6ea1SDimitry Andric // do middle whole words 54*0fca6ea1SDimitry Andric __storage_type __nw = __n / __bits_per_word; 55*0fca6ea1SDimitry Andric std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0); 56*0fca6ea1SDimitry Andric __n -= __nw * __bits_per_word; 57*0fca6ea1SDimitry Andric // do last partial word 58*0fca6ea1SDimitry Andric if (__n > 0) { 59*0fca6ea1SDimitry Andric __first.__seg_ += __nw; 60*0fca6ea1SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 61*0fca6ea1SDimitry Andric if (_FillVal) 62*0fca6ea1SDimitry Andric *__first.__seg_ |= __m; 63*0fca6ea1SDimitry Andric else 64*0fca6ea1SDimitry Andric *__first.__seg_ &= ~__m; 65*0fca6ea1SDimitry Andric } 66*0fca6ea1SDimitry Andric } 67*0fca6ea1SDimitry Andric 68*0fca6ea1SDimitry Andric template <class _Cp, class _Size> 69*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> 70*0fca6ea1SDimitry Andric __fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) { 71*0fca6ea1SDimitry Andric if (__n > 0) { 72*0fca6ea1SDimitry Andric if (__value) 73*0fca6ea1SDimitry Andric std::__fill_n_bool<true>(__first, __n); 74*0fca6ea1SDimitry Andric else 75*0fca6ea1SDimitry Andric std::__fill_n_bool<false>(__first, __n); 76*0fca6ea1SDimitry Andric } 77*0fca6ea1SDimitry Andric return __first + __n; 78*0fca6ea1SDimitry Andric } 79*0fca6ea1SDimitry Andric 80*0fca6ea1SDimitry Andric template <class _OutputIterator, class _Size, class _Tp> 81*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator 82cb14a3feSDimitry Andric __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) { 83fe6060f1SDimitry Andric for (; __n > 0; ++__first, (void)--__n) 84753f127fSDimitry Andric *__first = __value; 85fe6060f1SDimitry Andric return __first; 86fe6060f1SDimitry Andric } 87fe6060f1SDimitry Andric 88fe6060f1SDimitry Andric template <class _OutputIterator, class _Size, class _Tp> 89cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator 90cb14a3feSDimitry Andric fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) { 915f757f3fSDimitry Andric return std::__fill_n(__first, std::__convert_to_integral(__n), __value); 92fe6060f1SDimitry Andric } 93fe6060f1SDimitry Andric 94fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 95fe6060f1SDimitry Andric 96*0fca6ea1SDimitry Andric _LIBCPP_POP_MACROS 97*0fca6ea1SDimitry Andric 98fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_FILL_N_H 99