From nobody Sat Nov 23 20:52:07 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1729865877; cv=none; d=zohomail.com; s=zohoarc; b=XAai/1qFw95pBcDhaCC+wPgc/gHzsg89+YMDSiMUfVa7Thuqyh4YZHu4bdaD4C/YGaaUJyEf4TZc53L3U0wtSFzh6XBLP56G17jGKbwfuB/WKLmzrMKKOt3YlNDXwGhaHOsPl2iOiFYzzkHCid+QHae2CuDlnHApghd72zCjocQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729865877; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1cCo50wdC0NIgQKfnDkwtGpzRTYsS1ol+AjTcdAhO0A=; b=LBlEvWS0u1BGb2SCt/pn34KKSnB16kyTdnwFBZw/gGdSmXWwZ9HUvwVhwFfGtdxtq3Y0bREJdjVxd2hcoZ1IfwchPAie1qFN4GkvkXw9fCY+i7lVogqd7qUnch/Mea6Bh4uGh4j1SDOSkWkIybqEjhEPIh35hFjCRok6Zpiz+wc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1729865877041263.7460948126187; Fri, 25 Oct 2024 07:17:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t4L4D-0004RX-LA; Fri, 25 Oct 2024 10:13:29 -0400 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 1t4L44-0004GI-6r for qemu-devel@nongnu.org; Fri, 25 Oct 2024 10:13:21 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1t4L3u-0007sl-U0 for qemu-devel@nongnu.org; Fri, 25 Oct 2024 10:13:19 -0400 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-43169902057so20481835e9.0 for ; Fri, 25 Oct 2024 07:13:10 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4318b57b051sm50104535e9.42.2024.10.25.07.13.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 07:13:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1729865589; x=1730470389; darn=nongnu.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=1cCo50wdC0NIgQKfnDkwtGpzRTYsS1ol+AjTcdAhO0A=; b=SS6Q9Fp/HdnY+6ajn/QNUZwh7QwfvagZsrs28rDOk4zxQqLmZZXnEh8bQMw2OSKWZD wznleDwLjNuJKOghzwreMj9ZEW3IEWW3GLUrYu9hrKg5Ng1q8fy73G7WJhBXVfKIarjH GaYanxj2fvkCqJalQaskgtleKvRApWzsPKgr+/AATfIG/Axkt7QeFtGvAX1L2ZtFyspG 1DRYPy6mpTc29jwu+aRJSqIzKy1Zuv4Ks66lHPK7A9EPmSHluL3O7iDfMAGxetrwP8t/ oK3m83oRu5rwdo4Ex79/otPqyFnj4rJJY1NbToTzYl0nc9qZ1Kw9yaba2SYTv26AKxsE 63FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729865589; x=1730470389; 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=1cCo50wdC0NIgQKfnDkwtGpzRTYsS1ol+AjTcdAhO0A=; b=O9y8JK/LpFkFKoXLGZfmDAblWe2eXXJfY2h7DQMzpgzk1AmrzUAuDg0sXOWQh7hQvo jYSaJ5s50KcCUkgCF+CAL1C+H+tXsyFUIQecql6lcHnquI0b6RM8txGl1dbArkjTYBNA nkkY2CBMRonX5i0IXJ6WjeF02ajvgPrw48nV0+McIav1L+hirspa4WPg4+FdJKQ4VXG2 EJu+c9Va8lndFsUnVpNznCcekIMA/0N8CQBd2rSTJdn2T4a4F/rC5pvBsQFhw01xQ3Di RsK29TftrMaupbIkdoUvBpTfYux3pHFmBZm8WpGwefMzYOB8xL/cY3iPPhgStWX3zGPa QaTQ== X-Forwarded-Encrypted: i=1; AJvYcCV7+ozOQDmODyr/8hPcMGpIoM7wNQeqB7Eao0kKtw4mAHdi2uMvfHz7SkBHmdK00H/9Rsm/JKwVam+/@nongnu.org X-Gm-Message-State: AOJu0YyeywU58tkBbWJeGI3gl/Iz+3XVWY2TiQYtJpzbPvmZoGpb4Tq6 BeSs5YWTGSLzRvZgpx+fKmG5RQiSo0LPsi34YNcTYZ5hy8BhsmD9b7/7CwPWAy0= X-Google-Smtp-Source: AGHT+IErRofvNVko14JtpQxdLcThoMuGjBJqi3FTPlWg0ZwyTazi/nWN12GMkiVoSHlJvSwtaTXZqA== X-Received: by 2002:a05:600c:1d27:b0:431:58cd:b260 with SMTP id 5b1f17b1804b1-4318418a568mr84761005e9.25.1729865589416; Fri, 25 Oct 2024 07:13:09 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Song Gao , Eduardo Habkost , "Edgar E. Iglesias" , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Daniel Henrique Barboza , Yoshinori Sato , David Hildenbrand , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Max Filippov , qemu-ppc@nongnu.org, qemu-s390x@nongnu.org Subject: [PATCH 15/21] target/i386: Set 2-NaN propagation rule explicitly Date: Fri, 25 Oct 2024 15:12:48 +0100 Message-Id: <20241025141254.2141506-16-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241025141254.2141506-1-peter.maydell@linaro.org> References: <20241025141254.2141506-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x333.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 autolearn=ham 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1729865877605116600 Content-Type: text/plain; charset="utf-8" Set the NaN propagation rule explicitly for the float_status words used in the x86 target. This is a no-behaviour-change commit, so we retain the existing behaviour of using the x87-style "prefer QNaN over SNaN, then prefer the NaN with the larger significand" for MMX and SSE. This is however not the documented hardware behaviour, so we leave a TODO note about what we should be doing instead. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- target/i386/cpu.h | 3 +++ target/i386/cpu.c | 4 ++++ target/i386/tcg/fpu_helper.c | 40 ++++++++++++++++++++++++++++++++++ fpu/softfloat-specialize.c.inc | 3 ++- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 74886d1580f..43ba62e92d3 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -2562,6 +2562,9 @@ static inline bool cpu_vmx_maybe_enabled(CPUX86State = *env) int get_pg_mode(CPUX86State *env); =20 /* fpu_helper.c */ + +/* Set all non-runtime-variable float_status fields to x86 handling */ +void cpu_init_fp_statuses(CPUX86State *env); void update_fp_status(CPUX86State *env); void update_mxcsr_status(CPUX86State *env); void update_mxcsr_from_sse_status(CPUX86State *env); diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 1ff1af032ea..e9260fbc654 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -7083,6 +7083,10 @@ static void x86_cpu_reset_hold(Object *obj, ResetTyp= e type) =20 memset(env, 0, offsetof(CPUX86State, end_reset_fields)); =20 + if (tcg_enabled()) { + cpu_init_fp_statuses(env); + } + env->old_exception =3D -1; =20 /* init to reset state */ diff --git a/target/i386/tcg/fpu_helper.c b/target/i386/tcg/fpu_helper.c index e1b850f3fc2..53b49bb2977 100644 --- a/target/i386/tcg/fpu_helper.c +++ b/target/i386/tcg/fpu_helper.c @@ -135,6 +135,46 @@ static void fpu_set_exception(CPUX86State *env, int ma= sk) } } =20 +void cpu_init_fp_statuses(CPUX86State *env) +{ + /* + * Initialise the non-runtime-varying fields of the various + * float_status words to x86 behaviour. This must be called at + * CPU reset because the float_status words are in the + * "zeroed on reset" portion of the CPU state struct. + * Fields in float_status that vary under guest control are set + * via the codepath for setting that register, eg cpu_set_fpuc(). + */ + /* + * Use x87 NaN propagation rules: + * SNaN + QNaN =3D> return the QNaN + * two SNaNs =3D> return the one with the larger significand, silenced + * two QNaNs =3D> return the one with the larger significand + * SNaN and a non-NaN =3D> return the SNaN, silenced + * QNaN and a non-NaN =3D> return the QNaN + * + * If we get down to comparing significands and they are the same, + * return the NaN with the positive sign bit (if any). + */ + set_float_2nan_prop_rule(float_2nan_prop_x87, &env->fp_status); + /* + * TODO: These are incorrect: the x86 Software Developer's Manual vol 1 + * section 4.8.3.5 "Operating on SNaNs and QNaNs" says that the + * "larger significand" behaviour is only used for x87 FPU operations. + * For SSE the required behaviour is to always return the first NaN, + * which is float_2nan_prop_ab. + * + * mmx_status is used only for the AMD 3DNow! instructions, which + * are documented in the "3DNow! Technology Manual" as not supporting + * NaNs or infinities as inputs. The result of passing two NaNs is + * documented as "undefined", so we can do what we choose. + * (Strictly there is some behaviour we don't implement correctly + * for these "unsupported" NaN and Inf values, like "NaN * 0 =3D=3D 0"= .) + */ + set_float_2nan_prop_rule(float_2nan_prop_x87, &env->mmx_status); + set_float_2nan_prop_rule(float_2nan_prop_x87, &env->sse_status); +} + static inline uint8_t save_exception_flags(CPUX86State *env) { uint8_t old_flags =3D get_float_exception_flags(&env->fp_status); diff --git a/fpu/softfloat-specialize.c.inc b/fpu/softfloat-specialize.c.inc index b050c5eb04a..77ebc8216f6 100644 --- a/fpu/softfloat-specialize.c.inc +++ b/fpu/softfloat-specialize.c.inc @@ -405,7 +405,8 @@ static int pickNaN(FloatClass a_cls, FloatClass b_cls, || defined(TARGET_TRICORE) || defined(TARGET_ARM) || defined(TARGET_MI= PS) \ || defined(TARGET_LOONGARCH64) || defined(TARGET_HPPA) \ || defined(TARGET_S390X) || defined(TARGET_PPC) || defined(TARGET_M68K= ) \ - || defined(TARGET_SPARC) || defined(TARGET_XTENSA) + || defined(TARGET_SPARC) || defined(TARGET_XTENSA) \ + || defined(TARGET_I386) g_assert_not_reached(); #else rule =3D float_2nan_prop_x87; --=20 2.34.1