summaryrefslogtreecommitdiff
path: root/framework/common/tcuAstcUtil.cpp
blob: 096b8b96812e3c03cb26ba4e9c3dc4c625352c97 (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
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
/*-------------------------------------------------------------------------
 * drawElements Quality Program Tester Core
 * ----------------------------------------
 *
 * Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief ASTC Utilities.
 *//*--------------------------------------------------------------------*/

#include "tcuAstcUtil.hpp"
#include "deFloat16.h"
#include "deRandom.hpp"
#include "deMeta.hpp"

#include <algorithm>

namespace tcu
{
namespace astc
{

using std::vector;

namespace
{

// Common utilities

enum
{
	MAX_BLOCK_WIDTH		= 12,
	MAX_BLOCK_HEIGHT	= 12
};

inline deUint32 getBit (deUint32 src, int ndx)
{
	DE_ASSERT(de::inBounds(ndx, 0, 32));
	return (src >> ndx) & 1;
}

inline deUint32 getBits (deUint32 src, int low, int high)
{
	const int numBits = (high-low) + 1;

	DE_ASSERT(de::inRange(numBits, 1, 32));

	if (numBits < 32)
		return (deUint32)((src >> low) & ((1u<<numBits)-1));
	else
		return (deUint32)((src >> low) & 0xFFFFFFFFu);
}

inline bool isBitSet (deUint32 src, int ndx)
{
	return getBit(src, ndx) != 0;
}

inline deUint32 reverseBits (deUint32 src, int numBits)
{
	DE_ASSERT(de::inRange(numBits, 0, 32));
	deUint32 result = 0;
	for (int i = 0; i < numBits; i++)
		result |= ((src >> i) & 1) << (numBits-1-i);
	return result;
}

inline deUint32 bitReplicationScale (deUint32 src, int numSrcBits, int numDstBits)
{
	DE_ASSERT(numSrcBits <= numDstBits);
	DE_ASSERT((src & ((1<<numSrcBits)-1)) == src);
	deUint32 dst = 0;
	for (int shift = numDstBits-numSrcBits; shift > -numSrcBits; shift -= numSrcBits)
		dst |= shift >= 0 ? src << shift : src >> -shift;
	return dst;
}

inline deInt32 signExtend (deInt32 src, int numSrcBits)
{
	DE_ASSERT(de::inRange(numSrcBits, 2, 31));
	const bool negative = (src & (1 << (numSrcBits-1))) != 0;
	return src | (negative ? ~((1 << numSrcBits) - 1) : 0);
}

inline bool isFloat16InfOrNan (deFloat16 v)
{
	return getBits(v, 10, 14) == 31;
}

enum ISEMode
{
	ISEMODE_TRIT = 0,
	ISEMODE_QUINT,
	ISEMODE_PLAIN_BIT,

	ISEMODE_LAST
};

struct ISEParams
{
	ISEMode		mode;
	int			numBits;

	ISEParams (ISEMode mode_, int numBits_) : mode(mode_), numBits(numBits_) {}
};

inline int computeNumRequiredBits (const ISEParams& iseParams, int numValues)
{
	switch (iseParams.mode)
	{
		case ISEMODE_TRIT:			return deDivRoundUp32(numValues*8, 5) + numValues*iseParams.numBits;
		case ISEMODE_QUINT:			return deDivRoundUp32(numValues*7, 3) + numValues*iseParams.numBits;
		case ISEMODE_PLAIN_BIT:		return numValues*iseParams.numBits;
		default:
			DE_ASSERT(false);
			return -1;
	}
}

ISEParams computeMaximumRangeISEParams (int numAvailableBits, int numValuesInSequence)
{
	int curBitsForTritMode		= 6;
	int curBitsForQuintMode		= 5;
	int curBitsForPlainBitMode	= 8;

	while (true)
	{
		DE_ASSERT(curBitsForTritMode > 0 || curBitsForQuintMode > 0 || curBitsForPlainBitMode > 0);

		const int tritRange			= curBitsForTritMode > 0		? (3 << curBitsForTritMode) - 1			: -1;
		const int quintRange		= curBitsForQuintMode > 0		? (5 << curBitsForQuintMode) - 1		: -1;
		const int plainBitRange		= curBitsForPlainBitMode > 0	? (1 << curBitsForPlainBitMode) - 1		: -1;
		const int maxRange			= de::max(de::max(tritRange, quintRange), plainBitRange);

		if (maxRange == tritRange)
		{
			const ISEParams params(ISEMODE_TRIT, curBitsForTritMode);
			if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
				return ISEParams(ISEMODE_TRIT, curBitsForTritMode);
			curBitsForTritMode--;
		}
		else if (maxRange == quintRange)
		{
			const ISEParams params(ISEMODE_QUINT, curBitsForQuintMode);
			if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
				return ISEParams(ISEMODE_QUINT, curBitsForQuintMode);
			curBitsForQuintMode--;
		}
		else
		{
			const ISEParams params(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);
			DE_ASSERT(maxRange == plainBitRange);
			if (computeNumRequiredBits(params, numValuesInSequence) <= numAvailableBits)
				return ISEParams(ISEMODE_PLAIN_BIT, curBitsForPlainBitMode);
			curBitsForPlainBitMode--;
		}
	}
}

inline int computeNumColorEndpointValues (deUint32 endpointMode)
{
	DE_ASSERT(endpointMode < 16);
	return (endpointMode/4 + 1) * 2;
}

// Decompression utilities

enum DecompressResult
{
	DECOMPRESS_RESULT_VALID_BLOCK	= 0,	//!< Decompressed valid block
	DECOMPRESS_RESULT_ERROR,				//!< Encountered error while decompressing, error color written

	DECOMPRESS_RESULT_LAST
};

// A helper for getting bits from a 128-bit block.
class Block128
{
private:
	typedef deUint64 Word;

	enum
	{
		WORD_BYTES	= sizeof(Word),
		WORD_BITS	= 8*WORD_BYTES,
		NUM_WORDS	= 128 / WORD_BITS
	};

	DE_STATIC_ASSERT(128 % WORD_BITS == 0);

public:
	Block128 (const deUint8* src)
	{
		for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
		{
			m_words[wordNdx] = 0;
			for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
				m_words[wordNdx] |= (Word)src[wordNdx*WORD_BYTES + byteNdx] << (8*byteNdx);
		}
	}

	deUint32 getBit (int ndx) const
	{
		DE_ASSERT(de::inBounds(ndx, 0, 128));
		return (m_words[ndx / WORD_BITS] >> (ndx % WORD_BITS)) & 1;
	}

	deUint32 getBits (int low, int high) const
	{
		DE_ASSERT(de::inBounds(low, 0, 128));
		DE_ASSERT(de::inBounds(high, 0, 128));
		DE_ASSERT(de::inRange(high-low+1, 0, 32));

		if (high-low+1 == 0)
			return 0;

		const int word0Ndx = low / WORD_BITS;
		const int word1Ndx = high / WORD_BITS;

		// \note "foo << bar << 1" done instead of "foo << (bar+1)" to avoid overflow, i.e. shift amount being too big.

		if (word0Ndx == word1Ndx)
			return (deUint32)((m_words[word0Ndx] & ((((Word)1 << high%WORD_BITS << 1) - 1))) >> ((Word)low % WORD_BITS));
		else
		{
			DE_ASSERT(word1Ndx == word0Ndx + 1);

			return (deUint32)(m_words[word0Ndx] >> (low%WORD_BITS)) |
				   (deUint32)((m_words[word1Ndx] & (((Word)1 << high%WORD_BITS << 1) - 1)) << (high-low - high%WORD_BITS));
		}
	}

	bool isBitSet (int ndx) const
	{
		DE_ASSERT(de::inBounds(ndx, 0, 128));
		return getBit(ndx) != 0;
	}

private:
	Word m_words[NUM_WORDS];
};

// A helper for sequential access into a Block128.
class BitAccessStream
{
public:
	BitAccessStream (const Block128& src, int startNdxInSrc, int length, bool forward)
		: m_src				(src)
		, m_startNdxInSrc	(startNdxInSrc)
		, m_length			(length)
		, m_forward			(forward)
		, m_ndx				(0)
	{
	}

	// Get the next num bits. Bits at positions greater than or equal to m_length are zeros.
	deUint32 getNext (int num)
	{
		if (num == 0 || m_ndx >= m_length)
			return 0;

		const int end				= m_ndx + num;
		const int numBitsFromSrc	= de::max(0, de::min(m_length, end) - m_ndx);
		const int low				= m_ndx;
		const int high				= m_ndx + numBitsFromSrc - 1;

		m_ndx += num;

		return m_forward ?			   m_src.getBits(m_startNdxInSrc + low,  m_startNdxInSrc + high)
						 : reverseBits(m_src.getBits(m_startNdxInSrc - high, m_startNdxInSrc - low), numBitsFromSrc);
	}

private:
	const Block128&		m_src;
	const int			m_startNdxInSrc;
	const int			m_length;
	const bool			m_forward;

	int					m_ndx;
};

struct ISEDecodedResult
{
	deUint32 m;
	deUint32 tq; //!< Trit or quint value, depending on ISE mode.
	deUint32 v;
};

// Data from an ASTC block's "block mode" part (i.e. bits [0,10]).
struct ASTCBlockMode
{
	bool		isError;
	// \note Following fields only relevant if !isError.
	bool		isVoidExtent;
	// \note Following fields only relevant if !isVoidExtent.
	bool		isDualPlane;
	int			weightGridWidth;
	int			weightGridHeight;
	ISEParams	weightISEParams;

	ASTCBlockMode (void)
		: isError			(true)
		, isVoidExtent		(true)
		, isDualPlane		(true)
		, weightGridWidth	(-1)
		, weightGridHeight	(-1)
		, weightISEParams	(ISEMODE_LAST, -1)
	{
	}
};

inline int computeNumWeights (const ASTCBlockMode& mode)
{
	return mode.weightGridWidth * mode.weightGridHeight * (mode.isDualPlane ? 2 : 1);
}

struct ColorEndpointPair
{
	UVec4 e0;
	UVec4 e1;
};

struct TexelWeightPair
{
	deUint32 w[2];
};

ASTCBlockMode getASTCBlockMode (deUint32 blockModeData)
{
	ASTCBlockMode blockMode;
	blockMode.isError = true; // \note Set to false later, if not error.

	blockMode.isVoidExtent = getBits(blockModeData, 0, 8) == 0x1fc;

	if (!blockMode.isVoidExtent)
	{
		if ((getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 6, 8) == 7) || getBits(blockModeData, 0, 3) == 0)
			return blockMode; // Invalid ("reserved").

		deUint32 r = (deUint32)-1; // \note Set in the following branches.

		if (getBits(blockModeData, 0, 1) == 0)
		{
			const deUint32 r0	= getBit(blockModeData, 4);
			const deUint32 r1	= getBit(blockModeData, 2);
			const deUint32 r2	= getBit(blockModeData, 3);
			const deUint32 i78	= getBits(blockModeData, 7, 8);

			r = (r2 << 2) | (r1 << 1) | (r0 << 0);

			if (i78 == 3)
			{
				const bool i5 = isBitSet(blockModeData, 5);
				blockMode.weightGridWidth	= i5 ? 10 : 6;
				blockMode.weightGridHeight	= i5 ? 6  : 10;
			}
			else
			{
				const deUint32 a = getBits(blockModeData, 5, 6);
				switch (i78)
				{
					case 0:		blockMode.weightGridWidth = 12;		blockMode.weightGridHeight = a + 2;									break;
					case 1:		blockMode.weightGridWidth = a + 2;	blockMode.weightGridHeight = 12;									break;
					case 2:		blockMode.weightGridWidth = a + 6;	blockMode.weightGridHeight = getBits(blockModeData, 9, 10) + 6;		break;
					default: DE_ASSERT(false);
				}
			}
		}
		else
		{
			const deUint32 r0	= getBit(blockModeData, 4);
			const deUint32 r1	= getBit(blockModeData, 0);
			const deUint32 r2	= getBit(blockModeData, 1);
			const deUint32 i23	= getBits(blockModeData, 2, 3);
			const deUint32 a	= getBits(blockModeData, 5, 6);

			r = (r2 << 2) | (r1 << 1) | (r0 << 0);

			if (i23 == 3)
			{
				const deUint32	b	= getBit(blockModeData, 7);
				const bool		i8	= isBitSet(blockModeData, 8);
				blockMode.weightGridWidth	= i8 ? b+2 : a+2;
				blockMode.weightGridHeight	= i8 ? a+2 : b+6;
			}
			else
			{
				const deUint32 b = getBits(blockModeData, 7, 8);

				switch (i23)
				{
					case 0:		blockMode.weightGridWidth = b + 4;	blockMode.weightGridHeight = a + 2;	break;
					case 1:		blockMode.weightGridWidth = b + 8;	blockMode.weightGridHeight = a + 2;	break;
					case 2:		blockMode.weightGridWidth = a + 2;	blockMode.weightGridHeight = b + 8;	break;
					default: DE_ASSERT(false);
				}
			}
		}

		const bool	zeroDH		= getBits(blockModeData, 0, 1) == 0 && getBits(blockModeData, 7, 8) == 2;
		const bool	h			= zeroDH ? 0 : isBitSet(blockModeData, 9);
		blockMode.isDualPlane	= zeroDH ? 0 : isBitSet(blockModeData, 10);

		{
			ISEMode&	m	= blockMode.weightISEParams.mode;
			int&		b	= blockMode.weightISEParams.numBits;
			m = ISEMODE_PLAIN_BIT;
			b = 0;

			if (h)
			{
				switch (r)
				{
					case 2:							m = ISEMODE_QUINT;	b = 1;	break;
					case 3:		m = ISEMODE_TRIT;						b = 2;	break;
					case 4:												b = 4;	break;
					case 5:							m = ISEMODE_QUINT;	b = 2;	break;
					case 6:		m = ISEMODE_TRIT;						b = 3;	break;
					case 7:												b = 5;	break;
					default:	DE_ASSERT(false);
				}
			}
			else
			{
				switch (r)
				{
					case 2:												b = 1;	break;
					case 3:		m = ISEMODE_TRIT;								break;
					case 4:												b = 2;	break;
					case 5:							m = ISEMODE_QUINT;			break;
					case 6:		m = ISEMODE_TRIT;						b = 1;	break;
					case 7:												b = 3;	break;
					default:	DE_ASSERT(false);
				}
			}
		}
	}

	blockMode.isError = false;
	return blockMode;
}

inline void setASTCErrorColorBlock (void* dst, int blockWidth, int blockHeight, bool isSRGB)
{
	if (isSRGB)
	{
		deUint8* const dstU = (deUint8*)dst;

		for (int i = 0; i < blockWidth*blockHeight; i++)
		{
			dstU[4*i + 0] = 0xff;
			dstU[4*i + 1] = 0;
			dstU[4*i + 2] = 0xff;
			dstU[4*i + 3] = 0xff;
		}
	}
	else
	{
		float* const dstF = (float*)dst;

		for (int i = 0; i < blockWidth*blockHeight; i++)
		{
			dstF[4*i + 0] = 1.0f;
			dstF[4*i + 1] = 0.0f;
			dstF[4*i + 2] = 1.0f;
			dstF[4*i + 3] = 1.0f;
		}
	}
}

DecompressResult decodeVoidExtentBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode)
{
	const deUint32	minSExtent			= blockData.getBits(12, 24);
	const deUint32	maxSExtent			= blockData.getBits(25, 37);
	const deUint32	minTExtent			= blockData.getBits(38, 50);
	const deUint32	maxTExtent			= blockData.getBits(51, 63);
	const bool		allExtentsAllOnes	= minSExtent == 0x1fff && maxSExtent == 0x1fff && minTExtent == 0x1fff && maxTExtent == 0x1fff;
	const bool		isHDRBlock			= blockData.isBitSet(9);

	if ((isLDRMode && isHDRBlock) || (!allExtentsAllOnes && (minSExtent >= maxSExtent || minTExtent >= maxTExtent)))
	{
		setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
		return DECOMPRESS_RESULT_ERROR;
	}

	const deUint32 rgba[4] =
	{
		blockData.getBits(64,  79),
		blockData.getBits(80,  95),
		blockData.getBits(96,  111),
		blockData.getBits(112, 127)
	};

	if (isSRGB)
	{
		deUint8* const dstU = (deUint8*)dst;
		for (int i = 0; i < blockWidth*blockHeight; i++)
		for (int c = 0; c < 4; c++)
			dstU[i*4 + c] = (deUint8)((rgba[c] & 0xff00) >> 8);
	}
	else
	{
		float* const dstF = (float*)dst;

		if (isHDRBlock)
		{
			for (int c = 0; c < 4; c++)
			{
				if (isFloat16InfOrNan((deFloat16)rgba[c]))
					throw InternalError("Infinity or NaN color component in HDR void extent block in ASTC texture (behavior undefined by ASTC specification)");
			}

			for (int i = 0; i < blockWidth*blockHeight; i++)
			for (int c = 0; c < 4; c++)
				dstF[i*4 + c] = deFloat16To32((deFloat16)rgba[c]);
		}
		else
		{
			for (int i = 0; i < blockWidth*blockHeight; i++)
			for (int c = 0; c < 4; c++)
				dstF[i*4 + c] = rgba[c] == 65535 ? 1.0f : (float)rgba[c] / 65536.0f;
		}
	}

	return DECOMPRESS_RESULT_VALID_BLOCK;
}

void decodeColorEndpointModes (deUint32* endpointModesDst, const Block128& blockData, int numPartitions, int extraCemBitsStart)
{
	if (numPartitions == 1)
		endpointModesDst[0] = blockData.getBits(13, 16);
	else
	{
		const deUint32 highLevelSelector = blockData.getBits(23, 24);

		if (highLevelSelector == 0)
		{
			const deUint32 mode = blockData.getBits(25, 28);
			for (int i = 0; i < numPartitions; i++)
				endpointModesDst[i] = mode;
		}
		else
		{
			for (int partNdx = 0; partNdx < numPartitions; partNdx++)
			{
				const deUint32 cemClass		= highLevelSelector - (blockData.isBitSet(25 + partNdx) ? 0 : 1);
				const deUint32 lowBit0Ndx	= numPartitions + 2*partNdx;
				const deUint32 lowBit1Ndx	= numPartitions + 2*partNdx + 1;
				const deUint32 lowBit0		= blockData.getBit(lowBit0Ndx < 4 ? 25+lowBit0Ndx : extraCemBitsStart+lowBit0Ndx-4);
				const deUint32 lowBit1		= blockData.getBit(lowBit1Ndx < 4 ? 25+lowBit1Ndx : extraCemBitsStart+lowBit1Ndx-4);

				endpointModesDst[partNdx] = (cemClass << 2) | (lowBit1 << 1) | lowBit0;
			}
		}
	}
}

int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions)
{
	int result = 0;
	for (int i = 0; i < numPartitions; i++)
		result += computeNumColorEndpointValues(endpointModes[i]);
	return result;
}

void decodeISETritBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
{
	DE_ASSERT(de::inRange(numValues, 1, 5));

	deUint32 m[5];

	m[0]			= data.getNext(numBits);
	deUint32 T01	= data.getNext(2);
	m[1]			= data.getNext(numBits);
	deUint32 T23	= data.getNext(2);
	m[2]			= data.getNext(numBits);
	deUint32 T4		= data.getNext(1);
	m[3]			= data.getNext(numBits);
	deUint32 T56	= data.getNext(2);
	m[4]			= data.getNext(numBits);
	deUint32 T7		= data.getNext(1);

	switch (numValues)
	{
		case 1:
			T23	= 0;
		// Fallthrough
		case 2:
			T4	= 0;
		// Fallthrough
		case 3:
			T56	= 0;
		// Fallthrough
		case 4:
			T7	= 0;
		// Fallthrough
		case 5:
			break;
		default:
			DE_ASSERT(false);
	}

	const deUint32 T = (T7 << 7) | (T56 << 5) | (T4 << 4) | (T23 << 2) | (T01 << 0);

	static const deUint32 tritsFromT[256][5] =
	{
		{ 0,0,0,0,0 }, { 1,0,0,0,0 }, { 2,0,0,0,0 }, { 0,0,2,0,0 }, { 0,1,0,0,0 }, { 1,1,0,0,0 }, { 2,1,0,0,0 }, { 1,0,2,0,0 }, { 0,2,0,0,0 }, { 1,2,0,0,0 }, { 2,2,0,0,0 }, { 2,0,2,0,0 }, { 0,2,2,0,0 }, { 1,2,2,0,0 }, { 2,2,2,0,0 }, { 2,0,2,0,0 },
		{ 0,0,1,0,0 }, { 1,0,1,0,0 }, { 2,0,1,0,0 }, { 0,1,2,0,0 }, { 0,1,1,0,0 }, { 1,1,1,0,0 }, { 2,1,1,0,0 }, { 1,1,2,0,0 }, { 0,2,1,0,0 }, { 1,2,1,0,0 }, { 2,2,1,0,0 }, { 2,1,2,0,0 }, { 0,0,0,2,2 }, { 1,0,0,2,2 }, { 2,0,0,2,2 }, { 0,0,2,2,2 },
		{ 0,0,0,1,0 }, { 1,0,0,1,0 }, { 2,0,0,1,0 }, { 0,0,2,1,0 }, { 0,1,0,1,0 }, { 1,1,0,1,0 }, { 2,1,0,1,0 }, { 1,0,2,1,0 }, { 0,2,0,1,0 }, { 1,2,0,1,0 }, { 2,2,0,1,0 }, { 2,0,2,1,0 }, { 0,2,2,1,0 }, { 1,2,2,1,0 }, { 2,2,2,1,0 }, { 2,0,2,1,0 },
		{ 0,0,1,1,0 }, { 1,0,1,1,0 }, { 2,0,1,1,0 }, { 0,1,2,1,0 }, { 0,1,1,1,0 }, { 1,1,1,1,0 }, { 2,1,1,1,0 }, { 1,1,2,1,0 }, { 0,2,1,1,0 }, { 1,2,1,1,0 }, { 2,2,1,1,0 }, { 2,1,2,1,0 }, { 0,1,0,2,2 }, { 1,1,0,2,2 }, { 2,1,0,2,2 }, { 1,0,2,2,2 },
		{ 0,0,0,2,0 }, { 1,0,0,2,0 }, { 2,0,0,2,0 }, { 0,0,2,2,0 }, { 0,1,0,2,0 }, { 1,1,0,2,0 }, { 2,1,0,2,0 }, { 1,0,2,2,0 }, { 0,2,0,2,0 }, { 1,2,0,2,0 }, { 2,2,0,2,0 }, { 2,0,2,2,0 }, { 0,2,2,2,0 }, { 1,2,2,2,0 }, { 2,2,2,2,0 }, { 2,0,2,2,0 },
		{ 0,0,1,2,0 }, { 1,0,1,2,0 }, { 2,0,1,2,0 }, { 0,1,2,2,0 }, { 0,1,1,2,0 }, { 1,1,1,2,0 }, { 2,1,1,2,0 }, { 1,1,2,2,0 }, { 0,2,1,2,0 }, { 1,2,1,2,0 }, { 2,2,1,2,0 }, { 2,1,2,2,0 }, { 0,2,0,2,2 }, { 1,2,0,2,2 }, { 2,2,0,2,2 }, { 2,0,2,2,2 },
		{ 0,0,0,0,2 }, { 1,0,0,0,2 }, { 2,0,0,0,2 }, { 0,0,2,0,2 }, { 0,1,0,0,2 }, { 1,1,0,0,2 }, { 2,1,0,0,2 }, { 1,0,2,0,2 }, { 0,2,0,0,2 }, { 1,2,0,0,2 }, { 2,2,0,0,2 }, { 2,0,2,0,2 }, { 0,2,2,0,2 }, { 1,2,2,0,2 }, { 2,2,2,0,2 }, { 2,0,2,0,2 },
		{ 0,0,1,0,2 }, { 1,0,1,0,2 }, { 2,0,1,0,2 }, { 0,1,2,0,2 }, { 0,1,1,0,2 }, { 1,1,1,0,2 }, { 2,1,1,0,2 }, { 1,1,2,0,2 }, { 0,2,1,0,2 }, { 1,2,1,0,2 }, { 2,2,1,0,2 }, { 2,1,2,0,2 }, { 0,2,2,2,2 }, { 1,2,2,2,2 }, { 2,2,2,2,2 }, { 2,0,2,2,2 },
		{ 0,0,0,0,1 }, { 1,0,0,0,1 }, { 2,0,0,0,1 }, { 0,0,2,0,1 }, { 0,1,0,0,1 }, { 1,1,0,0,1 }, { 2,1,0,0,1 }, { 1,0,2,0,1 }, { 0,2,0,0,1 }, { 1,2,0,0,1 }, { 2,2,0,0,1 }, { 2,0,2,0,1 }, { 0,2,2,0,1 }, { 1,2,2,0,1 }, { 2,2,2,0,1 }, { 2,0,2,0,1 },
		{ 0,0,1,0,1 }, { 1,0,1,0,1 }, { 2,0,1,0,1 }, { 0,1,2,0,1 }, { 0,1,1,0,1 }, { 1,1,1,0,1 }, { 2,1,1,0,1 }, { 1,1,2,0,1 }, { 0,2,1,0,1 }, { 1,2,1,0,1 }, { 2,2,1,0,1 }, { 2,1,2,0,1 }, { 0,0,1,2,2 }, { 1,0,1,2,2 }, { 2,0,1,2,2 }, { 0,1,2,2,2 },
		{ 0,0,0,1,1 }, { 1,0,0,1,1 }, { 2,0,0,1,1 }, { 0,0,2,1,1 }, { 0,1,0,1,1 }, { 1,1,0,1,1 }, { 2,1,0,1,1 }, { 1,0,2,1,1 }, { 0,2,0,1,1 }, { 1,2,0,1,1 }, { 2,2,0,1,1 }, { 2,0,2,1,1 }, { 0,2,2,1,1 }, { 1,2,2,1,1 }, { 2,2,2,1,1 }, { 2,0,2,1,1 },
		{ 0,0,1,1,1 }, { 1,0,1,1,1 }, { 2,0,1,1,1 }, { 0,1,2,1,1 }, { 0,1,1,1,1 }, { 1,1,1,1,1 }, { 2,1,1,1,1 }, { 1,1,2,1,1 }, { 0,2,1,1,1 }, { 1,2,1,1,1 }, { 2,2,1,1,1 }, { 2,1,2,1,1 }, { 0,1,1,2,2 }, { 1,1,1,2,2 }, { 2,1,1,2,2 }, { 1,1,2,2,2 },
		{ 0,0,0,2,1 }, { 1,0,0,2,1 }, { 2,0,0,2,1 }, { 0,0,2,2,1 }, { 0,1,0,2,1 }, { 1,1,0,2,1 }, { 2,1,0,2,1 }, { 1,0,2,2,1 }, { 0,2,0,2,1 }, { 1,2,0,2,1 }, { 2,2,0,2,1 }, { 2,0,2,2,1 }, { 0,2,2,2,1 }, { 1,2,2,2,1 }, { 2,2,2,2,1 }, { 2,0,2,2,1 },
		{ 0,0,1,2,1 }, { 1,0,1,2,1 }, { 2,0,1,2,1 }, { 0,1,2,2,1 }, { 0,1,1,2,1 }, { 1,1,1,2,1 }, { 2,1,1,2,1 }, { 1,1,2,2,1 }, { 0,2,1,2,1 }, { 1,2,1,2,1 }, { 2,2,1,2,1 }, { 2,1,2,2,1 }, { 0,2,1,2,2 }, { 1,2,1,2,2 }, { 2,2,1,2,2 }, { 2,1,2,2,2 },
		{ 0,0,0,1,2 }, { 1,0,0,1,2 }, { 2,0,0,1,2 }, { 0,0,2,1,2 }, { 0,1,0,1,2 }, { 1,1,0,1,2 }, { 2,1,0,1,2 }, { 1,0,2,1,2 }, { 0,2,0,1,2 }, { 1,2,0,1,2 }, { 2,2,0,1,2 }, { 2,0,2,1,2 }, { 0,2,2,1,2 }, { 1,2,2,1,2 }, { 2,2,2,1,2 }, { 2,0,2,1,2 },
		{ 0,0,1,1,2 }, { 1,0,1,1,2 }, { 2,0,1,1,2 }, { 0,1,2,1,2 }, { 0,1,1,1,2 }, { 1,1,1,1,2 }, { 2,1,1,1,2 }, { 1,1,2,1,2 }, { 0,2,1,1,2 }, { 1,2,1,1,2 }, { 2,2,1,1,2 }, { 2,1,2,1,2 }, { 0,2,2,2,2 }, { 1,2,2,2,2 }, { 2,2,2,2,2 }, { 2,1,2,2,2 }
	};

	const deUint32 (& trits)[5] = tritsFromT[T];

	for (int i = 0; i < numValues; i++)
	{
		dst[i].m	= m[i];
		dst[i].tq	= trits[i];
		dst[i].v	= (trits[i] << numBits) + m[i];
	}
}

void decodeISEQuintBlock (ISEDecodedResult* dst, int numValues, BitAccessStream& data, int numBits)
{
	DE_ASSERT(de::inRange(numValues, 1, 3));

	deUint32 m[3];

	m[0]			= data.getNext(numBits);
	deUint32 Q012	= data.getNext(3);
	m[1]			= data.getNext(numBits);
	deUint32 Q34	= data.getNext(2);
	m[2]			= data.getNext(numBits);
	deUint32 Q56	= data.getNext(2);

	switch (numValues)
	{
		case 1:
			Q34	= 0;
		// Fallthrough
		case 2:
			Q56	= 0;
		// Fallthrough
		case 3:
			break;
		default:
			DE_ASSERT(false);
	}

	const deUint32 Q = (Q56 << 5) | (Q34 << 3) | (Q012 << 0);

	static const deUint32 quintsFromQ[256][3] =
	{
		{ 0,0,0 }, { 1,0,0 }, { 2,0,0 }, { 3,0,0 }, { 4,0,0 }, { 0,4,0 }, { 4,4,0 }, { 4,4,4 }, { 0,1,0 }, { 1,1,0 }, { 2,1,0 }, { 3,1,0 }, { 4,1,0 }, { 1,4,0 }, { 4,4,1 }, { 4,4,4 },
		{ 0,2,0 }, { 1,2,0 }, { 2,2,0 }, { 3,2,0 }, { 4,2,0 }, { 2,4,0 }, { 4,4,2 }, { 4,4,4 }, { 0,3,0 }, { 1,3,0 }, { 2,3,0 }, { 3,3,0 }, { 4,3,0 }, { 3,4,0 }, { 4,4,3 }, { 4,4,4 },
		{ 0,0,1 }, { 1,0,1 }, { 2,0,1 }, { 3,0,1 }, { 4,0,1 }, { 0,4,1 }, { 4,0,4 }, { 0,4,4 }, { 0,1,1 }, { 1,1,1 }, { 2,1,1 }, { 3,1,1 }, { 4,1,1 }, { 1,4,1 }, { 4,1,4 }, { 1,4,4 },
		{ 0,2,1 }, { 1,2,1 }, { 2,2,1 }, { 3,2,1 }, { 4,2,1 }, { 2,4,1 }, { 4,2,4 }, { 2,4,4 }, { 0,3,1 }, { 1,3,1 }, { 2,3,1 }, { 3,3,1 }, { 4,3,1 }, { 3,4,1 }, { 4,3,4 }, { 3,4,4 },
		{ 0,0,2 }, { 1,0,2 }, { 2,0,2 }, { 3,0,2 }, { 4,0,2 }, { 0,4,2 }, { 2,0,4 }, { 3,0,4 }, { 0,1,2 }, { 1,1,2 }, { 2,1,2 }, { 3,1,2 }, { 4,1,2 }, { 1,4,2 }, { 2,1,4 }, { 3,1,4 },
		{ 0,2,2 }, { 1,2,2 }, { 2,2,2 }, { 3,2,2 }, { 4,2,2 }, { 2,4,2 }, { 2,2,4 }, { 3,2,4 }, { 0,3,2 }, { 1,3,2 }, { 2,3,2 }, { 3,3,2 }, { 4,3,2 }, { 3,4,2 }, { 2,3,4 }, { 3,3,4 },
		{ 0,0,3 }, { 1,0,3 }, { 2,0,3 }, { 3,0,3 }, { 4,0,3 }, { 0,4,3 }, { 0,0,4 }, { 1,0,4 }, { 0,1,3 }, { 1,1,3 }, { 2,1,3 }, { 3,1,3 }, { 4,1,3 }, { 1,4,3 }, { 0,1,4 }, { 1,1,4 },
		{ 0,2,3 }, { 1,2,3 }, { 2,2,3 }, { 3,2,3 }, { 4,2,3 }, { 2,4,3 }, { 0,2,4 }, { 1,2,4 }, { 0,3,3 }, { 1,3,3 }, { 2,3,3 }, { 3,3,3 }, { 4,3,3 }, { 3,4,3 }, { 0,3,4 }, { 1,3,4 }
	};

	const deUint32 (& quints)[3] = quintsFromQ[Q];

	for (int i = 0; i < numValues; i++)
	{
		dst[i].m	= m[i];
		dst[i].tq	= quints[i];
		dst[i].v	= (quints[i] << numBits) + m[i];
	}
}

inline void decodeISEBitBlock (ISEDecodedResult* dst, BitAccessStream& data, int numBits)
{
	dst[0].m = data.getNext(numBits);
	dst[0].v = dst[0].m;
}

void decodeISE (ISEDecodedResult* dst, int numValues, BitAccessStream& data, const ISEParams& params)
{
	if (params.mode == ISEMODE_TRIT)
	{
		const int numBlocks = deDivRoundUp32(numValues, 5);
		for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
		{
			const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
			decodeISETritBlock(&dst[5*blockNdx], numValuesInBlock, data, params.numBits);
		}
	}
	else if (params.mode == ISEMODE_QUINT)
	{
		const int numBlocks = deDivRoundUp32(numValues, 3);
		for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
		{
			const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
			decodeISEQuintBlock(&dst[3*blockNdx], numValuesInBlock, data, params.numBits);
		}
	}
	else
	{
		DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
		for (int i = 0; i < numValues; i++)
			decodeISEBitBlock(&dst[i], data, params.numBits);
	}
}

void unquantizeColorEndpoints (deUint32* dst, const ISEDecodedResult* iseResults, int numEndpoints, const ISEParams& iseParams)
{
	if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
	{
		const int rangeCase				= iseParams.numBits*2 - (iseParams.mode == ISEMODE_TRIT ? 2 : 1);
		DE_ASSERT(de::inRange(rangeCase, 0, 10));
		static const deUint32	Ca[11]	= { 204, 113, 93, 54, 44, 26, 22, 13, 11, 6, 5 };
		const deUint32			C		= Ca[rangeCase];

		for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
		{
			const deUint32 a = getBit(iseResults[endpointNdx].m, 0);
			const deUint32 b = getBit(iseResults[endpointNdx].m, 1);
			const deUint32 c = getBit(iseResults[endpointNdx].m, 2);
			const deUint32 d = getBit(iseResults[endpointNdx].m, 3);
			const deUint32 e = getBit(iseResults[endpointNdx].m, 4);
			const deUint32 f = getBit(iseResults[endpointNdx].m, 5);

			const deUint32 A = a == 0 ? 0 : (1<<9)-1;
			const deUint32 B = rangeCase == 0	? 0
							 : rangeCase == 1	? 0
							 : rangeCase == 2	? (b << 8) |									(b << 4) |				(b << 2) |	(b << 1)
							 : rangeCase == 3	? (b << 8) |												(b << 3) |	(b << 2)
							 : rangeCase == 4	? (c << 8) | (b << 7) |										(c << 3) |	(b << 2) |	(c << 1) |	(b << 0)
							 : rangeCase == 5	? (c << 8) | (b << 7) |													(c << 2) |	(b << 1) |	(c << 0)
							 : rangeCase == 6	? (d << 8) | (c << 7) | (b << 6) |										(d << 2) |	(c << 1) |	(b << 0)
							 : rangeCase == 7	? (d << 8) | (c << 7) | (b << 6) |													(d << 1) |	(c << 0)
							 : rangeCase == 8	? (e << 8) | (d << 7) | (c << 6) | (b << 5) |										(e << 1) |	(d << 0)
							 : rangeCase == 9	? (e << 8) | (d << 7) | (c << 6) | (b << 5) |													(e << 0)
							 : rangeCase == 10	? (f << 8) | (e << 7) | (d << 6) | (c << 5) |	(b << 4) |										(f << 0)
							 : (deUint32)-1;
			DE_ASSERT(B != (deUint32)-1);

			dst[endpointNdx] = (((iseResults[endpointNdx].tq*C + B) ^ A) >> 2) | (A & 0x80);
		}
	}
	else
	{
		DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);

		for (int endpointNdx = 0; endpointNdx < numEndpoints; endpointNdx++)
			dst[endpointNdx] = bitReplicationScale(iseResults[endpointNdx].v, iseParams.numBits, 8);
	}
}

