From patchwork Mon Oct 23 21:36:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 737304 Delivered-To: patch@linaro.org Received: by 2002:adf:dd81:0:b0:32d:baff:b0ca with SMTP id x1csp1735626wrl; Mon, 23 Oct 2023 14:37:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuVRblhgROCIvQfCOk0BfmXCqkO050nQjCDbnqb5X2F/mo1H5v2Bvpe3K/Cu0tdtRtHt92 X-Received: by 2002:a05:620a:4109:b0:778:af06:640e with SMTP id j9-20020a05620a410900b00778af06640emr11771007qko.16.1698097054260; Mon, 23 Oct 2023 14:37:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698097054; cv=pass; d=google.com; s=arc-20160816; b=XvJ6ltSTmxxjIJSL1LrmRAhpRTN0of0mSeYBLBJoeybM9bQCy82V4zPoMQ0rpl3J2L gV7SBs4Fx+VuE709PoLcKkfUO/rmoVRxvfhvuk5EgSD6iAWg/JkjrQ1ISbPdC2QU76B3 MdprFlFRVNRd1vgY3kaMgyYzYfbjbl3H3s/ymUEeRV8f/zCkvYaI0TaOZgTKhF2AI2Ja UKyQ2rdUQpWnY1EE8xjZ0DYEVWS4erndBJ3+PnSL+dyMS8wa2p1S/5+s0Wd4iTcT3Evt H4RnOHm5j3n3/MnSwRULvUDESrOpryQo6sLPZFQLEtfK5dA5IvDmnfWmagFcFfczogGG XfwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=Rf/HRFBHeyiybHiS9O3Be3TfMfF1DsAPHz9Yj4poVI0=; fh=Rk9vnpDApgYog/EhmGDFyutcQpHXbdLZmc0l8HNU7lw=; b=laUJRaCQH5O4MJpq0PlJJhdGuoeiheZQ6BKKke/6jS1/F1R7x2rtF5gp/lrVjksabI 0KrmRRWIdJpJbYeUd/3yJrZTG7H35E5QOPq90NWrVTsm+vA7E6VFgMtAzbPyI9ZJCRkO etpxwix0fKCEY33RojLaT4zKY+PezV8VyhwoWjh6tULC9YyjcEOygBvIQ522S9Wh/7RA aFjw8E4sAyGgiipaXCOrmsla4VbBdn4PUOJtBCA0umDD+jfTT49eHZAEpLt7eQeky/vN RiJHi4Z1UxPU2IfWo8fZkbt26YGuVOlnbBzzcq2ZujAOTPRGs0ZSng4mN2xwV3f2vWQf UMrQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=opxZvSVM; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bq32-20020a05620a46a000b007756a7d993dsi6473433qkb.252.2023.10.23.14.37.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 14:37:34 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=opxZvSVM; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D9C433857806 for ; Mon, 23 Oct 2023 21:37:33 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id D32B43858410 for ; Mon, 23 Oct 2023 21:37:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D32B43858410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D32B43858410 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698097030; cv=none; b=s20lJ6Hkjp62bitrVrimV18rOimtMqD38C1Ejr9DFJq3s63aoE+lCineGxvKhpGuzVb+BMhJzl5wObd6JGAvkDiEM0oGLYwMaDGA2khWRfd5wEcCuPEcjclhCWNmSdG5PbYnRALPrOa2UorvPGnVZWDLl1RCOO+pQjRjLU/ON1s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698097030; c=relaxed/simple; bh=8U8Nhj1fuh3PG40nimc7iSYU1s2WrtmRJcqYWR8ax7E=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=lSUFqyDiScNQqTBFli53IkcA82GB9mZj7DoBAf8cy3qNnFQEn1AYcF6OoBRUpB+5bYrWSwtt/1Yk4xwac+NvHK8qcCFJoHG7fGDuncjAtU1zNFAb1ISCGGBjA70uqS0JM6MWZE3KjJehBBfECziO7kiMv1Po4HHHtIpF0KFYlgk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6b87c1edfd5so2916222b3a.1 for ; Mon, 23 Oct 2023 14:37:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698097027; x=1698701827; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Rf/HRFBHeyiybHiS9O3Be3TfMfF1DsAPHz9Yj4poVI0=; b=opxZvSVMiSgcwFDZU5Gcp41HMwzBF+AQjZ8LfDSS3I9H/NR51XcF8Wf7cTUaopY84b zvlZ1A+CwzIt3vrkxKQdrb/TD3hQyniWGmitZeytURInobZh7cI5WIrA51xKeCdMCFHV 7vKUJeDCA/562hH9XFEjIcS/2YUEUQFOx/eZjeqg7sUkpZqKWg7JxEjAkCW1eAeuPJ+6 vmJFMKTi1YHyCpRjKa9yZLEd5bm5JFGHE/2JIruteHYpZ0sjKOnxBQ0zjP20F4q4c+s2 tc+MFiwUflUOXchLWbrN+XY6y9T/9gEDmuNZxpqRC9hw8ATMZsl4182/TVdF94lnZb+o Ljcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698097027; x=1698701827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Rf/HRFBHeyiybHiS9O3Be3TfMfF1DsAPHz9Yj4poVI0=; b=rcypxgWXNReGVp9sOsNSGA3IEYpj/ezawFKhvM9cTzVpp2hkdBtsuAQ4f4XVCPsyK5 XkrfMVyCbpRNybOmOwzlW/Sj7aNuP9eOuImYL2YOnCw+fgIxnC5u/kFHc0GnhS+hjwxT kPNXCU+7KIdfUhiFNnZ15Ed6xrGK+0t8rQzwIPIch9csD2UWL1mXFjU69K/wpKLeTTUj hAAD6I4Fr9yezS/8xWUiWVv4aJB1IMf9XlZ2yo44VTnRENvwzwUsgcrpecntHAfTWtxM QOiLGRj9tH53BrYJildIZWTvToCKuAKDeh7oCHdcMLTqjMa880iAeQIw32l++sQqnneD 3TBQ== X-Gm-Message-State: AOJu0YzTqLCCFm34oRObis+eI+I8shKhxZzZkxhwoniM9YuLiZayRzcP 42y+uONor6EADASjIKEeUbZgkXpOBtCi6sXRW6v0CQ== X-Received: by 2002:a05:6a20:158b:b0:17a:eddb:ac65 with SMTP id h11-20020a056a20158b00b0017aeddbac65mr893683pzj.9.1698097027266; Mon, 23 Oct 2023 14:37:07 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:a647:232:4b54:1e33:3494]) by smtp.gmail.com with ESMTPSA id fe12-20020a056a002f0c00b0066a4e561beesm6819443pfb.173.2023.10.23.14.37.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 14:37:06 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Bruno Haible Subject: [PATCH 2/3] i686: Do not raise exception traps on fesetexcept (BZ 30989) Date: Mon, 23 Oct 2023 18:36:58 -0300 Message-Id: <20231023213659.3236496-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231023213659.3236496-1-adhemerval.zanella@linaro.org> References: <20231023213659.3236496-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org According to ISO C23 (7.6.4.4), fesetexcept is supposed to set floating-point exception flags without raising a trap (unlike feraiseexcept, which is supposed to raise a trap if feenableexcept was called with the appropriate argument). The flags can be set in the 387 unit or in the SSE unit. To set a flag, it is sufficient to do it in the SSE unit, because that is guaranteed to not trap. However, on i386 CPUs that have only a 387 unit, set the flags in the 387, as long as this cannot trap. Checked on i686-linux-gnu. --- math/test-fesetexcept-traps.c | 11 ++++++ sysdeps/i386/fpu/fesetexcept.c | 43 ++++++++++++++++++++++-- sysdeps/i386/fpu/math-tests-trap-force.h | 29 ++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 sysdeps/i386/fpu/math-tests-trap-force.h diff --git a/math/test-fesetexcept-traps.c b/math/test-fesetexcept-traps.c index 96f6c4752f..122c23eb7e 100644 --- a/math/test-fesetexcept-traps.c +++ b/math/test-fesetexcept-traps.c @@ -19,6 +19,7 @@ #include #include #include +#include static int do_test (void) @@ -43,6 +44,16 @@ do_test (void) where setting the exception might result in traps the function should return a nonzero value. */ ret = fesetexcept (FE_ALL_EXCEPT); + + /* Execute some floating-point operations, since on some CPUs exceptions + triggers a trap only at the next floating-point instruction. */ + double a = 1.0; + double b = a + a; + math_force_eval (b); + long double al = 1.0L; + long double bl = al + al; + math_force_eval (bl); + if (ret == 0) puts ("fesetexcept (FE_ALL_EXCEPT) succeeded"); else if (!EXCEPTION_SET_FORCES_TRAP) diff --git a/sysdeps/i386/fpu/fesetexcept.c b/sysdeps/i386/fpu/fesetexcept.c index 18949e982a..a4c70cd1d1 100644 --- a/sysdeps/i386/fpu/fesetexcept.c +++ b/sysdeps/i386/fpu/fesetexcept.c @@ -17,15 +17,54 @@ . */ #include +#include int fesetexcept (int excepts) { + /* The flags can be set in the 387 unit or in the SSE unit. To set a flag, + it is sufficient to do it in the SSE unit, because that is guaranteed to + not trap. However, on i386 CPUs that have only a 387 unit, set the flags + in the 387, as long as this cannot trap. */ + fenv_t temp; + excepts &= FE_ALL_EXCEPT; + __asm__ ("fnstenv %0" : "=m" (*&temp)); - temp.__status_word |= excepts & FE_ALL_EXCEPT; - __asm__ ("fldenv %0" : : "m" (*&temp)); + + if (CPU_FEATURE_USABLE (SSE)) + { + /* Clear relevant flags. */ + temp.__status_word &= ~excepts; + + /* Store the new status word (along with the rest of the environment). */ + __asm__ ("fldenv %0" : : "m" (*&temp)); + + /* And now similarly for SSE. */ + unsigned int mxcsr; + __asm__ ("stmxcsr %0" : "=m" (*&mxcsr)); + + /* Set relevant flags. */ + mxcsr |= excepts & FE_ALL_EXCEPT; + + /* Put the new data in effect. */ + __asm__ ("ldmxcsr %0" : : "m" (*&mxcsr)); + } + else + { + /* Clear or set relevant flags. */ + temp.__status_word ^= temp.__status_word & excepts; + + if (temp.__control_word & temp.__status_word & excepts) + /* Setting the exception flags may trigger a trap (at the next + floating-point instruction, but that does not matter). + ISO C 23 ยง 7.6.4.5 does not allow it. */ + return -1; + + /* Store the new status word (along with the rest of the environment). */ + __asm__ ("fldenv %0" : : "m" (*&temp)); + } return 0; } diff --git a/sysdeps/i386/fpu/math-tests-trap-force.h b/sysdeps/i386/fpu/math-tests-trap-force.h new file mode 100644 index 0000000000..d88229c271 --- /dev/null +++ b/sysdeps/i386/fpu/math-tests-trap-force.h @@ -0,0 +1,29 @@ +/* Configuration for math tests: support for setting exception flags + without causing enabled traps. i686 version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef I386_FPU_MATH_TESTS_TRAP_FORCE_H +#define I386_FPU_MATH_TESTS_TRAP_FORCE_H 1 + +#include + +/* Setting exception flags in FPSCR results in enabled traps for those + exceptions being taken. */ +#define EXCEPTION_SET_FORCES_TRAP (CPU_FEATURE_USABLE (SSE)) + +#endif /* math-tests-trap-force.h. */