about summary refs log tree commit diff
path: root/src/math
Commit message (Collapse)AuthorAgeFilesLines
* math: add (obsolete) bsd drem and finite functionsSzabolcs Nagy2013-11-214-0/+20
|
* math: lgamma cleanup (simpler sin(pi*x) for the negative case)Szabolcs Nagy2013-11-214-202/+110
| | | | | | | | | * simplify sin_pi(x) (don't care about inexact here, the result is inexact anyway, and x is not so small to underflow) * in lgammal add the previously removed special case for x==1 and x==2 (to fix the sign of zero in downward rounding mode) * only define lgammal on supported long double platforms * change tgamma so the generated code is a bit smaller
* math: extensive log*.c cleanupSzabolcs Nagy2013-10-2814-583/+369
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The log, log2 and log10 functions share a lot of code and to a lesser extent log1p too. A small part of the code was kept separately in __log1p.h, but since it did not capture much of the common code and it was inlined anyway, it did not solve the issue properly. Now the log functions have significant code duplication, which may be resolved later, until then they need to be modified together. logl, log10l, log2l, log1pl: * Fix the sign when the return value should be -inf. * Remove the volatile hack from log10l (seems unnecessary) log1p, log1pf: * Change the handling of small inputs: only |x|<2^-53 is special (then it is enough to return x with the usual subnormal handling) this fixes the sign of log1p(0) in downward rounding. * Do not handle the k==0 case specially (other than skipping the elaborate argument reduction) * Do not handle 1+x close to power-of-two specially (this code was used rarely, did not give much speed up and the precision wasn't better than the general) * Fix the correction term formula (c=1-(u-x) was used incorrectly when x<1 but (double)(x+1)==2, this was not a critical issue) * Use the exact same method for calculating log(1+f) as in log (except in log1p the c correction term is added to the result). log, logf, log10, log10f, log2, log2f: * Use double_t and float_t consistently. * Now the first part of log10 and log2 is identical to log (until the return statement, hopefully this makes maintainence easier). * Most special case formulas were removed (close to power-of-two and k==0 cases), they increase the code size without providing precision or performance benefits (and obfuscate the code). Only x==1 is handled specially so in downward rounding mode the sign of zero is correct (the general formula happens to give -0). * For x==0 instead of -1/0.0 or -two54/0.0, return -1/(x*x) to force raising the exception at runtime. * Arg reduction code is changed (slightly simplified) * The thresholds for arg reduction to [sqrt(2)/2,sqrt(2)] are now consistently the [0x3fe6a09e00000000,0x3ff6a09dffffffff] and the [0x3f3504f3,0x3fb504f2] intervals for double and float reductions respectively (the exact threshold values are not critical) * Remove the obsolete comment for the FLT_EVAL_METHOD!=0 case in log2f (The same code is used for all eval methods now, on i386 slightly simpler code could be used, but we have asm there anyway) all: * Fix signed int arithmetics (using unsigned for bitmanipulation) * Fix various comments
* math: fix rare underflow issue in fmaSzabolcs Nagy2013-10-073-13/+55
| | | | | the issue is described in commits 1e5eb73545ca6cfe8b918798835aaf6e07af5beb and ffd8ac2dd50f99c3c83d7d9d845df9874ec3e7d5
* math: use sqrtl if FLT_EVAL_METHOD==2 in acosh and acoshfSzabolcs Nagy2013-10-072-0/+13
| | | | this makes acosh slightly more precise around 1.0 on i386
* math: remove an unused variable from modflSzabolcs Nagy2013-10-061-1/+0
|
* math: remove code duplication in erfl found by clang analyzerSzabolcs Nagy2013-10-041-13/+2
| | | | | erfl had some superflous code left around after the last erf cleanup. the issue was reported by Alexander Monakov
* math: remove a useless assignment in lgammal found by clang analyzerSzabolcs Nagy2013-10-041-2/+2
| | | | the issue was reported by Alexander Monakov
* fix x86_64 lrintl asm, againRich Felker2013-09-131-2/+2
| | | | | | | | the underlying problem was not incorrect sign extension (fixed in the previous commit to this file by nsz) but that code that treats "long" as 32-bit was copied blindly from i386 to x86_64. now lrintl is identical to llrintl on x86_64, as it should be.
* math: remove STRICT_ASSIGN from exp2f (see previous commit)Szabolcs Nagy2013-09-061-1/+1
|
* math: remove STRICT_ASSIGN macroSzabolcs Nagy2013-09-0610-12/+13
| | | | | | | | | | | | | gcc did not always drop excess precision according to c99 at assignments before version 4.5 even if -std=c99 was requested which caused badly broken mathematical functions on i386 when FLT_EVAL_METHOD!=0 but STRICT_ASSIGN was not used consistently and it is worked around for old compilers with -ffloat-store so it is no longer needed the new convention is to get the compiler respect c99 semantics and when excess precision is not harmful use float_t or double_t or to specialize code using FLT_EVAL_METHOD
* math: support invalid ld80 representations in fpclassifySzabolcs Nagy2013-09-051-2/+4
| | | | | | | apparently gnulib requires invalid long double representations to be handled correctly in printf so we classify them according to how the fpu treats them: bad inf is nan, bad nan is nan, bad normal is nan and bad subnormal/zero is minimal normal
* math: fix atanh (overflow and underflow issues)Szabolcs Nagy2013-09-053-14/+37
| | | | | | | in atanh exception handling was left to the called log functions, but the argument to those functions could underflow or overflow. use double_t and float_t to avoid some useless stores on x86
* math: remove libc.h include from libm.hSzabolcs Nagy2013-09-054-1/+5
| | | | libc.h is only for weak_alias so include it directly where it is used
* math: fix acoshf on negative valuesSzabolcs Nagy2013-09-052-7/+8
| | | | | | acosh(x) is invalid for x<1, acoshf tried to be clever using signed comparisions to handle all x<2 the same way, but the formula was wrong on large negative values.
* math: fix expm1l on x86_64 (avoid underflow for large negative x)Szabolcs Nagy2013-09-053-3/+13
| | | | copy the fix from i386: return -1 instead of exp2l(x)-1 when x <= -65
* math: fix lrintl.s on x86_64 (use movslq to signextend the result)Szabolcs Nagy2013-09-051-1/+1
|
* math: fix exp2l asm on x86 (raise underflow correctly)Szabolcs Nagy2013-09-052-67/+78
| | | | | | | | | | | | | | | | there were two problems: * omitted underflow on subnormal results: exp2l(-16383.5) was calculated as sqrt(2)*2^-16384, the last bits of sqrt(2) are zero so the down scaling does not underflow eventhough the result is in subnormal range * spurious underflow for subnormal inputs: exp2l(0x1p-16400) was evaluated as f2xm1(x)+1 and f2xm1 raised underflow (because inexact subnormal result) the first issue is fixed by raising underflow manually if x is in (-32768,-16382] and not integer (x-0x1p63+0x1p63 != x) the second issue is fixed by treating x in (-0x1p64,0x1p64) specially for these fixes the special case handling was completely rewritten
* math: cosmetic cleanup (use explicit union instead of fshape and dshape)Szabolcs Nagy2013-09-0510-100/+84
|
* math: remove *_WORD64 macros from libm.hSzabolcs Nagy2013-09-051-13/+13
| | | | only fma used these macros and the explicit union is clearer
* math: long double fix (use ldshape union)Szabolcs Nagy2013-09-058-51/+24
| | | | | | * use new ldshape union consistently * add ld128 support to frexpl * simplify sqrtl comment (ld64 is not just arm)
* math: use float_t and double_t in scalbnf and scalbnSzabolcs Nagy2013-09-052-16/+20
| | | | | remove STRICT_ASSIGN (c99 semantics is assumed) and use the conventional union to prepare the scaling factor (so libm.h is no longer needed)
* math: fix remaining old long double code (erfl, fmal, lgammal, scalbnl)Szabolcs Nagy2013-09-055-93/+65
| | | | | in lgammal don't handle 1 and 2 specially, in fma use the new ldshape union instead of ld80 one.
* math: cbrt cleanup and long double fixSzabolcs Nagy2013-09-053-72/+59
| | | | | | | * use float_t and double_t * cleanup subnormal handling * bithacks according to the new convention (ldshape for long double and explicit unions for float and double)
* math: fix underflow in exp*.c and long double handling in exp2lSzabolcs Nagy2013-09-058-182/+139
| | | | | | | | | | | | * don't care about inexact flag * use double_t and float_t (faster, smaller, more precise on x86) * exp: underflow when result is zero or subnormal and not -inf * exp2: underflow when result is zero or subnormal and not exact * expm1: underflow when result is zero or subnormal * expl: don't underflow on -inf * exp2: fix incorrect comment * expm1: simplify special case handling and overflow properly * expm1: cleanup final scaling and fix negative left shift ub (twopk)
* math: long double trigonometric cleanup (cosl, sinl, sincosl, tanl)Szabolcs Nagy2013-09-058-236/+228
| | | | | | | | | | | | | | ld128 support was added to internal kernel functions (__cosl, __sinl, __tanl, __rem_pio2l) from freebsd (not tested, but should be a good start for when ld128 arch arrives) __rem_pio2l had some code cleanup, the freebsd ld128 code seems to gather the results of a large reduction with precision loss (fixed the bug but a todo comment was added for later investigation) the old copyright was removed from the non-kernel wrapper functions (cosl, sinl, sincosl, tanl) since these are trivial and the interesting parts and comments had been already rewritten.
* math: long double inverse trigonometric cleanup (acosl, asinl, atanl, atan2l)Szabolcs Nagy2013-09-056-103/+180
| | | | | | * added ld128 support from freebsd fdlibm (untested) * using new ldshape union instead of IEEEl2bits * inexact status flag is not supported
* math: rewrite hypotSzabolcs Nagy2013-09-053-324/+135
| | | | | | | | | | | | | | | | | | method: if there is a large difference between the scale of x and y then the larger magnitude dominates, otherwise reduce x,y so the argument of sqrt (x*x+y*y) does not overflow or underflow and calculate the argument precisely using exact multiplication. If the argument has less error than 1/sqrt(2) ~ 0.7 ulp, then the result has less error than 1 ulp in nearest rounding mode. the original fdlibm method was the same, except it used bit hacks instead of dekker-veltkamp algorithm, which is problematic for long double where different representations are supported. (the new hypot and hypotl code should be smaller and faster on 32bit cpu archs with fast fpu), the new code behaves differently in non-nearest rounding, but the error should be still less than 2ulps. ld80 and ld128 are supported
* math: rewrite remainder functions (remainder, remquo, fmod, modf)Szabolcs Nagy2013-09-0511-1010/+472
| | | | | | | | | * results are exact * modfl follows truncl (raises inexact flag spuriously now) * modf and modff only had cosmetic cleanup * remainder is just a wrapper around remquo now * using iterative shift+subtract for remquo and fmod * ld80 and ld128 are supported as well
* math: rewrite rounding functions (ceil, floor, trunc, round, rint)Szabolcs Nagy2013-09-0515-905/+274
| | | | | | | | | | * faster, smaller, cleaner implementation than the bit hacks of fdlibm * use arithmetics like y=(double)(x+0x1p52)-0x1p52, which is an integer neighbor of x in all rounding modes (0<=x<0x1p52) and only use bithacks when that's faster and smaller (for float it usually is) * the code assumes standard excess precision handling for casts * long double code supports both ld80 and ld128 * nearbyint is not changed (it is a wrapper around rint)
* math: fix logb(-0.0) in downward rounding modeSzabolcs Nagy2013-09-053-6/+6
| | | | | use -1/(x*x) instead of -1/(x+0) to return -inf, -0+0 is -0 in downward rounding mode
* math: ilogb cleanupSzabolcs Nagy2013-09-053-16/+43
| | | | | | | * consistent code style * explicit union instead of typedef for double and float bit access * turn FENV_ACCESS ON to make 0/0.0f raise invalid flag * (untested) ld128 version of ilogbl (used by logbl which has ld128 support)
* long double cleanup, initial commitSzabolcs Nagy2013-09-056-70/+61
| | | | | | | | new ldshape union, ld128 support is kept, code that used the old ldshape union was rewritten (IEEEl2bits union of freebsd libm is not touched yet) ld80 __fpclassifyl no longer tries to handle invalid representation
* some initial math asm for armhf (fabs[f] and sqrt[f])Rich Felker2013-08-1612-0/+32
|
* fix build of x86_64 expl assemblyRich Felker2013-08-161-1/+1
| | | | | apparently this label change was not carried over when adapting the changes from the i386 version.
* math: fix pow(x,-1) to raise underflow properlySzabolcs Nagy2013-08-151-2/+14
| | | | | | if FLT_EVAL_METHOD!=0 check if (double)(1/x) is subnormal and not a power of 2 (if 1/x is power of 2 then either it is exact or the long double to double rounding already raised inexact and underflow)
* math: fix i386 atan2.s to raise underflow for subnormal resultsSzabolcs Nagy2013-08-152-2/+24
|
* math: clean up atan2.cSzabolcs Nagy2013-08-154-103/+73
| | | | | | | | | * remove volatile hacks * don't care about inexact flag for now (removed all the +-tiny) * fix atanl to raise underflow properly * remove signed int arithmetics * use pi/2 instead of pi_o_2 (gcc generates the same code, which is not correct, but it does not matter: we mainly care about nearest rounding)
* math: fix x86 asin, atan, exp, log1p to raise underflowSzabolcs Nagy2013-08-156-3/+98
| | | | | | underflow is raised by an inexact subnormal float store, since subnormal operations are slow, check the underflow flag and skip the store if it's already raised
* math: fix x86 expl.s to raise underflow and clean up special case handlingSzabolcs Nagy2013-08-152-45/+31
|
* math: fix asin, atan, log1p, tanh to raise underflow on subnormalSzabolcs Nagy2013-08-159-26/+39
| | | | | | | | | | | | | | | | | | for these functions f(x)=x for small inputs, because f(0)=0 and f'(0)=1, but for subnormal values they should raise the underflow flag (required by annex F), if they are approximated by a polynomial around 0 then spurious underflow should be avoided (not required by annex F) all these functions should raise inexact flag for small x if x!=0, but it's not required by the standard and it does not seem a worthy goal, so support for it is removed in some cases. raising underflow: - x*x may not raise underflow for subnormal x if FLT_EVAL_METHOD!=0 - x*x may raise spurious underflow for normal x if FLT_EVAL_METHOD==0 - in case of double subnormal x, store x as float - in case of float subnormal x, store x*x as float
* math: fix tgamma to raise underflow for large negative valuesSzabolcs Nagy2013-08-151-0/+1
|
* math: fix pow(0,-inf) to raise divbyzero flagSzabolcs Nagy2013-08-152-2/+2
|
* math: minor scalbn*.c simplificationSzabolcs Nagy2013-08-153-18/+10
|
* add missing erfcl wrapper for archs where long double is plain doubleRich Felker2013-07-281-0/+4
|
* math: add fma TODO comments about the underflow issueSzabolcs Nagy2013-05-193-2/+14
| | | | | | | | | The underflow exception is not raised correctly in some cornercases (see previous fma commit), added comments with examples for fmaf, fmal and non-x86 fma. In fmaf store the result before returning so it has the correct precision when FLT_EVAL_METHOD!=0
* math: fix two fma issues (only affects non-nearest rounding mode, x86)Szabolcs Nagy2013-05-191-4/+38
| | | | | | | | | | | | | | | | | | | | | | 1) in downward rounding fma(1,1,-1) should be -0 but it was 0 with gcc, the code was correct but gcc does not support FENV_ACCESS ON so it used common subexpression elimination where it shouldn't have. now volatile memory access is used as a barrier after fesetround. 2) in directed rounding modes there is no double rounding issue so the complicated adjustments done for nearest rounding mode are not needed. the only exception to this rule is raising the underflow flag: assume "small" is an exactly representible subnormal value in double precision and "verysmall" is a much smaller value so that (long double)(small plus verysmall) == small then (double)(small plus verysmall) raises underflow because the result is an inexact subnormal, but (double)(long double)(small plus verysmall) does not because small is not a subnormal in long double precision and it is exact in double precision. now this problem is fixed by checking inexact using fenv when the result is subnormal
* math: sin cos cleanupSzabolcs Nagy2013-05-1810-112/+128
| | | | | | | | * use unsigned arithmetics * use unsigned to store arg reduction quotient (so n&3 is understood) * remove z=0.0 variables, use literal 0 * raise underflow and inexact exceptions properly when x is small * fix spurious underflow in tanl
* math: tan cleanupsSzabolcs Nagy2013-05-186-106/+80
| | | | | | | | | | | | | | | | * use unsigned arithmetics on the representation * store arg reduction quotient in unsigned (so n%2 would work like n&1) * use different convention to pass the arg reduction bit to __tan (this argument used to be 1 for even and -1 for odd reduction which meant obscure bithacks, the new n&1 is cleaner) * raise inexact and underflow flags correctly for small x (tanl(x) may still raise spurious underflow for small but normal x) (this exception raising code increases codesize a bit, similar fixes are needed in many other places, it may worth investigating at some point if the inexact and underflow flags are worth raising correctly as this is not strictly required by the standard) * tanf manual reduction optimization is kept for now * tanl code path is cleaned up to follow similar logic to tan and tanf
* math: use double_t for temporaries to avoid stores on i386Szabolcs Nagy2013-05-1521-28/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When FLT_EVAL_METHOD!=0 (only i386 with x87 fp) the excess precision of an expression must be removed in an assignment. (gcc needs -fexcess-precision=standard or -std=c99 for this) This is done by extra load/store instructions which adds code bloat when lot of temporaries are used and it makes the result less precise in many cases. Using double_t and float_t avoids these issues on i386 and it makes no difference on other archs. For now only a few functions are modified where the excess precision is clearly beneficial (mostly polynomial evaluations with temporaries). object size differences on i386, gcc-4.8: old new __cosdf.o 123 95 __cos.o 199 169 __sindf.o 131 95 __sin.o 225 203 __tandf.o 207 151 __tan.o 605 499 erff.o 1470 1416 erf.o 1703 1649 j0f.o 1779 1745 j0.o 2308 2274 j1f.o 1602 1568 j1.o 2286 2252 tgamma.o 1431 1424 math/*.o 64164 63635