[PATCH 04/18] fpu/softfloat: Add OCP(Open Compute Project) OFP4 data type

Max Chou posted 18 patches 1 month ago
There is a newer version of this series
[PATCH 04/18] fpu/softfloat: Add OCP(Open Compute Project) OFP4 data type
Posted by Max Chou 1 month ago
This commit provides the basic operation support for the OCP float4 data
type(e2m1).

Signed-off-by: Max Chou <max.chou@sifive.com>
---
 include/fpu/softfloat-types.h |  7 +++++-
 include/fpu/softfloat.h       | 45 +++++++++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/include/fpu/softfloat-types.h b/include/fpu/softfloat-types.h
index 835dd33bf1..82a54e9e6d 100644
--- a/include/fpu/softfloat-types.h
+++ b/include/fpu/softfloat-types.h
@@ -120,7 +120,7 @@ typedef struct {
 typedef uint16_t bfloat16;
 
 /*
- * Software OCP(Open Compute Project) 8-bit floating point types
+ * Software OCP(Open Compute Project) floating point types
  */
 typedef uint8_t float8_e4m3;
 typedef uint8_t float8_e5m2;
@@ -131,6 +131,11 @@ typedef uint8_t float8_e5m2;
 #define const_float8_e4m3(x) (x)
 #define const_float8_e5m2(x) (x)
 
+typedef uint8_t float4_e2m1;
+#define float4_e2m1_val(x) (x & 0xf)
+#define make_float4_e2m1(x) (x & 0xf)
+#define const_float4_e2m1(x) (x & 0xf)
+
 /*
  * Software IEC/IEEE floating-point underflow tininess-detection mode.
  */
diff --git a/include/fpu/softfloat.h b/include/fpu/softfloat.h
index 7ab585bfc8..13b882bc67 100644
--- a/include/fpu/softfloat.h
+++ b/include/fpu/softfloat.h
@@ -190,7 +190,7 @@ float128 uint64_to_float128(uint64_t, float_status *status);
 float128 uint128_to_float128(Int128, float_status *status);
 
 /*----------------------------------------------------------------------------
-| Software OCP FP8 conversion routines.
+| Software OCP FP conversion routines.
 *----------------------------------------------------------------------------*/
 
 bfloat16 float8_e4m3_to_bfloat16(float8_e4m3, float_status *status);
@@ -201,7 +201,7 @@ float8_e4m3 float32_to_float8_e4m3(float32, bool saturate, float_status *status)
 float8_e5m2 float32_to_float8_e5m2(float32, bool saturate, float_status *status);
 
 /*----------------------------------------------------------------------------
-| Software OCP FP8 operations.
+| Software OCP FP operations.
 *----------------------------------------------------------------------------*/
 
 bool float8_e4m3_is_quiet_nan(float8_e4m3, float_status *status);
@@ -270,6 +270,47 @@ static inline bool float8_e5m2_is_normal(float8_e5m2 a)
     return (((float8_e5m2_val(a) >> 2) + 1) & 0x1f) >= 2;
 }
 
+static inline bool float4_e2m1_is_quiet_nan(float4_e2m1 a, float_status *status)
+{
+    return false;
+}
+
+static inline bool float4_e2m1_is_signaling_nan(float4_e2m1 a, float_status *status)
+{
+    return false;
+}
+
+static inline bool float4_e2m1_is_any_nan(float4_e2m1 a)
+{
+    return false;
+}
+
+static inline bool float4_e2m1_is_neg(float4_e2m1 a)
+{
+    return float4_e2m1_val(a) >> 3;
+}
+
+static inline bool float4_e2m1_is_infinity(float4_e2m1 a)
+{
+    return false;
+}
+
+static inline bool float4_e2m1_is_zero(float4_e2m1 a)
+{
+    return (float4_e2m1_val(a) & 0x7) == 0;
+}
+
+static inline bool float4_e2m1_is_zero_or_denormal(float4_e2m1 a)
+{
+    return (float4_e2m1_val(a) & 0x6) == 0;
+}
+
+static inline bool float4_e2m1_is_normal(float4_e2m1 a)
+{
+    uint8_t em = float4_e2m1_val(a) & 0x7;
+    return em >= 0x2 && em <= 0x7;
+}
+
 /*----------------------------------------------------------------------------
 | Software half-precision conversion routines.
 *----------------------------------------------------------------------------*/
-- 
2.43.7
Re: [PATCH 04/18] fpu/softfloat: Add OCP(Open Compute Project) OFP4 data type
Posted by Chao Liu 1 month ago
Hi, Max:

On 1/8/2026 11:16 PM, Max Chou wrote:
> This commit provides the basic operation support for the OCP float4 data
> type(e2m1).
> 
> Signed-off-by: Max Chou <max.chou@sifive.com>
> ---
>  include/fpu/softfloat-types.h |  7 +++++-
>  include/fpu/softfloat.h       | 45 +++++++++++++++++++++++++++++++++--
>  2 files changed, 49 insertions(+), 3 deletions(-)
> 
> diff --git a/include/fpu/softfloat-types.h b/include/fpu/softfloat-types.h
> index 835dd33bf1..82a54e9e6d 100644
> --- a/include/fpu/softfloat-types.h
> +++ b/include/fpu/softfloat-types.h
> @@ -120,7 +120,7 @@ typedef struct {
>  typedef uint16_t bfloat16;
>  
>  /*
> - * Software OCP(Open Compute Project) 8-bit floating point types
> + * Software OCP(Open Compute Project) floating point types
>   */
>  typedef uint8_t float8_e4m3;
>  typedef uint8_t float8_e5m2;
> @@ -131,6 +131,11 @@ typedef uint8_t float8_e5m2;
>  #define const_float8_e4m3(x) (x)
>  #define const_float8_e5m2(x) (x)
>  
> +typedef uint8_t float4_e2m1;
> +#define float4_e2m1_val(x) (x & 0xf)
> +#define make_float4_e2m1(x) (x & 0xf)
> +#define const_float4_e2m1(x) (x & 0xf)
> +
I recommend adding parentheses around the parameter `x`:

    #define float4_e2m1_val(x) ((x) & 0xf)
    #define make_float4_e2m1(x) ((x) & 0xf)
    #define const_float4_e2m1(x) ((x) & 0xf)

Thanks,
Chao