Commit Graph

513 Commits

Author SHA1 Message Date
Steve Kargl
620d855fac msun: correct comment
The comment in msun/src/e_jn.c lacks proper grammar, and is incorrect on
the choice of normalization entity.

PR:	266503
MFC after:	3 days
2022-09-19 21:40:07 +03:00
Gordon Bergling
82007616d0 msun: Remove a double word in a source code comment
- s/to to/to/

MFC after:	3 days
2022-09-10 12:59:10 +02:00
Gordon Bergling
a52f4499ae libm: Correct some typos in source code comments
- s/modfied/modified/
- s/minimun/minimum/

While here, fix some mandoc warnings:

- whitespace at end of input line
- unusual Xr punctuation
- missing comma before name

Obtained from:	NetBSD
MFC after:	5 days
2022-09-03 19:14:02 +02:00
Steve Kargl
369ea0520a [libm] Correct comments in s_cbrt[l].c
Damian McGuckin <damianm at esi dot com dot au> noted that the accuracy
claims in the code for cbrt(3) and cbrtl(3) were incorrect. Fix the
comments to more accurately describe the accuracies.

PR:		265603
MFC after:	3 days
2022-08-04 19:33:34 +02:00
Dimitry Andric
e50027e38d Remove unnecessary const and volatile qualifiers from __fp_type_select()
Since https://github.com/llvm/llvm-project/commit/ca75ac5f04f2, clang 15
has a new warning about _Generic selection expressions, such as used in
math.h:

    lib/libc/gdtoa/_ldtoa.c:82:10: error: due to lvalue conversion of the controlling expression, association of type 'volatile float' will never be selected because it is qualified [-Werror,-Wunreachable-code-generic-assoc]
            switch (fpclassify(u.e)) {
                    ^
    lib/msun/src/math.h:109:2: note: expanded from macro 'fpclassify'
            __fp_type_select(x, __fpclassifyf, __fpclassifyd, __fpclassifyl)
            ^
    lib/msun/src/math.h:85:14: note: expanded from macro '__fp_type_select'
        volatile float: f(x),                                               \
                 ^

This is because the controlling expression always undergoes lvalue
conversion first, dropping any cv-qualifiers. The 'const', 'volatile',
and 'volatile const' associations will therefore never be used.

MFC after:	1 week
Reviewed by:	theraven
Differential Revision: https://reviews.freebsd.org/D35815
2022-07-15 20:09:27 +02:00
Yi Kong
7e06f4708c
msun: Rewrite function definitions with identifier lists
This syntax is removed in C2x proposal N2432.

Reviewed by:	pfg
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D35771
2022-07-12 13:17:47 +08:00
John Baldwin
56f5947a71 Remove checks for __GNUCLIKE_ASM assuming it is always true.
All supported compilers (modern versions of GCC and clang) support
this.

Many places didn't have an #else so would just silently do the wrong
thing.  Ancient versions of icc (the original motivation for this) are
no longer a compiler FreeBSD supports.

PR:		263102 (exp-run)
Reviewed by:	brooks, imp
Differential Revision:	https://reviews.freebsd.org/D34797
2022-04-12 10:05:45 -07:00
Gordon Bergling
29fea59e78 math(3): Remove a double word in a source code comment
- s/is is/is/

MFC after:	3 days
2022-04-09 10:13:37 +02:00
Mark Murray
03a88e3de9 * lib/msun/Makefile b/lib/msun/Makefile:
. Disconnect imprecise.c from the build.  This file can be deleted.
  . Add b_tgammal.c to the build for ld80 and ld128 targets.  The ld128
    is a 'git mv' of imprecise.c to ld128/b_tgammal.c.

* lib/msun/ld80/b_expl.c:
  . New file.  Implement __exp__D for ld80 targets.  This is based on
    bsdsrc/b_exp.c.

* lib/msun/ld80/b_logl.c:
  . New file.  Implement __log__D for ld80 targets.  This is based on
    bsdsrc/b_log.c.

* lib/msun/ld80/b_tgammal.c b/lib/msun/ld80/b_tgammal.c
  . New file.  Implement tgammal(x) for ld80 targets.

Submitted by:           Steve Kargl
Differential Revision:  https://reviews.freebsd.org/D33444
Reviewed by:            pfg
2021-12-15 18:36:20 +00:00
Dimitry Andric
20d425842a Remove set-but-unused variable from s_sincosl.c
This look like a copy and paste leftover.

Reported by:	enh@google.com (via freebsd-numerics@)
Reviewed by:	Steve Kargl <sgk@troutmask.apl.washington.edu>
MFC after:	3 days
2021-12-14 22:50:30 +01:00
Andrew Turner
b2e843161d Use a builtin where possible in msun
Some of the functions in msun can be implemented using a compiler
builtin function to generate a small number of instructions. Implement
this support in fma, fmax, fmin, and sqrt on arm64.

Care must be taken as the builtin can be implemented as a function
call on some architectures that lack direct support. In these cases
we need to use the original code path.

As we don't set errno on failure build with -fno-math-errno so the
toolchain doesn't convert a builtin into a function call when it
detects a failure, e.g. gcc will add a call to sqrt when the input
is negative leading to an infinite loop.

Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D32801
2021-11-19 11:40:46 +00:00
Dimitry Andric
e2157cd000 Partially revert ac76bc1145 because it is no longer necessary
In ac76bc1145, I added a few volatiles to work around ctrig_test
failures with {inf,inf}. This is not necessary anymore now, since in
3b00222f15 we added -fp-exception-behavior=maytrap for clang >= 10 in
libm's Makefile. (The flag tells clang to use stricter floating point
semantics, which libm depends on.)

