//! Low Discrepency Sequences.

use crate::geometry::*;
use crate::pbrt::*;
use crate::rng::*;
use crate::sobol_matrices::*;

/// Size of the prime numbers list.
pub const PRIME_TABLE_SIZE: usize = 1000;

/// Prime numbers.
#[rustfmt::skip]
pub const PRIMES: [usize; PRIME_TABLE_SIZE] = [
       2,    3,    5,    7,   11,   13,   17,   19,   23,   29,   31,   37,
      41,   43,   47,   53,   59,   61,   67,   71,   73,   79,   83,   89,
      97,  101,  103,  107,  109,  113,  127,  131,  137,  139,  149,  151,
     157,  163,  167,  173,  179,  181,  191,  193,  197,  199,  211,  223,
     227,  229,  233,  239,  241,  251,  257,  263,  269,  271,  277,  281,
     283,  293,  307,  311,  313,  317,  331,  337,  347,  349,  353,  359,
     367,  373,  379,  383,  389,  397,  401,  409,  419,  421,  431,  433,
     439,  443,  449,  457,  461,  463,  467,  479,  487,  491,  499,  503,
     509,  521,  523,  541,  547,  557,  563,  569,  571,  577,  587,  593,
     599,  601,  607,  613,  617,  619,  631,  641,  643,  647,  653,  659,
     661,  673,  677,  683,  691,  701,  709,  719,  727,  733,  739,  743,
     751,  757,  761,  769,  773,  787,  797,  809,  811,  821,  823,  827,
     829,  839,  853,  857,  859,  863,  877,  881,  883,  887,  907,  911,
     919,  929,  937,  941,  947,  953,  967,  971,  977,  983,  991,  997,
    1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069,
    1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163,
    1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249,
    1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321,
    1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439,
    1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511,
    1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601,
    1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 
    1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 
    1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 
    1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 
    1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 
    2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143,
    2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267,
    2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 
    2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 
    2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 
    2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 
    2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 
    2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 
    2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 
    2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 
    3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 
    3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 
    3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 
    3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 
    3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527,
    3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607,
    3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 
    3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 
    3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 
    3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 
    4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 
    4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 
    4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 
    4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 
    4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513,
    4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621,
    4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721,
    4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813,
    4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937,
    4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011,
    5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113,
    5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233,
    5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351,
    5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443,
    5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531,
    5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653,
    5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743,
    5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849,
    5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939,
    5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073,
    6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173,
    6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271,
    6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359,
    6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473,
    6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581,
    6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701,
    6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803,
    6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907,
    6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997,
    7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121,
    7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229,
    7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349,
    7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487,
    7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561,
    7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669,
    7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757,
    7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879,
    7883, 7901, 7907, 7919, 
];

