From patchwork Tue Oct 24 11:37:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 737535 Delivered-To: patch@linaro.org Received: by 2002:adf:dd81:0:b0:32d:baff:b0ca with SMTP id x1csp1978019wrl; Tue, 24 Oct 2023 04:37:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHe7ZN2f+TRIylmF+8fcRFIYKUpIDKG1aRp+S2Z+PDr4mTDyC1vxExK2yfa5kdGbJopRRSD X-Received: by 2002:a05:622a:24f:b0:41c:e161:c99a with SMTP id c15-20020a05622a024f00b0041ce161c99amr10425645qtx.56.1698147455996; Tue, 24 Oct 2023 04:37:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698147455; cv=pass; d=google.com; s=arc-20160816; b=PlPK5tSF/Tb9/4YrjdtfwKlv7ZzRMDUPg/0fwUAhU2+I/fMPsyUiy/gWEEjkdkMCct HPGhyNl9OmGmpYUwgVweLC5XX1XUWmHa0TGn86cS3YRUshxyEj/8nvNA9FxIzXcwboOy oD5lkTK1Q0XqmIbjMsR+8/m4v+JqVI5RrEyqlagPdgy0br8f+PG1L0jGtKCKZ0Mbs8QY CotsYT7yhzQME5kv+OPQ5vf9ScItvkAprLWIs43H5060pt78ehcavW0MPEAqg0w6/1wl VPn+ewYvAZaXrHG5bLfuqxT4ZHApr1FMHAG10CkThg71DLKcqoJBBdTufcKkLnDSSLXL Cu8A== 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=xyN0+MPmXPFjdAwhfIEaZ5KCieYNszKZx9zB2QRUtIw=; fh=Rk9vnpDApgYog/EhmGDFyutcQpHXbdLZmc0l8HNU7lw=; b=pCtyw14e1BUPbn1oNTxjaQdps6HP/kQ0juh5RWgmmdSG3fPnBQUw+1vQcrN0Tf6KkW XGgQAUssqDO0EwJHTG0O8BAmaoLioN5pOQP3giPqgQD32Yo4NMYVwcuHVNsy2gpc0dDz EZdMw/+xxG8vpqE1HorgEldKyylDACWtcACuhyomkKYNncl+p752QhcVHSTZ8aYe0z/Q 5sZpbmGXF2VcDE0UrWZ4wKHhRL+cQG8PjcMbVryEmY61p/a8YhCXps1pix9WwsdL1/Lb HOMBVfZaFjv6EjikhQa6cyiITHBFowR1ufyXYab+4PD4Nn4eFM+h0IZP1tC/CKkfICoU St6w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YZ7vaKTy; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id p8-20020a05622a13c800b0041820c4f419si7059597qtk.782.2023.10.24.04.37.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:37:35 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YZ7vaKTy; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 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 A2D5938582B7 for ; Tue, 24 Oct 2023 11:37:35 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id B22A93858C2C for ; Tue, 24 Oct 2023 11:37:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B22A93858C2C 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 B22A93858C2C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698147450; cv=none; b=re55Iw+rcc8ITX/ZgqDqfBBsQLw2OIjibeXm1gksWXq4gVZiu6wgeezffZ6hbVyueMm2TASic0fe1n6yZIqPfIdx1wo5UUAVQ2/whMIedLwvEUTqxSOcX4MIIv/jNmR/cXE+2tSoD2+0e0TFS+IfBwumYIkbFfCJ+e/FgvzyCw8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698147450; c=relaxed/simple; bh=F5dZBtGI0oHO9/8EX6VjjBDGl5NVNM4NeMvraHeIR54=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Y/895SK40Icb+5HjK8Mjki/NAGwU/GTeriA/iasEoLxGxBRS3CxQxG6mzuoh/JLqp/bzepzeK0aVjmAs0cnv91VETSGP/JatZozpyq0x6kI0RlI+OieRYs6Zknes6JBFXgG2RJNCS8J7rb4gI41zATK015+G5BVU33XPchCVRDE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-5ab94fc098cso2522385a12.1 for ; Tue, 24 Oct 2023 04:37:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698147447; x=1698752247; 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=xyN0+MPmXPFjdAwhfIEaZ5KCieYNszKZx9zB2QRUtIw=; b=YZ7vaKTyMmBuiUNCKjknCoYUD+vwgfQss6CooGnYz/CLl8LAh8CVmpDxU3axLr3z1T ghY372hGi5nQs8QeNjtwvIeyiN2yrFjr45U31wHMqtZNo9yeHWvx09nMC+yE8Q/ogL+N N43ann2NiqwXOEhdIc9cNYBPQZJCHFA5Lq0LDvkQWpaTxMUOp15RzUkdjs8coYOcGFeD rvYqcKsPh4pX6PpzVRXDog4rajATnVoBBTECoLBFiqX53fYUNJLKXL4ULiKfZb1BbhAo CH2Ng2ZX1I/oXMGj1nOUGJLiW03m3B1CuNeZ/oxrPVuzEPd/2WHqco4VxXYUrFF/+8X7 Nc+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698147447; x=1698752247; 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=xyN0+MPmXPFjdAwhfIEaZ5KCieYNszKZx9zB2QRUtIw=; b=nRQnFWr0zB/bQirv7XDe3R9iwHyO6u5BW7/zNZifNwuJ38ULyrx3i2CZyNyMAx8X9R WO9NoYsccLTbyGEXfCH38yk7kbl3/iaeKV7wa/n76eTzTCkTbYdmaR06CW0HOCtk8BfH qQGNnoN/Au8Mz001z5V3FP90ma76LU7IlaDElUc3NoYgtl+t/Ao45bPndQLvau+7/qp+ G5aSk1uRZwtHsLDK0sR3L+LMU68RImFqkPCPC8UOU/4dA3p53QqxzaUq2PwbklnKyxXM v5q66Ec3HVnENOXefi7jJkpRWm/IH9YEV8/9DsohcjgEViZJbfBVcBkO8xG/nSXyPTjM 69Jg== X-Gm-Message-State: AOJu0YwqW8phYrbMMHWuNa6CwOH6kS9QOk64nxNH6APMG7QzLRP72MzI CWSR6OipVkLPeKXLNRLRJ5Wu7GKfQsJ28MOgnz3uwg== X-Received: by 2002:a05:6a20:258a:b0:161:76a4:4f79 with SMTP id k10-20020a056a20258a00b0016176a44f79mr2379434pzd.23.1698147447128; Tue, 24 Oct 2023 04:37:27 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:a647:44d5:84b0:a3f2:fe73]) by smtp.gmail.com with ESMTPSA id k28-20020aa79d1c000000b006bde2480806sm7506663pfp.47.2023.10.24.04.37.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:37:26 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Bruno Haible Subject: [PATCH v2 2/3] i686: Do not raise exception traps on fesetexcept (BZ 30989) Date: Tue, 24 Oct 2023 08:37:15 -0300 Message-Id: <20231024113716.3911015-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024113716.3911015-1-adhemerval.zanella@linaro.org> References: <20231024113716.3911015-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 | 41 +++++++++++++++++++++--- sysdeps/i386/fpu/math-tests-trap-force.h | 29 +++++++++++++++++ 3 files changed, 77 insertions(+), 4 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..2f63e9ba23 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. */ + volatile double a = 1.0; + volatile double b = a + a; + math_force_eval (b); + volatile long double al = 1.0L; + volatile 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..6eeb5ab5b0 100644 --- a/sysdeps/i386/fpu/fesetexcept.c +++ b/sysdeps/i386/fpu/fesetexcept.c @@ -17,15 +17,48 @@ . */ #include +#include int fesetexcept (int excepts) { - fenv_t temp; + /* 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. */ - __asm__ ("fnstenv %0" : "=m" (*&temp)); - temp.__status_word |= excepts & FE_ALL_EXCEPT; - __asm__ ("fldenv %0" : : "m" (*&temp)); + excepts &= FE_ALL_EXCEPT; + + if (CPU_FEATURE_USABLE (SSE)) + { + /* And now similarly for SSE. */ + unsigned int mxcsr; + __asm__ ("stmxcsr %0" : "=m" (*&mxcsr)); + + /* Set relevant flags. */ + mxcsr |= excepts; + + /* Put the new data in effect. */ + __asm__ ("ldmxcsr %0" : : "m" (*&mxcsr)); + } + else + { + fenv_t temp; + + __asm__ ("fnstenv %0" : "=m" (*&temp)); + + /* Clear or set relevant flags. */ + temp.__status_word |= temp.__status_word & excepts; + + if ((~temp.__control_word) & excepts) + /* Setting the exception flags may trigger a trap (at the next + floating-point instruction, but that does not matter). + ISO C23 (7.6.4.4) 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..20f4ead98d --- /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 FPU Status Register results in enabled traps for + those exceptions being taken. */ +#define EXCEPTION_SET_FORCES_TRAP (CPU_FEATURE_USABLE (SSE)) + +#endif /* math-tests-trap-force.h. */