PR:		244732, 254911
Fixes:		ac76bc1145
MFC after:	3 days
2021-11-05 22:27:20 +01:00
Steve Kargl
046e2d5db1 Implementations of cexpl()
The change implements cexpl() for both ld80 and ld128 architectures.
Testing was done on x86_64 and aarch64 systems.

Along the way sincos[fl]() use an optimization that reduces the argument
to being done one rather than twice.  This optimization actually pointed
to a bug in the ld128 version of sincosl(), which is now fixed.  In
addition, the minmax polynomial coefficients for sincosl() have been
updated.

A concise log of the file-by-file changes follows.

* include/complex.h:
  . Add a prototype for cexpl().

* lib/msun/Makefile:
  . Add s_cexpl.c to the build.
  . Setup a link for cexpl.3 to cexp.3.

* lib/msun/Symbol.map:
  . Expose cexpl symbol in libm shared library.

* lib/msun/ld128/s_cexpl.c:
  * Implementation of cexpl() for 128-bit long double architectures.
    Tested on an aarch64 system.

* lib/msun/ld80/s_cexpl.c:
  * Implementation of cexpl() for Intel 80-bit long double.

* lib/msun/man/cexp.3:
  . Document cexpl().

* lib/msun/man/complex.3:
  . Add a BUGS section about cpow[fl].

* lib/msun/src/s_cexp.c:
  . Include float.h for weak references on 53-bit long double targets.
  . Use sincos() to reduce argument reduction cost.

* lib/msun/src/s_cexpf.c:
  . Use sincosf() to reduce argument reduction cost.

* lib/msun/src/k_sincosl.h:
  . Catch up with the new minmax polynomial coefficients for the kernel for
    the 128-bit cosl() implementation.
  . BUG FIX: *cs was used where *sn should have been.  This means that sinl()
    was no computed correctly when iy != 0.

* lib/msun/src/s_cosl.c:
  . Include fpmath.h to get access to IEEEl2bits.
  . Replace M_PI_4 with pio4,  a 64-bit or 113-bit approximation for pi / 4.

PR:	216862
MFC after:	1 week
2021-11-05 13:51:42 +02:00
Steve Kargl
3bfc837685 sinpi,cospi,tanpi: float.h needed for week reference
The patch fixes the omission of '#include <float.h>', which is needed for
the weak reference on systems with LDBL_MANT_DIG == DBL_MANT_DIG.