/// Stores the running sum of prime numbers.
#[rustfmt::skip]
pub const PRIME_SUMS: [usize; PRIME_TABLE_SIZE] = [
          0,       2,       5,      10,      17,      28,      41,      58,
         77,     100,     129,     160,     197,     238,     281,     328,
        381,     440,     501,     568,     639,     712,     791,     874,
        963,    1060,    1161,    1264,    1371,    1480,    1593,    1720,
       1851,    1988,    2127,    2276,    2427,    2584,    2747,    2914,
       3087,    3266,    3447,    3638,    3831,    4028,    4227,    4438,
       4661,    4888,    5117,    5350,    5589,    5830,    6081,    6338,
       6601,    6870,    7141,    7418,    7699,    7982,    8275,    8582,
       8893,    9206,    9523,    9854,   10191,   10538,   10887,   11240,
      11599,   11966,   12339,   12718,   13101,   13490,   13887,   14288,
      14697,   15116,   15537,   15968,   16401,   16840,   17283,   17732,
      18189,   18650,   19113,   19580,   20059,   20546,   21037,   21536,
      22039,   22548,   23069,   23592,   24133,   24680,   25237,   25800,
      26369,   26940,   27517,   28104,   28697,   29296,   29897,   30504,
      31117,   31734,   32353,   32984,   33625,   34268,   34915,   35568,
      36227,   36888,   37561,   38238,   38921,   39612,   40313,   41022,
      41741,   42468,   43201,   43940,   44683,   45434,   46191,   46952,
      47721,   48494,   49281,   50078,   50887,   51698,   52519,   53342,
      54169,   54998,   55837,   56690,   57547,   58406,   59269,   60146,
      61027,   61910,   62797,   63704,   64615,   65534,   66463,   67400,
      68341,   69288,   70241,   71208,   72179,   73156,   74139,   75130,
      76127,   77136,   78149,   79168,   80189,   81220,   82253,   83292,
      84341,   85392,   86453,   87516,   88585,   89672,   90763,   91856,
      92953,   94056,   95165,   96282,   97405,   98534,   99685,  100838,
     102001,  103172,  104353,  105540,  106733,  107934,  109147,  110364,
     111587,  112816,  114047,  115284,  116533,  117792,  119069,  120348,
     121631,  122920,  124211,  125508,  126809,  128112,  129419,  130738,
     132059,  133386,  134747,  136114,  137487,  138868,  140267,  141676,
     143099,  144526,  145955,  147388,  148827,  150274,  151725,  153178,
     154637,  156108,  157589,  159072,  160559,  162048,  163541,  165040,
     166551,  168074,  169605,  171148,  172697,  174250,  175809,  177376,
     178947,  180526,  182109,  183706,  185307,  186914,  188523,  190136,
     191755,  193376,  195003,  196640,  198297,  199960,  201627,  203296,
     204989,  206686,  208385,  210094,  211815,  213538,  215271,  217012,
     218759,  220512,  222271,  224048,  225831,  227618,  229407,  231208,
     233019,  234842,  236673,  238520,  240381,  242248,  244119,  245992,
     247869,  249748,  251637,  253538,  255445,  257358,  259289,  261222,
     263171,  265122,  267095,  269074,  271061,  273054,  275051,  277050,
     279053,  281064,  283081,  285108,  287137,  289176,  291229,  293292,
     295361,  297442,  299525,  301612,  303701,  305800,  307911,  310024,
     312153,  314284,  316421,  318562,  320705,  322858,  325019,  327198,
     329401,  331608,  333821,  336042,  338279,  340518,  342761,  345012,
     347279,  349548,  351821,  354102,  356389,  358682,  360979,  363288,
     365599,  367932,  370271,  372612,  374959,  377310,  379667,  382038,
     384415,  386796,  389179,  391568,  393961,  396360,  398771,  401188,
     403611,  406048,  408489,  410936,  413395,  415862,  418335,  420812,
     423315,  425836,  428367,  430906,  433449,  435998,  438549,  441106,
     443685,  446276,  448869,  451478,  454095,  456716,  459349,  461996,
     464653,  467312,  469975,  472646,  475323,  478006,  480693,  483382,
     486075,  488774,  491481,  494192,  496905,  499624,  502353,  505084,
     507825,  510574,  513327,  516094,  518871,  521660,  524451,  527248,
     530049,  532852,  535671,  538504,  541341,  544184,  547035,  549892,
     552753,  555632,  558519,  561416,  564319,  567228,  570145,  573072,
     576011,  578964,  581921,  584884,  587853,  590824,  593823,  596824,
     599835,  602854,  605877,  608914,  611955,  615004,  618065,  621132,
     624211,  627294,  630383,  633492,  636611,  639732,  642869,  646032,
     649199,  652368,  655549,  658736,  661927,  665130,  668339,  671556,
     674777,  678006,  681257,  684510,  687767,  691026,  694297,  697596,
     700897,  704204,  707517,  710836,  714159,  717488,  720819,  724162,
     727509,  730868,  734229,  737600,  740973,  744362,  747753,  751160,
     754573,  758006,  761455,  764912,  768373,  771836,  775303,  778772,
     782263,  785762,  789273,  792790,  796317,  799846,  803379,  806918,
     810459,  814006,  817563,  821122,  824693,  828274,  831857,  835450,
     839057,  842670,  846287,  849910,  853541,  857178,  860821,  864480,
     868151,  871824,  875501,  879192,  882889,  886590,  890299,  894018,
     897745,  901478,  905217,  908978,  912745,  916514,  920293,  924086,
     927883,  931686,  935507,  939330,  943163,  947010,  950861,  954714,
     958577,  962454,  966335,  970224,  974131,  978042,  981959,  985878,
     989801,  993730,  997661, 1001604, 1005551, 1009518, 1013507, 1017508,
    1021511, 1025518, 1029531, 1033550, 1037571, 1041598, 1045647, 1049698,
    1053755, 1057828, 1061907, 1065998, 1070091, 1074190, 1078301, 1082428,
    1086557, 1090690, 1094829, 1098982, 1103139, 1107298, 1111475, 1115676,
    1119887, 1124104, 1128323, 1132552, 1136783, 1141024, 1145267, 1149520,
    1153779, 1158040, 1162311, 1166584, 1170867, 1175156, 1179453, 1183780,
    1188117, 1192456, 1196805, 1201162, 1205525, 1209898, 1214289, 1218686,
    1223095, 1227516, 1231939, 1236380, 1240827, 1245278, 1249735, 1254198,
    1258679, 1263162, 1267655, 1272162, 1276675, 1281192, 1285711, 1290234,
    1294781, 1299330, 1303891, 1308458, 1313041, 1317632, 1322229, 1326832,
    1331453, 1336090, 1340729, 1345372, 1350021, 1354672, 1359329, 1363992,
    1368665, 1373344, 1378035, 1382738, 1387459, 1392182, 1396911, 1401644,
    1406395, 1411154, 1415937, 1420724, 1425513, 1430306, 1435105, 1439906,
    1444719, 1449536, 1454367, 1459228, 1464099, 1468976, 1473865, 1478768,
    1483677, 1488596, 1493527, 1498460, 1503397, 1508340, 1513291, 1518248,
    1523215, 1528184, 1533157, 1538144, 1543137, 1548136, 1553139, 1558148,
    1563159, 1568180, 1573203, 1578242, 1583293, 1588352, 1593429, 1598510,
    1603597, 1608696, 1613797, 1618904, 1624017, 1629136, 1634283, 1639436,
    1644603, 1649774, 1654953, 1660142, 1665339, 1670548, 1675775, 1681006,
    1686239, 1691476, 1696737, 1702010, 1707289, 1712570, 1717867, 1723170,
    1728479, 1733802, 1739135, 1744482, 1749833, 1755214, 1760601, 1765994,
    1771393, 1776800, 1782213, 1787630, 1793049, 1798480, 1803917, 1809358,
    1814801, 1820250, 1825721, 1831198, 1836677, 1842160, 1847661, 1853164,
    1858671, 1864190, 1869711, 1875238, 1880769, 1886326, 1891889, 1897458,
    1903031, 1908612, 1914203, 1919826, 1925465, 1931106, 1936753, 1942404,
    1948057, 1953714, 1959373, 1965042, 1970725, 1976414, 1982107, 1987808,
    1993519, 1999236, 2004973, 2010714, 2016457, 2022206, 2027985, 2033768,
    2039559, 2045360, 2051167, 2056980, 2062801, 2068628, 2074467, 2080310,
    2086159, 2092010, 2097867, 2103728, 2109595, 2115464, 2121343, 2127224,
    2133121, 2139024, 2144947, 2150874, 2156813, 2162766, 2168747, 2174734,
    2180741, 2186752, 2192781, 2198818, 2204861, 2210908, 2216961, 2223028,
    2229101, 2235180, 2241269, 2247360, 2253461, 2259574, 2265695, 2271826,
    2277959, 2284102, 2290253, 2296416, 2302589, 2308786, 2314985, 2321188,
    2327399, 2333616, 2339837, 2346066, 2352313, 2358570, 2364833, 2371102,
    2377373, 2383650, 2389937, 2396236, 2402537, 2408848, 2415165, 2421488,
    2427817, 2434154, 2440497, 2446850, 2453209, 2459570, 2465937, 2472310,
    2478689, 2485078, 2491475, 2497896, 2504323, 2510772, 2517223, 2523692,
    2530165, 2536646, 2543137, 2549658, 2556187, 2562734, 2569285, 2575838,
    2582401, 2588970, 2595541, 2602118, 2608699, 2615298, 2621905, 2628524,
    2635161, 2641814, 2648473, 2655134, 2661807, 2668486, 2675175, 2681866,
    2688567, 2695270, 2701979, 2708698, 2715431, 2722168, 2728929, 2735692,
    2742471, 2749252, 2756043, 2762836, 2769639, 2776462, 2783289, 2790118,
    2796951, 2803792, 2810649, 2817512, 2824381, 2831252, 2838135, 2845034,
    2851941, 2858852, 2865769, 2872716, 2879665, 2886624, 2893585, 2900552,
    2907523, 2914500, 2921483, 2928474, 2935471, 2942472, 2949485, 2956504,
    2963531, 2970570, 2977613, 2984670, 2991739, 2998818, 3005921, 3013030,
    3020151, 3027278, 3034407, 3041558, 3048717, 3055894, 3063081, 3070274,
    3077481, 3084692, 3091905, 3099124, 3106353, 3113590, 3120833, 3128080,
    3135333, 3142616, 3149913, 3157220, 3164529, 3171850, 3179181, 3186514,
    3193863, 3201214, 3208583, 3215976, 3223387, 3230804, 3238237, 3245688,
    3253145, 3260604, 3268081, 3275562, 3283049, 3290538, 3298037, 3305544,
    3313061, 3320584, 3328113, 3335650, 3343191, 3350738, 3358287, 3365846,
    3373407, 3380980, 3388557, 3396140, 3403729, 3411320, 3418923, 3426530,
    3434151, 3441790, 3449433, 3457082, 3464751, 3472424, 3480105, 3487792,
    3495483, 3503182, 3510885, 3518602, 3526325, 3534052, 3541793, 3549546,
    3557303, 3565062, 3572851, 3580644, 3588461, 3596284, 3604113, 3611954,
    3619807, 3627674, 3635547, 3643424, 3651303, 3659186, 3667087, 3674994, 
];

