Summary:  Slow dbl64 sin/cos/sincos for special values  

Product:  glibc  Reporter:  Petr Cervenka <grugh> 
Component:  math  Assignee:  Siddhesh Poyarekar <siddhesh> 
Status:  RESOLVED FIXED  
Severity:  enhancement  CC:  carlos, glibcbugs, john.wilkinson, markus, siddhesh, vincentsrcware, wdijkstr 
Priority:  P2  Flags:  fweimer:
security

Version:  unspecified  
Target Milestone:  2.34  
Host:  x86_64unknownlinuxgnu  Target:  
Build:  Last reconfirmed: 
Description
Petr Cervenka
20080221 09:10:38 UTC
Most of the double routines in libm come from IBM accurate matematical library, which ensures <= 0.5ulp error. Trigonometric etc. functions are computed using floating point computations, but if the possible error from that is too high, it uses slower multiprecision computation to guarantee ultimate precise result. Guess you just picked some worstcase values. i386 uses the nonprecise hardware instructions instead, so doesn't guarantee the <= 0.5ulp precision. Subject: Re: Slow sine function for special values on AMD64
 second attempt
On Thu, 21 Feb 2008, jakub at redhat dot com wrote:
> which ensures <= 0.5ulp error. Trigonometric etc. functions are
> computed using floating point computations, but if the possible error
> from that is too high, it uses slower multiprecision computation to
> guarantee ultimate precise result. Guess you just picked some worstcase
> values.
Note that the crlibm developers were willing to contribute their code, an
advantage of which is *much* better worstcase performance.
Yeah, I'm aware of crlibm, I think if it proves itself that it won't be much slower on average, has the same ultimate precision guarantees and faster worstcases, I don't see a reason why it can't be integrated. It will be a lot of work to integrate it though. Is there any compile flag or #define, which can disable the <=0.5 ulp precision and the math sin function will use only the fast builtin fp intructions? For our realtime software it is necessary to be "quick", the ultra precision has low priority. Now we are using a workaround: I can put the original argument to long double variable and call sinl function with long double result. Both, the new argument and the result, have to be volatile to disable the compiler optimization of it (probably uses the "fast" sin instead). Results of sin(0.93340582292648832662962377071381)  distr function value result_type printf_format  32 sin 0.80365140438773496889268699305831 double "%.32g" 32 sinl 0.80365140438773496889268699305831 double "%.32g" 32 sinl 0.80365140438773491338153576180048 long double "%.32Lg 64 sin 0.80365140438773485787038453054265 double "%.32g" (~ 5.5511151231257827021181583404541e17 difference from 80bit value) 64 sinl 0.80365140438773496889268699305831 double "%.32g" (~ +5.5511151231257827021181583404541e17 difference from 80bit value ) 64 sinl 0.80365140438773491338153576180048 long double "%.32Lg" I'm not the only one with such problems: http://sources.redhat.com/bugzilla/show_bug.cgi?id=5997 I assume that for the 64bit distribution (x86_64), it should use sin and sinf from i386 arch (sysdeps\i386\fpu\s_sin.S and sysdeps\i386\fpu\s_sinf.S) and only sinl implementation is explicit x86_64. But the sin and sinf are now used as software versions (IBM library). And it's usually bit slower, sometimes MUCH MORE slower (1000x). IBM library is perhaps only emergency implementation (if there is no hw support) and it's not used for "better" (<= 0.5ULP) precision. "The First Step is to Admit You Have a Problem!" Confirmed with current sources. Suspending until a faster correctly rounding implementation (such as that proposed in http://gcc.gnu.org/ml/gcc/201202/msg00298.html ) is available as this is probably not amenable to a simple local fix. FWIW, the function now runs much faster after the multiple precision improvements. The worst case is only about a 100 times slower now instead of 1000 times. I've not looked yet, but I think there is a case for capping maximum precision for worst case computation for sin (and all trigonometric functions) as well, so this could get even better. Opening this since I've been working on improvements to the multiple precision bits that should have positive effect here. In fact as I mentioned in comment 7, improvements are already evident. Since optimization patches can go on forever, I'm going to put a cap on it for the resolution of this bug. The cap is to implement findings of [1] if applicable. [1] http://perso.enslyon.fr/jeanmichel.muller/TMDworstcases.pdf I have also come across a very similar issue on i7 Intel platforms, please see bug 16531. Calls to cos can take around 0.15 ms, 1000 times their normal time, which is a serious problem for the realtime system we are developing. *** Bug 16531 has been marked as a duplicate of this bug. *** (In reply to John Wilkinson from comment #9) > I have also come across a very similar issue on i7 Intel platforms, please > see bug 16531. Calls to cos can take around 0.15 ms, 1000 times their normal > time, which is a serious problem for the realtime system we are developing. The default libm functions never guarantee constant runtime. You will have this same problem for many of the functions provided by the library. However we are working on enhancing libm to include something like what you're looking for. Please have look at and comment: https://sourceware.org/glibc/wiki/libm Really the issue for sin/cos/sincos is the same, so retitling the bug. *** Bug 14412 has been marked as a duplicate of this bug. *** (In reply to Carlos O'Donell from comment #11) > However we are working on enhancing libm to include something like what > you're looking for. Please have look at and comment: > https://sourceware.org/glibc/wiki/libm Thanks that looks useful. Is there a release schedule? (In reply to John Wilkinson from comment #14) > (In reply to Carlos O'Donell from comment #11) > > However we are working on enhancing libm to include something like what > > you're looking for. Please have look at and comment: > > https://sourceware.org/glibc/wiki/libm > > Thanks that looks useful. Is there a release schedule? Not yet. I'll update the wiki when I can commit resources. That doesn't stop others from joining in the discussion, or adding notes to the wiki like use cases and requirements. Simple workaround to use fast computation is to use functions from spec. header similar to following: #ifndef FAST_MATH_H #define FAST_MATH_H #include <cmath> inline double fast_sin(long double x) { return sinl(x); } inline double fast_cos(long double x) { return cosl(x); } inline double fast_tan(long double x) { return tanl(x); } inline double fast_asin(long double x) { return asinl(x); } inline double fast_acos(long double x) { return acosl(x); } inline double fast_atan(long double x) { return atanl(x); } inline double fast_atan2(long double x, long double y) { return atan2l(x, y); } inline double fast_sinh(long double x) { return sinhl(x); } inline double fast_cosh(long double x) { return coshl(x); } inline double fast_asinh(long double x) { return asinhl(x); } inline double fast_acosh(long double x) { return acoshl(x); } inline double fast_pow(long double x, long double y) { return powl(x, y); } inline double fast_sqrt(long double x) { return sqrtl(x); } inline double fast_exp(long double x) { return expl(x); } inline double fast_log(long double x) { return logl(x); } inline double fast_log10(long double x) { return log10l(x); } #endif /* FAST_MATH_H */ (In reply to Petr Cervenka from comment #16) > Simple workaround to use fast computation is to use functions from spec. > header similar to following: [long double versions: sinl, etc.] This is not quite correct. These long double versions currently have a lower worstcase time (this might change in the future), but in average they are slower than the current double versions, with a factor around 5  6 from some tests on my machine. So, use this workaround only if you want a better worstcase time, e.g. for realtime system (this is your case[1], isn't it?). [1] http://www.xenomai.org/pipermail/xenomai/2008February/012416.html The current library is slow, and libraries such as CRlibm could greatly improve things, but hardtoround cases would always be slower than the average cases. So, the best implementation depends on the user's application. I suppose that most users would be happy with a *good* correctly rounded implementation since the loss due to correct rounding should hardly be noticeable *in average* compared to an implementation with just a very good accuracy (something close to 0.5 ulp). Then there are users who accept to sacrifice correct rounding and accuracy for faster functions. IMHO, the question is whether the GNU libc should implement such variants and provide a way for the user to select them (but this could mean other two variants or more[2]) or the user should build his own library based on his own requirements, e.g. with tools like MetaLibm[3] or other future tools. [2] This should cover the accuracy for small arguments, but users may also have different requirements concerning the range reduction (large arguments). [3] http://www.metalibm.org/ As I outlined in <https://sourceware.org/ml/libcalpha/201307/msg00444.html>, and as described in the discussion of accuracy goals in the glibc manual, I don't think functions such as sin and cos should try to be correctly rounded, only functions such as crsin and crcos (if added). This was fixed by a series of commits starting from 19a8b9a300f2f1f0012aff0f2b70b09430f50d9e All slow paths have now been removed from all math functions. If you log in to Bugzilla with your @gcc.gnu.org address you should have access to mark bugs RESOLVED / FIXED with target milestone set to the first release that will have the fix, rather than just commenting that they are fixed. The last commit to remove slow paths appears to be this one: commit f67f9c9af228f6b84579cb8c86312d3a7a206a55 Author: Anssi Hannula <anssi.hannula@bitwise.fi> Date: Mon Jan 27 12:45:10 2020 +0200 ieee754: Remove slow paths from asin and acos So this was finished in 2.33. I just noticed that the last slow path removal patches went in with Wilco's comment 19. Adjusting target milestone. 