[2/2] libm: Set math_errhandling to match library and hardware

Message ID 20200803175504.844738-3-keithp@keithp.com
State Superseded
Headers show
Series
  • libm: Changes to errno and math_errhandling
Related show

Commit Message

Christophe Lyon via Newlib Aug. 3, 2020, 5:55 p.m.
math_errhandling is specified to contain two bits of information:

 1. MATH_ERRNO     -- Set when the library sets errno
 2. MATH_ERREXCEPT -- Set when math operations report exceptions

MATH_ERRNO should match whether the original math code is compiled in
_IEEE_LIBM mode and the new math code has WANT_ERRNO == 1.

MATH_ERREXCEPT should match whether the underlying hardware has
exception support. This patch adds configurations of this value for
RISC-V, ARM, Aarch64, x86 and x86_64 when using HW float.

Signed-off-by: Keith Packard <keithp@keithp.com>

---
 newlib/libc/include/machine/ieeefp.h | 11 +++++++++++
 newlib/libc/include/math.h           | 12 +++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

-- 
2.28.0

Comments

Christophe Lyon via Newlib Aug. 4, 2020, 8:57 a.m. | #1
On Aug  3 10:55, Keith Packard via Newlib wrote:
> math_errhandling is specified to contain two bits of information:

> 

>  1. MATH_ERRNO     -- Set when the library sets errno

>  2. MATH_ERREXCEPT -- Set when math operations report exceptions

> 

> MATH_ERRNO should match whether the original math code is compiled in

> _IEEE_LIBM mode and the new math code has WANT_ERRNO == 1.

> 

> MATH_ERREXCEPT should match whether the underlying hardware has

> exception support. This patch adds configurations of this value for

> RISC-V, ARM, Aarch64, x86 and x86_64 when using HW float.

> 

> Signed-off-by: Keith Packard <keithp@keithp.com>

> ---

>  newlib/libc/include/machine/ieeefp.h | 11 +++++++++++

>  newlib/libc/include/math.h           | 12 +++++++++++-

>  2 files changed, 22 insertions(+), 1 deletion(-)


Prior to this patch, math_errhandling on Cygwin was set to MATH_ERRNO.
With this patch, math_errhandling on Cygwin is set to MATH_ERREXCEPT.

That may break backward compatibility.  From your patch I take it
that not setting MATH_ERREXCEPT before was just missing, but actually
changing the MATH_ERRNO bit looks wrong to me.

Does that mean Cygwin just has to define _IEEE_LIBM somehwere?  I don't
see that any target is setting _IEEE_LIBM anywhere...


Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat
Christophe Lyon via Newlib Aug. 4, 2020, 6:11 p.m. | #2
Corinna Vinschen <vinschen@redhat.com> writes:

> Prior to this patch, math_errhandling on Cygwin was set to MATH_ERRNO.

> With this patch, math_errhandling on Cygwin is set to MATH_ERREXCEPT.


Oh, my test is *backwards*. _IEEE_LIBM means that the original math
functions never set errno. However, even inverted, my test is not
complete. In the non-_IEEE_LIBM case, it needs to check _LIB_VERSION !=
_IEEE_ as much of the old math library uses that to decide whether to
set errno or not.

Thanks for catching this! I didn't create a test case for this, which I
have done now.

> That may break backward compatibility.  From your patch I take it

> that not setting MATH_ERREXCEPT before was just missing, but actually

> changing the MATH_ERRNO bit looks wrong to me.


Yes, MATH_ERREXCEPT should be set on any platform that supports
exceptions. What that means for hardware that only supports exceptions
on a subset of floating point formats is unclear.

> Does that mean Cygwin just has to define _IEEE_LIBM somehwere?  I don't

> see that any target is setting _IEEE_LIBM anywhere...


I don't know -- right now, Cygwin is not setting _POSIX_MODE and so
applications are not getting errno set unless they set
_LIB_VERSION=_POSIX_, which I suspect no applications are even aware of.

The test I wrote uncovered some additional information about errno
handling in libm. There are three different mechanisms for controlling
errno in various parts of the library:

 1) _IEEE_LIBM. Used by some routines the old math library to only
    support IEEE (754 I assume) standards for math functions, which means
    not setting errno and (sometimes) applying slightly different
    semantics to the functions (!). Not universally supported (i.e.,
    some functions still set errno even when this flag is set).

 2) _LIB_VERSION. This global variable (named __fdlib_version) selects
    whether the library supports IEEE semantics or posix semantics at
    run time. The default value for this is _IEEE_ unless the
    configuration sets _POSIX_MODE, which is only set on the 'spu'
    host. As a result, the default math library configuration is to run
    in _IEEE_ mode, which doesn't set errno in lots of functions.

 3) WANT_ERRNO. This is used in most of the libm/common code to select
    whether math routines should set errno or not. All of the relevant
    code is found in math_err.c and math_errf.c. This talks about using
    math_errhandling but it doesn't actually do it. This is enabled by
    default, and no configurations appear to disable it.

