File size: 20,789 Bytes
501e3f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
//@HEADER
// ************************************************************************
//
//                        Kokkos v. 2.0
//              Copyright (2019) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
//
// ************************************************************************
//@HEADER
*/

#ifndef _LIBCUDACXX___MDSPAN_EXTENTS_HPP
#define _LIBCUDACXX___MDSPAN_EXTENTS_HPP

#ifndef __cuda_std__
#include <__config>
#endif // __cuda_std__

#include "../__mdspan/macros.h"
#ifdef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
#include "../__mdspan/no_unique_address.h"
#endif
#include "../__mdspan/static_array.h"
#include "../__mdspan/standard_layout_static_array.h"
#include "../__type_traits/conditional.h"
#include "../__type_traits/integral_constant.h"
#include "../__type_traits/is_convertible.h"
#include "../__type_traits/is_nothrow_constructible.h"
#include "../__type_traits/make_unsigned.h"
#include "../__utility/integer_sequence.h"
#include "../array"
#include "../cstddef"
#include "../span"

#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

#if defined(_LIBCUDACXX_PUSH_MACROS)
  _LIBCUDACXX_PUSH_MACROS
#endif
#include "../__undef_macros"

_LIBCUDACXX_BEGIN_NAMESPACE_STD

#if _LIBCUDACXX_STD_VER > 11

namespace __detail {

template<size_t ... _Extents>
struct __count_dynamic_extents;

template<size_t _Ep, size_t ... _Extents>
struct __count_dynamic_extents<_Ep,_Extents...> {
  static constexpr size_t val = (_Ep==dynamic_extent?1:0) + __count_dynamic_extents<_Extents...>::val;
};

template<>
struct __count_dynamic_extents<> {
  static constexpr size_t val = 0;
};

template <size_t... _Extents, size_t... _OtherExtents>
_LIBCUDACXX_HOST_DEVICE
static constexpr false_type __check_compatible_extents(
  false_type, _CUDA_VSTD::integer_sequence<size_t, _Extents...>, _CUDA_VSTD::integer_sequence<size_t, _OtherExtents...>
) noexcept { return { }; }

// This helper prevents ICE's on MSVC.
template <size_t _Lhs, size_t _Rhs>
struct __compare_extent_compatible : integral_constant<bool,
    _Lhs == dynamic_extent ||
    _Rhs == dynamic_extent ||
    _Lhs == _Rhs>
{};

template <size_t... _Extents, size_t... _OtherExtents>
static integral_constant<
  bool,
  __MDSPAN_FOLD_AND(
    (
      __compare_extent_compatible<_Extents, _OtherExtents>::value
    ) /* && ... */
  )
>
_LIBCUDACXX_HOST_DEVICE
__check_compatible_extents(
  true_type, _CUDA_VSTD::integer_sequence<size_t, _Extents...>, _CUDA_VSTD::integer_sequence<size_t, _OtherExtents...>
) noexcept { return { }; }

struct __extents_tag { };

} // end namespace __detail

template <class _ThisIndexType, size_t... _Extents>
class extents
#ifdef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
  : private __detail::__no_unique_address_emulation<
      __detail::__partially_static_sizes_tagged<__detail::__extents_tag, _ThisIndexType , size_t, _Extents...>>
