Precompute P1

Improves performance by ~1% and removes primal dependency
This commit is contained in:
Christopher Berner 2019-03-23 14:13:34 -07:00
parent 2ec675065b
commit ea913f0ddd
2 changed files with 486 additions and 12 deletions

@ -7,12 +7,10 @@ version = "0.8.0"
edition = "2018"
authors = ["Christopher Berner <christopherberner@gmail.com>"]
[dependencies]
primal = "0.2"
[dev-dependencies]
rand = "0.6"
criterion = "0.2"
primal = "0.2"
rand = "0.6"
[[bench]]
name = "codec_benchmark"

@ -481,6 +481,485 @@ pub const SYSTEMATIC_INDICES_AND_PARAMETERS: [(u32, u32, u32, u32, u32); 477] =
(55843, 963, 907, 16, 56393),
(56403, 471, 907, 16, 56951)];
const P1_TABLE: [(u32, u32); 477] = [
(10, 11),
(12, 11),
(18, 11),
(20, 11),
(26, 11),
(30, 11),
(32, 11),
(36, 11),
(42, 11),
(46, 11),
(48, 11),
(49, 11),
(55, 11),
(60, 13),
(62, 13),
(69, 13),
(75, 13),
(84, 17),
(88, 17),
(91, 17),
(95, 17),
(97, 17),
(101, 17),
(114, 17),
(119, 17),
(125, 17),
(127, 17),
(138, 19),
(140, 19),
(149, 19),
(153, 19),
(160, 23),
(166, 23),
(168, 23),
(179, 23),
(181, 23),
(185, 23),
(187, 23),
(200, 23),
(213, 23),
(217, 23),
(225, 23),
(236, 29),
(242, 29),
(248, 29),
(257, 29),
(263, 29),
(269, 29),
(280, 29),
(295, 29),
(301, 29),
(305, 29),
(324, 29),
(337, 29),
(341, 29),
(347, 29),
(355, 29),
(362, 31),
(368, 31),
(372, 31),
(380, 31),
(385, 31),
(393, 31),
(405, 31),
(418, 37),
(428, 37),
(434, 37),
(447, 37),
(453, 37),
(466, 37),
(478, 37),
(486, 37),
(491, 37),
(497, 37),
(511, 37),
(526, 37),
(532, 37),
(542, 37),
(549, 37),
(557, 37),
(563, 37),
(573, 37),
(580, 41),
(588, 41),
(594, 41),
(600, 41),
(606, 41),
(619, 41),
(633, 41),
(640, 41),
(648, 41),
(666, 41),
(675, 41),
(685, 41),
(693, 41),
(703, 41),
(718, 43),
(728, 43),
(736, 43),
(747, 43),
(759, 43),
(778, 47),
(792, 47),
(802, 47),
(811, 47),
(821, 47),
(835, 47),
(845, 47),
(860, 47),
(870, 47),
(891, 47),
(903, 47),
(913, 47),
(926, 53),
(938, 53),
(950, 53),
(963, 53),
(977, 53),
(989, 53),
(1002, 53),
(1020, 53),
(1032, 53),
(1050, 53),
(1074, 53),
(1085, 53),
(1099, 53),
(1111, 53),
(1136, 53),
(1152, 53),
(1169, 59),
(1183, 59),
(1205, 59),
(1220, 59),
(1236, 59),
(1255, 59),
(1269, 59),
(1285, 59),
(1306, 59),
(1347, 59),
(1361, 59),
(1389, 59),
(1404, 59),
(1420, 59),
(1436, 59),
(1461, 61),
(1477, 61),
(1502, 61),
(1522, 61),
(1539, 67),
(1561, 67),
(1579, 67),
(1600, 67),
(1616, 67),
(1649, 67),
(1673, 67),
(1698, 67),
(1716, 67),
(1734, 67),
(1759, 67),
(1777, 67),
(1800, 67),
(1824, 67),
(1844, 67),
(1863, 71),
(1887, 71),
(1906, 71),
(1926, 71),
(1954, 71),
(1979, 71),
(2005, 71),
(2040, 71),
(2070, 71),
(2103, 73),
(2125, 73),
(2152, 73),
(2195, 79),
(2217, 79),
(2247, 79),
(2278, 79),
(2315, 79),
(2339, 79),
(2367, 79),
(2392, 79),
(2416, 79),
(2447, 79),
(2473, 79),
(2502, 79),
(2528, 79),
(2565, 83),
(2601, 83),
(2640, 83),
(2668, 83),
(2701, 83),
(2737, 83),
(2772, 83),
(2802, 83),
(2831, 89),
(2875, 89),
(2906, 89),
(2938, 89),
(2979, 89),
(3015, 89),
(3056, 89),
(3101, 89),
(3151, 89),
(3186, 89),
(3224, 89),
(3265, 97),
(3299, 97),
(3344, 97),
(3387, 97),
(3423, 97),
(3466, 97),
(3502, 97),
(3539, 97),
(3579, 97),
(3616, 97),
(3658, 97),
(3697, 97),
(3751, 97),
(3792, 97),
(3840, 97),
(3883, 101),
(3924, 101),
(3970, 101),
(4015, 101),
(4069, 101),
(4112, 101),
(4165, 103),
(4207, 103),
(4252, 103),
(4318, 103),
(4365, 107),
(4418, 107),
(4468, 107),
(4513, 107),
(4567, 107),
(4626, 107),
(4681, 109),
(4731, 109),
(4780, 109),
(4838, 109),
(4901, 113),
(4954, 113),
(5008, 113),
(5063, 113),
(5116, 113),
(5172, 113),
(5225, 127),
(5279, 127),
(5334, 127),
(5391, 127),
(5449, 127),
(5506, 127),
(5566, 127),
(5637, 127),
(5694, 127),
(5763, 127),
(5823, 127),
(5896, 127),
(5975, 127),
(6039, 127),
(6102, 127),
(6169, 127),
(6233, 127),
(6296, 127),
(6363, 127),
(6427, 127),
(6518, 127),
(6589, 131),
(6655, 131),
(6730, 131),
(6799, 131),
(6878, 131),
(6956, 131),
(7033, 137),
(7108, 137),
(7185, 137),
(7281, 137),
(7360, 137),
(7445, 137),
(7520, 137),
(7596, 137),
(7675, 139),
(7770, 139),
(7855, 149),
(7935, 149),
(8030, 149),
(8111, 149),
(8194, 149),
(8290, 149),
(8377, 149),
(8474, 149),
(8559, 149),
(8654, 149),
(8744, 149),
(8837, 149),
(8928, 149),
(9019, 151),
(9111, 151),
(9206, 151),
(9303, 157),
(9400, 157),
(9497, 157),
(9601, 157),
(9708, 157),
(9813, 157),
(9916, 157),
(10017, 163),
(10120, 163),
(10241, 163),
(10351, 163),
(10458, 163),
(10567, 163),
(10676, 163),
(10787, 167),
(10899, 167),
(11015, 167),
(11130, 167),
(11245, 167),
(11358, 173),
(11475, 173),
(11590, 173),
(11711, 173),
(11829, 173),
(11956, 173),
(12087, 173),
(12208, 179),
(12333, 179),
(12460, 179),
(12593, 179),
(12726, 179),
(12857, 179),
(13002, 181),
(13143, 181),
(13284, 191),
(13417, 191),
(13558, 191),
(13695, 191),
(13833, 191),
(13974, 191),
(14115, 191),
(14272, 191),
(14415, 191),
(14560, 191),
(14713, 191),
(14862, 193),
(15011, 193),
(15170, 197),
(15325, 197),
(15496, 197),
(15651, 197),
(15808, 199),
(15977, 199),
(16161, 211),
(16336, 211),
(16505, 211),
(16674, 211),
(16851, 211),
(17024, 211),
(17195, 211),
(17376, 211),
(17559, 211),
(17742, 211),
(17929, 211),
(18116, 223),
(18309, 223),
(18503, 223),
(18694, 223),
(18909, 223),
(19126, 223),
(19325, 223),
(19539, 223),
(19740, 223),
(19939, 223),
(20152, 227),
(20355, 227),
(20564, 227),
(20778, 227),
(20988, 229),
(21199, 233),
(21412, 233),
(21629, 233),
(21852, 233),
(22073, 239),
(22301, 239),
(22536, 239),
(22779, 239),
(23010, 239),
(23252, 241),
(23491, 251),
(23730, 251),
(23971, 251),
(24215, 251),
(24476, 251),
(24721, 251),
(24976, 251),
(25230, 251),
(25493, 257),
(25756, 257),
(26022, 257),
(26291, 257),
(26566, 257),
(26838, 263),
(27111, 263),
(27392, 263),
(27682, 263),
(27959, 269),
(28248, 269),
(28548, 269),
(28845, 269),
(29138, 269),
(29434, 271),
(29731, 277),
(30037, 277),
(30346, 277),
(30654, 277),
(30974, 281),
(31285, 281),
(31605, 281),
(31948, 283),
(32272, 293),
(32601, 293),
(32932, 293),
(33282, 293),
(33623, 293),
(33961, 293),
(34302, 293),
(34654, 307),
(35031, 307),
(35395, 307),
(35750, 307),
(36112, 307),
(36479, 307),
(36849, 307),
(37227, 307),
(37606, 307),
(37992, 311),
(38385, 311),
(38787, 311),
(39176, 313),
(39576, 317),
(39980, 317),
(40398, 317),
(40816, 331),
(41226, 331),
(41641, 331),
(42067, 331),
(42490, 331),
(42916, 331),
(43388, 331),
(43840, 331),
(44279, 337),
(44729, 337),
(45183, 337),
(45638, 337),
(46104, 347),
(46574, 347),
(47047, 347),
(47523, 347),
(48007, 347),
(48489, 349),
(48976, 349),
(49470, 353),
(49978, 353),
(50511, 359),
(51017, 359),
(51530, 359),
(52062, 367),
(52586, 367),
(53114, 367),
(53650, 367),
(54188, 373),
(54735, 373),
(55289, 373),
(55843, 373),
(56403, 379)];
// Calculates, K', the extended source block size, in symbols, for a given source block size
// See section 5.3.1
pub fn extended_source_block_symbols(source_block_symbols: u32) -> u32 {
@ -557,16 +1036,13 @@ pub fn num_pi_symbols(source_block_symbols: u32) -> u32 {
// Calculates P1, smallest prime greater than P. See 5.3.3.3
pub fn calculate_p1(source_block_symbols: u32) -> u32 {
let mut p1 = num_pi_symbols(source_block_symbols);
while !primal::is_prime(p1 as u64) {
if p1 % 2 == 0 {
p1 += 1;
}
else {
p1 += 2;
assert!(source_block_symbols <= MAX_SOURCE_SYMBOLS_PER_BLOCK);
for &(block_size, p1) in P1_TABLE.iter() {
if block_size >= source_block_symbols {
return p1;
}
}
p1
unreachable!();
}
#[cfg(test)]