I think we need to fix things so that the library is internally
consistent -- right now, there's no way to configure the library to
disable errno entirely, nor is there a way to control errno at runtime
in all cases.

It seems that there are three possible ways we might want to configure
newlib:

 1) No errno ever. Use exceptions to report errors back to
    applications.

 2) Always set errno. All exceptions always set errno.

 3) Allow applications to enable/disable errno setting at runtime.

Right now, you can get 1) by defining _IEEE_LIBM, setting WANT_ERRNO=0
and never touching _LIB_VERSION. Applications may still end up
referencing errno if they use functions which only check _LIB_VERSION.

You can get 2) by by defining _POSIX_MODE, not defining _IEEE_LIBM and
never setting _LIB_VERSION.

You can't get 3) because math/common doesn't use _LIB_VERSION.

Given that only SPU ever sets _POSIX_MODE, I'm tempted to suggest that
newlib should never set errno and report errors only through the
exception mechanism. Applications could detect this by checking the
value of math_errhandling, which would not include MATH_ERRNO.

I don't think there's any reasonable standard way to enable 3). The
_LIB_VERSION mechanism isn't great -- it's a global setting (not
per-thread), and it's not supported in either musl or glibc. Plus,
newlib doesn't support the 'matherr' function anyways, which is tightly
associated with _LIB_VERSION. The math_errhandling 'replacement' doesn't
offer any defined way to change the current settings as math_errhandling
may well be constant.

If we want to support 2) in addition to 1), we'll want a single global
definition that covers that mode. I'd suggest using the existing
_IEEE_LIBM value as that is used in a lot more places than the somewhat
ambiguously named WANT_ERRNO, and has a nice '_' prefix so we can expose
it to applications (as is required if we want to use it when defining
math_errhandling).

-- 
-keith

Patch

diff --git a/newlib/libc/include/machine/ieeefp.h b/newlib/libc/include/machine/ieeefp.h
index aa8a1903b..b0042bbf6 100644
--- a/newlib/libc/include/machine/ieeefp.h
+++ b/newlib/libc/include/machine/ieeefp.h
@@ -87,6 +87,9 @@ 
 #  define __IEEE_BYTES_LITTLE_ENDIAN
 # endif
 #endif
+#ifndef __SOFTFP__
+# define _SUPPORTS_ERREXCEPT
+#endif
 #endif
 
 #if defined (__aarch64__)
@@ -96,6 +99,9 @@ 
 #define __IEEE_BIG_ENDIAN
 #endif
 #define __OBSOLETE_MATH_DEFAULT 0
+#ifdef __ARM_FP
+# define _SUPPORTS_ERREXCEPT
+#endif
 #endif
 
 #ifdef __epiphany__
@@ -189,10 +195,14 @@ 
 
 #ifdef __i386__
 #define __IEEE_LITTLE_ENDIAN
+# define _SUPPORTS_ERREXCEPT
 #endif
 
 #ifdef __riscv
 #define __IEEE_LITTLE_ENDIAN
+#ifdef __riscv_flen
+# define _SUPPORTS_ERREXCEPT
+#endif
 #endif
 
 #ifdef __i960__
@@ -386,6 +396,7 @@ 
 
 #ifdef __x86_64__
 #define __IEEE_LITTLE_ENDIAN
+# define _SUPPORTS_ERREXCEPT
 #endif
 
 #ifdef __mep__
diff --git a/newlib/libc/include/math.h b/newlib/libc/include/math.h
index 1efc5b92c..4a99b322e 100644
--- a/newlib/libc/include/math.h
+++ b/newlib/libc/include/math.h
@@ -188,7 +188,17 @@  extern int isnan (double);
 # define MATH_ERREXCEPT 2
 #endif
 #ifndef math_errhandling
-# define math_errhandling MATH_ERRNO
+# ifdef _IEEE_LIBM
+#  define _MATH_ERRHANDLING_ERRNO MATH_ERRNO
+# else
+#  define _MATH_ERRHANDLING_ERRNO 0
+# endif
+# ifdef _SUPPORTS_ERREXCEPT
+#  define _MATH_ERRHANDLING_ERREXCEPT MATH_ERREXCEPT
+# else
+#  define _MATH_ERRHANDLING_ERREXCEPT 0
+# endif
+# define math_errhandling (_MATH_ERRHANDLING_ERRNO | _MATH_ERRHANDLING_ERREXCEPT)
 #endif
 
 extern int __isinff (float x);