aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/std/mdspan
blob: 03cc4f02a1cd37a603d9f70b73c1cf785f3e292b (plain)
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
// <mdspan> -*- C++ -*-

// Copyright The GNU Toolchain Authors.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file mdspan
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_MDSPAN
#define _GLIBCXX_MDSPAN 1

#ifdef _GLIBCXX_SYSHDR
#pragma GCC system_header
#endif

#include <span>
#include <array>
#include <type_traits>
#include <utility>

#define __glibcxx_want_mdspan
#define __glibcxx_want_aligned_accessor
#define __glibcxx_want_submdspan
#include <bits/version.h>

#if __glibcxx_aligned_accessor
#include <bits/align.h>
#endif

#if __glibcxx_submdspan
#include <tuple>
#endif


#ifdef __glibcxx_mdspan

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  namespace __mdspan
  {
    consteval bool
    __all_static(std::span<const size_t> __extents)
    {
      for(auto __ext : __extents)
	if (__ext == dynamic_extent)
	  return false;
      return true;
    }

    consteval bool
    __all_dynamic(std::span<const size_t> __extents)
    {
      for(auto __ext : __extents)
	if (__ext != dynamic_extent)
	  return false;
      return true;
    }

    template<typename _IndexType, typename _OIndexType>
      constexpr _IndexType
      __index_type_cast(_OIndexType&& __other)
      {
	if constexpr (std::is_integral_v<_OIndexType>)
	  {
	    constexpr _IndexType __index_type_max
	      = __gnu_cxx::__int_traits<_IndexType>::__max;
	    constexpr _OIndexType __oindex_type_max
	      = __gnu_cxx::__int_traits<_OIndexType>::__max;

	    if constexpr (__index_type_max < __oindex_type_max)
	      __glibcxx_assert(cmp_less_equal(__other, __index_type_max));

	    if constexpr (std::is_signed_v<_OIndexType>)
	      __glibcxx_assert(__other >= 0);
	    return static_cast<_IndexType>(__other);
	  }
	else
	  {
	    auto __ret = static_cast<_IndexType>(std::move(__other));
	    if constexpr (std::is_signed_v<_IndexType>)
	      __glibcxx_assert(__ret >= 0);
	    return __ret;
	  }
      }

    template<array _Extents>
      class _StaticExtents
      {
      public:
	static constexpr size_t _S_rank = _Extents.size();

	// For __r in [0, _S_rank], _S_dynamic_index(__r) is the number
	// of dynamic extents up to (and not including) __r.
	//
	// If __r is the index of a dynamic extent, then
	// _S_dynamic_index[__r] is the index of that extent in
	// _M_dyn_exts.
	static constexpr size_t
	_S_dynamic_index(size_t __r) noexcept
	{ return _S_dynamic_index_data[__r]; }

	static constexpr auto _S_dynamic_index_data = [] consteval
	{
	  array<size_t, _S_rank+1> __ret;
	  size_t __dyn = 0;
	  for (size_t __i = 0; __i < _S_rank; ++__i)
	    {
	      __ret[__i] = __dyn;
	      __dyn += (_Extents[__i] == dynamic_extent);
	    }
	  __ret[_S_rank] = __dyn;
	  return __ret;
	}();

	static constexpr size_t _S_rank_dynamic = _S_dynamic_index(_S_rank);

	// For __r in [0, _S_rank_dynamic), _S_dynamic_index_inv(__r) is the
	// index of the __r-th dynamic extent in _Extents.
	static constexpr size_t
	_S_dynamic_index_inv(size_t __r) noexcept
	{ return _S_dynamic_index_inv_data[__r]; }

	static constexpr auto _S_dynamic_index_inv_data = [] consteval
	{
	  array<size_t, _S_rank_dynamic> __ret;
	  for (size_t __i = 0, __r = 0; __i < _S_rank; ++__i)
	    if (_Extents[__i] == dynamic_extent)
	      __ret[__r++] = __i;
	  return __ret;
	}();

	static constexpr size_t
	_S_static_extent(size_t __r) noexcept
	{ return _Extents[__r]; }
      };

    template<array _Extents>
      requires (__all_dynamic<_Extents>())
      class _StaticExtents<_Extents>
      {
      public:
	static constexpr size_t _S_rank = _Extents.size();

	static constexpr size_t
	_S_dynamic_index(size_t __r) noexcept
	{ return __r; }

	static constexpr size_t _S_rank_dynamic = _S_rank;

	static constexpr size_t
	_S_dynamic_index_inv(size_t __k) noexcept
	{ return __k; }

	static constexpr size_t
	_S_static_extent(size_t) noexcept
	{ return dynamic_extent; }
      };

    template<typename _IndexType, array _Extents>
      class _ExtentsStorage : public _StaticExtents<_Extents>
      {
      private:
	using _Base = _StaticExtents<_Extents>;

      public:
	using _Base::_S_rank;
	using _Base::_S_rank_dynamic;
	using _Base::_S_dynamic_index;
	using _Base::_S_dynamic_index_inv;
	using _Base::_S_static_extent;

	static constexpr bool
	_S_is_dynamic(size_t __r) noexcept
	{
	  if constexpr (__all_static(_Extents))
	    return false;
	  else if constexpr (__all_dynamic(_Extents))
	    return true;
	  else
	    return _Extents[__r] == dynamic_extent;
	}

	template<typename _OIndexType>
	  static constexpr _IndexType
	  _S_int_cast(const _OIndexType& __other) noexcept
	  { return _IndexType(__other); }

	constexpr _IndexType
	_M_extent(size_t __r) const noexcept
	{
	  if (_S_is_dynamic(__r))
	    return _M_dyn_exts[_S_dynamic_index(__r)];
	  else
	    return _S_static_extent(__r);
	}

	template<size_t _OtherRank, typename _GetOtherExtent>
	  static constexpr bool
	  _S_is_compatible_extents(_GetOtherExtent __get_extent) noexcept
	  {
	    if constexpr (_OtherRank == _S_rank)
	      for (size_t __i = 0; __i < _S_rank; ++__i)
		if (!_S_is_dynamic(__i)
		    && !cmp_equal(_Extents[__i], _S_int_cast(__get_extent(__i))))
		  return false;
	    return true;
	  }

	template<size_t _OtherRank, typename _GetOtherExtent>
	  constexpr void
	  _M_init_dynamic_extents(_GetOtherExtent __get_extent) noexcept
	  {
	    __glibcxx_assert(_S_is_compatible_extents<_OtherRank>(__get_extent));
	    for (size_t __i = 0; __i < _S_rank_dynamic; ++__i)
	      {
		size_t __di = __i;
		if constexpr (_OtherRank != _S_rank_dynamic)
		  __di = _S_dynamic_index_inv(__i);
		_M_dyn_exts[__i] = _S_int_cast(__get_extent(__di));
	      }
	  }

	constexpr
	_ExtentsStorage() noexcept = default;

	template<typename _OIndexType, array _OExtents>
	  constexpr
	  _ExtentsStorage(const _ExtentsStorage<_OIndexType, _OExtents>&
			  __other) noexcept
	  {
	    _M_init_dynamic_extents<_S_rank>([&__other](size_t __i)
	      { return __other._M_extent(__i); });
	  }

	template<typename _OIndexType, size_t _Nm>
	  constexpr
	  _ExtentsStorage(span<const _OIndexType, _Nm> __exts) noexcept
	  {
	    _M_init_dynamic_extents<_Nm>(
	      [&__exts](size_t __i) -> const _OIndexType&
	      { return __exts[__i]; });
	  }

	static constexpr const array<size_t, _S_rank>&
	_S_static_extents() noexcept
	{ return _Extents; }

	constexpr span<const _IndexType>
	_M_dynamic_extents(size_t __begin, size_t __end) const noexcept
	requires (_Extents.size() > 0)
	{
	  return {_M_dyn_exts + _S_dynamic_index(__begin),
		  _M_dyn_exts + _S_dynamic_index(__end)};
	}

      private:
	using _Storage = __array_traits<_IndexType, _S_rank_dynamic>::_Type;
	[[no_unique_address]] _Storage _M_dyn_exts{};
      };

    template<typename _OIndexType, typename _SIndexType>
      concept __valid_index_type =
	is_convertible_v<_OIndexType, _SIndexType> &&
	is_nothrow_constructible_v<_SIndexType, _OIndexType>;

    template<size_t _Extent, typename _IndexType>
      concept
      __valid_static_extent = _Extent == dynamic_extent
	|| _Extent <= __gnu_cxx::__int_traits<_IndexType>::__max;

    template<typename _Extents>
      constexpr const array<size_t, _Extents::rank()>&
      __static_extents() noexcept
      { return _Extents::_Storage::_S_static_extents(); }

    template<typename _Extents>
      constexpr span<const size_t>
      __static_extents(size_t __begin, size_t __end) noexcept
      {
	const auto& __sta_exts = __static_extents<_Extents>();
	return span<const size_t>(__sta_exts.data() + __begin, __end - __begin);
      }

    // Pre-compute: \prod_{i = 0}^r _Extents[i], for r = 0,..., n (exclusive)
    template<array _Extents>
      constexpr auto __fwd_partial_prods = [] consteval
	{
	  constexpr size_t __rank = _Extents.size();
	  std::array<size_t, __rank> __ret;
	  size_t __prod = 1;
	  for (size_t __r = 0; __r < __rank; ++__r)
	    {
	      __ret[__r] = __prod;
	      if (size_t __ext = _Extents[__r]; __ext != dynamic_extent)
		__prod *= __ext;
	    }
	  return __ret;
	}();

    // Pre-compute: \prod_{i = r+1}^{n-1} _Extents[i]
    template<array _Extents>
      constexpr auto __rev_partial_prods = [] consteval
	{
	  constexpr size_t __rank = _Extents.size();
	  std::array<size_t, __rank> __ret;
	  size_t __prod = 1;
	  for (size_t __r = __rank; __r > 0; --__r)
	    {
	      __ret[__r - 1] = __prod;
	      if (size_t __ext = _Extents[__r - 1]; __ext != dynamic_extent)
		__prod *= __ext;
	    }
	  return __ret;
	}();

    template<typename _Extents>
      constexpr span<const typename _Extents::index_type>
      __dynamic_extents(const _Extents& __exts, size_t __begin = 0,
			size_t __end = _Extents::rank()) noexcept
      { return __exts._M_exts._M_dynamic_extents(__begin, __end); }
  }

#if __glibcxx_submdspan
  struct full_extent_t
  {
    explicit full_extent_t() = default;
  };

  inline constexpr full_extent_t full_extent{};

  template<typename _OffsetType, typename _ExtentType, typename _StrideType>
    struct strided_slice
    {
      static_assert(__is_standard_integer<_OffsetType>::value
	|| __detail::__integral_constant_like<_OffsetType>);
      static_assert(__is_standard_integer<_ExtentType>::value
	|| __detail::__integral_constant_like<_ExtentType>);
      static_assert(__is_standard_integer<_StrideType>::value
	|| __detail::__integral_constant_like<_StrideType>);

      using offset_type = _OffsetType;
      using extent_type = _ExtentType;
      using stride_type = _StrideType;

      [[no_unique_address]] offset_type offset{};
      [[no_unique_address]] extent_type extent{};
      [[no_unique_address]] stride_type stride{};
    };

  template<typename _Mapping>
    struct submdspan_mapping_result
    {
      [[no_unique_address]] _Mapping mapping = _Mapping();
      size_t offset{};
    };
