Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4584606A31A for ; Tue, 1 Dec 2026 16:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=ZlRixh9A Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xYQIO-0007H9-8S; Tue, 01 Dec 2026 11:01:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xYQIH-0007De-5b for qemu-riscv@nongnu.org; Tue, 01 Dec 2026 11:01:25 -0500 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xYQIB-0007E9-Pt for qemu-riscv@nongnu.org; Tue, 01 Dec 2026 11:01:24 -0500 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2f0481b35c9so70418445ad.0 for ; Tue, 01 Dec 2026 08:01:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1796140878; x=1796745678; 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:content-type; bh=uhq9xi9oyCVyxw00+r8OJnt82DAXuaV6RS94k/RYxwg=; b=ZlRixh9AdPpuR+EjlU4eckj+aDjmNAC+eydt3UZKpXZTislfKOQtYAgzUBZRFsJRvW b3hR31A4+J7dul5s1WXhWPUQVk7pKHumRMn3O/dBaVlftRfwNETuNz5y4R43/vrvxsb7 uW5oJc0S6g9S4dxXBKrvjI6rxNl8FAbTEVSDygtbmD5ykLB+brpWrtsBf3e+FIuxKGtE qz9gR8KIMzt3WoJoTMb/kJXhv5jCxIEgNb4BHHmwEca7eBoPHoVpBTfixLFCtMVzKBvP wfoYz/hJlHumoIOZ3MILYBBMN+0NXHpAhwlBAGUHXxYfTgp+t+FlIfgfwzUy/kqntvl4 90OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796140878; x=1796745678; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uhq9xi9oyCVyxw00+r8OJnt82DAXuaV6RS94k/RYxwg=; b=NbDv2sQboIGCDbYZyvepMLx+IyazkYo/v7c14wOKq0PdK5/jWuh8ukCHFtIKVlnHtg 1m6Bl5IRq29vQSBolAzvCNowcJAcq7XIvKUjmhv7TciIJRT693pS4hP37erZdmIt2gJN kOmFsAHfYWdBt5/8prhBHwYtfWlsYOzk36wKJrvGJNdA4/7HqpfyTPNDjrPj9LXpDPTh 3OFp8jORBqBG0KoZgjZnX8GYWB6XitIGZ8ir05f+IyDar8zyE16fGBzBXpvwtUTSXVLf GrHMqvf5ejwx8/wQgpifkMgzyS+9U8TDCz89J2doYGqBQqlU4YEzpXeVm25J19VJOZKZ jIQw== X-Forwarded-Encrypted: i=1; AL+hErG2ddoCuY6eBsoz/sK4kYheWT4lvpy67EBJ3orOmSm+m48BpYfTmTeYK5JEIvSEZDx8YKEP+BeeGdKs@nongnu.org X-Gm-Message-State: APuU4Z99yY4zSaEaD/gxAx4ooOpiO25WqdYMe2RDzqWyPbCzDbWOrWOa DmZ2eykvQxiv/hjgRU7Poyjmul9dtTlMyVEiHm/oNv3lOylahsUkAIEX0zvGUgPVo48= X-Gm-Gg: AZFdTZcYYD2v6+X9s01FIVfYaPw6QBhFbpdy3a5H3PF7qwCp1AY7TT/g7vU2QjdyUQ8 Nsi39LuAR4B40Z9US4z+Zc9RyHQLBXl1frdRdyFyANF6hVuWR6BD6uU2STn0CCbz8EHW5KJgx0l yoLTsB7XQFAoofdj5UooETH/uplMdEUopYgHv3zyC3lnCiiSRfMMgoYvE5x9A0zR8yxxikBbKYf OdEvzkl2xlxWiFfRrB3m+fD71expkm/xF8PhUlkWtbBpQWhcJvwLPj/QhNcWHitO4tJLn9DegIx FptMXqxi7vqHEeHy9A5WVZ4ymrnmiKye8ggH5MGbA9iO3G0rzJsHgIO7wJ0OfLvODuNNJ672sET IV0sVipVA58TVg6qCjfvLyGcHTB6b2Jrj1wgdbvavewjeJf3yxBTMKIiXCe7OZaHZHObDHy3F9R FFJIiWLgOCRJapzUl2YxRojdeWC1akFl6p8l2CApXkgZkCuAOq/8lmazwF2m89RzWbFtY4rzpsg g+HQNMYy7wkHGuH43ouxpFO1tLh2bJjJojJJHDxLZSrtL0LzlaxH+lw1pd3oueAVsRHR8fI7kJ4 mmka35xwt5j0n1AidXexSCG5cjPrH6taU3P3iAXaQKGFQhQGY8lIKkRTdF0h4fvk5slRTkwecOn hHf3U/U3KoCs/TeOxeRzZnq/J7F6aOzXDDueQI2dvOjltHoXh58FzNnudRnGKBadaqtUsF6sGQm DS23rTbsWF5ykmZQ== X-Received: by 2002:a17:902:f609:b0:2ef:d46a:aa2b with SMTP id d9443c01a7336-2f03f215985mr326724825ad.38.1796140878282; Tue, 01 Dec 2026 08:01:18 -0800 (PST) Received: from duncan.localdomain (114-35-142-126.hinet-ip.hinet.net. [114.35.142.126]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f08d73dfe0sm149803225ad.69.2026.12.01.08.01.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 08:01:17 -0800 (PST) From: Max Chou To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , Aurelien Jarno , Peter Maydell , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Weiwei Li , Liu Zhiwei , Chao Liu , Max Chou , Alistair Francis Subject: [PATCH v1 03/38] target/riscv: rvv: Introduce RVBNA algorithm for Zvdota/Zvbdota extensions Date: Wed, 2 Dec 2026 00:00:23 +0800 Message-ID: <20261201160100.1911581-4-max.chou@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201160100.1911581-1-max.chou@sifive.com> References: <20261201160100.1911581-1-max.chou@sifive.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=max.chou@sifive.com; helo=mail-pl1-x634.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-riscv@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The RISC-V Bulk Normalization Algorithm (RVBNA) is a scheme employed by multiple RISC-V extensions (e.g. Zvdota/Zvbdota family) for efficient computation of floating-point dot products. Reviewed-by: Daniel Henrique Barboza Reviewed-by: Chao Liu Tested-by: Chao Liu Signed-off-by: Max Chou --- MAINTAINERS | 6 + target/riscv/tcg/meson.build | 2 +- target/riscv/tcg/rvbna.c | 182 +++++++++++++++++++++++++++++++ target/riscv/tcg/rvbna.h | 37 +++++++ target/riscv/tcg/vector_helper.c | 1 + 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 target/riscv/tcg/rvbna.c create mode 100644 target/riscv/tcg/rvbna.h diff --git a/MAINTAINERS b/MAINTAINERS index 47eefdf2c39..f7edaeb3521 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -416,6 +416,12 @@ F: target/riscv/tcg/xsf_helper.c F: disas/riscv-xsf* F: tests/tcg/riscv64/test-xsfvqmacc.S +RISC-V Bulk Normalization Algorithm (RVBNA) +M: Max Chou +L: qemu-riscv@nongnu.org +S: Supported +F: target/riscv/tcg/rvbna.* + RENESAS RX CPUs R: Yoshinori Sato S: Orphan diff --git a/target/riscv/tcg/meson.build b/target/riscv/tcg/meson.build index 86805b30cbc..3b7821178fc 100644 --- a/target/riscv/tcg/meson.build +++ b/target/riscv/tcg/meson.build @@ -10,6 +10,7 @@ riscv_ss.add(files( 'fpu_helper.c', 'm128_helper.c', 'op_helper.c', + 'rvbna.c', 'translate.c', 'tcg-cpu.c', 'vcrypto_helper.c', @@ -27,4 +28,3 @@ riscv_system_ss.add(files( 'th_csr.c', )) - diff --git a/target/riscv/tcg/rvbna.c b/target/riscv/tcg/rvbna.c new file mode 100644 index 00000000000..971542e2b35 --- /dev/null +++ b/target/riscv/tcg/rvbna.c @@ -0,0 +1,182 @@ +/* + * RISC-V Bulk Normalization Algorithm (RVBNA) + * + * Copyright (c) 2026 SiFive, Inc. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/bitops.h" +#include "qemu/host-utils.h" +#include "fpu/softfloat.h" +#include "fpu/softfloat-macros.h" +#include "rvbna.h" + +static inline int rvbna_guard_bits(uint32_t n_prod) +{ + g_assert(n_prod > 0); + return 32 - clz32(n_prod - 1); +} + +float32 rvbna_final_f32_add(float32 partial, float32 accumulator, + float_status *fp_status) +{ + FloatRoundMode rm = get_float_rounding_mode(fp_status); + int flags = get_float_exception_flags(fp_status); + float32 result; + + set_float_rounding_mode(float_round_to_odd_inf, fp_status); + result = float32_add(partial, accumulator, fp_status); + flags |= (get_float_exception_flags(fp_status) & (float_flag_invalid | + float_flag_overflow)); + set_float_rounding_mode(rm, fp_status); + set_float_exception_flags(flags, fp_status); + + return result; +} + +#define RVBNA_STACK_THRESHOLD 8 + +#define GEN_RVBNA(TA, TB, TO, A_E, A_M, B_E, B_M, OUT_E, OUT_M) \ +TO rvbna_dot_##TA##_##TB(const TA a[], const TB b[], uint32_t n_prod, \ + float_status *fp_status) \ +{ \ + int32_t stack_ref_exps[RVBNA_STACK_THRESHOLD]; \ + bool stack_signs[RVBNA_STACK_THRESHOLD]; \ + uint64_t stack_sigs[RVBNA_STACK_THRESHOLD]; \ + int32_t *prod_ref_exps = stack_ref_exps; \ + bool *prod_signs = stack_signs; \ + uint64_t *prod_sigs = stack_sigs; \ + g_autofree int32_t *heap_ref_exps = NULL; \ + g_autofree bool *heap_signs = NULL; \ + g_autofree uint64_t *heap_sigs = NULL; \ + int guard_bits = rvbna_guard_bits(n_prod); \ + int pad = OUT_M - (A_M + B_M) + guard_bits; \ + int output_bias = (1 << (OUT_E - 1)) - 1; \ + int a_exp_bias = (1 << (A_E - 1)) - 1; \ + int b_exp_bias = (1 << (B_E - 1)) - 1; \ + int ref_bias = output_bias - (a_exp_bias + b_exp_bias); \ + int max_prod_ref_exp = ref_bias; \ + int overflow_exp = (1 << OUT_E) - 1; \ + int64_t acc = 0; \ + bool nan_result = false; \ + bool inf_result = false; \ + bool inf_sign = false; \ + bool any_pos_inf = false; \ + bool any_neg_inf = false; \ + bool any_invalid_prod = false; \ + bool any_signan = false; \ + bool op_sign_inf, sign, overflow; \ + uint32_t i; \ + uint64_t sig; \ + int lead, exp, sig_bits; \ + \ + g_assert(pad >= 0); \ + if (n_prod > RVBNA_STACK_THRESHOLD) { \ + heap_ref_exps = g_new(int32_t, n_prod); \ + heap_signs = g_new(bool, n_prod); \ + heap_sigs = g_new(uint64_t, n_prod); \ + prod_ref_exps = heap_ref_exps; \ + prod_signs = heap_signs; \ + prod_sigs = heap_sigs; \ + } \ + \ + for (i = 0; i < n_prod; i++) { \ + bool a_sign = TA##_is_neg(a[i]); \ + bool b_sign = TB##_is_neg(b[i]); \ + bool a_sub = TA##_is_zero_or_denormal(a[i]); \ + bool b_sub = TB##_is_zero_or_denormal(b[i]); \ + bool a_zero = TA##_is_zero(a[i]); \ + bool b_zero = TB##_is_zero(b[i]); \ + bool a_inf = TA##_is_infinity(a[i]); \ + bool b_inf = TB##_is_infinity(b[i]); \ + bool a_nan = TA##_is_any_nan(a[i]); \ + bool b_nan = TB##_is_any_nan(b[i]); \ + bool prod_sign = a_sign ^ b_sign; \ + bool invalid_prod = (a_inf && b_zero) || (b_inf && a_zero); \ + bool inf_prod_lhs = a_inf && !b_nan && !b_zero; \ + bool inf_prod_rhs = b_inf && !a_nan && !a_zero; \ + bool inf_prod = inf_prod_lhs || inf_prod_rhs; \ + bool invalid_sum = inf_result && inf_prod && \ + (inf_sign != prod_sign); \ + uint32_t a_mant = extract32(a[i], 0, A_M); \ + uint32_t b_mant = extract32(b[i], 0, B_M); \ + bool a_signan = a_nan && TA##_is_signaling_nan(a[i], fp_status); \ + bool b_signan = b_nan && TB##_is_signaling_nan(b[i], fp_status); \ + uint64_t a_sig = a_mant | (!a_sub << A_M); \ + uint64_t b_sig = b_mant | (!b_sub << B_M); \ + bool prod_is_zero = a_zero || b_zero; \ + bool either_inf = a_inf || b_inf; \ + bool either_nan = a_nan || b_nan; \ + \ + any_invalid_prod |= invalid_prod; \ + nan_result |= a_nan || b_nan || any_invalid_prod || invalid_sum; \ + inf_result |= inf_prod; \ + inf_sign = inf_prod ? prod_sign : inf_sign; \ + \ + prod_signs[i] = prod_sign; \ + prod_sigs[i] = (uint64_t)(a_sig * b_sig) << pad; \ + prod_ref_exps[i] = ref_bias; \ + if (!prod_is_zero) { \ + prod_ref_exps[i] += (a_sub ? 1 : extract32(a[i], A_M, A_E)) + \ + (b_sub ? 1 : extract32(b[i], B_M, B_E)); \ + } \ + max_prod_ref_exp = MAX(prod_ref_exps[i], max_prod_ref_exp); \ + \ + any_pos_inf |= (either_inf && !either_nan && !prod_is_zero && \ + (a_sign == b_sign)); \ + any_neg_inf |= (either_inf && !either_nan && !prod_is_zero && \ + (a_sign != b_sign)); \ + any_signan |= a_signan || b_signan; \ + } \ + \ + op_sign_inf = any_pos_inf && any_neg_inf; \ + \ + if (nan_result) { \ + if (any_signan || any_invalid_prod || op_sign_inf) { \ + float_raise(float_flag_invalid, fp_status); \ + } \ + return TO##_default_nan(fp_status); \ + } else if (inf_result) { \ + return TO##_set_sign(TO##_infinity, inf_sign); \ + } \ + \ + for (i = 0; i < n_prod; i++) { \ + shift64RightJamming(prod_sigs[i], \ + max_prod_ref_exp - prod_ref_exps[i], &sig); \ + acc += prod_signs[i] ? -(int64_t)sig : (int64_t)sig; \ + } \ + \ + sign = acc < 0; \ + sig = sign ? -(uint64_t)acc : acc; \ + if (sig == 0) { \ + return 0; \ + } \ + \ + lead = 63 - clz64(sig); \ + exp = max_prod_ref_exp - OUT_M - guard_bits + lead; \ + overflow = exp >= overflow_exp; \ + if (overflow) { \ + float_raise(float_flag_overflow, fp_status); \ + sig = 0; \ + exp = overflow_exp; \ + } else { \ + sig_bits = MAX(0, exp <= 0 ? OUT_M - (1 - exp) : OUT_M); \ + if (lead > sig_bits) { \ + shift64RightJamming(sig, lead - sig_bits, &sig); \ + } else { \ + sig <<= (sig_bits - lead); \ + } \ + exp = MAX(exp, 0); \ + } \ + return (TO)deposit64(deposit64(sig, OUT_M, OUT_E, exp), \ + OUT_M + OUT_E, \ + 1, sign); \ +} + +GEN_RVBNA(float8_e4m3, float8_e4m3, float32, 4, 3, 4, 3, 8, 23) +GEN_RVBNA(float8_e4m3, float8_e5m2, float32, 4, 3, 5, 2, 8, 23) +GEN_RVBNA(float8_e5m2, float8_e4m3, float32, 5, 2, 4, 3, 8, 23) +GEN_RVBNA(float8_e5m2, float8_e5m2, float32, 5, 2, 5, 2, 8, 23) +GEN_RVBNA(bfloat16, bfloat16, float32, 8, 7, 8, 7, 8, 23) diff --git a/target/riscv/tcg/rvbna.h b/target/riscv/tcg/rvbna.h new file mode 100644 index 00000000000..97aed9b372e --- /dev/null +++ b/target/riscv/tcg/rvbna.h @@ -0,0 +1,37 @@ +/* + * RISC-V Bulk Normalization Algorithm (RVBNA) + * + * Copyright (c) 2026 SiFive, Inc. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef TARGET_RISCV_TCG_RVBNA_H +#define TARGET_RISCV_TCG_RVBNA_H + +#include "fpu/softfloat.h" + +float32 rvbna_final_f32_add(float32 partial, float32 accumulator, + float_status *fp_status); + +float32 rvbna_dot_float8_e4m3_float8_e4m3(const float8_e4m3 a[], + const float8_e4m3 b[], + uint32_t n_prod, + float_status *fp_status); +float32 rvbna_dot_float8_e4m3_float8_e5m2(const float8_e4m3 a[], + const float8_e5m2 b[], + uint32_t n_prod, + float_status *fp_status); +float32 rvbna_dot_float8_e5m2_float8_e4m3(const float8_e5m2 a[], + const float8_e4m3 b[], + uint32_t n_prod, + float_status *fp_status); +float32 rvbna_dot_float8_e5m2_float8_e5m2(const float8_e5m2 a[], + const float8_e5m2 b[], + uint32_t n_prod, + float_status *fp_status); +float32 rvbna_dot_bfloat16_bfloat16(const bfloat16 a[], const bfloat16 b[], + uint32_t n_prod, + float_status *fp_status); + +#endif diff --git a/target/riscv/tcg/vector_helper.c b/target/riscv/tcg/vector_helper.c index 917a48b5106..172ae0f11dc 100644 --- a/target/riscv/tcg/vector_helper.c +++ b/target/riscv/tcg/vector_helper.c @@ -31,6 +31,7 @@ #include "tcg/tcg-gvec-desc.h" #include "internals.h" #include "vector_internals.h" +#include "rvbna.h" #include static target_ulong vtype_reserved(CPURISCVState *env, target_ulong vtype) -- 2.43.0