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 _LIBCPP___ALGORITHM_FILL_N_H 10 #define _LIBCPP___ALGORITHM_FILL_N_H 11 12 #include <__algorithm/min.h> 13 #include <__config> 14 #include <__fwd/bit_reference.h> 15 #include <__memory/pointer_traits.h> 16 #include <__utility/convert_to_integral.h> 17 18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19 # pragma GCC system_header 20 #endif 21 22 _LIBCPP_PUSH_MACROS 23 #include <__undef_macros> 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 // fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to a call to std::memset. 28 29 template <class _OutputIterator, class _Size, class _Tp> 30 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator 31 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value); 32 33 template <bool _FillVal, class _Cp> 34 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void 35 __fill_n_bool(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) { 36 using _It = __bit_iterator<_Cp, false>; 37 using __storage_type = typename _It::__storage_type; 38 39 const int __bits_per_word = _It::__bits_per_word; 40 // do first partial word 41 if (__first.__ctz_ != 0) { 42 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 43 __storage_type __dn = std::min(__clz_f, __n); 44 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 45 if (_FillVal) 46 *__first.__seg_ |= __m; 47 else 48 *__first.__seg_ &= ~__m; 49 __n -= __dn; 50 ++__first.__seg_; 51 } 52 // do middle whole words 53 __storage_type __nw = __n / __bits_per_word; 54 std::__fill_n(std::__to_address(__first.__seg_), __nw, _FillVal ? static_cast<__storage_type>(-1) : 0); 55 __n -= __nw * __bits_per_word; 56 // do last partial word 57 if (__n > 0) { 58 __first.__seg_ += __nw; 59 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 60 if (_FillVal) 61 *__first.__seg_ |= __m; 62 else 63 *__first.__seg_ &= ~__m; 64 } 65 } 66 67 template <class _Cp, class _Size> 68 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> 69 __fill_n(__bit_iterator<_Cp, false> __first, _Size __n, const bool& __value) { 70 if (__n > 0) { 71 if (__value) 72 std::__fill_n_bool<true>(__first, __n); 73 else 74 std::__fill_n_bool<false>(__first, __n); 75 } 76 return __first + __n; 77 } 78 79 template <class _OutputIterator, class _Size, class _Tp> 80 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator 81 __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) { 82 for (; __n > 0; ++__first, (void)--__n) 83 *__first = __value; 84 return __first; 85 } 86 87 template <class _OutputIterator, class _Size, class _Tp> 88 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator 89 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value) { 90 return std::__fill_n(__first, std::__convert_to_integral(__n), __value); 91 } 92 93 _LIBCPP_END_NAMESPACE_STD 94 95 _LIBCPP_POP_MACROS 96 97 #endif // _LIBCPP___ALGORITHM_FILL_N_H 98