PR:	218514
MFC after:	1 week
2021-10-29 03:15:19 +03:00
Steve Kargl
dce5f3abed [LIBM] implementations of sinpi[fl], cospi[fl], and tanpi[fl]
Both IEEE-754 2008 and ISO/IEC TS 18661-4 define the half-cycle
trignometric functions cospi, sinpi, and tanpi.  The attached
patch implements cospi[fl], sinpi[fl], and tanpi[fl].  Limited
testing on the cospi and sinpi reveal a max ULP less than 0.89;
while tanpi is more problematic with a max ULP less than 2.01
in the interval [0,0.5].  The algorithms used in these functions
are documented in {ks}_cospi.c, {ks}_sinpi.c, and s_tanpi.c.

Note.  I no longer have access to a system with ld128 and
adequate support to compile and test the ld128 implementations
of these functions.  Given the almost complete lack of input from
others on improvements to libm, I doubt that anyone cares.  If
someone does care, the ld128 files contain a number of FIXME comments,
and in particular, while the polynomial coefficients are given
I did not update the polynomial algorithms to properly use the
coefficients.

PR:	218514
MFC after:	2 weeks
2021-10-26 02:50:20 +03:00
Warner Losh
3550a49f68 msun: Add copyright notices
These files were copied from MUSL. Add the standard copyright notice and
SPDX-License-Identifier: MIT consistent with our new draft license
policy. It reads word for word the same as the MIT license on the SPDX
web site. Add a pointer to the MUSL COPYIRGHT file which contains a list
of all authors of MUSL.

Sponsored by:		Netflix
Noticed by:		Steve Kargl
2021-10-22 22:00:54 -06:00
Mark Murray
292815eac6 Fix powf().
Summary:
From Steve Kargl:

Paul Zimmermann has identified a bug in Openlibm's powf(),
which is identical to FreeBSD's libm.  Both derived from
fdlibm. https://github.com/JuliaMath/openlibm/issues/212.

Consider

% cat h.c
int
main(void)
{
  float x, y, z;
  x =  0x1.ffffecp-1F;
  y = -0x1.000002p+27F;
  z =  0x1.557a86p115F;
  printf("%e %e %e <-- should be %e\n", x, y, powf(x,y), z);
  return 0;
}

% cc -o h -fno-builtin h.c -lm && ./h
9.999994e-01 -1.342177e+08 inf <-- should be 5.540807e+34

Reviewers: manu

Subscribers: imp, andrew, emaste

Differential Revision: https://reviews.freebsd.org/D31865
2021-09-06 18:51:31 +01:00
Dimitry Andric
0bcd49c13a Work around bogus old gcc "initializer element is not constant" error
After df3b437c1e, older gcc's such as
4.2.1 (still used on earlier branches for e.g. mips and powerpc) and
6.3.0 (still used for some cross-builds) started throwing bogus errors
like:

In file included from /workspace/src/lib/msun/src/s_llround.c:11:0:
/workspace/src/lib/msun/src/s_lround.c:54:31: error: initializer element is not constant
 static const type dtype_min = type_min - 0.5;
                               ^~~~~~~~
/workspace/src/lib/msun/src/s_lround.c:55:31: error: initializer element is not constant
 static const type dtype_max = type_max + 0.5;
                               ^~~~~~~~

Since 'type_min' and 'type_max' are constants declared just above these
lines this error is nonsensical, but older gcc's are not smart enough.

Work around the error by reusing the (type)DTYPE_MIN and (type)DTYPE_MAX
macros, so I can MFC this right away, unbreaking a few stable builds.

MFC after:	immediately
2021-06-25 20:43:20 +02:00
Dimitry Andric
df3b437c1e Fix failures in libm's lround_test after clang 12 import
It turned out that the (type)DTYPE_MAX conversions at the top of
s_lround.c are now emitted as cvtsi2sd instructions, at least on SSE
capable CPUs. This caused the FE_INEXACT flag to always be set, at least
for the double and float variants. Under clang 11, the whole INRANGE()
comparisons were still optimized away, but this has "improved" in clang
12, due to stricter adherence to the -ffp-exception-behavior=maytrap
compiler flag.

To avoid run-time integer to float conversions, use static constants
instead, so they are computed at compile time, and the INRANGE()
statements are optimized away again, if applicable.

While here, use an integer instead of a floating type to store the test
results in lround_test.c, as this is more appropriate, and we can also
drop the volatile hack.

