From patchwork Wed Dec 11 16:19:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 849158 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:2c4:b0:385:e875:8a9e with SMTP id o4csp413738wry; Wed, 11 Dec 2024 08:21:20 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUDUNbZB9SJbWoSGqRNkXNICGjEL28Hyk2Wv42nN31aPvsmpLNhBqZ114P0PzeSTq9T5DtTUg==@linaro.org X-Google-Smtp-Source: AGHT+IHM6UdI0S5Mbw4NdWR8MB/rHkizUOQgqAujB9nUban+EyWSarkit8zf7rHWq6o3EtWoKRbK X-Received: by 2002:a05:622a:8c16:b0:467:882d:e7cb with SMTP id d75a77b69052e-46789389573mr62973761cf.54.1733934080297; Wed, 11 Dec 2024 08:21:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1733934080; cv=none; d=google.com; s=arc-20240605; b=AUqiRod/5iYpH8jIkIn3njyPRXaU38LVllwaEC0ugEXquRWnslyHszo5ze8TuidH8p UiNr9zE0G4eO58gE9V0DYGysqt9il2rurKBMBeVH40EQiS68APE9JFKO2/nVEBsSqWFx eCRBSU8ifo04YuJbU2YnsrLVWbyHGPrBMrr0qXiw9/H5d1lVYxubfhbOuI5+RwsKRjgv ljN8zjsLzXE1sZXBh+TUhoHTXvhj7SioKRwLZ6CxIcbktonspoFdqU433MozlBE9m43V MlerS++jrDptFvLca+iFt5brlZ7sT2xdlnz4fm+PxixdXdiaEHy7GpcQlalhfgwqKhWs MnLA== 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=5/6kEX1gNzfQ+l4QKWLxGw2T4UU2uunALU33y3cz//o=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=FKgvgnYMesvXmSsDAoe2D+CZTMUgTWuYFy6mnBRt50WOR4MR1/CXEq0F+ORu3dk9fF vX8HBMo3i+jsRn0/AR2fj0lBaI1jI4KoYl0Dfs3Sl2hgOXCW88WDlZO3TWpP+hvT07yw CIXj+YtMsBF7UCQKPg3LbCjTWVVB0Jb6hdPXT8kknYG7CepFz+hrmy6PVFpwNXJ9bhma v0UeiJVA4n2NpHQh5m3JDBs/9ZsEVKiKeuORnrQmObIjdqd58s6TKINEpM56k23iIg1t YuZo8KxaWgO91youdU3kSG39YFHpW7BYj6MbpCCVrEZ6Xm0q7ZMxBBpdt6UibDTQWv8a lubQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CrX+O6rw; 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 d75a77b69052e-4677a08fd80si43378061cf.311.2024.12.11.08.21.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Dec 2024 08:21:20 -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=CrX+O6rw; 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 1tLPSM-0000Zu-I5; Wed, 11 Dec 2024 11:20:58 -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 1tLPSL-0000Yb-DN for qemu-devel@nongnu.org; Wed, 11 Dec 2024 11:20:57 -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 1tLPSJ-0007mt-4o for qemu-devel@nongnu.org; Wed, 11 Dec 2024 11:20:57 -0500 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-43540bdb448so13925305e9.2 for ; Wed, 11 Dec 2024 08:20:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1733934054; x=1734538854; 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=5/6kEX1gNzfQ+l4QKWLxGw2T4UU2uunALU33y3cz//o=; b=CrX+O6rwAy5yXlF9AIsv5HHgjV9lhHGCsgS2jRqg/M/rHSO7i8jVYbOTVZUWSvtfDF hQT5W10FftL2lmaHPKsj3vHg12L8DEFQh21xjToH6QHy1bvcza+NRePmUQqqkpFI+vkc hpoKMtUxR5mgBXbZ2OeKnIZ3nsMyhvI7GH6+HgF2cLaUyAtoZAGQex9K4T8pR2PQsbXb Dq/ASWoatV5e4qusgINldYJx24xapLkPTXw4W2qCyKNFrjNLLgPr4NUiU5txTKlSjzBF LgOm9iXaxqtNv1Rgn72UNR25W3P5fzrkEYfIWXEZHq+nwyxOT7mXieLecm1Jdy9QuTrb P8mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733934054; x=1734538854; 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=5/6kEX1gNzfQ+l4QKWLxGw2T4UU2uunALU33y3cz//o=; b=iQHYcFluTZ7D9R5H3Ui+ZbuuStAxMvNW/hXH76X8BVIJVPX0F1eSnbQd7BoXHNFaFg gCfsjQ02MwtKL9720WrDzs/MMXpFH63t1g1juITskxQs18aB8iaPqS0WYiZr3YkmXz96 Uair/3GaHtQfaGHiLiTD2mU0RRzAgkqcfzi/j086ZQVNDa45Nsuk0GHRo+d3uqqF1zcF ou/vdamPCTHfWwHvfo36YX8DCXk4pwpum2YgFO/RZ1zdm2RjDZ9JwaBbF3ZvPac876UY arRyQcUp9lyg/BeV2uBqpYPJphY0XAeIXOlvfDUzp0ikUJ7cF0M/oD0NcZ6PtC7/67pj ZEPA== X-Gm-Message-State: AOJu0Yy17dc7u1m30gbyQ6guLPedx4kgrwBiVnJeAtLfOG6K87KidmnZ d7v5Wk3JYD5Ph3emWWYZaXv/MoPU+gc7n6tn3wQai8b28cD+DJeDz7dFhx64h5xcYH5bjeC4nrN 8 X-Gm-Gg: ASbGncskZD0u5cSy53VDft8z4Ev8HGuoto1XexLDgUmRFcNquLf7A6uOTbO+Q4gKnEh wJ+3AkcfpwqoCubY9ZQRL+CaFrrqXYMNUWIqpU40I6iktCVT0XAqc+c9bA0g9ojKppE2S2mnIE1 x9fjNA+taCOLvNshiqSv1QTDG31VT0CALTlH9PSFn6MyQ7N28ZXeVZlJnlQjVS1AS+rBYU+HrPY pHxQDfPEJxErXBWAdjV1B4E6VQc1qFilZh8xqtLuECQsJ1vjlMeZSmYMfjx X-Received: by 2002:a05:600c:3552:b0:434:f131:1e6d with SMTP id 5b1f17b1804b1-4361c360e94mr33908025e9.10.1733934053683; Wed, 11 Dec 2024 08:20:53 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-434f4dfdcdfsm121460595e9.39.2024.12.11.08.20.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Dec 2024 08:20:53 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 40/72] fpu: Allow runtime choice of default NaN value Date: Wed, 11 Dec 2024 16:19:32 +0000 Message-Id: <20241211162004.2795499-41-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241211162004.2795499-1-peter.maydell@linaro.org> References: <20241211162004.2795499-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 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Currently we hardcode the default NaN value in parts64_default_nan() using a compile-time ifdef ladder. This is awkward for two cases: * for single-QEMU-binary we can't hard-code target-specifics like this * for Arm FEAT_AFP the default NaN value depends on FPCR.AH (specifically the sign bit is different) Add a field to float_status to specify the default NaN value; fall back to the old ifdef behaviour if these are not set. The default NaN value is specified by setting a uint8_t to a pattern corresponding to the sign and upper fraction parts of the NaN; the lower bits of the fraction are set from bit 0 of the pattern. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-id: 20241202131347.498124-35-peter.maydell@linaro.org --- include/fpu/softfloat-helpers.h | 11 +++++++ include/fpu/softfloat-types.h | 10 ++++++ fpu/softfloat-specialize.c.inc | 55 ++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/include/fpu/softfloat-helpers.h b/include/fpu/softfloat-helpers.h index 10a6763532c..dceee23c823 100644 --- a/include/fpu/softfloat-helpers.h +++ b/include/fpu/softfloat-helpers.h @@ -93,6 +93,12 @@ static inline void set_float_infzeronan_rule(FloatInfZeroNaNRule rule, status->float_infzeronan_rule = rule; } +static inline void set_float_default_nan_pattern(uint8_t dnan_pattern, + float_status *status) +{ + status->default_nan_pattern = dnan_pattern; +} + static inline void set_flush_to_zero(bool val, float_status *status) { status->flush_to_zero = val; @@ -154,6 +160,11 @@ static inline FloatInfZeroNaNRule get_float_infzeronan_rule(float_status *status return status->float_infzeronan_rule; } +static inline uint8_t get_float_default_nan_pattern(float_status *status) +{ + return status->default_nan_pattern; +} + static inline bool get_flush_to_zero(float_status *status) { return status->flush_to_zero; diff --git a/include/fpu/softfloat-types.h b/include/fpu/softfloat-types.h index 84ba4ed20e6..79ca44dcc30 100644 --- a/include/fpu/softfloat-types.h +++ b/include/fpu/softfloat-types.h @@ -303,6 +303,16 @@ typedef struct float_status { /* should denormalised inputs go to zero and set the input_denormal flag? */ bool flush_inputs_to_zero; bool default_nan_mode; + /* + * The pattern to use for the default NaN. Here the high bit specifies + * the default NaN's sign bit, and bits 6..0 specify the high bits of the + * fractional part. The low bits of the fractional part are copies of bit 0. + * The exponent of the default NaN is (as for any NaN) always all 1s. + * Note that a value of 0 here is not a valid NaN. The target must set + * this to the correct non-zero value, or we will assert when trying to + * create a default NaN. + */ + uint8_t default_nan_pattern; /* * The flags below are not used on all specializations and may * constant fold away (see snan_bit_is_one()/no_signalling_nans() in diff --git a/fpu/softfloat-specialize.c.inc b/fpu/softfloat-specialize.c.inc index 9f913ce20ab..b1ec534983c 100644 --- a/fpu/softfloat-specialize.c.inc +++ b/fpu/softfloat-specialize.c.inc @@ -133,35 +133,46 @@ static void parts64_default_nan(FloatParts64 *p, float_status *status) { bool sign = 0; uint64_t frac; + uint8_t dnan_pattern = status->default_nan_pattern; + if (dnan_pattern == 0) { #if defined(TARGET_SPARC) || defined(TARGET_M68K) - /* !snan_bit_is_one, set all bits */ - frac = (1ULL << DECOMPOSED_BINARY_POINT) - 1; -#elif defined(TARGET_I386) || defined(TARGET_X86_64) \ + /* Sign bit clear, all frac bits set */ + dnan_pattern = 0b01111111; +#elif defined(TARGET_I386) || defined(TARGET_X86_64) \ || defined(TARGET_MICROBLAZE) - /* !snan_bit_is_one, set sign and msb */ - frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1); - sign = 1; + /* Sign bit set, most significant frac bit set */ + dnan_pattern = 0b11000000; #elif defined(TARGET_HPPA) - /* snan_bit_is_one, set msb-1. */ - frac = 1ULL << (DECOMPOSED_BINARY_POINT - 2); + /* Sign bit clear, msb-1 frac bit set */ + dnan_pattern = 0b00100000; #elif defined(TARGET_HEXAGON) - sign = 1; - frac = ~0ULL; + /* Sign bit set, all frac bits set. */ + dnan_pattern = 0b11111111; #else - /* - * This case is true for Alpha, ARM, MIPS, OpenRISC, PPC, RISC-V, - * S390, SH4, TriCore, and Xtensa. Our other supported targets - * do not have floating-point. - */ - if (snan_bit_is_one(status)) { - /* set all bits other than msb */ - frac = (1ULL << (DECOMPOSED_BINARY_POINT - 1)) - 1; - } else { - /* set msb */ - frac = 1ULL << (DECOMPOSED_BINARY_POINT - 1); - } + /* + * This case is true for Alpha, ARM, MIPS, OpenRISC, PPC, RISC-V, + * S390, SH4, TriCore, and Xtensa. Our other supported targets + * do not have floating-point. + */ + if (snan_bit_is_one(status)) { + /* sign bit clear, set all frac bits other than msb */ + dnan_pattern = 0b00111111; + } else { + /* sign bit clear, set frac msb */ + dnan_pattern = 0b01000000; + } #endif + } + assert(dnan_pattern != 0); + + sign = dnan_pattern >> 7; + /* + * Place default_nan_pattern [6:0] into bits [62:56], + * and replecate bit [0] down into [55:0] + */ + frac = deposit64(0, DECOMPOSED_BINARY_POINT - 7, 7, dnan_pattern); + frac = deposit64(frac, 0, DECOMPOSED_BINARY_POINT - 7, -(dnan_pattern & 1)); *p = (FloatParts64) { .cls = float_class_qnan,