[libre-riscv-dev] correctly-rounded fp functions algorithm

Jacob Lifshay programmerjake at gmail.com
Tue Oct 22 21:26:01 BST 2019


I had an idea:
when we're trying to make a implementation of a correctly-rounded fp
function, we should be aiming to approximate the rounded results rather
than the exact mathematical result, since that allows us to have better
approximations with more wiggle room in the approximations, since the
result we want is the rounded result, not the exact mathematical result.

based on that idea, I thought of an algorithm for building a set of
mathematical lines (or other curves) which produce the exactly-rounded
results for all inputs: basically, take a line equation and adjust it until
it produces correctly-rounded results for the maximum number of input
values, then repeat starting at the first input that the previous line
failed on. this can be done by solving the equations for the next input
that produces an out-of-range output rather than just iterating over all
possible inputs.

to work with all rounding modes, the output just needs to be put in one of
the following ranges:
distance from preceeding fp value (pfpv) (in lsb digits) is:
* exactly 0 -- all modes round to pfpv
* between 0 and 1/2 -- to neg inf, to nearest even, to nearest max all
round to pfpv, to pos inf rounds to nextafter(pfpv)
* exactly 1/2 -- to neg inf rounds to pfpv, to pos inf rounds to
nextafter(pfpv), to nearest max/even rounds to whichever of pfpv and
nextafter(pfpv) is farthest-from-zero/even respectively
* between 1/2 and 1 -- to pos inf, to nearest even, to nearest max all
round to nextafter(pfpv), to neg inf rounds to pfpv

to zero mode behaves like to neg inf for positive results and to pos inf
for negative results.

a lot of transcendental functions only hit the two exact cases for a few
easily-decodable inputs since all other outputs are irrational: exp(0),
sinpi(n/2), cospi(n/2), tanpi(n/4), log(1), log2(2^n), log10(10^n), etc.
where n is an integer

thought I should write this all down before I forget.

Jacob


More information about the libre-riscv-dev mailing list