#endif // __glibcxx_submdspan

  template<typename _IndexType, size_t... _Extents>
    class extents
    {
      static_assert(__is_standard_integer<_IndexType>::value,
		    "IndexType must be a signed or unsigned integer type");
      static_assert(
	  (__mdspan::__valid_static_extent<_Extents, _IndexType> && ...),
	  "Extents must either be dynamic or representable as IndexType");

      using _Storage = __mdspan::_ExtentsStorage<
	_IndexType, array<size_t, sizeof...(_Extents)>{_Extents...}>;
      [[no_unique_address]] _Storage _M_exts;

    public:
      using index_type = _IndexType;
      using size_type = make_unsigned_t<index_type>;
      using rank_type = size_t;

      static constexpr rank_type
      rank() noexcept { return _Storage::_S_rank; }

      static constexpr rank_type
      rank_dynamic() noexcept { return _Storage::_S_rank_dynamic; }

      static constexpr size_t
      static_extent(rank_type __r) noexcept
      {
	__glibcxx_assert(__r < rank());
	if constexpr (rank() == 0)
	  __builtin_trap();
	else
	  return _Storage::_S_static_extent(__r);
      }

      constexpr index_type
      extent(rank_type __r) const noexcept
      {
	__glibcxx_assert(__r < rank());
	if constexpr (rank() == 0)
	  __builtin_trap();
	else
	  return _M_exts._M_extent(__r);
      }

      constexpr
      extents() noexcept = default;

    private:
      static consteval bool
      _S_is_less_dynamic(size_t __ext, size_t __oext)
      { return (__ext != dynamic_extent) && (__oext == dynamic_extent); }

      template<typename _OIndexType, size_t... _OExtents>
	static consteval bool
	_S_ctor_explicit()
	{
	  return (_S_is_less_dynamic(_Extents, _OExtents) || ...)
	    || (__gnu_cxx::__int_traits<index_type>::__max
		< __gnu_cxx::__int_traits<_OIndexType>::__max);
	}

      template<size_t... _OExtents>
	static consteval bool
	_S_is_compatible_extents()
	{
	  if constexpr (sizeof...(_OExtents) != rank())
	    return false;
	  else
	    return ((_OExtents == dynamic_extent || _Extents == dynamic_extent
		     || _OExtents == _Extents) && ...);
	}

    public:
      template<typename _OIndexType, size_t... _OExtents>
	requires (_S_is_compatible_extents<_OExtents...>())
	constexpr explicit(_S_ctor_explicit<_OIndexType, _OExtents...>())
	extents(const extents<_OIndexType, _OExtents...>& __other) noexcept
	: _M_exts(__other._M_exts)
	{ }

      template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
	requires (sizeof...(_OIndexTypes) == rank()
		  || sizeof...(_OIndexTypes) == rank_dynamic())
	constexpr explicit extents(_OIndexTypes... __exts) noexcept
	: _M_exts(span<const _IndexType, sizeof...(_OIndexTypes)>(
	    initializer_list{static_cast<_IndexType>(std::move(__exts))...}))
	{ }

      template<typename _OIndexType, size_t _Nm>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
	  && (_Nm == rank() || _Nm == rank_dynamic())
	constexpr explicit(_Nm != rank_dynamic())
	extents(span<_OIndexType, _Nm> __exts) noexcept
	: _M_exts(span<const _OIndexType, _Nm>(__exts))
	{ }

      template<typename _OIndexType, size_t _Nm>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
	  && (_Nm == rank() || _Nm == rank_dynamic())
	constexpr explicit(_Nm != rank_dynamic())
	extents(const array<_OIndexType, _Nm>& __exts) noexcept
	: _M_exts(span<const _OIndexType, _Nm>(__exts))
	{ }

      template<typename _OIndexType, size_t... _OExtents>
	friend constexpr bool
	operator==(const extents& __self,
		   const extents<_OIndexType, _OExtents...>& __other) noexcept
	{
	  if constexpr (!_S_is_compatible_extents<_OExtents...>())
	    return false;
	  else
	    {
	      auto __impl = [&__self, &__other]<size_t... _Counts>(
		  index_sequence<_Counts...>)
		{ return (cmp_equal(__self.extent(_Counts),
				    __other.extent(_Counts)) && ...); };
	      return __impl(make_index_sequence<__self.rank()>());
	    }
	}

    private:
      friend constexpr const array<size_t, rank()>&
      __mdspan::__static_extents<extents>() noexcept;

      friend constexpr span<const index_type>
      __mdspan::__dynamic_extents<extents>(const extents&, size_t, size_t)
      noexcept;

      template<typename _OIndexType, size_t... _OExtents>
	friend class extents;
    };

  namespace __mdspan
  {
    template<typename _Tp, size_t _Nm>
      constexpr bool
      __contains_zero(span<_Tp, _Nm> __exts) noexcept
      {
	for (size_t __i = 0; __i < __exts.size(); ++__i)
	  if (__exts[__i] == 0)
	    return true;
	return false;
      }

    template<typename _Tp, size_t _Nm>
      consteval bool
      __contains_zero(const array<_Tp, _Nm>& __exts) noexcept
      { return __contains_zero(span<const _Tp>(__exts)); }

    template<typename _Extents>
      constexpr bool
      __empty(const _Extents& __exts) noexcept
      {
	if constexpr (__contains_zero(__static_extents<_Extents>()))
	  return true;
	else if constexpr (_Extents::rank_dynamic() > 0)
	  return __contains_zero(__dynamic_extents(__exts));
	else
	  return false;
      }

    template<typename _Extents>
      constexpr typename _Extents::index_type
      __extents_prod(const _Extents& __exts, size_t __sta_prod, size_t __begin,
		     size_t __end) noexcept
      {
	if (__sta_prod == 0)
	  return 0;

	size_t __ret = __sta_prod;
	if constexpr (_Extents::rank_dynamic() > 0)
	  for (auto __factor : __dynamic_extents(__exts, __begin, __end))
	    __ret *= size_t(__factor);
	return static_cast<typename _Extents::index_type>(__ret);
      }

    // Preconditions: _r < _Extents::rank()
    template<typename _Extents>
      constexpr typename _Extents::index_type
      __fwd_prod(const _Extents& __exts, size_t __begin, size_t __end) noexcept
      {
	size_t __sta_prod = [__begin, __end] {
	  span<const size_t> __sta_exts
	    = __static_extents<_Extents>(__begin, __end);
	  size_t __ret = 1;
	  for(auto __ext : __sta_exts)
	    if (__ext != dynamic_extent)
	      __ret *= __ext;
	  return __ret;
	}();
	return __extents_prod(__exts, __sta_prod, __begin, __end);
      }

    template<typename _Extents>
      constexpr typename _Extents::index_type
      __fwd_prod(const _Extents& __exts, size_t __r) noexcept
      {
	constexpr size_t __rank = _Extents::rank();
	constexpr auto& __sta_exts = __static_extents<_Extents>();
	if constexpr (__rank == 1)
	  return 1;
	else if constexpr (__rank == 2)
	  return __r == 0 ? 1 : __exts.extent(0);
	else if constexpr (__all_dynamic(std::span(__sta_exts).first(__rank-1)))
	  return __extents_prod(__exts, 1, 0, __r);
	else
	  {
	    size_t __sta_prod = __fwd_partial_prods<__sta_exts>[__r];
	    return __extents_prod(__exts, __sta_prod, 0, __r);
	  }
      }

    // Preconditions: _r < _Extents::rank()
    template<typename _Extents>
      constexpr typename _Extents::index_type
      __rev_prod(const _Extents& __exts, size_t __r) noexcept
      {
	constexpr size_t __rank = _Extents::rank();
	constexpr auto& __sta_exts = __static_extents<_Extents>();
	if constexpr (__rank == 1)
	  return 1;
	else if constexpr (__rank == 2)
	  return __r == 0 ? __exts.extent(1) : 1;
	else if constexpr (__all_dynamic(std::span(__sta_exts).last(__rank-1)))
	  return __extents_prod(__exts, 1, __r + 1, __rank);
	else
	  {
	    size_t __sta_prod = __rev_partial_prods<__sta_exts>[__r];
	    return __extents_prod(__exts, __sta_prod, __r + 1, __rank);
	  }
      }

    template<typename _Extents>
      constexpr typename _Extents::index_type
      __size(const _Extents& __exts) noexcept
      {
	constexpr size_t __sta_prod = [] {
	  span<const size_t> __sta_exts = __static_extents<_Extents>();
	  size_t __ret = 1;
	  for(auto __ext : __sta_exts)
	    if (__ext != dynamic_extent)
	      __ret *= __ext;
	  return __ret;
	}();
	return __extents_prod(__exts, __sta_prod, 0, _Extents::rank());
      }

    template<typename _IndexType, size_t... _Counts>
      auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
	-> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
  }

  template<typename _IndexType, size_t _Rank>
    using dextents = decltype(__mdspan::__build_dextents_type<_IndexType>(
	make_index_sequence<_Rank>()));

#if __glibcxx_mdspan >= 202406L
  template<size_t _Rank, typename _IndexType = size_t>
    using dims = dextents<_IndexType, _Rank>;
#endif

  template<typename... _Integrals>
    requires (is_convertible_v<_Integrals, size_t> && ...)
    explicit extents(_Integrals...) ->
      extents<size_t, __detail::__maybe_static_ext<_Integrals>...>;

  struct layout_left
  {
    template<typename _Extents>
      class mapping;
  };

  struct layout_right
  {
    template<typename _Extents>
      class mapping;
  };

  struct layout_stride
  {
    template<typename _Extents>
      class mapping;
  };

#ifdef __glibcxx_padded_layouts
  template<size_t _PaddingValue>
    struct layout_left_padded
    {
      template<typename _Extents>
	class mapping;
    };

  template<size_t _PaddingValue>
    struct layout_right_padded
    {
      template<typename _Extents>
	class mapping;
    };
