Skip to content

fix(tin): make TIN identical to tin.py on every platform - #202

Closed
rhassaine wants to merge 1 commit into
seqeralabs:mainfrom
rhassaine:fix/tin-exact
Closed

rhassaine wants to merge 1 commit into
seqeralabs:mainfrom
rhassaine:fix/tin-exact

Conversation

@rhassaine

Copy link
Copy Markdown
Contributor

tin.py computes TIN with Python's math.log / math.exp, which come from glibc. glibc picks a different build of these at runtime depending on the CPU (an FMA build on x86-64 CPUs with FMA+AVX2, a generic build otherwise), and the two round differently in the last bit for about 1 in 10,000 inputs. RustQC called Rust's ln / exp, so its TIN values inherited the same machine dependence, and differed from tin.py in the last digit for roughly 1 transcript in several thousand depending on where each ran (macOS and musl differ again).

This adds a port of glibc 2.39's FMA log and exp, using mul_add exactly where glibc's FMA code fuses, and uses it for TIN. That's the build every x86-64 CPU from the last decade uses, and aarch64 glibc gives the same bits, so RustQC's TIN is now identical on every platform and equal to tin.py's on both.

  • The port matches glibc and Python's math.log / math.exp bit for bit on 7.25M inputs (random, near 1, subnormal, boundary exponents); as a control, glibc's generic build differs from it on 1,238 of them.
  • tin.xls and summary.txt are byte-identical to RSeQC 5.0.4 tin.py on four datasets at 1, 2 and 6 threads.
  • The one case not reproduced is tin.py on an old x86-64 CPU without FMA (pre-2013), where about 1 TIN in 1,000 differs in the last digit.

🤖 Generated with Claude 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 <noreply@anthropic.com>
@rhassaine

Copy link
Copy Markdown
Contributor Author

Closing per review: this only matters for the last digit of ~1 TIN value in 8,000 on machines whose libm doesn't match glibc's FMA path. RustQC's Docker image (glibc) already matches tin.py exactly on any modern x86-64 or ARM machine, so the benchmarks keep an exact TIN check without vendoring glibc's log/exp.

@rhassaine rhassaine closed this Nov 26, 2026
@rhassaine
rhassaine deleted the fix/tin-exact branch November 27, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant