delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2023/10/25/05:22:33

X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AB08385773C
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1698225750;
bh=MRz27CFNaszXF0AHAb4/rJxXq8HmgXOefofHLCauPQE=;
h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post:
List-Help:List-Subscribe:From:Reply-To:From;
b=SQkf0GTWJgy64HH4OtVJBIXnZqQxWCdwJQr1ZpcaeHjTH7upVx/anC3YO7qmHgtYC
Gn5qdvyAPtN8hHn7r9rJ5HmsqOFl+SgmNqUXwMsUFgebUhWqE7H0LlyQxH0x3DGllL
e0lDZfdHvEEBwn1VVcxhffQItVyAg1X63MYSq8do=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 74C9E3858C53
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 74C9E3858C53
ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1698225711; cv=pass;
b=juSBOk9/TK9GXqtuVCbF4HS4xldo9ZM/5mBO9Je7EA5+F54YUKClUiuB7+aH1uvfHXQ2QfcmlyRjYNVT29cst7NLkNpy7wByDQfNgOr1QayglFkSoOo1L7PpGkTt1wIQkWt04bhpR8OS7g50fqDxb0dyZvX2uS+4I35LjqVVvn8=
ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key;
t=1698225711; c=relaxed/simple;
bh=7wTs6eUCRzHeZZJAoReAt8sfYvS/qtx9/E7HACj3M8s=;
h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID:
MIME-Version;
b=LSpMfMUWPbAqbQaRTEC1iTbWSPypPYrLHdUvsnn7TkwVAa2jo9TZPOuVWu1J6JtlMd4aT4LiasDl0z/E5OU/WorCvKylauBxmjq/Ocx5Ed290bGc5K7WN7S0DkICOEVqcM5Q9V3VlKNtmfUhATnCe7F7WqK4mMvEvLNG7dZcOns=
ARC-Authentication-Results: i=2; server2.sourceware.org
ARC-Seal: i=1; a=rsa-sha256; t=1698225707; cv=none;
d=strato.com; s=strato-dkim-0002;
b=PBaXF08pTVaYr4TVkVahyMVayrjnXIyo0RL9l5GruEw1EyT+gWXGBRU9IZCmYotFw3
XHYIg2VhP628hbENkj6DCojTA8IwXtB0iI31aa8t++p01hXw/4JEeUDonzaLBK4LaB5+
QtfkH6GaWCBkrhY1mMAJgkfZoHeOBRqc8OYYHsUK3dy7SSccRmIPoi74aPYnkPu+y70m
tkRqwCuTauKOHVuWuAfN1Ao08dz4Sy4+XQTtSRfEBFGJfj9LhsdfZ9VZRjHaFYcTyqZn
bGQ8rm92Vx4+hFM/chcueCGKwD1GWurmPWXJoXT9Yz+N31xY4mmgKg4CjMSr/M0asz8D
ppTg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1698225707;
s=strato-dkim-0002; d=strato.com;
h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender;
bh=4l9d1n+VpxUigEntbdKpS1bVuHv6+1dOky+Z/e//AUU=;
b=AhhEt/Hw1qBSqAbC8/gbA6WYaoZJfrlQ5NDqaXG6FAgnzMMWh6ihoVXXOKYWi+yme6
W+AVKotPNTAv4x6JNELEQ7nU67FEKMTTB+KbVfrFjhiWRMsaD9hkRxoK69rcIwGAUuF8
AgXdxA4+KfpibNjT0RCDYmBmvU1RlI6MR9gfO7//NM2YPtNRgL9JYwwFdx/JIuIrLAlt
MtVJtXzVrVVbIqvfkK035RDPly6sdsQIMubKn1H+O/0PUEv/AD9jVpvejKvMIuMZ6gxY
kYJhsamDZTOP6vD4/lVZxo54Kdkuz53rKdIONstS6fc2LLvm4W/1hKtpb6a1auFtmWie
j09w==
ARC-Authentication-Results: i=1; strato.com;
arc=none;
dkim=none
X-RZG-CLASS-ID: mo00
X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpOQjaEBbGRPJOsIn/JZIZXFBU8r/A=="
To: cygwin AT cygwin DOT com
Subject: feraiseexcept does not raise traps in Cygwin
Date: Wed, 25 Oct 2023 11:21:47 +0200
Message-ID: <39807966.XM6RcZxFsP@nimes>
MIME-Version: 1.0
X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00, DKIM_SIGNED,
DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS,
RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,
SPF_NONE, TXREP autolearn=no autolearn_force=no version=3.4.6
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on
server2.sourceware.org
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Bruno Haible via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Bruno Haible <bruno AT clisp DOT org>
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com>
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id 39P9MX6Y010949

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




-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019