#endif

  namespace __mdspan
  {
    template<typename _Tp>
      constexpr bool __is_extents = false;

    template<typename _IndexType, size_t... _Extents>
      constexpr bool __is_extents<extents<_IndexType, _Extents...>> = true;

    template<typename _Extents, typename... _Indices>
      constexpr typename _Extents::index_type
      __linear_index_left(const _Extents& __exts, _Indices... __indices)
      noexcept
      {
	using _IndexType = typename _Extents::index_type;
	_IndexType __res = 0;
	if constexpr (sizeof...(__indices) > 0)
	  {
	    _IndexType __mult = 1;
	    auto __update = [&, __pos = 0u](_IndexType __idx) mutable
	      {
		_GLIBCXX_DEBUG_ASSERT(cmp_less(__idx, __exts.extent(__pos)));
		__res += __idx * __mult;
		__mult *= __exts.extent(__pos);
		++__pos;
	      };
	    (__update(__indices), ...);
	  }
	return __res;
      }

    template<typename _IndexType>
      consteval _IndexType
      __static_quotient(std::span<const size_t> __sta_exts,
	_IndexType __nom = __gnu_cxx::__int_traits<_IndexType>::__max)
      {
	for (auto __factor : __sta_exts)
	  {
	    if (__factor != dynamic_extent)
	      __nom /= _IndexType(__factor);
	    if (__nom == 0)
	      break;
	  }
	return __nom;
      }

    template<typename _Extents,
	     typename _IndexType = typename _Extents::index_type>
      requires __is_extents<_Extents>
      consteval _IndexType
      __static_quotient(_IndexType __nom
			  = __gnu_cxx::__int_traits<_IndexType>::__max)
      {
	std::span<const size_t> __sta_exts = __static_extents<_Extents>();
	return __static_quotient<_IndexType>(__sta_exts, __nom);
      }

    template<typename _Extents>
      constexpr bool
      __is_representable_extents(const _Extents& __exts) noexcept
      {
	using _IndexType = _Extents::index_type;

	if constexpr (__contains_zero(__static_extents<_Extents>()))
	  return true;
	else
	  {
	    constexpr auto __sta_quo = __static_quotient<_Extents>();
	    if constexpr (_Extents::rank_dynamic() == 0)
	      return __sta_quo != 0;
	    else
	      {
		auto __dyn_exts = __dynamic_extents(__exts);
		if (__contains_zero(__dyn_exts))
		  return true;

		if constexpr (__sta_quo == 0)
		  return false;
		else
		  {
		    auto __dyn_quo = _IndexType(__sta_quo);
		    for (auto __factor : __dyn_exts)
		      {
			__dyn_quo /= __factor;
			if (__dyn_quo == 0)
			  return false;
		      }
		    return true;
		  }
	      }
	  }
      }

    template<typename _Extents, typename _IndexType>
      concept __representable_size = _Extents::rank_dynamic() != 0
	|| __contains_zero(__static_extents<_Extents>())
	|| (__static_quotient<_Extents, _IndexType>() != 0);

    template<typename _Layout, typename _Mapping>
      concept __mapping_of =
	is_same_v<typename _Layout::template mapping<
		    typename _Mapping::extents_type>,
		  _Mapping>;

    template<template<size_t> typename _Layout, typename _Mapping>
      concept __padded_mapping_of = __mapping_of<
	_Layout<_Mapping::padding_value>, _Mapping>;

#ifdef __glibcxx_padded_layouts
    template<typename _Mapping>
      constexpr bool __is_left_padded_mapping = __padded_mapping_of<
	layout_left_padded, _Mapping>;

    template<typename _Mapping>
      constexpr bool __is_right_padded_mapping = __padded_mapping_of<
	layout_right_padded, _Mapping>;
#endif

    template<typename _PaddedMapping>
      consteval size_t
      __get_static_stride()
      { return _PaddedMapping::_PaddedStorage::_S_static_stride; }

    template<typename _Mapping>
      concept __standardized_mapping = __mapping_of<layout_left, _Mapping>
				       || __mapping_of<layout_right, _Mapping>
				       || __mapping_of<layout_stride, _Mapping>
#ifdef __glibcxx_padded_layouts
				       || __is_left_padded_mapping<_Mapping>
				       || __is_right_padded_mapping<_Mapping>
#endif
				       ;

    // A tag type to create internal ctors.
    class __internal_ctor
    { };

    template<typename _Mapping>
      constexpr typename _Mapping::index_type
      __offset(const _Mapping& __m) noexcept
      {
	using _IndexType = typename _Mapping::index_type;
	constexpr auto __rank = _Mapping::extents_type::rank();

	if constexpr (__standardized_mapping<_Mapping>)
	  return 0;
	else if (__empty(__m.extents()))
	  return 0;
	else
	  {
	    auto __impl = [&__m]<size_t... _Counts>(index_sequence<_Counts...>)
	      { return __m(((void) _Counts, _IndexType(0))...); };
	    return __impl(make_index_sequence<__rank>());
	  }
      }

#ifdef __glibcxx_submdspan
    template<typename _Tp>
      constexpr bool __is_strided_slice = false;

    template<typename _OffsetType, typename _ExtentType, typename _StrideType>
      constexpr bool __is_strided_slice<strided_slice<_OffsetType,
	  _ExtentType, _StrideType>> = true;

    template<typename _IndexType, typename _OIndexType>
      consteval bool
      __is_representable_integer(_OIndexType __value)
      {
	constexpr auto __min = __gnu_cxx::__int_traits<_IndexType>::__min;
	constexpr auto __max = __gnu_cxx::__int_traits<_IndexType>::__max;
	return std::cmp_less_equal(__min, __value)
	    && std::cmp_less_equal(__value, __max);
      }

    template<typename _Tp>
      constexpr bool __is_constant_wrapper = false;

    template<_CwFixedValue _Xv, typename _Tp>
      constexpr bool __is_constant_wrapper<constant_wrapper<_Xv, _Tp>>
	= true;

    template<size_t _Index, typename _Extents>
      constexpr auto
      __extract_extent(const _Extents& __exts)
      {
	using _IndexType = typename _Extents::index_type;
	return extents<_IndexType, _Extents::static_extent(_Index)>{
	  __exts.extent(_Index)};
      }

    template<typename _IndexType, typename... _Slices>
      consteval auto
      __subrank()
      {
	return (static_cast<size_t>(!convertible_to<_Slices, _IndexType>)
		+ ... + 0);
      }

    template<typename _IndexType, typename... _Slices>
      consteval auto
      __inv_map_rank()
      {
	constexpr auto __rank = sizeof...(_Slices);
	constexpr auto __sub_rank = __subrank<_IndexType, _Slices...>();
	auto __map = std::array<size_t, __sub_rank>{};
	auto __is_int_like = std::array<bool, __rank>{
	  convertible_to<_Slices, _IndexType>...};

	size_t __i = 0;
	for (size_t __k = 0; __k < __rank; ++__k)
	  if (!__is_int_like[__k])
	    __map[__i++] = __k;
	return __map;
      }
#endif // __glibcxx_submdspan
  }

  template<typename _Extents>
    class layout_left::mapping
    {
    public:
      using extents_type = _Extents;
      using index_type = typename extents_type::index_type;
      using size_type = typename extents_type::size_type;
      using rank_type = typename extents_type::rank_type;
      using layout_type = layout_left;

      static_assert(__mdspan::__representable_size<extents_type, index_type>,
	"The size of extents_type must be representable as index_type");

      constexpr
      mapping() noexcept = default;

      constexpr
      mapping(const mapping&) noexcept = default;

      constexpr
      mapping(const extents_type& __extents) noexcept
      : _M_extents(__extents)
      { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }

      template<typename _OExtents>
	requires is_constructible_v<extents_type, _OExtents>
	constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
	mapping(const mapping<_OExtents>& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{ }

      template<typename _OExtents>
	requires (extents_type::rank() <= 1)
		  && is_constructible_v<extents_type, _OExtents>
	constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
	mapping(const layout_right::mapping<_OExtents>& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{ }

      // noexcept for consistency with other layouts.
      template<typename _OExtents>
	requires is_constructible_v<extents_type, _OExtents>
	constexpr explicit(!(extents_type::rank() == 0
	                     && is_convertible_v<_OExtents, extents_type>))
	mapping(const layout_stride::mapping<_OExtents>& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{ __glibcxx_assert(*this == __other); }

#if __glibcxx_padded_layouts
      template<typename _LeftpadMapping>
	requires __mdspan::__is_left_padded_mapping<_LeftpadMapping>
		 && is_constructible_v<extents_type,
				       typename _LeftpadMapping::extents_type>
	constexpr
	explicit(!is_convertible_v<typename _LeftpadMapping::extents_type,
				   extents_type>)
	mapping(const _LeftpadMapping& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{
	  constexpr size_t __ostride_sta
	    = __mdspan::__get_static_stride<_LeftpadMapping>();

	  if constexpr (extents_type::rank() > 1)
	    {
	      if constexpr (extents_type::static_extent(0) != dynamic_extent
			    && __ostride_sta != dynamic_extent)
		static_assert(extents_type::static_extent(0) == __ostride_sta);
	      else
		__glibcxx_assert(__other.stride(1)
		    == __other.extents().extent(0));
	    }
	}
#endif // __glibcxx_padded_layouts

      constexpr mapping&
      operator=(const mapping&) noexcept = default;

      constexpr const extents_type&
      extents() const noexcept { return _M_extents; }

      constexpr index_type
      required_span_size() const noexcept
      { return __mdspan::__size(_M_extents); }

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 4314. Missing move in mdspan layout mapping::operator()
      template<__mdspan::__valid_index_type<index_type>... _Indices>
	requires (sizeof...(_Indices) == extents_type::rank())
	constexpr index_type
	operator()(_Indices... __indices) const noexcept
	{
	  return __mdspan::__linear_index_left(_M_extents,
	    static_cast<index_type>(std::move(__indices))...);
	}

      static constexpr bool
      is_always_unique() noexcept { return true; }

      static constexpr bool
      is_always_exhaustive() noexcept { return true; }

      static constexpr bool
      is_always_strided() noexcept { return true; }

      static constexpr bool
      is_unique() noexcept { return true; }

      static constexpr bool
      is_exhaustive() noexcept { return true; }

      static constexpr bool
      is_strided() noexcept { return true; }

      constexpr index_type
      stride(rank_type __i) const noexcept
      requires (extents_type::rank() > 0)
      {
	__glibcxx_assert(__i < extents_type::rank());
	return __mdspan::__fwd_prod(_M_extents, __i);
      }

      template<typename _OExtents>
	requires (extents_type::rank() == _OExtents::rank())
	friend constexpr bool
	operator==(const mapping& __self, const mapping<_OExtents>& __other)
	noexcept
	{ return __self.extents() == __other.extents(); }

    private:
      template<typename _OExtents>
	constexpr explicit
	mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
	: _M_extents(__oexts)
	{
	  static_assert(__mdspan::__representable_size<_OExtents, index_type>,
	    "The size of OtherExtents must be representable as index_type");
	  __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
	}

	[[no_unique_address]] extents_type _M_extents{};
    };

  namespace __mdspan
  {
    template<typename _Extents, typename... _Indices>
      constexpr typename _Extents::index_type
      __linear_index_right(const _Extents& __exts, _Indices... __indices)
      noexcept
      {
	using _IndexType = typename _Extents::index_type;
	array<_IndexType, sizeof...(__indices)> __ind_arr{__indices...};
	_IndexType __res = 0;
	if constexpr (sizeof...(__indices) > 0)
	  {
	    _IndexType __mult = 1;
	    auto __update = [&, __pos = __exts.rank()](_IndexType) mutable
	      {
		--__pos;
		_GLIBCXX_DEBUG_ASSERT(cmp_less(__ind_arr[__pos],
					       __exts.extent(__pos)));
		__res += __ind_arr[__pos] * __mult;
		__mult *= __exts.extent(__pos);
	      };
	    (__update(__indices), ...);
	  }
	return __res;
      }
  }

  template<typename _Extents>
    class layout_right::mapping
    {
    public:
      using extents_type = _Extents;
      using index_type = typename extents_type::index_type;
      using size_type = typename extents_type::size_type;
      using rank_type = typename extents_type::rank_type;
      using layout_type = layout_right;

      static_assert(__mdspan::__representable_size<extents_type, index_type>,
	"The size of extents_type must be representable as index_type");

      constexpr
      mapping() noexcept = default;

      constexpr
      mapping(const mapping&) noexcept = default;

      constexpr
      mapping(const extents_type& __extents) noexcept
      : _M_extents(__extents)
      { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }

      template<typename _OExtents>
	requires is_constructible_v<extents_type, _OExtents>
	constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
	mapping(const mapping<_OExtents>& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{ }

      template<typename _OExtents>
	requires (extents_type::rank() <= 1)
	    && is_constructible_v<extents_type, _OExtents>
	constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
	mapping(const layout_left::mapping<_OExtents>& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{ }

      template<typename _OExtents>
	requires is_constructible_v<extents_type, _OExtents>
	constexpr explicit(!(extents_type::rank() == 0
	                     && is_convertible_v<_OExtents, extents_type>))
	mapping(const layout_stride::mapping<_OExtents>& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{ __glibcxx_assert(*this == __other); }

#if __glibcxx_padded_layouts
      template<typename _RightPaddedMapping>
	requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
		 && is_constructible_v<extents_type,
		      typename _RightPaddedMapping::extents_type>
	constexpr
	explicit(!is_convertible_v<typename _RightPaddedMapping::extents_type,
				   extents_type>)
	mapping(const _RightPaddedMapping& __other) noexcept
	: mapping(__other.extents(), __mdspan::__internal_ctor{})
	{
	  constexpr size_t __rank = extents_type::rank();
	  constexpr size_t __ostride_sta
	    = __mdspan::__get_static_stride<_RightPaddedMapping>();

	  if constexpr (__rank > 1)
	    {
	      if constexpr (extents_type::static_extent(__rank - 1) != dynamic_extent
		  && __ostride_sta != dynamic_extent)
		static_assert(extents_type::static_extent(__rank - 1)
		    == __ostride_sta);
	      else
		__glibcxx_assert(__other.stride(__rank - 2)
		    == __other.extents().extent(__rank - 1));
	    }
	}
#endif

      constexpr mapping&
      operator=(const mapping&) noexcept = default;

      constexpr const extents_type&
      extents() const noexcept { return _M_extents; }

      constexpr index_type
      required_span_size() const noexcept
      { return __mdspan::__size(_M_extents); }

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 4314. Missing move in mdspan layout mapping::operator()
      template<__mdspan::__valid_index_type<index_type>... _Indices>
	requires (sizeof...(_Indices) == extents_type::rank())
	constexpr index_type
	operator()(_Indices... __indices) const noexcept
	{
	  return __mdspan::__linear_index_right(
	    _M_extents, static_cast<index_type>(std::move(__indices))...);
	}

      static constexpr bool
      is_always_unique() noexcept
      { return true; }

      static constexpr bool
      is_always_exhaustive() noexcept
      { return true; }

      static constexpr bool
      is_always_strided() noexcept
      { return true; }

      static constexpr bool
      is_unique() noexcept
      { return true; }

      static constexpr bool
      is_exhaustive() noexcept
      { return true; }

      static constexpr bool
      is_strided() noexcept
      { return true; }

      constexpr index_type
      stride(rank_type __i) const noexcept
      requires (extents_type::rank() > 0)
      {
	__glibcxx_assert(__i < extents_type::rank());
	return __mdspan::__rev_prod(_M_extents, __i);
      }

      template<typename _OExtents>
	requires (extents_type::rank() == _OExtents::rank())
	friend constexpr bool
	operator==(const mapping& __self, const mapping<_OExtents>& __other)
	noexcept
	{ return __self.extents() == __other.extents(); }

    private:
      template<typename _OExtents>
	constexpr explicit
	mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
	: _M_extents(__oexts)
	{
	  static_assert(__mdspan::__representable_size<_OExtents, index_type>,
	    "The size of OtherExtents must be representable as index_type");
	  __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
	}

	[[no_unique_address]] extents_type _M_extents{};
    };

  namespace __mdspan
  {
    template<typename _Mp>
      concept __mapping_alike = requires
      {
	requires __is_extents<typename _Mp::extents_type>;
	{ _Mp::is_always_strided() } -> same_as<bool>;
	{ _Mp::is_always_exhaustive() } -> same_as<bool>;
	{ _Mp::is_always_unique() } -> same_as<bool>;
	bool_constant<_Mp::is_always_strided()>::value;
	bool_constant<_Mp::is_always_exhaustive()>::value;
	bool_constant<_Mp::is_always_unique()>::value;
      };

    template<typename _Mapping, typename... _Indices>
      constexpr typename _Mapping::index_type
      __linear_index_strides(const _Mapping& __m, _Indices... __indices)
      noexcept
      {
	using _IndexType = typename _Mapping::index_type;
	_IndexType __res = 0;
	if constexpr (sizeof...(__indices) > 0)
	  {
	    auto __update = [&, __pos = 0u](_IndexType __idx) mutable
	      {
		_GLIBCXX_DEBUG_ASSERT(cmp_less(__idx,
					       __m.extents().extent(__pos)));
		__res += __idx * __m.stride(__pos++);
	      };
	    (__update(__indices), ...);
	  }
	return __res;
      }
  }

  template<typename _Extents>
    class layout_stride::mapping
    {
    public:
      using extents_type = _Extents;
      using index_type = typename extents_type::index_type;
      using size_type = typename extents_type::size_type;
      using rank_type = typename extents_type::rank_type;
      using layout_type = layout_stride;

      static_assert(__mdspan::__representable_size<extents_type, index_type>,
	"The size of extents_type must be representable as index_type");

      constexpr
      mapping() noexcept
      {
	// The precondition is either statically asserted, or automatically
	// satisfied because dynamic extents are zero-initialized.
	size_t __stride = 1;
	for (size_t __i = extents_type::rank(); __i > 0; --__i)
	  {
	    _M_strides[__i - 1] = index_type(__stride);
	    __stride *= size_t(_M_extents.extent(__i - 1));
	  }
      }

      constexpr
      mapping(const mapping&) noexcept = default;

      template<typename _OIndexType>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
	constexpr
	mapping(const extents_type& __exts,
		span<_OIndexType, extents_type::rank()> __strides) noexcept
	: _M_extents(__exts)
	{
	  for (size_t __i = 0; __i < extents_type::rank(); ++__i)
	    _M_strides[__i] = index_type(as_const(__strides[__i]));
	}

      template<typename _OIndexType>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
	constexpr
	mapping(const extents_type& __exts,
		const array<_OIndexType, extents_type::rank()>& __strides)
	noexcept
	: mapping(__exts,
		  span<const _OIndexType, extents_type::rank()>(__strides))
	{ }

      template<__mdspan::__mapping_alike _StridedMapping>
	requires (is_constructible_v<extents_type,
				     typename _StridedMapping::extents_type>
		  && _StridedMapping::is_always_unique()
		  && _StridedMapping::is_always_strided())
	constexpr explicit(!(
	  is_convertible_v<typename _StridedMapping::extents_type, extents_type>
	  && __mdspan::__standardized_mapping<_StridedMapping>))
	mapping(const _StridedMapping& __other) noexcept
	: _M_extents(__other.extents())
	{
	  using _OIndexType = _StridedMapping::index_type;
	  using _OExtents = _StridedMapping::extents_type;

	  __glibcxx_assert(__mdspan::__offset(__other) == 0);
	  static_assert(__mdspan::__representable_size<_OExtents, index_type>,
	    "The size of StridedMapping::extents_type must be representable as"
	    " index_type");
	  if constexpr (cmp_greater(__gnu_cxx::__int_traits<_OIndexType>::__max,
				    __gnu_cxx::__int_traits<index_type>::__max))
	    __glibcxx_assert(!cmp_less(
		__gnu_cxx::__int_traits<index_type>::__max,
		__other.required_span_size())
	      && "other.required_span_size() must be representable"
		 " as index_type");
	  if constexpr (extents_type::rank() > 0)
	    for (size_t __i = 0; __i < extents_type::rank(); ++__i)
	      _M_strides[__i] = index_type(__other.stride(__i));
	}

      constexpr mapping&
      operator=(const mapping&) noexcept = default;

      constexpr const extents_type&
      extents() const noexcept { return _M_extents; }

      constexpr array<index_type, extents_type::rank()>
      strides() const noexcept
      {
	array<index_type, extents_type::rank()> __ret;
	for (size_t __i = 0; __i < extents_type::rank(); ++__i)
	  __ret[__i] = _M_strides[__i];
	return __ret;
      }

      constexpr index_type
      required_span_size() const noexcept
      {
	if (__mdspan::__empty(_M_extents))
	  return 0;

	index_type __ret = 1;
	for (size_t __i = 0; __i < extents_type::rank(); ++__i)
	  __ret += (_M_extents.extent(__i) - 1) * _M_strides[__i];
	return __ret;
      }

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 4314. Missing move in mdspan layout mapping::operator()
      template<__mdspan::__valid_index_type<index_type>... _Indices>
	requires (sizeof...(_Indices) == extents_type::rank())
	constexpr index_type
	operator()(_Indices... __indices) const noexcept
	{
	  return __mdspan::__linear_index_strides(*this,
	    static_cast<index_type>(std::move(__indices))...);
	}

      static constexpr bool
      is_always_unique() noexcept { return true; }

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 4266. layout_stride::mapping should treat empty mappings as exhaustive
      static constexpr bool
      is_always_exhaustive() noexcept
      {
	return (_Extents::rank() == 0) || __mdspan::__contains_zero(
	    __mdspan::__static_extents<extents_type>());
      }

      static constexpr bool
      is_always_strided() noexcept { return true; }

      static constexpr bool
      is_unique() noexcept { return true; }

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 4266. layout_stride::mapping should treat empty mappings as exhaustive
      constexpr bool
      is_exhaustive() const noexcept
      {
	if constexpr (!is_always_exhaustive())
	  {
	    auto __size = __mdspan::__size(_M_extents);
	    if(__size > 0)
	      return __size == required_span_size();
	  }
	return true;
      }

      static constexpr bool
      is_strided() noexcept { return true; }

      constexpr index_type
      stride(rank_type __r) const noexcept { return _M_strides[__r]; }

      template<__mdspan::__mapping_alike _OMapping>
	requires ((extents_type::rank() == _OMapping::extents_type::rank())
		  && _OMapping::is_always_strided())
	friend constexpr bool
	operator==(const mapping& __self, const _OMapping& __other) noexcept
	{
	  if (__self.extents() != __other.extents())
	    return false;
	  if constexpr (extents_type::rank() > 0)
	    for (size_t __i = 0; __i < extents_type::rank(); ++__i)
	      if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
		return false;
	  return __mdspan::__offset(__other) == 0;
	}

    private:
      using _Strides = typename __array_traits<index_type,
					       extents_type::rank()>::_Type;
      [[no_unique_address]] extents_type _M_extents;
      [[no_unique_address]] _Strides _M_strides;
    };

#ifdef __glibcxx_padded_layouts
  namespace __mdspan
  {
    constexpr size_t
    __least_multiple(size_t __x, size_t __y)
    {
      if (__x <= 1)
	return __y;
      return (__y / __x + (__y % __x != 0)) * __x ;
    }

    template<typename _IndexType>
    constexpr bool
    __is_representable_least_multiple(size_t __x, size_t __y)
    {
      constexpr auto __y_max = __gnu_cxx::__int_traits<_IndexType>::__max;
      if(std::cmp_greater(__y, __y_max))
	return false;

      if(__x <= 1)
	return true;

      auto __max_delta = __y_max - static_cast<_IndexType>(__y);
      auto __y_mod_x = __y % __x;
      auto __delta = (__y_mod_x == 0) ? size_t(0) : (__x - __y_mod_x);
      return std::cmp_less_equal(__delta, __max_delta);
    }

    template<typename _Extents, size_t _PaddingValue, typename _LayoutTraits,
	     size_t _Rank = _Extents::rank()>
      concept __valid_static_stride = (_Extents::rank() <= 1)
	|| (_PaddingValue == dynamic_extent)
	|| (_Extents::static_extent(_LayoutTraits::_S_ext_idx) == dynamic_extent)
	|| (__is_representable_least_multiple<size_t>(
	    _PaddingValue, _Extents::static_extent(_LayoutTraits::_S_ext_idx)));

    template<size_t _PaddedStride, typename _Extents,
	     typename _LayoutTraits>
      consteval bool
      __is_representable_padded_size()
      {
	using _IndexType = typename _Extents::index_type;
	auto __sta_exts = __static_extents<_Extents>(
	  _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
	size_t __max = __gnu_cxx::__int_traits<_IndexType>::__max;
	return __static_quotient(__sta_exts, __max / _PaddedStride) != 0;
      }

    template<typename _Extents, size_t _PaddedStride, typename _LayoutTraits,
	     size_t _Rank = _Extents::rank()>
      concept __valid_padded_size = (_Rank <= 1)
	|| (_PaddedStride == dynamic_extent)
	|| (!__all_static(__static_extents<_Extents>()))
	|| (__contains_zero(__static_extents<_Extents>()))
	|| (__is_representable_padded_size<_PaddedStride, _Extents,
					   _LayoutTraits>());

    template<typename _Extents, typename _Stride, typename... _Indices>
      constexpr typename _Extents::index_type
      __linear_index_leftpad(const _Extents& __exts, _Stride __stride,
			     _Indices... __indices)
      {
	// i0 + stride*(i1 + extents.extent(1)*...)
	using _IndexType = typename _Extents::index_type;
	_IndexType __res = 0;
	if constexpr (sizeof...(__indices) > 0)
	  {
	    _IndexType __mult = 1;

	    auto __update_rest = [&, __pos = 1u](_IndexType __idx) mutable
	      {
		__res += __idx * __mult;
		__mult *= __exts.extent(__pos);
		++__pos;
	      };

	    auto __update = [&](_IndexType __idx, auto... __rest)
	      {
		__res += __idx;
		__mult = __stride.extent(0);
		(__update_rest(__rest), ...);
	      };
	    __update(__indices...);
	  }
	return __res;
      }

    template<typename _Extents, typename _Stride, typename... _Indices>
      constexpr typename _Extents::index_type
      __linear_index_rightpad(const _Extents& __exts, _Stride __stride,
			     _Indices... __indices)
      {
	// i[n-1] + stride*(i[n-2] + extents.extent(n-2])*...)
	using _IndexType = typename _Extents::index_type;
	_IndexType __res = 0;
	if constexpr (sizeof...(__indices) > 0)
	  {
	    _IndexType __mult = 1;
	    array<_IndexType, sizeof...(__indices)> __ind_arr{__indices...};

	    auto __update_rest = [&, __pos = __exts.rank()-1](_IndexType) mutable
	      {
		--__pos;
		__res += __ind_arr[__pos] * __mult;
		__mult *= __exts.extent(__pos);
	      };

	    auto __update = [&](_IndexType, auto... __rest)
	      {
		__res += __ind_arr[__exts.rank() - 1];
		__mult = __stride.extent(0);
		(__update_rest(__rest), ...);
	      };
	    __update(__indices...);
	  }
	return __res;
      }

    template<size_t _Rank>
      struct _LeftPaddedLayoutTraits
      {
	using _LayoutSame = layout_left;
	using _LayoutOther = layout_right;

	constexpr static const size_t _S_ext_idx = 0;
	constexpr static const size_t _S_stride_idx = 1;
	constexpr static const size_t _S_unpad_begin = 1;
	constexpr static const size_t _S_unpad_end = _Rank;

	template<typename _IndexType, size_t _StaticStride, size_t..._Extents>
	  constexpr static auto
	  _S_make_padded_extent(
	    extents<_IndexType, _StaticStride> __stride,
	    const extents<_IndexType, _Extents...>& __exts)
	  {
	    auto __impl = [&]<size_t... _Is>(integer_sequence<size_t, _Is...>)
	    {
	      return extents<_IndexType, _StaticStride,
			     (_Extents...[_Is + 1])...>{
		__stride.extent(0), __exts.extent(_Is + 1)...};
	    };
	    return __impl(make_index_sequence<sizeof...(_Extents) - 1>());
	  }
      };

    template<size_t _Rank>
      struct _RightPaddedLayoutTraits
      {
	using _LayoutSame = layout_right;
	using _LayoutOther = layout_left;

	constexpr static size_t _S_ext_idx = _Rank - 1;
	constexpr static size_t _S_stride_idx = _Rank - 2;
	constexpr static size_t _S_unpad_begin = 0;
	constexpr static size_t _S_unpad_end = _Rank - 1;

	template<typename _IndexType, size_t _StaticStride, size_t..._Extents>
	  constexpr static auto
	  _S_make_padded_extent(
	    extents<_IndexType, _StaticStride> __stride,
	    const extents<_IndexType, _Extents...>& __exts)
	  {
	    auto __impl = [&]<size_t... _Is>(integer_sequence<size_t, _Is...>)
	    {
	      return extents<_IndexType, (_Extents...[_Is])..., _StaticStride>{
		__exts.extent(_Is)..., __stride.extent(0)};
	    };
	    return __impl(make_index_sequence<sizeof...(_Extents) - 1>());
	  }
      };

    template<size_t _PaddingValue, typename _Extents, typename _LayoutTraits>
      class _PaddedStorage
      {
	using _LayoutSame = typename _LayoutTraits::_LayoutSame;

      public:
	using _IndexType = typename _Extents::index_type;
	constexpr static size_t _S_rank = _Extents::rank();

	// _GLIBCXX_RESOLVE_LIB_DEFECTS
	// 4372. Weaken Mandates: for dynamic padding values in padded layouts
	static_assert((_PaddingValue == dynamic_extent)
	    || (cmp_less_equal(_PaddingValue,
			       __gnu_cxx::__int_traits<_IndexType>::__max)),
	  "padding_value must be representable as index_type");

	static_assert(__representable_size<_Extents, _IndexType>,
	  "The size of extents_type must be representable as index_type");

	static_assert(__valid_static_stride<_Extents, _PaddingValue,
					    _LayoutTraits>,
	  "The padded stride must be representable as size_t");

	static constexpr size_t _S_static_stride = [] consteval
	{
	  constexpr size_t __rank = _Extents::rank();
	  if constexpr (__rank <= 1)
	    return 0;
	  else
	    {
	      constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
	      constexpr size_t __sta_ext = _Extents::static_extent(__ext_idx);
	      if constexpr (__sta_ext == 0)
		return size_t(0);
	      else if constexpr (_PaddingValue == dynamic_extent
		  || __sta_ext == dynamic_extent)
		return dynamic_extent;
	      else
		return __least_multiple(_PaddingValue, __sta_ext);
	    }
	}();

	static_assert(_S_static_stride == dynamic_extent
	  || cmp_less_equal(_S_static_stride,
			    __gnu_cxx::__int_traits<_IndexType>::__max),
	  "Padded stride must be representable as index_type");

	static_assert(__valid_padded_size<_Extents, _S_static_stride,
					  _LayoutTraits>);

	constexpr
	_PaddedStorage() noexcept
	{
	  if constexpr (_S_rank > 1)
	    if constexpr (_S_static_stride == dynamic_extent
			  && _S_static_padextent() != dynamic_extent)
	      _M_stride = _Stride{_S_static_padextent()};
	}

	constexpr explicit
	_PaddedStorage(const _Extents& __exts)
	: _M_extents(__exts)
	{
	  if constexpr (!__all_static(__static_extents<_Extents>()))
	    __glibcxx_assert(__is_representable_extents(_M_extents));

	  if constexpr (_S_rank > 1)
	    {
	      _IndexType __stride;
	      if constexpr (_PaddingValue == dynamic_extent)
		__stride = _M_padextent();
	      else if constexpr (_S_static_padextent() != dynamic_extent)
		return;
	      else
		{
		  __glibcxx_assert(
		      __is_representable_least_multiple<_IndexType>(
			_PaddingValue, _M_padextent()));

		  __stride = static_cast<_IndexType>(
		    __least_multiple(_PaddingValue, _M_padextent()));

		  __glibcxx_assert(__is_representable_extents(
		      _LayoutTraits::_S_make_padded_extent(
			std::dextents<_IndexType, 1>{__stride},
			_M_extents)));
		}
	      _M_stride = _Stride{__stride};
	    }
	}

	constexpr explicit
	_PaddedStorage(const _Extents& __exts, _IndexType __pad)
	: _M_extents(__exts)
	{
	  if constexpr (_PaddingValue != dynamic_extent)
	    __glibcxx_assert(cmp_equal(_PaddingValue, __pad));
	  if constexpr (_S_rank > 1 && _S_static_stride == dynamic_extent)
	    {
	      __glibcxx_assert(
		__is_representable_least_multiple<_IndexType>(
		  __pad, _M_padextent()));

	      _M_stride = _Stride{static_cast<_IndexType>(
		__least_multiple(__pad, _M_padextent()))};

	      __glibcxx_assert(__is_representable_extents(
		_LayoutTraits::_S_make_padded_extent(
		  _M_stride, _M_extents)));
	    }
	}

	template<typename _OExtents>
	  constexpr explicit
	  _PaddedStorage(
	      const typename _LayoutSame::template mapping<_OExtents>& __other)
	  : _PaddedStorage(_Extents(__other.extents()))
	  {
	    constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
	    constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
	    if constexpr (_S_rank > 1 && _PaddingValue != dynamic_extent)
	      {
		static_assert(_S_static_stride == dynamic_extent
		    || _OExtents::static_extent(__ext_idx) == dynamic_extent
		    || _S_static_stride == _OExtents::static_extent(__ext_idx),
		  "The padded stride must be compatible with other");

		if constexpr (_S_static_stride == dynamic_extent
		    || _OExtents::static_extent(__stride_idx) == dynamic_extent)
		  __glibcxx_assert(std::cmp_equal(_M_padstride(),
						  _M_padextent()));
	      }
	  }

	template<typename _OExtents>
	  constexpr explicit
	  _PaddedStorage(const typename layout_stride::mapping<_OExtents>&
			 __other)
	  : _M_extents(__other.extents())
	  {
	    __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
					    __gnu_cxx::__int_traits<_IndexType>
						     ::__max));

	    constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
	    if constexpr (_S_rank > 1)
	      {
		if constexpr (_PaddingValue != dynamic_extent)
		  __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
					     _M_calc_padstride())
		    && "The padded stride must be compatible with other");
		if constexpr (_S_static_stride == dynamic_extent)
		  _M_stride = _Stride{__other.stride(__stride_idx)};
	      }
	  }

	template<typename _SamePaddedMapping>
	  constexpr explicit
	  _PaddedStorage(_LayoutTraits::_LayoutSame,
			 const _SamePaddedMapping& __other)
	  : _M_extents(__other.extents())
	  {
	    if constexpr (_S_rank > 1)
	      {
		static_assert(_PaddingValue == dynamic_extent
		    || _SamePaddedMapping::padding_value  == dynamic_extent
		    || _PaddingValue == _SamePaddedMapping::padding_value,
		  "If neither PaddingValue is dynamic_extent, then they must "
		  "be equal");

		constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
		if constexpr (_PaddingValue != dynamic_extent)
		  __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
					     _M_calc_padstride())
		    && "The padded stride must be compatible with other");
		if constexpr (_S_static_stride == dynamic_extent)
		  _M_stride = _Stride{__other.stride(__stride_idx)};
	      }
	    __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
		__gnu_cxx::__int_traits<_IndexType>::__max));
	  }

	template<typename _OtherPaddedMapping>
	  constexpr explicit
	  _PaddedStorage(_LayoutTraits::_LayoutOther,
			 const _OtherPaddedMapping& __other) noexcept
	  : _M_extents(__other.extents())
	  {
	    __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
		__gnu_cxx::__int_traits<_IndexType>::__max));
	  }

	static constexpr bool
	_M_is_always_exhaustive() noexcept
	{
	  if constexpr (_S_rank <= 1)
	    return true;
	  else
	    return _S_static_padextent() != dynamic_extent
		   && _S_static_stride != dynamic_extent
		   && _S_static_padextent() == _S_static_stride;
	}

	constexpr bool
	_M_is_exhaustive() const noexcept
	{
	  if constexpr (_M_is_always_exhaustive())
	    return true;
	  else
	    return cmp_equal(_M_padextent(), _M_padstride());
	}

	constexpr static size_t
	_S_static_padextent() noexcept
	{ return _Extents::static_extent(_LayoutTraits::_S_ext_idx); }

	constexpr _IndexType
	_M_padextent() const noexcept
	{ return _M_extents.extent(_LayoutTraits::_S_ext_idx); }

	constexpr _IndexType
	_M_calc_padstride() const noexcept
	{
	  if constexpr (_S_static_stride != dynamic_extent)
	    return _S_static_stride;
	  else if constexpr (_PaddingValue != dynamic_extent)
	    return __least_multiple(_PaddingValue, _M_padextent());
	  else
	    return _M_padextent();
	}

	constexpr _IndexType
	_M_padstride() const noexcept
	{ return _M_stride.extent(0); }

	constexpr _IndexType
	_M_required_span_size() const noexcept
	{
	  if constexpr (_S_rank == 0)
	    return 1;
	  else if (__mdspan::__empty(_M_extents))
	    return 0;
	  else
	    {
	      size_t __stride = static_cast<size_t>(_M_padstride());
	      size_t __prod_rest = __mdspan::__fwd_prod(_M_extents,
		_LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
	      size_t __delta = _M_padstride() - _M_padextent();
	      return static_cast<_IndexType>(__stride * __prod_rest - __delta);
	    }
	}

	template<typename _SamePaddedMapping>
	  constexpr bool
	  _M_equal(const _SamePaddedMapping& __other) const noexcept
	  {
	    return _M_extents == __other.extents()
	      && (_S_rank < 2
		|| cmp_equal(_M_stride.extent(0),
			     __other.stride(_LayoutTraits::_S_stride_idx)));
	  }

	using _Stride = std::extents<_IndexType, _S_static_stride>;
	[[no_unique_address]] _Stride _M_stride;
	[[no_unique_address]] _Extents _M_extents;
      };
  }

  template<size_t _PaddingValue>
    template<typename _Extents>
      class layout_left_padded<_PaddingValue>::mapping
      {
      public:
	static constexpr size_t padding_value = _PaddingValue;

	using extents_type = _Extents;
	using index_type = typename extents_type::index_type;
	using size_type = typename extents_type::size_type;
	using rank_type = typename extents_type::rank_type;
	using layout_type = layout_left_padded<padding_value>;

      private:
	static constexpr size_t _S_rank = extents_type::rank();
	using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
	      _Extents, __mdspan::_LeftPaddedLayoutTraits<_S_rank>>;
	[[no_unique_address]] _PaddedStorage _M_storage;

	consteval friend size_t
	__mdspan::__get_static_stride<mapping>();

	constexpr index_type
	_M_extent(size_t __r) const noexcept
	{ return _M_storage._M_extents.extent(__r); }

	constexpr index_type
	_M_padstride() const noexcept
	{ return _M_storage._M_stride.extent(0); }

      public:
	constexpr
	mapping() noexcept
	{ }

	constexpr
	mapping(const mapping&) noexcept = default;

	constexpr
	mapping(const extents_type& __exts)
	: _M_storage(__exts)
	{ }

	template<__mdspan::__valid_index_type<index_type> _OIndexType>
	  constexpr
	  mapping(const extents_type& __exts, _OIndexType __pad)
	  : _M_storage(__exts,
	      __mdspan::__index_type_cast<index_type>(std::move(__pad)))
	  { }

	template<typename _OExtents>
	  requires is_constructible_v<extents_type, _OExtents>
	  constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
	  mapping(const layout_left::mapping<_OExtents>& __other)
	  : _M_storage(__other)
	  { }

	template<typename _OExtents>
	  requires is_constructible_v<_OExtents, extents_type>
	  constexpr explicit(!(_OExtents::rank() == 0
			       && is_convertible_v<_OExtents, extents_type>))
	  mapping(const typename layout_stride::mapping<_OExtents>& __other)
	  : _M_storage(__other)
	  { __glibcxx_assert(*this == __other); }

	template<typename _LeftPaddedMapping>
	  requires __mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
	      && is_constructible_v<extents_type,
				    typename _LeftPaddedMapping::extents_type>
	  constexpr explicit(
	   !is_convertible_v<typename _LeftPaddedMapping::extents_type,
			     extents_type>
	   || _S_rank > 1 && (padding_value != dynamic_extent
		|| _LeftPaddedMapping::padding_value == dynamic_extent))
	  mapping(const _LeftPaddedMapping& __other)
	  : _M_storage(layout_left{}, __other)
	  { }

	template<typename _RightPaddedMapping>
	  requires (__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
	      || __mdspan::__mapping_of<layout_right, _RightPaddedMapping>)
	    && (_S_rank <= 1)
	    && is_constructible_v<extents_type,
				  typename _RightPaddedMapping::extents_type>
	  constexpr explicit(!is_convertible_v<
	      typename _RightPaddedMapping::extents_type, extents_type>)
	  mapping(const _RightPaddedMapping& __other) noexcept
	  : _M_storage(layout_right{}, __other)
	  { }

	constexpr mapping&
	operator=(const mapping&) noexcept = default;

	constexpr const extents_type&
	extents() const noexcept { return _M_storage._M_extents; }

	constexpr array<index_type, _S_rank>
	strides() const noexcept
	{
	  array<index_type, _S_rank> __ret;
	  if constexpr (_S_rank > 0)
	    __ret[0] = 1;
	  if constexpr (_S_rank > 1)
	    __ret[1] = _M_padstride();
	  if constexpr (_S_rank > 2)
	    for(size_t __i = 2; __i < _S_rank; ++__i)
	      __ret[__i] = __ret[__i - 1] * _M_extent(__i - 1);
	  return __ret;
	}

	constexpr index_type
	required_span_size() const noexcept
	{ return _M_storage._M_required_span_size(); }

	// _GLIBCXX_RESOLVE_LIB_DEFECTS
	// 4314. Missing move in mdspan layout mapping::operator()
	template<__mdspan::__valid_index_type<index_type>... _Indices>
	  requires (sizeof...(_Indices) == _S_rank)
	  constexpr index_type
	  operator()(_Indices... __indices) const noexcept
	  {
	    return __mdspan::__linear_index_leftpad(
	      extents(), _M_storage._M_stride,
	      static_cast<index_type>(std::move(__indices))...);
	  }

	static constexpr bool
	is_always_exhaustive() noexcept
	{ return _PaddedStorage::_M_is_always_exhaustive(); }

	constexpr bool
	is_exhaustive() noexcept
	{ return _M_storage._M_is_exhaustive(); }

	static constexpr bool
	is_always_unique() noexcept { return true; }

	static constexpr bool
	is_unique() noexcept { return true; }

	static constexpr bool
	is_always_strided() noexcept { return true; }

	static constexpr bool
	is_strided() noexcept { return true; }

	constexpr index_type
	stride(rank_type __r) const noexcept
	{
	  __glibcxx_assert(__r < _S_rank);
	  if (__r == 0)
	    return 1;
	  else
	    return static_cast<index_type>(
	      static_cast<size_t>(_M_padstride()) *
	      static_cast<size_t>(__mdspan::__fwd_prod(extents(), 1, __r)));
	}

	template<typename _LeftpadMapping>
	  requires(__mdspan::__is_left_padded_mapping<_LeftpadMapping>
		   && _LeftpadMapping::extents_type::rank() == _S_rank)
	  friend constexpr bool
	  operator==(const mapping& __self, const _LeftpadMapping& __other)
	  noexcept
	  { return __self._M_storage._M_equal(__other); }
      };

  template<size_t _PaddingValue>
    template<typename _Extents>
      class layout_right_padded<_PaddingValue>::mapping {
      public:
	static constexpr size_t padding_value = _PaddingValue;
	using extents_type = _Extents;
	using index_type = typename extents_type::index_type;
	using size_type = typename extents_type::size_type;
	using rank_type = typename extents_type::rank_type;
	using layout_type = layout_right_padded<_PaddingValue>;

      private:
	static constexpr size_t _S_rank = extents_type::rank();
	using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
	      _Extents, __mdspan::_RightPaddedLayoutTraits<_S_rank>>;
	[[no_unique_address]] _PaddedStorage _M_storage;

	consteval friend size_t
	__mdspan::__get_static_stride<mapping>();

	constexpr index_type
	_M_extent(size_t __r) const noexcept
	{ return _M_storage._M_extents.extent(__r); }

	constexpr index_type
	_M_padstride() const noexcept
	{ return _M_storage._M_stride.extent(0); }

      public:
	constexpr
	mapping() noexcept
	{ }

	constexpr
	mapping(const mapping&) noexcept = default;

	constexpr
	mapping(const extents_type& __exts)
	: _M_storage(__exts)
	{ }

	template<__mdspan::__valid_index_type<index_type> _OIndexType>
	  constexpr
	  mapping(const extents_type& __exts, _OIndexType __pad)
	  : _M_storage(__exts,
	      __mdspan::__index_type_cast<index_type>(std::move(__pad)))
	  { }

	template<typename _OExtents>
	  requires is_constructible_v<extents_type, _OExtents>
	  constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
	  mapping(const layout_right::mapping<_OExtents>& __other)
	  : _M_storage(__other)
	  { }

	template<typename _OExtents>
	  requires is_constructible_v<_OExtents, extents_type>
	  constexpr explicit(!(_OExtents::rank() == 0
			       && is_convertible_v<_OExtents, extents_type>))
	  mapping(const typename layout_stride::mapping<_OExtents>& __other)
	  : _M_storage(__other)
	  { __glibcxx_assert(*this == __other); }

	template<typename _RightPaddedMapping>
	  requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
	      && is_constructible_v<extents_type,
				    typename _RightPaddedMapping::extents_type>
	  constexpr explicit(
	    !is_convertible_v<typename _RightPaddedMapping::extents_type,
			      extents_type>
	    || _S_rank > 1 && (padding_value != dynamic_extent
		 || _RightPaddedMapping::padding_value == dynamic_extent))
	  mapping(const _RightPaddedMapping& __other)
	  : _M_storage(layout_right{}, __other)
	  { }

	template<typename _LeftPaddedMapping>
	  requires (__mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
	      || __mdspan::__mapping_of<layout_left, _LeftPaddedMapping>)
	    && (_S_rank <= 1)
	    && is_constructible_v<extents_type,
				  typename _LeftPaddedMapping::extents_type>
	  constexpr explicit(!is_convertible_v<
	      typename _LeftPaddedMapping::extents_type, extents_type>)
	  mapping(const _LeftPaddedMapping& __other) noexcept
	  : _M_storage(layout_left{}, __other)
	  { }

	constexpr mapping& operator=(const mapping&) noexcept = default;

	constexpr const extents_type&
	extents() const noexcept { return _M_storage._M_extents; }

	constexpr array<index_type, _S_rank>
	strides() const noexcept
	{
	  array<index_type, _S_rank> __ret;
	  if constexpr (_S_rank > 0)
	    __ret[_S_rank - 1] = 1;
	  if constexpr (_S_rank > 1)
	    __ret[_S_rank - 2] = _M_padstride();
	  if constexpr (_S_rank > 2)
	    for(size_t __i = _S_rank - 2; __i > 0; --__i)
	      __ret[__i - 1] = __ret[__i] * _M_extent(__i);
	  return __ret;
	}

	constexpr index_type
	required_span_size() const noexcept
	{ return _M_storage._M_required_span_size(); }

	// _GLIBCXX_RESOLVE_LIB_DEFECTS
	// 4314. Missing move in mdspan layout mapping::operator()
	template<__mdspan::__valid_index_type<index_type>... _Indices>
	  requires (sizeof...(_Indices) == _S_rank)
	  constexpr index_type
	  operator()(_Indices... __indices) const noexcept
	  {
	    return __mdspan::__linear_index_rightpad(
	      extents(), _M_storage._M_stride,
	      static_cast<index_type>(std::move(__indices))...);
	  }

	static constexpr bool
	is_always_exhaustive() noexcept
	{ return _PaddedStorage::_M_is_always_exhaustive(); }

	constexpr bool
	is_exhaustive() noexcept
	{ return _M_storage._M_is_exhaustive(); }

	static constexpr bool
	is_always_unique() noexcept { return true; }

	static constexpr bool
	is_unique() noexcept { return true; }

	static constexpr bool
	is_always_strided() noexcept { return true; }

	static constexpr bool
	is_strided() noexcept { return true; }

	constexpr index_type
	stride(rank_type __r) const noexcept
	{
	  __glibcxx_assert(__r < _S_rank);
	  if constexpr (_S_rank <= 1)
	    return 1;
	  else if (__r == _S_rank - 1)
	    return 1;
	  else if (__r == _S_rank - 2)
	    return _M_padstride();
	  else
	    return static_cast<index_type>(
	      static_cast<size_t>(_M_padstride()) *
	      static_cast<size_t>(__mdspan::__fwd_prod(
		  extents(), __r + 1, _S_rank - 1)));
	}

	template<typename _RightPaddedMapping>
	  requires(__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
		   && _RightPaddedMapping::extents_type::rank() == _S_rank)
	  friend constexpr bool
	  operator==(const mapping& __self, const _RightPaddedMapping& __other)
	  noexcept
	  { return __self._M_storage._M_equal(__other); }
      };
