Commit Graph

14 Commits

Author SHA1 Message Date
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