inline void bitTransferSigned (deInt32& a, deInt32& b)
{
	b >>= 1;
	b |= a & 0x80;
	a >>= 1;
	a &= 0x3f;
	if (isBitSet(a, 5))
		a -= 0x40;
}

inline UVec4 clampedRGBA (const IVec4& rgba)
{
	return UVec4(de::clamp(rgba.x(), 0, 0xff),
				 de::clamp(rgba.y(), 0, 0xff),
				 de::clamp(rgba.z(), 0, 0xff),
				 de::clamp(rgba.w(), 0, 0xff));
}

inline IVec4 blueContract (int r, int g, int b, int a)
{
	return IVec4((r+b)>>1, (g+b)>>1, b, a);
}

inline bool isColorEndpointModeHDR (deUint32 mode)
{
	return mode == 2	||
		   mode == 3	||
		   mode == 7	||
		   mode == 11	||
		   mode == 14	||
		   mode == 15;
}

void decodeHDREndpointMode7 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3)
{
	const deUint32 m10		= getBit(v1, 7) | (getBit(v2, 7) << 1);
	const deUint32 m23		= getBits(v0, 6, 7);
	const deUint32 majComp	= m10 != 3	? m10
							: m23 != 3	? m23
							:			  0;
	const deUint32 mode		= m10 != 3	? m23
							: m23 != 3	? 4
							:			  5;

	deInt32			red		= (deInt32)getBits(v0, 0, 5);
	deInt32			green	= (deInt32)getBits(v1, 0, 4);
	deInt32			blue	= (deInt32)getBits(v2, 0, 4);
	deInt32			scale	= (deInt32)getBits(v3, 0, 4);

	{
#define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)
#define ASSIGN_X_BITS(V0,S0, V1,S1, V2,S2, V3,S3, V4,S4, V5,S5, V6,S6) do { SHOR(V0,S0,x0); SHOR(V1,S1,x1); SHOR(V2,S2,x2); SHOR(V3,S3,x3); SHOR(V4,S4,x4); SHOR(V5,S5,x5); SHOR(V6,S6,x6); } while (false)

		const deUint32	x0	= getBit(v1, 6);
		const deUint32	x1	= getBit(v1, 5);
		const deUint32	x2	= getBit(v2, 6);
		const deUint32	x3	= getBit(v2, 5);
		const deUint32	x4	= getBit(v3, 7);
		const deUint32	x5	= getBit(v3, 6);
		const deUint32	x6	= getBit(v3, 5);

		deInt32&		R	= red;
		deInt32&		G	= green;
		deInt32&		B	= blue;
		deInt32&		S	= scale;

		switch (mode)
		{
			case 0: ASSIGN_X_BITS(R,9,  R,8,  R,7,  R,10,  R,6,  S,6,   S,5); break;
			case 1: ASSIGN_X_BITS(R,8,  G,5,  R,7,  B,5,   R,6,  R,10,  R,9); break;
			case 2: ASSIGN_X_BITS(R,9,  R,8,  R,7,  R,6,   S,7,  S,6,   S,5); break;
			case 3: ASSIGN_X_BITS(R,8,  G,5,  R,7,  B,5,   R,6,  S,6,   S,5); break;
			case 4: ASSIGN_X_BITS(G,6,  G,5,  B,6,  B,5,   R,6,  R,7,   S,5); break;
			case 5: ASSIGN_X_BITS(G,6,  G,5,  B,6,  B,5,   R,6,  S,6,   S,5); break;
			default:
				DE_ASSERT(false);
		}

#undef ASSIGN_X_BITS
#undef SHOR
	}

	static const int shiftAmounts[] = { 1, 1, 2, 3, 4, 5 };
	DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(shiftAmounts));

	red		<<= shiftAmounts[mode];
	green	<<= shiftAmounts[mode];
	blue	<<= shiftAmounts[mode];
	scale	<<= shiftAmounts[mode];

	if (mode != 5)
	{
		green	= red - green;
		blue	= red - blue;
	}

	if (majComp == 1)
		std::swap(red, green);
	else if (majComp == 2)
		std::swap(red, blue);

	e0 = UVec4(de::clamp(red	- scale,	0, 0xfff),
			   de::clamp(green	- scale,	0, 0xfff),
			   de::clamp(blue	- scale,	0, 0xfff),
			   0x780);

	e1 = UVec4(de::clamp(red,				0, 0xfff),
			   de::clamp(green,				0, 0xfff),
			   de::clamp(blue,				0, 0xfff),
			   0x780);
}