#endif // __glibcxx_padded_layouts

  template<typename _ElementType>
    struct default_accessor
    {
      static_assert(!is_array_v<_ElementType>,
	"ElementType must not be an array type");
      static_assert(!is_abstract_v<_ElementType>,
	"ElementType must not be an abstract class type");

      using offset_policy = default_accessor;
      using element_type = _ElementType;
      using reference = element_type&;
      using data_handle_type = element_type*;

      constexpr
      default_accessor() noexcept = default;

      template<typename _OElementType>
	requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
	constexpr
	default_accessor(default_accessor<_OElementType>) noexcept
	{ }

      constexpr reference
      access(data_handle_type __p, size_t __i) const noexcept
      { return __p[__i]; }

      constexpr data_handle_type
      offset(data_handle_type __p, size_t __i) const noexcept
      { return __p + __i; }
    };

#ifdef __glibcxx_aligned_accessor
  template<typename _ElementType, size_t _ByteAlignment>
    struct aligned_accessor
    {
      static_assert(has_single_bit(_ByteAlignment),
	"ByteAlignment must be a power of two");
      static_assert(_ByteAlignment >= alignof(_ElementType));

      using offset_policy = default_accessor<_ElementType>;
      using element_type = _ElementType;
      using reference = element_type&;
      using data_handle_type = element_type*;

      static constexpr size_t byte_alignment = _ByteAlignment;

      constexpr
      aligned_accessor() noexcept = default;

      template<typename _OElementType, size_t _OByteAlignment>
	requires (_OByteAlignment >= byte_alignment)
	    && is_convertible_v<_OElementType(*)[], element_type(*)[]>
	constexpr
	aligned_accessor(aligned_accessor<_OElementType, _OByteAlignment>)
	noexcept
	{ }

      template<typename _OElementType>
	requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
	constexpr explicit
	aligned_accessor(default_accessor<_OElementType>) noexcept
	{ }

      template<typename _OElementType>
	requires is_convertible_v<element_type(*)[], _OElementType(*)[]>
	constexpr
	operator default_accessor<_OElementType>() const noexcept
	{ return {}; }

      constexpr reference
      access(data_handle_type __p, size_t __i) const noexcept
      { return std::assume_aligned<byte_alignment>(__p)[__i]; }

      constexpr typename offset_policy::data_handle_type
      offset(data_handle_type __p, size_t __i) const noexcept
      { return std::assume_aligned<byte_alignment>(__p) + __i; }
    };
