#ifndef _CUDA_ANNOTATED_PTR
#define _CUDA_ANNOTATED_PTR

#include <cuda/std/detail/__config>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
#  pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
#  pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
#  pragma system_header
#endif // no system header

#include <cuda/barrier>
#include <cuda/discard_memory>
#include <cuda/std/cstdint>
#include <cuda/std/detail/__access_property>

_LIBCUDACXX_BEGIN_NAMESPACE_CUDA

class access_property
{
private:
  std::uint64_t __descriptor = 0;

public:
  struct shared
  {};
  struct global
  {};
  struct persisting
  {
    _CCCL_HOST_DEVICE constexpr operator cudaAccessProperty() const noexcept
    {
      return cudaAccessProperty::cudaAccessPropertyPersisting;
    }
  };
  struct streaming
  {
    _CCCL_HOST_DEVICE constexpr operator cudaAccessProperty() const noexcept
    {
      return cudaAccessProperty::cudaAccessPropertyStreaming;
    }
  };
  struct normal
  {
    _CCCL_HOST_DEVICE constexpr operator cudaAccessProperty() const noexcept
    {
      return cudaAccessProperty::cudaAccessPropertyNormal;
    }
  };

  _CCCL_HOST_DEVICE constexpr access_property(global) noexcept
      : __descriptor(__detail_ap::__sm_80::__interleave_normal())
  {}
  _CCCL_HOST_DEVICE constexpr access_property() noexcept
      : __descriptor(__detail_ap::__sm_80::__interleave_normal())
  {}
  constexpr access_property(access_property const&) noexcept        = default;
  access_property& operator=(const access_property& other) noexcept = default;

  _CCCL_HOST_DEVICE constexpr access_property(normal, float __fraction)
      : __descriptor(__detail_ap::__interleave(normal{}, __fraction))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(streaming, float __fraction)
      : __descriptor(__detail_ap::__interleave(streaming{}, __fraction))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(persisting, float __fraction)
      : __descriptor(__detail_ap::__interleave(persisting{}, __fraction))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(normal, float __fraction, streaming)
      : __descriptor(__detail_ap::__interleave(normal{}, __fraction, streaming{}))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(persisting, float __fraction, streaming)
      : __descriptor(__detail_ap::__interleave(persisting{}, __fraction, streaming{}))
  {}

  _CCCL_HOST_DEVICE constexpr access_property(normal) noexcept
      : access_property(normal{}, 1.0)
  {}
  _CCCL_HOST_DEVICE constexpr access_property(streaming) noexcept
      : access_property(streaming{}, 1.0)
  {}
  _CCCL_HOST_DEVICE constexpr access_property(persisting) noexcept
      : access_property(persisting{}, 1.0)
  {}

  _CCCL_HOST_DEVICE constexpr access_property(void* __ptr, std::size_t __hit_bytes, std::size_t __total_bytes, normal)
      : __descriptor(__detail_ap::__block(__ptr, __hit_bytes, __total_bytes, normal{}))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(void* __ptr, std::size_t __hit_bytes, std::size_t __total_bytes, streaming)
      : __descriptor(__detail_ap::__block(__ptr, __hit_bytes, __total_bytes, streaming{}))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(
    void* __ptr, std::size_t __hit_bytes, std::size_t __total_bytes, persisting)
      : __descriptor(__detail_ap::__block(__ptr, __hit_bytes, __total_bytes, persisting{}))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(
    void* __ptr, std::size_t __hit_bytes, std::size_t __total_bytes, normal, streaming)
      : __descriptor(__detail_ap::__block(__ptr, __hit_bytes, __total_bytes, normal{}, streaming{}))
  {}
  _CCCL_HOST_DEVICE constexpr access_property(
    void* __ptr, std::size_t __hit_bytes, std::size_t __total_bytes, persisting, streaming)
      : __descriptor(__detail_ap::__block(__ptr, __hit_bytes, __total_bytes, persisting{}, streaming{}))
  {}

  _CCCL_HOST_DEVICE constexpr explicit operator std::uint64_t() const noexcept
  {
    return __descriptor;
  }
};

_LIBCUDACXX_END_NAMESPACE_CUDA

#include <cuda/std/detail/__annotated_ptr>

_LIBCUDACXX_BEGIN_NAMESPACE_CUDA

