From patchwork Fri Jan 24 16:27:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 859570 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:18c8:b0:385:e875:8a9e with SMTP id w8csp881787wrq; Fri, 24 Jan 2025 08:32:51 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXWWGXbEoLZ5QFo6YW8qqJox8NtPxmaHPrYi3f1kQVj78eVtpn65LSqEfjaaESsirwH7eriQw==@linaro.org X-Google-Smtp-Source: AGHT+IEOYNlCtz92NQuI19mwqTIcjtoAK8joTTaZJhPBdw+WPKNpcpFc99IhsOTZM2DGIPjNvUKR X-Received: by 2002:a05:6122:8ce:b0:50d:5e21:ef39 with SMTP id 71dfb90a1353d-51d51b65ca7mr23762762e0c.1.1737736371539; Fri, 24 Jan 2025 08:32:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1737736371; cv=none; d=google.com; s=arc-20240605; b=biaHQ4f0qnBJ8ORLzI2QeU4iJJ8l92VoFrOb8O8DEBtutDYxKq7kueZMzxBdN6u3Wl yIP+LeBmjVXWsRfIycJlYi21g6Fki3AhODrYjHHdZv58sFDxMLTtGxuytnBnN/e6BIpU XfyjHaHC9oTwY0JFRDKMDAUVzq7VMU8R9TDxCk8KOPG/e69hWpUq5BEckSw9JvI8H11Z 7YadZ3bZEpKljPfxqrP6xOcrApOgcEpZiNIXkHEn7gnC50WgiCfNiprqukvir1HyA4XI Ly528EhycnRE0WJHNW7xolCsF4RRPRyOqpIUGIgKbJbcuAEAIlQoU11fNEiZwoo9Efa+ fIQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender: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; bh=cIp/ry/DGiFODjiThDZlgJEFjr5yBLQ4629/fR6TMgU=; fh=SrcXO4O+xZDWjLKLO0UlQPvhNahfMo3l5+mwKI8/GaA=; b=JdVQzLUfIHtJN2LmjcBgmTNir/Yo4kLTEp0xt2veLGTGRds3XGC8LCdrNgeW2wuZHm Xagr/m4fVbjgwXbYNdOANlXvDZwRKClZuuLJbUYlP14/LuS+3OUEWExemQYruStQkkrK vicfbHW1W8ivq5oVtJtaJHmTHUl4CEyMW4Hm+RRcpj9mrgTS28Le90Idc0T9eUfU61uq T5wtSPAHrkI0R2QFQGYcB9mivkA69guFW/9jZqkpidtEltx7ApVlSYemMDwa1cgXNwOY SNKCT2HtYIBKBl5/UWEiJxni8FGjakThV5HTn3Kvp5F0FzJfw3+wioJSRZpRDl+NTMjg hIfA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eyt0uoBf; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 71dfb90a1353d-51e4ec4453csi1145855e0c.163.2025.01.24.08.32.51 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 24 Jan 2025 08:32:51 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eyt0uoBf; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tbMYj-0004bb-G4; Fri, 24 Jan 2025 11:29:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tbMY7-0003ai-J8 for qemu-devel@nongnu.org; Fri, 24 Jan 2025 11:28:52 -0500 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tbMXz-0005Lf-P3 for qemu-devel@nongnu.org; Fri, 24 Jan 2025 11:28:48 -0500 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-4362bae4d7dso16639895e9.1 for ; Fri, 24 Jan 2025 08:28:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1737736121; x=1738340921; darn=nongnu.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=cIp/ry/DGiFODjiThDZlgJEFjr5yBLQ4629/fR6TMgU=; b=eyt0uoBfDPexOTWkAA62YwpBXmkPpUJse9P0JcMVngJSYcrbdVjlt3s3DlxlxS2coM L8lNUilQ191LUpPCl035Fq1Avpm6jdm6qJFbe63rdG3BgG3pdDxJ23KGGbuBdB55twrV Zr7Z+CaaY2knhoCS1uW1XIk8YE8tZtse6e0V7DKxiDWNewez3KyJ/uEBSD5KgIkxmuk+ lgS6kpbkxIqR6RksmbNOm0K04fkiQCfAg5vwH4XuQIs9DIy8LViKExN4LevSIiAo44Ic 3CPDkGPW5KMhZpbvPXm06SZjYcE+wKGUWtOcDXiZRxQvRvupAtV4P4JE/ZhbRzI25iv+ qlkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737736121; x=1738340921; 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=cIp/ry/DGiFODjiThDZlgJEFjr5yBLQ4629/fR6TMgU=; b=s6fOhMEAavupJ/W6nHiFvSTGSYK+IJmOqbEbpg1vxk5N/MlP7PxIZpR4lJuHz/x1Nc OHuXUUSelcd9gk25fshouBpZo7F2FvoQeXLSPll1nrQBbGjG1raojoxZ+1/IGoBvYmNk oy6J5okPZLLbQzPthOW7DZUtJGoCAfjU17GOdeKVFrOing807zseIEKX8nZQTyJnnHbQ 4hv5iJyN1opiiD6Pq+Yxsy22iF5QeDXoub/xhefKo75i6qUFN2a/Ve/qcUqB4WeyNDiC OnbDAqzhzVwsxvtv8g1hO5/w2hOgjMLTtVLo9iZiujFgHac8BdbqcPZ1OkYoVKaSL+iK q3qA== X-Forwarded-Encrypted: i=1; AJvYcCXsRYG2Bd2sYfSHEVky2ZgoNqpLhL2A3TRM/u9rJ5R0wEvzSaVXnKmg/6dPbPxsqIKVQ41/IsKopwfG@nongnu.org X-Gm-Message-State: AOJu0YzCcOVBlKEkQJoNWQYwOSTKwv3TqVZL9mVJDTmMtt5EMCwcbXGf XeNCVL0hUFl3tpAUxcSn0qlp9mC1KAl+UbdGY1hfZ/0e5ztxD4sbinGCUx+lwSk= X-Gm-Gg: ASbGncvxm0GIKKeqrgiQzVQ9R+e4xRsD5exQkDyP5rKkNmY1cBNeyikB15QGhFp104e Z+9oZ3u2Yj1GYJ99xzo0sHm2ZwnOtujrc54x9iK7SELfH1GEiUImXsDDD/dYt07MZgdL0NMzuD2 /0lfRIiRwZR38ClWHb0qV25yXQ6Y2FAvO4VcW9wQ5UgKrGObvULHoJdxvNy+J5lR08Z7ZYoZExl rsG6ldLKUOcuZ76rBAy9SC/pDtk4a5RO+DpmOASKCg5t45zL23Njlqm3i/azP3laURPog38LWq6 ox4I4UNfU/IFr8TnMe/peg== X-Received: by 2002:a05:600c:1f0d:b0:431:3bf9:3ebb with SMTP id 5b1f17b1804b1-4389143923bmr262859175e9.24.1737736120992; Fri, 24 Jan 2025 08:28:40 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438bd47eecasm31683025e9.6.2025.01.24.08.28.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2025 08:28:40 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH 02/76] tests/tcg/x86_64/fma: Test some x86 fused-multiply-add cases Date: Fri, 24 Jan 2025 16:27:22 +0000 Message-Id: <20250124162836.2332150-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250124162836.2332150-1-peter.maydell@linaro.org> References: <20250124162836.2332150-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::331; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x331.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Add a test case which tests some corner case behaviour of fused-multiply-add on x86: * 0 * Inf + SNaN should raise Invalid * 0 * Inf + QNaN shouldh not raise Invalid * tininess should be detected after rounding There is also one currently-disabled test case: * flush-to-zero should be done after rounding This is disabled because QEMU's emulation currently does this incorrectly (and so would fail the test). The test case is kept in but disabled, as the justification for why the test running harness has support for testing both with and without FTZ set. Signed-off-by: Peter Maydell --- tests/tcg/x86_64/fma.c | 109 +++++++++++++++++++++++++++++++ tests/tcg/x86_64/Makefile.target | 1 + 2 files changed, 110 insertions(+) create mode 100644 tests/tcg/x86_64/fma.c diff --git a/tests/tcg/x86_64/fma.c b/tests/tcg/x86_64/fma.c new file mode 100644 index 00000000000..09c622ebc00 --- /dev/null +++ b/tests/tcg/x86_64/fma.c @@ -0,0 +1,109 @@ +/* + * Test some fused multiply add corner cases. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include +#include +#include +#include + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +/* + * Perform one "n * m + a" operation using the vfmadd insn and return + * the result; on return *mxcsr_p is set to the bottom 6 bits of MXCSR + * (the Flag bits). If ftz is true then we set MXCSR.FTZ while doing + * the operation. + * We print the operation and its results to stdout. + */ +static uint64_t do_fmadd(uint64_t n, uint64_t m, uint64_t a, + bool ftz, uint32_t *mxcsr_p) +{ + uint64_t r; + uint32_t mxcsr = 0; + uint32_t ftz_bit = ftz ? (1 << 15) : 0; + uint32_t saved_mxcsr = 0; + + asm volatile("stmxcsr %[saved_mxcsr]\n" + "stmxcsr %[mxcsr]\n" + "andl $0xffff7fc0, %[mxcsr]\n" + "orl %[ftz_bit], %[mxcsr]\n" + "ldmxcsr %[mxcsr]\n" + "movq %[a], %%xmm0\n" + "movq %[m], %%xmm1\n" + "movq %[n], %%xmm2\n" + /* xmm0 = xmm0 + xmm2 * xmm1 */ + "vfmadd231sd %%xmm1, %%xmm2, %%xmm0\n" + "movq %%xmm0, %[r]\n" + "stmxcsr %[mxcsr]\n" + "ldmxcsr %[saved_mxcsr]\n" + : [r] "=r" (r), [mxcsr] "=m" (mxcsr), + [saved_mxcsr] "=m" (saved_mxcsr) + : [n] "r" (n), [m] "r" (m), [a] "r" (a), + [ftz_bit] "r" (ftz_bit) + : "xmm0", "xmm1", "xmm2"); + *mxcsr_p = mxcsr & 0x3f; + printf("vfmadd132sd 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 + " = 0x%" PRIx64 " MXCSR flags 0x%" PRIx32 "\n", + n, m, a, r, *mxcsr_p); + return r; +} + +typedef struct testdata { + /* Input n, m, a */ + uint64_t n; + uint64_t m; + uint64_t a; + bool ftz; + /* Expected result */ + uint64_t expected_r; + /* Expected low 6 bits of MXCSR (the Flag bits) */ + uint32_t expected_mxcsr; +} testdata; + +static testdata tests[] = { + { 0, 0x7ff0000000000000, 0x7ff000000000aaaa, false, /* 0 * Inf + SNaN */ + 0x7ff800000000aaaa, 1 }, /* Should be QNaN and does raise Invalid */ + { 0, 0x7ff0000000000000, 0x7ff800000000aaaa, false, /* 0 * Inf + QNaN */ + 0x7ff800000000aaaa, 0 }, /* Should be QNaN and does *not* raise Invalid */ + /* + * These inputs give a result which is tiny before rounding but which + * becomes non-tiny after rounding. x86 is a "detect tininess after + * rounding" architecture, so it should give a non-denormal result and + * not set the Underflow flag (only the Precision flag for an inexact + * result). + */ + { 0x3fdfffffffffffff, 0x001fffffffffffff, 0x801fffffffffffff, false, + 0x8010000000000000, 0x20 }, + /* + * Flushing of denormal outputs to zero should also happen after + * rounding, so setting FTZ should not affect the result or the flags. + * QEMU currently does not emulate this correctly because we do the + * flush-to-zero check before rounding, so we incorrectly produce a + * zero result and set Underflow as well as Precision. + */ +#ifdef ENABLE_FAILING_TESTS + { 0x3fdfffffffffffff, 0x001fffffffffffff, 0x801fffffffffffff, true, + 0x8010000000000000, 0x20 }, /* Enabling FTZ shouldn't change flags */ +#endif +}; + +int main(void) +{ + bool passed = true; + for (int i = 0; i < ARRAY_SIZE(tests); i++) { + uint32_t mxcsr; + uint64_t r = do_fmadd(tests[i].n, tests[i].m, tests[i].a, + tests[i].ftz, &mxcsr); + if (r != tests[i].expected_r) { + printf("expected result 0x%" PRIx64 "\n", tests[i].expected_r); + passed = false; + } + if (mxcsr != tests[i].expected_mxcsr) { + printf("expected MXCSR flags 0x%x\n", tests[i].expected_mxcsr); + passed = false; + } + } + return passed ? 0 : 1; +} diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.target index d6dff559c7d..be20fc64e88 100644 --- a/tests/tcg/x86_64/Makefile.target +++ b/tests/tcg/x86_64/Makefile.target @@ -18,6 +18,7 @@ X86_64_TESTS += adox X86_64_TESTS += test-1648 X86_64_TESTS += test-2175 X86_64_TESTS += cross-modifying-code +X86_64_TESTS += fma TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 else TESTS=$(MULTIARCH_TESTS)