From patchwork Fri Nov 3 12:24:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 740659 Delivered-To: patch@linaro.org Received: by 2002:a5d:538f:0:b0:32d:baff:b0ca with SMTP id d15csp1335171wrv; Fri, 3 Nov 2023 05:25:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHipxQPj0SR6r8oK7Or/OBfTbRxa5HtDhN+sEbrNvNsZsypT26q23u+PjLGlFccRuosiSBp X-Received: by 2002:ac8:7f08:0:b0:41e:3795:8b7d with SMTP id f8-20020ac87f08000000b0041e37958b7dmr21677723qtk.61.1699014301377; Fri, 03 Nov 2023 05:25:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1699014301; cv=pass; d=google.com; s=arc-20160816; b=cAotVjAXfNnwbTrZCKyWn5kiDL6vWtYolEPwei/nsDMBv9iU6ERKqBiFF2hkyEJrap pHWq6ZwyfCb83AqZQ8AX1HtnYSn73Q3DyAzb1i23TeFCYVO3LX2HY+o/Rnny2yFUjT7s k0C7J/5VASiAMJwxkRpF2rK5pkmoF4c4GMolKlylfMKH7NWzCPHH+Ndmg9xkuHxYhIk+ r6+WOCEBaHG8ddZEMei8I5bnKdfM/UFc77xLmHGhZ3bUNYXRQmoNnR2aN9/JQbQCL3Mc PujOP5R1ENQh6EPXYmJSbYfSNCrDYW9tCHvIGq3C4atnsnP+vQQE6wiGovJnSfPul1XV gm3g== 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:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=8ujOWCJ4a4246lq6dUQMnVl3c7/GQti81/iYWZLRcvg=; fh=01HD8dtuIqmy9rV/eMJuvD8dAGq4TXBqBjIxWaMzNQI=; b=y3Kz21O7vJECaMRCaHsXsnAwQyEZtRaDPUbHLs0zwvAf2j++ygjFbFPYwsLJGgRXPB L5GHygR3hBuFNOo3zAjlFolDibikhkQn2St2dN7aSZnF+f9DK/LM0cACsM3fbBeuCoyC m58TKyGh4Os+3rgMk80iDt/CuZ+8v6kvddDC8kEl2zSSsvw++AdBemfUWpT4dk9ynDSB 240kfqTw4y28XM2Bs00Yzak9i6DM6Fn5yhS9L9clS23UhTVPBEGuzgCzX4uh82dN2f/y UtCevM4X4m8mb90gX/DfXrDQIGQ2eY3jlJAlzjU0pMDHFfhW7n8f+jg1tkC/mP/36/5+ vKjg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UYfAoR0R; 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 v8-20020a05622a014800b004055a48136fsi1386064qtw.129.2023.11.03.05.25.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 05:25:01 -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=UYfAoR0R; 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 454D1385C6E2 for ; Fri, 3 Nov 2023 12:24:57 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by sourceware.org (Postfix) with ESMTPS id 5458D3858C41 for ; Fri, 3 Nov 2023 12:24:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5458D3858C41 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 5458D3858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::729 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699014276; cv=none; b=QOIK23hGYGPdBlv9UpOuxiGJetFZ1J2J7ZZSR/vR1AI/GsJtPOdDE7ZCnzDOKShRaTChIfI0fbkfxGckKwi3cu498+LP5BBnBwn/2Wg1kfaLzHZttddYnQDjDb2wS9+ByjJbFdDlCiFU569LgqOiR9sE9Mo+RzbUNlH6Dh8p2Mg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699014276; c=relaxed/simple; bh=Tl/wbB2GCBTttl5IJGjM/4/upVxLAhwHvIgVt5uBijo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=PFUXGMBJosre7s7SWiWZFUMgTqrdL+JHjsXkR2CIokuHOICyQme+8Mf77MrijxleC+k9c3WSMe6ecD9kv4g9R4S1RD2bZq32frAb0iSD63hqtD4tYa+X/reuoT0CqZbfh+SoWU2Lg/5Gc0i1kdfUPQAPYxSWIEqQgwEawbRTtrU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x729.google.com with SMTP id af79cd13be357-77896da2118so110952185a.1 for ; Fri, 03 Nov 2023 05:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699014265; x=1699619065; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=8ujOWCJ4a4246lq6dUQMnVl3c7/GQti81/iYWZLRcvg=; b=UYfAoR0RHDYleHgUKpgELBmWc4/H8KACGflSoxMtpyxtFsm01FYcSHuiVr1u+B6Zzl h7PDBSkyzMHUKYe5KEoRhTamgWUFHLYQBL5pIR1AR3yRehwdlx8OPQtoCDauJ6f+LFse lddZt4p8QYabaz7gw69RdguphW6TbqxouYQbDGeqRe7Iv0hww+rJDu5tPEEaJHFLQt7t FlVX+LW+dn+zQIgJRYlU9wxCZYz8TDFo4IXBcrgp6hL9xTAjWmthribES9jasgoWJkHz OmLabB7N/hBDtYtWn+lrOYcutgqpU1hDqxT/n6CoGiCnfC6aTh27Gx0UjRy5AwrfqxGv 6SsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699014265; x=1699619065; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8ujOWCJ4a4246lq6dUQMnVl3c7/GQti81/iYWZLRcvg=; b=vYDE40lmh+cX0q3/S4Ef+gW+gdFsCgUdRb1PEJj1DxnveZaRJKRiZQVFxpc2yERC0r zkMAZseK7Of6kSHSRSLN6kKXZDxNYCDl/yN4dski3ok2+9Ia1Z9cYnEVr1sZGM2QQNIp y7s3gh607w/0sng3B+elSEtIkTh++gzwjRE7mKiQxUZrFGdSDPnyyPAtcNSx1zZWZlfX G49xfBWo8WE1OY1K9pG4SD4eV/PKI583YKbJgOElGbtASSTeALQK5LSPUOv1XIKr4PLQ jU7B45CTK2iNaDpAsvXUrKdZG2L/7zX8KwFYwTLHYgU8HsFkDYTQzk4knABJVcOpCQEl 94kg== X-Gm-Message-State: AOJu0YxWWtNFxxvrcBTOfvTZfFpxbj0OxJ6x/2DVOLirnYGrVl7tD7mm 6A08MBPNqvg72zalPSPLizyJshltw6HwBiyXlWUCBw== X-Received: by 2002:a05:620a:4785:b0:77a:1d5b:2cc8 with SMTP id dt5-20020a05620a478500b0077a1d5b2cc8mr17698599qkb.44.1699014264892; Fri, 03 Nov 2023 05:24:24 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:a715:dd81:f992:f726:338a]) by smtp.gmail.com with ESMTPSA id e15-20020a05620a12cf00b0076eee688a95sm693833qkl.0.2023.11.03.05.24.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 05:24:24 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Bruno Haible Subject: [PATCH 2/7] i686: Do not raise exception traps on fesetexcept (BZ 30989) Date: Fri, 3 Nov 2023 09:24:11 -0300 Message-Id: <20231103122416.2724355-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231103122416.2724355-1-adhemerval.zanella@linaro.org> References: <20231103122416.2724355-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 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, T_SCC_BODY_TEXT_LINE 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 | 28 ++++++++++++--- sysdeps/i386/fpu/fesetexcept.c | 46 +++++++++++++++++++++--- sysdeps/i386/fpu/math-tests-trap-force.h | 29 +++++++++++++++ sysdeps/x86/fpu/test-fenv-sse-2.c | 23 +++--------- 4 files changed, 100 insertions(+), 26 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..8a5c0bca80 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) @@ -41,8 +42,28 @@ do_test (void) /* Verify fesetexcept does not cause exception traps. For architectures where setting the exception might result in traps the function should - return a nonzero value. */ - ret = fesetexcept (FE_ALL_EXCEPT); + return a nonzero value. + Also check if the function does not alter the exception mask. */ + { + int exc_before = fegetexcept (); + ret = fesetexcept (FE_ALL_EXCEPT); + int exc_after = fegetexcept (); + if (exc_before != exc_after) + { + puts ("fesetexcept (FE_ALL_EXCEPT) changed the exceptions mask"); + return 1; + } + } + + /* 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) @@ -61,5 +82,4 @@ do_test (void) return result; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include diff --git a/sysdeps/i386/fpu/fesetexcept.c b/sysdeps/i386/fpu/fesetexcept.c index 18949e982a..2e9580207c 100644 --- a/sysdeps/i386/fpu/fesetexcept.c +++ b/sysdeps/i386/fpu/fesetexcept.c @@ -17,15 +17,53 @@ . */ #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; + + /* Note: fnstenv masks all floating-point exceptions until the fldenv + or fldcw below. */ + __asm__ ("fnstenv %0" : "=m" (*&temp)); + + /* Set relevant flags. */ + 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. */ + __asm__ volatile ("fldcw %0" : : "m" (*&temp.__control_word)); + 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..f41e1ffc2d --- /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. */ diff --git a/sysdeps/x86/fpu/test-fenv-sse-2.c b/sysdeps/x86/fpu/test-fenv-sse-2.c index f3e820b6ed..7a0503790f 100644 --- a/sysdeps/x86/fpu/test-fenv-sse-2.c +++ b/sysdeps/x86/fpu/test-fenv-sse-2.c @@ -22,17 +22,8 @@ #include #include #include - -static bool -have_sse2 (void) -{ - unsigned int eax, ebx, ecx, edx; - - if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) - return false; - - return (edx & bit_SSE2) != 0; -} +#include +#include static uint32_t get_sse_mxcsr (void) @@ -164,13 +155,9 @@ sse_tests (void) static int do_test (void) { - if (!have_sse2 ()) - { - puts ("CPU does not support SSE2, cannot test"); - return 0; - } + if (!CPU_FEATURE_USABLE (SSE2)) + FAIL_UNSUPPORTED ("CPU does not support SSE2"); return sse_tests (); } -#define TEST_FUNCTION do_test () -#include +#include