From patchwork Tue Oct 24 11:37:16 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: 737536 Delivered-To: patch@linaro.org Received: by 2002:adf:dd81:0:b0:32d:baff:b0ca with SMTP id x1csp1978513wrl; Tue, 24 Oct 2023 04:38:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHaxllRfoNKtxDhTFYVG3yg0c95DJPKSCwD4i8n2pt/5eHLRrIaqBN7q848ZTr0sOnuYhh2 X-Received: by 2002:a05:6214:da7:b0:65a:fcc7:d77 with SMTP id h7-20020a0562140da700b0065afcc70d77mr14035573qvh.55.1698147539636; Tue, 24 Oct 2023 04:38:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698147539; cv=pass; d=google.com; s=arc-20160816; b=GhKQ3LDJ7NK9NEE9SZaeZ2vqIz8nqGHZPh3eRYRLKZl+KWbRZrlhXiq9XSD5kqCuoO W7uhHG0P7iSyc/a1118vl8Z8FguE9ccZHlrv+itC9LyWRr0OrVB8ZNMT3kI1ySn9zAgR 1/12RWyQiUuJMA/shF8aL6EpVVRn/tuNZtFtb+BiTcIRvpfKJsSXB0wQKoE/p3RVgLkp thxTKkgIzS2eAZ86dEFWKh8qVpZ9p+zLbxQCvls9FXaUQcXjX+07uPWJXNuoCefltyLA U0oCHOZFXbvCdiFKUKgB9EA/cL+/KS2VMfTtdQg4cbg3DXMOTrWFjq0W2Kh7cV/Zb9WP /DCA== 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=Gddms76rhRzGJL+ypGhaxMa8z8FDoni+edQVUGF+F0E=; fh=Rk9vnpDApgYog/EhmGDFyutcQpHXbdLZmc0l8HNU7lw=; b=hWFj9y8XTexBQ3jGgDkmJwWiJGH91BxJ6/TsZPy40v2VdLVpSe1QfieZPEJ1Liq+RZ 5f6FfVtbSrtetjAjSt//qPw0GAgLZmXzV0BHmtSwKkzqAG+pHmMgJO1eG3sZUfWQqlHa 0A0attFJxxd0VjUtAGiEq5/Vk9UeJ+RJOlOJdsJKYZezqqKk/EFj+HD8HfoSsbKpZ5dM 6Wo+L514Tk4ug9kQ1gObzIBJhpXcsMbRD9ku+9JRf50T6zvH2ZGrnCDD7Eq3CMxxlfkN +tg8t6nrQvU3uMKJ6cCA5CQ69NqoeusXw5mY/LmHxcztu5grUP4ghD8Ito4N16TT0ANz 42pg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QhMAr61D; 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 da18-20020a05621408d200b0065b2d89bd70si6843327qvb.522.2023.10.24.04.38.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:38:59 -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=QhMAr61D; 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 2EA303856DC6 for ; Tue, 24 Oct 2023 11:38:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id C7C3D3858CDA for ; Tue, 24 Oct 2023 11:38:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7C3D3858CDA 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 C7C3D3858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::533 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698147533; cv=none; b=tzwXfXgfgaUZZT8wLPr+0mkrOF06Ug4p+MLP+Lvjsvcj+LpYeKAI8iwX2K+ElDSSIS5OnlKjhdkqPag4IN3euj1HtyTbNbIw0k5Ky4JA+GfoNJ2gbAefAKcu4HFVC5N8xYysndZ2eJ+sytmDWxgpK4Pt1pWzgJV00U3JC0a3mg0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698147533; c=relaxed/simple; bh=lEwbYBQph18tUUfmlxgyUMDyN+LkoVe3f6Ln9xKesf0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=BTsW1w/2EF7Dq8FGiXZx0TPMV4SEt3Ohe3gIqqZ/0tlozhmYN8h2y0qG6L9sKzIZNcy6N5jJ7ka8ShfdDfv/92mHB1t42as5iCPBzNBqFMkcHEPVd0A24OeicJ38i+0Xa5+ZUi1wSNi7GFzKA/mGQqpesrftenjYbegMCejd7Qw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-53fa455cd94so2680728a12.2 for ; Tue, 24 Oct 2023 04:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698147530; x=1698752330; 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=Gddms76rhRzGJL+ypGhaxMa8z8FDoni+edQVUGF+F0E=; b=QhMAr61DDaCKgn6OsubtIBpoKmDSUQAtWIgCMn9/5GhfJXkQYJ7bRSHL0I4gB/dQNx 0XVV3AO+oKsa7+xiiRxJBMlZHvGNoUw0s2V0BGEmHU9YCrXFppe7wZrwo21vbhxk1DuA CjJ1qOWU3yvValh8ssmhGvEcPbmnpbiJ1P7ta+R0D1IVrPuzZWPemHfoiS0WcyePQLLx Fvqh4l94HIzlyeBKQpxeORSrbJxufwxBzwrIKlYfS5Sw+15I6Joz/NNsPRQqdexNq6IZ s4O+/kLnrMpmvVaRBc4Rb8e/i/reciy6r55avnI/Rn1ssDyXn9WeWIdFx6wWPJvP21zu 0xQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698147530; x=1698752330; 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=Gddms76rhRzGJL+ypGhaxMa8z8FDoni+edQVUGF+F0E=; b=bs1lFCx3V8e+yMijjRppgfA7pjEj9P3H8+aCA6lCcNoKeQNcbivtdEOuJ4oBMxNDWo xUV4yJdtEvJpB+GyXvcvlvmrs+xxPY4ScQmRo1ZSQ8tykgMheqjkoMr3Wh86Q6CWnLnl tnoJSc3x9kaceslKKt4FePKFW9qmQXk+5aXW6ioFZRWeEilnUAx0eWHLc6xFU24iDYvd xq8nYkMQas1V69XrVbAYVlD5V9MSMc5RCdjQxah0YyBUL6yfS+54682mtCmntp++2vy3 0gfVcBXnbp4BaygJhASKvfd6S3AGyLeCmZ2L4X0qCg5wIlvnb8tdmR+YTvRXUb73+GP7 MtcA== X-Gm-Message-State: AOJu0YyBtMHK418s+b6jtuXL+yP2aXvlpxyiqkuF/do0Ded8zJ3TP9mF NwJdDMT/33oClc7K1n/XyV2tZ089QDxgtU8ltNNZ7g== X-Received: by 2002:a05:6a21:35c6:b0:17b:63ce:5b84 with SMTP id ba6-20020a056a2135c600b0017b63ce5b84mr2268222pzc.52.1698147530238; Tue, 24 Oct 2023 04:38:50 -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.38.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:38:49 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Bruno Haible Subject: [PATCH v2 3/3] x86: Do not raises floating-point exception traps on fesetexceptflag (BZ 30990) Date: Tue, 24 Oct 2023 08:37:16 -0300 Message-Id: <20231024113716.3911015-4-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.5 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 From: Bruno Haible 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. When we need to clear a flag, we need to do so in both units, due to the way fetestexcept is implemented. When we need 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. Co-authored-by: Adhemerval Zanella --- math/test-fexcept-traps.c | 11 ++++++ sysdeps/i386/fpu/fsetexcptflg.c | 58 ++++++++++++++++++++----------- sysdeps/x86_64/fpu/fsetexcptflg.c | 24 +++++++------ 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/math/test-fexcept-traps.c b/math/test-fexcept-traps.c index 9b8f583ae6..f10bf2d9a3 100644 --- a/math/test-fexcept-traps.c +++ b/math/test-fexcept-traps.c @@ -19,6 +19,7 @@ #include #include #include +#include static int do_test (void) @@ -67,6 +68,16 @@ do_test (void) where setting the exception might result in traps the function should return a nonzero value. */ ret = fesetexceptflag (&saved, 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 && !EXCEPTION_SET_FORCES_TRAP) { puts ("fesetexceptflag failed"); diff --git a/sysdeps/i386/fpu/fsetexcptflg.c b/sysdeps/i386/fpu/fsetexcptflg.c index e724b7d6fd..ccbcf35e8e 100644 --- a/sysdeps/i386/fpu/fsetexcptflg.c +++ b/sysdeps/i386/fpu/fsetexcptflg.c @@ -17,42 +17,58 @@ . */ #include -#include -#include #include -#include int __fesetexceptflag (const fexcept_t *flagp, int excepts) { - fenv_t temp; + /* The flags can be set in the 387 unit or in the SSE unit. When we need to + clear a flag, we need to do so in both units, due to the way fetestexcept + is implemented. + When we need 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. */ - /* Get the current environment. We have to do this since we cannot - separately set the status word. */ - __asm__ ("fnstenv %0" : "=m" (*&temp)); + fenv_t temp; - temp.__status_word &= ~(excepts & FE_ALL_EXCEPT); - temp.__status_word |= *flagp & excepts & FE_ALL_EXCEPT; + excepts &= FE_ALL_EXCEPT; - /* Store the new status word (along with the rest of the environment. - Possibly new exceptions are set but they won't get executed unless - the next floating-point instruction. */ - __asm__ ("fldenv %0" : : "m" (*&temp)); + /* Get the current x87 FPU environment. We have to do this since we + cannot separately set the status word. */ + __asm__ ("fnstenv %0" : "=m" (*&temp)); - /* If the CPU supports SSE, we set the MXCSR as well. */ if (CPU_FEATURE_USABLE (SSE)) { - unsigned int xnew_exc; + unsigned int mxcsr; + + /* Clear relevant flags. */ + temp.__status_word &= ~(excepts & ~ *flagp); - /* Get the current MXCSR. */ - __asm__ ("stmxcsr %0" : "=m" (*&xnew_exc)); + /* Store the new status word (along with the rest of the environment). */ + __asm__ ("fldenv %0" : : "m" (*&temp)); - /* Set the relevant bits. */ - xnew_exc &= ~(excepts & FE_ALL_EXCEPT); - xnew_exc |= *flagp & excepts & FE_ALL_EXCEPT; + /* And now similarly for SSE. */ + __asm__ ("stmxcsr %0" : "=m" (*&mxcsr)); + + /* Clear or set relevant flags. */ + mxcsr ^= (mxcsr ^ *flagp) & excepts; /* Put the new data in effect. */ - __asm__ ("ldmxcsr %0" : : "m" (*&xnew_exc)); + __asm__ ("ldmxcsr %0" : : "m" (*&mxcsr)); + } + else + { + /* Clear or set relevant flags. */ + temp.__status_word ^= (temp.__status_word ^ *flagp) & 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)); } /* Success. */ diff --git a/sysdeps/x86_64/fpu/fsetexcptflg.c b/sysdeps/x86_64/fpu/fsetexcptflg.c index a3ac1dea01..2ce2b509f2 100644 --- a/sysdeps/x86_64/fpu/fsetexcptflg.c +++ b/sysdeps/x86_64/fpu/fsetexcptflg.c @@ -22,30 +22,34 @@ int fesetexceptflag (const fexcept_t *flagp, int excepts) { + /* The flags can be set in the 387 unit or in the SSE unit. + When we need to clear a flag, we need to do so in both units, + due to the way fetestexcept() is implemented. + When we need to set a flag, it is sufficient to do it in the SSE unit, + because that is guaranteed to not trap. */ + fenv_t temp; unsigned int mxcsr; - /* XXX: Do we really need to set both the exception in both units? - Shouldn't it be enough to set only the SSE unit? */ + excepts &= FE_ALL_EXCEPT; /* Get the current x87 FPU environment. We have to do this since we cannot separately set the status word. */ __asm__ ("fnstenv %0" : "=m" (*&temp)); - temp.__status_word &= ~(excepts & FE_ALL_EXCEPT); - temp.__status_word |= *flagp & excepts & FE_ALL_EXCEPT; + /* Clear relevant flags. */ + temp.__status_word &= ~(excepts & ~ *flagp); - /* Store the new status word (along with the rest of the environment. - Possibly new exceptions are set but they won't get executed unless - the next floating-point instruction. */ + /* Store the new status word (along with the rest of the environment). */ __asm__ ("fldenv %0" : : "m" (*&temp)); - /* And now the same for SSE. */ + /* And now similarly for SSE. */ __asm__ ("stmxcsr %0" : "=m" (*&mxcsr)); - mxcsr &= ~(excepts & FE_ALL_EXCEPT); - mxcsr |= *flagp & excepts & FE_ALL_EXCEPT; + /* Clear or set relevant flags. */ + mxcsr ^= (mxcsr ^ *flagp) & excepts; + /* Put the new data in effect. */ __asm__ ("ldmxcsr %0" : : "m" (*&mxcsr)); /* Success. */