[PATCH 0/2] libm: Changes to errno and math_errhandling

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 0/2] libm: Changes to errno and math_errhandling

Sourceware - newlib list mailing list
Here are a couple of related patches that adjust errno and exception
configuration stuff.

The first patch makes libm/common use the same configuration value as
libm/math to decide whether to set errno when exceptions occur. With
that, users of libm/math get consistent errno reporting across the
whole math library when using the normal API. When errno is enabled,
calls to some __ieee754 symbols may end up setting errno still.

The second patch sets the math_errhandling value based on library and
hardware configuration so that applications can tell whether
exceptions and errno are supported. This requires configuration on a
per-target basis in machine/ieeefp.h; I've added configuration for
arm, aarch64, x86, x86_64 and RISC-V. I think this could be automated.

Reporting on whether exceptions are supported suffers from
inconsistent exception support for hardware that uses a mixture of
hardware and software for floating point operations. I haven't found
any standards-conformant mechanism to use though.


Reply | Threaded
Open this post in threaded view
|

[PATCH 1/2] libm/common: Set WANT_ERRNO based on _IEEE_LIBM value

Sourceware - newlib list mailing list
_IEEE_LIBM is the configuration value which controls whether the
original libm functions modify errno. Use that in the new math code as
well so that the resulting library is internally consistent.

Signed-off-by: Keith Packard <[hidden email]>
---
 newlib/libm/common/math_config.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/newlib/libm/common/math_config.h b/newlib/libm/common/math_config.h
index 1a2d0f639..3be7e6320 100644
--- a/newlib/libm/common/math_config.h
+++ b/newlib/libm/common/math_config.h
@@ -36,7 +36,9 @@
 /* Correct special case results in non-nearest rounding modes.  */
 # define WANT_ROUNDING 1
 #endif
-#ifndef WANT_ERRNO
+#ifdef _IEEE_LIBM
+# define WANT_ERRNO 0
+#else
 /* Set errno according to ISO C with (math_errhandling & MATH_ERRNO) != 0.  */
 # define WANT_ERRNO 1
 #endif
--
2.28.0

Reply | Threaded
Open this post in threaded view
|

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

Sourceware - newlib list mailing list
In reply to this post by Sourceware - newlib list mailing list
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 <[hidden email]>
---
 newlib/libc/include/machine/ieeefp.h | 11 +++++++++++
 newlib/libc/include/math.h           | 12 +++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

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);
--
2.28.0

Reply | Threaded
Open this post in threaded view
|

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

Sourceware - newlib list mailing list
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 <[hidden email]>
> ---
>  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

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 0/2] libm: Changes to errno and math_errhandling

Sourceware - newlib list mailing list
In reply to this post by Sourceware - newlib list mailing list
On Aug  3 10:55, Keith Packard via Newlib wrote:

> Here are a couple of related patches that adjust errno and exception
> configuration stuff.
>
> The first patch makes libm/common use the same configuration value as
> libm/math to decide whether to set errno when exceptions occur. With
> that, users of libm/math get consistent errno reporting across the
> whole math library when using the normal API. When errno is enabled,
> calls to some __ieee754 symbols may end up setting errno still.
>
> The second patch sets the math_errhandling value based on library and
> hardware configuration so that applications can tell whether
> exceptions and errno are supported. This requires configuration on a
> per-target basis in machine/ieeefp.h; I've added configuration for
> arm, aarch64, x86, x86_64 and RISC-V. I think this could be automated.
>
> Reporting on whether exceptions are supported suffers from
> inconsistent exception support for hardware that uses a mixture of
> hardware and software for floating point operations. I haven't found
> any standards-conformant mechanism to use though.
>

Pushed with v2 of patch 2.


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer
Red Hat

Reply | Threaded
Open this post in threaded view
|

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

Sourceware - newlib list mailing list
In reply to this post by Sourceware - newlib list mailing list
Corinna Vinschen <[hidden email]> 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

signature.asc (847 bytes) Download Attachment