#endif
{
public:

  using rank_type = size_t;
  using index_type = _ThisIndexType;
  using size_type = make_unsigned_t<index_type>;

// internal typedefs which for technical reasons are public
  using __storage_t = __detail::__partially_static_sizes_tagged<__detail::__extents_tag, index_type, size_t, _Extents...>;

#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
  _LIBCUDACXX_NO_UNIQUE_ADDRESS __storage_t __storage_;
#else
  using __base_t = __detail::__no_unique_address_emulation<__storage_t>;
#endif

// private members dealing with the way we internally store dynamic extents
 private:

  __MDSPAN_FORCE_INLINE_FUNCTION constexpr
  __storage_t& __storage() noexcept {
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    return __storage_;
#else
    return this->__base_t::__ref();
#endif
  }
  __MDSPAN_FORCE_INLINE_FUNCTION
  constexpr __storage_t const& __storage() const noexcept {
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    return __storage_;
#else
    return this->__base_t::__ref();
#endif
  }

  template <size_t... _Idxs>
  __MDSPAN_FORCE_INLINE_FUNCTION
  static constexpr
  index_type _static_extent_impl(size_t __n, _CUDA_VSTD::integer_sequence<size_t, _Idxs...>) noexcept {
    return __MDSPAN_FOLD_PLUS_RIGHT(((_Idxs == __n) ? _Extents : 0), /* + ... + */ 0);
  }

  template <class, size_t...>
  friend class extents;

  template <class _OtherIndexType, size_t... _OtherExtents, size_t... _Idxs>
  __MDSPAN_INLINE_FUNCTION
  constexpr bool _eq_impl(_CUDA_VSTD::extents<_OtherIndexType, _OtherExtents...>, false_type, _CUDA_VSTD::index_sequence<_Idxs...>) const noexcept { return false; }
  template <class _OtherIndexType, size_t... _OtherExtents, size_t... _Idxs>
  __MDSPAN_INLINE_FUNCTION
  constexpr bool _eq_impl(
    _CUDA_VSTD::extents<_OtherIndexType, _OtherExtents...> __other,
    true_type, _CUDA_VSTD::index_sequence<_Idxs...>
  ) const noexcept {
    return __MDSPAN_FOLD_AND(
      (__storage().template __get_n<_Idxs>() == __other.__storage().template __get_n<_Idxs>()) /* && ... */
    );
  }

  template <class _OtherIndexType, size_t... _OtherExtents, size_t... _Idxs>
  __MDSPAN_INLINE_FUNCTION
  constexpr bool _not_eq_impl(_CUDA_VSTD::extents<_OtherIndexType, _OtherExtents...>, false_type, _CUDA_VSTD::index_sequence<_Idxs...>) const noexcept { return true; }
  template <class _OtherIndexType, size_t... _OtherExtents, size_t... _Idxs>
  __MDSPAN_INLINE_FUNCTION
  constexpr bool _not_eq_impl(
    _CUDA_VSTD::extents<_OtherIndexType, _OtherExtents...> __other,
    true_type, _CUDA_VSTD::index_sequence<_Idxs...>
  ) const noexcept {
    return __MDSPAN_FOLD_OR(
      (__storage().template __get_n<_Idxs>() != __other.__storage().template __get_n<_Idxs>()) /* || ... */
    );
  }

#ifdef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
  __MDSPAN_INLINE_FUNCTION constexpr explicit
  extents(__base_t&& __b) noexcept
    : __base_t(_CUDA_VSTD::move(__b))
  { }
#endif


// public interface:
public:
  /* Defined above for use in the private code
  using rank_type = size_t;
  using index_type = _ThisIndexType;
  */

  __MDSPAN_INLINE_FUNCTION
  static constexpr rank_type rank() noexcept { return sizeof...(_Extents); }
  __MDSPAN_INLINE_FUNCTION
  static constexpr rank_type rank_dynamic() noexcept { return __MDSPAN_FOLD_PLUS_RIGHT((rank_type(_Extents == dynamic_extent)), /* + ... + */ 0); }

  //--------------------------------------------------------------------------------
  // Constructors, Destructors, and Assignment

  // Default constructor
  __MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr extents() noexcept = default;

  // Converting constructor
  __MDSPAN_TEMPLATE_REQUIRES(
    class _OtherIndexType, size_t... _OtherExtents,
    /* requires */ (
      /* multi-stage check to protect from invalid pack expansion when sizes don't match? */
      decltype(__detail::__check_compatible_extents(
        integral_constant<bool, sizeof...(_Extents) == sizeof...(_OtherExtents)>{},
        _CUDA_VSTD::integer_sequence<size_t, _Extents...>{},
        _CUDA_VSTD::integer_sequence<size_t, _OtherExtents...>{}
      ))::value
    )
  )
  __MDSPAN_INLINE_FUNCTION
  __MDSPAN_CONDITIONAL_EXPLICIT(
    (((_Extents != dynamic_extent) && (_OtherExtents == dynamic_extent)) || ...) ||
    (_CUDA_VSTD::numeric_limits<index_type>::max() < _CUDA_VSTD::numeric_limits<_OtherIndexType>::max()))
  constexpr extents(const extents<_OtherIndexType, _OtherExtents...>& __other)
    noexcept
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    : __storage_{
#else
    : __base_t(__base_t{__storage_t{
#endif
        __other.__storage().__enable_psa_conversion()
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      }
#else
      }})
#endif
  {
    /* TODO: precondition check
     * __other.extent(r) equals Er for each r for which Er is a static extent, and
     * either
     *   - sizeof...(_OtherExtents) is zero, or
     *   - __other.extent(r) is a representable value of type index_type for all rank index r of __other
     */
  }

#ifdef __NVCC__
    __MDSPAN_TEMPLATE_REQUIRES(
    class... _Integral,
    /* requires */ (
      // TODO: check whether the other version works with newest NVCC, doesn't with 11.4
      // NVCC seems to pick up rank_dynamic from the wrong extents type???
      __MDSPAN_FOLD_AND(_LIBCUDACXX_TRAIT(_CUDA_VSTD::is_convertible, _Integral, index_type) /* && ... */) &&
      __MDSPAN_FOLD_AND(_LIBCUDACXX_TRAIT(_CUDA_VSTD::is_nothrow_constructible, index_type, _Integral) /* && ... */) &&
      // NVCC chokes on the fold thingy here so wrote the workaround
      ((sizeof...(_Integral) == __detail::__count_dynamic_extents<_Extents...>::val) ||
       (sizeof...(_Integral) == sizeof...(_Extents)))
      )
    )
#else
    __MDSPAN_TEMPLATE_REQUIRES(
    class... _Integral,
    /* requires */ (
       __MDSPAN_FOLD_AND(_LIBCUDACXX_TRAIT(_CUDA_VSTD::is_convertible, _Integral, index_type) /* && ... */) &&
       __MDSPAN_FOLD_AND(_LIBCUDACXX_TRAIT(_CUDA_VSTD::is_nothrow_constructible, index_type, _Integral) /* && ... */) &&
       ((sizeof...(_Integral) == rank_dynamic()) || (sizeof...(_Integral) == rank()))
      )
    )
#endif
  __MDSPAN_INLINE_FUNCTION
  explicit constexpr extents(_Integral... __exts) noexcept
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    : __storage_{
#else
    : __base_t(__base_t{typename __base_t::__stored_type{
#endif
      _CUDA_VSTD::conditional_t<sizeof...(_Integral)==rank_dynamic(),
        __detail::__construct_psa_from_dynamic_exts_values_tag_t,
        __detail::__construct_psa_from_all_exts_values_tag_t>(),
        static_cast<index_type>(__exts)...
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      }
#else
      }})
#endif
  {
    /* TODO: precondition check
     * If sizeof...(_IndexTypes) != rank_dynamic() is true, exts_arr[r] equals Er for each r for which Er is a static extent, and
     * either
     *   - sizeof...(__exts) == 0 is true, or
     *   - each element of __exts is nonnegative and is a representable value of type index_type.
     */
  }

    // TODO: check whether this works with newest NVCC, doesn't with 11.4
#ifdef __NVCC__
  // NVCC seems to pick up rank_dynamic from the wrong extents type???
  // NVCC chokes on the fold thingy here so wrote the workaround
  __MDSPAN_TEMPLATE_REQUIRES(
    class _IndexType, size_t _Np,
    /* requires */ (
      _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_convertible, _IndexType, index_type) &&
      _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_nothrow_constructible, index_type, _IndexType) &&
      ((_Np == __detail::__count_dynamic_extents<_Extents...>::val) ||
       (_Np == sizeof...(_Extents)))
    )
  )