void decodeHDREndpointMode11 (UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5)
{
	const deUint32 major = (getBit(v5, 7) << 1) | getBit(v4, 7);

	if (major == 3)
	{
		e0 = UVec4(v0<<4, v2<<4, getBits(v4,0,6)<<5, 0x780);
		e1 = UVec4(v1<<4, v3<<4, getBits(v5,0,6)<<5, 0x780);
	}
	else
	{
		const deUint32 mode = (getBit(v3, 7) << 2) | (getBit(v2, 7) << 1) | getBit(v1, 7);

		deInt32 a	= (deInt32)((getBit(v1, 6) << 8) | v0);
		deInt32 c	= (deInt32)(getBits(v1, 0, 5));
		deInt32 b0	= (deInt32)(getBits(v2, 0, 5));
		deInt32 b1	= (deInt32)(getBits(v3, 0, 5));
		deInt32 d0	= (deInt32)(getBits(v4, 0, 4));
		deInt32 d1	= (deInt32)(getBits(v5, 0, 4));

		{
#define SHOR(DST_VAR, SHIFT, BIT_VAR) (DST_VAR) |= (BIT_VAR) << (SHIFT)
#define ASSIGN_X_BITS(V0,S0, V1,S1, V2,S2, V3,S3, V4,S4, V5,S5) do { SHOR(V0,S0,x0); SHOR(V1,S1,x1); SHOR(V2,S2,x2); SHOR(V3,S3,x3); SHOR(V4,S4,x4); SHOR(V5,S5,x5); } while (false)

			const deUint32 x0 = getBit(v2, 6);
			const deUint32 x1 = getBit(v3, 6);
			const deUint32 x2 = getBit(v4, 6);
			const deUint32 x3 = getBit(v5, 6);
			const deUint32 x4 = getBit(v4, 5);
			const deUint32 x5 = getBit(v5, 5);

			switch (mode)
			{
				case 0: ASSIGN_X_BITS(b0,6,  b1,6,   d0,6,  d1,6,  d0,5,  d1,5); break;
				case 1: ASSIGN_X_BITS(b0,6,  b1,6,   b0,7,  b1,7,  d0,5,  d1,5); break;
				case 2: ASSIGN_X_BITS(a,9,   c,6,    d0,6,  d1,6,  d0,5,  d1,5); break;
				case 3: ASSIGN_X_BITS(b0,6,  b1,6,   a,9,   c,6,   d0,5,  d1,5); break;
				case 4: ASSIGN_X_BITS(b0,6,  b1,6,   b0,7,  b1,7,  a,9,   a,10); break;
				case 5: ASSIGN_X_BITS(a,9,   a,10,   c,7,   c,6,   d0,5,  d1,5); break;
				case 6: ASSIGN_X_BITS(b0,6,  b1,6,   a,11,  c,6,   a,9,   a,10); break;
				case 7: ASSIGN_X_BITS(a,9,   a,10,   a,11,  c,6,   d0,5,  d1,5); break;
				default:
					DE_ASSERT(false);
			}

#undef ASSIGN_X_BITS
#undef SHOR
		}

		static const int numDBits[] = { 7, 6, 7, 6, 5, 6, 5, 6 };
		DE_ASSERT(mode < DE_LENGTH_OF_ARRAY(numDBits));

		d0 = signExtend(d0, numDBits[mode]);
		d1 = signExtend(d1, numDBits[mode]);

		const int shiftAmount = (mode >> 1) ^ 3;
		a	<<= shiftAmount;
		c	<<= shiftAmount;
		b0	<<= shiftAmount;
		b1	<<= shiftAmount;
		d0	<<= shiftAmount;
		d1	<<= shiftAmount;

		e0 = UVec4(de::clamp(a-c,			0, 0xfff),
				   de::clamp(a-b0-c-d0,		0, 0xfff),
				   de::clamp(a-b1-c-d1,		0, 0xfff),
				   0x780);

		e1 = UVec4(de::clamp(a,				0, 0xfff),
				   de::clamp(a-b0,			0, 0xfff),
				   de::clamp(a-b1,			0, 0xfff),
				   0x780);

		if (major == 1)
		{
			std::swap(e0.x(), e0.y());
			std::swap(e1.x(), e1.y());
		}
		else if (major == 2)
		{
			std::swap(e0.x(), e0.z());
			std::swap(e1.x(), e1.z());
		}
	}
}

void decodeHDREndpointMode15(UVec4& e0, UVec4& e1, deUint32 v0, deUint32 v1, deUint32 v2, deUint32 v3, deUint32 v4, deUint32 v5, deUint32 v6In, deUint32 v7In)
{
	decodeHDREndpointMode11(e0, e1, v0, v1, v2, v3, v4, v5);

	const deUint32	mode	= (getBit(v7In, 7) << 1) | getBit(v6In, 7);
	deInt32			v6		= (deInt32)getBits(v6In, 0, 6);
	deInt32			v7		= (deInt32)getBits(v7In, 0, 6);

	if (mode == 3)
	{
		e0.w() = v6 << 5;
		e1.w() = v7 << 5;
	}
	else
	{
		v6 |= (v7 << (mode+1)) & 0x780;
		v7 &= (0x3f >> mode);
		v7 ^= 0x20 >> mode;
		v7 -= 0x20 >> mode;
		v6 <<= 4-mode;
		v7 <<= 4-mode;

		v7 += v6;
		v7 = de::clamp(v7, 0, 0xfff);
		e0.w() = v6;
		e1.w() = v7;
	}
}

void decodeColorEndpoints (ColorEndpointPair* dst, const deUint32* unquantizedEndpoints, const deUint32* endpointModes, int numPartitions)
{
	int unquantizedNdx = 0;

	for (int partitionNdx = 0; partitionNdx < numPartitions; partitionNdx++)
	{
		const deUint32		endpointMode	= endpointModes[partitionNdx];
		const deUint32*		v				= &unquantizedEndpoints[unquantizedNdx];
		UVec4&				e0				= dst[partitionNdx].e0;
		UVec4&				e1				= dst[partitionNdx].e1;

		unquantizedNdx += computeNumColorEndpointValues(endpointMode);

		switch (endpointMode)
		{
			case 0:
				e0 = UVec4(v[0], v[0], v[0], 0xff);
				e1 = UVec4(v[1], v[1], v[1], 0xff);
				break;

			case 1:
			{
				const deUint32 L0 = (v[0] >> 2) | (getBits(v[1], 6, 7) << 6);
				const deUint32 L1 = de::min(0xffu, L0 + getBits(v[1], 0, 5));
				e0 = UVec4(L0, L0, L0, 0xff);
				e1 = UVec4(L1, L1, L1, 0xff);
				break;
			}

			case 2:
			{
				const deUint32 v1Gr		= v[1] >= v[0];
				const deUint32 y0		= v1Gr ? v[0]<<4 : (v[1]<<4) + 8;
				const deUint32 y1		= v1Gr ? v[1]<<4 : (v[0]<<4) - 8;

				e0 = UVec4(y0, y0, y0, 0x780);
				e1 = UVec4(y1, y1, y1, 0x780);
				break;
			}

			case 3:
			{
				const bool		m	= isBitSet(v[0], 7);
				const deUint32	y0	= m ? (getBits(v[1], 5, 7) << 9) | (getBits(v[0], 0, 6) << 2)
										: (getBits(v[1], 4, 7) << 8) | (getBits(v[0], 0, 6) << 1);
				const deUint32	d	= m ? getBits(v[1], 0, 4) << 2
										: getBits(v[1], 0, 3) << 1;
				const deUint32	y1	= de::min(0xfffu, y0+d);

				e0 = UVec4(y0, y0, y0, 0x780);
				e1 = UVec4(y1, y1, y1, 0x780);
				break;
			}

			case 4:
				e0 = UVec4(v[0], v[0], v[0], v[2]);
				e1 = UVec4(v[1], v[1], v[1], v[3]);
				break;

			case 5:
			{
				deInt32 v0 = (deInt32)v[0];
				deInt32 v1 = (deInt32)v[1];
				deInt32 v2 = (deInt32)v[2];
				deInt32 v3 = (deInt32)v[3];
				bitTransferSigned(v1, v0);
				bitTransferSigned(v3, v2);

				e0 = clampedRGBA(IVec4(v0,		v0,		v0,		v2));
				e1 = clampedRGBA(IVec4(v0+v1,	v0+v1,	v0+v1,	v2+v3));
				break;
			}

			case 6:
				e0 = UVec4((v[0]*v[3]) >> 8,	(v[1]*v[3]) >> 8,	(v[2]*v[3]) >> 8,	0xff);
				e1 = UVec4(v[0],				v[1],				v[2],				0xff);
				break;

			case 7:
				decodeHDREndpointMode7(e0, e1, v[0], v[1], v[2], v[3]);
				break;

			case 8:
				if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
				{
					e0 = UVec4(v[0], v[2], v[4], 0xff);
					e1 = UVec4(v[1], v[3], v[5], 0xff);
				}
				else
				{
					e0 = blueContract(v[1], v[3], v[5], 0xff).asUint();
					e1 = blueContract(v[0], v[2], v[4], 0xff).asUint();
				}
				break;

			case 9:
			{
				deInt32 v0 = (deInt32)v[0];
				deInt32 v1 = (deInt32)v[1];
				deInt32 v2 = (deInt32)v[2];
				deInt32 v3 = (deInt32)v[3];
				deInt32 v4 = (deInt32)v[4];
				deInt32 v5 = (deInt32)v[5];
				bitTransferSigned(v1, v0);
				bitTransferSigned(v3, v2);
				bitTransferSigned(v5, v4);

				if (v1+v3+v5 >= 0)
				{
					e0 = clampedRGBA(IVec4(v0,		v2,		v4,		0xff));
					e1 = clampedRGBA(IVec4(v0+v1,	v2+v3,	v4+v5,	0xff));
				}
				else
				{
					e0 = clampedRGBA(blueContract(v0+v1,	v2+v3,	v4+v5,	0xff));
					e1 = clampedRGBA(blueContract(v0,		v2,		v4,		0xff));
				}
				break;
			}

			case 10:
				e0 = UVec4((v[0]*v[3]) >> 8,	(v[1]*v[3]) >> 8,	(v[2]*v[3]) >> 8,	v[4]);
				e1 = UVec4(v[0],				v[1],				v[2],				v[5]);
				break;

			case 11:
				decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
				break;

			case 12:
				if (v[1]+v[3]+v[5] >= v[0]+v[2]+v[4])
				{
					e0 = UVec4(v[0], v[2], v[4], v[6]);
					e1 = UVec4(v[1], v[3], v[5], v[7]);
				}
				else
				{
					e0 = clampedRGBA(blueContract(v[1], v[3], v[5], v[7]));
					e1 = clampedRGBA(blueContract(v[0], v[2], v[4], v[6]));
				}
				break;

			case 13:
			{
				deInt32 v0 = (deInt32)v[0];
				deInt32 v1 = (deInt32)v[1];
				deInt32 v2 = (deInt32)v[2];
				deInt32 v3 = (deInt32)v[3];
				deInt32 v4 = (deInt32)v[4];
				deInt32 v5 = (deInt32)v[5];
				deInt32 v6 = (deInt32)v[6];
				deInt32 v7 = (deInt32)v[7];
				bitTransferSigned(v1, v0);
				bitTransferSigned(v3, v2);
				bitTransferSigned(v5, v4);
				bitTransferSigned(v7, v6);

				if (v1+v3+v5 >= 0)
				{
					e0 = clampedRGBA(IVec4(v0,		v2,		v4,		v6));
					e1 = clampedRGBA(IVec4(v0+v1,	v2+v3,	v4+v5,	v6+v7));
				}
				else
				{
					e0 = clampedRGBA(blueContract(v0+v1,	v2+v3,	v4+v5,	v6+v7));
					e1 = clampedRGBA(blueContract(v0,		v2,		v4,		v6));
				}

				break;
			}

			case 14:
				decodeHDREndpointMode11(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5]);
				e0.w() = v[6];
				e1.w() = v[7];
				break;

			case 15:
				decodeHDREndpointMode15(e0, e1, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
				break;

			default:
				DE_ASSERT(false);
		}
	}
}