#endif

  namespace __mdspan
  {
    template<typename _Extents, typename _IndexType, size_t _Nm>
      constexpr bool
      __is_multi_index(const _Extents& __exts, span<_IndexType, _Nm> __indices)
      {
	static_assert(__exts.rank() == _Nm);
	for (size_t __i = 0; __i < __exts.rank(); ++__i)
	  if (__indices[__i] >= __exts.extent(__i))
	    return false;
	return true;
      }
  }

  template<typename _ElementType, typename _Extents,
	   typename _LayoutPolicy = layout_right,
	   typename _AccessorPolicy = default_accessor<_ElementType>>
    class mdspan
    {
      static_assert(!is_array_v<_ElementType>,
	"ElementType must not be an array type");
      static_assert(!is_abstract_v<_ElementType>,
	"ElementType must not be an abstract class type");
      static_assert(__mdspan::__is_extents<_Extents>,
	"Extents must be a specialization of std::extents");
      static_assert(is_same_v<_ElementType,
			      typename _AccessorPolicy::element_type>);

    public:
      using extents_type = _Extents;
      using layout_type = _LayoutPolicy;
      using accessor_type = _AccessorPolicy;
      using mapping_type = typename layout_type::template mapping<extents_type>;
      using element_type = _ElementType;
      using value_type = remove_cv_t<element_type>;
      using index_type = typename extents_type::index_type;
      using size_type = typename extents_type::size_type;
      using rank_type = typename extents_type::rank_type;
      using data_handle_type = typename accessor_type::data_handle_type;
      using reference = typename accessor_type::reference;

      static constexpr rank_type
      rank() noexcept { return extents_type::rank(); }

      static constexpr rank_type
      rank_dynamic() noexcept { return extents_type::rank_dynamic(); }

      static constexpr size_t
      static_extent(rank_type __r) noexcept
      { return extents_type::static_extent(__r); }

      constexpr index_type
      extent(rank_type __r) const noexcept { return extents().extent(__r); }

      constexpr
      mdspan()
      requires (rank_dynamic() > 0)
	  && is_default_constructible_v<data_handle_type>
	  && is_default_constructible_v<mapping_type>
	  && is_default_constructible_v<accessor_type> = default;

      constexpr
      mdspan(const mdspan& __other) = default;

      constexpr
      mdspan(mdspan&& __other) = default;

      template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
	requires (sizeof...(_OIndexTypes) == rank()
		   || sizeof...(_OIndexTypes) == rank_dynamic())
		 && is_constructible_v<mapping_type, extents_type>
		 && is_default_constructible_v<accessor_type>
	constexpr explicit
	mdspan(data_handle_type __handle, _OIndexTypes... __exts)
	: _M_accessor(),
	  _M_mapping(_Extents(static_cast<index_type>(std::move(__exts))...)),
	  _M_handle(std::move(__handle))
	{ }

      template<typename _OIndexType, size_t _Nm>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
		 && (_Nm == rank() || _Nm == rank_dynamic())
		 && is_constructible_v<mapping_type, extents_type>
		 && is_default_constructible_v<accessor_type>
	constexpr explicit(_Nm != rank_dynamic())
	mdspan(data_handle_type __handle, span<_OIndexType, _Nm> __exts)
	: _M_accessor(), _M_mapping(extents_type(__exts)),
	  _M_handle(std::move(__handle))
	{ }

      template<typename _OIndexType, size_t _Nm>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
		 && (_Nm == rank() || _Nm == rank_dynamic())
		 && is_constructible_v<mapping_type, extents_type>
		 && is_default_constructible_v<accessor_type>
	constexpr explicit(_Nm != rank_dynamic())
	mdspan(data_handle_type __handle, const array<_OIndexType, _Nm>& __exts)
	: _M_accessor(), _M_mapping(extents_type(__exts)),
	  _M_handle(std::move(__handle))
	{ }

      constexpr
      mdspan(data_handle_type __handle, const extents_type& __exts)
      requires is_constructible_v<mapping_type, const extents_type&>
	       && is_default_constructible_v<accessor_type>
      : _M_accessor(), _M_mapping(__exts), _M_handle(std::move(__handle))
      { }

      constexpr
      mdspan(data_handle_type __handle, const mapping_type& __mapping)
      requires is_default_constructible_v<accessor_type>
      : _M_accessor(), _M_mapping(__mapping), _M_handle(std::move(__handle))
      { }

      constexpr
      mdspan(data_handle_type __handle, const mapping_type& __mapping,
	     const accessor_type& __accessor)
      : _M_accessor(__accessor), _M_mapping(__mapping),
	_M_handle(std::move(__handle))
      { }

      template<typename _OElementType, typename _OExtents, typename _OLayout,
	       typename _OAccessor>
	requires is_constructible_v<mapping_type,
	      const typename _OLayout::template mapping<_OExtents>&>
	  && is_constructible_v<accessor_type, const _OAccessor&>
	constexpr explicit(!is_convertible_v<
	    const typename _OLayout::template mapping<_OExtents>&, mapping_type>
	  || !is_convertible_v<const _OAccessor&, accessor_type>)
	mdspan(const mdspan<_OElementType, _OExtents, _OLayout, _OAccessor>&
		 __other)
	: _M_accessor(__other.accessor()), _M_mapping(__other.mapping()),
	  _M_handle(__other.data_handle())
	{
	  static_assert(is_constructible_v<data_handle_type,
	      const typename _OAccessor::data_handle_type&>);
	  static_assert(is_constructible_v<extents_type, _OExtents>);
	}

      constexpr mdspan&
      operator=(const mdspan& __other) = default;

      constexpr mdspan&
      operator=(mdspan&& __other) = default;

      template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
	requires (sizeof...(_OIndexTypes) == rank())
	constexpr reference
	operator[](_OIndexTypes... __indices) const
	{
	  auto __checked_call = [this](auto... __idxs) -> index_type
	    {
	      if constexpr (sizeof...(__idxs) > 0)
		__glibcxx_assert(__mdspan::__is_multi_index(extents(),
		  span<const index_type, sizeof...(__idxs)>({__idxs...})));
	      return _M_mapping(__idxs...);
	    };

	  auto __index = __checked_call(
	    static_cast<index_type>(std::move(__indices))...);
	  return _M_accessor.access(_M_handle, __index);
	}

      template<typename _OIndexType>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
	constexpr reference
	operator[](span<_OIndexType, rank()> __indices) const
	{
	  auto __call = [&]<size_t... _Counts>(index_sequence<_Counts...>)
	    -> reference
	    { return (*this)[index_type(as_const(__indices[_Counts]))...]; };
	  return __call(make_index_sequence<rank()>());
	}

      template<typename _OIndexType>
	requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
	constexpr reference
	operator[](const array<_OIndexType, rank()>& __indices) const
	{ return (*this)[span<const _OIndexType, rank()>(__indices)]; }

      constexpr size_type
      size() const noexcept
      {
	__glibcxx_assert(cmp_less_equal(_M_mapping.required_span_size(),
					__gnu_cxx::__int_traits<size_t>
						 ::__max));
	return size_type(__mdspan::__size(extents()));
      }

      [[nodiscard]]
      constexpr bool
      empty() const noexcept
      { return __mdspan::__empty(extents()); }

      friend constexpr void
      swap(mdspan& __x, mdspan& __y) noexcept
      {
	using std::swap;
	swap(__x._M_mapping, __y._M_mapping);
	swap(__x._M_accessor, __y._M_accessor);
	swap(__x._M_handle, __y._M_handle);
      }

      constexpr const extents_type&
      extents() const noexcept { return _M_mapping.extents(); }

      constexpr const data_handle_type&
      data_handle() const noexcept { return _M_handle; }

      constexpr const mapping_type&
      mapping() const noexcept { return _M_mapping; }

      constexpr const accessor_type&
      accessor() const noexcept { return _M_accessor; }

      // Strengthened noexcept for all `is_*` methods.

      static constexpr bool
      is_always_unique() noexcept(noexcept(mapping_type::is_always_unique()))
      { return mapping_type::is_always_unique(); }

      static constexpr bool
      is_always_exhaustive()
      noexcept(noexcept(mapping_type::is_always_exhaustive()))
      { return mapping_type::is_always_exhaustive(); }

      static constexpr bool
      is_always_strided()
      noexcept(noexcept(mapping_type::is_always_strided()))
      { return mapping_type::is_always_strided(); }

      constexpr bool
      is_unique() const noexcept(noexcept(_M_mapping.is_unique()))
      { return _M_mapping.is_unique(); }

      constexpr bool
      is_exhaustive() const noexcept(noexcept(_M_mapping.is_exhaustive()))
      { return _M_mapping.is_exhaustive(); }

      constexpr bool
      is_strided() const noexcept(noexcept(_M_mapping.is_strided()))
      { return _M_mapping.is_strided(); }

      constexpr index_type
      stride(rank_type __r) const { return _M_mapping.stride(__r); }

    private:
      [[no_unique_address]] accessor_type _M_accessor = accessor_type();
      [[no_unique_address]] mapping_type _M_mapping = mapping_type();
      [[no_unique_address]] data_handle_type _M_handle = data_handle_type();
    };

  template<typename _CArray>
    requires is_array_v<_CArray> && (rank_v<_CArray> == 1)
    mdspan(_CArray&)
    -> mdspan<remove_all_extents_t<_CArray>,
	      extents<size_t, extent_v<_CArray, 0>>>;

  template<typename _Pointer>
    requires is_pointer_v<remove_reference_t<_Pointer>>
    mdspan(_Pointer&&)
    -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;

  template<typename _ElementType, typename... _Integrals>
    requires (is_convertible_v<_Integrals, size_t> && ...)
	      && (sizeof...(_Integrals) > 0)
    explicit mdspan(_ElementType*, _Integrals...)
    -> mdspan<_ElementType,
	      extents<size_t, __detail::__maybe_static_ext<_Integrals>...>>;

  template<typename _ElementType, typename _OIndexType, size_t _Nm>
    mdspan(_ElementType*, span<_OIndexType, _Nm>)
    -> mdspan<_ElementType, dextents<size_t, _Nm>>;

  template<typename _ElementType, typename _OIndexType, size_t _Nm>
    mdspan(_ElementType*, const array<_OIndexType, _Nm>&)
    -> mdspan<_ElementType, dextents<size_t, _Nm>>;

  template<typename _ElementType, typename _IndexType, size_t... _ExtentsPack>
    mdspan(_ElementType*, const extents<_IndexType, _ExtentsPack...>&)
    -> mdspan<_ElementType, extents<_IndexType, _ExtentsPack...>>;

  template<typename _ElementType, typename _MappingType>
    mdspan(_ElementType*, const _MappingType&)
    -> mdspan<_ElementType, typename _MappingType::extents_type,
	      typename _MappingType::layout_type>;

  template<typename _MappingType, typename _AccessorType>
    mdspan(const typename _AccessorType::data_handle_type&, const _MappingType&,
	   const _AccessorType&)
    -> mdspan<typename _AccessorType::element_type,
	      typename _MappingType::extents_type,
	      typename _MappingType::layout_type, _AccessorType>;