Reported by:	arichardson
MFC after:	3 days
2021-06-22 18:38:45 +02:00
Dimitry Andric
7702d940ec Avoid -pedantic warnings about using _Generic in __fp_type_select
When compiling parts of math.h with clang using a C standard before C11,
and using -pedantic, it will result in warnings similar to:

bug254714.c:5:11: warning: '_Generic' is a C11 extension [-Wc11-extensions]
  return !isfinite(1.0);
          ^
/usr/include/math.h:111:21: note: expanded from macro 'isfinite'
                    ^
/usr/include/math.h:82:39: note: expanded from macro '__fp_type_select'
                                      ^

This is because the block that enables use of _Generic is conditional
not only on C11, but also on whether the compiler advertises support for
C generic selections via __has_extension(c_generic_selections).

To work around the warning without having to pessimize the code, use the
__extension__ keyword, which is supported by both clang and gcc. While
here, remove the check for __clang__, as _Generic has been supported for
a long time by gcc too now.

Reported by:	yuri
PR:		254714
MFC after:	1 week
2021-04-08 18:20:32 +02:00
Alex Richardson
f5542795b9 s_scalbn.c: Add missing float.h include
This caused LDBL_MANT_DIG to not be defined and therefore the scalbnl
alias was not being emitted for double==long double platforms.

Fixes:		760b2ffc ("Update scalbn* functions to the musl versions")
Reported by:	Jenkins
2021-03-01 14:22:47 +00:00
Alex Richardson
760b2ffc55 Update scalbn* functions to the musl versions
The only diff compared to musl is a minor change to scalbnl() to replace
musl's union ldshape with union IEEEl2bits.
This fixes the scalbn tests on non-x86 (since x86 has an assembly version
that is used instead).

