Commit Graph

25 Commits

Author SHA1 Message Date
Stephen Heumann 89664d2921 Slightly improve tgamma calculation for x < 8.
Previously, 1-4 low-order bits of the input value were essentially ignored when calculating the numerator, but used to some degree when calculating the denominator. This would lead to the calculated tgamma values decreasing slightly over the range of several consecutive input values (when they should increase). Now, the low-order bits of the input value are effectively just rounded away. This should give slightly more accurate results, and greatly reduces the frequency of cases where consecutive output values go in the wrong direction.
2022-12-24 21:59:52 -06:00
Stephen Heumann 5985e7d774 Implement tgamma (c99).
This uses an approximation based on the Stirling series for large enough x (for which it is highly accurate). For smaller x, identities are used to express gamma(x) in terms of gamma(x+1) or gamma(1-x), ultimately letting the Stirling series approximation be used.
2022-12-24 20:20:40 -06:00
Stephen Heumann 88e764f72d Implement the erf and erfc functions (C99).
This implementation is based on the approximations given in the following paper:

W. J. Cody, Rational Chebyshev Approximations for the Error Function, Mathematics of Computation, Vol. 23, No. 107 (Jul., 1969), pp. 631-637.

Per the paper, the approximations have maximal relative error of 6e-19 or lower (although I have not verified what the figure is for this actual implementation).

See also Cody's FORTRAN implementation based on the same approach:

https://netlib.org/specfun/erf
2022-12-17 22:25:53 -06:00
Stephen Heumann 997e430562 Implement asinh().
This is similar to the approach recommended in Apple Numerics Manual Ch. 9, except that there is an added case for large values that would otherwise cause an overflow or spuriously report underflow.
2021-12-24 15:56:36 -06:00
Stephen Heumann b62940404f Implement atanh().
This basically follows the approach recommended in Apple Numerics Manual Ch. 9.
2021-12-23 18:30:52 -06:00
Stephen Heumann 818707ed8c Use a more accurate implementation of cbrt().
The previous simple one could be wrong in several low-order digits due to the inaccuracy in the representation of the exponent (1/3). This version effectively breaks the number up into the form a*8^b, computes the cube root of 8^b exactly (i.e. 2^b), and uses the slightly inaccurate exponentiation only for a.
2021-12-21 19:11:18 -06:00
Stephen Heumann a45f531fe6 Implement hypot().
This uses the obvious calculation, except with scaling to avoid unnecessary overflow/underflow.

There is a discussion of hypot implementations in C. Borges, An Improved Algorithm for hypot(a,b) (https://arxiv.org/pdf/1904.09481.pdf). This implementation is similar to the "Naive (Unfused)" version discussed in that paper. As the paper notes, it is possible to get better accuracy by adding a correction term, but the "naive" version is already reasonably good, so we skip the correction in the interest of code size and speed.
2021-12-20 21:52:48 -06:00
Stephen Heumann b01800ff77 Fix rounding issues introduced by SANE bug workarounds.
The lrint functions could give the wrong result for negative numbers in upward/downward rounding modes. Casts to comp could also have different rounding behavior.
2021-11-30 20:19:57 -06:00
Stephen Heumann b6690c4826 Implement acosh().
This is basically the implementation recommended in Apple Numerics Manual Ch. 9, except that there is an added case for large values that would otherwise cause an overflow.
2021-11-30 19:15:54 -06:00
Stephen Heumann eddf778f09 Implement llround(). 2021-11-28 18:30:20 -06:00
Stephen Heumann 66cfa0d406 Remove unnecessary code in lround(). 2021-11-28 18:30:01 -06:00
Stephen Heumann e00c21dd70 Work around bug in FX2C and FX2L.
These SANE operations can sometimes return incorrect values for certain negative integers such as -2147483648 and -53021371269120 (numbers with at least 16 low-order zero bits in their two's-complement representation). To work around this, we now avoid calling FX2C or FX2L on negative numbers, generally by saving and restoring the sign separately.

These workarounds are used in several of the new <math.h> rounding functions, and also for code that converts floating-point values to comp or long long. There are some places in SysFloat that should be patched similarly, so we may still hit this problem in certain situations until that is done.
2021-11-28 14:18:27 -06:00
Stephen Heumann 503182e435 Initial implementation of lround().
This should work, and mostly does. However, it is affected by a bug in FX2L (and FX2C) which can sometimes give the wrong results for certain negative integers (such as -2147483648). I believe this can occur when at least the lower 16 bits if the integer (in two's-complement representation) are zeros.
2021-11-27 17:52:46 -06:00
Stephen Heumann 88a7bbebcc Implement round().
This is a bit more complex than other rounding functions, because it rounds to nearest but always away from zero in halfway cases, which is not a rounding direction directly supported by SANE.
2021-11-27 15:55:54 -06:00
Stephen Heumann d08773af0d Implement nextafter and nexttoward.
Unlike most of the math functions, these actually have separate implementations for float/double/long double.
2021-11-26 12:47:02 -06:00
Stephen Heumann 6364d0f48f Implement llrint. 2021-11-23 21:16:12 -06:00
Stephen Heumann ce05615a63 Implement fmax and fmin. 2021-11-23 18:54:18 -06:00
Stephen Heumann 14908ebcd6 Implement the nan() function.
This parses the NaN code string itself, but it should give equivalent behavior to the SANE parser.
2021-11-22 21:59:50 -06:00
Stephen Heumann c025bba177 Implement nearbyint and fdim. 2021-11-22 19:25:25 -06:00
Stephen Heumann 2334443437 Implement scalbln.
This differs from scalbn in that the exponent has type long. When scaling an extended value, exponents slightly outside the range of int can actually be used meaningfully. We address this by doing multiple SCALBX calls (at most 2) in a loop.
2021-11-21 20:10:36 -06:00
Stephen Heumann 268892b671 Add float and long double versions of functions in SysFloat.
Most of these actually just jump to the existing functions, since they really use extended precision anyway. The exception is the modf functions, which need a separate implementation for each type because they store a value through a pointer to that type.
2021-11-21 14:34:52 -06:00
Stephen Heumann 3ec8a8797f Implement some of the math functions added in C99.
The functions implemented so far are largely the ones that map (nearly) directly to SANE calls.

Note that C99 specifies separate float/double/long double versions of each of these functions, but under ORCA/C they generally use the same code.
2021-11-20 19:24:51 -06:00
Stephen Heumann fb5683a14d Add a function to implement the FP comparison macros in <math.h>.
These macros differ from the normal comparison operators in that they will not signal invalid due to the operands being unordered. However, this implementation will still signal invalid for SNaNs. That is clearly OK according to the wording in draft C23. C17 and earlier do not mention that possibility, but they do not really specify the behavior of SNaNs in general.
2021-11-02 21:56:30 -05:00
Stephen Heumann 9e727697d3 Add a helper function to get the sign bit. 2021-03-09 00:24:26 -06:00
Stephen Heumann 98cfd4e831 Add floating-point number classification functions.
These are the internal routines used by the fpclassify() macro.
2021-03-08 23:44:44 -06:00