#else
    __MDSPAN_TEMPLATE_REQUIRES(
        class _IndexType, size_t _Np,
        /* requires */ (
          _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_convertible, _IndexType, index_type) &&
          _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_nothrow_constructible, index_type, _IndexType) &&
          (_Np == rank() || _Np == rank_dynamic())
    )
  )
#endif
  __MDSPAN_CONDITIONAL_EXPLICIT(_Np != rank_dynamic())
  __MDSPAN_INLINE_FUNCTION
  constexpr
  extents(_CUDA_VSTD::array<_IndexType, _Np> const& __exts) noexcept
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    : __storage_{
#else
    : __base_t(__base_t{typename __base_t::__stored_type{
#endif
      _CUDA_VSTD::conditional_t<_Np==rank_dynamic(),
        __detail::__construct_psa_from_dynamic_exts_array_tag_t<0>,
        __detail::__construct_psa_from_all_exts_array_tag_t>(),
      _CUDA_VSTD::array<_IndexType,_Np>{__exts}
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      }
#else
      }})
#endif
  {
    /* TODO: precondition check
     * If _Np != rank_dynamic() is true, __exts[r] equals Er for each r for which Er is a static extent, and
     * either
     *   - _Np is zero, or
     *   - __exts[r] is nonnegative and is a representable value of type index_type for all rank index r
     */
  }

  // TODO: check whether the below works with newest NVCC, doesn't with 11.4