Musl commit messages:
commit 8c44a060243f04283ca68dad199aab90336141db
Author: Szabolcs Nagy <nsz@port70.net>
Date:   Mon Apr 3 02:38:13 2017 +0200

    fix scalbn when result is in the subnormal range

    in nearest rounding mode scalbn could introduce double rounding error
    when an intermediate value and the final result were both in the
    subnormal range e.g.

      scalbn(0x1.7ffffffffffffp-1, -1073)

    returned 0x1p-1073 instead of 0x1p-1074, because the intermediate
    computation got rounded to 0x1.8p-1023.

    with the fix an intermediate value can only be in the subnormal range
    if the final result is 0 which is correct even after double rounding.
    (there still can be two roundings so signals may be raised twice, but
    that's only observable with trapping exceptions which is not supported.)

commit 2eaed464e2080d8321d3903b71086a1ecfc4ee4a
Author: Szabolcs Nagy <nsz@port70.net>
Date:   Wed Sep 4 15:52:54 2013 +0000

    math: use float_t and double_t in scalbnf and scalbn

    remove STRICT_ASSIGN (c99 semantics is assumed) and use the conventional
    union to prepare the scaling factor (so libm.h is no longer needed)

commit 1b77b9072f374bd26eb0574b83a0d5f18d75ec60
Author: Szabolcs Nagy <nsz@port70.net>
Date:   Thu Aug 15 10:07:46 2013 +0000

    math: minor scalbn*.c simplification

commit c4359e01303da2755fe7e8033826b132eb3659b1
Author: Szabolcs Nagy <nsz@port70.net>
Date:   Tue Nov 13 10:55:35 2012 +0100

    math: excess precision fix modf, modff, scalbn, scalbnf

    old code was correct only if the result was stored (without the
    excess precision) or musl was compiled with -ffloat-store.
    now we use STRICT_ASSIGN to work around the issue.
    (see note 160 in c11 section 6.8.6.4)

commit 666271c105e4137bdfa195e217799d74143370d4
Author: Szabolcs Nagy <nsz@port70.net>
Date:   Tue Nov 13 10:30:40 2012 +0100

    math: fix scalbn and scalbnf on overflow/underflow

    old code was correct only if the result was stored (without the
    excess precision) or musl was compiled with -ffloat-store.
    (see note 160 in n1570.pdf section 6.8.6.4)

commit 8051e08e10d2b739fcfcbc6bc7466e8d77fa49f1
Author: nsz <nsz@port70.net>
Date:   Mon Mar 19 10:54:07 2012 +0100

    simplify scalbn*.c implementations

    The old scalbn.c was wrong and slow, the new one is just slow.
    (scalbn(0x1p+1023,-2097) should give 0x1p-1074, but the old code gave 0)

Reviewed By:	dim
Differential Revision: https://reviews.freebsd.org/D28872
2021-03-01 12:53:45 +00:00
Alex Richardson
a7b42c4b7f msun: ctanh/ctanhf: Import fix from musl libc
This applies musl commit b02eed9c4841913d690a2d0029737d72615384fe by
Szabolcs Nagy and updates the tests accordingly. This also allows
removing an XFAIL from the test.

musl commit message:

complex: fix ctanh(+-0+i*nan) and ctanh(+-0+-i*inf)

These cases were incorrect in C11 as described by
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1886.htm

PR: 217528

Reviewed By:	dim
MFC after:	1 week
Differential Revision: https://reviews.freebsd.org/D28578
2021-02-15 22:55:12 +00:00
Dimitry Andric
d3338f3355 Fix incorrect hypotl(3) result with subnormal numbers
This adjusts the factor used to scale the subnormal numbers, so it
becomes the right value after adjusting its exponent. Thanks to Steve
Kargl for finding the most elegant fix.

Also enable the hypot tests, and add a test case for this bug.

PR:		253313
MFC after:	1 week
2021-02-10 23:28:43 +01:00
Dimitry Andric
ac76bc1145 Fix lib/msun's ctrig_test/test_inf_inputs test case with clang >= 10
This sprinkles a few strategic volatiles in an attempt to defeat clang's
optimization interfering with the expected floating-point exception
flags.

Reported by:	lwhsu
PR:		244732
MFC after:	3 days
2021-02-09 22:07:05 +01:00
Steve Kargl
93fc678965 Fix incorrect powf(3) result with x near 1 and |y| much larger than 1
This adjusts the check to trigger overflow/underflow to a slightly lower
value.

Before: powf(9.999995e-01, -1.342177e+08) -> inf
After:  powf(9.999995e-01, -1.342177e+08) -> 1.858724e+31

MFC after:	1 week
2021-02-08 20:48:43 +01:00
Warner Losh
e369c79ca0 Remove intel compiler support from math.h
The intel compiler support has badly decayed over the years. Stop
pretending that we support it. Note, I've stopped short of requiring
gcc builtin support with this commit since other compilers may be used
to build non-base software and we need to support those so more
investigation is needed before simplifying further.
2020-10-24 23:21:22 +00:00
Stefan Eßer
fcce470c2b Apply an opimization for the kernels used by cexp(x) and cexpf(x) submitted
by Steve Kargl:

- Use sincos[f] instead of a call to cos[f] and a call to sin[f].

- While here, alphabetize declaration.

Submitted by:   sgk at troutmask.apl.washington.edu (Steve Kargl)
2020-09-20 05:32:53 +00:00
Alfredo Dal'Ava Junior
f1343c7f67 msun: swap words order instead of bits order on BIG ENDIAN
The "for" loop on big endian was inverting all the bits instead of
just the words

Issue reported by TestSuite (msun lib nan_test case)

Submitted by:	Renato Riolino <renato.riolino@eldorado.org.br>
Submitted by:	Fernando Valle <fernando.valle@eldorado.org.br>
Reviewed by:	pfg, alfredo
Approved by:	jhibbits (mentor)
Sponsored by:	Eldorado Research Institute (eldorado.org.br)
Differential Revision:	https://reviews.freebsd.org/D23926
2020-03-26 18:50:54 +00:00
Dimitry Andric
83fa0a287f The fdlibm hypot() implementations shouldn't potentially left-shift
negative numbers (invoking undefined behavior)

Summary:
Various paths through hypot(x, y) will multiply x and y by a power of
two, perform the calculation in a range where IEEE-754 provides greater
precision, then undo the multiplication to determine the true result.
Undoing that multiplication is implemented as t1*w, where t1=2**k.

2**k is often computed by taking the high word of 1.0, then adding k<<20
(for doubles or long doubles) or k<<23 (for floats) to it, then
overwriting that high word. But when k is negative this left-shifts a
negative value -- and that's undefined behavior in many editions of C
and C++.

This patch should fix all hypot implementations to compute 2**k without
triggering this particular bit of undefined behavior.

Test Plan: I've only very lightly tested out the hypot(double, double)
change, in SpiderMonkey's JavaScript engine, for consistency with prior
behavior.  The other functions' changes have more or less only been
eyeballed.  Careful examination appreciated!  Do note, however, that an
error in any of these changes would most likely produce a value that is
incorrect by a factor of two, so any mistake would most likely be
glaring if invoked.

Submitted by:	Jeff Walden <jwalden@mit.edu>
Obtained from:	https://github.com/freebsd/freebsd/pull/414
Reviewed by:	dim, lwhsu
MFC after:	1 week
Differential Revision: https://reviews.freebsd.org/D22354
2019-11-26 22:01:09 +00:00
Li-Wen Hsu
12c4ab4377 Get the fix in back by reverting the part accidentally included in r354491.
This brings back r354467.

MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2019-11-07 23:57:48 +00:00
Brooks Davis
a4330302f2 libcompat: build 32-bit rtld and ldd as part of "everything"
Alter bsd.compat.mk to set MACHINE and MACHINE_ARCH when included
directly so MD paths in Makefiles work. In the process centralize
setting them in LIBCOMPATWMAKEENV.

Alter .PATH and CFLAGS settings in work when the Makefile is included.

While here only support LIB32 on supported platforms rather than always
enabling it and requiring users of MK_LIB32 to filter based
TARGET/MACHINE_ARCH.

The net effect of this change is to make Makefile.libcompat only build
compatability libraries.

Changes relative to r354449:

Correct detection of the compiler type when bsd.compat.mk is used
outside Makefile.libcompat.  Previously it always matched the clang
case.

Set LDFLAGS including the linker emulation for mips where -m32 seems to
be insufficent.

Reviewed by:	imp, kib (origional version in r354449)
Obtained from:	CheriBSD (conceptually)
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D22251
2019-11-07 22:58:10 +00:00
Brooks Davis
271d2989ba Fix declaration of S1 by swapping misplaced ',' and ';'.
Reported by:	kargl
Obtained from:	OpenBSD (tb@openbsd.org)
MFC after:	1 week
2019-11-07 19:31:56 +00:00
Brooks Davis
1edbf0e9ba Revert change accidentally included in r354465.
Will recommit with a proper commit message shortly.
2019-11-07 19:28:03 +00:00
Brooks Davis
ccad77fe1e Revert r354449: libcompat: build 32-bit rtld and ldd as part of "everything"
Additional testing is required..
2019-11-07 19:22:51 +00:00
Dimitry Andric
f68ff1ac1c Add __isnan()/__isnanf() aliases for compatibility with glibc and CUDA
Even though clang comes with a number of internal CUDA wrapper headers,
compiling sample CUDA programs will result in errors similar to:

In file included from <built-in>:1:
In file included from /usr/lib/clang/9.0.0/include/__clang_cuda_runtime_wrapper.h:204:
/usr/home/arr/cuda/var/cuda-repo-10-0-local-10.0.130-410.48/usr/local/cuda-10.0//include/crt/math_functions.hpp:2910:7: error: no matching function for call to '__isnan'
  if (__isnan(a)) {
      ^~~~~~~
/usr/lib/clang/9.0.0/include/__clang_cuda_device_functions.h:460:16: note: candidate function not viable: call to __device__ function from __host__ function
__DEVICE__ int __isnan(double __a) { return __nv_isnand(__a); }
               ^

CUDA expects __isnan() and __isnanf() declarations to be available,
which are glibc specific extensions, equivalent to the regular isnan()
and isnanf().

To provide these, define __isnan() and __isnanf() as aliases of the
already existing static inline functions __inline_isnan() and
__inline_isnanf() from math.h.

Reported by:	arrowd
PR:		241550
MFC after:	1 week
2019-11-02 16:59:53 +00:00
Brooks Davis
e593620997 msun: Silence new harmless -Wimplicit-int-float-conversion warnings
Clang from trunk recently added a warning for when implicit int-to-float
conversions cause a loss of precision. The code in question is designed
to be able to handle that, so add explicit casts to silence this.

Submitted by:	James Clarke <jrtc27@jrtc27.com>
Reviewed by:	dim
Obtained from:	CheriBSD
MFC after:	1 week
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D21913
2019-10-08 21:39:51 +00:00
Dimitry Andric
5763a8cf06 Do not left-shift a negative number (inducing undefined behavior in
C/C++) in exp(3), expf(3), expm1(3) and expm1f(3) during intermediate
computations that compute the IEEE-754 bit pattern for |2**k| for
integer |k|.

The implementations of exp(3), expf(3), expm1(3) and expm1f(3) need to
compute IEEE-754 bit patterns for 2**k in certain places.  (k is an
integer and 2**k is exactly representable in IEEE-754.)

Currently they do things like 0x3FF0'0000+(k<<20), which is to say they
take the bit pattern representing 1 and then add directly to the
exponent field to get the desired power of two.  This is fine when k is
non-negative.

But when k<0 (and certain classes of input trigger this), this
left-shifts a negative number -- an operation with undefined behavior in
C and C++.

The desired semantics can be achieved by instead adding the
possibly-negative k to the IEEE-754 exponent bias to get the desired
exponent field, _then_ shifting that into its proper overall position.

(Note that in case of s_expm1.c and s_expm1f.c, there are SET_HIGH_WORD
and SET_FLOAT_WORD uses further down in each of these files that perform
shift operations involving k, but by these points k's range has been
restricted to 2 < k <= 56, and the shift operations under those
circumstances can't do anything that would be UB.)

Submitted by:	Jeff Walden, https://github.com/jswalden
Obtained from:	https://github.com/freebsd/freebsd/pull/411
Obtained from:	https://github.com/freebsd/freebsd/pull/412
MFC after:	3 days
2019-09-25 18:50:57 +00:00
Peter Jeremy
885bfcda11 Replace calls to sin(x) and cos(x) with a single call to sincos().
Replace calls to sinf(x) and cosf(x) with a single call to sincosf().

Submitted by:	Steve Kargl <sgk@troutmask.apl.washington.edu>
Reviewed by:	bde
Approved by:	grog
MFC after:	3 days
2019-02-10 08:46:07 +00:00
Pedro F. Giffuni
76c528d350 msun: reduce diff between src/e_j0.c and src/e_j0f.c
PR:	229501
MFC after:	1 week
2019-01-14 15:48:35 +00:00
Pedro F. Giffuni
bdd8abc6d6 pow(3): Workaround possible signed shift Undefined Behavior.
j is int32_t and thus j<<31 is undefined if j==1.

Hinted by:	muusl-lib (git 688d3da0f1730daddbc954bbc2d27cc96ceee04c)
Discussed with:	freebsd-numerics (kargl)
2019-01-07 17:35:09 +00:00
Pedro F. Giffuni
50757b1452 msun: Fix some old typos.
Seen in a posting from July 27 by "CM Graff" in musl-libc.
2018-12-31 15:43:06 +00:00
Justin Hibbits
003fdafbea libm: Include float.h to get LDBL_MANT_DIG
The long double aliases of double functions are only exposed as aliases if
LDBL_MANT_DIG is 53 (same as DBL_MANT_DIG).  Without float.h included these
files were not exposing weak aliases as expected, leading to link failures
if programs use the *l functions.  This should fix editors/calligra on
targets with 64-bit long double, which uses erfl and erfcl.  Found on
powerpc64.

Reviewed by:	kargl@
2018-12-28 01:34:08 +00:00
Bruce Evans
2011986f09 In C remquol() and thus also in C remainderl(), don't clobber the sign bit
of NaNs before possible returning a NaN.

The remquo*() and remainder*() functions should now give bitwise identical
results across arches and implementations, and bitwise consistent results
(with lower precisions having truncated mantissas) across precisions.  x86
already had consistency across amd64 and i386 and precisions by using the
i387 consistently and normally not using the C versions.  Inconsistencies
for C reqmquol() were first detected on sparc64.

Remove double second clearing of the sign bit and extra blank lines.
2018-07-24 11:50:05 +00:00
Bruce Evans
daa1e39110 Fix the conversion to use nan_mix() in r336362. fmod*(x, y),
remainder*(x, y) and remquo*(x, y, quo) were broken for y = 0 by changing
multiplication by y to addition of y.  (When y is 0, the result should be
NaN but became 1 for finite x.)

Use a new macro nan_mix_op() to give more control over the mixing, and
expand comments.

Recent re-testing missed finding this bug since I only tested the macro
version on amd64 and i386 and these arches don't use the C versions (they
use either asm versions or builtins).

Reported by:	enh via freebsd-numerics
2018-07-24 10:10:16 +00:00
Dimitry Andric
5a4c3b831b Recommit r336497: Fix powl, cpow, cpowf, and cpowl imports from OpenBSD
This is a follow-up to r336299.

* lib/msun/Makefile:
  . Remove polevll.c

* lib/msun/ld80/e_powl.c:
  . Copy contents of polevll.c to here.  This is the only consumer of
    these functions.  Make functions 'static inline'.
  . Make reducl a 'static inline' function.

* lib/msun/man/exp.3:
  . Remove BUGS section that no longer applies.

* lib/msun/src/math_private.h:
  . Remove prototypes of __p1evll() and __polevll()

* lib/msun/src/s_cpow.c:
* lib/msun/src/s_cpowf.c:
* lib/msun/src/s_cpowl.c
  . Include math_private.h.
  . Use the CMPLX macro from either C99 or math_private.h (depends on
    compiler support) instead of the problematic use of complex I.

Submitted by:	Steve Kargl <sgk@troutmask.apl.washington.edu>
PR:		229876
MFC after:	1 week
2018-07-20 18:27:30 +00:00
Bruce Evans
27aa844253 Centralize the complications for special efficient rounding to integers.
This was open-coded in range reduction for trig and exp functions.  Now
there are 3 static inline functions rnint[fl]() that replace open-coded
expressions, and type-generic irint() and i64rint() macros that hide the
complications for efficiently using non-generic irint() and irintl()
functions and casts.

Special details:

ld128/e_rem_pio2l.h needs to use i64rint() since it needs a 46-bit integer
result.  Everything else only needs a (less than) 32-bit integer result so
uses irint().

Float and double cases now use float_t and double_t locally instead of
STRICT_ASSIGN() to avoid bugs in extra precision.

On amd64, inline asm is now only used for irint() on long doubles.  The SSE
asm for irint() on amd64 only existed because the ifdef tangles made the
correct method of simply casting to int for this case non-obvious.
2018-07-20 12:42:24 +00:00
Dimitry Andric
c422fbac00 Revert r336497 for now, as it breaks on architectures using gcc, with:
cc1: warnings being treated as errors
/usr/src/lib/msun/src/s_cpow.c: In function 'cpow':
/usr/src/lib/msun/src/s_cpow.c:63: warning: implicit declaration of function 'CMPLX'
2018-07-19 19:07:25 +00:00
Dimitry Andric
2ae9055f49 Fix powl, cpow, cpowf, and cpowl imports from OpenBSD
This is a follow-up to r336299.

* lib/msun/Makefile:
  . Remove polevll.c

* lib/msun/ld80/e_powl.c:
  . Copy contents of polevll.c to here.  This is the only consumer of
    these functions.  Make functions 'static inline'.
  . Make reducl a 'static inline' function.

* lib/msun/man/exp.3:
  . Remove BUGS section that no longer applies.

* lib/msun/src/math_private.h:
  . Remove prototypes of __p1evll() and __polevll()

* lib/msun/src/s_cpow.c:
* lib/msun/src/s_cpowf.c:
* lib/msun/src/s_cpowl.c
  . Use the CMPLX macro from either C99 or math_private.h (depends of
    compiler support) instead of the problematic use of complex I.

Submitted by:	Steve Kargl <sgk@troutmask.apl.washington.edu>
PR:		229876
MFC after:	1 week
2018-07-19 18:44:10 +00:00
Bruce Evans
b7092eef4d Fix spurious and extra underflows and resulting inaccuracies for some cases
with 1 huge component and 1 tiny (but nowhere near denormal) component.
Rescale earlier so that a scale factor of 2 can be combined with a non-
scale divisor of 2, so that the division doesn't shift out a bit.  In the
usual case where the scale factor is just 1, the division may shift out a
bit, but then the underflow is not spurious and the inaccuracies are harder
to fix.
2018-07-19 15:04:10 +00:00