> From: Greg Steuck <gne...@openbsd.org> > Date: Mon, 10 Jan 2022 20:59:17 -0800 > > Greg Steuck <gne...@openbsd.org> writes: > > > This failure can be reduced to a trivial program which does change > > its behavior for the worse if s_cos.S is taken out: > > > > #include <math.h> > > #include <stdio.h> > > > > int main(int a, char**b) { > > double y = -0.34061437849088045332; > > printf("cos(%lf)=%le delta=%e\n", y, cos(y), 0.94254960031831729956 - > > cos(y)); > > } > > > > In HEAD: > > > > cos(-0.340614)=9.425496e-01 delta=-1.110223e-16 > > > > while with the patch below: > > > > cos(-0.340614)=9.425496e-01 delta=0.000000e+00 > > As Daniel noted, I swapped the cases. The HEAD is at 0.0 delta whereas > the patch used to make it worse. > > I went looking for why things are better on FreeBSD and they have a > different (simpler) implementation of cos. I copied it over. Given the > common provenance, I expect the copyright situation to be unambiguous.
I think you will also need the changes done in FreeBSD commit 4339c67c485f. > With the two patches things look almost universally better in > regress/libm. I attached both logs from amd64. > > Anybody has ideas for other tests that make sense to do? Maybe people > can help me run regress on less common platforms? > > Thanks > Greg > > >From a0b065bd3f5d48786f77f654dfb53cbf2617b0b3 Mon Sep 17 00:00:00 2001 > From: Greg Steuck <g...@nest.cx> > Date: Mon, 10 Jan 2022 20:22:07 -0800 > Subject: [PATCH 1/2] Copy cos(3) software implementation from FreeBSD-13 > > The result passes more tests from msun suite. In particular, > testacc(cos, -0.34061437849088045332L, 0.94254960031831729956L, > ALL_STD_EXCEPT, FE_INEXACT); > matches instead of being 1e-16 off. > --- > lib/libm/src/k_cos.c | 45 ++++++++++++++++++-------------------------- > lib/libm/src/s_cos.c | 6 +++++- > 2 files changed, 23 insertions(+), 28 deletions(-) > > diff --git a/lib/libm/src/k_cos.c b/lib/libm/src/k_cos.c > index 8f3882b6a00..0839243e90c 100644 > --- a/lib/libm/src/k_cos.c > +++ b/lib/libm/src/k_cos.c > @@ -36,13 +36,17 @@ > * ~ cos(x) - x*y, > * a correction term is necessary in cos(x) and hence > * cos(x+y) = 1 - (x*x/2 - (r - x*y)) > - * For better accuracy when x > 0.3, let qx = |x|/4 with > - * the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125. > - * Then > - * cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)). > - * Note that 1-qx and (x*x/2-qx) is EXACT here, and the > - * magnitude of the latter is at least a quarter of x*x/2, > - * thus, reducing the rounding error in the subtraction. > + * For better accuracy, rearrange to > + * cos(x+y) ~ w + (tmp + (r-x*y)) > + * where w = 1 - x*x/2 and tmp is a tiny correction term > + * (1 - x*x/2 == w + tmp exactly in infinite precision). > + * The exactness of w + tmp in infinite precision depends on w > + * and tmp having the same precision as x. If they have extra > + * precision due to compiler bugs, then the extra precision is > + * only good provided it is retained in all terms of the final > + * expression for cos(). Retention happens in all cases tested > + * under FreeBSD, so don't pessimize things by forcibly clipping > + * any extra precision in w. > */ > > #include "math.h" > @@ -60,25 +64,12 @@ C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, > 0xBE8838D4 */ > double > __kernel_cos(double x, double y) > { > - double a,hz,z,r,qx; > - int32_t ix; > - GET_HIGH_WORD(ix,x); > - ix &= 0x7fffffff; /* ix = |x|'s high word*/ > - if(ix<0x3e400000) { /* if x < 2**27 */ > - if(((int)x)==0) return one; /* generate inexact */ > - } > + double hz,z,r,w; > + > z = x*x; > - r = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*C6))))); > - if(ix < 0x3FD33333) /* if |x| < 0.3 */ > - return one - (0.5*z - (z*r - x*y)); > - else { > - if(ix > 0x3fe90000) { /* x > 0.78125 */ > - qx = 0.28125; > - } else { > - INSERT_WORDS(qx,ix-0x00200000,0); /* x/4 */ > - } > - hz = 0.5*z-qx; > - a = one-qx; > - return a - (hz - (z*r-x*y)); > - } > + w = z*z; > + r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6)); > + hz = 0.5*z; > + w = one-hz; > + return w + (((one-w)-hz) + (z*r-x*y)); > } > diff --git a/lib/libm/src/s_cos.c b/lib/libm/src/s_cos.c > index 8b923d5fe61..1406504e9ab 100644 > --- a/lib/libm/src/s_cos.c > +++ b/lib/libm/src/s_cos.c > @@ -57,7 +57,11 @@ cos(double x) > > /* |x| ~< pi/4 */ > ix &= 0x7fffffff; > - if(ix <= 0x3fe921fb) return __kernel_cos(x,z); > + if(ix <= 0x3fe921fb) { > + if(ix<0x3e46a09e) /* if x < 2**-27 * sqrt(2) */ > + if(((int)x)==0) return 1.0; /* generate inexact */ > + return __kernel_cos(x,z); > + } > > /* cos(Inf or NaN) is NaN */ > else if (ix>=0x7ff00000) return x-x; > -- > 2.34.1 > > >From 7461948f0e7ff41e5b3b9288d98edf13cb296742 Mon Sep 17 00:00:00 2001 > From: Greg Steuck <g...@nest.cx> > Date: Sun, 9 Jan 2022 13:45:51 -0800 > Subject: [PATCH 2/2] Unplug assembly implementations of trig functions on x86 > platforms > > The same change was done by NetBSD some time back as: > > Disable x87 implementations of sin, cos, tan. > > The x87 hardware uses a bad approximation to pi for argument > reduction, and consequently yields bad answers for inputs near pi or > pi/2. > --- > lib/libm/Makefile | 9 ++++----- > regress/lib/libm/msun/Makefile | 1 - > 2 files changed, 4 insertions(+), 6 deletions(-) > > diff --git a/lib/libm/Makefile b/lib/libm/Makefile > index 47cd94cac06..cd39bbe5c48 100644 > --- a/lib/libm/Makefile > +++ b/lib/libm/Makefile > @@ -22,11 +22,10 @@ ARCH_SRCS = e_acos.S e_asin.S e_atan2.S e_exp.S e_fmod.S > e_log.S e_log10.S \ > e_sqrtl.S \ > invtrig.c \ > s_atan.S s_atanf.S s_ceil.S s_ceilf.S s_copysign.S s_copysignf.S \ > - s_cos.S s_cosf.S s_floor.S s_floorf.S \ > + s_floor.S s_floorf.S \ > s_log1p.S s_log1pf.S s_logb.S s_logbf.S \ > s_llrint.S s_llrintf.S s_lrint.S s_lrintf.S s_rint.S s_rintf.S\ > - s_scalbnf.S s_significand.S s_significandf.S \ > - s_sin.S s_sinf.S s_tan.S s_tanf.S > + s_scalbnf.S s_significand.S s_significandf.S > .elif (${MACHINE_ARCH} == "amd64") > .PATH: ${.CURDIR}/arch/amd64 > CPPFLAGS+=-I${.CURDIR}/arch/amd64 > @@ -35,11 +34,11 @@ ARCH_SRCS = e_acos.S e_asin.S e_atan2.S e_exp.S e_fmod.S > e_log.S e_log10.S \ > e_sqrtl.S \ > invtrig.c \ > s_atan.S s_atanf.S s_ceil.S s_ceilf.S s_copysign.S s_copysignf.S \ > - s_cos.S s_cosf.S s_floor.S s_floorf.S \ > + s_floor.S s_floorf.S \ > s_log1p.S s_log1pf.S s_logb.S s_logbf.S \ > s_llrint.S s_llrintf.S s_lrint.S s_lrintf.S \ > s_rint.S s_rintf.S s_scalbnf.S s_significand.S \ > - s_significandf.S s_sin.S s_sinf.S s_tan.S s_tanf.S > + s_significandf.S > .elif (${MACHINE_ARCH} == "hppa") > .PATH: ${.CURDIR}/arch/hppa > ARCH_SRCS = e_sqrt.c e_sqrtf.c e_remainder.c e_remainderf.c \ > diff --git a/regress/lib/libm/msun/Makefile b/regress/lib/libm/msun/Makefile > index 1d6e9530c24..6f8bddf1831 100644 > --- a/regress/lib/libm/msun/Makefile > +++ b/regress/lib/libm/msun/Makefile > @@ -58,7 +58,6 @@ FAILING+= run-ctrig_test-1 > FAILING+= run-exponential_test-1 > FAILING+= run-invtrig_test-7 > FAILING+= run-next_test-{1,2,4} > -FAILING+= run-trig_test-3 > . elif ${MACHINE} == arm64 > FAILING+= run-cexp_test-{1,7} > FAILING+= run-ctrig_test-{1,5} > -- > 2.34.1 > > > [2:text/plain Show Save:before (68kB)] > > > [3:text/plain Show Save:after-all-soft (68kB)] >