#ifdef __NVCC__
  // NVCC seems to pick up rank_dynamic from the wrong extents type???
  // NVCC chokes on the fold thingy here so wrote the workaround
  __MDSPAN_TEMPLATE_REQUIRES(
    class _IndexType, size_t _Np,
    /* requires */ (
      _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_convertible, _IndexType, index_type) &&
      _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_nothrow_constructible, index_type, _IndexType) &&
      ((_Np == __detail::__count_dynamic_extents<_Extents...>::val) ||
       (_Np == sizeof...(_Extents)))
    )
  )
#else
    __MDSPAN_TEMPLATE_REQUIRES(
        class _IndexType, size_t _Np,
        /* requires */ (
          _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_convertible, _IndexType, index_type) &&
          _LIBCUDACXX_TRAIT(_CUDA_VSTD::is_nothrow_constructible, index_type, _IndexType) &&
          (_Np == rank() || _Np == rank_dynamic())
    )
  )
#endif
  __MDSPAN_CONDITIONAL_EXPLICIT(_Np != rank_dynamic())
  __MDSPAN_INLINE_FUNCTION
  constexpr
  extents(_CUDA_VSTD::span<_IndexType, _Np> __exts) noexcept
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    : __storage_{
#else
    : __base_t(__base_t{typename __base_t::__stored_type{
#endif
      _CUDA_VSTD::conditional_t<_Np==rank_dynamic(),
        __detail::__construct_psa_from_dynamic_exts_array_tag_t<0>,
        __detail::__construct_psa_from_all_exts_array_tag_t>(),
      __exts
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      }
#else
      }})
#endif
  {
    /* TODO: precondition check
     * If _Np != rank_dynamic() is true, __exts[r] equals Er for each r for which Er is a static extent, and
     * either
     *   - _Np is zero, or
     *   - __exts[r] is nonnegative and is a representable value of type index_type for all rank index r
     */
  }

  // Need this constructor for some submdspan implementation stuff
  // for the layout_stride case where I use an extents object for strides
  __MDSPAN_INLINE_FUNCTION
  constexpr explicit
  extents(__storage_t const& __sto ) noexcept
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
    : __storage_{
#else
    : __base_t(__base_t{
#endif
        __sto
#ifndef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      }
#else
      })
#endif
  { }

  //--------------------------------------------------------------------------------

  __MDSPAN_INLINE_FUNCTION
  static constexpr
  size_t static_extent(size_t __n) noexcept {
    // Can't do assert here since that breaks true constexpr ness
    // assert(__n<rank());
    return _static_extent_impl(__n, _CUDA_VSTD::make_integer_sequence<size_t, sizeof...(_Extents)>{});
  }

  __MDSPAN_INLINE_FUNCTION
  constexpr
  index_type extent(size_t __n) const noexcept {
    // Can't do assert here since that breaks true constexpr ness
    // assert(__n<rank());
    return __storage().__get(__n);
  }

  //--------------------------------------------------------------------------------

  template<class _OtherIndexType, size_t... _RHS>
  __MDSPAN_INLINE_FUNCTION
  friend constexpr bool operator==(extents const& lhs, extents<_OtherIndexType, _RHS...> const& __rhs) noexcept {
    return lhs._eq_impl(
      __rhs, integral_constant<bool, (sizeof...(_RHS) == rank())>{},
      _CUDA_VSTD::make_index_sequence<sizeof...(_RHS)>{}
    );
  }

#if !(__MDSPAN_HAS_CXX_20)
  template<class _OtherIndexType, size_t... _RHS>
  __MDSPAN_INLINE_FUNCTION
  friend constexpr bool operator!=(extents const& lhs, extents<_OtherIndexType, _RHS...> const& __rhs) noexcept {
    return lhs._not_eq_impl(
      __rhs, integral_constant<bool, (sizeof...(_RHS) == rank())>{},
      _CUDA_VSTD::make_index_sequence<sizeof...(_RHS)>{}
    );
  }
#endif

  // End of public interface

public:  // (but not really)

  __MDSPAN_INLINE_FUNCTION static constexpr
  extents __make_extents_impl(__detail::__partially_static_sizes<index_type, size_t,_Extents...>&& __bs) noexcept {
    // This effectively amounts to a sideways cast that can be done in a constexpr
    // context, but we have to do it to handle the case where the extents and the
    // strides could accidentally end up with the same types in their hierarchies
    // somehow (which would cause layout_stride::mapping to not be standard_layout)
    return extents(
#ifdef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      __base_t{
#endif
        _CUDA_VSTD::move(__bs.template __with_tag<__detail::__extents_tag>())
#ifdef _LIBCUDACXX_HAS_NO_ATTRIBUTE_NO_UNIQUE_ADDRESS
      }
#endif
    );
  }

  template <size_t _Np>
  __MDSPAN_FORCE_INLINE_FUNCTION
  constexpr
  index_type __extent() const noexcept {
    return __storage().template __get_n<_Np>();
  }

  template <size_t _Np, size_t _Default=dynamic_extent>
  __MDSPAN_INLINE_FUNCTION
  static constexpr
  index_type __static_extent() noexcept {
    return __storage_t::template __get_static_n<_Np, _Default>();
  }

};

