From patchwork Mon Nov 6 13:27:11 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: 79192 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE00A385C6F8 for ; Mon, 6 Nov 2023 13:28:00 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id 4D43F3856243 for ; Mon, 6 Nov 2023 13:27:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4D43F3856243 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 4D43F3856243 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::734 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699277251; cv=none; b=cnhO9y1qMpURAQEtOKWUXByv6tWuiu3j4zFRFTF9I2ASh7FdjN+aWoQZFXr+au2X4ApFY+LEE4nh4ApJ7cvuNOPB4xQxj367LORzCkIh+ZqWYhofsReZ2jvnMz0hF383xlN83n28ZRnoX54cfIq4NmkCl3EpP7NWkRR6igxaLEs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699277251; c=relaxed/simple; bh=rzkEODavlYasjMoFTbX7s648cPQFlzx0ZqRxC4i9FWQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=K4N60JMuv9riy/toT878gAVcTHUjgaRWy3OkXJfwrawsTnYnqG9yk+ll789MgBmqGs9Kicy+aW7IMdnEFbxrpSo1Y0rYXjzCGDfWlloFVym+YfrRNt0cKd/S9rs94ydMw3VnvAutg0a2cMtbAPteiAG/NE8s3CSPT4BZERcM8B4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x734.google.com with SMTP id af79cd13be357-7788f727dd7so292404485a.1 for ; Mon, 06 Nov 2023 05:27:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699277247; x=1699882047; 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=LHNjnkOvSbJeqaH29rBAWg6tdAoqLI2jG+IbFLY4Wo8=; b=LRzOLYVkCrumyS5PTmDyxPouoOGD5QYjVEE9zvfi33ATFyBVRdpbKL5BJ+l/jEdxft TCh4MAEWPVWyEH6UHxUQnGY2TBFbz+U9Nb+YUMuzpeddBFdPXmvHoFV8C0jajLFOcxfB 2N1Rzu5tAVQqg/rwa3j+BwF7jMuIOOzWQNsNwYrGBGyYd6lKXbJuC8G4405RmxrxGYeQ nJz/D6nEYz81T4VjeLTt6PVX8j4yC0bpimjWIvzoU+21AXjrdYUjck/SVYgLL9nvEv55 TO2WfhyVUagIknxMqRlauK7vcLRoPmwQWogefJ+QFPOWl+e5iNnsXzfgVDqc7Cxb/OSU EUKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699277247; x=1699882047; 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=LHNjnkOvSbJeqaH29rBAWg6tdAoqLI2jG+IbFLY4Wo8=; b=tpxGksQ65GaUlyVeLEr2Uds3Cl7As1qhrGtP/tIZd42/uhgKOuWi7VEd/boi77cGnh 0yIe38pI7lO763+J5sRuTjTyfeTY/3v27zoxVlSkJn4K3jh78x7pcwDY00Ffm+E8tU09 4/llsxsMAtE0MF51Xc5Qa/2jz35jgwIGn1k4xR22NZP/iM+6TP/Ss3SMFTaZBDYN//je OPUVBGIVFDe1xmYVaA+9f9rrr3CJbdTtXXpahUB5GrltkKbtUbFUrcXT5MxH6XapJN3o y11VH+PeXIcjPuyqSM66i+xh0ARjGAKewz78uAE5S+oKfPZc/OCzNjz9Zw5ftgGwFc2B 5zrw== X-Gm-Message-State: AOJu0YykCwPl/JRp3JfKQtuO3Dn/EY7hGqxcarT6MvDQMzCHx5zIbGXO Kk6v9AdzP2K2JB4JuWLM/RgoyHf/B4RNzjbNf6dmmQ== X-Google-Smtp-Source: AGHT+IFB3a7ZSABVXKUM6PuvvPy4B4NUSXc+NuWMAwdMqszQkjWM537vkltJI9dD+yFZCb1JYqnvYA== X-Received: by 2002:a05:620a:4312:b0:77b:9360:8844 with SMTP id u18-20020a05620a431200b0077b93608844mr3585268qko.48.1699277246936; Mon, 06 Nov 2023 05:27:26 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a715:c1a0:7281:6384:2ee9]) by smtp.gmail.com with ESMTPSA id k3-20020a05620a142300b0076f12fcb0easm3272722qkj.2.2023.11.06.05.27.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 05:27:26 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Bruno Haible Subject: [PATCH v2 5/7] riscv: Fix feenvupdate with FE_DFL_ENV (BZ 31022) Date: Mon, 6 Nov 2023 10:27:11 -0300 Message-Id: <20231106132713.953501-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231106132713.953501-1-adhemerval.zanella@linaro.org> References: <20231106132713.953501-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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+patchwork=sourceware.org@sourceware.org libc_feupdateenv_riscv should check for FE_DFL_ENV, similar to libc_fesetenv_riscv. Also extend the test-fenv.c to test fenvupdate. Checked on riscv under qemu-system. Reviewed-by: Carlos O'Donell --- math/test-fenv.c | 131 ++++++++++++++++++++++++++++--- sysdeps/riscv/rvf/fenv_private.h | 8 +- 2 files changed, 124 insertions(+), 15 deletions(-) diff --git a/math/test-fenv.c b/math/test-fenv.c index 0af7141ba7..63dceddb10 100644 --- a/math/test-fenv.c +++ b/math/test-fenv.c @@ -196,6 +196,30 @@ set_single_exc (const char *test_name, int fe_exc, fexcept_t exception) feclearexcept (exception); test_exceptions (str, ALL_EXC ^ fe_exc, 0); } + +static void +update_single_exc (const char *test_name, const fenv_t *envp, int fe_exc, + int fe_exc_clear, int exception) +{ + char str[200]; + /* The standard allows the inexact exception to be set together with the + underflow and overflow exceptions. So ignore the inexact flag if the + others are raised. */ + int ignore_inexact = (fe_exc & (UNDERFLOW_EXC | OVERFLOW_EXC)) != 0; + + strcpy (str, test_name); + strcat (str, ": set flag, with rest not set"); + feclearexcept (FE_ALL_EXCEPT); + feraiseexcept (exception); + feupdateenv (envp); + test_exceptions (str, fe_exc, ignore_inexact); + + strcpy (str, test_name); + strcat (str, ": clear flag, rest also unset"); + feclearexcept (exception); + feupdateenv (envp); + test_exceptions (str, fe_exc_clear, ignore_inexact); +} #endif static void @@ -233,22 +257,32 @@ fe_tests (void) } #if FE_ALL_EXCEPT +static const char * +funcname (int (*func)(const fenv_t *)) +{ + if (func == fesetenv) + return "fesetenv"; + else if (func == feupdateenv) + return "feupdateenv"; + __builtin_unreachable (); +} + /* Test that program aborts with no masked interrupts */ static void -feenv_nomask_test (const char *flag_name, int fe_exc) +feenv_nomask_test (const char *flag_name, int fe_exc, int (*func)(const fenv_t *)) { # if defined FE_NOMASK_ENV int status; pid_t pid; if (!EXCEPTION_ENABLE_SUPPORTED (FE_ALL_EXCEPT) - && fesetenv (FE_NOMASK_ENV) != 0) + && func (FE_NOMASK_ENV) != 0) { printf ("Test: not testing FE_NOMASK_ENV, it isn't implemented.\n"); return; } - printf ("Test: after fesetenv (FE_NOMASK_ENV) processes will abort\n"); + printf ("Test: after %s (FE_NOMASK_ENV) processes will abort\n", funcname (func)); printf (" when feraiseexcept (%s) is called.\n", flag_name); pid = fork (); if (pid == 0) @@ -295,12 +329,12 @@ feenv_nomask_test (const char *flag_name, int fe_exc) /* Test that program doesn't abort with default environment */ static void -feenv_mask_test (const char *flag_name, int fe_exc) +feenv_mask_test (const char *flag_name, int fe_exc, int (*func)(const fenv_t *)) { int status; pid_t pid; - printf ("Test: after fesetenv (FE_DFL_ENV) processes will not abort\n"); + printf ("Test: after %s (FE_DFL_ENV) processes will not abort\n", funcname (func)); printf (" when feraiseexcept (%s) is called.\n", flag_name); pid = fork (); if (pid == 0) @@ -313,7 +347,7 @@ feenv_mask_test (const char *flag_name, int fe_exc) setrlimit (RLIMIT_CORE, &core_limit); #endif - fesetenv (FE_DFL_ENV); + func (FE_DFL_ENV); feraiseexcept (fe_exc); exit (2); } @@ -615,10 +649,18 @@ feenable_test (const char *flag_name, int fe_exc) static void fe_single_test (const char *flag_name, int fe_exc) { - feenv_nomask_test (flag_name, fe_exc); - feenv_mask_test (flag_name, fe_exc); + feenv_nomask_test (flag_name, fe_exc, fesetenv); + feenv_mask_test (flag_name, fe_exc, fesetenv); feenable_test (flag_name, fe_exc); } + + +static void +feupdate_single_test (const char *flag_name, int fe_exc) +{ + feenv_nomask_test (flag_name, fe_exc, feupdateenv); + feenv_mask_test (flag_name, fe_exc, feupdateenv); +} #endif @@ -646,6 +688,72 @@ feenv_tests (void) fesetenv (FE_DFL_ENV); } +#if FE_ALL_EXCEPT +static void +feupdateenv_single_test (const char *test_name, int fe_exc, int exception) +{ + char str[100]; + fenv_t env; + int res; + + snprintf (str, sizeof str, "feupdateenv %s and FL_DFL_ENV", test_name); + update_single_exc (str, FE_DFL_ENV, fe_exc, NO_EXC, exception); + + feraiseexcept (FE_ALL_EXCEPT); + res = fegetenv (&env); + if (res != 0) + { + printf ("fegetenv failed: %d\n", res); + ++count_errors; + return; + } + + snprintf (str, sizeof str, "feupdateenv %s and FE_ALL_EXCEPT", test_name); + update_single_exc (str, &env, ALL_EXC, ALL_EXC, exception); +} +#endif + +static void +feupdateenv_tests (void) +{ + /* We might have some exceptions still set. */ + feclearexcept (FE_ALL_EXCEPT); + +#ifdef FE_DIVBYZERO + feupdate_single_test ("FE_DIVBYZERO", FE_DIVBYZERO); +#endif +#ifdef FE_INVALID + feupdate_single_test ("FE_INVALID", FE_INVALID); +#endif +#ifdef FE_INEXACT + feupdate_single_test ("FE_INEXACT", FE_INEXACT); +#endif +#ifdef FE_UNDERFLOW + feupdate_single_test ("FE_UNDERFLOW", FE_UNDERFLOW); +#endif +#ifdef FE_OVERFLOW + feupdate_single_test ("FE_OVERFLOW", FE_OVERFLOW); +#endif + +#ifdef FE_DIVBYZERO + feupdateenv_single_test ("DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO); +#endif +#ifdef FE_INVALID + feupdateenv_single_test ("INVALID", INVALID_EXC, FE_INVALID); +#endif +#ifdef FE_INEXACT + feupdateenv_single_test ("INEXACT", INEXACT_EXC, FE_INEXACT); +#endif +#ifdef FE_UNDERFLOW + feupdateenv_single_test ("UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW); +#endif +#ifdef FE_OVERFLOW + feupdateenv_single_test ("OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW); +#endif + + feupdateenv (FE_DFL_ENV); +} + static void feholdexcept_tests (void) @@ -766,13 +874,14 @@ initial_tests (void) #endif } -int -main (void) +static int +do_test (void) { initial_tests (); fe_tests (); feenv_tests (); feholdexcept_tests (); + feupdateenv_tests (); if (count_errors) { @@ -782,3 +891,5 @@ main (void) printf ("\n All tests passed successfully.\n"); return 0; } + +#include diff --git a/sysdeps/riscv/rvf/fenv_private.h b/sysdeps/riscv/rvf/fenv_private.h index 40e23661b7..d8d65458b2 100644 --- a/sysdeps/riscv/rvf/fenv_private.h +++ b/sysdeps/riscv/rvf/fenv_private.h @@ -93,10 +93,7 @@ libc_fetestexcept_riscv (int ex) static __always_inline void libc_fesetenv_riscv (const fenv_t *envp) { - long int env = (long int) envp - (long int) FE_DFL_ENV; - if (env != 0) - env = *envp; - + long int env = (envp != FE_DFL_ENV ? *envp : 0); _FPU_SETCW (env); } @@ -123,7 +120,8 @@ libc_feupdateenv_test_riscv (const fenv_t *envp, int ex) static __always_inline void libc_feupdateenv_riscv (const fenv_t *envp) { - _FPU_SETCW (*envp | riscv_getflags ()); + long int env = (envp != FE_DFL_ENV ? *envp : 0); + _FPU_SETCW (env | riscv_getflags ()); } #define libc_feupdateenv libc_feupdateenv_riscv