Java port of CORE-MATH: correctly rounded (0.5 ulp) mathematical functions, translated from the C sources so that the results are bit-for-bit identical to upstream, with one exception: the bit pattern of a NaN result is not part of the promise; the promise is only that the result is a NaN.
Only the two binary floating-point formats that Java has types for are ported:
binary32 (float) and binary64 (double). CORE-MATH also implements the same
function set for binary16, bfloat16, binary80, binary128 and a
format-generic variant; none of those is covered here.
All 42 functions that CORE-MATH provides for binary32 and all 41 that it
provides for binary64 are translated, 83 entry points in all. Every one of them
has been checked bit for bit against a local build of the C sources, and every
one of them ships with a JUnit class fed by a committed vector file, so a
checkout builds and verifies with nothing but a JDK: 134,939 test cases that run
in about ten seconds under mvn verify.
CORE-MATH provides correctly rounded implementations of the mathematical functions in IEEE 754-2019, written in C and released under the MIT license. A correctly rounded function has exactly one correct result for every input, independent of language and compiler, so a faithful port produces the same bits as the C code, including signed zeros, infinities and subnormals.
This is an unofficial port, not affiliated with the CORE-MATH project or with INRIA. Where the Java code and the C code disagree, the C code is right. If upstream itself is wrong, the bug is reported upstream rather than worked around here: https://gitlab.inria.fr/core-math/core-math/-/issues.
The port currently follows commit
0ce0cbc15935e24c656280e5a46c34e89f301b7a of the CORE-MATH repository: that is
the commit the translations were taken from, and the one recorded in every file
header. It is a snapshot, not a fixed policy: a newer upstream is picked up by
replaying its diffs, as AGENTS.md describes.
The translation is mechanical: same control flow, same constant tables in the
same order, same variable names, so that anyone can put the C file and the Java
file next to each other and compare them by eye. AGENTS.md holds the
rules that keep it that way.
| Function | binary32 (float) |
binary64 (double) |
Upstream source |
|---|---|---|---|
sin |
translated, tested | translated, tested | src/binary{32,64}/sin |
cos |
translated, tested | translated, tested | src/binary{32,64}/cos |
tan |
translated, tested | translated, tested | src/binary{32,64}/tan |
exp |
translated, tested | translated, tested | src/binary{32,64}/exp |
log |
translated, tested | translated, tested | src/binary{32,64}/log |
pow |
translated, tested | translated, tested | src/binary{32,64}/pow |
atan |
translated, tested | translated, tested | src/binary{32,64}/atan |
atan2 |
translated, tested | translated, tested | src/binary{32,64}/atan2 |
log2 |
translated, tested | translated, tested | src/binary{32,64}/log2 |
log10 |
translated, tested | translated, tested | src/binary{32,64}/log10 |
asin |
translated, tested | translated, tested | src/binary{32,64}/asin |
acos |
translated, tested | translated, tested | src/binary{32,64}/acos |
sinh |
translated, tested | translated, tested | src/binary{32,64}/sinh |
cosh |
translated, tested | translated, tested | src/binary{32,64}/cosh |
tanh |
translated, tested | translated, tested | src/binary{32,64}/tanh |
cbrt |
translated, tested | translated, tested | src/binary{32,64}/cbrt |
exp2 |
translated, tested | translated, tested | src/binary{32,64}/exp2 |
log1p |
translated, tested | translated, tested | src/binary{32,64}/log1p |
expm1 |
translated, tested | translated, tested | src/binary{32,64}/expm1 |
sinpi |
translated, tested | translated, tested | src/binary{32,64}/sinpi |
cospi |
translated, tested | translated, tested | src/binary{32,64}/cospi |
hypot |
translated, tested | translated, tested | src/binary{32,64}/hypot |
asinh |
translated, tested | translated, tested | src/binary{32,64}/asinh |
acosh |
translated, tested | translated, tested | src/binary{32,64}/acosh |
atanh |
translated, tested | translated, tested | src/binary{32,64}/atanh |
erf |
translated, tested | translated, tested | src/binary{32,64}/erf |
erfc |
translated, tested | translated, tested | src/binary{32,64}/erfc |
tanpi |
translated, tested | translated, tested | src/binary{32,64}/tanpi |
atanpi |
translated, tested | translated, tested | src/binary{32,64}/atanpi |
asinpi |
translated, tested | translated, tested | src/binary{32,64}/asinpi |
acospi |
translated, tested | translated, tested | src/binary{32,64}/acospi |
rsqrt |
translated, tested | translated, tested | src/binary{32,64}/rsqrt |
exp10 |
translated, tested | translated, tested | src/binary{32,64}/exp10 |
exp2m1 |
translated, tested | translated, tested | src/binary{32,64}/exp2m1 |
exp10m1 |
translated, tested | translated, tested | src/binary{32,64}/exp10m1 |
log10p1 |
translated, tested | translated, tested | src/binary{32,64}/log10p1 |
log2p1 |
translated, tested | translated, tested | src/binary{32,64}/log2p1 |
tgamma |
translated, tested | translated, tested | src/binary{32,64}/tgamma |
lgamma |
translated, tested | translated, tested | src/binary{32,64}/lgamma |
sincos |
translated, tested | translated, tested | src/binary{32,64}/sincos |
atan2pi |
translated, tested | translated, tested | src/binary{32,64}/atan2pi |
compound |
translated, tested | not in CORE-MATH | src/binary32/compound |
Every function has been checked bit for bit against a local build of the C
sources (see Verification): the upstream .wc worst-case file where one
exists, five million pseudo-random inputs per format, and a directed set that
covers the special values and every comparison threshold of the translation.
The same three sets are what the JUnit vectors hold, function by function as
they are committed.
Everything in the table is written and compiles. CORE-MATH keeps growing, and this port follows it: when upstream adds a function, a format, or a new version of one of these files, the change is replayed here with the same mechanical translation. Nothing beyond the table is planned on this side.
- JDK 11 or newer. The code is compiled with
--release 11and uses no API newer than Java 11. - Maven 3.6 or newer to build and test.
The API. The scope table below lists the functions and says in which binary format each of them exists:
import io.github.ntysdd.coremath.CrMath;
float a = CrMath.sin(1.0f); // cr_sinf, binary32
double b = CrMath.sin(1.0); // cr_sin, binary64
float c = CrMath.pow(2.0f, 3.0f); // cr_powf, binary32
double d = CrMath.pow(2.0, 3.0); // cr_pow, binary64
double e = CrMath.hypot(3.0, 4.0); // cr_hypot, binary64The two overloads are separate implementations.
The C implementation is the reference. This project claims only that the Java code returns the bits the C code returns.
Every function is checked against a local build of upstream, and the JUnit
suite repeats that check on the vectors committed under
src/test/resources/vectors. Three sets make up a function's vectors, and for
the one-argument binary32 functions a fourth pass adds the double-rounding
frontier to the directed section:
- The directed cases: the special values of
AGENTS.mdsection 5.1, both sides of every comparison threshold of the translation, and the exceptional entries of the tables the entry point consults. - A uniform sample of the upstream
.wcworst cases, where upstream has such a file. - A deterministic pseudo-random sweep: a 64-bit xorshift seeded with
0x9e3779b97f4a7c15, the seed the generator and the test share. The test recomputes the inputs and fails if a row does not match them, so a hand-edited sweep cannot pass unnoticed. - For the one-argument functions, the binary32 double-rounding frontier: the
inputs of the whole binary32 domain where the float result and the binary64
result rounded to float disagree, found by
tools/cmref --frontier. Those are the inputs where the correctly rounded float result is decided by the last bit, the closest thing binary32 has to an upstream.wcfile, and they go into the directed section. The scan walks the domain of a one-argument function; the two-argument ones (atan2,atan2pi,hypot,pow,compound) have 2^64 argument pairs and their headers saydouble rounding: no scanrather than quoting a count.
A vector file has one section per set, introduced by a #--- marker, with one
case per line: the input arguments and the expected results as hexadecimal bit
patterns. Its header names the upstream commit, the reference build and how
each section was produced. A line whose expected result is a NaN asserts that
the port returns a NaN, not a particular bit pattern, because the bit pattern of
a NaN is not part of the promise.
The vectors are committed as plain text, so mvn verify needs nothing but a
JDK. Regenerating them needs a local CORE-MATH checkout and a C compiler:
CORE_MATH=/path/to/core-math sh tools/cmref/build.sh
python3 tools/mkvectors.py sin # one function, both formats
python3 tools/mkvectors.py --alltools/cmref/cmref.c is one small driver over the upstream entry points, built
with the reference flags (-O2 -ffp-contract=off -mfma). Neither the driver
nor its built binary is part of the Maven build, and the binary is not
committed. tools/gentests.py writes the JUnit classes from a small table of function
names and special values, so that the classes that differ in nothing but those
stay consistent; it is a development aid as well and never runs under Maven.
Both generators are idempotent: their --all form reproduces what is committed,
byte for byte, so a diff after regenerating one function is the change that
function actually needs.
Large runs, such as the exhaustive sweep of all 2^32 binary32 inputs or a long random sweep for binary64, stay offline in chunks: the same driver prints the expected block, and a scratch runner compares it against the port. None of that is part of the default build.
This is a rough guide to the order of magnitude, not a benchmark. The port is
plain Java, so it runs at Java speed and not at the speed of a CPU instruction.
On one x86-64 machine with JDK 27, over five runs, the binary64 entry points
land in the same range as StrictMath: faster for exp, log and pow,
slower for sin, tanh and cbrt, and faster still for log10, whose
StrictMath counterpart is unsteady. Against Math, whose functions the JIT
turns into CPU instructions, the port costs 1.2 to 2.5 times as much, apart
from log and log10, which it wins. The binary32 entry points are the
clearer win: they beat what a caller has to write today,
(float) Math.f((double) x), by a factor of one and a half to three, and
without the double rounding. In absolute terms a call costs a few nanoseconds
for the elementary functions and about 25 ns for tgamma.
None of this is a promise, and the repository carries no benchmark code: the figures came from a throwaway harness on one machine, and another JVM or another CPU will move them.
The port is pinned to commit 0ce0cbc. Upstream has since rewritten its
binary64 cosine, and the rewrite is faster than the version translated here, so
cos is the one entry point whose speed is behind what upstream ships now.
The commands are:
mvn -q verify # compile, run tests, build the jar
mvn -q -Dtest=SinFTest test # one test class
mvn -q -Dtest=SinTest testCorrespondence between the two trees:
| CORE-MATH | core-math-java |
|---|---|
src/binary32/sin/sinf.c (cr_sinf) |
io.github.ntysdd.coremath.binary32.SinF |
src/binary64/sin/sin.c (cr_sin) |
io.github.ntysdd.coremath.binary64.Sin |
src/binary32/cos/cosf.c (cr_cosf) |
io.github.ntysdd.coremath.binary32.CosF |
src/binary64/cos/cos.c (cr_cos) |
io.github.ntysdd.coremath.binary64.Cos |
src/binary32/tan/tanf.c (cr_tanf) |
io.github.ntysdd.coremath.binary32.TanF |
src/binary64/tan/tan.c (cr_tan) |
io.github.ntysdd.coremath.binary64.Tan |
src/binary32/exp/expf.c (cr_expf) |
io.github.ntysdd.coremath.binary32.ExpF |
src/binary64/exp/exp.c (cr_exp) |
io.github.ntysdd.coremath.binary64.Exp |
src/binary32/log/logf.c (cr_logf) |
io.github.ntysdd.coremath.binary32.LogF |
src/binary64/log/log.c (cr_log) and dint.h |
io.github.ntysdd.coremath.binary64.Log |
src/binary32/pow/powf.c (cr_powf) |
io.github.ntysdd.coremath.binary32.PowF |
src/binary64/pow/pow.c (cr_pow), pow.h, dint.h and qint.h |
io.github.ntysdd.coremath.binary64.Pow |
src/binary32/atan/atanf.c (cr_atanf) |
io.github.ntysdd.coremath.binary32.AtanF |
src/binary64/atan/atan.c (cr_atan) |
io.github.ntysdd.coremath.binary64.Atan |
src/binary32/atan2/atan2f.c (cr_atan2f) |
io.github.ntysdd.coremath.binary32.Atan2F |
src/binary64/atan2/atan2.c (cr_atan2) and tint.h |
io.github.ntysdd.coremath.binary64.Atan2 |
src/binary32/log2/log2f.c (cr_log2f) |
io.github.ntysdd.coremath.binary32.Log2F |
src/binary64/log2/log2.c (cr_log2) |
io.github.ntysdd.coremath.binary64.Log2 |
src/binary32/log10/log10f.c (cr_log10f) |
io.github.ntysdd.coremath.binary32.Log10F |
src/binary64/log10/log10.c (cr_log10) and dint.h |
io.github.ntysdd.coremath.binary64.Log10 |
src/binary32/asin/asinf.c (cr_asinf) |
io.github.ntysdd.coremath.binary32.AsinF |
src/binary64/asin/asin.c (cr_asin) |
io.github.ntysdd.coremath.binary64.Asin |
src/binary32/acos/acosf.c (cr_acosf) |
io.github.ntysdd.coremath.binary32.AcosF |
src/binary64/acos/acos.c (cr_acos) |
io.github.ntysdd.coremath.binary64.Acos |
src/binary32/sinh/sinhf.c (cr_sinhf) |
io.github.ntysdd.coremath.binary32.SinhF |
src/binary64/sinh/sinh.c (cr_sinh) |
io.github.ntysdd.coremath.binary64.Sinh |
src/binary32/cosh/coshf.c (cr_coshf) |
io.github.ntysdd.coremath.binary32.CoshF |
src/binary64/cosh/cosh.c (cr_cosh) |
io.github.ntysdd.coremath.binary64.Cosh |
src/binary32/tanh/tanhf.c (cr_tanhf) |
io.github.ntysdd.coremath.binary32.TanhF |
src/binary64/tanh/tanh.c (cr_tanh) |
io.github.ntysdd.coremath.binary64.Tanh |
src/binary32/cbrt/cbrtf.c (cr_cbrtf) |
io.github.ntysdd.coremath.binary32.CbrtF |
src/binary64/cbrt/cbrt.c (cr_cbrt) |
io.github.ntysdd.coremath.binary64.Cbrt |
src/binary32/exp2/exp2f.c (cr_exp2f) |
io.github.ntysdd.coremath.binary32.Exp2F |
src/binary64/exp2/exp2.c (cr_exp2) |
io.github.ntysdd.coremath.binary64.Exp2 |
src/binary32/log1p/log1pf.c (cr_log1pf) |
io.github.ntysdd.coremath.binary32.Log1pF |
src/binary64/log1p/log1p.c (cr_log1p) |
io.github.ntysdd.coremath.binary64.Log1p |
src/binary32/expm1/expm1f.c (cr_expm1f) |
io.github.ntysdd.coremath.binary32.Expm1F |
src/binary64/expm1/expm1.c (cr_expm1) |
io.github.ntysdd.coremath.binary64.Expm1 |
src/binary32/sinpi/sinpif.c (cr_sinpif) |
io.github.ntysdd.coremath.binary32.SinPiF |
src/binary64/sinpi/sinpi.c (cr_sinpi) |
io.github.ntysdd.coremath.binary64.SinPi |
src/binary32/cospi/cospif.c (cr_cospif) |
io.github.ntysdd.coremath.binary32.CosPiF |
src/binary64/cospi/cospi.c (cr_cospi) |
io.github.ntysdd.coremath.binary64.CosPi |
src/binary32/hypot/hypotf.c (cr_hypotf) |
io.github.ntysdd.coremath.binary32.HypotF |
src/binary64/hypot/hypot.c (cr_hypot) |
io.github.ntysdd.coremath.binary64.Hypot |
src/binary32/asinh/asinhf.c (cr_asinhf) |
io.github.ntysdd.coremath.binary32.AsinhF |
src/binary64/asinh/asinh.c (cr_asinh) |
io.github.ntysdd.coremath.binary64.Asinh |
src/binary32/acosh/acoshf.c (cr_acoshf) |
io.github.ntysdd.coremath.binary32.AcoshF |
src/binary64/acosh/acosh.c (cr_acosh) |
io.github.ntysdd.coremath.binary64.Acosh |
src/binary32/atanh/atanhf.c (cr_atanhf) |
io.github.ntysdd.coremath.binary32.AtanhF |
src/binary64/atanh/atanh.c (cr_atanh) |
io.github.ntysdd.coremath.binary64.Atanh |
src/binary32/erf/erff.c (cr_erff) |
io.github.ntysdd.coremath.binary32.ErfF |
src/binary64/erf/erf.c (cr_erf) |
io.github.ntysdd.coremath.binary64.Erf |
src/binary32/erfc/erfcf.c (cr_erfcf) |
io.github.ntysdd.coremath.binary32.ErfcF |
src/binary64/erfc/erfc.c (cr_erfc) |
io.github.ntysdd.coremath.binary64.Erfc |
src/binary32/tanpi/tanpif.c (cr_tanpif) |
io.github.ntysdd.coremath.binary32.TanPiF |
src/binary64/tanpi/tanpi.c (cr_tanpi) |
io.github.ntysdd.coremath.binary64.TanPi |
src/binary32/atanpi/atanpif.c (cr_atanpif) |
io.github.ntysdd.coremath.binary32.AtanPiF |
src/binary64/atanpi/atanpi.c (cr_atanpi) |
io.github.ntysdd.coremath.binary64.AtanPi |
src/binary32/asinpi/asinpif.c (cr_asinpif) |
io.github.ntysdd.coremath.binary32.AsinPiF |
src/binary64/asinpi/asinpi.c (cr_asinpi) |
io.github.ntysdd.coremath.binary64.AsinPi |
src/binary32/acospi/acospif.c (cr_acospif) |
io.github.ntysdd.coremath.binary32.AcosPiF |
src/binary64/acospi/acospi.c (cr_acospi) |
io.github.ntysdd.coremath.binary64.AcosPi |
src/binary32/atan2pi/atan2pif.c (cr_atan2pif) |
io.github.ntysdd.coremath.binary32.Atan2PiF |
src/binary64/atan2pi/atan2pi.c (cr_atan2pi) |
io.github.ntysdd.coremath.binary64.Atan2Pi |
src/binary32/rsqrt/rsqrtf.c (cr_rsqrtf) |
io.github.ntysdd.coremath.binary32.RsqrtF |
src/binary64/rsqrt/rsqrt.c (cr_rsqrt) |
io.github.ntysdd.coremath.binary64.Rsqrt |
src/binary32/exp10/exp10f.c (cr_exp10f) |
io.github.ntysdd.coremath.binary32.Exp10F |
src/binary64/exp10/exp10.c (cr_exp10) |
io.github.ntysdd.coremath.binary64.Exp10 |
src/binary32/exp2m1/exp2m1f.c (cr_exp2m1f) |
io.github.ntysdd.coremath.binary32.Exp2M1F |
src/binary64/exp2m1/exp2m1.c (cr_exp2m1) |
io.github.ntysdd.coremath.binary64.Exp2M1 |
src/binary32/exp10m1/exp10m1f.c (cr_exp10m1f) |
io.github.ntysdd.coremath.binary32.Exp10M1F |
src/binary64/exp10m1/exp10m1.c (cr_exp10m1) |
io.github.ntysdd.coremath.binary64.Exp10M1 |
src/binary32/log10p1/log10p1f.c (cr_log10p1f) |
io.github.ntysdd.coremath.binary32.Log10P1F |
src/binary64/log10p1/log10p1.c (cr_log10p1) and dint.h |
io.github.ntysdd.coremath.binary64.Log10P1 |
src/binary32/log2p1/log2p1f.c (cr_log2p1f) |
io.github.ntysdd.coremath.binary32.Log2P1F |
src/binary64/log2p1/log2p1.c (cr_log2p1) and dint_log2p1.h |
io.github.ntysdd.coremath.binary64.Log2P1 |
src/binary32/tgamma/tgammaf.c (cr_tgammaf) |
io.github.ntysdd.coremath.binary32.TgammaF |
src/binary64/tgamma/tgamma.c (cr_tgamma) |
io.github.ntysdd.coremath.binary64.Tgamma |
src/binary32/lgamma/lgammaf.c (cr_lgammaf) |
io.github.ntysdd.coremath.binary32.LgammaF |
src/binary64/lgamma/lgamma.c (cr_lgamma) |
io.github.ntysdd.coremath.binary64.Lgamma |
src/binary32/sincos/sincosf.c (cr_sincosf) |
io.github.ntysdd.coremath.binary32.SincosF |
src/binary64/sincos/sincos.c (cr_sincos) |
io.github.ntysdd.coremath.binary64.Sincos |
src/binary32/compound/compoundf.c (cr_compoundf) |
io.github.ntysdd.coremath.binary32.CompoundF |
| public entry points | io.github.ntysdd.coremath.CrMath |
AGENTS.md holds the rules for changing code here: translation
conventions, the C to Java mapping table, test requirements and commit format.
MIT, see LICENSE.
The copyright notices of the CORE-MATH contributors are kept in each translated file, together with the upstream path and commit the file came from. Do not remove them.