void computeColorEndpoints (ColorEndpointPair* dst, const Block128& blockData, const deUint32* endpointModes, int numPartitions, int numColorEndpointValues, const ISEParams& iseParams, int numBitsAvailable)
{
	const int			colorEndpointDataStart = numPartitions == 1 ? 17 : 29;
	ISEDecodedResult	colorEndpointData[18];

	{
		BitAccessStream dataStream(blockData, colorEndpointDataStart, numBitsAvailable, true);
		decodeISE(&colorEndpointData[0], numColorEndpointValues, dataStream, iseParams);
	}

	{
		deUint32 unquantizedEndpoints[18];
		unquantizeColorEndpoints(&unquantizedEndpoints[0], &colorEndpointData[0], numColorEndpointValues, iseParams);
		decodeColorEndpoints(dst, &unquantizedEndpoints[0], &endpointModes[0], numPartitions);
	}
}

void unquantizeWeights (deUint32 dst[64], const ISEDecodedResult* weightGrid, const ASTCBlockMode& blockMode)
{
	const int			numWeights	= computeNumWeights(blockMode);
	const ISEParams&	iseParams	= blockMode.weightISEParams;

	if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
	{
		const int rangeCase = iseParams.numBits*2 + (iseParams.mode == ISEMODE_QUINT ? 1 : 0);

		if (rangeCase == 0 || rangeCase == 1)
		{
			static const deUint32 map0[3]	= { 0, 32, 63 };
			static const deUint32 map1[5]	= { 0, 16, 32, 47, 63 };
			const deUint32* const map		= rangeCase == 0 ? &map0[0] : &map1[0];
			for (int i = 0; i < numWeights; i++)
			{
				DE_ASSERT(weightGrid[i].v < (rangeCase == 0 ? 3u : 5u));
				dst[i] = map[weightGrid[i].v];
			}
		}
		else
		{
			DE_ASSERT(rangeCase <= 6);
			static const deUint32	Ca[5]	= { 50, 28, 23, 13, 11 };
			const deUint32			C		= Ca[rangeCase-2];

			for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
			{
				const deUint32 a = getBit(weightGrid[weightNdx].m, 0);
				const deUint32 b = getBit(weightGrid[weightNdx].m, 1);
				const deUint32 c = getBit(weightGrid[weightNdx].m, 2);

				const deUint32 A = a == 0 ? 0 : (1<<7)-1;
				const deUint32 B = rangeCase == 2 ? 0
								 : rangeCase == 3 ? 0
								 : rangeCase == 4 ? (b << 6) |					(b << 2) |				(b << 0)
								 : rangeCase == 5 ? (b << 6) |								(b << 1)
								 : rangeCase == 6 ? (c << 6) | (b << 5) |					(c << 1) |	(b << 0)
								 : (deUint32)-1;

				dst[weightNdx] = (((weightGrid[weightNdx].tq*C + B) ^ A) >> 2) | (A & 0x20);
			}
		}
	}
	else
	{
		DE_ASSERT(iseParams.mode == ISEMODE_PLAIN_BIT);

		for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
			dst[weightNdx] = bitReplicationScale(weightGrid[weightNdx].v, iseParams.numBits, 6);
	}

	for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
		dst[weightNdx] += dst[weightNdx] > 32 ? 1 : 0;

	// Initialize nonexistent weights to poison values
	for (int weightNdx = numWeights; weightNdx < 64; weightNdx++)
		dst[weightNdx] = ~0u;

}

void interpolateWeights (TexelWeightPair* dst, const deUint32 (&unquantizedWeights) [64], int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
{
	const int		numWeightsPerTexel	= blockMode.isDualPlane ? 2 : 1;
	const deUint32	scaleX				= (1024 + blockWidth/2) / (blockWidth-1);
	const deUint32	scaleY				= (1024 + blockHeight/2) / (blockHeight-1);

	DE_ASSERT(blockMode.weightGridWidth*blockMode.weightGridHeight*numWeightsPerTexel <= DE_LENGTH_OF_ARRAY(unquantizedWeights));

	for (int texelY = 0; texelY < blockHeight; texelY++)
	{
		for (int texelX = 0; texelX < blockWidth; texelX++)
		{
			const deUint32 gX	= (scaleX*texelX*(blockMode.weightGridWidth-1) + 32) >> 6;
			const deUint32 gY	= (scaleY*texelY*(blockMode.weightGridHeight-1) + 32) >> 6;
			const deUint32 jX	= gX >> 4;
			const deUint32 jY	= gY >> 4;
			const deUint32 fX	= gX & 0xf;
			const deUint32 fY	= gY & 0xf;

			const deUint32 w11	= (fX*fY + 8) >> 4;
			const deUint32 w10	= fY - w11;
			const deUint32 w01	= fX - w11;
			const deUint32 w00	= 16 - fX - fY + w11;

			const deUint32 i00	= jY*blockMode.weightGridWidth + jX;
			const deUint32 i01	= i00 + 1;
			const deUint32 i10	= i00 + blockMode.weightGridWidth;
			const deUint32 i11	= i00 + blockMode.weightGridWidth + 1;

			// These addresses can be out of bounds, but respective weights will be 0 then.
			DE_ASSERT(deInBounds32(i00, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w00 == 0);
			DE_ASSERT(deInBounds32(i01, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w01 == 0);
			DE_ASSERT(deInBounds32(i10, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w10 == 0);
			DE_ASSERT(deInBounds32(i11, 0, blockMode.weightGridWidth*blockMode.weightGridHeight) || w11 == 0);

			for (int texelWeightNdx = 0; texelWeightNdx < numWeightsPerTexel; texelWeightNdx++)
			{
				// & 0x3f clamps address to bounds of unquantizedWeights
				const deUint32 p00	= unquantizedWeights[(i00 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
				const deUint32 p01	= unquantizedWeights[(i01 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
				const deUint32 p10	= unquantizedWeights[(i10 * numWeightsPerTexel + texelWeightNdx) & 0x3f];
				const deUint32 p11	= unquantizedWeights[(i11 * numWeightsPerTexel + texelWeightNdx) & 0x3f];

				dst[texelY*blockWidth + texelX].w[texelWeightNdx] = (p00*w00 + p01*w01 + p10*w10 + p11*w11 + 8) >> 4;
			}
		}
	}
}

void computeTexelWeights (TexelWeightPair* dst, const Block128& blockData, int blockWidth, int blockHeight, const ASTCBlockMode& blockMode)
{
	ISEDecodedResult weightGrid[64];

	{
		BitAccessStream dataStream(blockData, 127, computeNumRequiredBits(blockMode.weightISEParams, computeNumWeights(blockMode)), false);
		decodeISE(&weightGrid[0], computeNumWeights(blockMode), dataStream, blockMode.weightISEParams);
	}

	{
		deUint32 unquantizedWeights[64];
		unquantizeWeights(&unquantizedWeights[0], &weightGrid[0], blockMode);
		interpolateWeights(dst, unquantizedWeights, blockWidth, blockHeight, blockMode);
	}
}

inline deUint32 hash52 (deUint32 v)
{
	deUint32 p = v;
	p ^= p >> 15;	p -= p << 17;	p += p << 7;	p += p << 4;
	p ^= p >>  5;	p += p << 16;	p ^= p >> 7;	p ^= p >> 3;
	p ^= p <<  6;	p ^= p >> 17;
	return p;
}

int computeTexelPartition (deUint32 seedIn, deUint32 xIn, deUint32 yIn, deUint32 zIn, int numPartitions, bool smallBlock)
{
	DE_ASSERT(zIn == 0);
	const deUint32	x		= smallBlock ? xIn << 1 : xIn;
	const deUint32	y		= smallBlock ? yIn << 1 : yIn;
	const deUint32	z		= smallBlock ? zIn << 1 : zIn;
	const deUint32	seed	= seedIn + 1024*(numPartitions-1);
	const deUint32	rnum	= hash52(seed);
	deUint8			seed1	= (deUint8)( rnum							& 0xf);
	deUint8			seed2	= (deUint8)((rnum >>  4)					& 0xf);
	deUint8			seed3	= (deUint8)((rnum >>  8)					& 0xf);
	deUint8			seed4	= (deUint8)((rnum >> 12)					& 0xf);
	deUint8			seed5	= (deUint8)((rnum >> 16)					& 0xf);
	deUint8			seed6	= (deUint8)((rnum >> 20)					& 0xf);
	deUint8			seed7	= (deUint8)((rnum >> 24)					& 0xf);
	deUint8			seed8	= (deUint8)((rnum >> 28)					& 0xf);
	deUint8			seed9	= (deUint8)((rnum >> 18)					& 0xf);
	deUint8			seed10	= (deUint8)((rnum >> 22)					& 0xf);
	deUint8			seed11	= (deUint8)((rnum >> 26)					& 0xf);
	deUint8			seed12	= (deUint8)(((rnum >> 30) | (rnum << 2))	& 0xf);

	seed1  = (deUint8)(seed1  * seed1 );
	seed2  = (deUint8)(seed2  * seed2 );
	seed3  = (deUint8)(seed3  * seed3 );
	seed4  = (deUint8)(seed4  * seed4 );
	seed5  = (deUint8)(seed5  * seed5 );
	seed6  = (deUint8)(seed6  * seed6 );
	seed7  = (deUint8)(seed7  * seed7 );
	seed8  = (deUint8)(seed8  * seed8 );
	seed9  = (deUint8)(seed9  * seed9 );
	seed10 = (deUint8)(seed10 * seed10);
	seed11 = (deUint8)(seed11 * seed11);
	seed12 = (deUint8)(seed12 * seed12);

	const int shA = (seed & 2) != 0		? 4		: 5;
	const int shB = numPartitions == 3	? 6		: 5;
	const int sh1 = (seed & 1) != 0		? shA	: shB;
	const int sh2 = (seed & 1) != 0		? shB	: shA;
	const int sh3 = (seed & 0x10) != 0	? sh1	: sh2;

	seed1  = (deUint8)(seed1  >> sh1);
	seed2  = (deUint8)(seed2  >> sh2);
	seed3  = (deUint8)(seed3  >> sh1);
	seed4  = (deUint8)(seed4  >> sh2);
	seed5  = (deUint8)(seed5  >> sh1);
	seed6  = (deUint8)(seed6  >> sh2);
	seed7  = (deUint8)(seed7  >> sh1);
	seed8  = (deUint8)(seed8  >> sh2);
	seed9  = (deUint8)(seed9  >> sh3);
	seed10 = (deUint8)(seed10 >> sh3);
	seed11 = (deUint8)(seed11 >> sh3);
	seed12 = (deUint8)(seed12 >> sh3);

	const int a =						0x3f & (seed1*x + seed2*y + seed11*z + (rnum >> 14));
	const int b =						0x3f & (seed3*x + seed4*y + seed12*z + (rnum >> 10));
	const int c = numPartitions >= 3 ?	0x3f & (seed5*x + seed6*y + seed9*z  + (rnum >>  6))	: 0;
	const int d = numPartitions >= 4 ?	0x3f & (seed7*x + seed8*y + seed10*z + (rnum >>  2))	: 0;

	return a >= b && a >= c && a >= d	? 0
		 : b >= c && b >= d				? 1
		 : c >= d						? 2
		 :								  3;
}

DecompressResult setTexelColors (void* dst, ColorEndpointPair* colorEndpoints, TexelWeightPair* texelWeights, int ccs, deUint32 partitionIndexSeed,
								 int numPartitions, int blockWidth, int blockHeight, bool isSRGB, bool isLDRMode, const deUint32* colorEndpointModes)
{
	const bool			smallBlock	= blockWidth*blockHeight < 31;
	DecompressResult	result		= DECOMPRESS_RESULT_VALID_BLOCK;
	bool				isHDREndpoint[4];

	for (int i = 0; i < numPartitions; i++)
		isHDREndpoint[i] = isColorEndpointModeHDR(colorEndpointModes[i]);

	for (int texelY = 0; texelY < blockHeight; texelY++)
	for (int texelX = 0; texelX < blockWidth; texelX++)
	{
		const int				texelNdx			= texelY*blockWidth + texelX;
		const int				colorEndpointNdx	= numPartitions == 1 ? 0 : computeTexelPartition(partitionIndexSeed, texelX, texelY, 0, numPartitions, smallBlock);
		DE_ASSERT(colorEndpointNdx < numPartitions);
		const UVec4&			e0					= colorEndpoints[colorEndpointNdx].e0;
		const UVec4&			e1					= colorEndpoints[colorEndpointNdx].e1;
		const TexelWeightPair&	weight				= texelWeights[texelNdx];

		if (isLDRMode && isHDREndpoint[colorEndpointNdx])
		{
			if (isSRGB)
			{
				((deUint8*)dst)[texelNdx*4 + 0] = 0xff;
				((deUint8*)dst)[texelNdx*4 + 1] = 0;
				((deUint8*)dst)[texelNdx*4 + 2] = 0xff;
				((deUint8*)dst)[texelNdx*4 + 3] = 0xff;
			}
			else
			{
				((float*)dst)[texelNdx*4 + 0] = 1.0f;
				((float*)dst)[texelNdx*4 + 1] = 0;
				((float*)dst)[texelNdx*4 + 2] = 1.0f;
				((float*)dst)[texelNdx*4 + 3] = 1.0f;
			}

			result = DECOMPRESS_RESULT_ERROR;
		}
		else
		{
			for (int channelNdx = 0; channelNdx < 4; channelNdx++)
			{
				if (!isHDREndpoint[colorEndpointNdx] || (channelNdx == 3 && colorEndpointModes[colorEndpointNdx] == 14)) // \note Alpha for mode 14 is treated the same as LDR.
				{
					const deUint32 c0	= (e0[channelNdx] << 8) | (isSRGB ? 0x80 : e0[channelNdx]);
					const deUint32 c1	= (e1[channelNdx] << 8) | (isSRGB ? 0x80 : e1[channelNdx]);
					const deUint32 w	= weight.w[ccs == channelNdx ? 1 : 0];
					const deUint32 c	= (c0*(64-w) + c1*w + 32) / 64;

					if (isSRGB)
						((deUint8*)dst)[texelNdx*4 + channelNdx] = (deUint8)((c & 0xff00) >> 8);
					else
						((float*)dst)[texelNdx*4 + channelNdx] = c == 65535 ? 1.0f : (float)c / 65536.0f;
				}
				else
				{
					DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
					const deUint32		c0	= e0[channelNdx] << 4;
					const deUint32		c1	= e1[channelNdx] << 4;
					const deUint32		w	= weight.w[ccs == channelNdx ? 1 : 0];
					const deUint32		c	= (c0*(64-w) + c1*w + 32) / 64;
					const deUint32		e	= getBits(c, 11, 15);
					const deUint32		m	= getBits(c, 0, 10);
					const deUint32		mt	= m < 512		? 3*m
											: m >= 1536		? 5*m - 2048
											:				  4*m - 512;
					const deFloat16		cf	= (deFloat16)((e << 10) + (mt >> 3));

					((float*)dst)[texelNdx*4 + channelNdx] = deFloat16To32(isFloat16InfOrNan(cf) ? 0x7bff : cf);
				}
			}
		}
	}

	return result;
}

DecompressResult decompressBlock (void* dst, const Block128& blockData, int blockWidth, int blockHeight, bool isSRGB, bool isLDR)
{
	DE_ASSERT(isLDR || !isSRGB);

	// Decode block mode.

	const ASTCBlockMode blockMode = getASTCBlockMode(blockData.getBits(0, 10));

	// Check for block mode errors.

	if (blockMode.isError)
	{
		setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
		return DECOMPRESS_RESULT_ERROR;
	}

	// Separate path for void-extent.

	if (blockMode.isVoidExtent)
		return decodeVoidExtentBlock(dst, blockData, blockWidth, blockHeight, isSRGB, isLDR);

	// Compute weight grid values.

	const int numWeights			= computeNumWeights(blockMode);
	const int numWeightDataBits		= computeNumRequiredBits(blockMode.weightISEParams, numWeights);
	const int numPartitions			= (int)blockData.getBits(11, 12) + 1;

	// Check for errors in weight grid, partition and dual-plane parameters.

	if (numWeights > 64								||
		numWeightDataBits > 96						||
		numWeightDataBits < 24						||
		blockMode.weightGridWidth > blockWidth		||
		blockMode.weightGridHeight > blockHeight	||
		(numPartitions == 4 && blockMode.isDualPlane))
	{
		setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
		return DECOMPRESS_RESULT_ERROR;
	}

	// Compute number of bits available for color endpoint data.

	const bool	isSingleUniqueCem			= numPartitions == 1 || blockData.getBits(23, 24) == 0;
	const int	numConfigDataBits			= (numPartitions == 1 ? 17 : isSingleUniqueCem ? 29 : 25 + 3*numPartitions) +
											  (blockMode.isDualPlane ? 2 : 0);
	const int	numBitsForColorEndpoints	= 128 - numWeightDataBits - numConfigDataBits;
	const int	extraCemBitsStart			= 127 - numWeightDataBits - (isSingleUniqueCem		? -1
																		: numPartitions == 4	? 7
																		: numPartitions == 3	? 4
																		: numPartitions == 2	? 1
																		: 0);
	// Decode color endpoint modes.

	deUint32 colorEndpointModes[4];
	decodeColorEndpointModes(&colorEndpointModes[0], blockData, numPartitions, extraCemBitsStart);

	const int numColorEndpointValues = computeNumColorEndpointValues(colorEndpointModes, numPartitions);

	// Check for errors in color endpoint value count.

	if (numColorEndpointValues > 18 || numBitsForColorEndpoints < deDivRoundUp32(13*numColorEndpointValues, 5))
	{
		setASTCErrorColorBlock(dst, blockWidth, blockHeight, isSRGB);
		return DECOMPRESS_RESULT_ERROR;
	}

	// Compute color endpoints.

	ColorEndpointPair colorEndpoints[4];
	computeColorEndpoints(&colorEndpoints[0], blockData, &colorEndpointModes[0], numPartitions, numColorEndpointValues,
						  computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues), numBitsForColorEndpoints);

	// Compute texel weights.

	TexelWeightPair texelWeights[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT];
	computeTexelWeights(&texelWeights[0], blockData, blockWidth, blockHeight, blockMode);

	// Set texel colors.

	const int		ccs						= blockMode.isDualPlane ? (int)blockData.getBits(extraCemBitsStart-2, extraCemBitsStart-1) : -1;
	const deUint32	partitionIndexSeed		= numPartitions > 1 ? blockData.getBits(13, 22) : (deUint32)-1;

	return setTexelColors(dst, &colorEndpoints[0], &texelWeights[0], ccs, partitionIndexSeed, numPartitions, blockWidth, blockHeight, isSRGB, isLDR, &colorEndpointModes[0]);
}

void decompress (const PixelBufferAccess& dst, const deUint8* data, bool isSRGB, bool isLDR)
{
	DE_ASSERT(isLDR || !isSRGB);

	const int blockWidth = dst.getWidth();
	const int blockHeight = dst.getHeight();

	union
	{
		deUint8		sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
		float		linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
	} decompressedBuffer;

	const Block128 blockData(data);
	decompressBlock(isSRGB ? (void*)&decompressedBuffer.sRGB[0] : (void*)&decompressedBuffer.linear[0],
					blockData, dst.getWidth(), dst.getHeight(), isSRGB, isLDR);

	if (isSRGB)
	{
		for (int i = 0; i < blockHeight; i++)
		for (int j = 0; j < blockWidth; j++)
		{
			dst.setPixel(IVec4(decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 0],
							   decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 1],
							   decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 2],
							   decompressedBuffer.sRGB[(i*blockWidth + j) * 4 + 3]), j, i);
		}
	}
	else
	{
		for (int i = 0; i < blockHeight; i++)
		for (int j = 0; j < blockWidth; j++)
		{
			dst.setPixel(Vec4(decompressedBuffer.linear[(i*blockWidth + j) * 4 + 0],
							  decompressedBuffer.linear[(i*blockWidth + j) * 4 + 1],
							  decompressedBuffer.linear[(i*blockWidth + j) * 4 + 2],
							  decompressedBuffer.linear[(i*blockWidth + j) * 4 + 3]), j, i);
		}
	}
}

// Helper class for setting bits in a 128-bit block.
class AssignBlock128
{
private:
	typedef deUint64 Word;

	enum
	{
		WORD_BYTES	= sizeof(Word),
		WORD_BITS	= 8*WORD_BYTES,
		NUM_WORDS	= 128 / WORD_BITS
	};

	DE_STATIC_ASSERT(128 % WORD_BITS == 0);

public:
	AssignBlock128 (void)
	{
		for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
			m_words[wordNdx] = 0;
	}

	void setBit (int ndx, deUint32 val)
	{
		DE_ASSERT(de::inBounds(ndx, 0, 128));
		DE_ASSERT((val & 1) == val);
		const int wordNdx	= ndx / WORD_BITS;
		const int bitNdx	= ndx % WORD_BITS;
		m_words[wordNdx] = (m_words[wordNdx] & ~((Word)1 << bitNdx)) | ((Word)val << bitNdx);
	}

	void setBits (int low, int high, deUint32 bits)
	{
		DE_ASSERT(de::inBounds(low, 0, 128));
		DE_ASSERT(de::inBounds(high, 0, 128));
		DE_ASSERT(de::inRange(high-low+1, 0, 32));
		DE_ASSERT((bits & (((Word)1 << (high-low+1)) - 1)) == bits);

		if (high-low+1 == 0)
			return;

		const int word0Ndx		= low / WORD_BITS;
		const int word1Ndx		= high / WORD_BITS;
		const int lowNdxInW0	= low % WORD_BITS;

		if (word0Ndx == word1Ndx)
			m_words[word0Ndx] = (m_words[word0Ndx] & ~((((Word)1 << (high-low+1)) - 1) << lowNdxInW0)) | ((Word)bits << lowNdxInW0);
		else
		{
			DE_ASSERT(word1Ndx == word0Ndx + 1);

			const int	highNdxInW1			= high % WORD_BITS;
			const int	numBitsToSetInW0	= WORD_BITS - lowNdxInW0;
			const Word	bitsLowMask			= ((Word)1 << numBitsToSetInW0) - 1;

			m_words[word0Ndx] = (m_words[word0Ndx] & (((Word)1 << lowNdxInW0) - 1))			| (((Word)bits & bitsLowMask) << lowNdxInW0);
			m_words[word1Ndx] = (m_words[word1Ndx] & ~(((Word)1 << (highNdxInW1+1)) - 1))	| (((Word)bits & ~bitsLowMask) >> numBitsToSetInW0);
		}
	}

	void assignToMemory (deUint8* dst) const
	{
		for (int wordNdx = 0; wordNdx < NUM_WORDS; wordNdx++)
		{
			for (int byteNdx = 0; byteNdx < WORD_BYTES; byteNdx++)
				dst[wordNdx*WORD_BYTES + byteNdx] = (deUint8)((m_words[wordNdx] >> (8*byteNdx)) & 0xff);
		}
	}

	void pushBytesToVector (vector<deUint8>& dst) const
	{
		const int assignStartIndex = (int)dst.size();
		dst.resize(dst.size() + BLOCK_SIZE_BYTES);
		assignToMemory(&dst[assignStartIndex]);
	}

private:
	Word m_words[NUM_WORDS];
};

// A helper for sequential access into a AssignBlock128.
class BitAssignAccessStream
{
public:
	BitAssignAccessStream (AssignBlock128& dst, int startNdxInSrc, int length, bool forward)
		: m_dst				(dst)
		, m_startNdxInSrc	(startNdxInSrc)
		, m_length			(length)
		, m_forward			(forward)
		, m_ndx				(0)
	{
	}

	// Set the next num bits. Bits at positions greater than or equal to m_length are not touched.
	void setNext (int num, deUint32 bits)
	{
		DE_ASSERT((bits & (((deUint64)1 << num) - 1)) == bits);

		if (num == 0 || m_ndx >= m_length)
			return;

		const int		end				= m_ndx + num;
		const int		numBitsToDst	= de::max(0, de::min(m_length, end) - m_ndx);
		const int		low				= m_ndx;
		const int		high			= m_ndx + numBitsToDst - 1;
		const deUint32	actualBits		= getBits(bits, 0, numBitsToDst-1);

		m_ndx += num;

		return m_forward ? m_dst.setBits(m_startNdxInSrc + low,  m_startNdxInSrc + high, actualBits)
						 : m_dst.setBits(m_startNdxInSrc - high, m_startNdxInSrc - low, reverseBits(actualBits, numBitsToDst));
	}

private:
	AssignBlock128&		m_dst;
	const int			m_startNdxInSrc;
	const int			m_length;
	const bool			m_forward;

	int					m_ndx;
};

struct VoidExtentParams
{
	DE_STATIC_ASSERT((de::meta::TypesSame<deFloat16, deUint16>::Value));
	bool		isHDR;
	deUint16	r;
	deUint16	g;
	deUint16	b;
	deUint16	a;
	// \note Currently extent coordinates are all set to all-ones.

	VoidExtentParams (bool isHDR_, deUint16 r_, deUint16 g_, deUint16 b_, deUint16 a_) : isHDR(isHDR_), r(r_), g(g_), b(b_), a(a_) {}
};

static AssignBlock128 generateVoidExtentBlock (const VoidExtentParams& params)
{
	AssignBlock128 block;

	block.setBits(0, 8, 0x1fc); // \note Marks void-extent block.
	block.setBit(9, params.isHDR);
	block.setBits(10, 11, 3); // \note Spec shows that these bits are both set, although they serve no purpose.

	// Extent coordinates - currently all-ones.
	block.setBits(12, 24, 0x1fff);
	block.setBits(25, 37, 0x1fff);
	block.setBits(38, 50, 0x1fff);
	block.setBits(51, 63, 0x1fff);

	DE_ASSERT(!params.isHDR || (!isFloat16InfOrNan(params.r) &&
								!isFloat16InfOrNan(params.g) &&
								!isFloat16InfOrNan(params.b) &&
								!isFloat16InfOrNan(params.a)));

	block.setBits(64,  79,  params.r);
	block.setBits(80,  95,  params.g);
	block.setBits(96,  111, params.b);
	block.setBits(112, 127, params.a);

	return block;
}

// An input array of ISE inputs for an entire ASTC block. Can be given as either single values in the
// range [0, maximumValueOfISERange] or as explicit block value specifications. The latter is needed
// so we can test all possible values of T and Q in a block, since multiple T or Q values may map
// to the same set of decoded values.
struct ISEInput
{
	struct Block
	{
		deUint32 tOrQValue; //!< The 8-bit T or 7-bit Q in a trit or quint ISE block.
		deUint32 bitValues[5];
	};

	bool isGivenInBlockForm;
	union
	{
		//!< \note 64 comes from the maximum number of weight values in an ASTC block.
		deUint32	plain[64];
		Block		block[64];
	} value;

	ISEInput (void)
		: isGivenInBlockForm (false)
	{
	}
};

static inline deUint32 computeISERangeMax (const ISEParams& iseParams)
{
	switch (iseParams.mode)
	{
		case ISEMODE_TRIT:			return (1u << iseParams.numBits) * 3 - 1;
		case ISEMODE_QUINT:			return (1u << iseParams.numBits) * 5 - 1;
		case ISEMODE_PLAIN_BIT:		return (1u << iseParams.numBits)     - 1;
		default:
			DE_ASSERT(false);
			return -1;
	}
}

struct NormalBlockParams
{
	int					weightGridWidth;
	int					weightGridHeight;
	ISEParams			weightISEParams;
	bool				isDualPlane;
	deUint32			ccs; //! \note Irrelevant if !isDualPlane.
	int					numPartitions;
	deUint32			colorEndpointModes[4];
	// \note Below members are irrelevant if numPartitions == 1.
	bool				isMultiPartSingleCemMode; //! \note If true, the single CEM is at colorEndpointModes[0].
	deUint32			partitionSeed;

	NormalBlockParams (void)
		: weightGridWidth			(-1)
		, weightGridHeight			(-1)
		, weightISEParams			(ISEMODE_LAST, -1)
		, isDualPlane				(true)
		, ccs						((deUint32)-1)
		, numPartitions				(-1)
		, isMultiPartSingleCemMode	(false)
		, partitionSeed				((deUint32)-1)
	{
		colorEndpointModes[0] = 0;
		colorEndpointModes[1] = 0;
		colorEndpointModes[2] = 0;
		colorEndpointModes[3] = 0;
	}
};

struct NormalBlockISEInputs
{
	ISEInput weight;
	ISEInput endpoint;

	NormalBlockISEInputs (void)
		: weight	()
		, endpoint	()
	{
	}
};

static inline int computeNumWeights (const NormalBlockParams& params)
{
	return params.weightGridWidth * params.weightGridHeight * (params.isDualPlane ? 2 : 1);
}

static inline int computeNumBitsForColorEndpoints (const NormalBlockParams& params)
{
	const int numWeightBits			= computeNumRequiredBits(params.weightISEParams, computeNumWeights(params));
	const int numConfigDataBits		= (params.numPartitions == 1 ? 17 : params.isMultiPartSingleCemMode ? 29 : 25 + 3*params.numPartitions) +
									  (params.isDualPlane ? 2 : 0);

	return 128 - numWeightBits - numConfigDataBits;
}

static inline int computeNumColorEndpointValues (const deUint32* endpointModes, int numPartitions, bool isMultiPartSingleCemMode)
{
	if (isMultiPartSingleCemMode)
		return numPartitions * computeNumColorEndpointValues(endpointModes[0]);
	else
	{
		int result = 0;
		for (int i = 0; i < numPartitions; i++)
			result += computeNumColorEndpointValues(endpointModes[i]);
		return result;
	}
}

static inline bool isValidBlockParams (const NormalBlockParams& params, int blockWidth, int blockHeight)
{
	const int numWeights				= computeNumWeights(params);
	const int numWeightBits				= computeNumRequiredBits(params.weightISEParams, numWeights);
	const int numColorEndpointValues	= computeNumColorEndpointValues(&params.colorEndpointModes[0], params.numPartitions, params.isMultiPartSingleCemMode);
	const int numBitsForColorEndpoints	= computeNumBitsForColorEndpoints(params);

	return numWeights <= 64										&&
		   de::inRange(numWeightBits, 24, 96)					&&
		   params.weightGridWidth <= blockWidth					&&
		   params.weightGridHeight <= blockHeight				&&
		   !(params.numPartitions == 4 && params.isDualPlane)	&&
		   numColorEndpointValues <= 18							&&
		   numBitsForColorEndpoints >= deDivRoundUp32(13*numColorEndpointValues, 5);
}

// Write bits 0 to 10 of an ASTC block.
static void writeBlockMode (AssignBlock128& dst, const NormalBlockParams& blockParams)
{
	const deUint32	d = blockParams.isDualPlane != 0;
	// r and h initialized in switch below.
	deUint32		r;
	deUint32		h;
	// a, b and blockModeLayoutNdx initialized in block mode layout index detecting loop below.
	deUint32		a = (deUint32)-1;
	deUint32		b = (deUint32)-1;
	int				blockModeLayoutNdx;

	// Find the values of r and h (ISE range).
	switch (computeISERangeMax(blockParams.weightISEParams))
	{
		case 1:		r = 2; h = 0;	break;
		case 2:		r = 3; h = 0;	break;
		case 3:		r = 4; h = 0;	break;
		case 4:		r = 5; h = 0;	break;
		case 5:		r = 6; h = 0;	break;
		case 7:		r = 7; h = 0;	break;

		case 9:		r = 2; h = 1;	break;
		case 11:	r = 3; h = 1;	break;
		case 15:	r = 4; h = 1;	break;
		case 19:	r = 5; h = 1;	break;
		case 23:	r = 6; h = 1;	break;
		case 31:	r = 7; h = 1;	break;

		default:
			DE_ASSERT(false);
			r = (deUint32)-1;
			h = (deUint32)-1;
	}

	// Find block mode layout index, i.e. appropriate row in the "2d block mode layout" table in ASTC spec.

	{
		enum BlockModeLayoutABVariable { Z=0, A=1, B=2 };

		static const struct BlockModeLayout
		{
			int							aNumBits;
			int							bNumBits;
			BlockModeLayoutABVariable	gridWidthVariableTerm;
			int							gridWidthConstantTerm;
			BlockModeLayoutABVariable	gridHeightVariableTerm;
			int							gridHeightConstantTerm;
		} blockModeLayouts[] =
		{
			{ 2, 2,   B,  4,   A,  2},
			{ 2, 2,   B,  8,   A,  2},
			{ 2, 2,   A,  2,   B,  8},
			{ 2, 1,   A,  2,   B,  6},
			{ 2, 1,   B,  2,   A,  2},
			{ 2, 0,   Z, 12,   A,  2},
			{ 2, 0,   A,  2,   Z, 12},
			{ 0, 0,   Z,  6,   Z, 10},
			{ 0, 0,   Z, 10,   Z,  6},
			{ 2, 2,   A,  6,   B,  6}
		};

		for (blockModeLayoutNdx = 0; blockModeLayoutNdx < DE_LENGTH_OF_ARRAY(blockModeLayouts); blockModeLayoutNdx++)
		{
			const BlockModeLayout&	layout					= blockModeLayouts[blockModeLayoutNdx];
			const int				aMax					= (1 << layout.aNumBits) - 1;
			const int				bMax					= (1 << layout.bNumBits) - 1;
			const int				variableOffsetsMax[3]	= { 0, aMax, bMax };
			const int				widthMin				= layout.gridWidthConstantTerm;
			const int				heightMin				= layout.gridHeightConstantTerm;
			const int				widthMax				= widthMin  + variableOffsetsMax[layout.gridWidthVariableTerm];
			const int				heightMax				= heightMin + variableOffsetsMax[layout.gridHeightVariableTerm];

			DE_ASSERT(layout.gridWidthVariableTerm != layout.gridHeightVariableTerm || layout.gridWidthVariableTerm == Z);

			if (de::inRange(blockParams.weightGridWidth, widthMin, widthMax) &&
				de::inRange(blockParams.weightGridHeight, heightMin, heightMax))
			{
				deUint32	dummy			= 0;
				deUint32&	widthVariable	= layout.gridWidthVariableTerm == A  ? a : layout.gridWidthVariableTerm == B  ? b : dummy;
				deUint32&	heightVariable	= layout.gridHeightVariableTerm == A ? a : layout.gridHeightVariableTerm == B ? b : dummy;

				widthVariable	= blockParams.weightGridWidth  - layout.gridWidthConstantTerm;
				heightVariable	= blockParams.weightGridHeight - layout.gridHeightConstantTerm;

				break;
			}
		}
	}

	// Set block mode bits.

	const deUint32 a0 = getBit(a, 0);
	const deUint32 a1 = getBit(a, 1);
	const deUint32 b0 = getBit(b, 0);
	const deUint32 b1 = getBit(b, 1);
	const deUint32 r0 = getBit(r, 0);
	const deUint32 r1 = getBit(r, 1);
	const deUint32 r2 = getBit(r, 2);

#define SB(NDX, VAL) dst.setBit((NDX), (VAL))
#define ASSIGN_BITS(B10, B9, B8, B7, B6, B5, B4, B3, B2, B1, B0) do { SB(10,(B10)); SB(9,(B9)); SB(8,(B8)); SB(7,(B7)); SB(6,(B6)); SB(5,(B5)); SB(4,(B4)); SB(3,(B3)); SB(2,(B2)); SB(1,(B1)); SB(0,(B0)); } while (false)

	switch (blockModeLayoutNdx)
	{
		case 0: ASSIGN_BITS(d,  h,  b1, b0, a1, a0, r0, 0,  0,  r2, r1);									break;
		case 1: ASSIGN_BITS(d,  h,  b1, b0, a1, a0, r0, 0,  1,  r2, r1);									break;
		case 2: ASSIGN_BITS(d,  h,  b1, b0, a1, a0, r0, 1,  0,  r2, r1);									break;
		case 3: ASSIGN_BITS(d,  h,   0,  b, a1, a0, r0, 1,  1,  r2, r1);									break;
		case 4: ASSIGN_BITS(d,  h,   1,  b, a1, a0, r0, 1,  1,  r2, r1);									break;
		case 5: ASSIGN_BITS(d,  h,   0,  0, a1, a0, r0, r2, r1,  0,  0);									break;
		case 6: ASSIGN_BITS(d,  h,   0,  1, a1, a0, r0, r2, r1,  0,  0);									break;
		case 7: ASSIGN_BITS(d,  h,   1,  1,  0,  0, r0, r2, r1,  0,  0);									break;
		case 8: ASSIGN_BITS(d,  h,   1,  1,  0,  1, r0, r2, r1,  0,  0);									break;
		case 9: ASSIGN_BITS(b1, b0,  1,  0, a1, a0, r0, r2, r1,  0,  0); DE_ASSERT(d == 0 && h == 0);		break;
		default:
			DE_ASSERT(false);
	}

#undef ASSIGN_BITS
#undef SB
}

// Write color endpoint mode data of an ASTC block.
static void writeColorEndpointModes (AssignBlock128& dst, const deUint32* colorEndpointModes, bool isMultiPartSingleCemMode, int numPartitions, int extraCemBitsStart)
{
	if (numPartitions == 1)
		dst.setBits(13, 16, colorEndpointModes[0]);
	else
	{
		if (isMultiPartSingleCemMode)
		{
			dst.setBits(23, 24, 0);
			dst.setBits(25, 28, colorEndpointModes[0]);
		}
		else
		{
			DE_ASSERT(numPartitions > 0);
			const deUint32 minCem				= *std::min_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
			const deUint32 maxCem				= *std::max_element(&colorEndpointModes[0], &colorEndpointModes[numPartitions]);
			const deUint32 minCemClass			= minCem/4;
			const deUint32 maxCemClass			= maxCem/4;
			DE_ASSERT(maxCemClass - minCemClass <= 1);
			DE_UNREF(minCemClass); // \note For non-debug builds.
			const deUint32 highLevelSelector	= de::max(1u, maxCemClass);

			dst.setBits(23, 24, highLevelSelector);

			for (int partNdx = 0; partNdx < numPartitions; partNdx++)
			{
				const deUint32 c			= colorEndpointModes[partNdx] / 4 == highLevelSelector ? 1 : 0;
				const deUint32 m			= colorEndpointModes[partNdx] % 4;
				const deUint32 lowMBit0Ndx	= numPartitions + 2*partNdx;
				const deUint32 lowMBit1Ndx	= numPartitions + 2*partNdx + 1;
				dst.setBit(25 + partNdx, c);
				dst.setBit(lowMBit0Ndx < 4 ? 25+lowMBit0Ndx : extraCemBitsStart+lowMBit0Ndx-4, getBit(m, 0));
				dst.setBit(lowMBit1Ndx < 4 ? 25+lowMBit1Ndx : extraCemBitsStart+lowMBit1Ndx-4, getBit(m, 1));
			}
		}
	}
}

static void encodeISETritBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
{
	// tritBlockTValue[t0][t1][t2][t3][t4] is a value of T (not necessarily the only one) that will yield the given trits when decoded.
	static const deUint32 tritBlockTValue[3][3][3][3][3] =
	{
		{
			{{{0, 128, 96}, {32, 160, 224}, {64, 192, 28}}, {{16, 144, 112}, {48, 176, 240}, {80, 208, 156}}, {{3, 131, 99}, {35, 163, 227}, {67, 195, 31}}},
			{{{4, 132, 100}, {36, 164, 228}, {68, 196, 60}}, {{20, 148, 116}, {52, 180, 244}, {84, 212, 188}}, {{19, 147, 115}, {51, 179, 243}, {83, 211, 159}}},
			{{{8, 136, 104}, {40, 168, 232}, {72, 200, 92}}, {{24, 152, 120}, {56, 184, 248}, {88, 216, 220}}, {{12, 140, 108}, {44, 172, 236}, {76, 204, 124}}}
		},
		{
			{{{1, 129, 97}, {33, 161, 225}, {65, 193, 29}}, {{17, 145, 113}, {49, 177, 241}, {81, 209, 157}}, {{7, 135, 103}, {39, 167, 231}, {71, 199, 63}}},
			{{{5, 133, 101}, {37, 165, 229}, {69, 197, 61}}, {{21, 149, 117}, {53, 181, 245}, {85, 213, 189}}, {{23, 151, 119}, {55, 183, 247}, {87, 215, 191}}},
			{{{9, 137, 105}, {41, 169, 233}, {73, 201, 93}}, {{25, 153, 121}, {57, 185, 249}, {89, 217, 221}}, {{13, 141, 109}, {45, 173, 237}, {77, 205, 125}}}
		},
		{
			{{{2, 130, 98}, {34, 162, 226}, {66, 194, 30}}, {{18, 146, 114}, {50, 178, 242}, {82, 210, 158}}, {{11, 139, 107}, {43, 171, 235}, {75, 203, 95}}},
			{{{6, 134, 102}, {38, 166, 230}, {70, 198, 62}}, {{22, 150, 118}, {54, 182, 246}, {86, 214, 190}}, {{27, 155, 123}, {59, 187, 251}, {91, 219, 223}}},
			{{{10, 138, 106}, {42, 170, 234}, {74, 202, 94}}, {{26, 154, 122}, {58, 186, 250}, {90, 218, 222}}, {{14, 142, 110}, {46, 174, 238}, {78, 206, 126}}}
		}
	};

	DE_ASSERT(de::inRange(numValues, 1, 5));

	deUint32 tritParts[5];
	deUint32 bitParts[5];

	for (int i = 0; i < 5; i++)
	{
		if (i < numValues)
		{
			if (fromExplicitInputBlock)
			{
				bitParts[i]		= blockInput.bitValues[i];
				tritParts[i]	= -1; // \note Won't be used, but silences warning.
			}
			else
			{
				// \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
				bitParts[i]		= numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
				tritParts[i]	= nonBlockInput[i] >> numBits;
			}
		}
		else
		{
			bitParts[i]		= 0;
			tritParts[i]	= 0;
		}
	}

	const deUint32 T = fromExplicitInputBlock ? blockInput.tOrQValue : tritBlockTValue[tritParts[0]]
																					  [tritParts[1]]
																					  [tritParts[2]]
																					  [tritParts[3]]
																					  [tritParts[4]];

	dst.setNext(numBits,	bitParts[0]);
	dst.setNext(2,			getBits(T, 0, 1));
	dst.setNext(numBits,	bitParts[1]);
	dst.setNext(2,			getBits(T, 2, 3));
	dst.setNext(numBits,	bitParts[2]);
	dst.setNext(1,			getBit(T, 4));
	dst.setNext(numBits,	bitParts[3]);
	dst.setNext(2,			getBits(T, 5, 6));
	dst.setNext(numBits,	bitParts[4]);
	dst.setNext(1,			getBit(T, 7));
}

static void encodeISEQuintBlock (BitAssignAccessStream& dst, int numBits, bool fromExplicitInputBlock, const ISEInput::Block& blockInput, const deUint32* nonBlockInput, int numValues)
{
	// quintBlockQValue[q0][q1][q2] is a value of Q (not necessarily the only one) that will yield the given quints when decoded.
	static const deUint32 quintBlockQValue[5][5][5] =
	{
		{{0, 32, 64, 96, 102}, {8, 40, 72, 104, 110}, {16, 48, 80, 112, 118}, {24, 56, 88, 120, 126}, {5, 37, 69, 101, 39}},
		{{1, 33, 65, 97, 103}, {9, 41, 73, 105, 111}, {17, 49, 81, 113, 119}, {25, 57, 89, 121, 127}, {13, 45, 77, 109, 47}},
		{{2, 34, 66, 98, 70}, {10, 42, 74, 106, 78}, {18, 50, 82, 114, 86}, {26, 58, 90, 122, 94}, {21, 53, 85, 117, 55}},
		{{3, 35, 67, 99, 71}, {11, 43, 75, 107, 79}, {19, 51, 83, 115, 87}, {27, 59, 91, 123, 95}, {29, 61, 93, 125, 63}},
		{{4, 36, 68, 100, 38}, {12, 44, 76, 108, 46}, {20, 52, 84, 116, 54}, {28, 60, 92, 124, 62}, {6, 14, 22, 30, 7}}
	};

	DE_ASSERT(de::inRange(numValues, 1, 3));

	deUint32 quintParts[3];
	deUint32 bitParts[3];

	for (int i = 0; i < 3; i++)
	{
		if (i < numValues)
		{
			if (fromExplicitInputBlock)
			{
				bitParts[i]		= blockInput.bitValues[i];
				quintParts[i]	= -1; // \note Won't be used, but silences warning.
			}
			else
			{
				// \todo [2016-01-20 pyry] numBits = 0 doesn't make sense
				bitParts[i]		= numBits > 0 ? getBits(nonBlockInput[i], 0, numBits-1) : 0;
				quintParts[i]	= nonBlockInput[i] >> numBits;
			}
		}
		else
		{
			bitParts[i]		= 0;
			quintParts[i]	= 0;
		}
	}

	const deUint32 Q = fromExplicitInputBlock ? blockInput.tOrQValue : quintBlockQValue[quintParts[0]]
																					   [quintParts[1]]
																					   [quintParts[2]];

	dst.setNext(numBits,	bitParts[0]);
	dst.setNext(3,			getBits(Q, 0, 2));
	dst.setNext(numBits,	bitParts[1]);
	dst.setNext(2,			getBits(Q, 3, 4));
	dst.setNext(numBits,	bitParts[2]);
	dst.setNext(2,			getBits(Q, 5, 6));
}

static void encodeISEBitBlock (BitAssignAccessStream& dst, int numBits, deUint32 value)
{
	DE_ASSERT(de::inRange(value, 0u, (1u<<numBits)-1));
	dst.setNext(numBits, value);
}

static void encodeISE (BitAssignAccessStream& dst, const ISEParams& params, const ISEInput& input, int numValues)
{
	if (params.mode == ISEMODE_TRIT)
	{
		const int numBlocks = deDivRoundUp32(numValues, 5);
		for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
		{
			const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 5*(numBlocks-1) : 5;
			encodeISETritBlock(dst, params.numBits, input.isGivenInBlockForm,
							   input.isGivenInBlockForm ? input.value.block[blockNdx]	: ISEInput::Block(),
							   input.isGivenInBlockForm ? DE_NULL						: &input.value.plain[5*blockNdx],
							   numValuesInBlock);
		}
	}
	else if (params.mode == ISEMODE_QUINT)
	{
		const int numBlocks = deDivRoundUp32(numValues, 3);
		for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
		{
			const int numValuesInBlock = blockNdx == numBlocks-1 ? numValues - 3*(numBlocks-1) : 3;
			encodeISEQuintBlock(dst, params.numBits, input.isGivenInBlockForm,
								input.isGivenInBlockForm ? input.value.block[blockNdx]	: ISEInput::Block(),
								input.isGivenInBlockForm ? DE_NULL						: &input.value.plain[3*blockNdx],
								numValuesInBlock);
		}
	}
	else
	{
		DE_ASSERT(params.mode == ISEMODE_PLAIN_BIT);
		for (int i = 0; i < numValues; i++)
			encodeISEBitBlock(dst, params.numBits, input.isGivenInBlockForm ? input.value.block[i].bitValues[0] : input.value.plain[i]);
	}
}

static void writeWeightData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numWeights)
{
	const int				numWeightBits	= computeNumRequiredBits(iseParams, numWeights);
	BitAssignAccessStream	access			(dst, 127, numWeightBits, false);
	encodeISE(access, iseParams, input, numWeights);
}

static void writeColorEndpointData (AssignBlock128& dst, const ISEParams& iseParams, const ISEInput& input, int numEndpoints, int numBitsForColorEndpoints, int colorEndpointDataStartNdx)
{
	BitAssignAccessStream access(dst, colorEndpointDataStartNdx, numBitsForColorEndpoints, true);
	encodeISE(access, iseParams, input, numEndpoints);
}

static AssignBlock128 generateNormalBlock (const NormalBlockParams& blockParams, int blockWidth, int blockHeight, const NormalBlockISEInputs& iseInputs)
{
	DE_ASSERT(isValidBlockParams(blockParams, blockWidth, blockHeight));
	DE_UNREF(blockWidth);	// \note For non-debug builds.
	DE_UNREF(blockHeight);	// \note For non-debug builds.

	AssignBlock128	block;
	const int		numWeights		= computeNumWeights(blockParams);
	const int		numWeightBits	= computeNumRequiredBits(blockParams.weightISEParams, numWeights);

	writeBlockMode(block, blockParams);

	block.setBits(11, 12, blockParams.numPartitions - 1);
	if (blockParams.numPartitions > 1)
		block.setBits(13, 22, blockParams.partitionSeed);

	{
		const int extraCemBitsStart = 127 - numWeightBits - (blockParams.numPartitions == 1 || blockParams.isMultiPartSingleCemMode		? -1
															: blockParams.numPartitions == 4											? 7
															: blockParams.numPartitions == 3											? 4
															: blockParams.numPartitions == 2											? 1
															: 0);

		writeColorEndpointModes(block, &blockParams.colorEndpointModes[0], blockParams.isMultiPartSingleCemMode, blockParams.numPartitions, extraCemBitsStart);

		if (blockParams.isDualPlane)
			block.setBits(extraCemBitsStart-2, extraCemBitsStart-1, blockParams.ccs);
	}

	writeWeightData(block, blockParams.weightISEParams, iseInputs.weight, numWeights);

	{
		const int			numColorEndpointValues		= computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
		const int			numBitsForColorEndpoints	= computeNumBitsForColorEndpoints(blockParams);
		const int			colorEndpointDataStartNdx	= blockParams.numPartitions == 1 ? 17 : 29;
		const ISEParams&	colorEndpointISEParams		= computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);

		writeColorEndpointData(block, colorEndpointISEParams, iseInputs.endpoint, numColorEndpointValues, numBitsForColorEndpoints, colorEndpointDataStartNdx);
	}

	return block;
}

// Generate default ISE inputs for weight and endpoint data - gradient-ish values.
static NormalBlockISEInputs generateDefaultISEInputs (const NormalBlockParams& blockParams)
{
	NormalBlockISEInputs result;

	{
		result.weight.isGivenInBlockForm = false;

		const int numWeights		= computeNumWeights(blockParams);
		const int weightRangeMax	= computeISERangeMax(blockParams.weightISEParams);

		if (blockParams.isDualPlane)
		{
			for (int i = 0; i < numWeights; i += 2)
				result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);

			for (int i = 1; i < numWeights; i += 2)
				result.weight.value.plain[i] = weightRangeMax - (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
		}
		else
		{
			for (int i = 0; i < numWeights; i++)
				result.weight.value.plain[i] = (i*weightRangeMax + (numWeights-1)/2) / (numWeights-1);
		}
	}

	{
		result.endpoint.isGivenInBlockForm = false;

		const int			numColorEndpointValues		= computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
		const int			numBitsForColorEndpoints	= computeNumBitsForColorEndpoints(blockParams);
		const ISEParams&	colorEndpointISEParams		= computeMaximumRangeISEParams(numBitsForColorEndpoints, numColorEndpointValues);
		const int			colorEndpointRangeMax		= computeISERangeMax(colorEndpointISEParams);

		for (int i = 0; i < numColorEndpointValues; i++)
			result.endpoint.value.plain[i] = (i*colorEndpointRangeMax + (numColorEndpointValues-1)/2) / (numColorEndpointValues-1);
	}

	return result;
}

static const ISEParams s_weightISEParamsCandidates[] =
{
	ISEParams(ISEMODE_PLAIN_BIT,	1),
	ISEParams(ISEMODE_TRIT,			0),
	ISEParams(ISEMODE_PLAIN_BIT,	2),
	ISEParams(ISEMODE_QUINT,		0),
	ISEParams(ISEMODE_TRIT,			1),
	ISEParams(ISEMODE_PLAIN_BIT,	3),
	ISEParams(ISEMODE_QUINT,		1),
	ISEParams(ISEMODE_TRIT,			2),
	ISEParams(ISEMODE_PLAIN_BIT,	4),
	ISEParams(ISEMODE_QUINT,		2),
	ISEParams(ISEMODE_TRIT,			3),
	ISEParams(ISEMODE_PLAIN_BIT,	5)
};

void generateRandomBlock (deUint8* dst, const IVec3& blockSize, de::Random& rnd)
{
	DE_ASSERT(blockSize.z() == 1);

	if (rnd.getFloat() < 0.1f)
	{
		// Void extent block.
		const bool		isVoidExtentHDR		= rnd.getBool();
		const deUint16	r					= isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
		const deUint16	g					= isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
		const deUint16	b					= isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
		const deUint16	a					= isVoidExtentHDR ? deFloat32To16(rnd.getFloat(0.0f, 1.0f)) : (deUint16)rnd.getInt(0, 0xffff);
		generateVoidExtentBlock(VoidExtentParams(isVoidExtentHDR, r, g, b, a)).assignToMemory(dst);
	}
	else
	{
		// Not void extent block.

		// Generate block params.

		NormalBlockParams blockParams;

		do
		{
			blockParams.weightGridWidth				= rnd.getInt(2, blockSize.x());
			blockParams.weightGridHeight			= rnd.getInt(2, blockSize.y());
			blockParams.weightISEParams				= s_weightISEParamsCandidates[rnd.getInt(0, DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates)-1)];
			blockParams.numPartitions				= rnd.getInt(1, 4);
			blockParams.isMultiPartSingleCemMode	= rnd.getFloat() < 0.25f;
			blockParams.isDualPlane					= blockParams.numPartitions != 4 && rnd.getBool();
			blockParams.ccs							= rnd.getInt(0, 3);
			blockParams.partitionSeed				= rnd.getInt(0, 1023);

			blockParams.colorEndpointModes[0] = rnd.getInt(0, 15);

			{
				const int cemDiff = blockParams.isMultiPartSingleCemMode		? 0
									: blockParams.colorEndpointModes[0] == 0	? 1
									: blockParams.colorEndpointModes[0] == 15	? -1
									: rnd.getBool()								? 1 : -1;

				for (int i = 1; i < blockParams.numPartitions; i++)
					blockParams.colorEndpointModes[i] = blockParams.colorEndpointModes[0] + (cemDiff == -1 ? rnd.getInt(-1, 0) : cemDiff == 1 ? rnd.getInt(0, 1) : 0);
			}
		} while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()));

		// Generate ISE inputs for both weight and endpoint data.

		NormalBlockISEInputs iseInputs;

		for (int weightOrEndpoints = 0; weightOrEndpoints <= 1; weightOrEndpoints++)
		{
			const bool			setWeights	= weightOrEndpoints == 0;
			const int			numValues	= setWeights ? computeNumWeights(blockParams) :
												computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], blockParams.numPartitions, blockParams.isMultiPartSingleCemMode);
			const ISEParams		iseParams	= setWeights ? blockParams.weightISEParams : computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams), numValues);
			ISEInput&			iseInput	= setWeights ? iseInputs.weight : iseInputs.endpoint;

			iseInput.isGivenInBlockForm = rnd.getBool();

			if (iseInput.isGivenInBlockForm)
			{
				const int numValuesPerISEBlock	= iseParams.mode == ISEMODE_TRIT	? 5
												: iseParams.mode == ISEMODE_QUINT	? 3
												:									  1;
				const int iseBitMax				= (1 << iseParams.numBits) - 1;
				const int numISEBlocks			= deDivRoundUp32(numValues, numValuesPerISEBlock);

				for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocks; iseBlockNdx++)
				{
					iseInput.value.block[iseBlockNdx].tOrQValue = rnd.getInt(0, 255);
					for (int i = 0; i < numValuesPerISEBlock; i++)
						iseInput.value.block[iseBlockNdx].bitValues[i] = rnd.getInt(0, iseBitMax);
				}
			}
			else
			{
				const int rangeMax = computeISERangeMax(iseParams);

				for (int valueNdx = 0; valueNdx < numValues; valueNdx++)
					iseInput.value.plain[valueNdx] = rnd.getInt(0, rangeMax);
			}
		}

		generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).assignToMemory(dst);
	}
}

} // anonymous

