xref: /freebsd/contrib/llvm-project/libcxx/include/__bit_reference (revision 4652422eb477731f284b1345afeefef7f269da50)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP___BIT_REFERENCE
110b57cec5SDimitry Andric#define _LIBCPP___BIT_REFERENCE
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric#include <__config>
14e8d8bef9SDimitry Andric#include <__bits>
150b57cec5SDimitry Andric#include <algorithm>
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
180b57cec5SDimitry Andric#pragma GCC system_header
190b57cec5SDimitry Andric#endif
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
220b57cec5SDimitry Andric#include <__undef_macros>
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
260b57cec5SDimitry Andric
270b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, typename _Cp::__storage_type = 0> class __bit_iterator;
280b57cec5SDimitry Andrictemplate <class _Cp> class __bit_const_reference;
290b57cec5SDimitry Andric
300b57cec5SDimitry Andrictemplate <class _Tp>
310b57cec5SDimitry Andricstruct __has_storage_type
320b57cec5SDimitry Andric{
330b57cec5SDimitry Andric    static const bool value = false;
340b57cec5SDimitry Andric};
350b57cec5SDimitry Andric
360b57cec5SDimitry Andrictemplate <class _Cp, bool = __has_storage_type<_Cp>::value>
370b57cec5SDimitry Andricclass __bit_reference
380b57cec5SDimitry Andric{
390b57cec5SDimitry Andric    typedef typename _Cp::__storage_type    __storage_type;
400b57cec5SDimitry Andric    typedef typename _Cp::__storage_pointer __storage_pointer;
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric    __storage_pointer __seg_;
430b57cec5SDimitry Andric    __storage_type    __mask_;
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric    friend typename _Cp::__self;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric    friend class __bit_const_reference<_Cp>;
480b57cec5SDimitry Andric    friend class __bit_iterator<_Cp, false>;
490b57cec5SDimitry Andricpublic:
50480093f4SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
51480093f4SDimitry Andric    __bit_reference(const __bit_reference&) = default;
52480093f4SDimitry Andric
530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
540b57cec5SDimitry Andric        {return static_cast<bool>(*__seg_ & __mask_);}
550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY bool operator ~() const _NOEXCEPT
560b57cec5SDimitry Andric        {return !static_cast<bool>(*this);}
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
590b57cec5SDimitry Andric    __bit_reference& operator=(bool __x) _NOEXCEPT
600b57cec5SDimitry Andric    {
610b57cec5SDimitry Andric        if (__x)
620b57cec5SDimitry Andric            *__seg_ |= __mask_;
630b57cec5SDimitry Andric        else
640b57cec5SDimitry Andric            *__seg_ &= ~__mask_;
650b57cec5SDimitry Andric        return *this;
660b57cec5SDimitry Andric    }
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
690b57cec5SDimitry Andric    __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT
700b57cec5SDimitry Andric        {return operator=(static_cast<bool>(__x));}
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {*__seg_ ^= __mask_;}
730b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, false> operator&() const _NOEXCEPT
740b57cec5SDimitry Andric        {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(__libcpp_ctz(__mask_)));}
750b57cec5SDimitry Andricprivate:
760b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
770b57cec5SDimitry Andric    __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
780b57cec5SDimitry Andric        : __seg_(__s), __mask_(__m) {}
790b57cec5SDimitry Andric};
800b57cec5SDimitry Andric
810b57cec5SDimitry Andrictemplate <class _Cp>
820b57cec5SDimitry Andricclass __bit_reference<_Cp, false>
830b57cec5SDimitry Andric{
840b57cec5SDimitry Andric};
850b57cec5SDimitry Andric
860b57cec5SDimitry Andrictemplate <class _Cp>
870b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
880b57cec5SDimitry Andricvoid
890b57cec5SDimitry Andricswap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
900b57cec5SDimitry Andric{
910b57cec5SDimitry Andric    bool __t = __x;
920b57cec5SDimitry Andric    __x = __y;
930b57cec5SDimitry Andric    __y = __t;
940b57cec5SDimitry Andric}
950b57cec5SDimitry Andric
960b57cec5SDimitry Andrictemplate <class _Cp, class _Dp>
970b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
980b57cec5SDimitry Andricvoid
990b57cec5SDimitry Andricswap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
1000b57cec5SDimitry Andric{
1010b57cec5SDimitry Andric    bool __t = __x;
1020b57cec5SDimitry Andric    __x = __y;
1030b57cec5SDimitry Andric    __y = __t;
1040b57cec5SDimitry Andric}
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andrictemplate <class _Cp>
1070b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1080b57cec5SDimitry Andricvoid
1090b57cec5SDimitry Andricswap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT
1100b57cec5SDimitry Andric{
1110b57cec5SDimitry Andric    bool __t = __x;
1120b57cec5SDimitry Andric    __x = __y;
1130b57cec5SDimitry Andric    __y = __t;
1140b57cec5SDimitry Andric}
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andrictemplate <class _Cp>
1170b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1180b57cec5SDimitry Andricvoid
1190b57cec5SDimitry Andricswap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT
1200b57cec5SDimitry Andric{
1210b57cec5SDimitry Andric    bool __t = __x;
1220b57cec5SDimitry Andric    __x = __y;
1230b57cec5SDimitry Andric    __y = __t;
1240b57cec5SDimitry Andric}
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andrictemplate <class _Cp>
1270b57cec5SDimitry Andricclass __bit_const_reference
1280b57cec5SDimitry Andric{
1290b57cec5SDimitry Andric    typedef typename _Cp::__storage_type          __storage_type;
1300b57cec5SDimitry Andric    typedef typename _Cp::__const_storage_pointer __storage_pointer;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric    __storage_pointer        __seg_;
1330b57cec5SDimitry Andric    __storage_type __mask_;
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric    friend typename _Cp::__self;
1360b57cec5SDimitry Andric    friend class __bit_iterator<_Cp, true>;
1370b57cec5SDimitry Andricpublic:
1380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
139480093f4SDimitry Andric    __bit_const_reference(const __bit_const_reference&) = default;
140480093f4SDimitry Andric
141480093f4SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1420b57cec5SDimitry Andric    __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT
1430b57cec5SDimitry Andric        : __seg_(__x.__seg_), __mask_(__x.__mask_) {}
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT
1460b57cec5SDimitry Andric        {return static_cast<bool>(*__seg_ & __mask_);}
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, true> operator&() const _NOEXCEPT
1490b57cec5SDimitry Andric        {return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(__libcpp_ctz(__mask_)));}
1500b57cec5SDimitry Andricprivate:
1510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1520b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR
1530b57cec5SDimitry Andric    __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
1540b57cec5SDimitry Andric        : __seg_(__s), __mask_(__m) {}
1550b57cec5SDimitry Andric
156480093f4SDimitry Andric    __bit_const_reference& operator=(const __bit_const_reference&) = delete;
1570b57cec5SDimitry Andric};
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric// find
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
1620b57cec5SDimitry Andric__bit_iterator<_Cp, _IsConst>
1630b57cec5SDimitry Andric__find_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
1640b57cec5SDimitry Andric{
1650b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
1660b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
1670b57cec5SDimitry Andric    static const int __bits_per_word = _It::__bits_per_word;
1680b57cec5SDimitry Andric    // do first partial word
1690b57cec5SDimitry Andric    if (__first.__ctz_ != 0)
1700b57cec5SDimitry Andric    {
1710b57cec5SDimitry Andric        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
1720b57cec5SDimitry Andric        __storage_type __dn = _VSTD::min(__clz_f, __n);
1730b57cec5SDimitry Andric        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
1740b57cec5SDimitry Andric        __storage_type __b = *__first.__seg_ & __m;
1750b57cec5SDimitry Andric        if (__b)
1760b57cec5SDimitry Andric            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
1770b57cec5SDimitry Andric        if (__n == __dn)
1780b57cec5SDimitry Andric            return __first + __n;
1790b57cec5SDimitry Andric        __n -= __dn;
1800b57cec5SDimitry Andric        ++__first.__seg_;
1810b57cec5SDimitry Andric    }
1820b57cec5SDimitry Andric    // do middle whole words
1830b57cec5SDimitry Andric    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
1840b57cec5SDimitry Andric        if (*__first.__seg_)
1850b57cec5SDimitry Andric            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(*__first.__seg_)));
1860b57cec5SDimitry Andric    // do last partial word
1870b57cec5SDimitry Andric    if (__n > 0)
1880b57cec5SDimitry Andric    {
1890b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
1900b57cec5SDimitry Andric        __storage_type __b = *__first.__seg_ & __m;
1910b57cec5SDimitry Andric        if (__b)
1920b57cec5SDimitry Andric            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
1930b57cec5SDimitry Andric    }
1940b57cec5SDimitry Andric    return _It(__first.__seg_, static_cast<unsigned>(__n));
1950b57cec5SDimitry Andric}
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
1980b57cec5SDimitry Andric__bit_iterator<_Cp, _IsConst>
1990b57cec5SDimitry Andric__find_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
2000b57cec5SDimitry Andric{
2010b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
2020b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
2030b57cec5SDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
2040b57cec5SDimitry Andric    // do first partial word
2050b57cec5SDimitry Andric    if (__first.__ctz_ != 0)
2060b57cec5SDimitry Andric    {
2070b57cec5SDimitry Andric        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
2080b57cec5SDimitry Andric        __storage_type __dn = _VSTD::min(__clz_f, __n);
2090b57cec5SDimitry Andric        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
2100b57cec5SDimitry Andric        __storage_type __b = ~*__first.__seg_ & __m;
2110b57cec5SDimitry Andric        if (__b)
2120b57cec5SDimitry Andric            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
2130b57cec5SDimitry Andric        if (__n == __dn)
2140b57cec5SDimitry Andric            return __first + __n;
2150b57cec5SDimitry Andric        __n -= __dn;
2160b57cec5SDimitry Andric        ++__first.__seg_;
2170b57cec5SDimitry Andric    }
2180b57cec5SDimitry Andric    // do middle whole words
2190b57cec5SDimitry Andric    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
2200b57cec5SDimitry Andric    {
2210b57cec5SDimitry Andric        __storage_type __b = ~*__first.__seg_;
2220b57cec5SDimitry Andric        if (__b)
2230b57cec5SDimitry Andric            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
2240b57cec5SDimitry Andric    }
2250b57cec5SDimitry Andric    // do last partial word
2260b57cec5SDimitry Andric    if (__n > 0)
2270b57cec5SDimitry Andric    {
2280b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2290b57cec5SDimitry Andric        __storage_type __b = ~*__first.__seg_ & __m;
2300b57cec5SDimitry Andric        if (__b)
2310b57cec5SDimitry Andric            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
2320b57cec5SDimitry Andric    }
2330b57cec5SDimitry Andric    return _It(__first.__seg_, static_cast<unsigned>(__n));
2340b57cec5SDimitry Andric}
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, class _Tp>
2370b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
2380b57cec5SDimitry Andric__bit_iterator<_Cp, _IsConst>
2390b57cec5SDimitry Andricfind(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value_)
2400b57cec5SDimitry Andric{
2410b57cec5SDimitry Andric    if (static_cast<bool>(__value_))
242e8d8bef9SDimitry Andric        return _VSTD::__find_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
243e8d8bef9SDimitry Andric    return _VSTD::__find_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
2440b57cec5SDimitry Andric}
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andric// count
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
2490b57cec5SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type
2500b57cec5SDimitry Andric__count_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
2510b57cec5SDimitry Andric{
2520b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
2530b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
2540b57cec5SDimitry Andric    typedef typename _It::difference_type difference_type;
2550b57cec5SDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
2560b57cec5SDimitry Andric    difference_type __r = 0;
2570b57cec5SDimitry Andric    // do first partial word
2580b57cec5SDimitry Andric    if (__first.__ctz_ != 0)
2590b57cec5SDimitry Andric    {
2600b57cec5SDimitry Andric        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
2610b57cec5SDimitry Andric        __storage_type __dn = _VSTD::min(__clz_f, __n);
2620b57cec5SDimitry Andric        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
2630b57cec5SDimitry Andric        __r = _VSTD::__libcpp_popcount(*__first.__seg_ & __m);
2640b57cec5SDimitry Andric        __n -= __dn;
2650b57cec5SDimitry Andric        ++__first.__seg_;
2660b57cec5SDimitry Andric    }
2670b57cec5SDimitry Andric    // do middle whole words
2680b57cec5SDimitry Andric    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
2690b57cec5SDimitry Andric        __r += _VSTD::__libcpp_popcount(*__first.__seg_);
2700b57cec5SDimitry Andric    // do last partial word
2710b57cec5SDimitry Andric    if (__n > 0)
2720b57cec5SDimitry Andric    {
2730b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
2740b57cec5SDimitry Andric        __r += _VSTD::__libcpp_popcount(*__first.__seg_ & __m);
2750b57cec5SDimitry Andric    }
2760b57cec5SDimitry Andric    return __r;
2770b57cec5SDimitry Andric}
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
2800b57cec5SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type
2810b57cec5SDimitry Andric__count_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
2820b57cec5SDimitry Andric{
2830b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _It;
2840b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
2850b57cec5SDimitry Andric    typedef typename _It::difference_type difference_type;
2860b57cec5SDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
2870b57cec5SDimitry Andric    difference_type __r = 0;
2880b57cec5SDimitry Andric    // do first partial word
2890b57cec5SDimitry Andric    if (__first.__ctz_ != 0)
2900b57cec5SDimitry Andric    {
2910b57cec5SDimitry Andric        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
2920b57cec5SDimitry Andric        __storage_type __dn = _VSTD::min(__clz_f, __n);
2930b57cec5SDimitry Andric        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
2940b57cec5SDimitry Andric        __r = _VSTD::__libcpp_popcount(~*__first.__seg_ & __m);
2950b57cec5SDimitry Andric        __n -= __dn;
2960b57cec5SDimitry Andric        ++__first.__seg_;
2970b57cec5SDimitry Andric    }
2980b57cec5SDimitry Andric    // do middle whole words
2990b57cec5SDimitry Andric    for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
3000b57cec5SDimitry Andric        __r += _VSTD::__libcpp_popcount(~*__first.__seg_);
3010b57cec5SDimitry Andric    // do last partial word
3020b57cec5SDimitry Andric    if (__n > 0)
3030b57cec5SDimitry Andric    {
3040b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3050b57cec5SDimitry Andric        __r += _VSTD::__libcpp_popcount(~*__first.__seg_ & __m);
3060b57cec5SDimitry Andric    }
3070b57cec5SDimitry Andric    return __r;
3080b57cec5SDimitry Andric}
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, class _Tp>
3110b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3120b57cec5SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type
3130b57cec5SDimitry Andriccount(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value_)
3140b57cec5SDimitry Andric{
3150b57cec5SDimitry Andric    if (static_cast<bool>(__value_))
316e8d8bef9SDimitry Andric        return _VSTD::__count_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
317e8d8bef9SDimitry Andric    return _VSTD::__count_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
3180b57cec5SDimitry Andric}
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric// fill_n
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andrictemplate <class _Cp>
3230b57cec5SDimitry Andricvoid
3240b57cec5SDimitry Andric__fill_n_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
3250b57cec5SDimitry Andric{
3260b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, false> _It;
3270b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
3280b57cec5SDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
3290b57cec5SDimitry Andric    // do first partial word
3300b57cec5SDimitry Andric    if (__first.__ctz_ != 0)
3310b57cec5SDimitry Andric    {
3320b57cec5SDimitry Andric        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
3330b57cec5SDimitry Andric        __storage_type __dn = _VSTD::min(__clz_f, __n);
3340b57cec5SDimitry Andric        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
3350b57cec5SDimitry Andric        *__first.__seg_ &= ~__m;
3360b57cec5SDimitry Andric        __n -= __dn;
3370b57cec5SDimitry Andric        ++__first.__seg_;
3380b57cec5SDimitry Andric    }
3390b57cec5SDimitry Andric    // do middle whole words
3400b57cec5SDimitry Andric    __storage_type __nw = __n / __bits_per_word;
341480093f4SDimitry Andric    _VSTD::memset(_VSTD::__to_address(__first.__seg_), 0, __nw * sizeof(__storage_type));
3420b57cec5SDimitry Andric    __n -= __nw * __bits_per_word;
3430b57cec5SDimitry Andric    // do last partial word
3440b57cec5SDimitry Andric    if (__n > 0)
3450b57cec5SDimitry Andric    {
3460b57cec5SDimitry Andric        __first.__seg_ += __nw;
3470b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3480b57cec5SDimitry Andric        *__first.__seg_ &= ~__m;
3490b57cec5SDimitry Andric    }
3500b57cec5SDimitry Andric}
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andrictemplate <class _Cp>
3530b57cec5SDimitry Andricvoid
3540b57cec5SDimitry Andric__fill_n_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
3550b57cec5SDimitry Andric{
3560b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, false> _It;
3570b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
3580b57cec5SDimitry Andric    const int __bits_per_word = _It::__bits_per_word;
3590b57cec5SDimitry Andric    // do first partial word
3600b57cec5SDimitry Andric    if (__first.__ctz_ != 0)
3610b57cec5SDimitry Andric    {
3620b57cec5SDimitry Andric        __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
3630b57cec5SDimitry Andric        __storage_type __dn = _VSTD::min(__clz_f, __n);
3640b57cec5SDimitry Andric        __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
3650b57cec5SDimitry Andric        *__first.__seg_ |= __m;
3660b57cec5SDimitry Andric        __n -= __dn;
3670b57cec5SDimitry Andric        ++__first.__seg_;
3680b57cec5SDimitry Andric    }
3690b57cec5SDimitry Andric    // do middle whole words
3700b57cec5SDimitry Andric    __storage_type __nw = __n / __bits_per_word;
371480093f4SDimitry Andric    _VSTD::memset(_VSTD::__to_address(__first.__seg_), -1, __nw * sizeof(__storage_type));
3720b57cec5SDimitry Andric    __n -= __nw * __bits_per_word;
3730b57cec5SDimitry Andric    // do last partial word
3740b57cec5SDimitry Andric    if (__n > 0)
3750b57cec5SDimitry Andric    {
3760b57cec5SDimitry Andric        __first.__seg_ += __nw;
3770b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
3780b57cec5SDimitry Andric        *__first.__seg_ |= __m;
3790b57cec5SDimitry Andric    }
3800b57cec5SDimitry Andric}
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andrictemplate <class _Cp>
3830b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
3840b57cec5SDimitry Andricvoid
3850b57cec5SDimitry Andricfill_n(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __value_)
3860b57cec5SDimitry Andric{
3870b57cec5SDimitry Andric    if (__n > 0)
3880b57cec5SDimitry Andric    {
3890b57cec5SDimitry Andric        if (__value_)
390e8d8bef9SDimitry Andric            _VSTD::__fill_n_true(__first, __n);
3910b57cec5SDimitry Andric        else
392e8d8bef9SDimitry Andric            _VSTD::__fill_n_false(__first, __n);
3930b57cec5SDimitry Andric    }
3940b57cec5SDimitry Andric}
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric// fill
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andrictemplate <class _Cp>
3990b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
4000b57cec5SDimitry Andricvoid
4010b57cec5SDimitry Andricfill(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, bool __value_)
4020b57cec5SDimitry Andric{
4030b57cec5SDimitry Andric    _VSTD::fill_n(__first, static_cast<typename _Cp::size_type>(__last - __first), __value_);
4040b57cec5SDimitry Andric}
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric// copy
4070b57cec5SDimitry Andric
4080b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
4090b57cec5SDimitry Andric__bit_iterator<_Cp, false>
4100b57cec5SDimitry Andric__copy_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
4110b57cec5SDimitry Andric                                                     __bit_iterator<_Cp, false> __result)
4120b57cec5SDimitry Andric{
4130b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _In;
4140b57cec5SDimitry Andric    typedef  typename _In::difference_type difference_type;
4150b57cec5SDimitry Andric    typedef typename _In::__storage_type __storage_type;
4160b57cec5SDimitry Andric    const int __bits_per_word = _In::__bits_per_word;
4170b57cec5SDimitry Andric    difference_type __n = __last - __first;
4180b57cec5SDimitry Andric    if (__n > 0)
4190b57cec5SDimitry Andric    {
4200b57cec5SDimitry Andric        // do first word
4210b57cec5SDimitry Andric        if (__first.__ctz_ != 0)
4220b57cec5SDimitry Andric        {
4230b57cec5SDimitry Andric            unsigned __clz = __bits_per_word - __first.__ctz_;
4240b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
4250b57cec5SDimitry Andric            __n -= __dn;
4260b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
4270b57cec5SDimitry Andric            __storage_type __b = *__first.__seg_ & __m;
4280b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
4290b57cec5SDimitry Andric            *__result.__seg_ |= __b;
4300b57cec5SDimitry Andric            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
4310b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
4320b57cec5SDimitry Andric            ++__first.__seg_;
4330b57cec5SDimitry Andric            // __first.__ctz_ = 0;
4340b57cec5SDimitry Andric        }
4350b57cec5SDimitry Andric        // __first.__ctz_ == 0;
4360b57cec5SDimitry Andric        // do middle words
4370b57cec5SDimitry Andric        __storage_type __nw = __n / __bits_per_word;
438480093f4SDimitry Andric        _VSTD::memmove(_VSTD::__to_address(__result.__seg_),
439480093f4SDimitry Andric                       _VSTD::__to_address(__first.__seg_),
4400b57cec5SDimitry Andric                       __nw * sizeof(__storage_type));
4410b57cec5SDimitry Andric        __n -= __nw * __bits_per_word;
4420b57cec5SDimitry Andric        __result.__seg_ += __nw;
4430b57cec5SDimitry Andric        // do last word
4440b57cec5SDimitry Andric        if (__n > 0)
4450b57cec5SDimitry Andric        {
4460b57cec5SDimitry Andric            __first.__seg_ += __nw;
4470b57cec5SDimitry Andric            __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
4480b57cec5SDimitry Andric            __storage_type __b = *__first.__seg_ & __m;
4490b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
4500b57cec5SDimitry Andric            *__result.__seg_ |= __b;
4510b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>(__n);
4520b57cec5SDimitry Andric        }
4530b57cec5SDimitry Andric    }
4540b57cec5SDimitry Andric    return __result;
4550b57cec5SDimitry Andric}
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
4580b57cec5SDimitry Andric__bit_iterator<_Cp, false>
4590b57cec5SDimitry Andric__copy_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
4600b57cec5SDimitry Andric                                                       __bit_iterator<_Cp, false> __result)
4610b57cec5SDimitry Andric{
4620b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _In;
4630b57cec5SDimitry Andric    typedef  typename _In::difference_type difference_type;
4640b57cec5SDimitry Andric    typedef typename _In::__storage_type __storage_type;
4650b57cec5SDimitry Andric    static const int __bits_per_word = _In::__bits_per_word;
4660b57cec5SDimitry Andric    difference_type __n = __last - __first;
4670b57cec5SDimitry Andric    if (__n > 0)
4680b57cec5SDimitry Andric    {
4690b57cec5SDimitry Andric        // do first word
4700b57cec5SDimitry Andric        if (__first.__ctz_ != 0)
4710b57cec5SDimitry Andric        {
4720b57cec5SDimitry Andric            unsigned __clz_f = __bits_per_word - __first.__ctz_;
4730b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
4740b57cec5SDimitry Andric            __n -= __dn;
4750b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
4760b57cec5SDimitry Andric            __storage_type __b = *__first.__seg_ & __m;
4770b57cec5SDimitry Andric            unsigned __clz_r = __bits_per_word - __result.__ctz_;
4780b57cec5SDimitry Andric            __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
4790b57cec5SDimitry Andric            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
4800b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
4810b57cec5SDimitry Andric            if (__result.__ctz_ > __first.__ctz_)
4820b57cec5SDimitry Andric                *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_);
4830b57cec5SDimitry Andric            else
4840b57cec5SDimitry Andric                *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_);
4850b57cec5SDimitry Andric            __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
4860b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_)  % __bits_per_word);
4870b57cec5SDimitry Andric            __dn -= __ddn;
4880b57cec5SDimitry Andric            if (__dn > 0)
4890b57cec5SDimitry Andric            {
4900b57cec5SDimitry Andric                __m = ~__storage_type(0) >> (__bits_per_word - __dn);
4910b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
4920b57cec5SDimitry Andric                *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn);
4930b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(__dn);
4940b57cec5SDimitry Andric            }
4950b57cec5SDimitry Andric            ++__first.__seg_;
4960b57cec5SDimitry Andric            // __first.__ctz_ = 0;
4970b57cec5SDimitry Andric        }
4980b57cec5SDimitry Andric        // __first.__ctz_ == 0;
4990b57cec5SDimitry Andric        // do middle words
5000b57cec5SDimitry Andric        unsigned __clz_r = __bits_per_word - __result.__ctz_;
5010b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) << __result.__ctz_;
5020b57cec5SDimitry Andric        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
5030b57cec5SDimitry Andric        {
5040b57cec5SDimitry Andric            __storage_type __b = *__first.__seg_;
5050b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
5060b57cec5SDimitry Andric            *__result.__seg_ |= __b << __result.__ctz_;
5070b57cec5SDimitry Andric            ++__result.__seg_;
5080b57cec5SDimitry Andric            *__result.__seg_ &= __m;
5090b57cec5SDimitry Andric            *__result.__seg_ |= __b >> __clz_r;
5100b57cec5SDimitry Andric        }
5110b57cec5SDimitry Andric        // do last word
5120b57cec5SDimitry Andric        if (__n > 0)
5130b57cec5SDimitry Andric        {
5140b57cec5SDimitry Andric            __m = ~__storage_type(0) >> (__bits_per_word - __n);
5150b57cec5SDimitry Andric            __storage_type __b = *__first.__seg_ & __m;
5160b57cec5SDimitry Andric            __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
5170b57cec5SDimitry Andric            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
5180b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
5190b57cec5SDimitry Andric            *__result.__seg_ |= __b << __result.__ctz_;
5200b57cec5SDimitry Andric            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
5210b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
5220b57cec5SDimitry Andric            __n -= __dn;
5230b57cec5SDimitry Andric            if (__n > 0)
5240b57cec5SDimitry Andric            {
5250b57cec5SDimitry Andric                __m = ~__storage_type(0) >> (__bits_per_word - __n);
5260b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
5270b57cec5SDimitry Andric                *__result.__seg_ |= __b >> __dn;
5280b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(__n);
5290b57cec5SDimitry Andric            }
5300b57cec5SDimitry Andric        }
5310b57cec5SDimitry Andric    }
5320b57cec5SDimitry Andric    return __result;
5330b57cec5SDimitry Andric}
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
5360b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5370b57cec5SDimitry Andric__bit_iterator<_Cp, false>
5380b57cec5SDimitry Andriccopy(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
5390b57cec5SDimitry Andric{
5400b57cec5SDimitry Andric    if (__first.__ctz_ == __result.__ctz_)
541e8d8bef9SDimitry Andric        return _VSTD::__copy_aligned(__first, __last, __result);
542e8d8bef9SDimitry Andric    return _VSTD::__copy_unaligned(__first, __last, __result);
5430b57cec5SDimitry Andric}
5440b57cec5SDimitry Andric
5450b57cec5SDimitry Andric// copy_backward
5460b57cec5SDimitry Andric
5470b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
5480b57cec5SDimitry Andric__bit_iterator<_Cp, false>
5490b57cec5SDimitry Andric__copy_backward_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
5500b57cec5SDimitry Andric                                                     __bit_iterator<_Cp, false> __result)
5510b57cec5SDimitry Andric{
5520b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _In;
5530b57cec5SDimitry Andric    typedef  typename _In::difference_type difference_type;
5540b57cec5SDimitry Andric    typedef typename _In::__storage_type __storage_type;
5550b57cec5SDimitry Andric    const int __bits_per_word = _In::__bits_per_word;
5560b57cec5SDimitry Andric    difference_type __n = __last - __first;
5570b57cec5SDimitry Andric    if (__n > 0)
5580b57cec5SDimitry Andric    {
5590b57cec5SDimitry Andric        // do first word
5600b57cec5SDimitry Andric        if (__last.__ctz_ != 0)
5610b57cec5SDimitry Andric        {
5620b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
5630b57cec5SDimitry Andric            __n -= __dn;
5640b57cec5SDimitry Andric            unsigned __clz = __bits_per_word - __last.__ctz_;
5650b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz);
5660b57cec5SDimitry Andric            __storage_type __b = *__last.__seg_ & __m;
5670b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
5680b57cec5SDimitry Andric            *__result.__seg_ |= __b;
5690b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
5700b57cec5SDimitry Andric                                                       __result.__ctz_)  % __bits_per_word);
5710b57cec5SDimitry Andric            // __last.__ctz_ = 0
5720b57cec5SDimitry Andric         }
5730b57cec5SDimitry Andric        // __last.__ctz_ == 0 || __n == 0
5740b57cec5SDimitry Andric        // __result.__ctz_ == 0 || __n == 0
5750b57cec5SDimitry Andric        // do middle words
5760b57cec5SDimitry Andric        __storage_type __nw = __n / __bits_per_word;
5770b57cec5SDimitry Andric        __result.__seg_ -= __nw;
5780b57cec5SDimitry Andric        __last.__seg_ -= __nw;
579480093f4SDimitry Andric        _VSTD::memmove(_VSTD::__to_address(__result.__seg_),
580480093f4SDimitry Andric                       _VSTD::__to_address(__last.__seg_),
5810b57cec5SDimitry Andric                       __nw * sizeof(__storage_type));
5820b57cec5SDimitry Andric        __n -= __nw * __bits_per_word;
5830b57cec5SDimitry Andric        // do last word
5840b57cec5SDimitry Andric        if (__n > 0)
5850b57cec5SDimitry Andric        {
5860b57cec5SDimitry Andric            __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n);
5870b57cec5SDimitry Andric            __storage_type __b = *--__last.__seg_ & __m;
5880b57cec5SDimitry Andric            *--__result.__seg_ &= ~__m;
5890b57cec5SDimitry Andric            *__result.__seg_ |= __b;
5900b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
5910b57cec5SDimitry Andric        }
5920b57cec5SDimitry Andric    }
5930b57cec5SDimitry Andric    return __result;
5940b57cec5SDimitry Andric}
5950b57cec5SDimitry Andric
5960b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
5970b57cec5SDimitry Andric__bit_iterator<_Cp, false>
5980b57cec5SDimitry Andric__copy_backward_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
5990b57cec5SDimitry Andric                                                       __bit_iterator<_Cp, false> __result)
6000b57cec5SDimitry Andric{
6010b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IsConst> _In;
6020b57cec5SDimitry Andric    typedef  typename _In::difference_type difference_type;
6030b57cec5SDimitry Andric    typedef typename _In::__storage_type __storage_type;
6040b57cec5SDimitry Andric    const int __bits_per_word = _In::__bits_per_word;
6050b57cec5SDimitry Andric    difference_type __n = __last - __first;
6060b57cec5SDimitry Andric    if (__n > 0)
6070b57cec5SDimitry Andric    {
6080b57cec5SDimitry Andric        // do first word
6090b57cec5SDimitry Andric        if (__last.__ctz_ != 0)
6100b57cec5SDimitry Andric        {
6110b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
6120b57cec5SDimitry Andric            __n -= __dn;
6130b57cec5SDimitry Andric            unsigned __clz_l = __bits_per_word - __last.__ctz_;
6140b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l);
6150b57cec5SDimitry Andric            __storage_type __b = *__last.__seg_ & __m;
6160b57cec5SDimitry Andric            unsigned __clz_r = __bits_per_word - __result.__ctz_;
6170b57cec5SDimitry Andric            __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_));
6180b57cec5SDimitry Andric            if (__ddn > 0)
6190b57cec5SDimitry Andric            {
6200b57cec5SDimitry Andric                __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r);
6210b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
6220b57cec5SDimitry Andric                if (__result.__ctz_ > __last.__ctz_)
6230b57cec5SDimitry Andric                    *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
6240b57cec5SDimitry Andric                else
6250b57cec5SDimitry Andric                    *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_);
6260b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) +
6270b57cec5SDimitry Andric                                                         __result.__ctz_)  % __bits_per_word);
6280b57cec5SDimitry Andric                __dn -= __ddn;
6290b57cec5SDimitry Andric            }
6300b57cec5SDimitry Andric            if (__dn > 0)
6310b57cec5SDimitry Andric            {
6320b57cec5SDimitry Andric                // __result.__ctz_ == 0
6330b57cec5SDimitry Andric                --__result.__seg_;
6340b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1));
6350b57cec5SDimitry Andric                __m = ~__storage_type(0) << __result.__ctz_;
6360b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
6370b57cec5SDimitry Andric                __last.__ctz_ -= __dn + __ddn;
6380b57cec5SDimitry Andric                *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
6390b57cec5SDimitry Andric            }
6400b57cec5SDimitry Andric            // __last.__ctz_ = 0
6410b57cec5SDimitry Andric         }
6420b57cec5SDimitry Andric        // __last.__ctz_ == 0 || __n == 0
6430b57cec5SDimitry Andric        // __result.__ctz_ != 0 || __n == 0
6440b57cec5SDimitry Andric        // do middle words
6450b57cec5SDimitry Andric        unsigned __clz_r = __bits_per_word - __result.__ctz_;
6460b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) >> __clz_r;
6470b57cec5SDimitry Andric        for (; __n >= __bits_per_word; __n -= __bits_per_word)
6480b57cec5SDimitry Andric        {
6490b57cec5SDimitry Andric            __storage_type __b = *--__last.__seg_;
6500b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
6510b57cec5SDimitry Andric            *__result.__seg_ |= __b >> __clz_r;
6520b57cec5SDimitry Andric            *--__result.__seg_ &= __m;
6530b57cec5SDimitry Andric            *__result.__seg_ |= __b << __result.__ctz_;
6540b57cec5SDimitry Andric        }
6550b57cec5SDimitry Andric        // do last word
6560b57cec5SDimitry Andric        if (__n > 0)
6570b57cec5SDimitry Andric        {
6580b57cec5SDimitry Andric            __m = ~__storage_type(0) << (__bits_per_word - __n);
6590b57cec5SDimitry Andric            __storage_type __b = *--__last.__seg_ & __m;
6600b57cec5SDimitry Andric            __clz_r = __bits_per_word - __result.__ctz_;
6610b57cec5SDimitry Andric            __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_));
6620b57cec5SDimitry Andric            __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r);
6630b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
6640b57cec5SDimitry Andric            *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_);
6650b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
6660b57cec5SDimitry Andric                                                     __result.__ctz_)  % __bits_per_word);
6670b57cec5SDimitry Andric            __n -= __dn;
6680b57cec5SDimitry Andric            if (__n > 0)
6690b57cec5SDimitry Andric            {
6700b57cec5SDimitry Andric                // __result.__ctz_ == 0
6710b57cec5SDimitry Andric                --__result.__seg_;
6720b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
6730b57cec5SDimitry Andric                __m = ~__storage_type(0) << __result.__ctz_;
6740b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
6750b57cec5SDimitry Andric                *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn));
6760b57cec5SDimitry Andric            }
6770b57cec5SDimitry Andric        }
6780b57cec5SDimitry Andric    }
6790b57cec5SDimitry Andric    return __result;
6800b57cec5SDimitry Andric}
6810b57cec5SDimitry Andric
6820b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
6830b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
6840b57cec5SDimitry Andric__bit_iterator<_Cp, false>
6850b57cec5SDimitry Andriccopy_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
6860b57cec5SDimitry Andric{
6870b57cec5SDimitry Andric    if (__last.__ctz_ == __result.__ctz_)
688e8d8bef9SDimitry Andric        return _VSTD::__copy_backward_aligned(__first, __last, __result);
689e8d8bef9SDimitry Andric    return _VSTD::__copy_backward_unaligned(__first, __last, __result);
6900b57cec5SDimitry Andric}
6910b57cec5SDimitry Andric
6920b57cec5SDimitry Andric// move
6930b57cec5SDimitry Andric
6940b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
6950b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
6960b57cec5SDimitry Andric__bit_iterator<_Cp, false>
6970b57cec5SDimitry Andricmove(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
6980b57cec5SDimitry Andric{
6990b57cec5SDimitry Andric    return _VSTD::copy(__first, __last, __result);
7000b57cec5SDimitry Andric}
7010b57cec5SDimitry Andric
7020b57cec5SDimitry Andric// move_backward
7030b57cec5SDimitry Andric
7040b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst>
7050b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
7060b57cec5SDimitry Andric__bit_iterator<_Cp, false>
7070b57cec5SDimitry Andricmove_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
7080b57cec5SDimitry Andric{
7090b57cec5SDimitry Andric    return _VSTD::copy_backward(__first, __last, __result);
7100b57cec5SDimitry Andric}
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric// swap_ranges
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andrictemplate <class __C1, class __C2>
7150b57cec5SDimitry Andric__bit_iterator<__C2, false>
7160b57cec5SDimitry Andric__swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
7170b57cec5SDimitry Andric                      __bit_iterator<__C2, false> __result)
7180b57cec5SDimitry Andric{
7190b57cec5SDimitry Andric    typedef __bit_iterator<__C1, false> _I1;
7200b57cec5SDimitry Andric    typedef  typename _I1::difference_type difference_type;
7210b57cec5SDimitry Andric    typedef typename _I1::__storage_type __storage_type;
7220b57cec5SDimitry Andric    const int __bits_per_word = _I1::__bits_per_word;
7230b57cec5SDimitry Andric    difference_type __n = __last - __first;
7240b57cec5SDimitry Andric    if (__n > 0)
7250b57cec5SDimitry Andric    {
7260b57cec5SDimitry Andric        // do first word
7270b57cec5SDimitry Andric        if (__first.__ctz_ != 0)
7280b57cec5SDimitry Andric        {
7290b57cec5SDimitry Andric            unsigned __clz = __bits_per_word - __first.__ctz_;
7300b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
7310b57cec5SDimitry Andric            __n -= __dn;
7320b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
7330b57cec5SDimitry Andric            __storage_type __b1 = *__first.__seg_ & __m;
7340b57cec5SDimitry Andric            *__first.__seg_ &= ~__m;
7350b57cec5SDimitry Andric            __storage_type __b2 = *__result.__seg_ & __m;
7360b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
7370b57cec5SDimitry Andric            *__result.__seg_ |= __b1;
7380b57cec5SDimitry Andric            *__first.__seg_  |= __b2;
7390b57cec5SDimitry Andric            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
7400b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
7410b57cec5SDimitry Andric            ++__first.__seg_;
7420b57cec5SDimitry Andric            // __first.__ctz_ = 0;
7430b57cec5SDimitry Andric        }
7440b57cec5SDimitry Andric        // __first.__ctz_ == 0;
7450b57cec5SDimitry Andric        // do middle words
7460b57cec5SDimitry Andric        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_)
7470b57cec5SDimitry Andric            swap(*__first.__seg_, *__result.__seg_);
7480b57cec5SDimitry Andric        // do last word
7490b57cec5SDimitry Andric        if (__n > 0)
7500b57cec5SDimitry Andric        {
7510b57cec5SDimitry Andric            __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
7520b57cec5SDimitry Andric            __storage_type __b1 = *__first.__seg_ & __m;
7530b57cec5SDimitry Andric            *__first.__seg_ &= ~__m;
7540b57cec5SDimitry Andric            __storage_type __b2 = *__result.__seg_ & __m;
7550b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
7560b57cec5SDimitry Andric            *__result.__seg_ |= __b1;
7570b57cec5SDimitry Andric            *__first.__seg_  |= __b2;
7580b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>(__n);
7590b57cec5SDimitry Andric        }
7600b57cec5SDimitry Andric    }
7610b57cec5SDimitry Andric    return __result;
7620b57cec5SDimitry Andric}
7630b57cec5SDimitry Andric
7640b57cec5SDimitry Andrictemplate <class __C1, class __C2>
7650b57cec5SDimitry Andric__bit_iterator<__C2, false>
7660b57cec5SDimitry Andric__swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
7670b57cec5SDimitry Andric                        __bit_iterator<__C2, false> __result)
7680b57cec5SDimitry Andric{
7690b57cec5SDimitry Andric    typedef __bit_iterator<__C1, false> _I1;
7700b57cec5SDimitry Andric    typedef  typename _I1::difference_type difference_type;
7710b57cec5SDimitry Andric    typedef typename _I1::__storage_type __storage_type;
7720b57cec5SDimitry Andric    const int __bits_per_word = _I1::__bits_per_word;
7730b57cec5SDimitry Andric    difference_type __n = __last - __first;
7740b57cec5SDimitry Andric    if (__n > 0)
7750b57cec5SDimitry Andric    {
7760b57cec5SDimitry Andric        // do first word
7770b57cec5SDimitry Andric        if (__first.__ctz_ != 0)
7780b57cec5SDimitry Andric        {
7790b57cec5SDimitry Andric            unsigned __clz_f = __bits_per_word - __first.__ctz_;
7800b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
7810b57cec5SDimitry Andric            __n -= __dn;
7820b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
7830b57cec5SDimitry Andric            __storage_type __b1 = *__first.__seg_ & __m;
7840b57cec5SDimitry Andric            *__first.__seg_ &= ~__m;
7850b57cec5SDimitry Andric            unsigned __clz_r = __bits_per_word - __result.__ctz_;
7860b57cec5SDimitry Andric            __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
7870b57cec5SDimitry Andric            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
7880b57cec5SDimitry Andric            __storage_type __b2 = *__result.__seg_ & __m;
7890b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
7900b57cec5SDimitry Andric            if (__result.__ctz_ > __first.__ctz_)
7910b57cec5SDimitry Andric            {
7920b57cec5SDimitry Andric                unsigned __s = __result.__ctz_ - __first.__ctz_;
7930b57cec5SDimitry Andric                *__result.__seg_ |= __b1 << __s;
7940b57cec5SDimitry Andric                *__first.__seg_  |= __b2 >> __s;
7950b57cec5SDimitry Andric            }
7960b57cec5SDimitry Andric            else
7970b57cec5SDimitry Andric            {
7980b57cec5SDimitry Andric                unsigned __s = __first.__ctz_ - __result.__ctz_;
7990b57cec5SDimitry Andric                *__result.__seg_ |= __b1 >> __s;
8000b57cec5SDimitry Andric                *__first.__seg_  |= __b2 << __s;
8010b57cec5SDimitry Andric            }
8020b57cec5SDimitry Andric            __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
8030b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_)  % __bits_per_word);
8040b57cec5SDimitry Andric            __dn -= __ddn;
8050b57cec5SDimitry Andric            if (__dn > 0)
8060b57cec5SDimitry Andric            {
8070b57cec5SDimitry Andric                __m = ~__storage_type(0) >> (__bits_per_word - __dn);
8080b57cec5SDimitry Andric                __b2 = *__result.__seg_ & __m;
8090b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
8100b57cec5SDimitry Andric                unsigned __s = __first.__ctz_ + __ddn;
8110b57cec5SDimitry Andric                *__result.__seg_ |= __b1 >> __s;
8120b57cec5SDimitry Andric                *__first.__seg_  |= __b2 << __s;
8130b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(__dn);
8140b57cec5SDimitry Andric            }
8150b57cec5SDimitry Andric            ++__first.__seg_;
8160b57cec5SDimitry Andric            // __first.__ctz_ = 0;
8170b57cec5SDimitry Andric        }
8180b57cec5SDimitry Andric        // __first.__ctz_ == 0;
8190b57cec5SDimitry Andric        // do middle words
8200b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) << __result.__ctz_;
8210b57cec5SDimitry Andric        unsigned __clz_r = __bits_per_word - __result.__ctz_;
8220b57cec5SDimitry Andric        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
8230b57cec5SDimitry Andric        {
8240b57cec5SDimitry Andric            __storage_type __b1 = *__first.__seg_;
8250b57cec5SDimitry Andric            __storage_type __b2 = *__result.__seg_ & __m;
8260b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
8270b57cec5SDimitry Andric            *__result.__seg_ |= __b1 << __result.__ctz_;
8280b57cec5SDimitry Andric            *__first.__seg_  = __b2 >> __result.__ctz_;
8290b57cec5SDimitry Andric            ++__result.__seg_;
8300b57cec5SDimitry Andric            __b2 = *__result.__seg_ & ~__m;
8310b57cec5SDimitry Andric            *__result.__seg_ &= __m;
8320b57cec5SDimitry Andric            *__result.__seg_ |= __b1 >> __clz_r;
8330b57cec5SDimitry Andric            *__first.__seg_  |= __b2 << __clz_r;
8340b57cec5SDimitry Andric        }
8350b57cec5SDimitry Andric        // do last word
8360b57cec5SDimitry Andric        if (__n > 0)
8370b57cec5SDimitry Andric        {
8380b57cec5SDimitry Andric            __m = ~__storage_type(0) >> (__bits_per_word - __n);
8390b57cec5SDimitry Andric            __storage_type __b1 = *__first.__seg_ & __m;
8400b57cec5SDimitry Andric            *__first.__seg_ &= ~__m;
8410b57cec5SDimitry Andric            __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r);
8420b57cec5SDimitry Andric            __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
8430b57cec5SDimitry Andric            __storage_type __b2 = *__result.__seg_ & __m;
8440b57cec5SDimitry Andric            *__result.__seg_ &= ~__m;
8450b57cec5SDimitry Andric            *__result.__seg_ |= __b1 << __result.__ctz_;
8460b57cec5SDimitry Andric            *__first.__seg_  |= __b2 >> __result.__ctz_;
8470b57cec5SDimitry Andric            __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
8480b57cec5SDimitry Andric            __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_)  % __bits_per_word);
8490b57cec5SDimitry Andric            __n -= __dn;
8500b57cec5SDimitry Andric            if (__n > 0)
8510b57cec5SDimitry Andric            {
8520b57cec5SDimitry Andric                __m = ~__storage_type(0) >> (__bits_per_word - __n);
8530b57cec5SDimitry Andric                __b2 = *__result.__seg_ & __m;
8540b57cec5SDimitry Andric                *__result.__seg_ &= ~__m;
8550b57cec5SDimitry Andric                *__result.__seg_ |= __b1 >> __dn;
8560b57cec5SDimitry Andric                *__first.__seg_  |= __b2 << __dn;
8570b57cec5SDimitry Andric                __result.__ctz_ = static_cast<unsigned>(__n);
8580b57cec5SDimitry Andric            }
8590b57cec5SDimitry Andric        }
8600b57cec5SDimitry Andric    }
8610b57cec5SDimitry Andric    return __result;
8620b57cec5SDimitry Andric}
8630b57cec5SDimitry Andric
8640b57cec5SDimitry Andrictemplate <class __C1, class __C2>
8650b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8660b57cec5SDimitry Andric__bit_iterator<__C2, false>
8670b57cec5SDimitry Andricswap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1,
8680b57cec5SDimitry Andric            __bit_iterator<__C2, false> __first2)
8690b57cec5SDimitry Andric{
8700b57cec5SDimitry Andric    if (__first1.__ctz_ == __first2.__ctz_)
871e8d8bef9SDimitry Andric        return _VSTD::__swap_ranges_aligned(__first1, __last1, __first2);
872e8d8bef9SDimitry Andric    return _VSTD::__swap_ranges_unaligned(__first1, __last1, __first2);
8730b57cec5SDimitry Andric}
8740b57cec5SDimitry Andric
8750b57cec5SDimitry Andric// rotate
8760b57cec5SDimitry Andric
8770b57cec5SDimitry Andrictemplate <class _Cp>
8780b57cec5SDimitry Andricstruct __bit_array
8790b57cec5SDimitry Andric{
8800b57cec5SDimitry Andric    typedef typename _Cp::difference_type difference_type;
8810b57cec5SDimitry Andric    typedef typename _Cp::__storage_type  __storage_type;
8820b57cec5SDimitry Andric    typedef typename _Cp::__storage_pointer __storage_pointer;
8830b57cec5SDimitry Andric    typedef typename _Cp::iterator        iterator;
8840b57cec5SDimitry Andric    static const unsigned __bits_per_word = _Cp::__bits_per_word;
8850b57cec5SDimitry Andric    static const unsigned _Np = 4;
8860b57cec5SDimitry Andric
8870b57cec5SDimitry Andric    difference_type __size_;
8880b57cec5SDimitry Andric    __storage_type __word_[_Np];
8890b57cec5SDimitry Andric
8900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY static difference_type capacity()
8910b57cec5SDimitry Andric        {return static_cast<difference_type>(_Np * __bits_per_word);}
8920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit __bit_array(difference_type __s) : __size_(__s) {}
8930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY iterator begin()
8940b57cec5SDimitry Andric    {
8950b57cec5SDimitry Andric        return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
8960b57cec5SDimitry Andric    }
8970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY iterator end()
8980b57cec5SDimitry Andric    {
8990b57cec5SDimitry Andric        return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
9000b57cec5SDimitry Andric                                                  static_cast<unsigned>(__size_ % __bits_per_word));
9010b57cec5SDimitry Andric    }
9020b57cec5SDimitry Andric};
9030b57cec5SDimitry Andric
9040b57cec5SDimitry Andrictemplate <class _Cp>
9050b57cec5SDimitry Andric__bit_iterator<_Cp, false>
9060b57cec5SDimitry Andricrotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last)
9070b57cec5SDimitry Andric{
9080b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, false> _I1;
9090b57cec5SDimitry Andric    typedef  typename _I1::difference_type difference_type;
9100b57cec5SDimitry Andric    difference_type __d1 = __middle - __first;
9110b57cec5SDimitry Andric    difference_type __d2 = __last - __middle;
9120b57cec5SDimitry Andric    _I1 __r = __first + __d2;
9130b57cec5SDimitry Andric    while (__d1 != 0 && __d2 != 0)
9140b57cec5SDimitry Andric    {
9150b57cec5SDimitry Andric        if (__d1 <= __d2)
9160b57cec5SDimitry Andric        {
9170b57cec5SDimitry Andric            if (__d1 <= __bit_array<_Cp>::capacity())
9180b57cec5SDimitry Andric            {
9190b57cec5SDimitry Andric                __bit_array<_Cp> __b(__d1);
9200b57cec5SDimitry Andric                _VSTD::copy(__first, __middle, __b.begin());
9210b57cec5SDimitry Andric                _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first));
9220b57cec5SDimitry Andric                break;
9230b57cec5SDimitry Andric            }
9240b57cec5SDimitry Andric            else
9250b57cec5SDimitry Andric            {
9260b57cec5SDimitry Andric                __bit_iterator<_Cp, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle);
9270b57cec5SDimitry Andric                __first = __middle;
9280b57cec5SDimitry Andric                __middle = __mp;
9290b57cec5SDimitry Andric                __d2 -= __d1;
9300b57cec5SDimitry Andric            }
9310b57cec5SDimitry Andric        }
9320b57cec5SDimitry Andric        else
9330b57cec5SDimitry Andric        {
9340b57cec5SDimitry Andric            if (__d2 <= __bit_array<_Cp>::capacity())
9350b57cec5SDimitry Andric            {
9360b57cec5SDimitry Andric                __bit_array<_Cp> __b(__d2);
9370b57cec5SDimitry Andric                _VSTD::copy(__middle, __last, __b.begin());
9380b57cec5SDimitry Andric                _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last));
9390b57cec5SDimitry Andric                break;
9400b57cec5SDimitry Andric            }
9410b57cec5SDimitry Andric            else
9420b57cec5SDimitry Andric            {
9430b57cec5SDimitry Andric                __bit_iterator<_Cp, false> __mp = __first + __d2;
9440b57cec5SDimitry Andric                _VSTD::swap_ranges(__first, __mp, __middle);
9450b57cec5SDimitry Andric                __first = __mp;
9460b57cec5SDimitry Andric                __d1 -= __d2;
9470b57cec5SDimitry Andric            }
9480b57cec5SDimitry Andric        }
9490b57cec5SDimitry Andric    }
9500b57cec5SDimitry Andric    return __r;
9510b57cec5SDimitry Andric}
9520b57cec5SDimitry Andric
9530b57cec5SDimitry Andric// equal
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2>
9560b57cec5SDimitry Andricbool
9570b57cec5SDimitry Andric__equal_unaligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1,
9580b57cec5SDimitry Andric                  __bit_iterator<_Cp, _IC2> __first2)
9590b57cec5SDimitry Andric{
9600b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IC1> _It;
9610b57cec5SDimitry Andric    typedef  typename _It::difference_type difference_type;
9620b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
9630b57cec5SDimitry Andric    static const int __bits_per_word = _It::__bits_per_word;
9640b57cec5SDimitry Andric    difference_type __n = __last1 - __first1;
9650b57cec5SDimitry Andric    if (__n > 0)
9660b57cec5SDimitry Andric    {
9670b57cec5SDimitry Andric        // do first word
9680b57cec5SDimitry Andric        if (__first1.__ctz_ != 0)
9690b57cec5SDimitry Andric        {
9700b57cec5SDimitry Andric            unsigned __clz_f = __bits_per_word - __first1.__ctz_;
9710b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
9720b57cec5SDimitry Andric            __n -= __dn;
9730b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
9740b57cec5SDimitry Andric            __storage_type __b = *__first1.__seg_ & __m;
9750b57cec5SDimitry Andric            unsigned __clz_r = __bits_per_word - __first2.__ctz_;
9760b57cec5SDimitry Andric            __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
9770b57cec5SDimitry Andric            __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
9780b57cec5SDimitry Andric            if (__first2.__ctz_ > __first1.__ctz_)
9790b57cec5SDimitry Andric            {
9800b57cec5SDimitry Andric                if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_)))
9810b57cec5SDimitry Andric                    return false;
9820b57cec5SDimitry Andric            }
9830b57cec5SDimitry Andric            else
9840b57cec5SDimitry Andric            {
9850b57cec5SDimitry Andric                if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_)))
9860b57cec5SDimitry Andric                    return false;
9870b57cec5SDimitry Andric            }
9880b57cec5SDimitry Andric            __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word;
9890b57cec5SDimitry Andric            __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_)  % __bits_per_word);
9900b57cec5SDimitry Andric            __dn -= __ddn;
9910b57cec5SDimitry Andric            if (__dn > 0)
9920b57cec5SDimitry Andric            {
9930b57cec5SDimitry Andric                __m = ~__storage_type(0) >> (__bits_per_word - __dn);
9940b57cec5SDimitry Andric                if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn)))
9950b57cec5SDimitry Andric                    return false;
9960b57cec5SDimitry Andric                __first2.__ctz_ = static_cast<unsigned>(__dn);
9970b57cec5SDimitry Andric            }
9980b57cec5SDimitry Andric            ++__first1.__seg_;
9990b57cec5SDimitry Andric            // __first1.__ctz_ = 0;
10000b57cec5SDimitry Andric        }
10010b57cec5SDimitry Andric        // __first1.__ctz_ == 0;
10020b57cec5SDimitry Andric        // do middle words
10030b57cec5SDimitry Andric        unsigned __clz_r = __bits_per_word - __first2.__ctz_;
10040b57cec5SDimitry Andric        __storage_type __m = ~__storage_type(0) << __first2.__ctz_;
10050b57cec5SDimitry Andric        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_)
10060b57cec5SDimitry Andric        {
10070b57cec5SDimitry Andric            __storage_type __b = *__first1.__seg_;
10080b57cec5SDimitry Andric            if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
10090b57cec5SDimitry Andric                return false;
10100b57cec5SDimitry Andric            ++__first2.__seg_;
10110b57cec5SDimitry Andric            if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r))
10120b57cec5SDimitry Andric                return false;
10130b57cec5SDimitry Andric        }
10140b57cec5SDimitry Andric        // do last word
10150b57cec5SDimitry Andric        if (__n > 0)
10160b57cec5SDimitry Andric        {
10170b57cec5SDimitry Andric            __m = ~__storage_type(0) >> (__bits_per_word - __n);
10180b57cec5SDimitry Andric            __storage_type __b = *__first1.__seg_ & __m;
10190b57cec5SDimitry Andric            __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
10200b57cec5SDimitry Andric            __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
10210b57cec5SDimitry Andric            if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
10220b57cec5SDimitry Andric                return false;
10230b57cec5SDimitry Andric            __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word;
10240b57cec5SDimitry Andric            __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_)  % __bits_per_word);
10250b57cec5SDimitry Andric            __n -= __dn;
10260b57cec5SDimitry Andric            if (__n > 0)
10270b57cec5SDimitry Andric            {
10280b57cec5SDimitry Andric                __m = ~__storage_type(0) >> (__bits_per_word - __n);
10290b57cec5SDimitry Andric                if ((*__first2.__seg_ & __m) != (__b >> __dn))
10300b57cec5SDimitry Andric                    return false;
10310b57cec5SDimitry Andric            }
10320b57cec5SDimitry Andric        }
10330b57cec5SDimitry Andric    }
10340b57cec5SDimitry Andric    return true;
10350b57cec5SDimitry Andric}
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2>
10380b57cec5SDimitry Andricbool
10390b57cec5SDimitry Andric__equal_aligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1,
10400b57cec5SDimitry Andric                __bit_iterator<_Cp, _IC2> __first2)
10410b57cec5SDimitry Andric{
10420b57cec5SDimitry Andric    typedef __bit_iterator<_Cp, _IC1> _It;
10430b57cec5SDimitry Andric    typedef  typename _It::difference_type difference_type;
10440b57cec5SDimitry Andric    typedef typename _It::__storage_type __storage_type;
10450b57cec5SDimitry Andric    static const int __bits_per_word = _It::__bits_per_word;
10460b57cec5SDimitry Andric    difference_type __n = __last1 - __first1;
10470b57cec5SDimitry Andric    if (__n > 0)
10480b57cec5SDimitry Andric    {
10490b57cec5SDimitry Andric        // do first word
10500b57cec5SDimitry Andric        if (__first1.__ctz_ != 0)
10510b57cec5SDimitry Andric        {
10520b57cec5SDimitry Andric            unsigned __clz = __bits_per_word - __first1.__ctz_;
10530b57cec5SDimitry Andric            difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
10540b57cec5SDimitry Andric            __n -= __dn;
10550b57cec5SDimitry Andric            __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
10560b57cec5SDimitry Andric            if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
10570b57cec5SDimitry Andric                return false;
10580b57cec5SDimitry Andric            ++__first2.__seg_;
10590b57cec5SDimitry Andric            ++__first1.__seg_;
10600b57cec5SDimitry Andric            // __first1.__ctz_ = 0;
10610b57cec5SDimitry Andric            // __first2.__ctz_ = 0;
10620b57cec5SDimitry Andric        }
10630b57cec5SDimitry Andric        // __first1.__ctz_ == 0;
10640b57cec5SDimitry Andric        // __first2.__ctz_ == 0;
10650b57cec5SDimitry Andric        // do middle words
10660b57cec5SDimitry Andric        for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_)
10670b57cec5SDimitry Andric            if (*__first2.__seg_ != *__first1.__seg_)
10680b57cec5SDimitry Andric                return false;
10690b57cec5SDimitry Andric        // do last word
10700b57cec5SDimitry Andric        if (__n > 0)
10710b57cec5SDimitry Andric        {
10720b57cec5SDimitry Andric            __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
10730b57cec5SDimitry Andric            if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
10740b57cec5SDimitry Andric                return false;
10750b57cec5SDimitry Andric        }
10760b57cec5SDimitry Andric    }
10770b57cec5SDimitry Andric    return true;
10780b57cec5SDimitry Andric}
10790b57cec5SDimitry Andric
10800b57cec5SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2>
10810b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
10820b57cec5SDimitry Andricbool
10830b57cec5SDimitry Andricequal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __bit_iterator<_Cp, _IC2> __first2)
10840b57cec5SDimitry Andric{
10850b57cec5SDimitry Andric    if (__first1.__ctz_ == __first2.__ctz_)
1086e8d8bef9SDimitry Andric        return _VSTD::__equal_aligned(__first1, __last1, __first2);
1087e8d8bef9SDimitry Andric    return _VSTD::__equal_unaligned(__first1, __last1, __first2);
10880b57cec5SDimitry Andric}
10890b57cec5SDimitry Andric
10900b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst,
10910b57cec5SDimitry Andric          typename _Cp::__storage_type>
10920b57cec5SDimitry Andricclass __bit_iterator
10930b57cec5SDimitry Andric{
10940b57cec5SDimitry Andricpublic:
10950b57cec5SDimitry Andric    typedef typename _Cp::difference_type                                                          difference_type;
10960b57cec5SDimitry Andric    typedef bool                                                                                  value_type;
10970b57cec5SDimitry Andric    typedef __bit_iterator                                                                        pointer;
10980b57cec5SDimitry Andric    typedef typename conditional<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >::type reference;
10990b57cec5SDimitry Andric    typedef random_access_iterator_tag                                                            iterator_category;
11000b57cec5SDimitry Andric
11010b57cec5SDimitry Andricprivate:
11020b57cec5SDimitry Andric    typedef typename _Cp::__storage_type                                           __storage_type;
11030b57cec5SDimitry Andric    typedef typename conditional<_IsConst, typename _Cp::__const_storage_pointer,
11040b57cec5SDimitry Andric                                           typename _Cp::__storage_pointer>::type  __storage_pointer;
11050b57cec5SDimitry Andric    static const unsigned __bits_per_word = _Cp::__bits_per_word;
11060b57cec5SDimitry Andric
11070b57cec5SDimitry Andric    __storage_pointer __seg_;
11080b57cec5SDimitry Andric    unsigned          __ctz_;
11090b57cec5SDimitry Andric
11100b57cec5SDimitry Andricpublic:
11110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT
11120b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
11130b57cec5SDimitry Andric    : __seg_(nullptr), __ctz_(0)
11140b57cec5SDimitry Andric#endif
11150b57cec5SDimitry Andric    {}
11160b57cec5SDimitry Andric
1117*4652422eSDimitry Andric    // When _IsConst=false, this is the copy constructor.
1118*4652422eSDimitry Andric    // It is non-trivial. Making it trivial would break ABI.
1119*4652422eSDimitry Andric    // When _IsConst=true, this is a converting constructor;
1120*4652422eSDimitry Andric    // the copy and move constructors are implicitly generated
1121*4652422eSDimitry Andric    // and trivial.
11220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1123*4652422eSDimitry Andric    __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
11240b57cec5SDimitry Andric        : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {}
11250b57cec5SDimitry Andric
1126*4652422eSDimitry Andric    // When _IsConst=false, we have a user-provided copy constructor,
1127*4652422eSDimitry Andric    // so we must also provide a copy assignment operator because
1128*4652422eSDimitry Andric    // the implicit generation of a defaulted one is deprecated.
1129*4652422eSDimitry Andric    // When _IsConst=true, the assignment operators are
1130*4652422eSDimitry Andric    // implicitly generated and trivial.
113147395794SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1132*4652422eSDimitry Andric    __bit_iterator& operator=(const _If<_IsConst, struct __private_nat, __bit_iterator>& __it) {
1133*4652422eSDimitry Andric        __seg_ = __it.__seg_;
1134*4652422eSDimitry Andric        __ctz_ = __it.__ctz_;
1135*4652422eSDimitry Andric        return *this;
1136*4652422eSDimitry Andric    }
113747395794SDimitry Andric
11380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
11390b57cec5SDimitry Andric        {return reference(__seg_, __storage_type(1) << __ctz_);}
11400b57cec5SDimitry Andric
11410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator++()
11420b57cec5SDimitry Andric    {
11430b57cec5SDimitry Andric        if (__ctz_ != __bits_per_word-1)
11440b57cec5SDimitry Andric            ++__ctz_;
11450b57cec5SDimitry Andric        else
11460b57cec5SDimitry Andric        {
11470b57cec5SDimitry Andric            __ctz_ = 0;
11480b57cec5SDimitry Andric            ++__seg_;
11490b57cec5SDimitry Andric        }
11500b57cec5SDimitry Andric        return *this;
11510b57cec5SDimitry Andric    }
11520b57cec5SDimitry Andric
11530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator++(int)
11540b57cec5SDimitry Andric    {
11550b57cec5SDimitry Andric        __bit_iterator __tmp = *this;
11560b57cec5SDimitry Andric        ++(*this);
11570b57cec5SDimitry Andric        return __tmp;
11580b57cec5SDimitry Andric    }
11590b57cec5SDimitry Andric
11600b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator--()
11610b57cec5SDimitry Andric    {
11620b57cec5SDimitry Andric        if (__ctz_ != 0)
11630b57cec5SDimitry Andric            --__ctz_;
11640b57cec5SDimitry Andric        else
11650b57cec5SDimitry Andric        {
11660b57cec5SDimitry Andric            __ctz_ = __bits_per_word - 1;
11670b57cec5SDimitry Andric            --__seg_;
11680b57cec5SDimitry Andric        }
11690b57cec5SDimitry Andric        return *this;
11700b57cec5SDimitry Andric    }
11710b57cec5SDimitry Andric
11720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator--(int)
11730b57cec5SDimitry Andric    {
11740b57cec5SDimitry Andric        __bit_iterator __tmp = *this;
11750b57cec5SDimitry Andric        --(*this);
11760b57cec5SDimitry Andric        return __tmp;
11770b57cec5SDimitry Andric    }
11780b57cec5SDimitry Andric
11790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator+=(difference_type __n)
11800b57cec5SDimitry Andric    {
11810b57cec5SDimitry Andric        if (__n >= 0)
11820b57cec5SDimitry Andric            __seg_ += (__n + __ctz_) / __bits_per_word;
11830b57cec5SDimitry Andric        else
11840b57cec5SDimitry Andric            __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1)
11850b57cec5SDimitry Andric                    / static_cast<difference_type>(__bits_per_word);
11860b57cec5SDimitry Andric        __n &= (__bits_per_word - 1);
11870b57cec5SDimitry Andric        __ctz_ = static_cast<unsigned>((__n + __ctz_)  % __bits_per_word);
11880b57cec5SDimitry Andric        return *this;
11890b57cec5SDimitry Andric    }
11900b57cec5SDimitry Andric
11910b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator-=(difference_type __n)
11920b57cec5SDimitry Andric    {
11930b57cec5SDimitry Andric        return *this += -__n;
11940b57cec5SDimitry Andric    }
11950b57cec5SDimitry Andric
11960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator+(difference_type __n) const
11970b57cec5SDimitry Andric    {
11980b57cec5SDimitry Andric        __bit_iterator __t(*this);
11990b57cec5SDimitry Andric        __t += __n;
12000b57cec5SDimitry Andric        return __t;
12010b57cec5SDimitry Andric    }
12020b57cec5SDimitry Andric
12030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __bit_iterator operator-(difference_type __n) const
12040b57cec5SDimitry Andric    {
12050b57cec5SDimitry Andric        __bit_iterator __t(*this);
12060b57cec5SDimitry Andric        __t -= __n;
12070b57cec5SDimitry Andric        return __t;
12080b57cec5SDimitry Andric    }
12090b57cec5SDimitry Andric
12100b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
12110b57cec5SDimitry Andric    friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;}
12120b57cec5SDimitry Andric
12130b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
12140b57cec5SDimitry Andric    friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y)
12150b57cec5SDimitry Andric        {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;}
12160b57cec5SDimitry Andric
12170b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const {return *(*this + __n);}
12180b57cec5SDimitry Andric
12190b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y)
12200b57cec5SDimitry Andric        {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;}
12210b57cec5SDimitry Andric
12220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y)
12230b57cec5SDimitry Andric        {return !(__x == __y);}
12240b57cec5SDimitry Andric
12250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y)
12260b57cec5SDimitry Andric        {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);}
12270b57cec5SDimitry Andric
12280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y)
12290b57cec5SDimitry Andric        {return __y < __x;}
12300b57cec5SDimitry Andric
12310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y)
12320b57cec5SDimitry Andric        {return !(__y < __x);}
12330b57cec5SDimitry Andric
12340b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y)
12350b57cec5SDimitry Andric        {return !(__x < __y);}
12360b57cec5SDimitry Andric
12370b57cec5SDimitry Andricprivate:
12380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
12390b57cec5SDimitry Andric    __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
12400b57cec5SDimitry Andric        : __seg_(__s), __ctz_(__ctz) {}
12410b57cec5SDimitry Andric
12420b57cec5SDimitry Andric    friend typename _Cp::__self;
12430b57cec5SDimitry Andric
12440b57cec5SDimitry Andric    friend class __bit_reference<_Cp>;
12450b57cec5SDimitry Andric    friend class __bit_const_reference<_Cp>;
12460b57cec5SDimitry Andric    friend class __bit_iterator<_Cp, true>;
12470b57cec5SDimitry Andric    template <class _Dp> friend struct __bit_array;
12480b57cec5SDimitry Andric    template <class _Dp> friend void __fill_n_false(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
12490b57cec5SDimitry Andric    template <class _Dp> friend void __fill_n_true(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
12500b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_aligned(__bit_iterator<_Dp, _IC> __first,
12510b57cec5SDimitry Andric                                                                                  __bit_iterator<_Dp, _IC> __last,
12520b57cec5SDimitry Andric                                                                                  __bit_iterator<_Dp, false> __result);
12530b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_unaligned(__bit_iterator<_Dp, _IC> __first,
12540b57cec5SDimitry Andric                                                                                    __bit_iterator<_Dp, _IC> __last,
12550b57cec5SDimitry Andric                                                                                    __bit_iterator<_Dp, false> __result);
12560b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy(__bit_iterator<_Dp, _IC> __first,
12570b57cec5SDimitry Andric                                                                        __bit_iterator<_Dp, _IC> __last,
12580b57cec5SDimitry Andric                                                                        __bit_iterator<_Dp, false> __result);
12590b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_aligned(__bit_iterator<_Dp, _IC> __first,
12600b57cec5SDimitry Andric                                                                                           __bit_iterator<_Dp, _IC> __last,
12610b57cec5SDimitry Andric                                                                                           __bit_iterator<_Dp, false> __result);
12620b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_unaligned(__bit_iterator<_Dp, _IC> __first,
12630b57cec5SDimitry Andric                                                                                             __bit_iterator<_Dp, _IC> __last,
12640b57cec5SDimitry Andric                                                                                             __bit_iterator<_Dp, false> __result);
12650b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy_backward(__bit_iterator<_Dp, _IC> __first,
12660b57cec5SDimitry Andric                                                                                 __bit_iterator<_Dp, _IC> __last,
12670b57cec5SDimitry Andric                                                                                 __bit_iterator<_Dp, false> __result);
12680b57cec5SDimitry Andric    template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>,
12690b57cec5SDimitry Andric                                                                                           __bit_iterator<__C1, false>,
12700b57cec5SDimitry Andric                                                                                           __bit_iterator<__C2, false>);
12710b57cec5SDimitry Andric    template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>,
12720b57cec5SDimitry Andric                                                                                             __bit_iterator<__C1, false>,
12730b57cec5SDimitry Andric                                                                                             __bit_iterator<__C2, false>);
12740b57cec5SDimitry Andric    template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>,
12750b57cec5SDimitry Andric                                                                                 __bit_iterator<__C1, false>,
12760b57cec5SDimitry Andric                                                                                 __bit_iterator<__C2, false>);
12770b57cec5SDimitry Andric    template <class _Dp> friend __bit_iterator<_Dp, false> rotate(__bit_iterator<_Dp, false>,
12780b57cec5SDimitry Andric                                                                __bit_iterator<_Dp, false>,
12790b57cec5SDimitry Andric                                                                __bit_iterator<_Dp, false>);
12800b57cec5SDimitry Andric    template <class _Dp, bool _IC1, bool _IC2> friend bool __equal_aligned(__bit_iterator<_Dp, _IC1>,
12810b57cec5SDimitry Andric                                                    __bit_iterator<_Dp, _IC1>,
12820b57cec5SDimitry Andric                                                    __bit_iterator<_Dp, _IC2>);
12830b57cec5SDimitry Andric    template <class _Dp, bool _IC1, bool _IC2> friend bool __equal_unaligned(__bit_iterator<_Dp, _IC1>,
12840b57cec5SDimitry Andric                                                      __bit_iterator<_Dp, _IC1>,
12850b57cec5SDimitry Andric                                                      __bit_iterator<_Dp, _IC2>);
12860b57cec5SDimitry Andric    template <class _Dp, bool _IC1, bool _IC2> friend bool equal(__bit_iterator<_Dp, _IC1>,
12870b57cec5SDimitry Andric                                                                __bit_iterator<_Dp, _IC1>,
12880b57cec5SDimitry Andric                                                                __bit_iterator<_Dp, _IC2>);
12890b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, _IC> __find_bool_true(__bit_iterator<_Dp, _IC>,
12900b57cec5SDimitry Andric                                                                          typename _Dp::size_type);
12910b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend __bit_iterator<_Dp, _IC> __find_bool_false(__bit_iterator<_Dp, _IC>,
12920b57cec5SDimitry Andric                                                                           typename _Dp::size_type);
12930b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type
12940b57cec5SDimitry Andric                   __count_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
12950b57cec5SDimitry Andric    template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type
12960b57cec5SDimitry Andric                   __count_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
12970b57cec5SDimitry Andric};
12980b57cec5SDimitry Andric
12990b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
13000b57cec5SDimitry Andric
13010b57cec5SDimitry Andric_LIBCPP_POP_MACROS
13020b57cec5SDimitry Andric
13030b57cec5SDimitry Andric#endif  // _LIBCPP___BIT_REFERENCE
1304