From nobody Sat Sep 28 16:55:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1546964795423685.597325965591; Tue, 8 Jan 2019 08:26:35 -0800 (PST) Received: from localhost ([127.0.0.1]:54130 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gguD3-0007VU-Pu for importer@patchew.org; Tue, 08 Jan 2019 11:26:33 -0500 Received: from eggs.gnu.org ([209.51.188.92]:48000) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ggu8m-0004HR-6W for qemu-devel@nongnu.org; Tue, 08 Jan 2019 11:22:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ggu8g-0000jY-Sm for qemu-devel@nongnu.org; Tue, 08 Jan 2019 11:22:05 -0500 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:35990) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ggu8f-0000Uz-Dt for qemu-devel@nongnu.org; Tue, 08 Jan 2019 11:22:02 -0500 Received: by mail-wr1-x442.google.com with SMTP id u4so4673521wrp.3 for ; Tue, 08 Jan 2019 08:21:59 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id w125sm10303466wmb.45.2019.01.08.08.21.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Jan 2019 08:21:56 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id ADB363E113B; Tue, 8 Jan 2019 16:21:54 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Cu0zWCd/QWoV++VI/VVTfNtwQXOM6UIZaGTYIh0OvRk=; b=RU7NZAsVWKRCWRoOFovvTpFekdnjE94bjqnfMRak74doeNqaakbCecBvEVF7E/kzrN pN/n3pGCdviZJor25a4rVWjsvffzLYin2GjnFQEsZLa/imu0VsqXxLsEebz9NxlkCqc8 jYj/hwUJQJFoTGaeBzn8cJhDtho02oUNuiZnA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Cu0zWCd/QWoV++VI/VVTfNtwQXOM6UIZaGTYIh0OvRk=; b=o6ryX5krW08x4cDbl5koL99xZhDI84smCW/AFhsMYNLvnLjXToD63VqOtwpR90iiWL Qf3npjHf3ky89B1z+hDeMi9WX9w/1RdKgkQ0sn865gVvR2Wv/1K/Kd74jf+ZH856FscX ctXh9pNqU5PQvJqSYQSUEToGkv/wW/bt2OMZFyIAyBhLF6LcSRAT3+N8AXfExBLufGHg fs+G4exWk3DghtqOsAwU99tpY9qcylxVv3G9jnYceRYTzAmCk8Sq/mh6y6581egIK1SV HzGcnCUNWtqvEktVqSS5MtX5UNeX1PnruyyJTLh6yPIxharx6vZRg1wMuPW6AdNb85s8 oRxw== X-Gm-Message-State: AJcUukfl1FaNyrmnffB0tMHCHtohGhpie5kAm3EreerboIydmp9i2aKB jUV/sS7Y1w6KUI+1iCVv8vkjvw== X-Google-Smtp-Source: ALg8bN7loUL4EaQ6v5zQbJ2RIddQ+eehKWfs9179E9A0wwV/puGO/QnOp3X4otvaeQzWXINxI+bCLQ== X-Received: by 2002:adf:d089:: with SMTP id y9mr1989239wrh.22.1546964518269; Tue, 08 Jan 2019 08:21:58 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Tue, 8 Jan 2019 16:21:51 +0000 Message-Id: <20190108162154.22259-4-alex.bennee@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190108162154.22259-1-alex.bennee@linaro.org> References: <20190108162154.22259-1-alex.bennee@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::442 Subject: [Qemu-devel] [PATCH v1 3/6] softfloat: enforce softfloat if the host's FMA is broken X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , cota@braap.org, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Aurelien Jarno Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Emilio G. Cota" The added branch to the FMA ops is marked as unlikely and therefore its impact on performance (measured with fp-bench) is within noise range when measured on an Intel(R) Xeon(R) Gold 6142 CPU @ 2.60GHz. In addition, when the host doesn't have a hardware FMA instruction we force the use of softfloat, since whatever the libc does (e.g. checking the host's FP flags) is unlikely to be faster than our softfloat implementation. For instance, on an i386 machine with no hardware support for FMA, we get: $ for precision in single double; do ./fp-bench -o mulAdd -p $precision done - before: 5.07 MFlops 1.85 MFlops - after: 12.65 MFlops 10.05 MFlops Reported-by: Laurent Desnogues Suggested-by: Richard Henderson Signed-off-by: Emilio G. Cota --- fpu/softfloat.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ include/qemu/cpuid.h | 6 ++++ 2 files changed, 91 insertions(+) diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 59eac97d10..ccaed85b0f 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -1542,6 +1542,8 @@ soft_f64_muladd(float64 a, float64 b, float64 c, int = flags, return float64_round_pack_canonical(pr, status); } =20 +static bool force_soft_fma; + float32 QEMU_FLATTEN float32_muladd(float32 xa, float32 xb, float32 xc, int flags, float_status= *s) { @@ -1562,6 +1564,11 @@ float32_muladd(float32 xa, float32 xb, float32 xc, i= nt flags, float_status *s) if (unlikely(!f32_is_zon3(ua, ub, uc))) { goto soft; } + + if (unlikely(force_soft_fma)) { + goto soft; + } + /* * When (a || b) =3D=3D 0, there's no need to check for under/over flo= w, * since we know the addend is (normal || 0) and the product is 0. @@ -1623,6 +1630,11 @@ float64_muladd(float64 xa, float64 xb, float64 xc, i= nt flags, float_status *s) if (unlikely(!f64_is_zon3(ua, ub, uc))) { goto soft; } + + if (unlikely(force_soft_fma)) { + goto soft; + } + /* * When (a || b) =3D=3D 0, there's no need to check for under/over flo= w, * since we know the addend is (normal || 0) and the product is 0. @@ -7974,3 +7986,76 @@ float128 float128_scalbn(float128 a, int n, float_st= atus *status) , status); =20 } + +#ifdef CONFIG_CPUID_H +#include "qemu/cpuid.h" +#endif + +static void check_host_hw_fma(void) +{ +#ifdef CONFIG_CPUID_H + int max =3D __get_cpuid_max(0, NULL); + int a, b, c, d; + bool has_fma3 =3D false; + bool has_fma4 =3D false; + bool has_avx =3D false; + + if (max >=3D 1) { + __cpuid(1, a, b, c, d); + + /* check whether avx is usable */ + if (c & bit_OSXSAVE) { + int bv; + + __asm("xgetbv" : "=3Da"(bv), "=3Dd"(d) : "c"(0)); + if ((bv & 6) =3D=3D 6) { + has_avx =3D c & bit_AVX; + } + } + + if (has_avx) { + /* fma3 */ + has_fma3 =3D c & bit_FMA3; + + /* fma4 */ + __cpuid(0x80000000, a, b, c, d); + if (a >=3D 0x80000001) { + __cpuid(0x80000001, a, b, c, d); + + has_fma4 =3D c & bit_FMA4; + } + } + } + /* + * Without HW FMA, whatever the libc does is probably slower than our + * softfloat implementation. + */ + if (!has_fma3 && !has_fma4) { + force_soft_fma =3D true; + } +#endif +} + +static void __attribute__((constructor)) softfloat_init(void) +{ + union_float64 ua, ub, uc, ur; + + if (QEMU_NO_HARDFLOAT) { + return; + } + + /* + * Test that the host's FMA is not obviously broken. For example, + * glibc < 2.23 can perform an incorrect FMA on certain hosts; see + * https://sourceware.org/bugzilla/show_bug.cgi?id=3D13304 + */ + ua.s =3D 0x0020000000000001ULL; + ub.s =3D 0x3ca0000000000000ULL; + uc.s =3D 0x0020000000000000ULL; + ur.h =3D fma(ua.h, ub.h, uc.h); + if (ur.s !=3D 0x0020000000000001ULL) { + force_soft_fma =3D true; + } + + check_host_hw_fma(); +} diff --git a/include/qemu/cpuid.h b/include/qemu/cpuid.h index 69301700bd..320926ffe0 100644 --- a/include/qemu/cpuid.h +++ b/include/qemu/cpuid.h @@ -25,6 +25,9 @@ #endif =20 /* Leaf 1, %ecx */ +#ifndef bit_FMA3 +#define bit_FMA3 (1 << 12) +#endif #ifndef bit_SSE4_1 #define bit_SSE4_1 (1 << 19) #endif @@ -53,5 +56,8 @@ #ifndef bit_LZCNT #define bit_LZCNT (1 << 5) #endif +#ifndef bit_FMA4 +#define bit_FMA4 (1 << 16) +#endif =20 #endif /* QEMU_CPUID_H */ --=20 2.17.1