namespace __detail {

template <class _IndexType, size_t _Rank, class _Extents = _CUDA_VSTD::extents<_IndexType>>
struct __make_dextents;

template <class _IndexType, size_t _Rank, size_t... _ExtentsPack>
struct __make_dextents<_IndexType, _Rank, _CUDA_VSTD::extents<_IndexType, _ExtentsPack...>> {
  using type = typename __make_dextents<_IndexType, _Rank - 1,
    _CUDA_VSTD::extents<_IndexType, _CUDA_VSTD::dynamic_extent, _ExtentsPack...>>::type;
};

template <class _IndexType, size_t... _ExtentsPack>
struct __make_dextents<_IndexType, 0, _CUDA_VSTD::extents<_IndexType, _ExtentsPack...>> {
  using type = _CUDA_VSTD::extents<_IndexType, _ExtentsPack...>;
};

} // end namespace __detail

template <class _IndexType, size_t _Rank>
using dextents = typename __detail::__make_dextents<_IndexType, _Rank>::type;

#if defined(__MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
template <class... _IndexTypes>
_LIBCUDACXX_HOST_DEVICE extents(_IndexTypes...)
  // Workaround for nvcc
  //-> extents<size_t, __detail::__make_dynamic_extent<_IndexTypes>()...>;
  // Adding "(void)" so that clang doesn't complain this is unused
  -> extents<size_t, size_t(((void)_IndexTypes(), -1))...>;
#endif

namespace __detail {

template <class _Tp>
struct __is_extents : false_type {};

template <class _IndexType, size_t... _ExtentsPack>
struct __is_extents<_CUDA_VSTD::extents<_IndexType, _ExtentsPack...>> : true_type {};

template <class _Tp>
static constexpr bool __is_extents_v = __is_extents<_Tp>::value;


template <typename _Extents>
struct __extents_to_partially_static_sizes;

template <class _IndexType, size_t... _ExtentsPack>
struct __extents_to_partially_static_sizes<_CUDA_VSTD::extents<_IndexType, _ExtentsPack...>> {
  using type = __detail::__partially_static_sizes<
          typename _CUDA_VSTD::extents<_IndexType, _ExtentsPack...>::index_type, size_t,
          _ExtentsPack...>;
};

template <typename _Extents>
using __extents_to_partially_static_sizes_t = typename __extents_to_partially_static_sizes<_Extents>::type;

} // end namespace __detail

#endif // _LIBCUDACXX_STD_VER > 11

_LIBCUDACXX_END_NAMESPACE_STD

#if defined(_LIBCUDACXX_POP_MACROS)
  _LIBCUDACXX_POP_MACROS
#endif

#endif // _LIBCUDACXX___MDSPAN_EXTENTS_HPP