From: Matt Corallo Date: Wed, 1 Nov 2023 01:11:42 +0000 (+0000) Subject: Correct approximate log-times-2048 tables for small values X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2F2023-10-log-fix;p=rust-lightning Correct approximate log-times-2048 tables for small values The table generation didn't exactly match the lookup logic, causing values below 2^6 to not always line up correctly. --- diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 9c907c3f7..91349d845 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -1477,26 +1477,26 @@ mod approx { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, - 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, 617, - 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, - 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977, 977], - [1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, 1233, - 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, 1431, - 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, 1594, - 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731, 1731], - [1850, 1850, 1850, 1850, 1850, 1850, 1850, 1850, 1954, 1954, 1954, 1954, 1954, 1954, 1954, 1954, - 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2133, 2133, 2133, 2133, 2133, 2133, 2133, 2133, - 2210, 2210, 2210, 2210, 2210, 2210, 2210, 2210, 2281, 2281, 2281, 2281, 2281, 2281, 2281, 2281, - 2347, 2347, 2347, 2347, 2347, 2347, 2347, 2347, 2409, 2409, 2409, 2409, 2409, 2409, 2409, 2409], - [2466, 2466, 2466, 2466, 2520, 2520, 2520, 2520, 2571, 2571, 2571, 2571, 2619, 2619, 2619, 2619, - 2665, 2665, 2665, 2665, 2708, 2708, 2708, 2708, 2749, 2749, 2749, 2749, 2789, 2789, 2789, 2789, - 2827, 2827, 2827, 2827, 2863, 2863, 2863, 2863, 2898, 2898, 2898, 2898, 2931, 2931, 2931, 2931, - 2964, 2964, 2964, 2964, 2995, 2995, 2995, 2995, 3025, 3025, 3025, 3025, 3054, 3054, 3054, 3054], - [3083, 3083, 3110, 3110, 3136, 3136, 3162, 3162, 3187, 3187, 3212, 3212, 3235, 3235, 3259, 3259, - 3281, 3281, 3303, 3303, 3324, 3324, 3345, 3345, 3366, 3366, 3386, 3386, 3405, 3405, 3424, 3424, - 3443, 3443, 3462, 3462, 3479, 3479, 3497, 3497, 3514, 3514, 3531, 3531, 3548, 3548, 3564, 3564, - 3580, 3580, 3596, 3596, 3612, 3612, 3627, 3627, 3642, 3642, 3656, 3656, 3671, 3671, 3685, 3685], + [617, 617, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, + 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, + 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, + 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977, 617, 977], + [1233, 1233, 1233, 1233, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, + 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, + 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, + 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731, 1233, 1431, 1594, 1731], + [1850, 1850, 1850, 1850, 1850, 1850, 1850, 1850, 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409, + 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409, 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409, + 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409, 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409, + 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409, 1850, 1954, 2048, 2133, 2210, 2281, 2347, 2409], + [2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, 2466, + 2466, 2520, 2571, 2619, 2665, 2708, 2749, 2789, 2827, 2863, 2898, 2931, 2964, 2995, 3025, 3054, + 2466, 2520, 2571, 2619, 2665, 2708, 2749, 2789, 2827, 2863, 2898, 2931, 2964, 2995, 3025, 3054, + 2466, 2520, 2571, 2619, 2665, 2708, 2749, 2789, 2827, 2863, 2898, 2931, 2964, 2995, 3025, 3054], + [3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, + 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, 3083, + 3083, 3110, 3136, 3162, 3187, 3212, 3235, 3259, 3281, 3303, 3324, 3345, 3366, 3386, 3405, 3424, + 3443, 3462, 3479, 3497, 3514, 3531, 3548, 3564, 3580, 3596, 3612, 3627, 3642, 3656, 3671, 3685], [3699, 3713, 3726, 3740, 3753, 3766, 3779, 3791, 3804, 3816, 3828, 3840, 3852, 3864, 3875, 3886, 3898, 3909, 3919, 3930, 3941, 3951, 3962, 3972, 3982, 3992, 4002, 4012, 4022, 4031, 4041, 4050, 4060, 4069, 4078, 4087, 4096, 4105, 4114, 4122, 4131, 4139, 4148, 4156, 4164, 4173, 4181, 4189, @@ -1754,18 +1754,25 @@ mod approx { fn prints_negative_log10_times_2048_lookup_table() { for msb in 0..BITS { for i in 0..LOWER_BITS_BOUND { - let x = ((LOWER_BITS_BOUND + i) << (HIGHEST_BIT - LOWER_BITS)) >> (HIGHEST_BIT - msb); - let log10_times_2048 = ((x as f64).log10() * 2048.0).round() as u16; - assert_eq!(log10_times_2048, LOG10_TIMES_2048[msb as usize][i as usize]); + let mut x = 1 << msb; + if msb >= LOWER_BITS || i >= (1 << msb) { + x |= i << msb.saturating_sub(LOWER_BITS); + } + x <<= 63 - msb; + x >>= 63 - msb; + if x == 0 { x = 1; } + let calc_log10_times_2048 = ((x as f64).log10() * 2048.0).round() as u16; + assert_eq!(calc_log10_times_2048, log10_times_2048(x)); + assert_eq!(calc_log10_times_2048, LOG10_TIMES_2048[msb as usize][i as usize]); if i % LOWER_BITS_BOUND == 0 { - print!("\t\t[{}, ", log10_times_2048); + print!("\t\t[{}, ", calc_log10_times_2048); } else if i % LOWER_BITS_BOUND == LOWER_BITS_BOUND - 1 { - println!("{}],", log10_times_2048); + println!("{}],", calc_log10_times_2048); } else if i % (LOWER_BITS_BOUND/4) == LOWER_BITS_BOUND/4 - 1 { - print!("{},\n\t\t\t", log10_times_2048); + print!("{},\n\t\t\t", calc_log10_times_2048); } else { - print!("{}, ", log10_times_2048); + print!("{}, ", calc_log10_times_2048); } } }