// Generate block data for a given BlockTestType and format.
void generateBlockCaseTestData (vector<deUint8>& dst, CompressedTexFormat format, BlockTestType testType)
{
	DE_ASSERT(isAstcFormat(format));
	DE_ASSERT(!(isAstcSRGBFormat(format) && isBlockTestTypeHDROnly(testType)));

	const IVec3 blockSize = getBlockPixelSize(format);
	DE_ASSERT(blockSize.z() == 1);

	switch (testType)
	{
		case BLOCK_TEST_TYPE_VOID_EXTENT_LDR:
		// Generate a gradient-like set of LDR void-extent blocks.
		{
			const int			numBlocks	= 1<<13;
			const deUint32		numValues	= 1<<16;
			dst.reserve(numBlocks*BLOCK_SIZE_BYTES);

			for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
			{
				const deUint32 baseValue	= blockNdx*(numValues-1) / (numBlocks-1);
				const deUint16 r			= (deUint16)((baseValue + numValues*0/4) % numValues);
				const deUint16 g			= (deUint16)((baseValue + numValues*1/4) % numValues);
				const deUint16 b			= (deUint16)((baseValue + numValues*2/4) % numValues);
				const deUint16 a			= (deUint16)((baseValue + numValues*3/4) % numValues);
				AssignBlock128 block;

				generateVoidExtentBlock(VoidExtentParams(false, r, g, b, a)).pushBytesToVector(dst);
			}

			break;
		}

		case BLOCK_TEST_TYPE_VOID_EXTENT_HDR:
		// Generate a gradient-like set of HDR void-extent blocks, with values ranging from the largest finite negative to largest finite positive of fp16.
		{
			const float		minValue	= -65504.0f;
			const float		maxValue	= +65504.0f;
			const int		numBlocks	= 1<<13;
			dst.reserve(numBlocks*BLOCK_SIZE_BYTES);

			for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
			{
				const int			rNdx	= (blockNdx + numBlocks*0/4) % numBlocks;
				const int			gNdx	= (blockNdx + numBlocks*1/4) % numBlocks;
				const int			bNdx	= (blockNdx + numBlocks*2/4) % numBlocks;
				const int			aNdx	= (blockNdx + numBlocks*3/4) % numBlocks;
				const deFloat16		r		= deFloat32To16(minValue + (float)rNdx * (maxValue - minValue) / (float)(numBlocks-1));
				const deFloat16		g		= deFloat32To16(minValue + (float)gNdx * (maxValue - minValue) / (float)(numBlocks-1));
				const deFloat16		b		= deFloat32To16(minValue + (float)bNdx * (maxValue - minValue) / (float)(numBlocks-1));
				const deFloat16		a		= deFloat32To16(minValue + (float)aNdx * (maxValue - minValue) / (float)(numBlocks-1));

				generateVoidExtentBlock(VoidExtentParams(true, r, g, b, a)).pushBytesToVector(dst);
			}

			break;
		}

		case BLOCK_TEST_TYPE_WEIGHT_GRID:
		// Generate different combinations of plane count, weight ISE params, and grid size.
		{
			for (int isDualPlane = 0;		isDualPlane <= 1;												isDualPlane++)
			for (int iseParamsNdx = 0;		iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates);	iseParamsNdx++)
			for (int weightGridWidth = 2;	weightGridWidth <= 12;											weightGridWidth++)
			for (int weightGridHeight = 2;	weightGridHeight <= 12;											weightGridHeight++)
			{
				NormalBlockParams		blockParams;
				NormalBlockISEInputs	iseInputs;

				blockParams.weightGridWidth			= weightGridWidth;
				blockParams.weightGridHeight		= weightGridHeight;
				blockParams.isDualPlane				= isDualPlane != 0;
				blockParams.weightISEParams			= s_weightISEParamsCandidates[iseParamsNdx];
				blockParams.ccs						= 0;
				blockParams.numPartitions			= 1;
				blockParams.colorEndpointModes[0]	= 0;

				if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
					generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
			}

			break;
		}

		case BLOCK_TEST_TYPE_WEIGHT_ISE:
		// For each weight ISE param set, generate blocks that cover:
		// - each single value of the ISE's range, at each position inside an ISE block
		// - for trit and quint ISEs, each single T or Q value of an ISE block
		{
			for (int iseParamsNdx = 0;	iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates);	iseParamsNdx++)
			{
				const ISEParams&	iseParams = s_weightISEParamsCandidates[iseParamsNdx];
				NormalBlockParams	blockParams;

				blockParams.weightGridWidth			= 4;
				blockParams.weightGridHeight		= 4;
				blockParams.weightISEParams			= iseParams;
				blockParams.numPartitions			= 1;
				blockParams.isDualPlane				= blockParams.weightGridWidth * blockParams.weightGridHeight < 24 ? true : false;
				blockParams.ccs						= 0;
				blockParams.colorEndpointModes[0]	= 0;

				while (!isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
				{
					blockParams.weightGridWidth--;
					blockParams.weightGridHeight--;
				}

				const int numValuesInISEBlock	= iseParams.mode == ISEMODE_TRIT ? 5 : iseParams.mode == ISEMODE_QUINT ? 3 : 1;
				const int numWeights			= computeNumWeights(blockParams);

				{
					const int				numWeightValues		= (int)computeISERangeMax(iseParams) + 1;
					const int				numBlocks			= deDivRoundUp32(numWeightValues, numWeights);
					NormalBlockISEInputs	iseInputs			= generateDefaultISEInputs(blockParams);
					iseInputs.weight.isGivenInBlockForm = false;

					for (int offset = 0;	offset < numValuesInISEBlock;	offset++)
					for (int blockNdx = 0;	blockNdx < numBlocks;			blockNdx++)
					{
						for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
							iseInputs.weight.value.plain[weightNdx] = (blockNdx*numWeights + weightNdx + offset) % numWeightValues;

						generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
					}
				}

				if (iseParams.mode == ISEMODE_TRIT || iseParams.mode == ISEMODE_QUINT)
				{
					NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
					iseInputs.weight.isGivenInBlockForm = true;

					const int numTQValues			= 1 << (iseParams.mode == ISEMODE_TRIT ? 8 : 7);
					const int numISEBlocksPerBlock	= deDivRoundUp32(numWeights, numValuesInISEBlock);
					const int numBlocks				= deDivRoundUp32(numTQValues, numISEBlocksPerBlock);

					for (int offset = 0;	offset < numValuesInISEBlock;	offset++)
					for (int blockNdx = 0;	blockNdx < numBlocks;			blockNdx++)
					{
						for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
						{
							for (int i = 0; i < numValuesInISEBlock; i++)
								iseInputs.weight.value.block[iseBlockNdx].bitValues[i] = 0;
							iseInputs.weight.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
						}

						generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
					}
				}
			}

			break;
		}

		case BLOCK_TEST_TYPE_CEMS:
		// For each plane count & partition count combination, generate all color endpoint mode combinations.
		{
			for (int isDualPlane = 0;		isDualPlane <= 1;								isDualPlane++)
			for (int numPartitions = 1;		numPartitions <= (isDualPlane != 0 ? 3 : 4);	numPartitions++)
			{
				// Multi-partition, single-CEM mode.
				if (numPartitions > 1)
				{
					for (deUint32 singleCem = 0; singleCem < 16; singleCem++)
					{
						NormalBlockParams blockParams;
						blockParams.weightGridWidth				= 4;
						blockParams.weightGridHeight			= 4;
						blockParams.isDualPlane					= isDualPlane != 0;
						blockParams.ccs							= 0;
						blockParams.numPartitions				= numPartitions;
						blockParams.isMultiPartSingleCemMode	= true;
						blockParams.colorEndpointModes[0]		= singleCem;
						blockParams.partitionSeed				= 634;

						for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
						{
							blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
							if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
							{
								generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
								break;
							}
						}
					}
				}

				// Separate-CEM mode.
				for (deUint32 cem0 = 0; cem0 < 16; cem0++)
				for (deUint32 cem1 = 0; cem1 < (numPartitions >= 2 ? 16u : 1u); cem1++)
				for (deUint32 cem2 = 0; cem2 < (numPartitions >= 3 ? 16u : 1u); cem2++)
				for (deUint32 cem3 = 0; cem3 < (numPartitions >= 4 ? 16u : 1u); cem3++)
				{
					NormalBlockParams blockParams;
					blockParams.weightGridWidth				= 4;
					blockParams.weightGridHeight			= 4;
					blockParams.isDualPlane					= isDualPlane != 0;
					blockParams.ccs							= 0;
					blockParams.numPartitions				= numPartitions;
					blockParams.isMultiPartSingleCemMode	= false;
					blockParams.colorEndpointModes[0]		= cem0;
					blockParams.colorEndpointModes[1]		= cem1;
					blockParams.colorEndpointModes[2]		= cem2;
					blockParams.colorEndpointModes[3]		= cem3;
					blockParams.partitionSeed				= 634;

					{
						const deUint32 minCem		= *std::min_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
						const deUint32 maxCem		= *std::max_element(&blockParams.colorEndpointModes[0], &blockParams.colorEndpointModes[numPartitions]);
						const deUint32 minCemClass	= minCem/4;
						const deUint32 maxCemClass	= maxCem/4;

						if (maxCemClass - minCemClass > 1)
							continue;
					}

					for (int iseParamsNdx = 0; iseParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates); iseParamsNdx++)
					{
						blockParams.weightISEParams = s_weightISEParamsCandidates[iseParamsNdx];
						if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
						{
							generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
							break;
						}
					}
				}
			}

			break;
		}

		case BLOCK_TEST_TYPE_PARTITION_SEED:
		// Test all partition seeds ("partition pattern indices").
		{
			for (int		numPartitions = 2;	numPartitions <= 4;		numPartitions++)
			for (deUint32	partitionSeed = 0;	partitionSeed < 1<<10;	partitionSeed++)
			{
				NormalBlockParams blockParams;
				blockParams.weightGridWidth				= 4;
				blockParams.weightGridHeight			= 4;
				blockParams.weightISEParams				= ISEParams(ISEMODE_PLAIN_BIT, 2);
				blockParams.isDualPlane					= false;
				blockParams.numPartitions				= numPartitions;
				blockParams.isMultiPartSingleCemMode	= true;
				blockParams.colorEndpointModes[0]		= 0;
				blockParams.partitionSeed				= partitionSeed;

				generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
			}

			break;
		}

		// \note Fall-through.
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR:
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:
		// For each endpoint mode, for each pair of components in the endpoint value, test 10x10 combinations of values for that pair.
		// \note Separate modes for HDR and mode 15 due to different color scales and biases.
		{
			for (deUint32 cem = 0; cem < 16; cem++)
			{
				const bool isHDRCem = cem == 2		||
									  cem == 3		||
									  cem == 7		||
									  cem == 11		||
									  cem == 14		||
									  cem == 15;

				if ((testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR			&& isHDRCem)					||
					(testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15		&& (!isHDRCem || cem == 15))	||
					(testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15		&& cem != 15))
					continue;

				NormalBlockParams blockParams;
				blockParams.weightGridWidth			= 3;
				blockParams.weightGridHeight		= 4;
				blockParams.weightISEParams			= ISEParams(ISEMODE_PLAIN_BIT, 2);
				blockParams.isDualPlane				= false;
				blockParams.numPartitions			= 1;
				blockParams.colorEndpointModes[0]	= cem;

				{
					const int			numBitsForEndpoints		= computeNumBitsForColorEndpoints(blockParams);
					const int			numEndpointParts		= computeNumColorEndpointValues(cem);
					const ISEParams		endpointISE				= computeMaximumRangeISEParams(numBitsForEndpoints, numEndpointParts);
					const int			endpointISERangeMax		= computeISERangeMax(endpointISE);

					for (int endpointPartNdx0 = 0;						endpointPartNdx0 < numEndpointParts; endpointPartNdx0++)
					for (int endpointPartNdx1 = endpointPartNdx0+1;		endpointPartNdx1 < numEndpointParts; endpointPartNdx1++)
					{
						NormalBlockISEInputs	iseInputs			= generateDefaultISEInputs(blockParams);
						const int				numEndpointValues	= de::min(10, endpointISERangeMax+1);

						for (int endpointValueNdx0 = 0; endpointValueNdx0 < numEndpointValues; endpointValueNdx0++)
						for (int endpointValueNdx1 = 0; endpointValueNdx1 < numEndpointValues; endpointValueNdx1++)
						{
							const int endpointValue0 = endpointValueNdx0 * endpointISERangeMax / (numEndpointValues-1);
							const int endpointValue1 = endpointValueNdx1 * endpointISERangeMax / (numEndpointValues-1);

							iseInputs.endpoint.value.plain[endpointPartNdx0] = endpointValue0;
							iseInputs.endpoint.value.plain[endpointPartNdx1] = endpointValue1;

							generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
						}
					}
				}
			}

			break;
		}

		case BLOCK_TEST_TYPE_ENDPOINT_ISE:
		// Similar to BLOCK_TEST_TYPE_WEIGHT_ISE, see above.
		{
			static const deUint32 endpointRangeMaximums[] = { 5, 9, 11, 19, 23, 39, 47, 79, 95, 159, 191 };

			for (int endpointRangeNdx = 0; endpointRangeNdx < DE_LENGTH_OF_ARRAY(endpointRangeMaximums); endpointRangeNdx++)
			{
				bool validCaseGenerated = false;

				for (int numPartitions = 1;			!validCaseGenerated && numPartitions <= 4;														numPartitions++)
				for (int isDual = 0;				!validCaseGenerated && isDual <= 1;																isDual++)
				for (int weightISEParamsNdx = 0;	!validCaseGenerated && weightISEParamsNdx < DE_LENGTH_OF_ARRAY(s_weightISEParamsCandidates);	weightISEParamsNdx++)
				for (int weightGridWidth = 2;		!validCaseGenerated && weightGridWidth <= 12;													weightGridWidth++)
				for (int weightGridHeight = 2;		!validCaseGenerated && weightGridHeight <= 12;													weightGridHeight++)
				{
					NormalBlockParams blockParams;
					blockParams.weightGridWidth				= weightGridWidth;
					blockParams.weightGridHeight			= weightGridHeight;
					blockParams.weightISEParams				= s_weightISEParamsCandidates[weightISEParamsNdx];
					blockParams.isDualPlane					= isDual != 0;
					blockParams.ccs							= 0;
					blockParams.numPartitions				= numPartitions;
					blockParams.isMultiPartSingleCemMode	= true;
					blockParams.colorEndpointModes[0]		= 12;
					blockParams.partitionSeed				= 634;

					if (isValidBlockParams(blockParams, blockSize.x(), blockSize.y()))
					{
						const ISEParams endpointISEParams = computeMaximumRangeISEParams(computeNumBitsForColorEndpoints(blockParams),
																						 computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, true));

						if (computeISERangeMax(endpointISEParams) == endpointRangeMaximums[endpointRangeNdx])
						{
							validCaseGenerated = true;

							const int numColorEndpoints		= computeNumColorEndpointValues(&blockParams.colorEndpointModes[0], numPartitions, blockParams.isMultiPartSingleCemMode);
							const int numValuesInISEBlock	= endpointISEParams.mode == ISEMODE_TRIT ? 5 : endpointISEParams.mode == ISEMODE_QUINT ? 3 : 1;

							{
								const int				numColorEndpointValues	= (int)computeISERangeMax(endpointISEParams) + 1;
								const int				numBlocks				= deDivRoundUp32(numColorEndpointValues, numColorEndpoints);
								NormalBlockISEInputs	iseInputs				= generateDefaultISEInputs(blockParams);
								iseInputs.endpoint.isGivenInBlockForm = false;

								for (int offset = 0;	offset < numValuesInISEBlock;	offset++)
								for (int blockNdx = 0;	blockNdx < numBlocks;			blockNdx++)
								{
									for (int endpointNdx = 0; endpointNdx < numColorEndpoints; endpointNdx++)
										iseInputs.endpoint.value.plain[endpointNdx] = (blockNdx*numColorEndpoints + endpointNdx + offset) % numColorEndpointValues;

									generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
								}
							}

							if (endpointISEParams.mode == ISEMODE_TRIT || endpointISEParams.mode == ISEMODE_QUINT)
							{
								NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
								iseInputs.endpoint.isGivenInBlockForm = true;

								const int numTQValues			= 1 << (endpointISEParams.mode == ISEMODE_TRIT ? 8 : 7);
								const int numISEBlocksPerBlock	= deDivRoundUp32(numColorEndpoints, numValuesInISEBlock);
								const int numBlocks				= deDivRoundUp32(numTQValues, numISEBlocksPerBlock);

								for (int offset = 0;	offset < numValuesInISEBlock;	offset++)
								for (int blockNdx = 0;	blockNdx < numBlocks;			blockNdx++)
								{
									for (int iseBlockNdx = 0; iseBlockNdx < numISEBlocksPerBlock; iseBlockNdx++)
									{
										for (int i = 0; i < numValuesInISEBlock; i++)
											iseInputs.endpoint.value.block[iseBlockNdx].bitValues[i] = 0;
										iseInputs.endpoint.value.block[iseBlockNdx].tOrQValue = (blockNdx*numISEBlocksPerBlock + iseBlockNdx + offset) % numTQValues;
									}

									generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), iseInputs).pushBytesToVector(dst);
								}
							}
						}
					}
				}

				DE_ASSERT(validCaseGenerated);
			}

			break;
		}

		case BLOCK_TEST_TYPE_CCS:
		// For all partition counts, test all values of the CCS (color component selector).
		{
			for (int		numPartitions = 1;		numPartitions <= 3;		numPartitions++)
			for (deUint32	ccs = 0;				ccs < 4;				ccs++)
			{
				NormalBlockParams blockParams;
				blockParams.weightGridWidth				= 3;
				blockParams.weightGridHeight			= 3;
				blockParams.weightISEParams				= ISEParams(ISEMODE_PLAIN_BIT, 2);
				blockParams.isDualPlane					= true;
				blockParams.ccs							= ccs;
				blockParams.numPartitions				= numPartitions;
				blockParams.isMultiPartSingleCemMode	= true;
				blockParams.colorEndpointModes[0]		= 8;
				blockParams.partitionSeed				= 634;

				generateNormalBlock(blockParams, blockSize.x(), blockSize.y(), generateDefaultISEInputs(blockParams)).pushBytesToVector(dst);
			}

			break;
		}

		case BLOCK_TEST_TYPE_RANDOM:
		// Generate a number of random (including invalid) blocks.
		{
			const int		numBlocks	= 16384;
			const deUint32	seed		= 1;

			dst.resize(numBlocks*BLOCK_SIZE_BYTES);

			generateRandomBlocks(&dst[0], numBlocks, format, seed);

			break;
		}

		default:
			DE_ASSERT(false);
	}
}

void generateRandomBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, deUint32 seed)
{
	const IVec3		blockSize			= getBlockPixelSize(format);
	de::Random		rnd					(seed);
	size_t			numBlocksGenerated	= 0;

	DE_ASSERT(isAstcFormat(format));
	DE_ASSERT(blockSize.z() == 1);

	for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
	{
		deUint8* const	curBlockPtr		= dst + numBlocksGenerated*BLOCK_SIZE_BYTES;

		generateRandomBlock(curBlockPtr, blockSize, rnd);
	}
}

void generateRandomValidBlocks (deUint8* dst, size_t numBlocks, CompressedTexFormat format, TexDecompressionParams::AstcMode mode, deUint32 seed)
{
	const IVec3		blockSize			= getBlockPixelSize(format);
	de::Random		rnd					(seed);
	size_t			numBlocksGenerated	= 0;

	DE_ASSERT(isAstcFormat(format));
	DE_ASSERT(blockSize.z() == 1);

	for (numBlocksGenerated = 0; numBlocksGenerated < numBlocks; numBlocksGenerated++)
	{
		deUint8* const	curBlockPtr		= dst + numBlocksGenerated*BLOCK_SIZE_BYTES;

		do
		{
			generateRandomBlock(curBlockPtr, blockSize, rnd);
		} while (!isValidBlock(curBlockPtr, format, mode));
	}
}

