_LIBCUDACXX_BEGIN_NAMESPACE_CUDA

namespace __detail_ap
{

template <typename _Property>
_CCCL_DEVICE void* __associate_address_space(void* __ptr, _Property __prop)
{
  if (std::is_same<_Property, access_property::shared>::value == true)
  {
    bool __b = __isShared(__ptr);
    _LIBCUDACXX_ASSERT(__b, "");
#if !defined(_CCCL_CUDACC_BELOW_11_2)
    __builtin_assume(__b);
#else // ^^^ !_CCCL_CUDACC_BELOW_11_2 ^^^ / vvv _CCCL_CUDACC_BELOW_11_2 vvv
    (void) __b;
#endif // _CCCL_CUDACC_BELOW_11_2
  }
  else if (std::is_same<_Property, access_property::global>::value == true
           || std::is_same<_Property, access_property::normal>::value == true
           || std::is_same<_Property, access_property::persisting>::value == true
           || std::is_same<_Property, access_property::streaming>::value == true
           || std::is_same<_Property, access_property>::value)
  {
    bool __b = __isGlobal(__ptr);
    _LIBCUDACXX_ASSERT(__b, "");
#if !defined(_CCCL_CUDACC_BELOW_11_2)
    __builtin_assume(__b);
#else // ^^^ !_CCCL_CUDACC_BELOW_11_2 ^^^ / vvv _CCCL_CUDACC_BELOW_11_2 vvv
    (void) __b;
#endif // _CCCL_CUDACC_BELOW_11_2
  }

  return __ptr;
}

template <typename __Prop>
_CCCL_DEVICE void* __associate_descriptor(void* __ptr, __Prop __prop)
{
  return __associate_descriptor(__ptr, static_cast<std::uint64_t>(access_property(__prop)));
}

template <>
inline _CCCL_DEVICE void* __associate_descriptor(void* __ptr, std::uint64_t __prop)
{
  NV_IF_ELSE_TARGET(NV_PROVIDES_SM_80, (return __nv_associate_access_property(__ptr, __prop);), (return __ptr;))
}

template <>
inline _CCCL_DEVICE void* __associate_descriptor(void* __ptr, access_property::shared)
{
  return __ptr;
}

template <typename _Type, typename _Property>
_CCCL_HOST_DEVICE _Type* __associate(_Type* __ptr, _Property __prop)
{
  NV_IF_ELSE_TARGET(NV_IS_DEVICE,
                    (return static_cast<_Type*>(__associate_descriptor(
                      __associate_address_space(const_cast<void*>(static_cast<const void*>(__ptr)), __prop), __prop));),
                    (return __ptr;))
}

template <typename _Property>
class __annotated_ptr_base
{
  using __error = typename _Property::__unknown_access_property_type;
};

template <>
class __annotated_ptr_base<access_property::shared>
{
protected:
  static constexpr std::uint64_t __prop = 0;

  constexpr __annotated_ptr_base() noexcept                                          = default;
  constexpr __annotated_ptr_base(__annotated_ptr_base const&)                        = default;
  _CCCL_CONSTEXPR_CXX14 __annotated_ptr_base& operator=(const __annotated_ptr_base&) = default;
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(access_property::shared) noexcept {}
  inline _CCCL_DEVICE void* __apply_prop(void* __p) const
  {
    return __associate(__p, access_property::shared{});
  }
  _CCCL_HOST_DEVICE constexpr access_property::shared __get_property() const noexcept
  {
    return access_property::shared{};
  }
};

template <>
class __annotated_ptr_base<access_property::global>
{
protected:
  static constexpr std::uint64_t __prop = __sm_80::__interleave_normal();

  constexpr __annotated_ptr_base() noexcept                                          = default;
  constexpr __annotated_ptr_base(__annotated_ptr_base const&)                        = default;
  _CCCL_CONSTEXPR_CXX14 __annotated_ptr_base& operator=(const __annotated_ptr_base&) = default;
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(access_property::global) noexcept {}
  inline _CCCL_DEVICE void* __apply_prop(void* __p) const
  {
    return __associate(__p, access_property::global{});
  }
  _CCCL_HOST_DEVICE constexpr access_property::global __get_property() const noexcept
  {
    return access_property::global{};
  }
};

template <>
class __annotated_ptr_base<access_property::normal>
{
protected:
  static constexpr std::uint64_t __prop = __sm_80::__interleave_normal_demote();

  constexpr __annotated_ptr_base() noexcept                                          = default;
  constexpr __annotated_ptr_base(__annotated_ptr_base const&)                        = default;
  _CCCL_CONSTEXPR_CXX14 __annotated_ptr_base& operator=(const __annotated_ptr_base&) = default;
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(access_property::normal) noexcept {}
  inline _CCCL_DEVICE void* __apply_prop(void* __p) const
  {
    return __associate(__p, access_property::normal{});
  }
  _CCCL_HOST_DEVICE constexpr access_property::normal __get_property() const noexcept
  {
    return access_property::normal{};
  }
};

template <>
class __annotated_ptr_base<access_property::persisting>
{
protected:
  static constexpr std::uint64_t __prop = __sm_80::__interleave_persisting();

  constexpr __annotated_ptr_base() noexcept                                          = default;
  constexpr __annotated_ptr_base(__annotated_ptr_base const&)                        = default;
  _CCCL_CONSTEXPR_CXX14 __annotated_ptr_base& operator=(const __annotated_ptr_base&) = default;
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(access_property::persisting) noexcept {}
  inline _CCCL_DEVICE void* __apply_prop(void* __p) const
  {
    return __associate(__p, access_property::persisting{});
  }
  _CCCL_HOST_DEVICE constexpr access_property::persisting __get_property() const noexcept
  {
    return access_property::persisting{};
  }
};

template <>
class __annotated_ptr_base<access_property::streaming>
{
protected:
  static constexpr std::uint64_t __prop = __sm_80::__interleave_streaming();

  constexpr __annotated_ptr_base() noexcept                                          = default;
  constexpr __annotated_ptr_base(__annotated_ptr_base const&)                        = default;
  _CCCL_CONSTEXPR_CXX14 __annotated_ptr_base& operator=(const __annotated_ptr_base&) = default;
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(access_property::streaming) noexcept {}
  inline _CCCL_DEVICE void* __apply_prop(void* __p) const
  {
    return __associate(__p, access_property::streaming{});
  }
  _CCCL_HOST_DEVICE constexpr access_property::streaming __get_property() const noexcept
  {
    return access_property::streaming{};
  }
};

template <>
class __annotated_ptr_base<access_property>
{
protected:
  std::uint64_t __prop;

  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base() noexcept
      : __prop(access_property())
  {}
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(std::uint64_t __property) noexcept
      : __prop(__property)
  {}
  _CCCL_HOST_DEVICE constexpr __annotated_ptr_base(access_property __property) noexcept
      : __annotated_ptr_base(static_cast<std::uint64_t>(__property))
  {}
  constexpr __annotated_ptr_base(__annotated_ptr_base const&)                        = default;
  _CCCL_CONSTEXPR_CXX14 __annotated_ptr_base& operator=(const __annotated_ptr_base&) = default;
  inline _CCCL_DEVICE void* __apply_prop(void* __p) const
  {
    return __associate(__p, __prop);
  }
  _CCCL_HOST_DEVICE access_property __get_property() const noexcept
  {
    return reinterpret_cast<access_property&>(const_cast<std::uint64_t&>(__prop));
  }
};
} // namespace __detail_ap

_LIBCUDACXX_END_NAMESPACE_CUDA
