17a984708SDavid Chisnall// -*- C++ -*-
27a984708SDavid Chisnall//===----------------------------------------------------------------------===//
37a984708SDavid Chisnall//
47a984708SDavid Chisnall//                     The LLVM Compiler Infrastructure
57a984708SDavid Chisnall//
67a984708SDavid Chisnall// This file is dual licensed under the MIT and the University of Illinois Open
77a984708SDavid Chisnall// Source Licenses. See LICENSE.TXT for details.
87a984708SDavid Chisnall//
97a984708SDavid Chisnall//===----------------------------------------------------------------------===//
107a984708SDavid Chisnall
117a984708SDavid Chisnall#ifndef _LIBCPP___BIT_REFERENCE
127a984708SDavid Chisnall#define _LIBCPP___BIT_REFERENCE
137a984708SDavid Chisnall
147a984708SDavid Chisnall#include <__config>
15*b5893f02SDimitry Andric#include <bit>
167a984708SDavid Chisnall#include <algorithm>
177a984708SDavid Chisnall
187a984708SDavid Chisnall#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
197a984708SDavid Chisnall#pragma GCC system_header
207a984708SDavid Chisnall#endif
217a984708SDavid Chisnall
22f9448bf3SDimitry Andric_LIBCPP_PUSH_MACROS
23f9448bf3SDimitry Andric#include <__undef_macros>
24f9448bf3SDimitry Andric
25f9448bf3SDimitry Andric
267a984708SDavid Chisnall_LIBCPP_BEGIN_NAMESPACE_STD
277a984708SDavid Chisnall
28936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst, typename _Cp::__storage_type = 0> class __bit_iterator;
2994e3ee44SDavid Chisnalltemplate <class _Cp> class __bit_const_reference;
307a984708SDavid Chisnall
317a984708SDavid Chisnalltemplate <class _Tp>
327a984708SDavid Chisnallstruct __has_storage_type
337a984708SDavid Chisnall{
347a984708SDavid Chisnall    static const bool value = false;
357a984708SDavid Chisnall};
367a984708SDavid Chisnall
3794e3ee44SDavid Chisnalltemplate <class _Cp, bool = __has_storage_type<_Cp>::value>
387a984708SDavid Chisnallclass __bit_reference
397a984708SDavid Chisnall{
4094e3ee44SDavid Chisnall    typedef typename _Cp::__storage_type    __storage_type;
4194e3ee44SDavid Chisnall    typedef typename _Cp::__storage_pointer __storage_pointer;
427a984708SDavid Chisnall
437a984708SDavid Chisnall    __storage_pointer __seg_;
447a984708SDavid Chisnall    __storage_type    __mask_;
457a984708SDavid Chisnall
4694e3ee44SDavid Chisnall    friend typename _Cp::__self;
4780779b37SDimitry Andric
4894e3ee44SDavid Chisnall    friend class __bit_const_reference<_Cp>;
4994e3ee44SDavid Chisnall    friend class __bit_iterator<_Cp, false>;
507a984708SDavid Chisnallpublic:
517a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
527a984708SDavid Chisnall        {return static_cast<bool>(*__seg_ & __mask_);}
537a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY bool operator ~() const _NOEXCEPT
547a984708SDavid Chisnall        {return !static_cast<bool>(*this);}
557a984708SDavid Chisnall
567a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
577a984708SDavid Chisnall    __bit_reference& operator=(bool __x) _NOEXCEPT
587a984708SDavid Chisnall    {
597a984708SDavid Chisnall        if (__x)
607a984708SDavid Chisnall            *__seg_ |= __mask_;
617a984708SDavid Chisnall        else
627a984708SDavid Chisnall            *__seg_ &= ~__mask_;
637a984708SDavid Chisnall        return *this;
647a984708SDavid Chisnall    }
657a984708SDavid Chisnall
667a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
677a984708SDavid Chisnall    __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT
687a984708SDavid Chisnall        {return operator=(static_cast<bool>(__x));}
697a984708SDavid Chisnall
707a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {*__seg_ ^= __mask_;}
7194e3ee44SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, false> operator&() const _NOEXCEPT
7294e3ee44SDavid Chisnall        {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(__ctz(__mask_)));}
737a984708SDavid Chisnallprivate:
747a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
757a984708SDavid Chisnall    __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
767a984708SDavid Chisnall        : __seg_(__s), __mask_(__m) {}
777a984708SDavid Chisnall};
787a984708SDavid Chisnall
7994e3ee44SDavid Chisnalltemplate <class _Cp>
8094e3ee44SDavid Chisnallclass __bit_reference<_Cp, false>
817a984708SDavid Chisnall{
827a984708SDavid Chisnall};
837a984708SDavid Chisnall
841bf9f7c1SDimitry Andrictemplate <class _Cp>
854f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
861bf9f7c1SDimitry Andricvoid
871bf9f7c1SDimitry Andricswap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
881bf9f7c1SDimitry Andric{
891bf9f7c1SDimitry Andric    bool __t = __x;
901bf9f7c1SDimitry Andric    __x = __y;
911bf9f7c1SDimitry Andric    __y = __t;
921bf9f7c1SDimitry Andric}
931bf9f7c1SDimitry Andric
9494e3ee44SDavid Chisnalltemplate <class _Cp, class _Dp>
954f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
967a984708SDavid Chisnallvoid
9794e3ee44SDavid Chisnallswap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
987a984708SDavid Chisnall{
997a984708SDavid Chisnall    bool __t = __x;
1007a984708SDavid Chisnall    __x = __y;
1017a984708SDavid Chisnall    __y = __t;
1027a984708SDavid Chisnall}
1037a984708SDavid Chisnall
10494e3ee44SDavid Chisnalltemplate <class _Cp>
1054f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1067a984708SDavid Chisnallvoid
10794e3ee44SDavid Chisnallswap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT
1087a984708SDavid Chisnall{
1097a984708SDavid Chisnall    bool __t = __x;
1107a984708SDavid Chisnall    __x = __y;
1117a984708SDavid Chisnall    __y = __t;
1127a984708SDavid Chisnall}
1137a984708SDavid Chisnall
11494e3ee44SDavid Chisnalltemplate <class _Cp>
1154f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1167a984708SDavid Chisnallvoid
11794e3ee44SDavid Chisnallswap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT
1187a984708SDavid Chisnall{
1197a984708SDavid Chisnall    bool __t = __x;
1207a984708SDavid Chisnall    __x = __y;
1217a984708SDavid Chisnall    __y = __t;
1227a984708SDavid Chisnall}
1237a984708SDavid Chisnall
12494e3ee44SDavid Chisnalltemplate <class _Cp>
1257a984708SDavid Chisnallclass __bit_const_reference
1267a984708SDavid Chisnall{
12794e3ee44SDavid Chisnall    typedef typename _Cp::__storage_type          __storage_type;
12894e3ee44SDavid Chisnall    typedef typename _Cp::__const_storage_pointer __storage_pointer;
1297a984708SDavid Chisnall
1307a984708SDavid Chisnall    __storage_pointer        __seg_;
1317a984708SDavid Chisnall    __storage_type __mask_;
1327a984708SDavid Chisnall
13394e3ee44SDavid Chisnall    friend typename _Cp::__self;
13494e3ee44SDavid Chisnall    friend class __bit_iterator<_Cp, true>;
1357a984708SDavid Chisnallpublic:
1367a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
13794e3ee44SDavid Chisnall    __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT
1387a984708SDavid Chisnall        : __seg_(__x.__seg_), __mask_(__x.__mask_) {}
1397a984708SDavid Chisnall
140936e9439SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT
1417a984708SDavid Chisnall        {return static_cast<bool>(*__seg_ & __mask_);}
1427a984708SDavid Chisnall
14394e3ee44SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, true> operator&() const _NOEXCEPT
14494e3ee44SDavid Chisnall        {return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(__ctz(__mask_)));}
1457a984708SDavid Chisnallprivate:
1467a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
147936e9439SDimitry Andric    _LIBCPP_CONSTEXPR
1487a984708SDavid Chisnall    __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
1497a984708SDavid Chisnall        : __seg_(__s), __mask_(__m) {}
1507a984708SDavid Chisnall
1517a984708SDavid Chisnall    __bit_const_reference& operator=(const __bit_const_reference& __x);
1527a984708SDavid Chisnall};
1537a984708SDavid Chisnall
1547a984708SDavid Chisnall// find
1557a984708SDavid Chisnall
156936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst>
157936e9439SDimitry Andric__bit_iterator<_Cp, _IsConst>
158936e9439SDimitry Andric__find_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
1597a984708SDavid Chisnall{
160936e9439SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
1617a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
162aed8d94eSDimitry Andric    static const int __bits_per_word = _It::__bits_per_word;
1637a984708SDavid Chisnall    // do first partial word
1647a984708SDavid Chisnall    if (__first.__ctz_ != 0)
1657a984708SDavid Chisnall    {
1667a984708SDavid Chisnall        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
1677a984708SDavid Chisnall        __storage_type __dn = _VSTD::min(__clz_f, __n);
1687a984708SDavid Chisnall        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
1697a984708SDavid Chisnall        __storage_type __b = *__first.__seg_ & __m;
1707a984708SDavid Chisnall        if (__b)
1717a984708SDavid Chisnall            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
1724f7ab58eSDimitry Andric        if (__n == __dn)
173d72607e9SDimitry Andric            return __first + __n;
1747a984708SDavid Chisnall        __n -= __dn;
1757a984708SDavid Chisnall        ++__first.__seg_;
1767a984708SDavid Chisnall    }
1777a984708SDavid Chisnall    // do middle whole words
1787a984708SDavid Chisnall    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
1797a984708SDavid Chisnall        if (*__first.__seg_)
1807a984708SDavid Chisnall            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(*__first.__seg_)));
1817a984708SDavid Chisnall    // do last partial word
1827a984708SDavid Chisnall    if (__n > 0)
1837a984708SDavid Chisnall    {
1847a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
1857a984708SDavid Chisnall        __storage_type __b = *__first.__seg_ & __m;
1867a984708SDavid Chisnall        if (__b)
1877a984708SDavid Chisnall            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
1887a984708SDavid Chisnall    }
1897a984708SDavid Chisnall    return _It(__first.__seg_, static_cast<unsigned>(__n));
1907a984708SDavid Chisnall}
1917a984708SDavid Chisnall
192936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst>
193936e9439SDimitry Andric__bit_iterator<_Cp, _IsConst>
194936e9439SDimitry Andric__find_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
1957a984708SDavid Chisnall{
196936e9439SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
1977a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
198aed8d94eSDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
1997a984708SDavid Chisnall    // do first partial word
2007a984708SDavid Chisnall    if (__first.__ctz_ != 0)
2017a984708SDavid Chisnall    {
2027a984708SDavid Chisnall        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
2037a984708SDavid Chisnall        __storage_type __dn = _VSTD::min(__clz_f, __n);
2047a984708SDavid Chisnall        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
205936e9439SDimitry Andric        __storage_type __b = ~*__first.__seg_ & __m;
2067a984708SDavid Chisnall        if (__b)
2077a984708SDavid Chisnall            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
2084f7ab58eSDimitry Andric        if (__n == __dn)
209d72607e9SDimitry Andric            return __first + __n;
2107a984708SDavid Chisnall        __n -= __dn;
2117a984708SDavid Chisnall        ++__first.__seg_;
2127a984708SDavid Chisnall    }
2137a984708SDavid Chisnall    // do middle whole words
2147a984708SDavid Chisnall    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
2157a984708SDavid Chisnall    {
2167a984708SDavid Chisnall        __storage_type __b = ~*__first.__seg_;
2177a984708SDavid Chisnall        if (__b)
2187a984708SDavid Chisnall            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
2197a984708SDavid Chisnall    }
2207a984708SDavid Chisnall    // do last partial word
2217a984708SDavid Chisnall    if (__n > 0)
2227a984708SDavid Chisnall    {
2237a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
224936e9439SDimitry Andric        __storage_type __b = ~*__first.__seg_ & __m;
2257a984708SDavid Chisnall        if (__b)
2267a984708SDavid Chisnall            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
2277a984708SDavid Chisnall    }
2287a984708SDavid Chisnall    return _It(__first.__seg_, static_cast<unsigned>(__n));
2297a984708SDavid Chisnall}
2307a984708SDavid Chisnall
231936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst, class _Tp>
2327a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
233936e9439SDimitry Andric__bit_iterator<_Cp, _IsConst>
234936e9439SDimitry Andricfind(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value_)
2357a984708SDavid Chisnall{
2367a984708SDavid Chisnall    if (static_cast<bool>(__value_))
23794e3ee44SDavid Chisnall        return __find_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
23894e3ee44SDavid Chisnall    return __find_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
2397a984708SDavid Chisnall}
2407a984708SDavid Chisnall
2417a984708SDavid Chisnall// count
2427a984708SDavid Chisnall
243936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst>
244936e9439SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type
245936e9439SDimitry Andric__count_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
2467a984708SDavid Chisnall{
247936e9439SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
2487a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
2497a984708SDavid Chisnall    typedef typename _It::difference_type difference_type;
250aed8d94eSDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
2517a984708SDavid Chisnall    difference_type __r = 0;
2527a984708SDavid Chisnall    // do first partial word
2537a984708SDavid Chisnall    if (__first.__ctz_ != 0)
2547a984708SDavid Chisnall    {
2557a984708SDavid Chisnall        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
2567a984708SDavid Chisnall        __storage_type __dn = _VSTD::min(__clz_f, __n);
2577a984708SDavid Chisnall        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
258*b5893f02SDimitry Andric        __r = _VSTD::__popcount(*__first.__seg_ & __m);
2597a984708SDavid Chisnall        __n -= __dn;
2607a984708SDavid Chisnall        ++__first.__seg_;
2617a984708SDavid Chisnall    }
2627a984708SDavid Chisnall    // do middle whole words
2637a984708SDavid Chisnall    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
264*b5893f02SDimitry Andric        __r += _VSTD::__popcount(*__first.__seg_);
2657a984708SDavid Chisnall    // do last partial word
2667a984708SDavid Chisnall    if (__n > 0)
2677a984708SDavid Chisnall    {
2687a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
269*b5893f02SDimitry Andric        __r += _VSTD::__popcount(*__first.__seg_ & __m);
2707a984708SDavid Chisnall    }
2717a984708SDavid Chisnall    return __r;
2727a984708SDavid Chisnall}
2737a984708SDavid Chisnall
274936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst>
275936e9439SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type
276936e9439SDimitry Andric__count_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
2777a984708SDavid Chisnall{
278936e9439SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
2797a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
2807a984708SDavid Chisnall    typedef typename _It::difference_type difference_type;
281aed8d94eSDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
2827a984708SDavid Chisnall    difference_type __r = 0;
2837a984708SDavid Chisnall    // do first partial word
2847a984708SDavid Chisnall    if (__first.__ctz_ != 0)
2857a984708SDavid Chisnall    {
2867a984708SDavid Chisnall        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
2877a984708SDavid Chisnall        __storage_type __dn = _VSTD::min(__clz_f, __n);
2887a984708SDavid Chisnall        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
289*b5893f02SDimitry Andric        __r = _VSTD::__popcount(~*__first.__seg_ & __m);
2907a984708SDavid Chisnall        __n -= __dn;
2917a984708SDavid Chisnall        ++__first.__seg_;
2927a984708SDavid Chisnall    }
2937a984708SDavid Chisnall    // do middle whole words
2947a984708SDavid Chisnall    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
295*b5893f02SDimitry Andric        __r += _VSTD::__popcount(~*__first.__seg_);
2967a984708SDavid Chisnall    // do last partial word
2977a984708SDavid Chisnall    if (__n > 0)
2987a984708SDavid Chisnall    {
2997a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
300*b5893f02SDimitry Andric        __r += _VSTD::__popcount(~*__first.__seg_ & __m);
3017a984708SDavid Chisnall    }
3027a984708SDavid Chisnall    return __r;
3037a984708SDavid Chisnall}
3047a984708SDavid Chisnall
305936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst, class _Tp>
3067a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
307936e9439SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type
308936e9439SDimitry Andriccount(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value_)
3097a984708SDavid Chisnall{
3107a984708SDavid Chisnall    if (static_cast<bool>(__value_))
31194e3ee44SDavid Chisnall        return __count_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
31294e3ee44SDavid Chisnall    return __count_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
3137a984708SDavid Chisnall}
3147a984708SDavid Chisnall
3157a984708SDavid Chisnall// fill_n
3167a984708SDavid Chisnall
31794e3ee44SDavid Chisnalltemplate <class _Cp>
3187a984708SDavid Chisnallvoid
31994e3ee44SDavid Chisnall__fill_n_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
3207a984708SDavid Chisnall{
32194e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, false> _It;
3227a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
323aed8d94eSDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
3247a984708SDavid Chisnall    // do first partial word
3257a984708SDavid Chisnall    if (__first.__ctz_ != 0)
3267a984708SDavid Chisnall    {
3277a984708SDavid Chisnall        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
3287a984708SDavid Chisnall        __storage_type __dn = _VSTD::min(__clz_f, __n);
3297a984708SDavid Chisnall        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
3307a984708SDavid Chisnall        *__first.__seg_ &= ~__m;
3317a984708SDavid Chisnall        __n -= __dn;
3327a984708SDavid Chisnall        ++__first.__seg_;
3337a984708SDavid Chisnall    }
3347a984708SDavid Chisnall    // do middle whole words
3357a984708SDavid Chisnall    __storage_type __nw = __n / __bits_per_word;
3364bab9fd9SDavid Chisnall    _VSTD::memset(_VSTD::__to_raw_pointer(__first.__seg_), 0, __nw * sizeof(__storage_type));
3377a984708SDavid Chisnall    __n -= __nw * __bits_per_word;
3387a984708SDavid Chisnall    // do last partial word
3397a984708SDavid Chisnall    if (__n > 0)
3407a984708SDavid Chisnall    {
3417a984708SDavid Chisnall        __first.__seg_ += __nw;
3427a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3437a984708SDavid Chisnall        *__first.__seg_ &= ~__m;
3447a984708SDavid Chisnall    }
3457a984708SDavid Chisnall}
3467a984708SDavid Chisnall
34794e3ee44SDavid Chisnalltemplate <class _Cp>
3487a984708SDavid Chisnallvoid
34994e3ee44SDavid Chisnall__fill_n_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
3507a984708SDavid Chisnall{
35194e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, false> _It;
3527a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
353aed8d94eSDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
3547a984708SDavid Chisnall    // do first partial word
3557a984708SDavid Chisnall    if (__first.__ctz_ != 0)
3567a984708SDavid Chisnall    {
3577a984708SDavid Chisnall        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
3587a984708SDavid Chisnall        __storage_type __dn = _VSTD::min(__clz_f, __n);
3597a984708SDavid Chisnall        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
3607a984708SDavid Chisnall        *__first.__seg_ |= __m;
3617a984708SDavid Chisnall        __n -= __dn;
3627a984708SDavid Chisnall        ++__first.__seg_;
3637a984708SDavid Chisnall    }
3647a984708SDavid Chisnall    // do middle whole words
3657a984708SDavid Chisnall    __storage_type __nw = __n / __bits_per_word;
3664bab9fd9SDavid Chisnall    _VSTD::memset(_VSTD::__to_raw_pointer(__first.__seg_), -1, __nw * sizeof(__storage_type));
3677a984708SDavid Chisnall    __n -= __nw * __bits_per_word;
3687a984708SDavid Chisnall    // do last partial word
3697a984708SDavid Chisnall    if (__n > 0)
3707a984708SDavid Chisnall    {
3717a984708SDavid Chisnall        __first.__seg_ += __nw;
3727a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3737a984708SDavid Chisnall        *__first.__seg_ |= __m;
3747a984708SDavid Chisnall    }
3757a984708SDavid Chisnall}
3767a984708SDavid Chisnall
37794e3ee44SDavid Chisnalltemplate <class _Cp>
3784f7ab58eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3797a984708SDavid Chisnallvoid
38094e3ee44SDavid Chisnallfill_n(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __value_)
3817a984708SDavid Chisnall{
3827a984708SDavid Chisnall    if (__n > 0)
3837a984708SDavid Chisnall    {
3847a984708SDavid Chisnall        if (__value_)
3857a984708SDavid Chisnall            __fill_n_true(__first, __n);
3867a984708SDavid Chisnall        else
3877a984708SDavid Chisnall            __fill_n_false(__first, __n);
3887a984708SDavid Chisnall    }
3897a984708SDavid Chisnall}
3907a984708SDavid Chisnall
3917a984708SDavid Chisnall// fill
3927a984708SDavid Chisnall
39394e3ee44SDavid Chisnalltemplate <class _Cp>
3947a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
3957a984708SDavid Chisnallvoid
39694e3ee44SDavid Chisnallfill(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, bool __value_)
3977a984708SDavid Chisnall{
39894e3ee44SDavid Chisnall    _VSTD::fill_n(__first, static_cast<typename _Cp::size_type>(__last - __first), __value_);
3997a984708SDavid Chisnall}
4007a984708SDavid Chisnall
4017a984708SDavid Chisnall// copy
4027a984708SDavid Chisnall
40394e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
40494e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
40594e3ee44SDavid Chisnall__copy_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
40694e3ee44SDavid Chisnall                                                     __bit_iterator<_Cp, false> __result)
4077a984708SDavid Chisnall{
40894e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, _IsConst> _In;
4097a984708SDavid Chisnall    typedef  typename _In::difference_type difference_type;
4107a984708SDavid Chisnall    typedef typename _In::__storage_type __storage_type;
411aed8d94eSDimitry Andric    const int __bits_per_word = _In::__bits_per_word;
4127a984708SDavid Chisnall    difference_type __n = __last - __first;
4137a984708SDavid Chisnall    if (__n > 0)
4147a984708SDavid Chisnall    {
4157a984708SDavid Chisnall        // do first word
4167a984708SDavid Chisnall        if (__first.__ctz_ != 0)
4177a984708SDavid Chisnall        {
4187a984708SDavid Chisnall            unsigned __clz = __bits_per_word - __first.__ctz_;
4197a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
4207a984708SDavid Chisnall            __n -= __dn;
4217a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
4227a984708SDavid Chisnall            __storage_type __b = *__first.__seg_ & __m;
4237a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
4247a984708SDavid Chisnall            *__result.__seg_ |= __b;
4257a984708SDavid Chisnall            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
4267a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
4277a984708SDavid Chisnall            ++__first.__seg_;
4287a984708SDavid Chisnall            // __first.__ctz_ = 0;
4297a984708SDavid Chisnall        }
4307a984708SDavid Chisnall        // __first.__ctz_ == 0;
4317a984708SDavid Chisnall        // do middle words
4327a984708SDavid Chisnall        __storage_type __nw = __n / __bits_per_word;
4334bab9fd9SDavid Chisnall        _VSTD::memmove(_VSTD::__to_raw_pointer(__result.__seg_),
4344bab9fd9SDavid Chisnall                       _VSTD::__to_raw_pointer(__first.__seg_),
4354bab9fd9SDavid Chisnall                       __nw * sizeof(__storage_type));
4367a984708SDavid Chisnall        __n -= __nw * __bits_per_word;
4377a984708SDavid Chisnall        __result.__seg_ += __nw;
4387a984708SDavid Chisnall        // do last word
4397a984708SDavid Chisnall        if (__n > 0)
4407a984708SDavid Chisnall        {
4417a984708SDavid Chisnall            __first.__seg_ += __nw;
4427a984708SDavid Chisnall            __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
4437a984708SDavid Chisnall            __storage_type __b = *__first.__seg_ & __m;
4447a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
4457a984708SDavid Chisnall            *__result.__seg_ |= __b;
4467a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>(__n);
4477a984708SDavid Chisnall        }
4487a984708SDavid Chisnall    }
4497a984708SDavid Chisnall    return __result;
4507a984708SDavid Chisnall}
4517a984708SDavid Chisnall
45294e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
45394e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
45494e3ee44SDavid Chisnall__copy_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
45594e3ee44SDavid Chisnall                                                       __bit_iterator<_Cp, false> __result)
4567a984708SDavid Chisnall{
45794e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, _IsConst> _In;
4587a984708SDavid Chisnall    typedef  typename _In::difference_type difference_type;
4597a984708SDavid Chisnall    typedef typename _In::__storage_type __storage_type;
460aed8d94eSDimitry Andric    static const int __bits_per_word = _In::__bits_per_word;
4617a984708SDavid Chisnall    difference_type __n = __last - __first;
4627a984708SDavid Chisnall    if (__n > 0)
4637a984708SDavid Chisnall    {
4647a984708SDavid Chisnall        // do first word
4657a984708SDavid Chisnall        if (__first.__ctz_ != 0)
4667a984708SDavid Chisnall        {
4677a984708SDavid Chisnall            unsigned __clz_f = __bits_per_word - __first.__ctz_;
4687a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
4697a984708SDavid Chisnall            __n -= __dn;
4707a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
4717a984708SDavid Chisnall            __storage_type __b = *__first.__seg_ & __m;
4727a984708SDavid Chisnall            unsigned __clz_r = __bits_per_word - __result.__ctz_;
4737a984708SDavid Chisnall            __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
4747a984708SDavid Chisnall            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
4757a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
4767a984708SDavid Chisnall            if (__result.__ctz_ > __first.__ctz_)
4777a984708SDavid Chisnall                *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_);
4787a984708SDavid Chisnall            else
4797a984708SDavid Chisnall                *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_);
4807a984708SDavid Chisnall            __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
4817a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_)  % __bits_per_word);
4827a984708SDavid Chisnall            __dn -= __ddn;
4837a984708SDavid Chisnall            if (__dn > 0)
4847a984708SDavid Chisnall            {
4857a984708SDavid Chisnall                __m = ~__storage_type(0) >> (__bits_per_word - __dn);
4867a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
4877a984708SDavid Chisnall                *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn);
4887a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(__dn);
4897a984708SDavid Chisnall            }
4907a984708SDavid Chisnall            ++__first.__seg_;
4917a984708SDavid Chisnall            // __first.__ctz_ = 0;
4927a984708SDavid Chisnall        }
4937a984708SDavid Chisnall        // __first.__ctz_ == 0;
4947a984708SDavid Chisnall        // do middle words
4957a984708SDavid Chisnall        unsigned __clz_r = __bits_per_word - __result.__ctz_;
4967a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) << __result.__ctz_;
4977a984708SDavid Chisnall        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
4987a984708SDavid Chisnall        {
4997a984708SDavid Chisnall            __storage_type __b = *__first.__seg_;
5007a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
5017a984708SDavid Chisnall            *__result.__seg_ |= __b << __result.__ctz_;
5027a984708SDavid Chisnall            ++__result.__seg_;
5037a984708SDavid Chisnall            *__result.__seg_ &= __m;
5047a984708SDavid Chisnall            *__result.__seg_ |= __b >> __clz_r;
5057a984708SDavid Chisnall        }
5067a984708SDavid Chisnall        // do last word
5077a984708SDavid Chisnall        if (__n > 0)
5087a984708SDavid Chisnall        {
5097a984708SDavid Chisnall            __m = ~__storage_type(0) >> (__bits_per_word - __n);
5107a984708SDavid Chisnall            __storage_type __b = *__first.__seg_ & __m;
5117a984708SDavid Chisnall            __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
5127a984708SDavid Chisnall            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
5137a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
5147a984708SDavid Chisnall            *__result.__seg_ |= __b << __result.__ctz_;
5157a984708SDavid Chisnall            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
5167a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
5177a984708SDavid Chisnall            __n -= __dn;
5187a984708SDavid Chisnall            if (__n > 0)
5197a984708SDavid Chisnall            {
5207a984708SDavid Chisnall                __m = ~__storage_type(0) >> (__bits_per_word - __n);
5217a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
5227a984708SDavid Chisnall                *__result.__seg_ |= __b >> __dn;
5237a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(__n);
5247a984708SDavid Chisnall            }
5257a984708SDavid Chisnall        }
5267a984708SDavid Chisnall    }
5277a984708SDavid Chisnall    return __result;
5287a984708SDavid Chisnall}
5297a984708SDavid Chisnall
53094e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
5317a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
53294e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
53394e3ee44SDavid Chisnallcopy(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
5347a984708SDavid Chisnall{
5357a984708SDavid Chisnall    if (__first.__ctz_ == __result.__ctz_)
5367a984708SDavid Chisnall        return __copy_aligned(__first, __last, __result);
5377a984708SDavid Chisnall    return __copy_unaligned(__first, __last, __result);
5387a984708SDavid Chisnall}
5397a984708SDavid Chisnall
5407a984708SDavid Chisnall// copy_backward
5417a984708SDavid Chisnall
54294e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
54394e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
54494e3ee44SDavid Chisnall__copy_backward_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
54594e3ee44SDavid Chisnall                                                     __bit_iterator<_Cp, false> __result)
5467a984708SDavid Chisnall{
54794e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, _IsConst> _In;
5487a984708SDavid Chisnall    typedef  typename _In::difference_type difference_type;
5497a984708SDavid Chisnall    typedef typename _In::__storage_type __storage_type;
550aed8d94eSDimitry Andric    const int __bits_per_word = _In::__bits_per_word;
5517a984708SDavid Chisnall    difference_type __n = __last - __first;
5527a984708SDavid Chisnall    if (__n > 0)
5537a984708SDavid Chisnall    {
5547a984708SDavid Chisnall        // do first word
5557a984708SDavid Chisnall        if (__last.__ctz_ != 0)
5567a984708SDavid Chisnall        {
5577a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
5587a984708SDavid Chisnall            __n -= __dn;
5597a984708SDavid Chisnall            unsigned __clz = __bits_per_word - __last.__ctz_;
5607a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz);
5617a984708SDavid Chisnall            __storage_type __b = *__last.__seg_ & __m;
5627a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
5637a984708SDavid Chisnall            *__result.__seg_ |= __b;
5647a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
5657a984708SDavid Chisnall                                                       __result.__ctz_)  % __bits_per_word);
5667a984708SDavid Chisnall            // __last.__ctz_ = 0
5677a984708SDavid Chisnall         }
5687a984708SDavid Chisnall        // __last.__ctz_ == 0 || __n == 0
5697a984708SDavid Chisnall        // __result.__ctz_ == 0 || __n == 0
5707a984708SDavid Chisnall        // do middle words
5717a984708SDavid Chisnall        __storage_type __nw = __n / __bits_per_word;
5727a984708SDavid Chisnall        __result.__seg_ -= __nw;
5737a984708SDavid Chisnall        __last.__seg_ -= __nw;
5744bab9fd9SDavid Chisnall        _VSTD::memmove(_VSTD::__to_raw_pointer(__result.__seg_),
5754bab9fd9SDavid Chisnall                       _VSTD::__to_raw_pointer(__last.__seg_),
5764bab9fd9SDavid Chisnall                       __nw * sizeof(__storage_type));
5777a984708SDavid Chisnall        __n -= __nw * __bits_per_word;
5787a984708SDavid Chisnall        // do last word
5797a984708SDavid Chisnall        if (__n > 0)
5807a984708SDavid Chisnall        {
5817a984708SDavid Chisnall            __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n);
5827a984708SDavid Chisnall            __storage_type __b = *--__last.__seg_ & __m;
5837a984708SDavid Chisnall            *--__result.__seg_ &= ~__m;
5847a984708SDavid Chisnall            *__result.__seg_ |= __b;
5857a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
5867a984708SDavid Chisnall        }
5877a984708SDavid Chisnall    }
5887a984708SDavid Chisnall    return __result;
5897a984708SDavid Chisnall}
5907a984708SDavid Chisnall
59194e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
59294e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
59394e3ee44SDavid Chisnall__copy_backward_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
59494e3ee44SDavid Chisnall                                                       __bit_iterator<_Cp, false> __result)
5957a984708SDavid Chisnall{
59694e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, _IsConst> _In;
5977a984708SDavid Chisnall    typedef  typename _In::difference_type difference_type;
5987a984708SDavid Chisnall    typedef typename _In::__storage_type __storage_type;
599aed8d94eSDimitry Andric    const int __bits_per_word = _In::__bits_per_word;
6007a984708SDavid Chisnall    difference_type __n = __last - __first;
6017a984708SDavid Chisnall    if (__n > 0)
6027a984708SDavid Chisnall    {
6037a984708SDavid Chisnall        // do first word
6047a984708SDavid Chisnall        if (__last.__ctz_ != 0)
6057a984708SDavid Chisnall        {
6067a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
6077a984708SDavid Chisnall            __n -= __dn;
6087a984708SDavid Chisnall            unsigned __clz_l = __bits_per_word - __last.__ctz_;
6097a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l);
6107a984708SDavid Chisnall            __storage_type __b = *__last.__seg_ & __m;
6117a984708SDavid Chisnall            unsigned __clz_r = __bits_per_word - __result.__ctz_;
6127a984708SDavid Chisnall            __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_));
6137a984708SDavid Chisnall            if (__ddn > 0)
6147a984708SDavid Chisnall            {
6157a984708SDavid Chisnall                __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r);
6167a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
6177a984708SDavid Chisnall                if (__result.__ctz_ > __last.__ctz_)
6187a984708SDavid Chisnall                    *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
6197a984708SDavid Chisnall                else
6207a984708SDavid Chisnall                    *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_);
6217a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) +
6227a984708SDavid Chisnall                                                         __result.__ctz_)  % __bits_per_word);
6237a984708SDavid Chisnall                __dn -= __ddn;
6247a984708SDavid Chisnall            }
6257a984708SDavid Chisnall            if (__dn > 0)
6267a984708SDavid Chisnall            {
6277a984708SDavid Chisnall                // __result.__ctz_ == 0
6287a984708SDavid Chisnall                --__result.__seg_;
6297a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1));
6307a984708SDavid Chisnall                __m = ~__storage_type(0) << __result.__ctz_;
6317a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
6327a984708SDavid Chisnall                __last.__ctz_ -= __dn + __ddn;
6337a984708SDavid Chisnall                *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
6347a984708SDavid Chisnall            }
6357a984708SDavid Chisnall            // __last.__ctz_ = 0
6367a984708SDavid Chisnall         }
6377a984708SDavid Chisnall        // __last.__ctz_ == 0 || __n == 0
6387a984708SDavid Chisnall        // __result.__ctz_ != 0 || __n == 0
6397a984708SDavid Chisnall        // do middle words
6407a984708SDavid Chisnall        unsigned __clz_r = __bits_per_word - __result.__ctz_;
6417a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) >> __clz_r;
6427a984708SDavid Chisnall        for (; __n >= __bits_per_word; __n -= __bits_per_word)
6437a984708SDavid Chisnall        {
6447a984708SDavid Chisnall            __storage_type __b = *--__last.__seg_;
6457a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
6467a984708SDavid Chisnall            *__result.__seg_ |= __b >> __clz_r;
6477a984708SDavid Chisnall            *--__result.__seg_ &= __m;
6487a984708SDavid Chisnall            *__result.__seg_ |= __b << __result.__ctz_;
6497a984708SDavid Chisnall        }
6507a984708SDavid Chisnall        // do last word
6517a984708SDavid Chisnall        if (__n > 0)
6527a984708SDavid Chisnall        {
6537a984708SDavid Chisnall            __m = ~__storage_type(0) << (__bits_per_word - __n);
6547a984708SDavid Chisnall            __storage_type __b = *--__last.__seg_ & __m;
65594e3ee44SDavid Chisnall            __clz_r = __bits_per_word - __result.__ctz_;
6567a984708SDavid Chisnall            __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_));
6577a984708SDavid Chisnall            __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r);
6587a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
6597a984708SDavid Chisnall            *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_);
6607a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
6617a984708SDavid Chisnall                                                     __result.__ctz_)  % __bits_per_word);
6627a984708SDavid Chisnall            __n -= __dn;
6637a984708SDavid Chisnall            if (__n > 0)
6647a984708SDavid Chisnall            {
6657a984708SDavid Chisnall                // __result.__ctz_ == 0
6667a984708SDavid Chisnall                --__result.__seg_;
6677a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
6687a984708SDavid Chisnall                __m = ~__storage_type(0) << __result.__ctz_;
6697a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
6707a984708SDavid Chisnall                *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn));
6717a984708SDavid Chisnall            }
6727a984708SDavid Chisnall        }
6737a984708SDavid Chisnall    }
6747a984708SDavid Chisnall    return __result;
6757a984708SDavid Chisnall}
6767a984708SDavid Chisnall
67794e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
6787a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
67994e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
68094e3ee44SDavid Chisnallcopy_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
6817a984708SDavid Chisnall{
6827a984708SDavid Chisnall    if (__last.__ctz_ == __result.__ctz_)
6837a984708SDavid Chisnall        return __copy_backward_aligned(__first, __last, __result);
6847a984708SDavid Chisnall    return __copy_backward_unaligned(__first, __last, __result);
6857a984708SDavid Chisnall}
6867a984708SDavid Chisnall
6877a984708SDavid Chisnall// move
6887a984708SDavid Chisnall
68994e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
6907a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
69194e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
69294e3ee44SDavid Chisnallmove(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
6937a984708SDavid Chisnall{
6947a984708SDavid Chisnall    return _VSTD::copy(__first, __last, __result);
6957a984708SDavid Chisnall}
6967a984708SDavid Chisnall
6977a984708SDavid Chisnall// move_backward
6987a984708SDavid Chisnall
69994e3ee44SDavid Chisnalltemplate <class _Cp, bool _IsConst>
7007a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
70194e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
70294e3ee44SDavid Chisnallmove_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
7037a984708SDavid Chisnall{
704d72607e9SDimitry Andric    return _VSTD::copy_backward(__first, __last, __result);
7057a984708SDavid Chisnall}
7067a984708SDavid Chisnall
7077a984708SDavid Chisnall// swap_ranges
7087a984708SDavid Chisnall
7097a984708SDavid Chisnalltemplate <class __C1, class __C2>
7107a984708SDavid Chisnall__bit_iterator<__C2, false>
7117a984708SDavid Chisnall__swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
7127a984708SDavid Chisnall                      __bit_iterator<__C2, false> __result)
7137a984708SDavid Chisnall{
7147a984708SDavid Chisnall    typedef __bit_iterator<__C1, false> _I1;
7157a984708SDavid Chisnall    typedef  typename _I1::difference_type difference_type;
7167a984708SDavid Chisnall    typedef typename _I1::__storage_type __storage_type;
717aed8d94eSDimitry Andric    const int __bits_per_word = _I1::__bits_per_word;
7187a984708SDavid Chisnall    difference_type __n = __last - __first;
7197a984708SDavid Chisnall    if (__n > 0)
7207a984708SDavid Chisnall    {
7217a984708SDavid Chisnall        // do first word
7227a984708SDavid Chisnall        if (__first.__ctz_ != 0)
7237a984708SDavid Chisnall        {
7247a984708SDavid Chisnall            unsigned __clz = __bits_per_word - __first.__ctz_;
7257a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
7267a984708SDavid Chisnall            __n -= __dn;
7277a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
7287a984708SDavid Chisnall            __storage_type __b1 = *__first.__seg_ & __m;
7297a984708SDavid Chisnall            *__first.__seg_ &= ~__m;
7307a984708SDavid Chisnall            __storage_type __b2 = *__result.__seg_ & __m;
7317a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
7327a984708SDavid Chisnall            *__result.__seg_ |= __b1;
7337a984708SDavid Chisnall            *__first.__seg_  |= __b2;
7347a984708SDavid Chisnall            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
7357a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
7367a984708SDavid Chisnall            ++__first.__seg_;
7377a984708SDavid Chisnall            // __first.__ctz_ = 0;
7387a984708SDavid Chisnall        }
7397a984708SDavid Chisnall        // __first.__ctz_ == 0;
7407a984708SDavid Chisnall        // do middle words
7417a984708SDavid Chisnall        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_)
7427a984708SDavid Chisnall            swap(*__first.__seg_, *__result.__seg_);
7437a984708SDavid Chisnall        // do last word
7447a984708SDavid Chisnall        if (__n > 0)
7457a984708SDavid Chisnall        {
7467a984708SDavid Chisnall            __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
7477a984708SDavid Chisnall            __storage_type __b1 = *__first.__seg_ & __m;
7487a984708SDavid Chisnall            *__first.__seg_ &= ~__m;
7497a984708SDavid Chisnall            __storage_type __b2 = *__result.__seg_ & __m;
7507a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
7517a984708SDavid Chisnall            *__result.__seg_ |= __b1;
7527a984708SDavid Chisnall            *__first.__seg_  |= __b2;
7537a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>(__n);
7547a984708SDavid Chisnall        }
7557a984708SDavid Chisnall    }
7567a984708SDavid Chisnall    return __result;
7577a984708SDavid Chisnall}
7587a984708SDavid Chisnall
7597a984708SDavid Chisnalltemplate <class __C1, class __C2>
7607a984708SDavid Chisnall__bit_iterator<__C2, false>
7617a984708SDavid Chisnall__swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
7627a984708SDavid Chisnall                        __bit_iterator<__C2, false> __result)
7637a984708SDavid Chisnall{
7647a984708SDavid Chisnall    typedef __bit_iterator<__C1, false> _I1;
7657a984708SDavid Chisnall    typedef  typename _I1::difference_type difference_type;
7667a984708SDavid Chisnall    typedef typename _I1::__storage_type __storage_type;
767aed8d94eSDimitry Andric    const int __bits_per_word = _I1::__bits_per_word;
7687a984708SDavid Chisnall    difference_type __n = __last - __first;
7697a984708SDavid Chisnall    if (__n > 0)
7707a984708SDavid Chisnall    {
7717a984708SDavid Chisnall        // do first word
7727a984708SDavid Chisnall        if (__first.__ctz_ != 0)
7737a984708SDavid Chisnall        {
7747a984708SDavid Chisnall            unsigned __clz_f = __bits_per_word - __first.__ctz_;
7757a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
7767a984708SDavid Chisnall            __n -= __dn;
7777a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
7787a984708SDavid Chisnall            __storage_type __b1 = *__first.__seg_ & __m;
7797a984708SDavid Chisnall            *__first.__seg_ &= ~__m;
7807a984708SDavid Chisnall            unsigned __clz_r = __bits_per_word - __result.__ctz_;
7817a984708SDavid Chisnall            __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
7827a984708SDavid Chisnall            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
7837a984708SDavid Chisnall            __storage_type __b2 = *__result.__seg_ & __m;
7847a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
7857a984708SDavid Chisnall            if (__result.__ctz_ > __first.__ctz_)
7867a984708SDavid Chisnall            {
7877a984708SDavid Chisnall                unsigned __s = __result.__ctz_ - __first.__ctz_;
7887a984708SDavid Chisnall                *__result.__seg_ |= __b1 << __s;
7897a984708SDavid Chisnall                *__first.__seg_  |= __b2 >> __s;
7907a984708SDavid Chisnall            }
7917a984708SDavid Chisnall            else
7927a984708SDavid Chisnall            {
7937a984708SDavid Chisnall                unsigned __s = __first.__ctz_ - __result.__ctz_;
7947a984708SDavid Chisnall                *__result.__seg_ |= __b1 >> __s;
7957a984708SDavid Chisnall                *__first.__seg_  |= __b2 << __s;
7967a984708SDavid Chisnall            }
7977a984708SDavid Chisnall            __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
7987a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_)  % __bits_per_word);
7997a984708SDavid Chisnall            __dn -= __ddn;
8007a984708SDavid Chisnall            if (__dn > 0)
8017a984708SDavid Chisnall            {
8027a984708SDavid Chisnall                __m = ~__storage_type(0) >> (__bits_per_word - __dn);
8037a984708SDavid Chisnall                __b2 = *__result.__seg_ & __m;
8047a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
8057a984708SDavid Chisnall                unsigned __s = __first.__ctz_ + __ddn;
8067a984708SDavid Chisnall                *__result.__seg_ |= __b1 >> __s;
8077a984708SDavid Chisnall                *__first.__seg_  |= __b2 << __s;
8087a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(__dn);
8097a984708SDavid Chisnall            }
8107a984708SDavid Chisnall            ++__first.__seg_;
8117a984708SDavid Chisnall            // __first.__ctz_ = 0;
8127a984708SDavid Chisnall        }
8137a984708SDavid Chisnall        // __first.__ctz_ == 0;
8147a984708SDavid Chisnall        // do middle words
8157a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) << __result.__ctz_;
8167a984708SDavid Chisnall        unsigned __clz_r = __bits_per_word - __result.__ctz_;
8177a984708SDavid Chisnall        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
8187a984708SDavid Chisnall        {
8197a984708SDavid Chisnall            __storage_type __b1 = *__first.__seg_;
8207a984708SDavid Chisnall            __storage_type __b2 = *__result.__seg_ & __m;
8217a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
8227a984708SDavid Chisnall            *__result.__seg_ |= __b1 << __result.__ctz_;
8237a984708SDavid Chisnall            *__first.__seg_  = __b2 >> __result.__ctz_;
8247a984708SDavid Chisnall            ++__result.__seg_;
8257a984708SDavid Chisnall            __b2 = *__result.__seg_ & ~__m;
8267a984708SDavid Chisnall            *__result.__seg_ &= __m;
8277a984708SDavid Chisnall            *__result.__seg_ |= __b1 >> __clz_r;
8287a984708SDavid Chisnall            *__first.__seg_  |= __b2 << __clz_r;
8297a984708SDavid Chisnall        }
8307a984708SDavid Chisnall        // do last word
8317a984708SDavid Chisnall        if (__n > 0)
8327a984708SDavid Chisnall        {
8337a984708SDavid Chisnall            __m = ~__storage_type(0) >> (__bits_per_word - __n);
8347a984708SDavid Chisnall            __storage_type __b1 = *__first.__seg_ & __m;
8357a984708SDavid Chisnall            *__first.__seg_ &= ~__m;
8367a984708SDavid Chisnall            __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r);
8377a984708SDavid Chisnall            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
8387a984708SDavid Chisnall            __storage_type __b2 = *__result.__seg_ & __m;
8397a984708SDavid Chisnall            *__result.__seg_ &= ~__m;
8407a984708SDavid Chisnall            *__result.__seg_ |= __b1 << __result.__ctz_;
8417a984708SDavid Chisnall            *__first.__seg_  |= __b2 >> __result.__ctz_;
8427a984708SDavid Chisnall            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
8437a984708SDavid Chisnall            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
8447a984708SDavid Chisnall            __n -= __dn;
8457a984708SDavid Chisnall            if (__n > 0)
8467a984708SDavid Chisnall            {
8477a984708SDavid Chisnall                __m = ~__storage_type(0) >> (__bits_per_word - __n);
8487a984708SDavid Chisnall                __b2 = *__result.__seg_ & __m;
8497a984708SDavid Chisnall                *__result.__seg_ &= ~__m;
8507a984708SDavid Chisnall                *__result.__seg_ |= __b1 >> __dn;
8517a984708SDavid Chisnall                *__first.__seg_  |= __b2 << __dn;
8527a984708SDavid Chisnall                __result.__ctz_ = static_cast<unsigned>(__n);
8537a984708SDavid Chisnall            }
8547a984708SDavid Chisnall        }
8557a984708SDavid Chisnall    }
8567a984708SDavid Chisnall    return __result;
8577a984708SDavid Chisnall}
8587a984708SDavid Chisnall
8597a984708SDavid Chisnalltemplate <class __C1, class __C2>
8607a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
8617a984708SDavid Chisnall__bit_iterator<__C2, false>
8627a984708SDavid Chisnallswap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1,
8637a984708SDavid Chisnall            __bit_iterator<__C2, false> __first2)
8647a984708SDavid Chisnall{
8657a984708SDavid Chisnall    if (__first1.__ctz_ == __first2.__ctz_)
8667a984708SDavid Chisnall        return __swap_ranges_aligned(__first1, __last1, __first2);
8677a984708SDavid Chisnall    return __swap_ranges_unaligned(__first1, __last1, __first2);
8687a984708SDavid Chisnall}
8697a984708SDavid Chisnall
8707a984708SDavid Chisnall// rotate
8717a984708SDavid Chisnall
87294e3ee44SDavid Chisnalltemplate <class _Cp>
8737a984708SDavid Chisnallstruct __bit_array
8747a984708SDavid Chisnall{
87594e3ee44SDavid Chisnall    typedef typename _Cp::difference_type difference_type;
87694e3ee44SDavid Chisnall    typedef typename _Cp::__storage_type  __storage_type;
8774bab9fd9SDavid Chisnall    typedef typename _Cp::__storage_pointer __storage_pointer;
87894e3ee44SDavid Chisnall    typedef typename _Cp::iterator        iterator;
87994e3ee44SDavid Chisnall    static const unsigned __bits_per_word = _Cp::__bits_per_word;
88094e3ee44SDavid Chisnall    static const unsigned _Np = 4;
8817a984708SDavid Chisnall
8827a984708SDavid Chisnall    difference_type __size_;
88394e3ee44SDavid Chisnall    __storage_type __word_[_Np];
8847a984708SDavid Chisnall
8857a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY static difference_type capacity()
88694e3ee44SDavid Chisnall        {return static_cast<difference_type>(_Np * __bits_per_word);}
8877a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY explicit __bit_array(difference_type __s) : __size_(__s) {}
8884bab9fd9SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY iterator begin()
8894bab9fd9SDavid Chisnall    {
8904bab9fd9SDavid Chisnall        return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
8914bab9fd9SDavid Chisnall    }
8924bab9fd9SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY iterator end()
8934bab9fd9SDavid Chisnall    {
8944bab9fd9SDavid Chisnall        return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
8954bab9fd9SDavid Chisnall                                                  static_cast<unsigned>(__size_ % __bits_per_word));
8964bab9fd9SDavid Chisnall    }
8977a984708SDavid Chisnall};
8987a984708SDavid Chisnall
89994e3ee44SDavid Chisnalltemplate <class _Cp>
90094e3ee44SDavid Chisnall__bit_iterator<_Cp, false>
90194e3ee44SDavid Chisnallrotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last)
9027a984708SDavid Chisnall{
90394e3ee44SDavid Chisnall    typedef __bit_iterator<_Cp, false> _I1;
9047a984708SDavid Chisnall    typedef  typename _I1::difference_type difference_type;
9057a984708SDavid Chisnall    difference_type __d1 = __middle - __first;
9067a984708SDavid Chisnall    difference_type __d2 = __last - __middle;
9077a984708SDavid Chisnall    _I1 __r = __first + __d2;
9087a984708SDavid Chisnall    while (__d1 != 0 && __d2 != 0)
9097a984708SDavid Chisnall    {
9107a984708SDavid Chisnall        if (__d1 <= __d2)
9117a984708SDavid Chisnall        {
91294e3ee44SDavid Chisnall            if (__d1 <= __bit_array<_Cp>::capacity())
9137a984708SDavid Chisnall            {
91494e3ee44SDavid Chisnall                __bit_array<_Cp> __b(__d1);
9157a984708SDavid Chisnall                _VSTD::copy(__first, __middle, __b.begin());
9167a984708SDavid Chisnall                _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first));
9177a984708SDavid Chisnall                break;
9187a984708SDavid Chisnall            }
9197a984708SDavid Chisnall            else
9207a984708SDavid Chisnall            {
92194e3ee44SDavid Chisnall                __bit_iterator<_Cp, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle);
9227a984708SDavid Chisnall                __first = __middle;
9237a984708SDavid Chisnall                __middle = __mp;
9247a984708SDavid Chisnall                __d2 -= __d1;
9257a984708SDavid Chisnall            }
9267a984708SDavid Chisnall        }
9277a984708SDavid Chisnall        else
9287a984708SDavid Chisnall        {
92994e3ee44SDavid Chisnall            if (__d2 <= __bit_array<_Cp>::capacity())
9307a984708SDavid Chisnall            {
93194e3ee44SDavid Chisnall                __bit_array<_Cp> __b(__d2);
9327a984708SDavid Chisnall                _VSTD::copy(__middle, __last, __b.begin());
9337a984708SDavid Chisnall                _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last));
9347a984708SDavid Chisnall                break;
9357a984708SDavid Chisnall            }
9367a984708SDavid Chisnall            else
9377a984708SDavid Chisnall            {
93894e3ee44SDavid Chisnall                __bit_iterator<_Cp, false> __mp = __first + __d2;
9397a984708SDavid Chisnall                _VSTD::swap_ranges(__first, __mp, __middle);
9407a984708SDavid Chisnall                __first = __mp;
9417a984708SDavid Chisnall                __d1 -= __d2;
9427a984708SDavid Chisnall            }
9437a984708SDavid Chisnall        }
9447a984708SDavid Chisnall    }
9457a984708SDavid Chisnall    return __r;
9467a984708SDavid Chisnall}
9477a984708SDavid Chisnall
9487a984708SDavid Chisnall// equal
9497a984708SDavid Chisnall
950936e9439SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2>
9517a984708SDavid Chisnallbool
952936e9439SDimitry Andric__equal_unaligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1,
953936e9439SDimitry Andric                  __bit_iterator<_Cp, _IC2> __first2)
9547a984708SDavid Chisnall{
955936e9439SDimitry Andric    typedef __bit_iterator<_Cp, _IC1> _It;
9567a984708SDavid Chisnall    typedef  typename _It::difference_type difference_type;
9577a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
958aed8d94eSDimitry Andric    static const int __bits_per_word = _It::__bits_per_word;
9597a984708SDavid Chisnall    difference_type __n = __last1 - __first1;
9607a984708SDavid Chisnall    if (__n > 0)
9617a984708SDavid Chisnall    {
9627a984708SDavid Chisnall        // do first word
9637a984708SDavid Chisnall        if (__first1.__ctz_ != 0)
9647a984708SDavid Chisnall        {
9657a984708SDavid Chisnall            unsigned __clz_f = __bits_per_word - __first1.__ctz_;
9667a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
9677a984708SDavid Chisnall            __n -= __dn;
9687a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
9697a984708SDavid Chisnall            __storage_type __b = *__first1.__seg_ & __m;
9707a984708SDavid Chisnall            unsigned __clz_r = __bits_per_word - __first2.__ctz_;
9717a984708SDavid Chisnall            __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
9727a984708SDavid Chisnall            __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
9737a984708SDavid Chisnall            if (__first2.__ctz_ > __first1.__ctz_)
974e3726a67SDimitry Andric            {
9757a984708SDavid Chisnall                if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_)))
9767a984708SDavid Chisnall                    return false;
977e3726a67SDimitry Andric            }
9787a984708SDavid Chisnall            else
979e3726a67SDimitry Andric            {
9807a984708SDavid Chisnall                if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_)))
9817a984708SDavid Chisnall                    return false;
982e3726a67SDimitry Andric            }
9837a984708SDavid Chisnall            __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word;
9847a984708SDavid Chisnall            __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_)  % __bits_per_word);
9857a984708SDavid Chisnall            __dn -= __ddn;
9867a984708SDavid Chisnall            if (__dn > 0)
9877a984708SDavid Chisnall            {
9887a984708SDavid Chisnall                __m = ~__storage_type(0) >> (__bits_per_word - __dn);
9897a984708SDavid Chisnall                if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn)))
9907a984708SDavid Chisnall                    return false;
9917a984708SDavid Chisnall                __first2.__ctz_ = static_cast<unsigned>(__dn);
9927a984708SDavid Chisnall            }
9937a984708SDavid Chisnall            ++__first1.__seg_;
9947a984708SDavid Chisnall            // __first1.__ctz_ = 0;
9957a984708SDavid Chisnall        }
9967a984708SDavid Chisnall        // __first1.__ctz_ == 0;
9977a984708SDavid Chisnall        // do middle words
9987a984708SDavid Chisnall        unsigned __clz_r = __bits_per_word - __first2.__ctz_;
9997a984708SDavid Chisnall        __storage_type __m = ~__storage_type(0) << __first2.__ctz_;
10007a984708SDavid Chisnall        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_)
10017a984708SDavid Chisnall        {
10027a984708SDavid Chisnall            __storage_type __b = *__first1.__seg_;
10037a984708SDavid Chisnall            if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
10047a984708SDavid Chisnall                return false;
10057a984708SDavid Chisnall            ++__first2.__seg_;
10067a984708SDavid Chisnall            if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r))
10077a984708SDavid Chisnall                return false;
10087a984708SDavid Chisnall        }
10097a984708SDavid Chisnall        // do last word
10107a984708SDavid Chisnall        if (__n > 0)
10117a984708SDavid Chisnall        {
10127a984708SDavid Chisnall            __m = ~__storage_type(0) >> (__bits_per_word - __n);
10137a984708SDavid Chisnall            __storage_type __b = *__first1.__seg_ & __m;
10147a984708SDavid Chisnall            __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
10157a984708SDavid Chisnall            __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
10167a984708SDavid Chisnall            if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
10177a984708SDavid Chisnall                return false;
10187a984708SDavid Chisnall            __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word;
10197a984708SDavid Chisnall            __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_)  % __bits_per_word);
10207a984708SDavid Chisnall            __n -= __dn;
10217a984708SDavid Chisnall            if (__n > 0)
10227a984708SDavid Chisnall            {
10237a984708SDavid Chisnall                __m = ~__storage_type(0) >> (__bits_per_word - __n);
10247a984708SDavid Chisnall                if ((*__first2.__seg_ & __m) != (__b >> __dn))
10257a984708SDavid Chisnall                    return false;
10267a984708SDavid Chisnall            }
10277a984708SDavid Chisnall        }
10287a984708SDavid Chisnall    }
10297a984708SDavid Chisnall    return true;
10307a984708SDavid Chisnall}
10317a984708SDavid Chisnall
1032936e9439SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2>
10337a984708SDavid Chisnallbool
1034936e9439SDimitry Andric__equal_aligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1,
1035936e9439SDimitry Andric                __bit_iterator<_Cp, _IC2> __first2)
10367a984708SDavid Chisnall{
1037936e9439SDimitry Andric    typedef __bit_iterator<_Cp, _IC1> _It;
10387a984708SDavid Chisnall    typedef  typename _It::difference_type difference_type;
10397a984708SDavid Chisnall    typedef typename _It::__storage_type __storage_type;
1040aed8d94eSDimitry Andric    static const int __bits_per_word = _It::__bits_per_word;
10417a984708SDavid Chisnall    difference_type __n = __last1 - __first1;
10427a984708SDavid Chisnall    if (__n > 0)
10437a984708SDavid Chisnall    {
10447a984708SDavid Chisnall        // do first word
10457a984708SDavid Chisnall        if (__first1.__ctz_ != 0)
10467a984708SDavid Chisnall        {
10477a984708SDavid Chisnall            unsigned __clz = __bits_per_word - __first1.__ctz_;
10487a984708SDavid Chisnall            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
10497a984708SDavid Chisnall            __n -= __dn;
10507a984708SDavid Chisnall            __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
10517a984708SDavid Chisnall            if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
10527a984708SDavid Chisnall                return false;
10537a984708SDavid Chisnall            ++__first2.__seg_;
10547a984708SDavid Chisnall            ++__first1.__seg_;
10557a984708SDavid Chisnall            // __first1.__ctz_ = 0;
10567a984708SDavid Chisnall            // __first2.__ctz_ = 0;
10577a984708SDavid Chisnall        }
10587a984708SDavid Chisnall        // __first1.__ctz_ == 0;
10597a984708SDavid Chisnall        // __first2.__ctz_ == 0;
10607a984708SDavid Chisnall        // do middle words
10617a984708SDavid Chisnall        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_)
10627a984708SDavid Chisnall            if (*__first2.__seg_ != *__first1.__seg_)
10637a984708SDavid Chisnall                return false;
10647a984708SDavid Chisnall        // do last word
10657a984708SDavid Chisnall        if (__n > 0)
10667a984708SDavid Chisnall        {
10677a984708SDavid Chisnall            __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
10687a984708SDavid Chisnall            if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
10697a984708SDavid Chisnall                return false;
10707a984708SDavid Chisnall        }
10717a984708SDavid Chisnall    }
10727a984708SDavid Chisnall    return true;
10737a984708SDavid Chisnall}
10747a984708SDavid Chisnall
107594e3ee44SDavid Chisnalltemplate <class _Cp, bool _IC1, bool _IC2>
10767a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY
10777a984708SDavid Chisnallbool
107894e3ee44SDavid Chisnallequal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __bit_iterator<_Cp, _IC2> __first2)
10797a984708SDavid Chisnall{
10807a984708SDavid Chisnall    if (__first1.__ctz_ == __first2.__ctz_)
10817a984708SDavid Chisnall        return __equal_aligned(__first1, __last1, __first2);
10827a984708SDavid Chisnall    return __equal_unaligned(__first1, __last1, __first2);
10837a984708SDavid Chisnall}
10847a984708SDavid Chisnall
1085936e9439SDimitry Andrictemplate <class _Cp, bool _IsConst,
1086936e9439SDimitry Andric          typename _Cp::__storage_type>
10877a984708SDavid Chisnallclass __bit_iterator
10887a984708SDavid Chisnall{
10897a984708SDavid Chisnallpublic:
109094e3ee44SDavid Chisnall    typedef typename _Cp::difference_type                                                          difference_type;
10917a984708SDavid Chisnall    typedef bool                                                                                  value_type;
10927a984708SDavid Chisnall    typedef __bit_iterator                                                                        pointer;
109394e3ee44SDavid Chisnall    typedef typename conditional<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >::type reference;
10947a984708SDavid Chisnall    typedef random_access_iterator_tag                                                            iterator_category;
10957a984708SDavid Chisnall
10967a984708SDavid Chisnallprivate:
109794e3ee44SDavid Chisnall    typedef typename _Cp::__storage_type                                           __storage_type;
109894e3ee44SDavid Chisnall    typedef typename conditional<_IsConst, typename _Cp::__const_storage_pointer,
109994e3ee44SDavid Chisnall                                           typename _Cp::__storage_pointer>::type  __storage_pointer;
110094e3ee44SDavid Chisnall    static const unsigned __bits_per_word = _Cp::__bits_per_word;
11017a984708SDavid Chisnall
11027a984708SDavid Chisnall    __storage_pointer __seg_;
11037a984708SDavid Chisnall    unsigned          __ctz_;
11047a984708SDavid Chisnall
11057a984708SDavid Chisnallpublic:
11064f7ab58eSDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT
11074f7ab58eSDimitry Andric#if _LIBCPP_STD_VER > 11
11084f7ab58eSDimitry Andric    : __seg_(nullptr), __ctz_(0)
11094f7ab58eSDimitry Andric#endif
11104f7ab58eSDimitry Andric    {}
11117a984708SDavid Chisnall
11127a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
111394e3ee44SDavid Chisnall    __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
11147a984708SDavid Chisnall        : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {}
11157a984708SDavid Chisnall
11167a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
11177a984708SDavid Chisnall        {return reference(__seg_, __storage_type(1) << __ctz_);}
11187a984708SDavid Chisnall
11197a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator++()
11207a984708SDavid Chisnall    {
11217a984708SDavid Chisnall        if (__ctz_ != __bits_per_word-1)
11227a984708SDavid Chisnall            ++__ctz_;
11237a984708SDavid Chisnall        else
11247a984708SDavid Chisnall        {
11257a984708SDavid Chisnall            __ctz_ = 0;
11267a984708SDavid Chisnall            ++__seg_;
11277a984708SDavid Chisnall        }
11287a984708SDavid Chisnall        return *this;
11297a984708SDavid Chisnall    }
11307a984708SDavid Chisnall
11317a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator++(int)
11327a984708SDavid Chisnall    {
11337a984708SDavid Chisnall        __bit_iterator __tmp = *this;
11347a984708SDavid Chisnall        ++(*this);
11357a984708SDavid Chisnall        return __tmp;
11367a984708SDavid Chisnall    }
11377a984708SDavid Chisnall
11387a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator--()
11397a984708SDavid Chisnall    {
11407a984708SDavid Chisnall        if (__ctz_ != 0)
11417a984708SDavid Chisnall            --__ctz_;
11427a984708SDavid Chisnall        else
11437a984708SDavid Chisnall        {
11447a984708SDavid Chisnall            __ctz_ = __bits_per_word - 1;
11457a984708SDavid Chisnall            --__seg_;
11467a984708SDavid Chisnall        }
11477a984708SDavid Chisnall        return *this;
11487a984708SDavid Chisnall    }
11497a984708SDavid Chisnall
11507a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator--(int)
11517a984708SDavid Chisnall    {
11527a984708SDavid Chisnall        __bit_iterator __tmp = *this;
11537a984708SDavid Chisnall        --(*this);
11547a984708SDavid Chisnall        return __tmp;
11557a984708SDavid Chisnall    }
11567a984708SDavid Chisnall
11577a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator+=(difference_type __n)
11587a984708SDavid Chisnall    {
11597a984708SDavid Chisnall        if (__n >= 0)
11607a984708SDavid Chisnall            __seg_ += (__n + __ctz_) / __bits_per_word;
11617a984708SDavid Chisnall        else
11627a984708SDavid Chisnall            __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1)
11637a984708SDavid Chisnall                    / static_cast<difference_type>(__bits_per_word);
11647a984708SDavid Chisnall        __n &= (__bits_per_word - 1);
11657a984708SDavid Chisnall        __ctz_ = static_cast<unsigned>((__n + __ctz_)  % __bits_per_word);
11667a984708SDavid Chisnall        return *this;
11677a984708SDavid Chisnall    }
11687a984708SDavid Chisnall
11697a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator-=(difference_type __n)
11707a984708SDavid Chisnall    {
11717a984708SDavid Chisnall        return *this += -__n;
11727a984708SDavid Chisnall    }
11737a984708SDavid Chisnall
11747a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator+(difference_type __n) const
11757a984708SDavid Chisnall    {
11767a984708SDavid Chisnall        __bit_iterator __t(*this);
11777a984708SDavid Chisnall        __t += __n;
11787a984708SDavid Chisnall        return __t;
11797a984708SDavid Chisnall    }
11807a984708SDavid Chisnall
11817a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator-(difference_type __n) const
11827a984708SDavid Chisnall    {
11837a984708SDavid Chisnall        __bit_iterator __t(*this);
11847a984708SDavid Chisnall        __t -= __n;
11857a984708SDavid Chisnall        return __t;
11867a984708SDavid Chisnall    }
11877a984708SDavid Chisnall
11887a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
11897a984708SDavid Chisnall    friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;}
11907a984708SDavid Chisnall
11917a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
11927a984708SDavid Chisnall    friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y)
11937a984708SDavid Chisnall        {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;}
11947a984708SDavid Chisnall
11957a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const {return *(*this + __n);}
11967a984708SDavid Chisnall
11977a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y)
11987a984708SDavid Chisnall        {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;}
11997a984708SDavid Chisnall
12007a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y)
12017a984708SDavid Chisnall        {return !(__x == __y);}
12027a984708SDavid Chisnall
12037a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y)
12047a984708SDavid Chisnall        {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);}
12057a984708SDavid Chisnall
12067a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y)
12077a984708SDavid Chisnall        {return __y < __x;}
12087a984708SDavid Chisnall
12097a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y)
12107a984708SDavid Chisnall        {return !(__y < __x);}
12117a984708SDavid Chisnall
12127a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y)
12137a984708SDavid Chisnall        {return !(__x < __y);}
12147a984708SDavid Chisnall
12157a984708SDavid Chisnallprivate:
12167a984708SDavid Chisnall    _LIBCPP_INLINE_VISIBILITY
12177a984708SDavid Chisnall    __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
12187a984708SDavid Chisnall        : __seg_(__s), __ctz_(__ctz) {}
12197a984708SDavid Chisnall
122094e3ee44SDavid Chisnall    friend typename _Cp::__self;
122180779b37SDimitry Andric
122294e3ee44SDavid Chisnall    friend class __bit_reference<_Cp>;
122394e3ee44SDavid Chisnall    friend class __bit_const_reference<_Cp>;
122494e3ee44SDavid Chisnall    friend class __bit_iterator<_Cp, true>;
122594e3ee44SDavid Chisnall    template <class _Dp> friend struct __bit_array;
122694e3ee44SDavid Chisnall    template <class _Dp> friend void __fill_n_false(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
122794e3ee44SDavid Chisnall    template <class _Dp> friend void __fill_n_true(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
122894e3ee44SDavid Chisnall    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_aligned(__bit_iterator<_Dp, _IC> __first,
122994e3ee44SDavid Chisnall                                                                                  __bit_iterator<_Dp, _IC> __last,
123094e3ee44SDavid Chisnall                                                                                  __bit_iterator<_Dp, false> __result);
123194e3ee44SDavid Chisnall    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_unaligned(__bit_iterator<_Dp, _IC> __first,
123294e3ee44SDavid Chisnall                                                                                    __bit_iterator<_Dp, _IC> __last,
123394e3ee44SDavid Chisnall                                                                                    __bit_iterator<_Dp, false> __result);
123494e3ee44SDavid Chisnall    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy(__bit_iterator<_Dp, _IC> __first,
123594e3ee44SDavid Chisnall                                                                        __bit_iterator<_Dp, _IC> __last,
123694e3ee44SDavid Chisnall                                                                        __bit_iterator<_Dp, false> __result);
123794e3ee44SDavid Chisnall    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_aligned(__bit_iterator<_Dp, _IC> __first,
123894e3ee44SDavid Chisnall                                                                                           __bit_iterator<_Dp, _IC> __last,
123994e3ee44SDavid Chisnall                                                                                           __bit_iterator<_Dp, false> __result);
124094e3ee44SDavid Chisnall    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_unaligned(__bit_iterator<_Dp, _IC> __first,
124194e3ee44SDavid Chisnall                                                                                             __bit_iterator<_Dp, _IC> __last,
124294e3ee44SDavid Chisnall                                                                                             __bit_iterator<_Dp, false> __result);
124394e3ee44SDavid Chisnall    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy_backward(__bit_iterator<_Dp, _IC> __first,
124494e3ee44SDavid Chisnall                                                                                 __bit_iterator<_Dp, _IC> __last,
124594e3ee44SDavid Chisnall                                                                                 __bit_iterator<_Dp, false> __result);
12467a984708SDavid Chisnall    template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>,
12477a984708SDavid Chisnall                                                                                           __bit_iterator<__C1, false>,
12487a984708SDavid Chisnall                                                                                           __bit_iterator<__C2, false>);
12497a984708SDavid Chisnall    template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>,
12507a984708SDavid Chisnall                                                                                             __bit_iterator<__C1, false>,
12517a984708SDavid Chisnall                                                                                             __bit_iterator<__C2, false>);
12527a984708SDavid Chisnall    template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>,
12537a984708SDavid Chisnall                                                                                 __bit_iterator<__C1, false>,
12547a984708SDavid Chisnall                                                                                 __bit_iterator<__C2, false>);
125594e3ee44SDavid Chisnall    template <class _Dp> friend __bit_iterator<_Dp, false> rotate(__bit_iterator<_Dp, false>,
125694e3ee44SDavid Chisnall                                                                __bit_iterator<_Dp, false>,
125794e3ee44SDavid Chisnall                                                                __bit_iterator<_Dp, false>);
1258936e9439SDimitry Andric    template <class _Dp, bool _IC1, bool _IC2> friend bool __equal_aligned(__bit_iterator<_Dp, _IC1>,
1259936e9439SDimitry Andric                                                    __bit_iterator<_Dp, _IC1>,
1260936e9439SDimitry Andric                                                    __bit_iterator<_Dp, _IC2>);
1261936e9439SDimitry Andric    template <class _Dp, bool _IC1, bool _IC2> friend bool __equal_unaligned(__bit_iterator<_Dp, _IC1>,
1262936e9439SDimitry Andric                                                      __bit_iterator<_Dp, _IC1>,
1263936e9439SDimitry Andric                                                      __bit_iterator<_Dp, _IC2>);
126494e3ee44SDavid Chisnall    template <class _Dp, bool _IC1, bool _IC2> friend bool equal(__bit_iterator<_Dp, _IC1>,
126594e3ee44SDavid Chisnall                                                                __bit_iterator<_Dp, _IC1>,
126694e3ee44SDavid Chisnall                                                                __bit_iterator<_Dp, _IC2>);
1267936e9439SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, _IC> __find_bool_true(__bit_iterator<_Dp, _IC>,
126894e3ee44SDavid Chisnall                                                                          typename _Dp::size_type);
1269936e9439SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, _IC> __find_bool_false(__bit_iterator<_Dp, _IC>,
127094e3ee44SDavid Chisnall                                                                           typename _Dp::size_type);
1271936e9439SDimitry Andric    template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type
1272936e9439SDimitry Andric                   __count_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
1273936e9439SDimitry Andric    template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type
1274936e9439SDimitry Andric                   __count_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
12757a984708SDavid Chisnall};
12767a984708SDavid Chisnall
12777a984708SDavid Chisnall_LIBCPP_END_NAMESPACE_STD
12787a984708SDavid Chisnall
1279f9448bf3SDimitry Andric_LIBCPP_POP_MACROS
1280f9448bf3SDimitry Andric
12817a984708SDavid Chisnall#endif  // _LIBCPP___BIT_REFERENCE
1282