template <class _Tp, class _Property>
_CCCL_HOST_DEVICE _Tp* associate_access_property(_Tp* __ptr, _Property __prop)
{
  static_assert(
    std::is_same<_Property, access_property>::value || std::is_same<_Property, access_property::persisting>::value
      || std::is_same<_Property, access_property::streaming>::value
      || std::is_same<_Property, access_property::normal>::value
      || std::is_same<_Property, access_property::global>::value
      || std::is_same<_Property, access_property::shared>::value,
    "property is not convertible to cuda::access_property");
  return __detail_ap::__associate(__ptr, __prop);
}

template <class _Shape>
_CCCL_HOST_DEVICE void
apply_access_property(const volatile void* __ptr, const _Shape __shape, access_property::persisting __prop) noexcept
{
  NV_IF_TARGET(
    NV_PROVIDES_SM_80,
    (if (!__isGlobal((void*) __ptr)) return;

     char* __p                               = reinterpret_cast<char*>(const_cast<void*>(__ptr));
     static constexpr std::size_t _LINE_SIZE = 128;
     std::size_t __nbytes                    = static_cast<std::size_t>(__shape);
     std::size_t __end = ((std::uintptr_t)(__p + __nbytes) % _LINE_SIZE) ? __nbytes + _LINE_SIZE : __nbytes;
     __end /= _LINE_SIZE;

     // Apply to all 128 bytes aligned cache lines inclusive of __p
     for (std::size_t __i = 0; __i < __end; __i += _LINE_SIZE) {
       asm volatile("prefetch.global.L2::evict_last [%0];" ::"l"(__p + (__i * _LINE_SIZE)) :);
     }))
}

template <class _Shape>
_CCCL_HOST_DEVICE void
apply_access_property(const volatile void* __ptr, const _Shape __shape, access_property::normal __prop) noexcept
{
  NV_IF_TARGET(
    NV_PROVIDES_SM_80,
    (if (!__isGlobal((void*) __ptr)) return;

     char* __p                               = reinterpret_cast<char*>(const_cast<void*>(__ptr));
     static constexpr std::size_t _LINE_SIZE = 128;
     std::size_t __nbytes                    = static_cast<std::size_t>(__shape);
     std::size_t __end = ((std::uintptr_t)(__p + __nbytes) % _LINE_SIZE) ? __nbytes + _LINE_SIZE : __nbytes;
     __end /= _LINE_SIZE;

     // Apply to all 128 bytes aligned cache lines inclusive of __p
     for (std::size_t __i = 0; __i < __end; __i += _LINE_SIZE) {
       asm volatile("prefetch.global.L2::evict_normal [%0];" ::"l"(__p + (__i * _LINE_SIZE)) :);
     }))
}

template <class _Tp, class _Property>
class annotated_ptr : public __detail_ap::__annotated_ptr_base<_Property>
{
public:
  using value_type      = _Tp;
  using size_type       = std::size_t;
  using reference       = value_type&;
  using pointer         = value_type*;
  using const_pointer   = value_type const*;
  using difference_type = std::ptrdiff_t;

private:
  using __self = annotated_ptr<_Tp, _Property>;

  // Converting from a 64-bit to 32-bit shared pointer and maybe back just for storage might or might not be profitable.
  pointer __repr = (pointer) ((size_type) nullptr);

  _CCCL_HOST_DEVICE pointer __get(bool __skip_prop = false, difference_type __n = 0) const
  {
    NV_IF_TARGET(NV_IS_DEVICE, (if (!__skip_prop) {
                   return static_cast<pointer>(
                     this->__apply_prop(const_cast<void*>(static_cast<const volatile void*>(__repr + __n))));
                 }))
    return __repr + __n;
  }
  _CCCL_HOST_DEVICE pointer __offset(difference_type __n, bool __skip_prop = false) const
  {
    return __get(__skip_prop, __n);
  }

public:
  _CCCL_HOST_DEVICE pointer operator->() const
  {
    return __get();
  }

  _CCCL_HOST_DEVICE reference operator*() const
  {
    return *__get();
  }

  _CCCL_HOST_DEVICE reference operator[](difference_type __n) const
  {
    return *__offset(__n);
  }

  _CCCL_HOST_DEVICE constexpr difference_type operator-(annotated_ptr o) const
  {
    return __repr - o.__repr;
  }

  constexpr annotated_ptr() noexcept                     = default;
  constexpr annotated_ptr(annotated_ptr const&) noexcept = default;
  // No constexpr for c11 as the method can't be const
  _CCCL_CONSTEXPR_CXX14 annotated_ptr& operator=(annotated_ptr const& other) noexcept = default;