// Generate a number of trivial dummy blocks to fill unneeded space in a texture.
void generateDummyVoidExtentBlocks (deUint8* dst, size_t numBlocks)
{
	AssignBlock128 block = generateVoidExtentBlock(VoidExtentParams(false, 0, 0, 0, 0));
	for (size_t ndx = 0; ndx < numBlocks; ndx++)
		block.assignToMemory(&dst[ndx * BLOCK_SIZE_BYTES]);
}

void generateDummyNormalBlocks (deUint8* dst, size_t numBlocks, int blockWidth, int blockHeight)
{
	NormalBlockParams blockParams;

	blockParams.weightGridWidth			= 3;
	blockParams.weightGridHeight		= 3;
	blockParams.weightISEParams			= ISEParams(ISEMODE_PLAIN_BIT, 5);
	blockParams.isDualPlane				= false;
	blockParams.numPartitions			= 1;
	blockParams.colorEndpointModes[0]	= 8;

	NormalBlockISEInputs iseInputs = generateDefaultISEInputs(blockParams);
	iseInputs.weight.isGivenInBlockForm = false;

	const int numWeights		= computeNumWeights(blockParams);
	const int weightRangeMax	= computeISERangeMax(blockParams.weightISEParams);

	for (size_t blockNdx = 0; blockNdx < numBlocks; blockNdx++)
	{
		for (int weightNdx = 0; weightNdx < numWeights; weightNdx++)
			iseInputs.weight.value.plain[weightNdx] = (deUint32)((blockNdx*numWeights + weightNdx) * weightRangeMax / (numBlocks*numWeights-1));

		generateNormalBlock(blockParams, blockWidth, blockHeight, iseInputs).assignToMemory(dst + blockNdx*BLOCK_SIZE_BYTES);
	}
}

bool isValidBlock (const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
{
	const tcu::IVec3		blockPixelSize	= getBlockPixelSize(format);
	const bool				isSRGB			= isAstcSRGBFormat(format);
	const bool				isLDR			= isSRGB || mode == TexDecompressionParams::ASTCMODE_LDR;

	// sRGB is not supported in HDR mode
	DE_ASSERT(!(mode == TexDecompressionParams::ASTCMODE_HDR && isSRGB));

	union
	{
		deUint8		sRGB[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
		float		linear[MAX_BLOCK_WIDTH*MAX_BLOCK_HEIGHT*4];
	} tmpBuffer;
	const Block128			blockData		(data);
	const DecompressResult	result			= decompressBlock((isSRGB ? (void*)&tmpBuffer.sRGB[0] : (void*)&tmpBuffer.linear[0]),
															  blockData, blockPixelSize.x(), blockPixelSize.y(), isSRGB, isLDR);

	return result == DECOMPRESS_RESULT_VALID_BLOCK;
}

void decompress (const PixelBufferAccess& dst, const deUint8* data, CompressedTexFormat format, TexDecompressionParams::AstcMode mode)
{
	const bool			isSRGBFormat	= isAstcSRGBFormat(format);

#if defined(DE_DEBUG)
	const tcu::IVec3	blockPixelSize	= getBlockPixelSize(format);

	DE_ASSERT(dst.getWidth()	== blockPixelSize.x() &&
			  dst.getHeight()	== blockPixelSize.y() &&
			  dst.getDepth()	== blockPixelSize.z());
	DE_ASSERT(mode == TexDecompressionParams::ASTCMODE_LDR || mode == TexDecompressionParams::ASTCMODE_HDR);
#endif

	// sRGB is not supported in HDR mode
	DE_ASSERT(!(mode == TexDecompressionParams::ASTCMODE_HDR && isSRGBFormat));

	decompress(dst, data, isSRGBFormat, isSRGBFormat || mode == TexDecompressionParams::ASTCMODE_LDR);
}

const char* getBlockTestTypeName (BlockTestType testType)
{
	switch (testType)
	{
		case BLOCK_TEST_TYPE_VOID_EXTENT_LDR:			return "void_extent_ldr";
		case BLOCK_TEST_TYPE_VOID_EXTENT_HDR:			return "void_extent_hdr";
		case BLOCK_TEST_TYPE_WEIGHT_GRID:				return "weight_grid";
		case BLOCK_TEST_TYPE_WEIGHT_ISE:				return "weight_ise";
		case BLOCK_TEST_TYPE_CEMS:						return "color_endpoint_modes";
		case BLOCK_TEST_TYPE_PARTITION_SEED:			return "partition_pattern_index";
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR:		return "endpoint_value_ldr";
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:	return "endpoint_value_hdr_cem_not_15";
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:		return "endpoint_value_hdr_cem_15";
		case BLOCK_TEST_TYPE_ENDPOINT_ISE:				return "endpoint_ise";
		case BLOCK_TEST_TYPE_CCS:						return "color_component_selector";
		case BLOCK_TEST_TYPE_RANDOM:					return "random";
		default:
			DE_ASSERT(false);
			return DE_NULL;
	}
}

const char* getBlockTestTypeDescription (BlockTestType testType)
{
	switch (testType)
	{
		case BLOCK_TEST_TYPE_VOID_EXTENT_LDR:			return "Test void extent block, LDR mode";
		case BLOCK_TEST_TYPE_VOID_EXTENT_HDR:			return "Test void extent block, HDR mode";
		case BLOCK_TEST_TYPE_WEIGHT_GRID:				return "Test combinations of plane count, weight integer sequence encoding parameters, and weight grid size";
		case BLOCK_TEST_TYPE_WEIGHT_ISE:				return "Test different integer sequence encoding block values for weight grid";
		case BLOCK_TEST_TYPE_CEMS:						return "Test different color endpoint mode combinations, combined with different plane and partition counts";
		case BLOCK_TEST_TYPE_PARTITION_SEED:			return "Test different partition pattern indices";
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_LDR:		return "Test various combinations of each pair of color endpoint values, for each LDR color endpoint mode";
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:	return "Test various combinations of each pair of color endpoint values, for each HDR color endpoint mode other than mode 15";
		case BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:		return "Test various combinations of each pair of color endpoint values, HDR color endpoint mode 15";
		case BLOCK_TEST_TYPE_ENDPOINT_ISE:				return "Test different integer sequence encoding block values for color endpoints";
		case BLOCK_TEST_TYPE_CCS:						return "Test color component selector, for different partition counts";
		case BLOCK_TEST_TYPE_RANDOM:					return "Random block test";
		default:
			DE_ASSERT(false);
			return DE_NULL;
	}
}

bool isBlockTestTypeHDROnly (BlockTestType testType)
{
	return testType == BLOCK_TEST_TYPE_VOID_EXTENT_HDR			||
		   testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15	||
		   testType == BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15;
}

Vec4 getBlockTestTypeColorScale (BlockTestType testType)
{
	switch (testType)
	{
		case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR:			return Vec4(0.5f/65504.0f);
		case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_NO_15:	return Vec4(1.0f/65504.0f, 1.0f/65504.0f, 1.0f/65504.0f, 1.0f);
		case tcu::astc::BLOCK_TEST_TYPE_ENDPOINT_VALUE_HDR_15:		return Vec4(1.0f/65504.0f);
		default:													return Vec4(1.0f);
	}
}

Vec4 getBlockTestTypeColorBias (BlockTestType testType)
{
	switch (testType)
	{
		case tcu::astc::BLOCK_TEST_TYPE_VOID_EXTENT_HDR:	return Vec4(0.5f);
		default:											return Vec4(0.0f);
	}
}

} // astc
} // tcu