From: <Ezra.Sitorus@arm.com>
To: <gdb-patches@sourceware.org>
Cc: <eliz@gnu.org>, <luis.machado.foss@gmail.com>,
Ezra Sitorus <ezra.sitorus@arm.com>
Subject: [PATCH] gdb: Add support for fp8 formats
Date: Thu, 25 Sep 2025 22:18:02 +0100 [thread overview]
Message-ID: <20250925211804.21967-3-Ezra.Sitorus@arm.com> (raw)
In-Reply-To: <20250925211804.21967-1-Ezra.Sitorus@arm.com>
From: Ezra Sitorus <ezra.sitorus@arm.com>
FP8 introduces 2 new formats: E5M2 and E4M3. E5M2 follows IEEE
conventions for exponents and special values. On the other hand, E4M3
increases the dynamic range by having fewer special values - there is no
infinity, and only one encoding for NaN. This has been accounted for in
gdb/target-float.c: floatformat_classify and from_target.
One particular thing I would like to highlight is that since e4m3 has
only 1 NaN encoding - all ones - this will be printed as nan(0x7). You
can see this in the test cases later. I would like to ask if there is a
better way of handling this?
More information about FP8 can be found in the technical paper here:
https://arxiv.org/abs/2209.05433
---
gdb/gdbtypes.c | 8 ++++++++
gdb/gdbtypes.h | 2 ++
gdb/target-descriptions.c | 10 ++++++++++
gdb/target-float.c | 12 +++++++++++-
gdbsupport/tdesc.cc | 4 +++-
gdbsupport/tdesc.h | 2 ++
include/floatformat.h | 5 +++++
7 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 24e6d0bf8f5..f07c259e25b 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -79,6 +79,14 @@ const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
const struct rank VARARG_BADNESS = {4, 0};
/* Floatformat pairs. */
+const struct floatformat *floatformats_fp8_e5m2[BFD_ENDIAN_UNKNOWN] = {
+ &floatformat_fp8_e5m2_big,
+ &floatformat_fp8_e5m2_little
+};
+const struct floatformat *floatformats_fp8_e4m3[BFD_ENDIAN_UNKNOWN] = {
+ &floatformat_fp8_e4m3_big,
+ &floatformat_fp8_e4m3_little
+};
const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
&floatformat_ieee_half_big,
&floatformat_ieee_half_little
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 75c77b3a90c..6faffb4217d 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2209,6 +2209,8 @@ extern const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD
extern const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN];
extern const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN];
extern const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN];
+extern const struct floatformat *floatformats_fp8_e5m2[BFD_ENDIAN_UNKNOWN];
+extern const struct floatformat *floatformats_fp8_e4m3[BFD_ENDIAN_UNKNOWN];
extern const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN];
extern const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN];
extern const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN];
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index dc5fbacfebd..9f9cc9ad95f 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -147,6 +147,16 @@ make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
m_type = init_float_type (alloc, -1, "builtin_type_bfloat16",
floatformats_bfloat16);
return;
+
+ case TDESC_TYPE_FP8_E5M2:
+ m_type = init_float_type (alloc, -1, "builtin_type_fp8_e5m2",
+ floatformats_fp8_e5m2);
+ return;
+
+ case TDESC_TYPE_FP8_E4M3:
+ m_type = init_float_type (alloc, -1, "builtin_type_fp8_e4m3",
+ floatformats_fp8_e4m3);
+ return;
}
internal_error ("Type \"%s\" has an unknown kind %d",
diff --git a/gdb/target-float.c b/gdb/target-float.c
index 9b0376f2909..d0068e9f453 100644
--- a/gdb/target-float.c
+++ b/gdb/target-float.c
@@ -396,6 +396,12 @@ floatformat_classify (const struct floatformat *fmt,
if (exponent == fmt->exp_nan)
{
+ if (fmt->totalsize == 8 && fmt->exp_len == 4)
+ {
+ if ((mant & 0b111) == 0b111)
+ return float_nan;
+ return float_normal;
+ }
if (mant_zero)
return float_infinite;
else
@@ -1287,7 +1293,11 @@ mpfr_float_ops::from_target (const struct floatformat *fmt,
mant_off = fmt->man_start;
mpfr_set_zero (to.val, 0);
- special_exponent = exponent == 0 || exponent == fmt->exp_nan;
+ /* If format is FP8 E4M3, then we handle the case of NaN above, and there is
+ no inf. */
+ special_exponent = (exponent == 0)
+ || (!(fmt->totalsize == 8 && fmt->exp_len == 4)
+ && (exponent == fmt->exp_nan));
/* Don't bias NaNs. Use minimum exponent for denorms. For
simplicity, we don't check for zero as the exponent doesn't matter.
diff --git a/gdbsupport/tdesc.cc b/gdbsupport/tdesc.cc
index ed92c864732..2b029484e51 100644
--- a/gdbsupport/tdesc.cc
+++ b/gdbsupport/tdesc.cc
@@ -54,7 +54,9 @@ static tdesc_type_builtin tdesc_predefined_types[] =
{ "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
{ "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
{ "i387_ext", TDESC_TYPE_I387_EXT },
- { "bfloat16", TDESC_TYPE_BFLOAT16 }
+ { "bfloat16", TDESC_TYPE_BFLOAT16 },
+ { "fp8_e5m2", TDESC_TYPE_FP8_E5M2 },
+ { "fp8_e4m3", TDESC_TYPE_FP8_E4M3 }
};
void tdesc_feature::accept (tdesc_element_visitor &v) const
diff --git a/gdbsupport/tdesc.h b/gdbsupport/tdesc.h
index 6bf66ad3dcd..f898f49647e 100644
--- a/gdbsupport/tdesc.h
+++ b/gdbsupport/tdesc.h
@@ -176,6 +176,8 @@ enum tdesc_type_kind
TDESC_TYPE_ARM_FPA_EXT,
TDESC_TYPE_I387_EXT,
TDESC_TYPE_BFLOAT16,
+ TDESC_TYPE_FP8_E5M2,
+ TDESC_TYPE_FP8_E4M3,
/* Types defined by a target feature. */
TDESC_TYPE_VECTOR,
diff --git a/include/floatformat.h b/include/floatformat.h
index b65968a8755..dadb5a217aa 100644
--- a/include/floatformat.h
+++ b/include/floatformat.h
@@ -136,6 +136,11 @@ extern const struct floatformat floatformat_ibm_long_double_little;
/* bfloat16. */
extern const struct floatformat floatformat_bfloat16_big;
extern const struct floatformat floatformat_bfloat16_little;
+/* fp8. */
+extern const struct floatformat floatformat_fp8_e5m2_big;
+extern const struct floatformat floatformat_fp8_e5m2_little;
+extern const struct floatformat floatformat_fp8_e4m3_big;
+extern const struct floatformat floatformat_fp8_e4m3_little;
/* Convert from FMT to a double.
FROM is the address of the extended float.
--
2.45.2
next prev parent reply other threads:[~2025-09-25 21:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-25 21:18 [PATCH 0/4] gdb: Display fp8 format for AArch64 registers Ezra.Sitorus
2025-09-25 21:18 ` [PATCH 1/4] libiberty: Add fp8 float formats Ezra.Sitorus
2025-09-25 21:18 ` Ezra.Sitorus [this message]
2025-09-25 21:18 ` [PATCH 3/4] gdb/aarch64: Display fp8 formats in registers Ezra.Sitorus
2025-10-11 13:10 ` Luis
2025-10-14 0:56 ` Ezra Sitorus
2025-10-17 10:20 ` Luis
2025-09-25 21:18 ` [PATCH 4/4] gdb/aarch64: Tests for verifying fp8 register formats Ezra.Sitorus
2025-09-29 17:55 ` [PATCH 0/4] gdb: Display fp8 format for AArch64 registers Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250925211804.21967-3-Ezra.Sitorus@arm.com \
--to=ezra.sitorus@arm.com \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
--cc=luis.machado.foss@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox