public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2] libm: Changes to errno and math_errhandling
@ 2020-08-03 17:55 Keith Packard
  2020-08-03 17:55 ` [PATCH 1/2] libm/common: Set WANT_ERRNO based on _IEEE_LIBM value Keith Packard
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Keith Packard @ 2020-08-03 17:55 UTC (permalink / raw)
  To: newlib

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.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] libm/common: Set WANT_ERRNO based on _IEEE_LIBM value
  2020-08-03 17:55 [PATCH 0/2] libm: Changes to errno and math_errhandling Keith Packard
@ 2020-08-03 17:55 ` Keith Packard
  2020-08-03 17:55 ` [PATCH 2/2] libm: Set math_errhandling to match library and hardware Keith Packard
  2020-08-04 17:31 ` [PATCH 0/2] libm: Changes to errno and math_errhandling Corinna Vinschen
  2 siblings, 0 replies; 6+ messages in thread
From: Keith Packard @ 2020-08-03 17:55 UTC (permalink / raw)
  To: newlib

_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 <keithp@keithp.com>
---
 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/2] libm: Set math_errhandling to match library and hardware
  2020-08-03 17:55 [PATCH 0/2] libm: Changes to errno and math_errhandling Keith Packard
  2020-08-03 17:55 ` [PATCH 1/2] libm/common: Set WANT_ERRNO based on _IEEE_LIBM value Keith Packard
@ 2020-08-03 17:55 ` Keith Packard
  2020-08-04  8:57   ` Corinna Vinschen
  2020-08-04 17:31 ` [PATCH 0/2] libm: Changes to errno and math_errhandling Corinna Vinschen
  2 siblings, 1 reply; 6+ messages in thread
From: Keith Packard @ 2020-08-03 17:55 UTC (permalink / raw)
  To: newlib

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(-)

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] libm: Set math_errhandling to match library and hardware
  2020-08-03 17:55 ` [PATCH 2/2] libm: Set math_errhandling to match library and hardware Keith Packard
@ 2020-08-04  8:57   ` Corinna Vinschen
  2020-08-04 18:11     ` Keith Packard
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2020-08-04  8:57 UTC (permalink / raw)
  To: Keith Packard; +Cc: newlib

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/2] libm: Changes to errno and math_errhandling
  2020-08-03 17:55 [PATCH 0/2] libm: Changes to errno and math_errhandling Keith Packard
  2020-08-03 17:55 ` [PATCH 1/2] libm/common: Set WANT_ERRNO based on _IEEE_LIBM value Keith Packard
  2020-08-03 17:55 ` [PATCH 2/2] libm: Set math_errhandling to match library and hardware Keith Packard
@ 2020-08-04 17:31 ` Corinna Vinschen
  2 siblings, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2020-08-04 17:31 UTC (permalink / raw)
  To: Keith Packard; +Cc: newlib

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] libm: Set math_errhandling to match library and hardware
  2020-08-04  8:57   ` Corinna Vinschen
@ 2020-08-04 18:11     ` Keith Packard
  0 siblings, 0 replies; 6+ messages in thread
From: Keith Packard @ 2020-08-04 18:11 UTC (permalink / raw)
  To: Corinna Vinschen; +Cc: newlib

[-- Attachment #1: Type: text/plain, Size: 4611 bytes --]

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-08-04 18:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-03 17:55 [PATCH 0/2] libm: Changes to errno and math_errhandling Keith Packard
2020-08-03 17:55 ` [PATCH 1/2] libm/common: Set WANT_ERRNO based on _IEEE_LIBM value Keith Packard
2020-08-03 17:55 ` [PATCH 2/2] libm: Set math_errhandling to match library and hardware Keith Packard
2020-08-04  8:57   ` Corinna Vinschen
2020-08-04 18:11     ` Keith Packard
2020-08-04 17:31 ` [PATCH 0/2] libm: Changes to errno and math_errhandling Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).