1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*- 2*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===// 3*58b9f456SAndroid Build Coastguard Worker// 4*58b9f456SAndroid Build Coastguard Worker// The LLVM Compiler Infrastructure 5*58b9f456SAndroid Build Coastguard Worker// 6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open 7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details. 8*58b9f456SAndroid Build Coastguard Worker// 9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===// 10*58b9f456SAndroid Build Coastguard Worker 11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP___BIT_REFERENCE 12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP___BIT_REFERENCE 13*58b9f456SAndroid Build Coastguard Worker 14*58b9f456SAndroid Build Coastguard Worker#include <__config> 15*58b9f456SAndroid Build Coastguard Worker#include <bit> 16*58b9f456SAndroid Build Coastguard Worker#include <algorithm> 17*58b9f456SAndroid Build Coastguard Worker 18*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header 20*58b9f456SAndroid Build Coastguard Worker#endif 21*58b9f456SAndroid Build Coastguard Worker 22*58b9f456SAndroid Build Coastguard Worker_LIBCPP_PUSH_MACROS 23*58b9f456SAndroid Build Coastguard Worker#include <__undef_macros> 24*58b9f456SAndroid Build Coastguard Worker 25*58b9f456SAndroid Build Coastguard Worker 26*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD 27*58b9f456SAndroid Build Coastguard Worker 28*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst, typename _Cp::__storage_type = 0> class __bit_iterator; 29*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> class __bit_const_reference; 30*58b9f456SAndroid Build Coastguard Worker 31*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp> 32*58b9f456SAndroid Build Coastguard Workerstruct __has_storage_type 33*58b9f456SAndroid Build Coastguard Worker{ 34*58b9f456SAndroid Build Coastguard Worker static const bool value = false; 35*58b9f456SAndroid Build Coastguard Worker}; 36*58b9f456SAndroid Build Coastguard Worker 37*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool = __has_storage_type<_Cp>::value> 38*58b9f456SAndroid Build Coastguard Workerclass __bit_reference 39*58b9f456SAndroid Build Coastguard Worker{ 40*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__storage_type __storage_type; 41*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__storage_pointer __storage_pointer; 42*58b9f456SAndroid Build Coastguard Worker 43*58b9f456SAndroid Build Coastguard Worker __storage_pointer __seg_; 44*58b9f456SAndroid Build Coastguard Worker __storage_type __mask_; 45*58b9f456SAndroid Build Coastguard Worker 46*58b9f456SAndroid Build Coastguard Worker friend typename _Cp::__self; 47*58b9f456SAndroid Build Coastguard Worker 48*58b9f456SAndroid Build Coastguard Worker friend class __bit_const_reference<_Cp>; 49*58b9f456SAndroid Build Coastguard Worker friend class __bit_iterator<_Cp, false>; 50*58b9f456SAndroid Build Coastguard Workerpublic: 51*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT 52*58b9f456SAndroid Build Coastguard Worker {return static_cast<bool>(*__seg_ & __mask_);} 53*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY bool operator ~() const _NOEXCEPT 54*58b9f456SAndroid Build Coastguard Worker {return !static_cast<bool>(*this);} 55*58b9f456SAndroid Build Coastguard Worker 56*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 57*58b9f456SAndroid Build Coastguard Worker __bit_reference& operator=(bool __x) _NOEXCEPT 58*58b9f456SAndroid Build Coastguard Worker { 59*58b9f456SAndroid Build Coastguard Worker if (__x) 60*58b9f456SAndroid Build Coastguard Worker *__seg_ |= __mask_; 61*58b9f456SAndroid Build Coastguard Worker else 62*58b9f456SAndroid Build Coastguard Worker *__seg_ &= ~__mask_; 63*58b9f456SAndroid Build Coastguard Worker return *this; 64*58b9f456SAndroid Build Coastguard Worker } 65*58b9f456SAndroid Build Coastguard Worker 66*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 67*58b9f456SAndroid Build Coastguard Worker __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT 68*58b9f456SAndroid Build Coastguard Worker {return operator=(static_cast<bool>(__x));} 69*58b9f456SAndroid Build Coastguard Worker 70*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {*__seg_ ^= __mask_;} 71*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, false> operator&() const _NOEXCEPT 72*58b9f456SAndroid Build Coastguard Worker {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(__ctz(__mask_)));} 73*58b9f456SAndroid Build Coastguard Workerprivate: 74*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 75*58b9f456SAndroid Build Coastguard Worker __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT 76*58b9f456SAndroid Build Coastguard Worker : __seg_(__s), __mask_(__m) {} 77*58b9f456SAndroid Build Coastguard Worker}; 78*58b9f456SAndroid Build Coastguard Worker 79*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 80*58b9f456SAndroid Build Coastguard Workerclass __bit_reference<_Cp, false> 81*58b9f456SAndroid Build Coastguard Worker{ 82*58b9f456SAndroid Build Coastguard Worker}; 83*58b9f456SAndroid Build Coastguard Worker 84*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 85*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 86*58b9f456SAndroid Build Coastguard Workervoid 87*58b9f456SAndroid Build Coastguard Workerswap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT 88*58b9f456SAndroid Build Coastguard Worker{ 89*58b9f456SAndroid Build Coastguard Worker bool __t = __x; 90*58b9f456SAndroid Build Coastguard Worker __x = __y; 91*58b9f456SAndroid Build Coastguard Worker __y = __t; 92*58b9f456SAndroid Build Coastguard Worker} 93*58b9f456SAndroid Build Coastguard Worker 94*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, class _Dp> 95*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 96*58b9f456SAndroid Build Coastguard Workervoid 97*58b9f456SAndroid Build Coastguard Workerswap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT 98*58b9f456SAndroid Build Coastguard Worker{ 99*58b9f456SAndroid Build Coastguard Worker bool __t = __x; 100*58b9f456SAndroid Build Coastguard Worker __x = __y; 101*58b9f456SAndroid Build Coastguard Worker __y = __t; 102*58b9f456SAndroid Build Coastguard Worker} 103*58b9f456SAndroid Build Coastguard Worker 104*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 105*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 106*58b9f456SAndroid Build Coastguard Workervoid 107*58b9f456SAndroid Build Coastguard Workerswap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT 108*58b9f456SAndroid Build Coastguard Worker{ 109*58b9f456SAndroid Build Coastguard Worker bool __t = __x; 110*58b9f456SAndroid Build Coastguard Worker __x = __y; 111*58b9f456SAndroid Build Coastguard Worker __y = __t; 112*58b9f456SAndroid Build Coastguard Worker} 113*58b9f456SAndroid Build Coastguard Worker 114*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 115*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 116*58b9f456SAndroid Build Coastguard Workervoid 117*58b9f456SAndroid Build Coastguard Workerswap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT 118*58b9f456SAndroid Build Coastguard Worker{ 119*58b9f456SAndroid Build Coastguard Worker bool __t = __x; 120*58b9f456SAndroid Build Coastguard Worker __x = __y; 121*58b9f456SAndroid Build Coastguard Worker __y = __t; 122*58b9f456SAndroid Build Coastguard Worker} 123*58b9f456SAndroid Build Coastguard Worker 124*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 125*58b9f456SAndroid Build Coastguard Workerclass __bit_const_reference 126*58b9f456SAndroid Build Coastguard Worker{ 127*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__storage_type __storage_type; 128*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__const_storage_pointer __storage_pointer; 129*58b9f456SAndroid Build Coastguard Worker 130*58b9f456SAndroid Build Coastguard Worker __storage_pointer __seg_; 131*58b9f456SAndroid Build Coastguard Worker __storage_type __mask_; 132*58b9f456SAndroid Build Coastguard Worker 133*58b9f456SAndroid Build Coastguard Worker friend typename _Cp::__self; 134*58b9f456SAndroid Build Coastguard Worker friend class __bit_iterator<_Cp, true>; 135*58b9f456SAndroid Build Coastguard Workerpublic: 136*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 137*58b9f456SAndroid Build Coastguard Worker __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT 138*58b9f456SAndroid Build Coastguard Worker : __seg_(__x.__seg_), __mask_(__x.__mask_) {} 139*58b9f456SAndroid Build Coastguard Worker 140*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT 141*58b9f456SAndroid Build Coastguard Worker {return static_cast<bool>(*__seg_ & __mask_);} 142*58b9f456SAndroid Build Coastguard Worker 143*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, true> operator&() const _NOEXCEPT 144*58b9f456SAndroid Build Coastguard Worker {return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(__ctz(__mask_)));} 145*58b9f456SAndroid Build Coastguard Workerprivate: 146*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 147*58b9f456SAndroid Build Coastguard Worker _LIBCPP_CONSTEXPR 148*58b9f456SAndroid Build Coastguard Worker __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT 149*58b9f456SAndroid Build Coastguard Worker : __seg_(__s), __mask_(__m) {} 150*58b9f456SAndroid Build Coastguard Worker 151*58b9f456SAndroid Build Coastguard Worker __bit_const_reference& operator=(const __bit_const_reference& __x); 152*58b9f456SAndroid Build Coastguard Worker}; 153*58b9f456SAndroid Build Coastguard Worker 154*58b9f456SAndroid Build Coastguard Worker// find 155*58b9f456SAndroid Build Coastguard Worker 156*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 157*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, _IsConst> 158*58b9f456SAndroid Build Coastguard Worker__find_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 159*58b9f456SAndroid Build Coastguard Worker{ 160*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _It; 161*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 162*58b9f456SAndroid Build Coastguard Worker static const int __bits_per_word = _It::__bits_per_word; 163*58b9f456SAndroid Build Coastguard Worker // do first partial word 164*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 165*58b9f456SAndroid Build Coastguard Worker { 166*58b9f456SAndroid Build Coastguard Worker __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 167*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__clz_f, __n); 168*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 169*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_ & __m; 170*58b9f456SAndroid Build Coastguard Worker if (__b) 171*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); 172*58b9f456SAndroid Build Coastguard Worker if (__n == __dn) 173*58b9f456SAndroid Build Coastguard Worker return __first + __n; 174*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 175*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 176*58b9f456SAndroid Build Coastguard Worker } 177*58b9f456SAndroid Build Coastguard Worker // do middle whole words 178*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 179*58b9f456SAndroid Build Coastguard Worker if (*__first.__seg_) 180*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(*__first.__seg_))); 181*58b9f456SAndroid Build Coastguard Worker // do last partial word 182*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 183*58b9f456SAndroid Build Coastguard Worker { 184*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 185*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_ & __m; 186*58b9f456SAndroid Build Coastguard Worker if (__b) 187*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); 188*58b9f456SAndroid Build Coastguard Worker } 189*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(__n)); 190*58b9f456SAndroid Build Coastguard Worker} 191*58b9f456SAndroid Build Coastguard Worker 192*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 193*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, _IsConst> 194*58b9f456SAndroid Build Coastguard Worker__find_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 195*58b9f456SAndroid Build Coastguard Worker{ 196*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _It; 197*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 198*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _It::__bits_per_word; 199*58b9f456SAndroid Build Coastguard Worker // do first partial word 200*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 201*58b9f456SAndroid Build Coastguard Worker { 202*58b9f456SAndroid Build Coastguard Worker __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 203*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__clz_f, __n); 204*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 205*58b9f456SAndroid Build Coastguard Worker __storage_type __b = ~*__first.__seg_ & __m; 206*58b9f456SAndroid Build Coastguard Worker if (__b) 207*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); 208*58b9f456SAndroid Build Coastguard Worker if (__n == __dn) 209*58b9f456SAndroid Build Coastguard Worker return __first + __n; 210*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 211*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 212*58b9f456SAndroid Build Coastguard Worker } 213*58b9f456SAndroid Build Coastguard Worker // do middle whole words 214*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 215*58b9f456SAndroid Build Coastguard Worker { 216*58b9f456SAndroid Build Coastguard Worker __storage_type __b = ~*__first.__seg_; 217*58b9f456SAndroid Build Coastguard Worker if (__b) 218*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); 219*58b9f456SAndroid Build Coastguard Worker } 220*58b9f456SAndroid Build Coastguard Worker // do last partial word 221*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 222*58b9f456SAndroid Build Coastguard Worker { 223*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 224*58b9f456SAndroid Build Coastguard Worker __storage_type __b = ~*__first.__seg_ & __m; 225*58b9f456SAndroid Build Coastguard Worker if (__b) 226*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); 227*58b9f456SAndroid Build Coastguard Worker } 228*58b9f456SAndroid Build Coastguard Worker return _It(__first.__seg_, static_cast<unsigned>(__n)); 229*58b9f456SAndroid Build Coastguard Worker} 230*58b9f456SAndroid Build Coastguard Worker 231*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst, class _Tp> 232*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 233*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, _IsConst> 234*58b9f456SAndroid Build Coastguard Workerfind(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value_) 235*58b9f456SAndroid Build Coastguard Worker{ 236*58b9f456SAndroid Build Coastguard Worker if (static_cast<bool>(__value_)) 237*58b9f456SAndroid Build Coastguard Worker return __find_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first)); 238*58b9f456SAndroid Build Coastguard Worker return __find_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first)); 239*58b9f456SAndroid Build Coastguard Worker} 240*58b9f456SAndroid Build Coastguard Worker 241*58b9f456SAndroid Build Coastguard Worker// count 242*58b9f456SAndroid Build Coastguard Worker 243*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 244*58b9f456SAndroid Build Coastguard Workertypename __bit_iterator<_Cp, _IsConst>::difference_type 245*58b9f456SAndroid Build Coastguard Worker__count_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 246*58b9f456SAndroid Build Coastguard Worker{ 247*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _It; 248*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 249*58b9f456SAndroid Build Coastguard Worker typedef typename _It::difference_type difference_type; 250*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _It::__bits_per_word; 251*58b9f456SAndroid Build Coastguard Worker difference_type __r = 0; 252*58b9f456SAndroid Build Coastguard Worker // do first partial word 253*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 254*58b9f456SAndroid Build Coastguard Worker { 255*58b9f456SAndroid Build Coastguard Worker __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 256*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__clz_f, __n); 257*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 258*58b9f456SAndroid Build Coastguard Worker __r = _VSTD::__popcount(*__first.__seg_ & __m); 259*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 260*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 261*58b9f456SAndroid Build Coastguard Worker } 262*58b9f456SAndroid Build Coastguard Worker // do middle whole words 263*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 264*58b9f456SAndroid Build Coastguard Worker __r += _VSTD::__popcount(*__first.__seg_); 265*58b9f456SAndroid Build Coastguard Worker // do last partial word 266*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 267*58b9f456SAndroid Build Coastguard Worker { 268*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 269*58b9f456SAndroid Build Coastguard Worker __r += _VSTD::__popcount(*__first.__seg_ & __m); 270*58b9f456SAndroid Build Coastguard Worker } 271*58b9f456SAndroid Build Coastguard Worker return __r; 272*58b9f456SAndroid Build Coastguard Worker} 273*58b9f456SAndroid Build Coastguard Worker 274*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 275*58b9f456SAndroid Build Coastguard Workertypename __bit_iterator<_Cp, _IsConst>::difference_type 276*58b9f456SAndroid Build Coastguard Worker__count_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 277*58b9f456SAndroid Build Coastguard Worker{ 278*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _It; 279*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 280*58b9f456SAndroid Build Coastguard Worker typedef typename _It::difference_type difference_type; 281*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _It::__bits_per_word; 282*58b9f456SAndroid Build Coastguard Worker difference_type __r = 0; 283*58b9f456SAndroid Build Coastguard Worker // do first partial word 284*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 285*58b9f456SAndroid Build Coastguard Worker { 286*58b9f456SAndroid Build Coastguard Worker __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 287*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__clz_f, __n); 288*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 289*58b9f456SAndroid Build Coastguard Worker __r = _VSTD::__popcount(~*__first.__seg_ & __m); 290*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 291*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 292*58b9f456SAndroid Build Coastguard Worker } 293*58b9f456SAndroid Build Coastguard Worker // do middle whole words 294*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 295*58b9f456SAndroid Build Coastguard Worker __r += _VSTD::__popcount(~*__first.__seg_); 296*58b9f456SAndroid Build Coastguard Worker // do last partial word 297*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 298*58b9f456SAndroid Build Coastguard Worker { 299*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 300*58b9f456SAndroid Build Coastguard Worker __r += _VSTD::__popcount(~*__first.__seg_ & __m); 301*58b9f456SAndroid Build Coastguard Worker } 302*58b9f456SAndroid Build Coastguard Worker return __r; 303*58b9f456SAndroid Build Coastguard Worker} 304*58b9f456SAndroid Build Coastguard Worker 305*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst, class _Tp> 306*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 307*58b9f456SAndroid Build Coastguard Workertypename __bit_iterator<_Cp, _IsConst>::difference_type 308*58b9f456SAndroid Build Coastguard Workercount(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value_) 309*58b9f456SAndroid Build Coastguard Worker{ 310*58b9f456SAndroid Build Coastguard Worker if (static_cast<bool>(__value_)) 311*58b9f456SAndroid Build Coastguard Worker return __count_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first)); 312*58b9f456SAndroid Build Coastguard Worker return __count_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first)); 313*58b9f456SAndroid Build Coastguard Worker} 314*58b9f456SAndroid Build Coastguard Worker 315*58b9f456SAndroid Build Coastguard Worker// fill_n 316*58b9f456SAndroid Build Coastguard Worker 317*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 318*58b9f456SAndroid Build Coastguard Workervoid 319*58b9f456SAndroid Build Coastguard Worker__fill_n_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) 320*58b9f456SAndroid Build Coastguard Worker{ 321*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, false> _It; 322*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 323*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _It::__bits_per_word; 324*58b9f456SAndroid Build Coastguard Worker // do first partial word 325*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 326*58b9f456SAndroid Build Coastguard Worker { 327*58b9f456SAndroid Build Coastguard Worker __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 328*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__clz_f, __n); 329*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 330*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ &= ~__m; 331*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 332*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 333*58b9f456SAndroid Build Coastguard Worker } 334*58b9f456SAndroid Build Coastguard Worker // do middle whole words 335*58b9f456SAndroid Build Coastguard Worker __storage_type __nw = __n / __bits_per_word; 336*58b9f456SAndroid Build Coastguard Worker _VSTD::memset(_VSTD::__to_raw_pointer(__first.__seg_), 0, __nw * sizeof(__storage_type)); 337*58b9f456SAndroid Build Coastguard Worker __n -= __nw * __bits_per_word; 338*58b9f456SAndroid Build Coastguard Worker // do last partial word 339*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 340*58b9f456SAndroid Build Coastguard Worker { 341*58b9f456SAndroid Build Coastguard Worker __first.__seg_ += __nw; 342*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 343*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ &= ~__m; 344*58b9f456SAndroid Build Coastguard Worker } 345*58b9f456SAndroid Build Coastguard Worker} 346*58b9f456SAndroid Build Coastguard Worker 347*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 348*58b9f456SAndroid Build Coastguard Workervoid 349*58b9f456SAndroid Build Coastguard Worker__fill_n_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) 350*58b9f456SAndroid Build Coastguard Worker{ 351*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, false> _It; 352*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 353*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _It::__bits_per_word; 354*58b9f456SAndroid Build Coastguard Worker // do first partial word 355*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 356*58b9f456SAndroid Build Coastguard Worker { 357*58b9f456SAndroid Build Coastguard Worker __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 358*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__clz_f, __n); 359*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 360*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __m; 361*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 362*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 363*58b9f456SAndroid Build Coastguard Worker } 364*58b9f456SAndroid Build Coastguard Worker // do middle whole words 365*58b9f456SAndroid Build Coastguard Worker __storage_type __nw = __n / __bits_per_word; 366*58b9f456SAndroid Build Coastguard Worker _VSTD::memset(_VSTD::__to_raw_pointer(__first.__seg_), -1, __nw * sizeof(__storage_type)); 367*58b9f456SAndroid Build Coastguard Worker __n -= __nw * __bits_per_word; 368*58b9f456SAndroid Build Coastguard Worker // do last partial word 369*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 370*58b9f456SAndroid Build Coastguard Worker { 371*58b9f456SAndroid Build Coastguard Worker __first.__seg_ += __nw; 372*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 373*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __m; 374*58b9f456SAndroid Build Coastguard Worker } 375*58b9f456SAndroid Build Coastguard Worker} 376*58b9f456SAndroid Build Coastguard Worker 377*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 378*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 379*58b9f456SAndroid Build Coastguard Workervoid 380*58b9f456SAndroid Build Coastguard Workerfill_n(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __value_) 381*58b9f456SAndroid Build Coastguard Worker{ 382*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 383*58b9f456SAndroid Build Coastguard Worker { 384*58b9f456SAndroid Build Coastguard Worker if (__value_) 385*58b9f456SAndroid Build Coastguard Worker __fill_n_true(__first, __n); 386*58b9f456SAndroid Build Coastguard Worker else 387*58b9f456SAndroid Build Coastguard Worker __fill_n_false(__first, __n); 388*58b9f456SAndroid Build Coastguard Worker } 389*58b9f456SAndroid Build Coastguard Worker} 390*58b9f456SAndroid Build Coastguard Worker 391*58b9f456SAndroid Build Coastguard Worker// fill 392*58b9f456SAndroid Build Coastguard Worker 393*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 394*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 395*58b9f456SAndroid Build Coastguard Workervoid 396*58b9f456SAndroid Build Coastguard Workerfill(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, bool __value_) 397*58b9f456SAndroid Build Coastguard Worker{ 398*58b9f456SAndroid Build Coastguard Worker _VSTD::fill_n(__first, static_cast<typename _Cp::size_type>(__last - __first), __value_); 399*58b9f456SAndroid Build Coastguard Worker} 400*58b9f456SAndroid Build Coastguard Worker 401*58b9f456SAndroid Build Coastguard Worker// copy 402*58b9f456SAndroid Build Coastguard Worker 403*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 404*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 405*58b9f456SAndroid Build Coastguard Worker__copy_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 406*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, false> __result) 407*58b9f456SAndroid Build Coastguard Worker{ 408*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _In; 409*58b9f456SAndroid Build Coastguard Worker typedef typename _In::difference_type difference_type; 410*58b9f456SAndroid Build Coastguard Worker typedef typename _In::__storage_type __storage_type; 411*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _In::__bits_per_word; 412*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last - __first; 413*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 414*58b9f456SAndroid Build Coastguard Worker { 415*58b9f456SAndroid Build Coastguard Worker // do first word 416*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 417*58b9f456SAndroid Build Coastguard Worker { 418*58b9f456SAndroid Build Coastguard Worker unsigned __clz = __bits_per_word - __first.__ctz_; 419*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); 420*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 421*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); 422*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_ & __m; 423*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 424*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b; 425*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 426*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 427*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 428*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ = 0; 429*58b9f456SAndroid Build Coastguard Worker } 430*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ == 0; 431*58b9f456SAndroid Build Coastguard Worker // do middle words 432*58b9f456SAndroid Build Coastguard Worker __storage_type __nw = __n / __bits_per_word; 433*58b9f456SAndroid Build Coastguard Worker _VSTD::memmove(_VSTD::__to_raw_pointer(__result.__seg_), 434*58b9f456SAndroid Build Coastguard Worker _VSTD::__to_raw_pointer(__first.__seg_), 435*58b9f456SAndroid Build Coastguard Worker __nw * sizeof(__storage_type)); 436*58b9f456SAndroid Build Coastguard Worker __n -= __nw * __bits_per_word; 437*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += __nw; 438*58b9f456SAndroid Build Coastguard Worker // do last word 439*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 440*58b9f456SAndroid Build Coastguard Worker { 441*58b9f456SAndroid Build Coastguard Worker __first.__seg_ += __nw; 442*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 443*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_ & __m; 444*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 445*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b; 446*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(__n); 447*58b9f456SAndroid Build Coastguard Worker } 448*58b9f456SAndroid Build Coastguard Worker } 449*58b9f456SAndroid Build Coastguard Worker return __result; 450*58b9f456SAndroid Build Coastguard Worker} 451*58b9f456SAndroid Build Coastguard Worker 452*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 453*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 454*58b9f456SAndroid Build Coastguard Worker__copy_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 455*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, false> __result) 456*58b9f456SAndroid Build Coastguard Worker{ 457*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _In; 458*58b9f456SAndroid Build Coastguard Worker typedef typename _In::difference_type difference_type; 459*58b9f456SAndroid Build Coastguard Worker typedef typename _In::__storage_type __storage_type; 460*58b9f456SAndroid Build Coastguard Worker static const int __bits_per_word = _In::__bits_per_word; 461*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last - __first; 462*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 463*58b9f456SAndroid Build Coastguard Worker { 464*58b9f456SAndroid Build Coastguard Worker // do first word 465*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 466*58b9f456SAndroid Build Coastguard Worker { 467*58b9f456SAndroid Build Coastguard Worker unsigned __clz_f = __bits_per_word - __first.__ctz_; 468*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); 469*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 470*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 471*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_ & __m; 472*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __result.__ctz_; 473*58b9f456SAndroid Build Coastguard Worker __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); 474*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); 475*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 476*58b9f456SAndroid Build Coastguard Worker if (__result.__ctz_ > __first.__ctz_) 477*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_); 478*58b9f456SAndroid Build Coastguard Worker else 479*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_); 480*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word; 481*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word); 482*58b9f456SAndroid Build Coastguard Worker __dn -= __ddn; 483*58b9f456SAndroid Build Coastguard Worker if (__dn > 0) 484*58b9f456SAndroid Build Coastguard Worker { 485*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __dn); 486*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 487*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn); 488*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(__dn); 489*58b9f456SAndroid Build Coastguard Worker } 490*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 491*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ = 0; 492*58b9f456SAndroid Build Coastguard Worker } 493*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ == 0; 494*58b9f456SAndroid Build Coastguard Worker // do middle words 495*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __result.__ctz_; 496*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) << __result.__ctz_; 497*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) 498*58b9f456SAndroid Build Coastguard Worker { 499*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_; 500*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 501*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << __result.__ctz_; 502*58b9f456SAndroid Build Coastguard Worker ++__result.__seg_; 503*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= __m; 504*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> __clz_r; 505*58b9f456SAndroid Build Coastguard Worker } 506*58b9f456SAndroid Build Coastguard Worker // do last word 507*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 508*58b9f456SAndroid Build Coastguard Worker { 509*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __n); 510*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first.__seg_ & __m; 511*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r)); 512*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); 513*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 514*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << __result.__ctz_; 515*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 516*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 517*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 518*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 519*58b9f456SAndroid Build Coastguard Worker { 520*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __n); 521*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 522*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> __dn; 523*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(__n); 524*58b9f456SAndroid Build Coastguard Worker } 525*58b9f456SAndroid Build Coastguard Worker } 526*58b9f456SAndroid Build Coastguard Worker } 527*58b9f456SAndroid Build Coastguard Worker return __result; 528*58b9f456SAndroid Build Coastguard Worker} 529*58b9f456SAndroid Build Coastguard Worker 530*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 531*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 532*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 533*58b9f456SAndroid Build Coastguard Workercopy(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 534*58b9f456SAndroid Build Coastguard Worker{ 535*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ == __result.__ctz_) 536*58b9f456SAndroid Build Coastguard Worker return __copy_aligned(__first, __last, __result); 537*58b9f456SAndroid Build Coastguard Worker return __copy_unaligned(__first, __last, __result); 538*58b9f456SAndroid Build Coastguard Worker} 539*58b9f456SAndroid Build Coastguard Worker 540*58b9f456SAndroid Build Coastguard Worker// copy_backward 541*58b9f456SAndroid Build Coastguard Worker 542*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 543*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 544*58b9f456SAndroid Build Coastguard Worker__copy_backward_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 545*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, false> __result) 546*58b9f456SAndroid Build Coastguard Worker{ 547*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _In; 548*58b9f456SAndroid Build Coastguard Worker typedef typename _In::difference_type difference_type; 549*58b9f456SAndroid Build Coastguard Worker typedef typename _In::__storage_type __storage_type; 550*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _In::__bits_per_word; 551*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last - __first; 552*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 553*58b9f456SAndroid Build Coastguard Worker { 554*58b9f456SAndroid Build Coastguard Worker // do first word 555*58b9f456SAndroid Build Coastguard Worker if (__last.__ctz_ != 0) 556*58b9f456SAndroid Build Coastguard Worker { 557*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n); 558*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 559*58b9f456SAndroid Build Coastguard Worker unsigned __clz = __bits_per_word - __last.__ctz_; 560*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz); 561*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__last.__seg_ & __m; 562*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 563*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b; 564*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + 565*58b9f456SAndroid Build Coastguard Worker __result.__ctz_) % __bits_per_word); 566*58b9f456SAndroid Build Coastguard Worker // __last.__ctz_ = 0 567*58b9f456SAndroid Build Coastguard Worker } 568*58b9f456SAndroid Build Coastguard Worker // __last.__ctz_ == 0 || __n == 0 569*58b9f456SAndroid Build Coastguard Worker // __result.__ctz_ == 0 || __n == 0 570*58b9f456SAndroid Build Coastguard Worker // do middle words 571*58b9f456SAndroid Build Coastguard Worker __storage_type __nw = __n / __bits_per_word; 572*58b9f456SAndroid Build Coastguard Worker __result.__seg_ -= __nw; 573*58b9f456SAndroid Build Coastguard Worker __last.__seg_ -= __nw; 574*58b9f456SAndroid Build Coastguard Worker _VSTD::memmove(_VSTD::__to_raw_pointer(__result.__seg_), 575*58b9f456SAndroid Build Coastguard Worker _VSTD::__to_raw_pointer(__last.__seg_), 576*58b9f456SAndroid Build Coastguard Worker __nw * sizeof(__storage_type)); 577*58b9f456SAndroid Build Coastguard Worker __n -= __nw * __bits_per_word; 578*58b9f456SAndroid Build Coastguard Worker // do last word 579*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 580*58b9f456SAndroid Build Coastguard Worker { 581*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n); 582*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *--__last.__seg_ & __m; 583*58b9f456SAndroid Build Coastguard Worker *--__result.__seg_ &= ~__m; 584*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b; 585*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1)); 586*58b9f456SAndroid Build Coastguard Worker } 587*58b9f456SAndroid Build Coastguard Worker } 588*58b9f456SAndroid Build Coastguard Worker return __result; 589*58b9f456SAndroid Build Coastguard Worker} 590*58b9f456SAndroid Build Coastguard Worker 591*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 592*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 593*58b9f456SAndroid Build Coastguard Worker__copy_backward_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 594*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, false> __result) 595*58b9f456SAndroid Build Coastguard Worker{ 596*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IsConst> _In; 597*58b9f456SAndroid Build Coastguard Worker typedef typename _In::difference_type difference_type; 598*58b9f456SAndroid Build Coastguard Worker typedef typename _In::__storage_type __storage_type; 599*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _In::__bits_per_word; 600*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last - __first; 601*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 602*58b9f456SAndroid Build Coastguard Worker { 603*58b9f456SAndroid Build Coastguard Worker // do first word 604*58b9f456SAndroid Build Coastguard Worker if (__last.__ctz_ != 0) 605*58b9f456SAndroid Build Coastguard Worker { 606*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n); 607*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 608*58b9f456SAndroid Build Coastguard Worker unsigned __clz_l = __bits_per_word - __last.__ctz_; 609*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l); 610*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__last.__seg_ & __m; 611*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __result.__ctz_; 612*58b9f456SAndroid Build Coastguard Worker __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_)); 613*58b9f456SAndroid Build Coastguard Worker if (__ddn > 0) 614*58b9f456SAndroid Build Coastguard Worker { 615*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r); 616*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 617*58b9f456SAndroid Build Coastguard Worker if (__result.__ctz_ > __last.__ctz_) 618*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_); 619*58b9f456SAndroid Build Coastguard Worker else 620*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_); 621*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) + 622*58b9f456SAndroid Build Coastguard Worker __result.__ctz_) % __bits_per_word); 623*58b9f456SAndroid Build Coastguard Worker __dn -= __ddn; 624*58b9f456SAndroid Build Coastguard Worker } 625*58b9f456SAndroid Build Coastguard Worker if (__dn > 0) 626*58b9f456SAndroid Build Coastguard Worker { 627*58b9f456SAndroid Build Coastguard Worker // __result.__ctz_ == 0 628*58b9f456SAndroid Build Coastguard Worker --__result.__seg_; 629*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1)); 630*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) << __result.__ctz_; 631*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 632*58b9f456SAndroid Build Coastguard Worker __last.__ctz_ -= __dn + __ddn; 633*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_); 634*58b9f456SAndroid Build Coastguard Worker } 635*58b9f456SAndroid Build Coastguard Worker // __last.__ctz_ = 0 636*58b9f456SAndroid Build Coastguard Worker } 637*58b9f456SAndroid Build Coastguard Worker // __last.__ctz_ == 0 || __n == 0 638*58b9f456SAndroid Build Coastguard Worker // __result.__ctz_ != 0 || __n == 0 639*58b9f456SAndroid Build Coastguard Worker // do middle words 640*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __result.__ctz_; 641*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> __clz_r; 642*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; __n -= __bits_per_word) 643*58b9f456SAndroid Build Coastguard Worker { 644*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *--__last.__seg_; 645*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 646*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> __clz_r; 647*58b9f456SAndroid Build Coastguard Worker *--__result.__seg_ &= __m; 648*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << __result.__ctz_; 649*58b9f456SAndroid Build Coastguard Worker } 650*58b9f456SAndroid Build Coastguard Worker // do last word 651*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 652*58b9f456SAndroid Build Coastguard Worker { 653*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) << (__bits_per_word - __n); 654*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *--__last.__seg_ & __m; 655*58b9f456SAndroid Build Coastguard Worker __clz_r = __bits_per_word - __result.__ctz_; 656*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_)); 657*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r); 658*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 659*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_); 660*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + 661*58b9f456SAndroid Build Coastguard Worker __result.__ctz_) % __bits_per_word); 662*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 663*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 664*58b9f456SAndroid Build Coastguard Worker { 665*58b9f456SAndroid Build Coastguard Worker // __result.__ctz_ == 0 666*58b9f456SAndroid Build Coastguard Worker --__result.__seg_; 667*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1)); 668*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) << __result.__ctz_; 669*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 670*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn)); 671*58b9f456SAndroid Build Coastguard Worker } 672*58b9f456SAndroid Build Coastguard Worker } 673*58b9f456SAndroid Build Coastguard Worker } 674*58b9f456SAndroid Build Coastguard Worker return __result; 675*58b9f456SAndroid Build Coastguard Worker} 676*58b9f456SAndroid Build Coastguard Worker 677*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 678*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 679*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 680*58b9f456SAndroid Build Coastguard Workercopy_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 681*58b9f456SAndroid Build Coastguard Worker{ 682*58b9f456SAndroid Build Coastguard Worker if (__last.__ctz_ == __result.__ctz_) 683*58b9f456SAndroid Build Coastguard Worker return __copy_backward_aligned(__first, __last, __result); 684*58b9f456SAndroid Build Coastguard Worker return __copy_backward_unaligned(__first, __last, __result); 685*58b9f456SAndroid Build Coastguard Worker} 686*58b9f456SAndroid Build Coastguard Worker 687*58b9f456SAndroid Build Coastguard Worker// move 688*58b9f456SAndroid Build Coastguard Worker 689*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 690*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 691*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 692*58b9f456SAndroid Build Coastguard Workermove(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 693*58b9f456SAndroid Build Coastguard Worker{ 694*58b9f456SAndroid Build Coastguard Worker return _VSTD::copy(__first, __last, __result); 695*58b9f456SAndroid Build Coastguard Worker} 696*58b9f456SAndroid Build Coastguard Worker 697*58b9f456SAndroid Build Coastguard Worker// move_backward 698*58b9f456SAndroid Build Coastguard Worker 699*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst> 700*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 701*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 702*58b9f456SAndroid Build Coastguard Workermove_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 703*58b9f456SAndroid Build Coastguard Worker{ 704*58b9f456SAndroid Build Coastguard Worker return _VSTD::copy_backward(__first, __last, __result); 705*58b9f456SAndroid Build Coastguard Worker} 706*58b9f456SAndroid Build Coastguard Worker 707*58b9f456SAndroid Build Coastguard Worker// swap_ranges 708*58b9f456SAndroid Build Coastguard Worker 709*58b9f456SAndroid Build Coastguard Workertemplate <class __C1, class __C2> 710*58b9f456SAndroid Build Coastguard Worker__bit_iterator<__C2, false> 711*58b9f456SAndroid Build Coastguard Worker__swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last, 712*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C2, false> __result) 713*58b9f456SAndroid Build Coastguard Worker{ 714*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<__C1, false> _I1; 715*58b9f456SAndroid Build Coastguard Worker typedef typename _I1::difference_type difference_type; 716*58b9f456SAndroid Build Coastguard Worker typedef typename _I1::__storage_type __storage_type; 717*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _I1::__bits_per_word; 718*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last - __first; 719*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 720*58b9f456SAndroid Build Coastguard Worker { 721*58b9f456SAndroid Build Coastguard Worker // do first word 722*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 723*58b9f456SAndroid Build Coastguard Worker { 724*58b9f456SAndroid Build Coastguard Worker unsigned __clz = __bits_per_word - __first.__ctz_; 725*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); 726*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 727*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); 728*58b9f456SAndroid Build Coastguard Worker __storage_type __b1 = *__first.__seg_ & __m; 729*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ &= ~__m; 730*58b9f456SAndroid Build Coastguard Worker __storage_type __b2 = *__result.__seg_ & __m; 731*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 732*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1; 733*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2; 734*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 735*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 736*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 737*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ = 0; 738*58b9f456SAndroid Build Coastguard Worker } 739*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ == 0; 740*58b9f456SAndroid Build Coastguard Worker // do middle words 741*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_) 742*58b9f456SAndroid Build Coastguard Worker swap(*__first.__seg_, *__result.__seg_); 743*58b9f456SAndroid Build Coastguard Worker // do last word 744*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 745*58b9f456SAndroid Build Coastguard Worker { 746*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 747*58b9f456SAndroid Build Coastguard Worker __storage_type __b1 = *__first.__seg_ & __m; 748*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ &= ~__m; 749*58b9f456SAndroid Build Coastguard Worker __storage_type __b2 = *__result.__seg_ & __m; 750*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 751*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1; 752*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2; 753*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(__n); 754*58b9f456SAndroid Build Coastguard Worker } 755*58b9f456SAndroid Build Coastguard Worker } 756*58b9f456SAndroid Build Coastguard Worker return __result; 757*58b9f456SAndroid Build Coastguard Worker} 758*58b9f456SAndroid Build Coastguard Worker 759*58b9f456SAndroid Build Coastguard Workertemplate <class __C1, class __C2> 760*58b9f456SAndroid Build Coastguard Worker__bit_iterator<__C2, false> 761*58b9f456SAndroid Build Coastguard Worker__swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last, 762*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C2, false> __result) 763*58b9f456SAndroid Build Coastguard Worker{ 764*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<__C1, false> _I1; 765*58b9f456SAndroid Build Coastguard Worker typedef typename _I1::difference_type difference_type; 766*58b9f456SAndroid Build Coastguard Worker typedef typename _I1::__storage_type __storage_type; 767*58b9f456SAndroid Build Coastguard Worker const int __bits_per_word = _I1::__bits_per_word; 768*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last - __first; 769*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 770*58b9f456SAndroid Build Coastguard Worker { 771*58b9f456SAndroid Build Coastguard Worker // do first word 772*58b9f456SAndroid Build Coastguard Worker if (__first.__ctz_ != 0) 773*58b9f456SAndroid Build Coastguard Worker { 774*58b9f456SAndroid Build Coastguard Worker unsigned __clz_f = __bits_per_word - __first.__ctz_; 775*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); 776*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 777*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 778*58b9f456SAndroid Build Coastguard Worker __storage_type __b1 = *__first.__seg_ & __m; 779*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ &= ~__m; 780*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __result.__ctz_; 781*58b9f456SAndroid Build Coastguard Worker __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); 782*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); 783*58b9f456SAndroid Build Coastguard Worker __storage_type __b2 = *__result.__seg_ & __m; 784*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 785*58b9f456SAndroid Build Coastguard Worker if (__result.__ctz_ > __first.__ctz_) 786*58b9f456SAndroid Build Coastguard Worker { 787*58b9f456SAndroid Build Coastguard Worker unsigned __s = __result.__ctz_ - __first.__ctz_; 788*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 << __s; 789*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2 >> __s; 790*58b9f456SAndroid Build Coastguard Worker } 791*58b9f456SAndroid Build Coastguard Worker else 792*58b9f456SAndroid Build Coastguard Worker { 793*58b9f456SAndroid Build Coastguard Worker unsigned __s = __first.__ctz_ - __result.__ctz_; 794*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 >> __s; 795*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2 << __s; 796*58b9f456SAndroid Build Coastguard Worker } 797*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word; 798*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word); 799*58b9f456SAndroid Build Coastguard Worker __dn -= __ddn; 800*58b9f456SAndroid Build Coastguard Worker if (__dn > 0) 801*58b9f456SAndroid Build Coastguard Worker { 802*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __dn); 803*58b9f456SAndroid Build Coastguard Worker __b2 = *__result.__seg_ & __m; 804*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 805*58b9f456SAndroid Build Coastguard Worker unsigned __s = __first.__ctz_ + __ddn; 806*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 >> __s; 807*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2 << __s; 808*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(__dn); 809*58b9f456SAndroid Build Coastguard Worker } 810*58b9f456SAndroid Build Coastguard Worker ++__first.__seg_; 811*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ = 0; 812*58b9f456SAndroid Build Coastguard Worker } 813*58b9f456SAndroid Build Coastguard Worker // __first.__ctz_ == 0; 814*58b9f456SAndroid Build Coastguard Worker // do middle words 815*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) << __result.__ctz_; 816*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __result.__ctz_; 817*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) 818*58b9f456SAndroid Build Coastguard Worker { 819*58b9f456SAndroid Build Coastguard Worker __storage_type __b1 = *__first.__seg_; 820*58b9f456SAndroid Build Coastguard Worker __storage_type __b2 = *__result.__seg_ & __m; 821*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 822*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 << __result.__ctz_; 823*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ = __b2 >> __result.__ctz_; 824*58b9f456SAndroid Build Coastguard Worker ++__result.__seg_; 825*58b9f456SAndroid Build Coastguard Worker __b2 = *__result.__seg_ & ~__m; 826*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= __m; 827*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 >> __clz_r; 828*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2 << __clz_r; 829*58b9f456SAndroid Build Coastguard Worker } 830*58b9f456SAndroid Build Coastguard Worker // do last word 831*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 832*58b9f456SAndroid Build Coastguard Worker { 833*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __n); 834*58b9f456SAndroid Build Coastguard Worker __storage_type __b1 = *__first.__seg_ & __m; 835*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ &= ~__m; 836*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r); 837*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); 838*58b9f456SAndroid Build Coastguard Worker __storage_type __b2 = *__result.__seg_ & __m; 839*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 840*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 << __result.__ctz_; 841*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2 >> __result.__ctz_; 842*58b9f456SAndroid Build Coastguard Worker __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 843*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 844*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 845*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 846*58b9f456SAndroid Build Coastguard Worker { 847*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __n); 848*58b9f456SAndroid Build Coastguard Worker __b2 = *__result.__seg_ & __m; 849*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ &= ~__m; 850*58b9f456SAndroid Build Coastguard Worker *__result.__seg_ |= __b1 >> __dn; 851*58b9f456SAndroid Build Coastguard Worker *__first.__seg_ |= __b2 << __dn; 852*58b9f456SAndroid Build Coastguard Worker __result.__ctz_ = static_cast<unsigned>(__n); 853*58b9f456SAndroid Build Coastguard Worker } 854*58b9f456SAndroid Build Coastguard Worker } 855*58b9f456SAndroid Build Coastguard Worker } 856*58b9f456SAndroid Build Coastguard Worker return __result; 857*58b9f456SAndroid Build Coastguard Worker} 858*58b9f456SAndroid Build Coastguard Worker 859*58b9f456SAndroid Build Coastguard Workertemplate <class __C1, class __C2> 860*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 861*58b9f456SAndroid Build Coastguard Worker__bit_iterator<__C2, false> 862*58b9f456SAndroid Build Coastguard Workerswap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1, 863*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C2, false> __first2) 864*58b9f456SAndroid Build Coastguard Worker{ 865*58b9f456SAndroid Build Coastguard Worker if (__first1.__ctz_ == __first2.__ctz_) 866*58b9f456SAndroid Build Coastguard Worker return __swap_ranges_aligned(__first1, __last1, __first2); 867*58b9f456SAndroid Build Coastguard Worker return __swap_ranges_unaligned(__first1, __last1, __first2); 868*58b9f456SAndroid Build Coastguard Worker} 869*58b9f456SAndroid Build Coastguard Worker 870*58b9f456SAndroid Build Coastguard Worker// rotate 871*58b9f456SAndroid Build Coastguard Worker 872*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 873*58b9f456SAndroid Build Coastguard Workerstruct __bit_array 874*58b9f456SAndroid Build Coastguard Worker{ 875*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::difference_type difference_type; 876*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__storage_type __storage_type; 877*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__storage_pointer __storage_pointer; 878*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::iterator iterator; 879*58b9f456SAndroid Build Coastguard Worker static const unsigned __bits_per_word = _Cp::__bits_per_word; 880*58b9f456SAndroid Build Coastguard Worker static const unsigned _Np = 4; 881*58b9f456SAndroid Build Coastguard Worker 882*58b9f456SAndroid Build Coastguard Worker difference_type __size_; 883*58b9f456SAndroid Build Coastguard Worker __storage_type __word_[_Np]; 884*58b9f456SAndroid Build Coastguard Worker 885*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY static difference_type capacity() 886*58b9f456SAndroid Build Coastguard Worker {return static_cast<difference_type>(_Np * __bits_per_word);} 887*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY explicit __bit_array(difference_type __s) : __size_(__s) {} 888*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY iterator begin() 889*58b9f456SAndroid Build Coastguard Worker { 890*58b9f456SAndroid Build Coastguard Worker return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0); 891*58b9f456SAndroid Build Coastguard Worker } 892*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY iterator end() 893*58b9f456SAndroid Build Coastguard Worker { 894*58b9f456SAndroid Build Coastguard Worker return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word, 895*58b9f456SAndroid Build Coastguard Worker static_cast<unsigned>(__size_ % __bits_per_word)); 896*58b9f456SAndroid Build Coastguard Worker } 897*58b9f456SAndroid Build Coastguard Worker}; 898*58b9f456SAndroid Build Coastguard Worker 899*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp> 900*58b9f456SAndroid Build Coastguard Worker__bit_iterator<_Cp, false> 901*58b9f456SAndroid Build Coastguard Workerrotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last) 902*58b9f456SAndroid Build Coastguard Worker{ 903*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, false> _I1; 904*58b9f456SAndroid Build Coastguard Worker typedef typename _I1::difference_type difference_type; 905*58b9f456SAndroid Build Coastguard Worker difference_type __d1 = __middle - __first; 906*58b9f456SAndroid Build Coastguard Worker difference_type __d2 = __last - __middle; 907*58b9f456SAndroid Build Coastguard Worker _I1 __r = __first + __d2; 908*58b9f456SAndroid Build Coastguard Worker while (__d1 != 0 && __d2 != 0) 909*58b9f456SAndroid Build Coastguard Worker { 910*58b9f456SAndroid Build Coastguard Worker if (__d1 <= __d2) 911*58b9f456SAndroid Build Coastguard Worker { 912*58b9f456SAndroid Build Coastguard Worker if (__d1 <= __bit_array<_Cp>::capacity()) 913*58b9f456SAndroid Build Coastguard Worker { 914*58b9f456SAndroid Build Coastguard Worker __bit_array<_Cp> __b(__d1); 915*58b9f456SAndroid Build Coastguard Worker _VSTD::copy(__first, __middle, __b.begin()); 916*58b9f456SAndroid Build Coastguard Worker _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first)); 917*58b9f456SAndroid Build Coastguard Worker break; 918*58b9f456SAndroid Build Coastguard Worker } 919*58b9f456SAndroid Build Coastguard Worker else 920*58b9f456SAndroid Build Coastguard Worker { 921*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle); 922*58b9f456SAndroid Build Coastguard Worker __first = __middle; 923*58b9f456SAndroid Build Coastguard Worker __middle = __mp; 924*58b9f456SAndroid Build Coastguard Worker __d2 -= __d1; 925*58b9f456SAndroid Build Coastguard Worker } 926*58b9f456SAndroid Build Coastguard Worker } 927*58b9f456SAndroid Build Coastguard Worker else 928*58b9f456SAndroid Build Coastguard Worker { 929*58b9f456SAndroid Build Coastguard Worker if (__d2 <= __bit_array<_Cp>::capacity()) 930*58b9f456SAndroid Build Coastguard Worker { 931*58b9f456SAndroid Build Coastguard Worker __bit_array<_Cp> __b(__d2); 932*58b9f456SAndroid Build Coastguard Worker _VSTD::copy(__middle, __last, __b.begin()); 933*58b9f456SAndroid Build Coastguard Worker _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last)); 934*58b9f456SAndroid Build Coastguard Worker break; 935*58b9f456SAndroid Build Coastguard Worker } 936*58b9f456SAndroid Build Coastguard Worker else 937*58b9f456SAndroid Build Coastguard Worker { 938*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, false> __mp = __first + __d2; 939*58b9f456SAndroid Build Coastguard Worker _VSTD::swap_ranges(__first, __mp, __middle); 940*58b9f456SAndroid Build Coastguard Worker __first = __mp; 941*58b9f456SAndroid Build Coastguard Worker __d1 -= __d2; 942*58b9f456SAndroid Build Coastguard Worker } 943*58b9f456SAndroid Build Coastguard Worker } 944*58b9f456SAndroid Build Coastguard Worker } 945*58b9f456SAndroid Build Coastguard Worker return __r; 946*58b9f456SAndroid Build Coastguard Worker} 947*58b9f456SAndroid Build Coastguard Worker 948*58b9f456SAndroid Build Coastguard Worker// equal 949*58b9f456SAndroid Build Coastguard Worker 950*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IC1, bool _IC2> 951*58b9f456SAndroid Build Coastguard Workerbool 952*58b9f456SAndroid Build Coastguard Worker__equal_unaligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, 953*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, _IC2> __first2) 954*58b9f456SAndroid Build Coastguard Worker{ 955*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IC1> _It; 956*58b9f456SAndroid Build Coastguard Worker typedef typename _It::difference_type difference_type; 957*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 958*58b9f456SAndroid Build Coastguard Worker static const int __bits_per_word = _It::__bits_per_word; 959*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last1 - __first1; 960*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 961*58b9f456SAndroid Build Coastguard Worker { 962*58b9f456SAndroid Build Coastguard Worker // do first word 963*58b9f456SAndroid Build Coastguard Worker if (__first1.__ctz_ != 0) 964*58b9f456SAndroid Build Coastguard Worker { 965*58b9f456SAndroid Build Coastguard Worker unsigned __clz_f = __bits_per_word - __first1.__ctz_; 966*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); 967*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 968*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 969*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first1.__seg_ & __m; 970*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __first2.__ctz_; 971*58b9f456SAndroid Build Coastguard Worker __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); 972*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); 973*58b9f456SAndroid Build Coastguard Worker if (__first2.__ctz_ > __first1.__ctz_) 974*58b9f456SAndroid Build Coastguard Worker { 975*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_))) 976*58b9f456SAndroid Build Coastguard Worker return false; 977*58b9f456SAndroid Build Coastguard Worker } 978*58b9f456SAndroid Build Coastguard Worker else 979*58b9f456SAndroid Build Coastguard Worker { 980*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_))) 981*58b9f456SAndroid Build Coastguard Worker return false; 982*58b9f456SAndroid Build Coastguard Worker } 983*58b9f456SAndroid Build Coastguard Worker __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word; 984*58b9f456SAndroid Build Coastguard Worker __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_) % __bits_per_word); 985*58b9f456SAndroid Build Coastguard Worker __dn -= __ddn; 986*58b9f456SAndroid Build Coastguard Worker if (__dn > 0) 987*58b9f456SAndroid Build Coastguard Worker { 988*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __dn); 989*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn))) 990*58b9f456SAndroid Build Coastguard Worker return false; 991*58b9f456SAndroid Build Coastguard Worker __first2.__ctz_ = static_cast<unsigned>(__dn); 992*58b9f456SAndroid Build Coastguard Worker } 993*58b9f456SAndroid Build Coastguard Worker ++__first1.__seg_; 994*58b9f456SAndroid Build Coastguard Worker // __first1.__ctz_ = 0; 995*58b9f456SAndroid Build Coastguard Worker } 996*58b9f456SAndroid Build Coastguard Worker // __first1.__ctz_ == 0; 997*58b9f456SAndroid Build Coastguard Worker // do middle words 998*58b9f456SAndroid Build Coastguard Worker unsigned __clz_r = __bits_per_word - __first2.__ctz_; 999*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) << __first2.__ctz_; 1000*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_) 1001*58b9f456SAndroid Build Coastguard Worker { 1002*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first1.__seg_; 1003*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_)) 1004*58b9f456SAndroid Build Coastguard Worker return false; 1005*58b9f456SAndroid Build Coastguard Worker ++__first2.__seg_; 1006*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r)) 1007*58b9f456SAndroid Build Coastguard Worker return false; 1008*58b9f456SAndroid Build Coastguard Worker } 1009*58b9f456SAndroid Build Coastguard Worker // do last word 1010*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 1011*58b9f456SAndroid Build Coastguard Worker { 1012*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __n); 1013*58b9f456SAndroid Build Coastguard Worker __storage_type __b = *__first1.__seg_ & __m; 1014*58b9f456SAndroid Build Coastguard Worker __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r)); 1015*58b9f456SAndroid Build Coastguard Worker __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); 1016*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_)) 1017*58b9f456SAndroid Build Coastguard Worker return false; 1018*58b9f456SAndroid Build Coastguard Worker __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word; 1019*58b9f456SAndroid Build Coastguard Worker __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_) % __bits_per_word); 1020*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 1021*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 1022*58b9f456SAndroid Build Coastguard Worker { 1023*58b9f456SAndroid Build Coastguard Worker __m = ~__storage_type(0) >> (__bits_per_word - __n); 1024*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (__b >> __dn)) 1025*58b9f456SAndroid Build Coastguard Worker return false; 1026*58b9f456SAndroid Build Coastguard Worker } 1027*58b9f456SAndroid Build Coastguard Worker } 1028*58b9f456SAndroid Build Coastguard Worker } 1029*58b9f456SAndroid Build Coastguard Worker return true; 1030*58b9f456SAndroid Build Coastguard Worker} 1031*58b9f456SAndroid Build Coastguard Worker 1032*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IC1, bool _IC2> 1033*58b9f456SAndroid Build Coastguard Workerbool 1034*58b9f456SAndroid Build Coastguard Worker__equal_aligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, 1035*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Cp, _IC2> __first2) 1036*58b9f456SAndroid Build Coastguard Worker{ 1037*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator<_Cp, _IC1> _It; 1038*58b9f456SAndroid Build Coastguard Worker typedef typename _It::difference_type difference_type; 1039*58b9f456SAndroid Build Coastguard Worker typedef typename _It::__storage_type __storage_type; 1040*58b9f456SAndroid Build Coastguard Worker static const int __bits_per_word = _It::__bits_per_word; 1041*58b9f456SAndroid Build Coastguard Worker difference_type __n = __last1 - __first1; 1042*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 1043*58b9f456SAndroid Build Coastguard Worker { 1044*58b9f456SAndroid Build Coastguard Worker // do first word 1045*58b9f456SAndroid Build Coastguard Worker if (__first1.__ctz_ != 0) 1046*58b9f456SAndroid Build Coastguard Worker { 1047*58b9f456SAndroid Build Coastguard Worker unsigned __clz = __bits_per_word - __first1.__ctz_; 1048*58b9f456SAndroid Build Coastguard Worker difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); 1049*58b9f456SAndroid Build Coastguard Worker __n -= __dn; 1050*58b9f456SAndroid Build Coastguard Worker __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); 1051*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m)) 1052*58b9f456SAndroid Build Coastguard Worker return false; 1053*58b9f456SAndroid Build Coastguard Worker ++__first2.__seg_; 1054*58b9f456SAndroid Build Coastguard Worker ++__first1.__seg_; 1055*58b9f456SAndroid Build Coastguard Worker // __first1.__ctz_ = 0; 1056*58b9f456SAndroid Build Coastguard Worker // __first2.__ctz_ = 0; 1057*58b9f456SAndroid Build Coastguard Worker } 1058*58b9f456SAndroid Build Coastguard Worker // __first1.__ctz_ == 0; 1059*58b9f456SAndroid Build Coastguard Worker // __first2.__ctz_ == 0; 1060*58b9f456SAndroid Build Coastguard Worker // do middle words 1061*58b9f456SAndroid Build Coastguard Worker for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_) 1062*58b9f456SAndroid Build Coastguard Worker if (*__first2.__seg_ != *__first1.__seg_) 1063*58b9f456SAndroid Build Coastguard Worker return false; 1064*58b9f456SAndroid Build Coastguard Worker // do last word 1065*58b9f456SAndroid Build Coastguard Worker if (__n > 0) 1066*58b9f456SAndroid Build Coastguard Worker { 1067*58b9f456SAndroid Build Coastguard Worker __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 1068*58b9f456SAndroid Build Coastguard Worker if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m)) 1069*58b9f456SAndroid Build Coastguard Worker return false; 1070*58b9f456SAndroid Build Coastguard Worker } 1071*58b9f456SAndroid Build Coastguard Worker } 1072*58b9f456SAndroid Build Coastguard Worker return true; 1073*58b9f456SAndroid Build Coastguard Worker} 1074*58b9f456SAndroid Build Coastguard Worker 1075*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IC1, bool _IC2> 1076*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY 1077*58b9f456SAndroid Build Coastguard Workerbool 1078*58b9f456SAndroid Build Coastguard Workerequal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __bit_iterator<_Cp, _IC2> __first2) 1079*58b9f456SAndroid Build Coastguard Worker{ 1080*58b9f456SAndroid Build Coastguard Worker if (__first1.__ctz_ == __first2.__ctz_) 1081*58b9f456SAndroid Build Coastguard Worker return __equal_aligned(__first1, __last1, __first2); 1082*58b9f456SAndroid Build Coastguard Worker return __equal_unaligned(__first1, __last1, __first2); 1083*58b9f456SAndroid Build Coastguard Worker} 1084*58b9f456SAndroid Build Coastguard Worker 1085*58b9f456SAndroid Build Coastguard Workertemplate <class _Cp, bool _IsConst, 1086*58b9f456SAndroid Build Coastguard Worker typename _Cp::__storage_type> 1087*58b9f456SAndroid Build Coastguard Workerclass __bit_iterator 1088*58b9f456SAndroid Build Coastguard Worker{ 1089*58b9f456SAndroid Build Coastguard Workerpublic: 1090*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::difference_type difference_type; 1091*58b9f456SAndroid Build Coastguard Worker typedef bool value_type; 1092*58b9f456SAndroid Build Coastguard Worker typedef __bit_iterator pointer; 1093*58b9f456SAndroid Build Coastguard Worker typedef typename conditional<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >::type reference; 1094*58b9f456SAndroid Build Coastguard Worker typedef random_access_iterator_tag iterator_category; 1095*58b9f456SAndroid Build Coastguard Worker 1096*58b9f456SAndroid Build Coastguard Workerprivate: 1097*58b9f456SAndroid Build Coastguard Worker typedef typename _Cp::__storage_type __storage_type; 1098*58b9f456SAndroid Build Coastguard Worker typedef typename conditional<_IsConst, typename _Cp::__const_storage_pointer, 1099*58b9f456SAndroid Build Coastguard Worker typename _Cp::__storage_pointer>::type __storage_pointer; 1100*58b9f456SAndroid Build Coastguard Worker static const unsigned __bits_per_word = _Cp::__bits_per_word; 1101*58b9f456SAndroid Build Coastguard Worker 1102*58b9f456SAndroid Build Coastguard Worker __storage_pointer __seg_; 1103*58b9f456SAndroid Build Coastguard Worker unsigned __ctz_; 1104*58b9f456SAndroid Build Coastguard Worker 1105*58b9f456SAndroid Build Coastguard Workerpublic: 1106*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT 1107*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 11 1108*58b9f456SAndroid Build Coastguard Worker : __seg_(nullptr), __ctz_(0) 1109*58b9f456SAndroid Build Coastguard Worker#endif 1110*58b9f456SAndroid Build Coastguard Worker {} 1111*58b9f456SAndroid Build Coastguard Worker 1112*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 1113*58b9f456SAndroid Build Coastguard Worker __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT 1114*58b9f456SAndroid Build Coastguard Worker : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {} 1115*58b9f456SAndroid Build Coastguard Worker 1116*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT 1117*58b9f456SAndroid Build Coastguard Worker {return reference(__seg_, __storage_type(1) << __ctz_);} 1118*58b9f456SAndroid Build Coastguard Worker 1119*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator++() 1120*58b9f456SAndroid Build Coastguard Worker { 1121*58b9f456SAndroid Build Coastguard Worker if (__ctz_ != __bits_per_word-1) 1122*58b9f456SAndroid Build Coastguard Worker ++__ctz_; 1123*58b9f456SAndroid Build Coastguard Worker else 1124*58b9f456SAndroid Build Coastguard Worker { 1125*58b9f456SAndroid Build Coastguard Worker __ctz_ = 0; 1126*58b9f456SAndroid Build Coastguard Worker ++__seg_; 1127*58b9f456SAndroid Build Coastguard Worker } 1128*58b9f456SAndroid Build Coastguard Worker return *this; 1129*58b9f456SAndroid Build Coastguard Worker } 1130*58b9f456SAndroid Build Coastguard Worker 1131*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator operator++(int) 1132*58b9f456SAndroid Build Coastguard Worker { 1133*58b9f456SAndroid Build Coastguard Worker __bit_iterator __tmp = *this; 1134*58b9f456SAndroid Build Coastguard Worker ++(*this); 1135*58b9f456SAndroid Build Coastguard Worker return __tmp; 1136*58b9f456SAndroid Build Coastguard Worker } 1137*58b9f456SAndroid Build Coastguard Worker 1138*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator--() 1139*58b9f456SAndroid Build Coastguard Worker { 1140*58b9f456SAndroid Build Coastguard Worker if (__ctz_ != 0) 1141*58b9f456SAndroid Build Coastguard Worker --__ctz_; 1142*58b9f456SAndroid Build Coastguard Worker else 1143*58b9f456SAndroid Build Coastguard Worker { 1144*58b9f456SAndroid Build Coastguard Worker __ctz_ = __bits_per_word - 1; 1145*58b9f456SAndroid Build Coastguard Worker --__seg_; 1146*58b9f456SAndroid Build Coastguard Worker } 1147*58b9f456SAndroid Build Coastguard Worker return *this; 1148*58b9f456SAndroid Build Coastguard Worker } 1149*58b9f456SAndroid Build Coastguard Worker 1150*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator operator--(int) 1151*58b9f456SAndroid Build Coastguard Worker { 1152*58b9f456SAndroid Build Coastguard Worker __bit_iterator __tmp = *this; 1153*58b9f456SAndroid Build Coastguard Worker --(*this); 1154*58b9f456SAndroid Build Coastguard Worker return __tmp; 1155*58b9f456SAndroid Build Coastguard Worker } 1156*58b9f456SAndroid Build Coastguard Worker 1157*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator+=(difference_type __n) 1158*58b9f456SAndroid Build Coastguard Worker { 1159*58b9f456SAndroid Build Coastguard Worker if (__n >= 0) 1160*58b9f456SAndroid Build Coastguard Worker __seg_ += (__n + __ctz_) / __bits_per_word; 1161*58b9f456SAndroid Build Coastguard Worker else 1162*58b9f456SAndroid Build Coastguard Worker __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1) 1163*58b9f456SAndroid Build Coastguard Worker / static_cast<difference_type>(__bits_per_word); 1164*58b9f456SAndroid Build Coastguard Worker __n &= (__bits_per_word - 1); 1165*58b9f456SAndroid Build Coastguard Worker __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word); 1166*58b9f456SAndroid Build Coastguard Worker return *this; 1167*58b9f456SAndroid Build Coastguard Worker } 1168*58b9f456SAndroid Build Coastguard Worker 1169*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator-=(difference_type __n) 1170*58b9f456SAndroid Build Coastguard Worker { 1171*58b9f456SAndroid Build Coastguard Worker return *this += -__n; 1172*58b9f456SAndroid Build Coastguard Worker } 1173*58b9f456SAndroid Build Coastguard Worker 1174*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator operator+(difference_type __n) const 1175*58b9f456SAndroid Build Coastguard Worker { 1176*58b9f456SAndroid Build Coastguard Worker __bit_iterator __t(*this); 1177*58b9f456SAndroid Build Coastguard Worker __t += __n; 1178*58b9f456SAndroid Build Coastguard Worker return __t; 1179*58b9f456SAndroid Build Coastguard Worker } 1180*58b9f456SAndroid Build Coastguard Worker 1181*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY __bit_iterator operator-(difference_type __n) const 1182*58b9f456SAndroid Build Coastguard Worker { 1183*58b9f456SAndroid Build Coastguard Worker __bit_iterator __t(*this); 1184*58b9f456SAndroid Build Coastguard Worker __t -= __n; 1185*58b9f456SAndroid Build Coastguard Worker return __t; 1186*58b9f456SAndroid Build Coastguard Worker } 1187*58b9f456SAndroid Build Coastguard Worker 1188*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 1189*58b9f456SAndroid Build Coastguard Worker friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;} 1190*58b9f456SAndroid Build Coastguard Worker 1191*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 1192*58b9f456SAndroid Build Coastguard Worker friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y) 1193*58b9f456SAndroid Build Coastguard Worker {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;} 1194*58b9f456SAndroid Build Coastguard Worker 1195*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const {return *(*this + __n);} 1196*58b9f456SAndroid Build Coastguard Worker 1197*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y) 1198*58b9f456SAndroid Build Coastguard Worker {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;} 1199*58b9f456SAndroid Build Coastguard Worker 1200*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y) 1201*58b9f456SAndroid Build Coastguard Worker {return !(__x == __y);} 1202*58b9f456SAndroid Build Coastguard Worker 1203*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y) 1204*58b9f456SAndroid Build Coastguard Worker {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);} 1205*58b9f456SAndroid Build Coastguard Worker 1206*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y) 1207*58b9f456SAndroid Build Coastguard Worker {return __y < __x;} 1208*58b9f456SAndroid Build Coastguard Worker 1209*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y) 1210*58b9f456SAndroid Build Coastguard Worker {return !(__y < __x);} 1211*58b9f456SAndroid Build Coastguard Worker 1212*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y) 1213*58b9f456SAndroid Build Coastguard Worker {return !(__x < __y);} 1214*58b9f456SAndroid Build Coastguard Worker 1215*58b9f456SAndroid Build Coastguard Workerprivate: 1216*58b9f456SAndroid Build Coastguard Worker _LIBCPP_INLINE_VISIBILITY 1217*58b9f456SAndroid Build Coastguard Worker __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT 1218*58b9f456SAndroid Build Coastguard Worker : __seg_(__s), __ctz_(__ctz) {} 1219*58b9f456SAndroid Build Coastguard Worker 1220*58b9f456SAndroid Build Coastguard Worker friend typename _Cp::__self; 1221*58b9f456SAndroid Build Coastguard Worker 1222*58b9f456SAndroid Build Coastguard Worker friend class __bit_reference<_Cp>; 1223*58b9f456SAndroid Build Coastguard Worker friend class __bit_const_reference<_Cp>; 1224*58b9f456SAndroid Build Coastguard Worker friend class __bit_iterator<_Cp, true>; 1225*58b9f456SAndroid Build Coastguard Worker template <class _Dp> friend struct __bit_array; 1226*58b9f456SAndroid Build Coastguard Worker template <class _Dp> friend void __fill_n_false(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); 1227*58b9f456SAndroid Build Coastguard Worker template <class _Dp> friend void __fill_n_true(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); 1228*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_aligned(__bit_iterator<_Dp, _IC> __first, 1229*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC> __last, 1230*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false> __result); 1231*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_unaligned(__bit_iterator<_Dp, _IC> __first, 1232*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC> __last, 1233*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false> __result); 1234*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy(__bit_iterator<_Dp, _IC> __first, 1235*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC> __last, 1236*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false> __result); 1237*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_aligned(__bit_iterator<_Dp, _IC> __first, 1238*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC> __last, 1239*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false> __result); 1240*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_unaligned(__bit_iterator<_Dp, _IC> __first, 1241*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC> __last, 1242*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false> __result); 1243*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy_backward(__bit_iterator<_Dp, _IC> __first, 1244*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC> __last, 1245*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false> __result); 1246*58b9f456SAndroid Build Coastguard Worker template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>, 1247*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C1, false>, 1248*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C2, false>); 1249*58b9f456SAndroid Build Coastguard Worker template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>, 1250*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C1, false>, 1251*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C2, false>); 1252*58b9f456SAndroid Build Coastguard Worker template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>, 1253*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C1, false>, 1254*58b9f456SAndroid Build Coastguard Worker __bit_iterator<__C2, false>); 1255*58b9f456SAndroid Build Coastguard Worker template <class _Dp> friend __bit_iterator<_Dp, false> rotate(__bit_iterator<_Dp, false>, 1256*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false>, 1257*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, false>); 1258*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC1, bool _IC2> friend bool __equal_aligned(__bit_iterator<_Dp, _IC1>, 1259*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC1>, 1260*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC2>); 1261*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC1, bool _IC2> friend bool __equal_unaligned(__bit_iterator<_Dp, _IC1>, 1262*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC1>, 1263*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC2>); 1264*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC1, bool _IC2> friend bool equal(__bit_iterator<_Dp, _IC1>, 1265*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC1>, 1266*58b9f456SAndroid Build Coastguard Worker __bit_iterator<_Dp, _IC2>); 1267*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, _IC> __find_bool_true(__bit_iterator<_Dp, _IC>, 1268*58b9f456SAndroid Build Coastguard Worker typename _Dp::size_type); 1269*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend __bit_iterator<_Dp, _IC> __find_bool_false(__bit_iterator<_Dp, _IC>, 1270*58b9f456SAndroid Build Coastguard Worker typename _Dp::size_type); 1271*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type 1272*58b9f456SAndroid Build Coastguard Worker __count_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); 1273*58b9f456SAndroid Build Coastguard Worker template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type 1274*58b9f456SAndroid Build Coastguard Worker __count_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); 1275*58b9f456SAndroid Build Coastguard Worker}; 1276*58b9f456SAndroid Build Coastguard Worker 1277*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD 1278*58b9f456SAndroid Build Coastguard Worker 1279*58b9f456SAndroid Build Coastguard Worker_LIBCPP_POP_MACROS 1280*58b9f456SAndroid Build Coastguard Worker 1281*58b9f456SAndroid Build Coastguard Worker#endif // _LIBCPP___BIT_REFERENCE 1282