/// To convert matrices from the "(t, m, s)-Nets and Maximized Minimum
/// Distance" paper:
/// * Recall that each uint32_t represents a column of the matrix, so copy 
///   columns of bits.
/// * Reverse the bits in each column (so we don't need to reverse the result 
///   after the matrix multiply.)
#[rustfmt::skip]
pub const C_MAX_MIN_DIST: [[u32; 32]; 17] = [
    [
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x80000000,
    ],
    [
        0xc0000000, 0x80000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0xa0000000, 0x40000000, 0x80000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0xc0000000, 0x50000000, 0x20000000, 0x30000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x88000000, 0x58000000, 0x20000000, 0x40000000, 0x80000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0xe0000000, 0x60000000, 0x28000000, 0x10000000, 0x18000000, 0x04000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x82000000, 0x44000000, 0x2c000000, 0x10000000, 0x20000000, 0x40000000,
        0x80000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x70000000, 0x30000000, 0x14000000, 0x08000000, 0x0c000000,
        0x02000000, 0x01000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0xc0000000, 0x41000000, 0x22000000, 0x16000000, 0x08000000, 0x10000000,
        0x20000000, 0x40800000, 0x80000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x40000000, 0x20000000, 0x18000000, 0x08000000, 0x1c000000,
        0x1e000000, 0x03000000, 0x00800000, 0x00400000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x60400000, 0x20800000, 0x11000000, 0x0b000000, 0x04000000,
        0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x00200000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x40000000, 0x20000000, 0x1c000000, 0x0c000000, 0x05000000,
        0x02000000, 0x03000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x40000000, 0x30200000, 0x10400000, 0x08800000, 0x05800000,
        0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x00100000,
        0x00080000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x0e000000, 0x06000000,
        0x02800000, 0x01000000, 0x01800000, 0x00400000, 0x00200000, 0x00100000,
        0x00080000, 0x00040000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x40000000, 0x20000000, 0x18100000, 0x08200000, 0x04400000,
        0x02c00000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
        0x00080000, 0x00040000, 0x00020000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
    [
        0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x07000000,
        0x03000000, 0x01400000, 0x00800000, 0x00c00000, 0x00200000, 0x00100000,
        0x00080000, 0x00040000, 0x00020000, 0x00010000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
        0x00000000, 0x00000000,
    ],
];

/// Reverse the bits of a 32-bit integer.
///
/// * `n` - 32-bit integer.
#[inline]
pub fn reverse_bits_32(n: u32) -> u32 {
    let mut n = (n << 16) | (n >> 16);
    n = ((n & 0x00ff00ff) << 8) | ((n & 0xff00ff00) >> 8);
    n = ((n & 0x0f0f0f0f) << 4) | ((n & 0xf0f0f0f0) >> 4);
    n = ((n & 0x33333333) << 2) | ((n & 0xcccccccc) >> 2);
    n = ((n & 0x55555555) << 1) | ((n & 0xaaaaaaaa) >> 1);
    n
}

/// Reverse the bits of a 64-bit integer.
///
/// * `n` - 64-bit integer.
#[inline]
fn reverse_bits_64(n: u64) -> u64 {
    let n0 = reverse_bits_32(n as u32) as u64;
    let n1 = reverse_bits_32((n >> 32) as u32) as u64;
    (n0 << 32) | n1
}

/// Returns the radical inverse of an integer in given prime base.
///
/// * `base` - The base is a prime number.
/// * `a`    - The integer value.
fn radical_inverse_specialized(base: u16, a: u64) -> Float {
    let inv_base = 1.0 / base as Float;
    let base = base as u64;

    let mut reversed_digits = 0_u64;
    let mut inv_base_n = 1.0;

    let mut a = a; // Local copy that we can modify.
    while a != 0 {
        let next = a / base;
        let digit = a - next * base;
        reversed_digits = reversed_digits * base + digit;
        inv_base_n *= inv_base;
        a = next;
    }

    let r = reversed_digits as Float * inv_base_n;
    debug_assert!(r < 1.00001);
    min(r, ONE_MINUS_EPSILON)
}

/// Returns the scrambled radical inverse of an integer in given prime base by
/// putting each digit through a permutation table for the given base.
///
/// * `base` - The base is a prime number.
/// * `a`    - The integer value.
/// * `perm` - Permutation table for each digit of base `base_index`.
fn scrambled_radical_inverse_specialized(base: u16, a: u64, perm: &[u16]) -> Float {
    let inv_base = 1.0 / base as Float;
    let base = base as u64;
    let mut reversed_digits = 0_u64;
    let mut inv_base_n = 1.0;

    let mut a = a; // Local copy that we can modify.
    while a != 0 {
        let next = a / base;
        let digit = (a - next * base) as usize;
        assert!((perm[digit] as u64) < base);
        reversed_digits = reversed_digits * base + perm[digit] as u64;
        inv_base_n *= inv_base;
        a = next;
    }

    let r =
        inv_base_n * (reversed_digits as Float + inv_base * perm[0] as Float / (1.0 - inv_base));
    debug_assert!(r < 1.00001);
    min(r, ONE_MINUS_EPSILON)
}

/// Returns the radical inverse of an integer in given base.
///
/// * `base_index` - The base.
/// * `a`          - The integer value.
pub fn radical_inverse(base_index: u16, a: u64) -> Float {
    match base_index {
        0 => {
            // Compute base-2 radical inverse.
            // hexf32!("0x1.0p-64") as Float = 5.4210108624275222e-20
            reverse_bits_64(a) as Float * hexf32!("0x1.0p-64") as Float
        }

        _ => radical_inverse_specialized(base_index_to_prime(base_index), a),
    }
}

/// Returns the scrambled radical inverse of an integer in given base by putting
/// each digit through a permutation table for the given base.
///
/// * `base_index` - The base.
/// * `a`          - The integer value.
/// * `perm`       - Permutation table for each digit of base `base_index`.
pub fn scrambled_radical_inverse(base_index: u16, a: u64, perm: &[u16]) -> Float {
    scrambled_radical_inverse_specialized(base_index_to_prime(base_index), a, perm)
}

/// Maps the integer base index to a prime number that can be used as the `base`
/// for the radical inverse functions.
///
/// * `base_index` - A 16-bit integer in [0, 1023].
fn base_index_to_prime(base_index: u16) -> u16 {
    match base_index {
        0 => 2,
        1 => 3,
        2 => 5,
        3 => 7,
        4 => 11,
        5 => 13,
        6 => 17,
        7 => 19,
        8 => 23,
        9 => 29,
        10 => 31,
        11 => 37,
        12 => 41,
        13 => 43,
        14 => 47,
        15 => 53,
        16 => 59,
        17 => 61,
        18 => 67,
        19 => 71,
        20 => 73,
        21 => 79,
        22 => 83,
        23 => 89,
        24 => 97,
        25 => 101,
        26 => 103,
        27 => 107,
        28 => 109,
        29 => 113,
        30 => 127,
        31 => 131,
        32 => 137,
        33 => 139,
        34 => 149,
        35 => 151,
        36 => 157,
        37 => 163,
        38 => 167,
        39 => 173,
        40 => 179,
        41 => 181,
        42 => 191,
        43 => 193,
        44 => 197,
        45 => 199,
        46 => 211,
        47 => 223,
        48 => 227,
        49 => 229,
        50 => 233,
        51 => 239,
        52 => 241,
        53 => 251,
        54 => 257,
        55 => 263,
        56 => 269,
        57 => 271,
        58 => 277,
        59 => 281,
        60 => 283,
        61 => 293,
        62 => 307,
        63 => 311,
        64 => 313,
        65 => 317,
        66 => 331,
        67 => 337,
        68 => 347,
        69 => 349,
        70 => 353,
        71 => 359,
        72 => 367,
        73 => 373,
        74 => 379,
        75 => 383,
        76 => 389,
        77 => 397,
        78 => 401,
        79 => 409,
        80 => 419,
        81 => 421,
        82 => 431,
        83 => 433,
        84 => 439,
        85 => 443,
        86 => 449,
        87 => 457,
        88 => 461,
        89 => 463,
        90 => 467,
        91 => 479,
        92 => 487,
        93 => 491,
        94 => 499,
        95 => 503,
        96 => 509,
        97 => 521,
        98 => 523,
        99 => 541,
        100 => 547,
        101 => 557,
        102 => 563,
        103 => 569,
        104 => 571,
        105 => 577,
        106 => 587,
        107 => 593,
        108 => 599,
        109 => 601,
        110 => 607,
        111 => 613,
        112 => 617,
        113 => 619,
        114 => 631,
        115 => 641,
        116 => 643,
        117 => 647,
        118 => 653,
        119 => 659,
        120 => 661,
        121 => 673,
        122 => 677,
        123 => 683,
        124 => 691,
        125 => 701,
        126 => 709,
        127 => 719,
        128 => 727,
        129 => 733,
        130 => 739,
        131 => 743,
        132 => 751,
        133 => 757,
        134 => 761,
        135 => 769,
        136 => 773,
        137 => 787,
        138 => 797,
        139 => 809,
        140 => 811,
        141 => 821,
        142 => 823,
        143 => 827,
        144 => 829,
        145 => 839,
        146 => 853,
        147 => 857,
        148 => 859,
        149 => 863,
        150 => 877,
        151 => 881,
        152 => 883,
        153 => 887,
        154 => 907,
        155 => 911,
        156 => 919,
        157 => 929,
        158 => 937,
        159 => 941,
        160 => 947,
        161 => 953,
        162 => 967,
        163 => 971,
        164 => 977,
        165 => 983,
        166 => 991,
        167 => 997,
        168 => 1009,
        169 => 1013,
        170 => 1019,
        171 => 1021,
        172 => 1031,
        173 => 1033,
        174 => 1039,
        175 => 1049,
        176 => 1051,
        177 => 1061,
        178 => 1063,
        179 => 1069,
        180 => 1087,
        181 => 1091,
        182 => 1093,
        183 => 1097,
        184 => 1103,
        185 => 1109,
        186 => 1117,
        187 => 1123,
        188 => 1129,
        189 => 1151,
        190 => 1153,
        191 => 1163,
        192 => 1171,
        193 => 1181,
        194 => 1187,
        195 => 1193,
        196 => 1201,
        197 => 1213,
        198 => 1217,
        199 => 1223,
        200 => 1229,
        201 => 1231,
        202 => 1237,
        203 => 1249,
        204 => 1259,
        205 => 1277,
        206 => 1279,
        207 => 1283,
        208 => 1289,
        209 => 1291,
        210 => 1297,
        211 => 1301,
        212 => 1303,
        213 => 1307,
        214 => 1319,
        215 => 1321,
        216 => 1327,
        217 => 1361,
        218 => 1367,
        219 => 1373,
        220 => 1381,
        221 => 1399,
        222 => 1409,
        223 => 1423,
        224 => 1427,
        225 => 1429,
        226 => 1433,
        227 => 1439,
        228 => 1447,
        229 => 1451,
        230 => 1453,
        231 => 1459,
        232 => 1471,
        233 => 1481,
        234 => 1483,
        235 => 1487,
        236 => 1489,
        237 => 1493,
        238 => 1499,
        239 => 1511,
        240 => 1523,
        241 => 1531,
        242 => 1543,
        243 => 1549,
        244 => 1553,
        245 => 1559,
        246 => 1567,
        247 => 1571,
        248 => 1579,
        249 => 1583,
        250 => 1597,
        251 => 1601,
        252 => 1607,
        253 => 1609,
        254 => 1613,
        255 => 1619,
        256 => 1621,
        257 => 1627,
        258 => 1637,
        259 => 1657,
        260 => 1663,
        261 => 1667,
        262 => 1669,
        263 => 1693,
        264 => 1697,
        265 => 1699,
        266 => 1709,
        267 => 1721,
        268 => 1723,
        269 => 1733,
        270 => 1741,
        271 => 1747,
        272 => 1753,
        273 => 1759,
        274 => 1777,
        275 => 1783,
        276 => 1787,
        277 => 1789,
        278 => 1801,
        279 => 1811,
        280 => 1823,
        281 => 1831,
        282 => 1847,
        283 => 1861,
        284 => 1867,
        285 => 1871,
        286 => 1873,
        287 => 1877,
        288 => 1879,
        289 => 1889,
        290 => 1901,
        291 => 1907,
        292 => 1913,
        293 => 1931,
        294 => 1933,
        295 => 1949,
        296 => 1951,
        297 => 1973,
        298 => 1979,
        299 => 1987,
        300 => 1993,
        301 => 1997,
        302 => 1999,
        303 => 2003,
        304 => 2011,
        305 => 2017,
        306 => 2027,
        307 => 2029,
        308 => 2039,
        309 => 2053,
        310 => 2063,
        311 => 2069,
        312 => 2081,
        313 => 2083,
        314 => 2087,
        315 => 2089,
        316 => 2099,
        317 => 2111,
        318 => 2113,
        319 => 2129,
        320 => 2131,
        321 => 2137,
        322 => 2141,
        323 => 2143,
        324 => 2153,
        325 => 2161,
        326 => 2179,
        327 => 2203,
        328 => 2207,
        329 => 2213,
        330 => 2221,
        331 => 2237,
        332 => 2239,
        333 => 2243,
        334 => 2251,
        335 => 2267,
        336 => 2269,
        337 => 2273,
        338 => 2281,
        339 => 2287,
        340 => 2293,
        341 => 2297,
        342 => 2309,
        343 => 2311,
        344 => 2333,
        345 => 2339,
        346 => 2341,
        347 => 2347,
        348 => 2351,
        349 => 2357,
        350 => 2371,
        351 => 2377,
        352 => 2381,
        353 => 2383,
        354 => 2389,
        355 => 2393,
        356 => 2399,
        357 => 2411,
        358 => 2417,
        359 => 2423,
        360 => 2437,
        361 => 2441,
        362 => 2447,
        363 => 2459,
        364 => 2467,
        365 => 2473,
        366 => 2477,
        367 => 2503,
        368 => 2521,
        369 => 2531,
        370 => 2539,
        371 => 2543,
        372 => 2549,
        373 => 2551,
        374 => 2557,
        375 => 2579,
        376 => 2591,
        377 => 2593,
        378 => 2609,
        379 => 2617,
        380 => 2621,
        381 => 2633,
        382 => 2647,
        383 => 2657,
        384 => 2659,
        385 => 2663,
        386 => 2671,
        387 => 2677,
        388 => 2683,
        389 => 2687,
        390 => 2689,
        391 => 2693,
        392 => 2699,
        393 => 2707,
        394 => 2711,
        395 => 2713,
        396 => 2719,
        397 => 2729,
        398 => 2731,
        399 => 2741,
        400 => 2749,
        401 => 2753,
        402 => 2767,
        403 => 2777,
        404 => 2789,
        405 => 2791,
        406 => 2797,
        407 => 2801,
        408 => 2803,
        409 => 2819,
        410 => 2833,
        411 => 2837,
        412 => 2843,
        413 => 2851,
        414 => 2857,
        415 => 2861,
        416 => 2879,
        417 => 2887,
        418 => 2897,
        419 => 2903,
        420 => 2909,
        421 => 2917,
        422 => 2927,
        423 => 2939,
        424 => 2953,
        425 => 2957,
        426 => 2963,
        427 => 2969,
        428 => 2971,
        429 => 2999,
        430 => 3001,
        431 => 3011,
        432 => 3019,
        433 => 3023,
        434 => 3037,
        435 => 3041,
        436 => 3049,
        437 => 3061,
        438 => 3067,
        439 => 3079,
        440 => 3083,
        441 => 3089,
        442 => 3109,
        443 => 3119,
        444 => 3121,
        445 => 3137,
        446 => 3163,
        447 => 3167,
        448 => 3169,
        449 => 3181,
        450 => 3187,
        451 => 3191,
        452 => 3203,
        453 => 3209,
        454 => 3217,
        455 => 3221,
        456 => 3229,
        457 => 3251,
        458 => 3253,
        459 => 3257,
        460 => 3259,
        461 => 3271,
        462 => 3299,
        463 => 3301,
        464 => 3307,
        465 => 3313,
        466 => 3319,
        467 => 3323,
        468 => 3329,
        469 => 3331,
        470 => 3343,
        471 => 3347,
        472 => 3359,
        473 => 3361,
        474 => 3371,
        475 => 3373,
        476 => 3389,
        477 => 3391,
        478 => 3407,
        479 => 3413,
        480 => 3433,
        481 => 3449,
        482 => 3457,
        483 => 3461,
        484 => 3463,
        485 => 3467,
        486 => 3469,
        487 => 3491,
        488 => 3499,
        489 => 3511,
        490 => 3517,
        491 => 3527,
        492 => 3529,
        493 => 3533,
        494 => 3539,
        495 => 3541,
        496 => 3547,
        497 => 3557,
        498 => 3559,
        499 => 3571,
        500 => 3581,
        501 => 3583,
        502 => 3593,
        503 => 3607,
        504 => 3613,
        505 => 3617,
        506 => 3623,
        507 => 3631,
        508 => 3637,
        509 => 3643,
        510 => 3659,
        511 => 3671,
        512 => 3673,
        513 => 3677,
        514 => 3691,
        515 => 3697,
        516 => 3701,
        517 => 3709,
        518 => 3719,
        519 => 3727,
        520 => 3733,
        521 => 3739,
        522 => 3761,
        523 => 3767,
        524 => 3769,
        525 => 3779,
        526 => 3793,
        527 => 3797,
        528 => 3803,
        529 => 3821,
        530 => 3823,
        531 => 3833,
        532 => 3847,
        533 => 3851,
        534 => 3853,
        535 => 3863,
        536 => 3877,
        537 => 3881,
        538 => 3889,
        539 => 3907,
        540 => 3911,
        541 => 3917,
        542 => 3919,
        543 => 3923,
        544 => 3929,
        545 => 3931,
        546 => 3943,
        547 => 3947,
        548 => 3967,
        549 => 3989,
        550 => 4001,
        551 => 4003,
        552 => 4007,
        553 => 4013,
        554 => 4019,
        555 => 4021,
        556 => 4027,
        557 => 4049,
        558 => 4051,
        559 => 4057,
        560 => 4073,
        561 => 4079,
        562 => 4091,
        563 => 4093,
        564 => 4099,
        565 => 4111,
        566 => 4127,
        567 => 4129,
        568 => 4133,
        569 => 4139,
        570 => 4153,
        571 => 4157,
        572 => 4159,
        573 => 4177,
        574 => 4201,
        575 => 4211,
        576 => 4217,
        577 => 4219,
        578 => 4229,
        579 => 4231,
        580 => 4241,
        581 => 4243,
        582 => 4253,
        583 => 4259,
        584 => 4261,
        585 => 4271,
        586 => 4273,
        587 => 4283,
        588 => 4289,
        589 => 4297,
        590 => 4327,
        591 => 4337,
        592 => 4339,
        593 => 4349,
        594 => 4357,
        595 => 4363,
        596 => 4373,
        597 => 4391,
        598 => 4397,
        599 => 4409,
        600 => 4421,
        601 => 4423,
        602 => 4441,
        603 => 4447,
        604 => 4451,
        605 => 4457,
        606 => 4463,
        607 => 4481,
        608 => 4483,
        609 => 4493,
        610 => 4507,
        611 => 4513,
        612 => 4517,
        613 => 4519,
        614 => 4523,
        615 => 4547,
        616 => 4549,
        617 => 4561,
        618 => 4567,
        619 => 4583,
        620 => 4591,
        621 => 4597,
        622 => 4603,
        623 => 4621,
        624 => 4637,
        625 => 4639,
        626 => 4643,
        627 => 4649,
        628 => 4651,
        629 => 4657,
        630 => 4663,
        631 => 4673,
        632 => 4679,
        633 => 4691,
        634 => 4703,
        635 => 4721,
        636 => 4723,
        637 => 4729,
        638 => 4733,
        639 => 4751,
        640 => 4759,
        641 => 4783,
        642 => 4787,
        643 => 4789,
        644 => 4793,
        645 => 4799,
        646 => 4801,
        647 => 4813,
        648 => 4817,
        649 => 4831,
        650 => 4861,
        651 => 4871,
        652 => 4877,
        653 => 4889,
        654 => 4903,
        655 => 4909,
        656 => 4919,
        657 => 4931,
        658 => 4933,
        659 => 4937,
        660 => 4943,
        661 => 4951,
        662 => 4957,
        663 => 4967,
        664 => 4969,
        665 => 4973,
        666 => 4987,
        667 => 4993,
        668 => 4999,
        669 => 5003,
        670 => 5009,
        671 => 5011,
        672 => 5021,
        673 => 5023,
        674 => 5039,
        675 => 5051,
        676 => 5059,
        677 => 5077,
        678 => 5081,
        679 => 5087,
        680 => 5099,
        681 => 5101,
        682 => 5107,
        683 => 5113,
        684 => 5119,
        685 => 5147,
        686 => 5153,
        687 => 5167,
        688 => 5171,
        689 => 5179,
        690 => 5189,
        691 => 5197,
        692 => 5209,
        693 => 5227,
        694 => 5231,
        695 => 5233,
        696 => 5237,
        697 => 5261,
        698 => 5273,
        699 => 5279,
        700 => 5281,
        701 => 5297,
        702 => 5303,
        703 => 5309,
        704 => 5323,
        705 => 5333,
        706 => 5347,
        707 => 5351,
        708 => 5381,
        709 => 5387,
        710 => 5393,
        711 => 5399,
        712 => 5407,
        713 => 5413,
        714 => 5417,
        715 => 5419,
        716 => 5431,
        717 => 5437,
        718 => 5441,
        719 => 5443,
        720 => 5449,
        721 => 5471,
        722 => 5477,
        723 => 5479,
        724 => 5483,
        725 => 5501,
        726 => 5503,
        727 => 5507,
        728 => 5519,
        729 => 5521,
        730 => 5527,
        731 => 5531,
        732 => 5557,
        733 => 5563,
        734 => 5569,
        735 => 5573,
        736 => 5581,
        737 => 5591,
        738 => 5623,
        739 => 5639,
        740 => 5641,
        741 => 5647,
        742 => 5651,
        743 => 5653,
        744 => 5657,
        745 => 5659,
        746 => 5669,
        747 => 5683,
        748 => 5689,
        749 => 5693,
        750 => 5701,
        751 => 5711,
        752 => 5717,
        753 => 5737,
        754 => 5741,
        755 => 5743,
        756 => 5749,
        757 => 5779,
        758 => 5783,
        759 => 5791,
        760 => 5801,
        761 => 5807,
        762 => 5813,
        763 => 5821,
        764 => 5827,
        765 => 5839,
        766 => 5843,
        767 => 5849,
        768 => 5851,
        769 => 5857,
        770 => 5861,
        771 => 5867,
        772 => 5869,
        773 => 5879,
        774 => 5881,
        775 => 5897,
        776 => 5903,
        777 => 5923,
        778 => 5927,
        779 => 5939,
        780 => 5953,
        781 => 5981,
        782 => 5987,
        783 => 6007,
        784 => 6011,
        785 => 6029,
        786 => 6037,
        787 => 6043,
        788 => 6047,
        789 => 6053,
        790 => 6067,
        791 => 6073,
        792 => 6079,
        793 => 6089,
        794 => 6091,
        795 => 6101,
        796 => 6113,
        797 => 6121,
        798 => 6131,
        799 => 6133,
        800 => 6143,
        801 => 6151,
        802 => 6163,
        803 => 6173,
        804 => 6197,
        805 => 6199,
        806 => 6203,
        807 => 6211,
        808 => 6217,
        809 => 6221,
        810 => 6229,
        811 => 6247,
        812 => 6257,
        813 => 6263,
        814 => 6269,
        815 => 6271,
        816 => 6277,
        817 => 6287,
        818 => 6299,
        819 => 6301,
        820 => 6311,
        821 => 6317,
        822 => 6323,
        823 => 6329,
        824 => 6337,
        825 => 6343,
        826 => 6353,
        827 => 6359,
        828 => 6361,
        829 => 6367,
        830 => 6373,
        831 => 6379,
        832 => 6389,
        833 => 6397,
        834 => 6421,
        835 => 6427,
        836 => 6449,
        837 => 6451,
        838 => 6469,
        839 => 6473,
        840 => 6481,
        841 => 6491,
        842 => 6521,
        843 => 6529,
        844 => 6547,
        845 => 6551,
        846 => 6553,
        847 => 6563,
        848 => 6569,
        849 => 6571,
        850 => 6577,
        851 => 6581,
        852 => 6599,
        853 => 6607,
        854 => 6619,
        855 => 6637,
        856 => 6653,
        857 => 6659,
        858 => 6661,
        859 => 6673,
        860 => 6679,
        861 => 6689,
        862 => 6691,
        863 => 6701,
        864 => 6703,
        865 => 6709,
        866 => 6719,
        867 => 6733,
        868 => 6737,
        869 => 6761,
        870 => 6763,
        871 => 6779,
        872 => 6781,
        873 => 6791,
        874 => 6793,
        875 => 6803,
        876 => 6823,
        877 => 6827,
        878 => 6829,
        879 => 6833,
        880 => 6841,
        881 => 6857,
        882 => 6863,
        883 => 6869,
        884 => 6871,
        885 => 6883,
        886 => 6899,
        887 => 6907,
        888 => 6911,
        889 => 6917,
        890 => 6947,
        891 => 6949,
        892 => 6959,
        893 => 6961,
        894 => 6967,
        895 => 6971,
        896 => 6977,
        897 => 6983,
        898 => 6991,
        899 => 6997,
        900 => 7001,
        901 => 7013,
        902 => 7019,
        903 => 7027,
        904 => 7039,
        905 => 7043,
        906 => 7057,
        907 => 7069,
        908 => 7079,
        909 => 7103,
        910 => 7109,
        911 => 7121,
        912 => 7127,
        913 => 7129,
        914 => 7151,
        915 => 7159,
        916 => 7177,
        917 => 7187,
        918 => 7193,
        919 => 7207,
        920 => 7211,
        921 => 7213,
        922 => 7219,
        923 => 7229,
        924 => 7237,
        925 => 7243,
        926 => 7247,
        927 => 7253,
        928 => 7283,
        929 => 7297,
        930 => 7307,
        931 => 7309,
        932 => 7321,
        933 => 7331,
        934 => 7333,
        935 => 7349,
        936 => 7351,
        937 => 7369,
        938 => 7393,
        939 => 7411,
        940 => 7417,
        941 => 7433,
        942 => 7451,
        943 => 7457,
        944 => 7459,
        945 => 7477,
        946 => 7481,
        947 => 7487,
        948 => 7489,
        949 => 7499,
        950 => 7507,
        951 => 7517,
        952 => 7523,
        953 => 7529,
        954 => 7537,
        955 => 7541,
        956 => 7547,
        957 => 7549,
        958 => 7559,
        959 => 7561,
        960 => 7573,
        961 => 7577,
        962 => 7583,
        963 => 7589,
        964 => 7591,
        965 => 7603,
        966 => 7607,
        967 => 7621,
        968 => 7639,
        969 => 7643,
        970 => 7649,
        971 => 7669,
        972 => 7673,
        973 => 7681,
        974 => 7687,
        975 => 7691,
        976 => 7699,
        977 => 7703,
        978 => 7717,
        979 => 7723,
        980 => 7727,
        981 => 7741,
        982 => 7753,
        983 => 7757,
        984 => 7759,
        985 => 7789,
        986 => 7793,
        987 => 7817,
        988 => 7823,
        989 => 7829,
        990 => 7841,
        991 => 7853,
        992 => 7867,
        993 => 7873,
        994 => 7877,
        995 => 7879,
        996 => 7883,
        997 => 7901,
        998 => 7907,
        999 => 7919,
        1000 => 7927,
        1001 => 7933,
        1002 => 7937,
        1003 => 7949,
        1004 => 7951,
        1005 => 7963,
        1006 => 7993,
        1007 => 8009,
        1008 => 8011,
        1009 => 8017,
        1010 => 8039,
        1011 => 8053,
        1012 => 8059,
        1013 => 8069,
        1014 => 8081,
        1015 => 8087,
        1016 => 8089,
        1017 => 8093,
        1018 => 8101,
        1019 => 8111,
        1020 => 8117,
        1021 => 8123,
        1022 => 8147,
        1023 => 8161,
        _ => panic!("base_index {} >= 1024 not suppoerted", base_index),
    }
}

/// Compute random permutation tables.
///
/// * `rng` - The random number generator.
pub fn compute_radical_inverse_permutations(rng: &mut RNG) -> Vec<u16> {
    // Allocate space for radical inverse permutations.
    let perm_array_size = (0..PRIME_TABLE_SIZE).fold(0, |a, i| a + PRIMES[i]);

    let mut perms = vec![0_u16; perm_array_size];
    let mut p = 0;
    for i in 0..PRIME_TABLE_SIZE {
        // Generate random permutation for i^th prime base.
        for j in 0..PRIMES[i] {
            perms[p + j] = j as u16;
        }

        rng.shuffle(&mut perms[p..p + PRIMES[i]], PRIMES[i], 1);
        p += PRIMES[i];
    }
    perms
}

/// Computes the inverse of the radical inverse function.
///
/// * `base`     - The base is a prime number.
/// * `inverse`  - The integer with reversed digits from `radical_inverse_specialized()`.
/// * `n_digits` - The number of digits.
pub fn inverse_radical_inverse(base: u16, inverse: u64, n_digits: u64) -> u64 {
    let mut index = 0;
    let mut inverse = inverse;
    let base = base as u64;
    for _i in 0..n_digits {
        let digit = inverse % base;
        inverse /= base;
        index = index * base + digit;
    }
    index
}

/// Computes matrix-vector product C[di(a)]^T for some n-digit number
/// `a` and nxn matrix C.
///
/// * `c` - An nxn generator matrix as a linear array of length n^2.
/// * `a` - An n-digit number in some base.
pub fn multiply_generator(c: &[u32], a: u32) -> u32 {
    let mut v = 0_u32;

    let mut i = 0;
    let mut a = a;

    loop {
        if a == 0 {
            break;
        }

        if a & 1 != 0 {
            v ^= c[i];
        }

        i += 1;
        a >>= 1;
    }

    v
}

/// Generate sample values for the (0,2)-sequence.
///
///
/// * `c`        - An nxn generator matrix as a linear array of length n^2.
/// * `a`        - An n-digit number in some base.
/// * `scramble` - Encodes the scrambling as bits of `u32` integeger.
///                Default this to 0.
pub fn sample_generator_matrix(c: &[u32], a: u32, scramble: u32) -> Float {
    min(
        (multiply_generator(c, a) ^ scramble) as Float * hexf32!("0x1.0p-32") as Float,
        ONE_MINUS_EPSILON,
    )
}

/// Returns the n^th Gray code. Each value g(n) differs by just a single
/// bit from the previous one g(n - 1).
///
/// * `n` - Gray code order.
#[inline]
pub fn gray_code_sample(n: u32) -> u32 {
    (n >> 1) ^ n
}

/// Generates the 1D samples using a generator matrix.
///
/// * `c`        - A generator matrix.
/// * `n`        - Number of samples to generate
/// * `scramble` - Starting set of bits used for scrambling.
pub fn gray_code_sample_1d(c: &[u32], n: usize, scramble: u32) -> Vec<Float> {
    let mut v = scramble;
    let mut p = vec![0.0; n];
    for i in 0..n {
        p[i] = min(
            v as Float * hexf32!("0x1.0p-32") as Float, // 1 / (2^32)
            ONE_MINUS_EPSILON,
        );
        v ^= c[(i + 1).trailing_zeros() as usize];
    }
    p
}

/// Generates the 2D samples using a generator matrices.
///
/// * `c`        - Generator matrices for the 2 dimensions.
/// * `n`        - Number of samples to generate
/// * `scramble` - Starting set of bits used for scrambling in each dimension.
pub fn gray_code_sample_2d(c: &[[u32; 32]; 2], n: usize, scramble: &[u32; 2]) -> Vec<Point2f> {
    let mut v = [scramble[0], scramble[1]];
    let mut p = vec![Point2f::default(); n];
    for (i, pi) in p.iter_mut().enumerate().take(n) {
        let t = (i + 1).trailing_zeros() as usize;

        for j in 0..2 {
            (*pi)[j] = min(
                v[j] as Float * hexf32!("0x1.0p-32") as Float,
                ONE_MINUS_EPSILON,
            );
            v[j] ^= c[j][t];
        }
    }
    p
}

/// Define VanDerCorput Generator Matrix.
#[rustfmt::skip]
const C_VANDER_CORPUT: [u32; 32] = [
    0b10000000000000000000000000000000,
    0b1000000000000000000000000000000,
    0b100000000000000000000000000000,
    0b10000000000000000000000000000,
    0b1000000000000000000000000000,
    0b100000000000000000000000000,
    0b10000000000000000000000000,
    0b1000000000000000000000000,
    0b100000000000000000000000,
    0b10000000000000000000000,
    0b1000000000000000000000,
    0b100000000000000000000,
    0b10000000000000000000,
    0b1000000000000000000,
    0b100000000000000000,
    0b10000000000000000,
    0b1000000000000000,
    0b100000000000000,
    0b10000000000000,
    0b1000000000000,
    0b100000000000,
    0b10000000000,
    0b1000000000,
    0b100000000,
    0b10000000,
    0b1000000,
    0b100000,
    0b10000,
    0b1000,
    0b100,
    0b10,
    0b1,
];

/// Generate a number of scrambled 1D sample values using the Gray code-based
/// sampling and the VanDerCorput generator matrix.
///
/// * `n_samples_per_pixel_sample` - Number of samples to generate for every
///                                  sample for a pixel.
/// * `n_pixel_samples`            - Number of samples for a pixel.
/// * `rng`                        - Random number generator.
pub fn van_der_corput(
    n_samples_per_pixel_sample: usize,
    n_pixel_samples: usize,
    rng: &mut RNG,
) -> Vec<Float> {
    let scramble = rng.uniform_u32();
    let total_samples = n_samples_per_pixel_sample * n_pixel_samples;

    let mut samples = gray_code_sample_1d(&C_VANDER_CORPUT, total_samples, scramble);

    // Randomly shuffle 1D sample points.
    for i in 0..n_pixel_samples {
        let start = i * n_samples_per_pixel_sample;
        let end = start + n_samples_per_pixel_sample;
        rng.shuffle(&mut samples[start..end], n_samples_per_pixel_sample, 1);
    }

    // Randomly shuffle last set of 1D sample points.
    let start = n_pixel_samples;
    let end = start + n_samples_per_pixel_sample;
    rng.shuffle(&mut samples[start..end], n_samples_per_pixel_sample, 1);

    samples
}

/// Define 2D Sobol Generator Matrices.
#[rustfmt::skip]
const C_SOBOL: [[u32; 32]; 2] = [
    [
        0x80000000, 0x40000000, 0x20000000, 0x10000000, 
        0x8000000,  0x4000000,  0x2000000,  0x1000000,  
        0x800000,   0x400000,   0x200000,   0x100000, 
        0x80000,    0x40000,    0x20000,    0x10000,
        0x8000,     0x4000,     0x2000,     0x1000,
        0x800,      0x400,      0x200,      0x100, 
        0x80,       0x40,       0x20,       0x10,
        0x8,        0x4,        0x2,        0x1
    ],
    [
        0x80000000, 0xc0000000, 0xa0000000, 0xf0000000, 
        0x88000000, 0xcc000000, 0xaa000000, 0xff000000, 
        0x80800000, 0xc0c00000, 0xa0a00000, 0xf0f00000,
        0x88880000, 0xcccc0000, 0xaaaa0000, 0xffff0000, 
        0x80008000, 0xc000c000, 0xa000a000, 0xf000f000, 
        0x88008800, 0xcc00cc00, 0xaa00aa00, 0xff00ff00,
        0x80808080, 0xc0c0c0c0, 0xa0a0a0a0, 0xf0f0f0f0,
        0x88888888, 0xcccccccc, 0xaaaaaaaa, 0xffffffff
    ]
];

/// Generate a number of scrambled 2D sample values using the Gray code-based
/// sampling and the Sobol generator matrices.
///
/// * `n_samples_per_pixel_sample` - Number of samples to generate for every
///                                  sample for a pixel.
/// * `n_pixel_samples`            - Number of samples for a pixel.
/// * `rng`                        - Random number generator.
pub fn sobol_2d(
    n_samples_per_pixel_sample: usize,
    n_pixel_samples: usize,
    rng: &mut RNG,
) -> Vec<Point2f> {
    let scramble = [rng.uniform_u32(), rng.uniform_u32()];

    let mut samples = gray_code_sample_2d(
        &C_SOBOL,
        n_samples_per_pixel_sample * n_pixel_samples,
        &scramble,
    );

    for i in 0..n_pixel_samples {
        let start = i * n_samples_per_pixel_sample;
        let end = start + n_samples_per_pixel_sample;
        rng.shuffle(&mut samples[start..end], n_samples_per_pixel_sample, 1);
    }

    let start = n_pixel_samples;
    let end = start + n_samples_per_pixel_sample;
    rng.shuffle(&mut samples[start..end], n_samples_per_pixel_sample, 1);

    samples
}

/// Returns the index of a pixel sample.
///
/// * `m`     - Resolution.
/// * `frame` - Sample number.
/// * `p`     - Pixel.
pub fn sobol_interval_to_index(m: u32, frame: u64, p: &Point2i) -> u64 {
    if m == 0 {
        return 0;
    }

    let m2 = m << 1;
    let mut index = frame << m2;

    let mut delta = 0;
    let mut c = 0;
    let mut frame = frame;

    while frame > 0 {
        if frame & 1 > 0 {
            // Add flipped column m + c + 1.
            delta ^= VD_C_SOBOL_MATRICES[(m - 1) as usize][c];
        }
        frame >>= 1;
        c += 1;
    }

    // Flipped b
    let mut b = (((p.x as u32) << m) as u64 | (p.y as u64)) ^ delta;

    c = 0;
    while b > 0 {
        if b & 1 > 0 {
            // Add column 2 * m - c.
            index ^= VD_C_SOBOL_MATRICES_INV[(m - 1) as usize][c];
        }
        b >>= 1;
        c += 1;
    }

    index
}

/// Returns the sample value for a given sample index and dimension.
///
/// * `a`         - Sample index.
/// * `dimension` - Dimension.
/// * `scramble`  - Encodes the scrambling as bits of `u32` integeger.
pub fn sobol_sample(a: u64, dimension: u16, scramble: u64) -> Float {
    // TODO: There is no way to use generics or traits to implement this
    // function for both f32/f64. Maybe macros with compiler feature flags
    // might be the way.
    sobol_sample_f32(a, dimension, scramble)
}

/// Returns the sample value for a given sample index and dimension.
///
/// * `a`         - Sample index.
/// * `dimension` - Dimension.
/// * `scramble`  - Encodes the scrambling as bits of `u32` integeger.
///                 Default to 0.
fn sobol_sample_f32(a: u64, dimension: u16, scramble: u64) -> f32 {
    assert!(
        (dimension as usize) < NUM_SOBOL_DIMENSIONS,
        "Integrator has consumed too many Sobol dimensions; you \
        may want to use a Sampler without a dimension limit like \
            (0, 2)-sequence"
    );

    let mut a = a;
    let mut v = scramble as u32;

    let mut i = (dimension as usize) * SOBOL_MATRIX_SIZE;
    loop {
        if a == 0 {
            break;
        }

        if (a & 1) > 0 {
            v ^= SOBOL_MATRICES_32[i];
        }

        a >>= 1;
        i += 1;
    }

    min(
        (v as f32) * (hexf32!("0x1.0p-32") as f32),
        FLOAT_ONE_MINUS_EPSILON,
    )
}

/// Returns the sample value for a given sample index and dimension.
///
/// * `a`         - Sample index.
/// * `dimension` - Dimension.
/// * `scramble`  - Encodes the scrambling as bits of `u32` integeger.
///                 Default to 0.
#[allow(unused)]
fn sobol_sample_f64(a: u64, dimension: u16, scramble: u64) -> f64 {
    assert!(
        (dimension as usize) < NUM_SOBOL_DIMENSIONS,
        "Integrator has consumed too many Sobol dimensions; you \
        may want to use a Sampler without a dimension limit like \
            (0, 2)-sequence"
    );

    let mut a = a;
    let mut result = scramble & !(-((1_usize << SOBOL_MATRIX_SIZE) as i64) as u64);

    let mut i = (dimension as usize) * SOBOL_MATRIX_SIZE;
    loop {
        if a == 0 {
            break;
        }

        if (a & 1) > 0 {
            result ^= SOBOL_MATRICES_64[i];
        }

        a >>= 1;
        i += 1;
    }

    min(
        result as f64 * (1.0 / (1_usize << SOBOL_MATRIX_SIZE) as f64),
        DOUBLE_ONE_MINUS_EPSILON,
    )
}
