From 4aaa5e344d1eadd37823de1e3762990e67bf42f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rayan=20Hassa=C3=AFne?= Date: Thu, 26 Nov 2026 14:22:00 +0100 Subject: [PATCH] fix(tin): compute log and exp with a port of glibc's FMA code tin.py takes its logarithms and exponentials from Python's math module, i.e. the C library. Rust's f64::ln/exp use the C library of the target as well, so TINs differed from tin.py's in the last digit on some platforms (macOS, musl) and, on glibc, depended on whether the CPU has FMA: glibc picks different builds of log and exp (__ieee754_log_fma, __ieee754_exp_fma) that round differently for about 1 in 10 000 inputs, which changes about one TIN in a thousand and can move the summary digits. Add bit-exact ports of glibc 2.39's FMA log and exp (the builds glibc selects on every x86-64 CPU with FMA and AVX2, and whose results aarch64 matches), and use them in compute_tin. The code follows sysdeps/ieee754/dbl-64/e_log.c and e_exp.c with f64::mul_add at the points where GCC fused a*b+c in glibc's FMA builds, read from the machine code of libm. The ports match glibc's FMA path and Python's math.log/math.exp on 7 million inputs (random bit patterns, near 1, subnormals, c/total), and the TINs tin.py prints, on every platform. The NumPy-based summary needs no change: add.reduce uses the same scalar pairwise sum under every CPU dispatch target. Co-Authored-By: Claude Sonnet 5.5 --- docs/src/content/docs/rna/rseqc.mdx | 9 + src/rna/rseqc/glibc_math.rs | 290 ++++++++++++++++++++ src/rna/rseqc/glibc_math_tables.rs | 393 ++++++++++++++++++++++++++++ src/rna/rseqc/mod.rs | 2 + src/rna/rseqc/tin.rs | 34 ++- 5 files changed, 725 insertions(+), 3 deletions(-) create mode 100644 src/rna/rseqc/glibc_math.rs create mode 100644 src/rna/rseqc/glibc_math_tables.rs diff --git a/docs/src/content/docs/rna/rseqc.mdx b/docs/src/content/docs/rna/rseqc.mdx index ffd4ec4..9467c73 100644 --- a/docs/src/content/docs/rna/rseqc.mdx +++ b/docs/src/content/docs/rna/rseqc.mdx @@ -523,6 +523,15 @@ Transcripts below the minimum coverage threshold are reported with a TIN of `0.0 and are left out of the summary statistics, as in `tin.py`. TIN values are written at full precision, like Python's `str(float)`. +`tin.py` takes its logarithms and exponentials from the C library, and the last digit of +some of them depends on the machine: glibc runs a different build of `log` and `exp` on +CPUs with FMA than on CPUs without it, and macOS uses another library again. RustQC +carries its own port of glibc's FMA build of `log` and `exp`, so its TIN values are the +same on every platform. They are the values `tin.py` prints on any x86-64 CPU with +FMA and AVX2 (Intel Haswell and AMD Zen or newer) and on aarch64. On an older x86-64 CPU, +`tin.py` runs glibc's generic build and can differ from this in the last digit for a +small share of the transcripts (about one in a thousand). + Rows follow the BED order produced by nf-core/rnaseq's `eautils/gtf2bed` module. The converter sorts by chromosome name, the start of each transcript's first exon line in the GTF, then transcript ID. Exons without a transcript ID are omitted. diff --git a/src/rna/rseqc/glibc_math.rs b/src/rna/rseqc/glibc_math.rs new file mode 100644 index 0000000..c3f1fce --- /dev/null +++ b/src/rna/rseqc/glibc_math.rs @@ -0,0 +1,290 @@ +//! Bit-exact ports of glibc's double-precision `log` and `exp`, FMA variant. +//! +//! `tin.py` takes its logarithms and exponentials from Python's `math` +//! module, which calls the C library. Rust's `f64::ln` and `f64::exp` call +//! the C library of the build target as well, so a binary built for macOS, +//! musl or another glibc configuration prints TINs that differ from `tin.py`'s +//! in the last digits for a small share of the transcripts, and the summary +//! statistics inherit those differences. +//! +//! Both functions are glibc 2.28+'s `log` and `exp` +//! (`sysdeps/ieee754/dbl-64/e_log.c` and `e_exp.c`, tables in `e_log_data.c` +//! and `e_exp_data.c`, taken from glibc 2.39) as compiled with FMA enabled: +//! `__ieee754_log_fma` and `__ieee754_exp_fma`. glibc on x86-64 selects those +//! through an ifunc on every CPU with FMA and AVX2 (anything since Intel +//! Haswell and AMD Zen), and the aarch64 build has FMA in its baseline and +//! returns the same bits (checked on 300 000 inputs). The generic build used +//! on older x86-64 CPUs rounds differently in the last bit for about 1 in +//! 10 000 inputs, so `tin.py` on such a CPU prints slightly different TINs; +//! its output is the one thing RustQC does not reproduce. +//! +//! The code below follows the C code operation by operation, with +//! [`f64::mul_add`] (a correctly rounded fused multiply-add on every target) +//! at exactly the places where GCC contracted `a * b + c` when it built +//! glibc's FMA variants. The contraction points were read from the machine +//! code of Ubuntu 24.04's `libm.so.6` (glibc 2.39), and each is noted next to +//! the code. Plain `*` and `+` are never contracted by Rust, so every other +//! operation rounds exactly once, as in the machine code. +//! +//! Only the arguments `compute_tin` can produce are handled here: `log` of a +//! positive finite number and `exp` of a finite number below 512 in magnitude +//! (an entropy of 512 nats would need more than 10^222 sampled positions). +//! Anything else is passed on to the platform's `ln` and `exp`. + +use super::glibc_math_tables::{EXP_TAB, LOG_TAB}; + +const LN2_HI: f64 = f64::from_bits(0x3fe62e42fefa3800); +const LN2_LO: f64 = f64::from_bits(0x3d2ef35793c76730); + +/// `__log_data.poly1`: polynomial for inputs close to 1. +const LOG_POLY1: [f64; 11] = [ + f64::from_bits(0xbfe0000000000000), + f64::from_bits(0x3fd5555555555577), + f64::from_bits(0xbfcffffffffffdcb), + f64::from_bits(0x3fc999999995dd0c), + f64::from_bits(0xbfc55555556745a7), + f64::from_bits(0x3fc24924a344de30), + f64::from_bits(0xbfbfffffa4423d65), + f64::from_bits(0x3fbc7184282ad6ca), + f64::from_bits(0xbfb999eb43b068ff), + f64::from_bits(0x3fb78182f7afd085), + f64::from_bits(0xbfb5521375d145cd), +]; + +/// `__log_data.poly`: polynomial for the table-driven path. +const LOG_POLY: [f64; 5] = [ + f64::from_bits(0xbfe0000000000001), + f64::from_bits(0x3fd555555551305b), + f64::from_bits(0xbfcfffffffeb4590), + f64::from_bits(0x3fc999b324f10111), + f64::from_bits(0xbfc55575e506c89f), +]; + +const EXP_INV_LN2_N: f64 = f64::from_bits(0x40671547652b82fe); +const EXP_NEG_LN2_HI_N: f64 = f64::from_bits(0xbf762e42fefa0000); +const EXP_NEG_LN2_LO_N: f64 = f64::from_bits(0xbd0cf79abc9e3b3a); +const EXP_SHIFT: f64 = f64::from_bits(0x4338000000000000); + +/// `__exp_data.poly` (`C2` to `C5` in `e_exp.c`). +const EXP_POLY: [f64; 4] = [ + f64::from_bits(0x3fdffffffffffdbd), + f64::from_bits(0x3fc555555555543c), + f64::from_bits(0x3fa55555cf172b91), + f64::from_bits(0x3f81111167a4d017), +]; + +/// glibc's `log(x)` (FMA build), for `x > 0`. Zero, negative, infinite and NaN +/// inputs are passed to the platform `ln`. +pub(crate) fn log(x: f64) -> f64 { + let mut ix = x.to_bits(); + let top = (ix >> 48) as u32; + + const LO: u64 = 0x3fee000000000000; // 1.0 - 0x1p-4 + const HI: u64 = 0x3ff1090000000000; // 1.0 + 0x1.09p-4 + if ix.wrapping_sub(LO) < HI - LO { + // Close to 1.0. + if ix == 1.0f64.to_bits() { + return 0.0; + } + let b = &LOG_POLY1; + let r = x - 1.0; + let r2 = r * r; + let r3 = r * r2; + // Horner steps of the degree-10 polynomial, each `a + r^j * b` fused. + let p1 = r.mul_add(b[2], b[1]); + let p2 = r.mul_add(b[5], b[4]); + let p3 = r.mul_add(b[8], b[7]); + let p1 = r2.mul_add(b[3], p1); + let p2 = r2.mul_add(b[6], p2); + let q = r2.mul_add(b[9], p3); + let q = r3.mul_add(b[10], q); + let q = q.mul_add(r3, p2); + let q = q.mul_add(r3, p1); + // r + r*2^27 and (r + w) - w are exact in either rounding mode. + let w = r * f64::from_bits(0x41a0000000000000); // 0x1p27 + let rhi = r + w - w; + let rlo = r - rhi; + let rhi2 = rhi * rhi; + let hi = rhi2.mul_add(b[0], r); // r + rhi^2 * b0 + let lo = rhi2.mul_add(b[0], r - hi); // (r - hi) + rhi^2 * b0 + let lo = (b[0] * rlo).mul_add(rhi + r, lo); // lo + b0 * rlo * (rhi + r) + let y = q.mul_add(r3, lo); // lo + r^3 * poly + return y + hi; + } + if top.wrapping_sub(0x0010) >= 0x7ff0 - 0x0010 { + if ix == 0 || ix >> 52 != 0 { + // Zero, negative, infinite or NaN. + return x.ln(); + } + // Subnormal: scale into the normal range, as glibc does. + ix = (x * f64::from_bits(0x4330000000000000)).to_bits(); // 0x1p52 + ix = ix.wrapping_sub(52u64 << 52); + } + + // x = 2^k z, with z in [OFF, 2 OFF), split into 128 subintervals. + const OFF: u64 = 0x3fe6000000000000; + let tmp = ix.wrapping_sub(OFF); + let i = ((tmp >> (52 - 7)) % 128) as usize; + let k = (tmp as i64) >> 52; + let iz = ix.wrapping_sub(tmp & (0xfffu64 << 52)); + let invc = f64::from_bits(LOG_TAB[i].0); + let logc = f64::from_bits(LOG_TAB[i].1); + let z = f64::from_bits(iz); + + // r ~= z/c - 1, rounded once. + let r = z.mul_add(invc, -1.0); + let kd = k as f64; + + // hi + lo = r + log(c) + k*Ln2. + let w = kd.mul_add(LN2_HI, logc); + let hi = r + w; + let lo = kd.mul_add(LN2_LO, w - hi + r); + + // log(x) = lo + (log1p(r) - r) + hi. + let a = &LOG_POLY; + let r2 = r * r; + let r3 = r * r2; + let t1 = r.mul_add(a[2], a[1]); + let t2 = r.mul_add(a[4], a[3]); + let lo = r2.mul_add(a[0], lo); + let poly = t2.mul_add(r2, t1); + r3.mul_add(poly, lo) + hi +} + +/// glibc's `exp(x)` (FMA build), for |x| < 512. Larger magnitudes and +/// non-finite inputs are passed to the platform `exp`. +pub(crate) fn exp(x: f64) -> f64 { + let abstop = ((x.to_bits() >> 52) & 0x7ff) as u32; + const TOP_TINY: u32 = 0x3c9; // top 12 bits of 0x1p-54 + const TOP_512: u32 = 0x408; // top 12 bits of 512.0 + if abstop.wrapping_sub(TOP_TINY) >= TOP_512 - TOP_TINY { + if abstop.wrapping_sub(TOP_TINY) >= 0x8000_0000 { + // Tiny |x| (including 0): 1 + x. + return 1.0 + x; + } + return x.exp(); + } + + // exp(x) = 2^(k/N) * exp(r), with N = 128 and r in [-ln2/2N, ln2/2N]. + let mut kd = x.mul_add(EXP_INV_LN2_N, EXP_SHIFT); // InvLn2N * x + Shift + let ki = kd.to_bits(); + kd -= EXP_SHIFT; + let r = kd.mul_add(EXP_NEG_LN2_HI_N, x); + let r = kd.mul_add(EXP_NEG_LN2_LO_N, r); + // 2^(k/N) ~= scale * (1 + tail). + let idx = 2 * (ki % 128) as usize; + let top = ki << (52 - 7); + let tail = f64::from_bits(EXP_TAB[idx]); + let sbits = EXP_TAB[idx + 1].wrapping_add(top); + let r2 = r * r; + let c = &EXP_POLY; + let ta = r.mul_add(c[1], c[0]); + let tb = r.mul_add(c[3], c[2]); + let tmp = ta.mul_add(r2, tail + r); + let tmp = (r2 * r2).mul_add(tb, tmp); + let scale = f64::from_bits(sbits); + scale.mul_add(tmp, scale) +} + +#[cfg(test)] +mod tests { + use super::*; + + // Inputs and the results of glibc 2.39's `log` and `exp` on a CPU with FMA + // (what Python's `math.log` and `math.exp` return there), as IEEE-754 bit + // patterns. Entries marked "generic build differs" are inputs for which + // glibc's generic (non-FMA) build returns a neighbouring double. The + // subnormal and tiny inputs exercise the special paths. + const LOG_VECTORS: &[(u64, u64)] = &[ + (0x3fecec2bb38afa34, 0xbfb9e2a5c7d0b764), // generic build differs + (0x3fe65be5b00e8522, 0xbfd6f1cc39044bf3), // generic build differs + (0x3ff14ee7e9f500a4, 0x3fb41eae99645126), // generic build differs + (0x3fe547332e20495c, 0xbfda1d9874251361), // generic build differs + (0x3fed9d33141fad8d, 0xbfb3d629f7ed4440), // generic build differs + (0x3fe9ccc8547ab8a2, 0xbfcb910ca183e7c0), // generic build differs + (0x3fed21b5116f5de0, 0xbfb80a7ced5115d4), // generic build differs + (0x3fecdc66cf1f9d9b, 0xbfba6e5f562538b3), // generic build differs + (0x3fee3c402fc0214a, 0xbfad0aed2d115496), // generic build differs + (0x3fee5c36d76842ef, 0xbfaaeec6e62e1b9d), // generic build differs + (0x3fee1ef3e671abc4, 0xbfaefbfea4e6a3a8), // generic build differs + (0x3ff0c7e4dfeaefc8, 0x3fa8656c944572f0), // generic build differs (near 1) + (0x3fee0b57736386d2, 0xbfb024e2df71533f), // generic build differs (near 1) + (0x3fee8269f4f4327e, 0xbfa86c261cee6484), // generic build differs (near 1) + (0x3fd5935f686ee044, 0xbff165a612420b4c), + (0x3fe5eede501e021a, 0xbfd82ce40a36cc16), + (0x369c72d601975bb2, 0xc059d9615d3293bd), + (0x7604fd61992d17f0, 0x4082bec004044773), + (0x3e6dab7233c0f7cc, 0xc030b60d5f12f4f7), + (0x3fefffffffff562c, 0xbd953a8000003855), + (0x3feff1ac5d6874da, 0xbf5cadb122b0b270), + (0x3ff0e6f463521c65, 0x3fac15b44c0b260a), + (0x000ee1780f9c0e82, 0xc08623c06369870e), + (0x0007e6de644a3e99, 0xc08628d0b7a2ee4b), + (0x000fc3341b23eb93, 0xc086234a7dc1f43c), + ]; + const EXP_VECTORS: &[(u64, u64)] = &[ + (0xc0485bfef4264fc0, 0x3b8a3dadb406b785), // generic build differs + (0x3fee107bbde0d838, 0x40047848e4d06db1), // generic build differs + (0x40392d7e01f530c4, 0x4234067712417e53), // generic build differs + (0xc07c6f6b1d36e92e, 0x16e8b223988e78a1), // generic build differs + (0xbfef168fbcec9eb0, 0x3fd8398fd1a4a781), // generic build differs + (0xc070e8d96829ff3e, 0x27898903d421ce21), // generic build differs + (0x402ffa60ee3abc5a, 0x4160c38b3056e4b9), // generic build differs + (0x40382d9f21bef4d0, 0x421d7b7f90b1f779), // generic build differs + (0xbfeb18418c2966e0, 0x3fdb71d62f7503a8), + (0x4068d4fa8ae2a6bc, 0x51d83de75a00a782), + (0xbf868915c87e7f00, 0x3fefa65a284488a7), + (0x3ffe3a4bc1c4c1f0, 0x401a750857119ada), + (0x40223e02dd073db4, 0x40c1dd3326870d7c), + (0xbbf89cf39c0eeeb0, 0x3ff0000000000000), + (0xa7cc3ef0cbb3af1c, 0x3ff0000000000000), + ]; + + #[test] + fn test_log_matches_glibc() { + for &(x, want) in LOG_VECTORS { + let x = f64::from_bits(x); + assert_eq!(log(x).to_bits(), want, "log({x:e})"); + } + } + + #[test] + fn test_exp_matches_glibc() { + for &(x, want) in EXP_VECTORS { + let x = f64::from_bits(x); + assert_eq!(exp(x).to_bits(), want, "exp({x:e})"); + } + } + + #[test] + fn test_special_values() { + assert_eq!(log(1.0).to_bits(), 0.0f64.to_bits()); + assert_eq!(exp(0.0), 1.0); + assert_eq!(exp(-0.0), 1.0); + assert_eq!(exp(1e-300), 1.0); + assert!(log(0.0).is_infinite()); + assert!(log(-1.0).is_nan()); + assert!(exp(1000.0).is_infinite()); + assert_eq!(exp(-1000.0), 0.0); + } + + #[test] + fn test_close_to_platform_libm() { + // Both are correct to within an ulp or so; this only guards + // against a transcription error in a table or coefficient. + for i in 1..20_000u32 { + let x = f64::from(i) * 0.000_731; + let l = log(x); + assert!( + (l - x.ln()).abs() <= 4.0 * f64::EPSILON * l.abs().max(1e-3), + "log({x})" + ); + let e = exp(x - 7.0); + assert!( + (e / (x - 7.0).exp() - 1.0).abs() <= 4.0 * f64::EPSILON, + "exp({})", + x - 7.0 + ); + } + } +} diff --git a/src/rna/rseqc/glibc_math_tables.rs b/src/rna/rseqc/glibc_math_tables.rs new file mode 100644 index 0000000..fbd7aee --- /dev/null +++ b/src/rna/rseqc/glibc_math_tables.rs @@ -0,0 +1,393 @@ +// Generated from glibc 2.39 sysdeps/ieee754/dbl-64/e_exp_data.c and e_log_data.c. +// Values are the IEEE-754 bit patterns of the original hexadecimal constants. + +pub(super) const EXP_TAB: [u64; 256] = [ + 0x0000000000000000, + 0x3ff0000000000000, + 0x3c9b3b4f1a88bf6e, + 0x3feff63da9fb3335, + 0xbc7160139cd8dc5d, + 0x3fefec9a3e778061, + 0xbc905e7a108766d1, + 0x3fefe315e86e7f85, + 0x3c8cd2523567f613, + 0x3fefd9b0d3158574, + 0xbc8bce8023f98efa, + 0x3fefd06b29ddf6de, + 0x3c60f74e61e6c861, + 0x3fefc74518759bc8, + 0x3c90a3e45b33d399, + 0x3fefbe3ecac6f383, + 0x3c979aa65d837b6d, + 0x3fefb5586cf9890f, + 0x3c8eb51a92fdeffc, + 0x3fefac922b7247f7, + 0x3c3ebe3d702f9cd1, + 0x3fefa3ec32d3d1a2, + 0xbc6a033489906e0b, + 0x3fef9b66affed31b, + 0xbc9556522a2fbd0e, + 0x3fef9301d0125b51, + 0xbc5080ef8c4eea55, + 0x3fef8abdc06c31cc, + 0xbc91c923b9d5f416, + 0x3fef829aaea92de0, + 0x3c80d3e3e95c55af, + 0x3fef7a98c8a58e51, + 0xbc801b15eaa59348, + 0x3fef72b83c7d517b, + 0xbc8f1ff055de323d, + 0x3fef6af9388c8dea, + 0x3c8b898c3f1353bf, + 0x3fef635beb6fcb75, + 0xbc96d99c7611eb26, + 0x3fef5be084045cd4, + 0x3c9aecf73e3a2f60, + 0x3fef54873168b9aa, + 0xbc8fe782cb86389d, + 0x3fef4d5022fcd91d, + 0x3c8a6f4144a6c38d, + 0x3fef463b88628cd6, + 0x3c807a05b0e4047d, + 0x3fef3f49917ddc96, + 0x3c968efde3a8a894, + 0x3fef387a6e756238, + 0x3c875e18f274487d, + 0x3fef31ce4fb2a63f, + 0x3c80472b981fe7f2, + 0x3fef2b4565e27cdd, + 0xbc96b87b3f71085e, + 0x3fef24dfe1f56381, + 0x3c82f7e16d09ab31, + 0x3fef1e9df51fdee1, + 0xbc3d219b1a6fbffa, + 0x3fef187fd0dad990, + 0x3c8b3782720c0ab4, + 0x3fef1285a6e4030b, + 0x3c6e149289cecb8f, + 0x3fef0cafa93e2f56, + 0x3c834d754db0abb6, + 0x3fef06fe0a31b715, + 0x3c864201e2ac744c, + 0x3fef0170fc4cd831, + 0x3c8fdd395dd3f84a, + 0x3feefc08b26416ff, + 0xbc86a3803b8e5b04, + 0x3feef6c55f929ff1, + 0xbc924aedcc4b5068, + 0x3feef1a7373aa9cb, + 0xbc9907f81b512d8e, + 0x3feeecae6d05d866, + 0xbc71d1e83e9436d2, + 0x3feee7db34e59ff7, + 0xbc991919b3ce1b15, + 0x3feee32dc313a8e5, + 0x3c859f48a72a4c6d, + 0x3feedea64c123422, + 0xbc9312607a28698a, + 0x3feeda4504ac801c, + 0xbc58a78f4817895b, + 0x3feed60a21f72e2a, + 0xbc7c2c9b67499a1b, + 0x3feed1f5d950a897, + 0x3c4363ed60c2ac11, + 0x3feece086061892d, + 0x3c9666093b0664ef, + 0x3feeca41ed1d0057, + 0x3c6ecce1daa10379, + 0x3feec6a2b5c13cd0, + 0x3c93ff8e3f0f1230, + 0x3feec32af0d7d3de, + 0x3c7690cebb7aafb0, + 0x3feebfdad5362a27, + 0x3c931dbdeb54e077, + 0x3feebcb299fddd0d, + 0xbc8f94340071a38e, + 0x3feeb9b2769d2ca7, + 0xbc87deccdc93a349, + 0x3feeb6daa2cf6642, + 0xbc78dec6bd0f385f, + 0x3feeb42b569d4f82, + 0xbc861246ec7b5cf6, + 0x3feeb1a4ca5d920f, + 0x3c93350518fdd78e, + 0x3feeaf4736b527da, + 0x3c7b98b72f8a9b05, + 0x3feead12d497c7fd, + 0x3c9063e1e21c5409, + 0x3feeab07dd485429, + 0x3c34c7855019c6ea, + 0x3feea9268a5946b7, + 0x3c9432e62b64c035, + 0x3feea76f15ad2148, + 0xbc8ce44a6199769f, + 0x3feea5e1b976dc09, + 0xbc8c33c53bef4da8, + 0x3feea47eb03a5585, + 0xbc845378892be9ae, + 0x3feea34634ccc320, + 0xbc93cedd78565858, + 0x3feea23882552225, + 0x3c5710aa807e1964, + 0x3feea155d44ca973, + 0xbc93b3efbf5e2228, + 0x3feea09e667f3bcd, + 0xbc6a12ad8734b982, + 0x3feea012750bdabf, + 0xbc6367efb86da9ee, + 0x3fee9fb23c651a2f, + 0xbc80dc3d54e08851, + 0x3fee9f7df9519484, + 0xbc781f647e5a3ecf, + 0x3fee9f75e8ec5f74, + 0xbc86ee4ac08b7db0, + 0x3fee9f9a48a58174, + 0xbc8619321e55e68a, + 0x3fee9feb564267c9, + 0x3c909ccb5e09d4d3, + 0x3feea0694fde5d3f, + 0xbc7b32dcb94da51d, + 0x3feea11473eb0187, + 0x3c94ecfd5467c06b, + 0x3feea1ed0130c132, + 0x3c65ebe1abd66c55, + 0x3feea2f336cf4e62, + 0xbc88a1c52fb3cf42, + 0x3feea427543e1a12, + 0xbc9369b6f13b3734, + 0x3feea589994cce13, + 0xbc805e843a19ff1e, + 0x3feea71a4623c7ad, + 0xbc94d450d872576e, + 0x3feea8d99b4492ed, + 0x3c90ad675b0e8a00, + 0x3feeaac7d98a6699, + 0x3c8db72fc1f0eab4, + 0x3feeace5422aa0db, + 0xbc65b6609cc5e7ff, + 0x3feeaf3216b5448c, + 0x3c7bf68359f35f44, + 0x3feeb1ae99157736, + 0xbc93091fa71e3d83, + 0x3feeb45b0b91ffc6, + 0xbc5da9b88b6c1e29, + 0x3feeb737b0cdc5e5, + 0xbc6c23f97c90b959, + 0x3feeba44cbc8520f, + 0xbc92434322f4f9aa, + 0x3feebd829fde4e50, + 0xbc85ca6cd7668e4b, + 0x3feec0f170ca07ba, + 0x3c71affc2b91ce27, + 0x3feec49182a3f090, + 0x3c6dd235e10a73bb, + 0x3feec86319e32323, + 0xbc87c50422622263, + 0x3feecc667b5de565, + 0x3c8b1c86e3e231d5, + 0x3feed09bec4a2d33, + 0xbc91bbd1d3bcbb15, + 0x3feed503b23e255d, + 0x3c90cc319cee31d2, + 0x3feed99e1330b358, + 0x3c8469846e735ab3, + 0x3feede6b5579fdbf, + 0xbc82dfcd978e9db4, + 0x3feee36bbfd3f37a, + 0x3c8c1a7792cb3387, + 0x3feee89f995ad3ad, + 0xbc907b8f4ad1d9fa, + 0x3feeee07298db666, + 0xbc55c3d956dcaeba, + 0x3feef3a2b84f15fb, + 0xbc90a40e3da6f640, + 0x3feef9728de5593a, + 0xbc68d6f438ad9334, + 0x3feeff76f2fb5e47, + 0xbc91eee26b588a35, + 0x3fef05b030a1064a, + 0x3c74ffd70a5fddcd, + 0x3fef0c1e904bc1d2, + 0xbc91bdfbfa9298ac, + 0x3fef12c25bd71e09, + 0x3c736eae30af0cb3, + 0x3fef199bdd85529c, + 0x3c8ee3325c9ffd94, + 0x3fef20ab5fffd07a, + 0x3c84e08fd10959ac, + 0x3fef27f12e57d14b, + 0x3c63cdaf384e1a67, + 0x3fef2f6d9406e7b5, + 0x3c676b2c6c921968, + 0x3fef3720dcef9069, + 0xbc808a1883ccb5d2, + 0x3fef3f0b555dc3fa, + 0xbc8fad5d3ffffa6f, + 0x3fef472d4a07897c, + 0xbc900dae3875a949, + 0x3fef4f87080d89f2, + 0x3c74a385a63d07a7, + 0x3fef5818dcfba487, + 0xbc82919e2040220f, + 0x3fef60e316c98398, + 0x3c8e5a50d5c192ac, + 0x3fef69e603db3285, + 0x3c843a59ac016b4b, + 0x3fef7321f301b460, + 0xbc82d52107b43e1f, + 0x3fef7c97337b9b5f, + 0xbc892ab93b470dc9, + 0x3fef864614f5a129, + 0x3c74b604603a88d3, + 0x3fef902ee78b3ff6, + 0x3c83c5ec519d7271, + 0x3fef9a51fbc74c83, + 0xbc8ff7128fd391f0, + 0x3fefa4afa2a490da, + 0xbc8dae98e223747d, + 0x3fefaf482d8e67f1, + 0x3c8ec3bc41aa2008, + 0x3fefba1bee615a27, + 0x3c842b94c3a9eb32, + 0x3fefc52b376bba97, + 0x3c8a64a931d185ee, + 0x3fefd0765b6e4540, + 0xbc8e37bae43be3ed, + 0x3fefdbfdad9cbe14, + 0x3c77893b4d91cd9d, + 0x3fefe7c1819e90d8, + 0x3c5305c14160cc89, + 0x3feff3c22b8f71f1, +]; + +/// `(invc, logc)` entries of `__log_data.tab`. +pub(super) const LOG_TAB: [(u64, u64); 128] = [ + (0x3ff734f0c3e0de9f, 0xbfd7cc7f79e69000), + (0x3ff713786a2ce91f, 0xbfd76feec20d0000), + (0x3ff6f26008fab5a0, 0xbfd713e31351e000), + (0x3ff6d1a61f138c7d, 0xbfd6b85b38287800), + (0x3ff6b1490bc5b4d1, 0xbfd65d5590807800), + (0x3ff69147332f0cba, 0xbfd602d076180000), + (0x3ff6719f18224223, 0xbfd5a8ca86909000), + (0x3ff6524f99a51ed9, 0xbfd54f4356035000), + (0x3ff63356aa8f24c4, 0xbfd4f637c36b4000), + (0x3ff614b36b9ddc14, 0xbfd49da7fda85000), + (0x3ff5f66452c65c4c, 0xbfd445923989a800), + (0x3ff5d867b5912c4f, 0xbfd3edf439b0b800), + (0x3ff5babccb5b90de, 0xbfd396ce448f7000), + (0x3ff59d61f2d91a78, 0xbfd3401e17bda000), + (0x3ff5805612465687, 0xbfd2e9e2ef468000), + (0x3ff56397cee76bd3, 0xbfd2941b3830e000), + (0x3ff54725e2a77f93, 0xbfd23ec58cda8800), + (0x3ff52aff42064583, 0xbfd1e9e129279000), + (0x3ff50f22dbb2bddf, 0xbfd1956d2b48f800), + (0x3ff4f38f4734ded7, 0xbfd141679ab9f800), + (0x3ff4d843cfde2840, 0xbfd0edd094ef9800), + (0x3ff4bd3ec078a3c8, 0xbfd09aa518db1000), + (0x3ff4a27fc3e0258a, 0xbfd047e65263b800), + (0x3ff4880524d48434, 0xbfcfeb224586f000), + (0x3ff46dce1b192d0b, 0xbfcf474a7517b000), + (0x3ff453d9d3391854, 0xbfcea4443d103000), + (0x3ff43a2744b4845a, 0xbfce020d44e9b000), + (0x3ff420b54115f8fb, 0xbfcd60a22977f000), + (0x3ff40782da3ef4b1, 0xbfccc00104959000), + (0x3ff3ee8f5d57fe8f, 0xbfcc202956891000), + (0x3ff3d5d9a00b4ce9, 0xbfcb81178d811000), + (0x3ff3bd60c010c12b, 0xbfcae2c9ccd3d000), + (0x3ff3a5242b75dab8, 0xbfca45402e129000), + (0x3ff38d22cd9fd002, 0xbfc9a877681df000), + (0x3ff3755bc5847a1c, 0xbfc90c6d69483000), + (0x3ff35dce49ad36e2, 0xbfc87120a645c000), + (0x3ff34679984dd440, 0xbfc7d68fb4143000), + (0x3ff32f5cceffcb24, 0xbfc73cb83c627000), + (0x3ff3187775a10d49, 0xbfc6a39a9b376000), + (0x3ff301c8373e3990, 0xbfc60b3154b7a000), + (0x3ff2eb4ebb95f841, 0xbfc5737d76243000), + (0x3ff2d50a0219a9d1, 0xbfc4dc7b8fc23000), + (0x3ff2bef9a8b7fd2a, 0xbfc4462c51d20000), + (0x3ff2a91c7a0c1bab, 0xbfc3b08abc830000), + (0x3ff293726014b530, 0xbfc31b996b490000), + (0x3ff27dfa5757a1f5, 0xbfc2875490a44000), + (0x3ff268b39b1d3bbf, 0xbfc1f3b9f879a000), + (0x3ff2539d838ff5bd, 0xbfc160c8252ca000), + (0x3ff23eb7aac9083b, 0xbfc0ce7f57f72000), + (0x3ff22a012ba940b6, 0xbfc03cdc49fea000), + (0x3ff2157996cc4132, 0xbfbf57bdbc4b8000), + (0x3ff201201dd2fc9b, 0xbfbe370896404000), + (0x3ff1ecf4494d480b, 0xbfbd17983ef94000), + (0x3ff1d8f5528f6569, 0xbfbbf9674ed8a000), + (0x3ff1c52311577e7c, 0xbfbadc79202f6000), + (0x3ff1b17c74cb26e9, 0xbfb9c0c3e7288000), + (0x3ff19e010c2c1ab6, 0xbfb8a646b372c000), + (0x3ff18ab07bb670bd, 0xbfb78d01b3ac0000), + (0x3ff1778a25efbcb6, 0xbfb674f145380000), + (0x3ff1648d354c31da, 0xbfb55e0e6d878000), + (0x3ff151b990275fdd, 0xbfb4485cdea1e000), + (0x3ff13f0ea432d24c, 0xbfb333d94d6aa000), + (0x3ff12c8b7210f9da, 0xbfb22079f8c56000), + (0x3ff11a3028ecb531, 0xbfb10e4698622000), + (0x3ff107fbda8434af, 0xbfaffa6c6ad20000), + (0x3ff0f5ee0f4e6bb3, 0xbfadda8d4a774000), + (0x3ff0e4065d2a9fce, 0xbfabbcece4850000), + (0x3ff0d244632ca521, 0xbfa9a1894012c000), + (0x3ff0c0a77ce2981a, 0xbfa788583302c000), + (0x3ff0af2f83c636d1, 0xbfa5715e67d68000), + (0x3ff09ddb98a01339, 0xbfa35c8a49658000), + (0x3ff08cabaf52e7df, 0xbfa149e364154000), + (0x3ff07b9f2f4e28fb, 0xbf9e72c082eb8000), + (0x3ff06ab58c358f19, 0xbf9a55f152528000), + (0x3ff059eea5ecf92c, 0xbf963d62cf818000), + (0x3ff04949cdd12c90, 0xbf9228fb8caa0000), + (0x3ff038c6c6f0ada9, 0xbf8c317b20f90000), + (0x3ff02865137932a9, 0xbf8419355daa0000), + (0x3ff0182427ea7348, 0xbf781203c2ec0000), + (0x3ff008040614b195, 0xbf60040979240000), + (0x3fefe01ff726fa1a, 0x3f6feff384900000), + (0x3fefa11cc261ea74, 0x3f87dc41353d0000), + (0x3fef6310b081992e, 0x3f93cea3c4c28000), + (0x3fef25f63ceeadcd, 0x3f9b9fc114890000), + (0x3feee9c8039113e7, 0x3fa1b0d8ce110000), + (0x3feeae8078cbb1ab, 0x3fa58a5bd001c000), + (0x3fee741aa29d0c9b, 0x3fa95c8340d88000), + (0x3fee3a91830a99b5, 0x3fad276aef578000), + (0x3fee01e009609a56, 0x3fb07598e598c000), + (0x3fedca01e577bb98, 0x3fb253f5e30d2000), + (0x3fed92f20b7c9103, 0x3fb42edd8b380000), + (0x3fed5cac66fb5cce, 0x3fb606598757c000), + (0x3fed272caa5ede9d, 0x3fb7da76356a0000), + (0x3fecf26e3e6b2ccd, 0x3fb9ab434e1c6000), + (0x3fecbe6da2a77902, 0x3fbb78c7bb0d6000), + (0x3fec8b266d37086d, 0x3fbd431332e72000), + (0x3fec5894bd5d5804, 0x3fbf0a3171de6000), + (0x3fec26b533bb9f8c, 0x3fc067152b914000), + (0x3febf583eeece73f, 0x3fc147858292b000), + (0x3febc4fd75db96c1, 0x3fc2266ecdca3000), + (0x3feb951e0c864a28, 0x3fc303d7a6c55000), + (0x3feb65e2c5ef3e2c, 0x3fc3dfc33c331000), + (0x3feb374867c9888b, 0x3fc4ba366b7a8000), + (0x3feb094b211d304a, 0x3fc5933928d1f000), + (0x3feadbe885f2ef7e, 0x3fc66acd2418f000), + (0x3feaaf1d31603da2, 0x3fc740f8ec669000), + (0x3fea82e63fd358a7, 0x3fc815c0f51af000), + (0x3fea5740ef09738b, 0x3fc8e92954f68000), + (0x3fea2c2a90ab4b27, 0x3fc9bb3602f84000), + (0x3fea01a01393f2d1, 0x3fca8bed1c2c0000), + (0x3fe9d79f24db3c1b, 0x3fcb5b515c01d000), + (0x3fe9ae2505c7b190, 0x3fcc2967ccbcc000), + (0x3fe9852ef297ce2f, 0x3fccf635d5486000), + (0x3fe95cbaeea44b75, 0x3fcdc1bd3446c000), + (0x3fe934c69de74838, 0x3fce8c01b8cfe000), + (0x3fe90d4f2f6752e6, 0x3fcf5509c0179000), + (0x3fe8e6528effd79d, 0x3fd00e6c121fb800), + (0x3fe8bfce9fcc007c, 0x3fd071b80e93d000), + (0x3fe899c0dabec30e, 0x3fd0d46b9e867000), + (0x3fe87427aa2317fb, 0x3fd13687334bd000), + (0x3fe84f00acb39a08, 0x3fd1980d67234800), + (0x3fe82a49e8653e55, 0x3fd1f8ffe0cc8000), + (0x3fe8060195f40260, 0x3fd2595fd7636800), + (0x3fe7e22563e0a329, 0x3fd2b9300914a800), + (0x3fe7beb377dcb5ad, 0x3fd3187210436000), + (0x3fe79baa679725c2, 0x3fd377266dec1800), + (0x3fe77907f2170657, 0x3fd3d54ffbaf3000), + (0x3fe756cadbd6130c, 0x3fd432eee32fe000), +]; diff --git a/src/rna/rseqc/mod.rs b/src/rna/rseqc/mod.rs index 967a280..21b60d5 100644 --- a/src/rna/rseqc/mod.rs +++ b/src/rna/rseqc/mod.rs @@ -5,6 +5,8 @@ pub mod accumulators; pub mod common; +mod glibc_math; +mod glibc_math_tables; pub mod plots; pub mod bam_stat; diff --git a/src/rna/rseqc/tin.rs b/src/rna/rseqc/tin.rs index c20b3c0..e6315ef 100644 --- a/src/rna/rseqc/tin.rs +++ b/src/rna/rseqc/tin.rs @@ -16,6 +16,7 @@ use indexmap::IndexMap; use log::debug; use rand::Rng; +use super::glibc_math; use crate::gtf::{Gene, Transcript}; // =================================================================== @@ -1466,7 +1467,8 @@ fn compute_tin(coverage: &[u32], n_total_positions: usize) -> f64 { let total: f64 = nonzero.iter().sum(); - // Shannon entropy over non-zero positions only (matching upstream) + // Shannon entropy over non-zero positions only (matching upstream's + // `shannon_entropy`): the terms are added left to right, in position order. let entropy: f64 = if total == 0.0 { 0.0 } else { @@ -1474,14 +1476,14 @@ fn compute_tin(coverage: &[u32], n_total_positions: usize) -> f64 { .iter() .map(|&c| { let p = c / total; - -p * p.ln() + -p * glibc_math::log(p) }) .sum() }; // Upstream: tin = 100 * exp(entropy) / l // where l = total number of sampled positions (not just nonzero) - 100.0 * entropy.exp() / n_total_positions as f64 + 100.0 * glibc_math::exp(entropy) / n_total_positions as f64 } /// An aligned (M/=/X) block of a read. @@ -1854,6 +1856,32 @@ mod tests { assert_eq!(positions[2], 103); } + // Coverage vectors for which glibc's generic (non-FMA) build of log/exp gives a + // TIN one ulp away from its FMA build, with the TIN (bits) that tin.py prints on + // a CPU with FMA (glibc 2.39, Python 3.12). + const TIN_PY_TINS: &[(&str, u64)] = &[ + ( + "3 2 5 40 1 8 1 0 0 3 200 13 2 8 13 200 1 0 5 0 2 200 1 3 13 8 13 200 5 1 3 5 200 200 0 3 13 5 3 13 0 2 2 2 8 2 2 40 200 13 3 5 8 3 40 13 5 0 8 1 2 2 13 0 0 0 13 2 200 8 13 1 8 13 5 200 8 40 3 40 0 0 5 1 1 40 8 3 13 0 5 200 13 13 40 0 1 13 13 1", + 0x403a1a3c59ec9a81, + ), + ( + "3 1 1 8 0 0 8 2 5 0 0 2 3 3 8 40 8 13 13 0 1 0 1 40 0 2 3 13 40 1 40 3 5 5 5 0 40 2 0 200 3 0 200 2 3 2 1 1 1 40 40 5 8 0 5 2 8 40 0 8 8 3 13 8 0 0 8 5 40 1 5 40 40 13 1 3 0 0 13 13 40 13 1 0 0 200 3 2 3 3 8 8 2 0 40 13 2 0 1 1", + 0x403b67ba9234a965, + ), + ( + "1 0 13 2 200 0 40 3 0 1 0 200 2 40 2 8 2 5 8 5 0 40 8 5 40 5 200 3 13 1 5 3 40 3 8 40 200 5 1 8 0 5 2 0 0 0 40 5 13 200 40 5 0 0 3 13 3 200 0 5 13 13 5 5 0 40 0 3 0 200 3 1 2 40 13 8 0 2 40 13 200 0 2 13 40 3 2 1 13 5 200 8 0 200 13 1 1 1 8 8", + 0x403b6ed5fcbf1c13, + ), + ]; + + #[test] + fn test_compute_tin_matches_tin_py() { + for &(cov, want) in TIN_PY_TINS { + let cov: Vec = cov.split(' ').map(|v| v.parse().unwrap()).collect(); + assert_eq!(compute_tin(&cov, cov.len()).to_bits(), want); + } + } + #[test] fn test_compute_tin_uniform() { // Uniform coverage → max TIN