  _CCCL_HOST_DEVICE explicit annotated_ptr(pointer __p)
      : __repr(__p)
  {
    NV_IF_TARGET(
      NV_IS_DEVICE,
      (_LIBCUDACXX_DEBUG_ASSERT((std::is_same<_Property, shared>::value && __isShared(__p) || __isGlobal(__p)), "");))
  }

  template <typename _RuntimeProperty>
  _CCCL_HOST_DEVICE annotated_ptr(pointer __p, _RuntimeProperty __prop)
      : __detail_ap::__annotated_ptr_base<_Property>(static_cast<std::uint64_t>(access_property(__prop)))
      , __repr(__p)
  {
    static_assert(std::is_same<_Property, access_property>::value,
                  "This method requires annotated_ptr<T, cuda::access_property>");
    static_assert(
      std::is_same<_RuntimeProperty, access_property::global>::value
        || std::is_same<_RuntimeProperty, access_property::normal>::value
        || std::is_same<_RuntimeProperty, access_property::streaming>::value
        || std::is_same<_RuntimeProperty, access_property::persisting>::value
        || std::is_same<_RuntimeProperty, access_property>::value,
      "This method requires RuntimeProperty=global|normal|streaming|persisting|access_property");
    NV_IF_TARGET(NV_IS_DEVICE, (_LIBCUDACXX_DEBUG_ASSERT((__isGlobal(__p) == true), "");))
  }

  template <class _TTp, class _Prop>
  _CCCL_HOST_DEVICE annotated_ptr(const annotated_ptr<_TTp, _Prop>& __other);

  _CCCL_HOST_DEVICE constexpr explicit operator bool() const noexcept
  {
    return __repr != nullptr;
  }

  _CCCL_HOST_DEVICE pointer get() const noexcept
  {
    constexpr bool __is_shared = std::is_same<_Property, access_property::shared>::value;
    return __is_shared ? __repr : &(*annotated_ptr<value_type, access_property::global>(__repr));
  }

  _CCCL_HOST_DEVICE _Property __property() const noexcept
  {
    return this->__get_property();
  }
};

template <class _Tp, class _Property>
template <class _TTp, class _Prop>
_CCCL_HOST_DEVICE annotated_ptr<_Tp, _Property>::annotated_ptr(const annotated_ptr<_TTp, _Prop>& __other)
    : __detail_ap::__annotated_ptr_base<_Property>(__other.__property())
    , __repr(__other.get())
{
  static_assert(std::is_assignable<pointer&, _TTp*>::value, "pointer must be assignable from other pointer");
  static_assert(
    (std::is_same<_Property, access_property>::value && !std::is_same<_Prop, access_property::shared>::value)
      || std::is_same<_Property, _Prop>::value,
    "Property must be either access_property or other property, and both properties must have same address space");
  // note: precondition "__other.__rep must be compatible with _Property" currently always holds
}

template <class _Dst, class _Src, class _SrcProperty, class _Shape, class _Sync>
_CCCL_HOST_DEVICE void memcpy_async(_Dst* __dst, annotated_ptr<_Src, _SrcProperty> __src, _Shape __shape, _Sync& __sync)
{
  memcpy_async(__dst, &(*__src), __shape, __sync);
}

template <class _Dst, class _DstProperty, class _Src, class _SrcProperty, class _Shape, class _Sync>
_CCCL_HOST_DEVICE void memcpy_async(
  annotated_ptr<_Dst, _DstProperty> __dst, annotated_ptr<_Src, _SrcProperty> __src, _Shape __shape, _Sync& __sync)
{
  memcpy_async(&(*__dst), &(*__src), __shape, __sync);
}

template <class _Group, class _Dst, class _Src, class _SrcProperty, class _Shape, class _Sync>
_CCCL_HOST_DEVICE void
memcpy_async(const _Group& __group, _Dst* __dst, annotated_ptr<_Src, _SrcProperty> __src, _Shape __shape, _Sync& __sync)
{
  memcpy_async(__group, __dst, &(*__src), __shape, __sync);
}

template <class _Group, class _Dst, class _DstProperty, class _Src, class _SrcProperty, class _Shape, class _Sync>
_CCCL_HOST_DEVICE void memcpy_async(
  const _Group& __group,
  annotated_ptr<_Dst, _DstProperty> __dst,
  annotated_ptr<_Src, _SrcProperty> __src,
  _Shape __shape,
  _Sync& __sync)
{
  memcpy_async(__group, &(*__dst), &(*__src), __shape, __sync);
}

_LIBCUDACXX_END_NAMESPACE_CUDA

#endif // _CUDA_ANNOTATED_PTR
