From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 6E5793858CDA for ; Mon, 6 Nov 2023 16:14:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E5793858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6E5793858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699287261; cv=none; b=qQB3dMdC32/uI65Hx7ftJ0G4eJlUXAfmcjfBAdjH1hIg6pIezIFcKSOh/8eKf3whFSXtWTRKIBPwX3s6BRj+zwPPnGkK6jOWTIKZAztzr2YIzwCWULpsIN7trNDDPkj7aMMbKJmOgqUU/e/x/iNqUtTu2vlbVMteWlG0GR11mAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699287261; c=relaxed/simple; bh=Qo4fzablXVeIIH2Gccwqo/sLc1FBlgNSbfnLd9FgrfI=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=UkZxxfdEyCSLTNHoEgEf+h0KSQ6ExXyE+1xxnrxO6/1izijrdQk/UHDlAoeOMhh3Fy+7Uk/4DWGAupl7anFZzEeoUCekXVTnJHzYf7Zqx9earjoIFRtGrLDArtnLNjYJBtgo4EwZbJ4TuNr1sO4YJhEOVuqIAjP/cxjzcdLdSr0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699287258; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s3UPkquQrpT9AAhKtuXrDK2bmnJVpCNxQ9eYYy6dfKg=; b=PXRNnYp5YkfRfm5XX8oCeiiWQMzE+ir1b1P3IeCgxEB0CTHPuVuwvxndBGmbwfeD2aoK/2 yF+kS3Enry0QO9CC1IslZRIn3aiJs/SRyMPQR5SHyJJiIyaa0T/azwd422fGYY5D9iIkyW 6lLOTiXDSQ6x+slT5LKjRrDOiew7Luk= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-653--kkzJOqjP_CEL_IxkjpAUQ-1; Mon, 06 Nov 2023 11:14:16 -0500 X-MC-Unique: -kkzJOqjP_CEL_IxkjpAUQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7789f06778aso838987285a.0 for ; Mon, 06 Nov 2023 08:14:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699287255; x=1699892055; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=s3UPkquQrpT9AAhKtuXrDK2bmnJVpCNxQ9eYYy6dfKg=; b=XYTNA81sgzIpsDvPuk1bZbBeI7cgbl3RkoxIvK8SPfaIVWD/eS3wS5RNvxYyFnG4yW FfyEzJb5KMZ1GsQjAlz6ktSuiWjqYv08X6XqEbWssvN7WiRYCiZ/UxCzm3xQlZE6NIrE NpUEUa4zDge3HlyOxx7fALPoofu+jhepc0Wkas084cP2SpsCXQLbnPikLDTEN2vSt7If C/gsKghdwnKL75qwlSz+bnwviMam3IHRLuMvFpCrsviB6qNS/5JqSLMW38cOHjls/roV GWcaasdIUgy8hb0zmTTlktsLYoAVoCJuvMtDD3ZsF/Toe4wsDIkRE5KIgsNWGZGwuO1U +lBQ== X-Gm-Message-State: AOJu0YyiQ3qHFuRPy7RiEnPeUE4f28BO30kiuuzYmcaJ/YpvuOK1N5/+ 3dG6hXG7sMoZ9F/WfGDyhVtsUKc+9eHczkv2vvydfdA/8P/fXIcOlZg1CcBmByFVpQeCeAHmkNJ xSXo0HXYtfmj4G9Ol022PlLL6Nh3o X-Received: by 2002:a05:620a:4150:b0:778:b153:dacd with SMTP id k16-20020a05620a415000b00778b153dacdmr15570235qko.20.1699287255389; Mon, 06 Nov 2023 08:14:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFfn6Jq6SsHpUYkEpMwi5kKJKioxHyKrGCtibNXGdxtmm/A+W5GBp+hW3fVU9I2ZVYCUFbM1g== X-Received: by 2002:a05:620a:4150:b0:778:b153:dacd with SMTP id k16-20020a05620a415000b00778b153dacdmr15570201qko.20.1699287254989; Mon, 06 Nov 2023 08:14:14 -0800 (PST) Received: from [192.168.0.241] ([198.48.244.52]) by smtp.gmail.com with ESMTPSA id e15-20020a05620a12cf00b0076eee688a95sm3442316qkl.0.2023.11.06.08.14.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Nov 2023 08:14:14 -0800 (PST) Message-ID: <067cc983-cc14-f9f7-5875-88a7dadb77a6@redhat.com> Date: Mon, 6 Nov 2023 11:14:13 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH v2 2/7] i686: Do not raise exception traps on fesetexcept (BZ 30989) To: Adhemerval Zanella , libc-alpha@sourceware.org, Bruno Haible References: <20231106132713.953501-1-adhemerval.zanella@linaro.org> <20231106132713.953501-3-adhemerval.zanella@linaro.org> From: Carlos O'Donell Organization: Red Hat In-Reply-To: <20231106132713.953501-3-adhemerval.zanella@linaro.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 List-Id: On 11/6/23 08:27, Adhemerval Zanella wrote: > 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. LGTM. Reviewed-by: Carlos O'Donell > --- > 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. */ Correct, this is called delayed floating point exception handling and hppa has this too. > + 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..58f577d93d 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)) > + { > + /* Get the control word of the SSE unit. */ > + 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 -- Cheers, Carlos.