#if __glibcxx_submdspan
  namespace __mdspan
  {
    template<typename _IndexType, typename _Slice>
      constexpr auto
      __canonical_index(_Slice&& __slice)
      {
	if constexpr (__detail::__integral_constant_like<_Slice>)
	  {
	    static_assert(__is_representable_integer<_IndexType>(_Slice::value));
	    static_assert(_Slice::value >= 0);
	    return std::cw<_IndexType(_Slice::value)>;
	  }
	else
	  return __mdspan::__index_type_cast<_IndexType>(std::move(__slice));
      }

    template<typename _IndexType, typename _Slice>
      constexpr auto
      __slice_cast(_Slice&& __slice)
      {
	using _SliceType = remove_cvref_t<_Slice>;
	if constexpr (is_convertible_v<_SliceType, full_extent_t>)
	  return static_cast<full_extent_t>(std::move(__slice));
	else if constexpr (is_convertible_v<_SliceType, _IndexType>)
	  return __mdspan::__canonical_index<_IndexType>(std::move(__slice));
	else if constexpr (__is_strided_slice<_SliceType>)
	  {
	    auto __extent
	      = __mdspan::__canonical_index<_IndexType>(std::move(__slice.extent));
	    auto __offset
	      = __mdspan::__canonical_index<_IndexType>(std::move(__slice.offset));
	    if constexpr (is_same_v<decltype(__extent),
				    constant_wrapper<_IndexType(0)>>)
	      return strided_slice{
		.offset = __offset,
		.extent = __extent,
		.stride = cw<_IndexType(1)>
	      };
	    else
	      return strided_slice{
		.offset = __offset,
		.extent = __extent,
		.stride
		  = __mdspan::__canonical_index<_IndexType>(std::move(__slice.stride))
	      };
	  }
	else
	  {
	    auto [__sbegin, __send] = std::move(__slice);
	    auto __offset
	      = __mdspan::__canonical_index<_IndexType>(std::move(__sbegin));
	    auto __end
	      = __mdspan::__canonical_index<_IndexType>(std::move(__send));
	    return strided_slice{
	      .offset = __offset,
	      .extent = __mdspan::__canonical_index<_IndexType>(__end - __offset),
	      .stride = cw<_IndexType(1)>
	    };
	  }
      }

    template<typename _IndexType, size_t _Extent, typename _OIndexType>
      constexpr void
      __check_valid_index(const extents<_IndexType, _Extent>& __ext,
			   const _OIndexType& __idx)
      {
	if constexpr (__is_constant_wrapper<_OIndexType>
		      && _Extent != dynamic_extent)
	  {
	    static_assert(_OIndexType::value >= 0);
	    static_assert(std::cmp_less_equal(_OIndexType::value, _Extent));
	  }
	else
	  __glibcxx_assert(__idx <= __ext.extent(0));
}

    template<typename _IndexType, size_t _Extent, typename _Slice>
      constexpr void
      __check_valid_slice(const extents<_IndexType, _Extent>& __ext,
			   const _Slice& __slice)
      {
	if constexpr (__is_strided_slice<_Slice>)
	  {
	    // DEVIATION: For empty slices, P3663r3 does not allow us to check
	    // that this is less than or equal to the k-th extent (at runtime).
	    // We're only allowed to check if __slice.offset, __slice.extent
	    // are constant wrappers and __ext is a static extent.
	    __mdspan::__check_valid_index(__ext, __slice.offset);
	    __mdspan::__check_valid_index(__ext, __slice.extent);

	    if constexpr (__is_constant_wrapper<typename _Slice::extent_type>
			  && __is_constant_wrapper<typename _Slice::stride_type>)
	      static_assert(_Slice::stride_type::value > 0);
	    else
	      __glibcxx_assert(__slice.extent == 0 || __slice.stride > 0);

	    if constexpr (__is_constant_wrapper<typename _Slice::offset_type>
		&& __is_constant_wrapper<typename _Slice::extent_type>
		&& _Extent != dynamic_extent)
	      static_assert(std::cmp_greater_equal(
		  _Extent - _Slice::offset_type::value,
		  _Slice::extent_type::value));
	    else
	      __glibcxx_assert(__ext.extent(0) - __slice.offset
			       >= __slice.extent);
	  }
	else if constexpr (__is_constant_wrapper<_Slice>
			   && _Extent != dynamic_extent)
	  static_assert(std::cmp_less(_Slice::value, _Extent));
	else if constexpr (convertible_to<_Slice, _IndexType>)
	  __glibcxx_assert(__slice < __ext.extent(0));
      }

    template<typename _Extents, typename... _Slices>
      constexpr void
      __check_valid_slices(const _Extents& __exts, const _Slices&... __slices)
      {
	constexpr auto __rank = _Extents::rank();
	auto __impl = [&]<size_t... _Is>(index_sequence<_Is...>)
	{
	  ((__mdspan::__check_valid_slice(__extract_extent<_Is>(__exts),
					  __slices...[_Is])),...);
	};
	__impl(make_index_sequence<__rank>());
      }

    template<typename _IndexType, size_t _Extent, typename _Slice>
      consteval size_t
      __static_slice_extent()
      {
	if constexpr (same_as<_Slice, full_extent_t>)
	  return _Extent;
	else if constexpr (same_as<_Slice, constant_wrapper<_IndexType(0)>>)
	  return 0;
	else if constexpr (__is_constant_wrapper<typename _Slice::extent_type>
			&& __is_constant_wrapper<typename _Slice::stride_type>)
	  return 1 + ((typename _Slice::extent_type{}) - 1)
		   / (typename _Slice::stride_type{});
	else
	  return dynamic_extent;
      }

    template<size_t _K, typename _Extents, typename _Slice>
      constexpr typename _Extents::index_type
      __dynamic_slice_extent(const _Extents& __exts, _Slice __slice)
      {
	if constexpr (__is_strided_slice<_Slice>)
	  return __slice.extent == 0 ? 0 : 1 + (__slice.extent - 1) / __slice.stride;
	else
	  return __exts.extent(_K);
      }

    template<typename _IndexType, size_t... _Extents, typename... _Slices>
      requires (sizeof...(_Slices) == sizeof...(_Extents))
      constexpr auto
      __subextents(const extents<_IndexType, _Extents...>& __exts,
		   _Slices... __slices)
      {
	constexpr auto __inv_map = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
	auto __impl = [&]<size_t... _Indices>(index_sequence<_Indices...>)
	{
	  using _SubExtents = extents<_IndexType,
	      (__mdspan::__static_slice_extent<_IndexType,
		 _Extents...[__inv_map[_Indices]],
		 _Slices...[__inv_map[_Indices]]>())...>;
	  if constexpr (_SubExtents::rank_dynamic() == 0)
	    return _SubExtents{};
	  else
	    {
	      using _StaticSubExtents = __mdspan::_StaticExtents<
		__mdspan::__static_extents<_SubExtents>()>;
	      auto __create = [&]<size_t... _Is>(index_sequence<_Is...>)
	      {
		constexpr auto __slice_idx = [__inv_map](size_t __i) consteval
		{
		  return __inv_map[_StaticSubExtents::_S_dynamic_index_inv(__i)];
		};

		return _SubExtents{
		  (__mdspan::__dynamic_slice_extent<__slice_idx(_Is)>(
		     __exts, __slices...[__slice_idx(_Is)]))...};
	      };
	      constexpr auto __dyn_subrank = _SubExtents::rank_dynamic();
	      return __create(make_index_sequence<__dyn_subrank>());
	    }
	};

	return __impl(make_index_sequence<__inv_map.size()>());
      }
  }

  template<typename _IndexType, size_t... _Extents, typename... _RawSlices>
    requires (sizeof...(_RawSlices) == sizeof...(_Extents))
    constexpr auto
    submdspan_extents(const extents<_IndexType, _Extents...>& __exts,
		      _RawSlices... __raw_slices)
    {
      auto __impl = [&__exts](auto... __slices)
      {
	__mdspan::__check_valid_slices(__exts, __slices...);
	return __mdspan::__subextents(__exts, __slices...);
      };
      return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
    }

  template<typename _IndexType, size_t... _Extents, typename... _RawSlices>
    requires (sizeof...(_Extents) == sizeof...(_RawSlices))
    constexpr auto
    submdspan_canonicalize_slices(const extents<_IndexType, _Extents...>& __exts,
				  _RawSlices... __raw_slices)
    {
      auto __impl = [&__exts](auto... __slices)
      {
	__mdspan::__check_valid_slices(__exts, __slices...);
	return std::make_tuple(__slices...);
      };
      return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
    }
#endif // __glibcxx_submdspan

_GLIBCXX_END_NAMESPACE_VERSION
}
#endif
#endif