From: Bruno Haible <bruno@clisp.org>
To: cygwin@cygwin.com
Subject: feraiseexcept does not raise traps in Cygwin
Date: Wed, 25 Oct 2023 11:21:47 +0200 [thread overview]
Message-ID: <39807966.XM6RcZxFsP@nimes> (raw)
Hi,
Seen on
- Cygwin 3.4.6 or 2.9.0 on x86_64.
- Cygwin 2.9.0 on i386.
According to ISO C 23 § 7.6.4.3
"The feraiseexcept function attempts to raise the supported floating-point
exceptions represented by its argument. 266)
Footnote 266) The effect is intended to be similar to that of floating-point
exceptions raised by arithmetic operations. Hence, implementation extensions
associated with raising a floating-point exception (for example, enabled
traps or IEC 60559 alternate exception handling) should be honored."
This does not work. How to reproduce:
=================================== foo.c ===================================
#define _GNU_SOURCE 1
#include <fenv.h>
#include <assert.h>
int
main ()
{
/* Clear FE_INVALID exceptions from past operations. */
if (feclearexcept (FE_INVALID))
return 1;
/* An FE_INVALID exception shall trigger a SIGFPE signal, which by default
terminates the program. */
if (feenableexcept (FE_INVALID) == -1)
return 2;
if (feraiseexcept (FE_INVALID))
return 3;
return 0;
}
=============================================================================
For x86_64:
$ x86_64-pc-cygwin-gcc -Wall foo.c
$ ./a.exe; echo $?
For i386:
$ i686-pc-cygwin-gcc -Wall foo.c
$ ./a.exe; echo $?
Expected result (like seen e.g. on Linux/glibc):
Floating-point exception (core dumped)
136
Actual result:
0
The workaround for x86_64 is to redefine feeraiseexcept in the same way
as glibc does. This modified test program includes the workaround:
=================================== foo.c ===================================
#define _GNU_SOURCE 1
#include <fenv.h>
#include <assert.h>
/* The floating-point environment of the 387 unit. */
typedef struct
{
/* 7 32-bit words: */
unsigned short __control_word; /* fctrl register */
unsigned short __reserved1;
unsigned short __status_word; /* fstat register */
unsigned short __reserved2;
unsigned int more[5];
}
x86_387_fenv_t;
int
feraiseexcept (int exceptions)
{
exceptions &= FE_ALL_EXCEPT;
if ((exceptions & ~(FE_INVALID | FE_DIVBYZERO)) == 0 && 0)
{
/* First: invalid exception. */
if (exceptions & FE_INVALID)
{
double volatile a;
double volatile b;
a = 0; b = a / a;
(void) b;
}
/* Next: division by zero. */
if (exceptions & FE_DIVBYZERO)
{
double volatile a, b;
double volatile c;
a = 1; b = 0; c = a / b;
(void) c;
}
}
else
{
/* The general case. */
/* Set the bits in the 387 unit. */
x86_387_fenv_t env;
unsigned short orig_status_word;
__asm__ __volatile__ ("fnstenv %0" : "=m" (*&env));
orig_status_word = env.__status_word;
env.__status_word |= exceptions;
if (env.__status_word != orig_status_word)
{
__asm__ __volatile__ ("fldenv %0" : : "m" (*&env));
/* A trap (if enabled) is triggered only at the next floating-point
instruction. Force it to occur here. */
__asm__ __volatile__ ("fwait");
}
}
return 0;
}
int
main ()
{
/* Clear FE_INVALID exceptions from past operations. */
if (feclearexcept (FE_INVALID))
return 1;
/* An FE_INVALID exception shall trigger a SIGFPE signal, which by default
terminates the program. */
if (feenableexcept (FE_INVALID) == -1)
return 2;
if (feraiseexcept (FE_INVALID))
return 3;
return 0;
}
=============================================================================
(This workaround *should* also work on i386, but it doesn't. I don't know
why.)
Bruno
next reply other threads:[~2023-10-25 9:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-25 9:21 Bruno Haible [this message]
2023-10-25 22:39 ` feraiseexcept does not raise traps in newlib on x86/_64 [was Cygwin] Brian Inglis
2023-10-31 12:33 ` feraiseexcept does not raise traps in Cygwin Corinna Vinschen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=39807966.XM6RcZxFsP@nimes \
--to=bruno@clisp.org \
--cc=cygwin@cygwin.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).