gcc/testsuite/ChangeLog:

        * gcc.target/i386/avx10-check.h: Add builtin check for AVX10V2AUX.
        * gcc.target/i386/avx10-helper.h: Add random input helper.
        * gcc.target/i386/fp8-helper.h: Add fp32 to fp8 enulation helper.
        * gcc/testsuite/gcc.target/i386/avx10v2aux-ps2fp8-2.h: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2bf8-2.c: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2bf8-2.h: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.c: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.h: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2hf8-2.c: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2hf8-2.h: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.c: New test.
        * gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.h: New test.
        * gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.c: New test.
        * gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.h: New test.
        * gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.c: New test.
        * gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.h: New test.
---
 gcc/testsuite/gcc.target/i386/avx10-check.h   |   3 +
 gcc/testsuite/gcc.target/i386/avx10-helper.h  |  28 ++
 .../gcc.target/i386/avx10v2aux-ps2fp8-2.h     | 100 ++++++++
 .../gcc.target/i386/avx10v2aux-vcvtps2bf8-2.c |  20 ++
 .../gcc.target/i386/avx10v2aux-vcvtps2bf8-2.h |   7 +
 .../i386/avx10v2aux-vcvtps2bf8s-2.c           |  20 ++
 .../i386/avx10v2aux-vcvtps2bf8s-2.h           |   7 +
 .../gcc.target/i386/avx10v2aux-vcvtps2hf8-2.c |  20 ++
 .../gcc.target/i386/avx10v2aux-vcvtps2hf8-2.h |   7 +
 .../i386/avx10v2aux-vcvtps2hf8s-2.c           |  20 ++
 .../i386/avx10v2aux-vcvtps2hf8s-2.h           |   7 +
 .../i386/avx10v2aux-vcvtrops2hf8-2.c          |  20 ++
 .../i386/avx10v2aux-vcvtrops2hf8-2.h          |   7 +
 .../i386/avx10v2aux-vcvtrops2hf8s-2.c         |  20 ++
 .../i386/avx10v2aux-vcvtrops2hf8s-2.h         |   7 +
 gcc/testsuite/gcc.target/i386/fp8-helper.h    | 242 ++++++++++++++++++
 16 files changed, 535 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-ps2fp8-2.h
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.h
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.h
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.h
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.h
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.h
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.c
 create mode 100644 gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.h

diff --git a/gcc/testsuite/gcc.target/i386/avx10-check.h 
b/gcc/testsuite/gcc.target/i386/avx10-check.h
index 6a1a151f9091..e8fdc921e1b4 100644
--- a/gcc/testsuite/gcc.target/i386/avx10-check.h
+++ b/gcc/testsuite/gcc.target/i386/avx10-check.h
@@ -39,6 +39,9 @@ main ()
   if (__builtin_cpu_supports ("avx10.1")
 #ifdef AVX10_2
       && __builtin_cpu_supports ("avx10.2")
+#endif
+#ifdef AVX10V2AUX
+      && __builtin_cpu_supports ("avx10v2aux")
 #endif
       && avx10_os_support ())
     {
diff --git a/gcc/testsuite/gcc.target/i386/avx10-helper.h 
b/gcc/testsuite/gcc.target/i386/avx10-helper.h
index ee5fd43de18f..12d6d96fc935 100644
--- a/gcc/testsuite/gcc.target/i386/avx10-helper.h
+++ b/gcc/testsuite/gcc.target/i386/avx10-helper.h
@@ -7,6 +7,28 @@
 #include "avx512f-helper.h"
 #include <stdint.h>
 
+static unsigned int v2aux_rand_state = 0x2545f491;
+
+static unsigned int
+v2aux_rand (void)
+{
+  v2aux_rand_state ^= v2aux_rand_state << 13;
+  v2aux_rand_state ^= v2aux_rand_state >> 17;
+  v2aux_rand_state ^= v2aux_rand_state << 5;
+  return v2aux_rand_state;
+}
+
+/* Random seeded FP32 encoding  */
+
+static unsigned int
+v2aux_rand_fp32 (void)
+{
+  unsigned int r = v2aux_rand ();
+  unsigned int exp = 127 - 26 + (r >> 8) % 44;
+
+  return (r & 0x80000000) | (exp << 23) | (v2aux_rand () & 0x7fffff);
+}
+
 #define NOINLINE __attribute__((noinline,noclone))
 typedef union
 {
@@ -94,3 +116,9 @@ getmant(float val)
 #else
 #define INTRINSIC(NAME) _mm ## NAME
 #endif
+
+#ifdef AVX10V2AUX
+/* Expand intrinsic suffix macros (e.g. PS2FP8_I) before INTRINSIC().  */
+#undef V2AUX_I
+#define V2AUX_I(F) INTRINSIC(F)
+#endif
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-ps2fp8-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-ps2fp8-2.h
new file mode 100644
index 000000000000..4a8ea03096e4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-ps2fp8-2.h
@@ -0,0 +1,100 @@
+#define AVX10V2AUX
+#include "avx10-helper.h"
+#include "fp8-helper.h"
+#include <string.h>
+
+#define SRC_SIZE (AVX512F_LEN / 32)
+#define SIZE 16
+#include "avx512f-mask-type.h"
+
+#ifndef PS2FP8_DATA_INCLUDED
+#define PS2FP8_DATA_INCLUDED
+
+static const unsigned int ps2fp8_directed[] = {
+  /* Zeros, FP32 denormals, infinities, NaNs and the FP32 extremes.  */
+  0x00000000, 0x00000001, 0x007fffff, 0x00800000, 0x7f7fffff,
+  0x7f800000, 0x7fc00000, 0x7fa00000, 0x7f800001,
+  /* E4M3: the maximum normal, the ties above it, the NaN encoding and
+     the first overflowing exponent.  */
+  0x43e00000, 0x43e00001, 0x43e80000, 0x43e80001, 0x43f00000,
+  0x43f80000, 0x44000000,
+  /* E4M3: normal-range ties with even and odd LSB, values on either side
+     of a tie and a tie that carries into the exponent.  */
+  0x3f880000, 0x3f87ffff, 0x3f880001, 0x3f980000, 0x3ff80000,
+  /* E4M3: subnormal results and ties, a tie that rounds up to the minimum
+     normal and magnitudes below the smallest subnormal.  */
+  0x3c800000, 0x3c000000, 0x3b000000, 0x3b400000, 0x3ba00000,
+  0x3b800001, 0x3c700000, 0x3a800000, 0x3a000000,
+  /* E4M3: one sticky bit and an exact value.  */
+  0x3f800001, 0x3f900000,
+  /* E5M2: the maximum normal, the overflow tie and the value below it,
+     normal-range ties and a tie that carries into the exponent.  */
+  0x47600000, 0x47700000, 0x476fffff, 0x3fb00000, 0x3ff00000,
+  /* E5M2: subnormal results and ties, a tie that rounds up to the minimum
+     normal and magnitudes around half of the smallest subnormal.  */
+  0x38800000, 0x37800000, 0x38000000, 0x38400000, 0x37000000,
+  0x37000001, 0x37c00000, 0x38200000, 0x38600000, 0x36800000
+};
+
+#define PS2FP8_DIRECTED (2 * (int) ARRAY_SIZE (ps2fp8_directed))
+#define PS2FP8_COUNT (PS2FP8_DIRECTED + 2048)
+
+static unsigned int
+ps2fp8_input (int n)
+{
+  if (n < PS2FP8_DIRECTED)
+    return ps2fp8_directed[n / 2] ^ (n & 1 ? 0x80000000 : 0);
+  return v2aux_rand_fp32 ();
+}
+
+#endif
+
+static void
+CALC (unsigned char *r, unsigned int *s)
+{
+  int i;
+
+  for (i = 0; i < SIZE; i++)
+    r[i] = 0;
+
+  for (i = 0; i < SRC_SIZE; i++)
+    r[i] = convert_fp32_to_fp8 (s[i], 0, PS2FP8_HF8_BF8, PS2FP8_SATURATE,
+                               PS2FP8_ROUNDING);
+}
+
+void
+TEST (void)
+{
+  int i, n;
+  UNION_TYPE (128, i_ub) res1, res2, res3;
+  UNION_TYPE (AVX512F_LEN, ) src;
+  MASK_TYPE mask = MASK_VALUE;
+  unsigned char res_ref[SIZE];
+  unsigned int bits[SRC_SIZE];
+
+  for (n = 0; n < PS2FP8_COUNT; n += SRC_SIZE)
+    {
+      for (i = 0; i < SRC_SIZE; i++)
+       bits[i] = ps2fp8_input (n + i);
+      memcpy (src.a, bits, sizeof (bits));
+
+      for (i = 0; i < SIZE; i++)
+       res2.a[i] = DEFAULT_VALUE;
+
+      CALC (res_ref, bits);
+
+      res1.x = V2AUX_I (PS2FP8_I) (src.x);
+      if (UNION_CHECK (128, i_ub) (res1, res_ref))
+       abort ();
+
+      res2.x = V2AUX_I (PS2FP8_MASK_I) (res2.x, mask, src.x);
+      MASK_MERGE (i_ub) (res_ref, mask, SRC_SIZE);
+      if (UNION_CHECK (128, i_ub) (res2, res_ref))
+       abort ();
+
+      res3.x = V2AUX_I (PS2FP8_MASKZ_I) (mask, src.x);
+      MASK_ZERO (i_ub) (res_ref, mask, SRC_SIZE);
+      if (UNION_CHECK (128, i_ub) (res3, res_ref))
+       abort ();
+    }
+}
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.c 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.c
new file mode 100644
index 000000000000..c0d4a256202f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.c
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=x86-64-v3 -mavx10v2aux" } */
+/* { dg-require-effective-target avx10v2aux } */
+
+#include "avx10v2aux-vcvtps2bf8-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512VL
+#define AVX512F_LEN 256
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2bf8-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512F_LEN 128
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2bf8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.h
new file mode 100644
index 000000000000..b7494a7452ae
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8-2.h
@@ -0,0 +1,7 @@
+#define PS2FP8_I _cvtps_bf8
+#define PS2FP8_MASK_I _mask_cvtps_bf8
+#define PS2FP8_MASKZ_I _maskz_cvtps_bf8
+#define PS2FP8_HF8_BF8 1
+#define PS2FP8_SATURATE 0
+#define PS2FP8_ROUNDING FP8_ROUND_RTNE
+#include "avx10v2aux-ps2fp8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.c 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.c
new file mode 100644
index 000000000000..3e24fdfa2a3d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.c
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=x86-64-v3 -mavx10v2aux" } */
+/* { dg-require-effective-target avx10v2aux } */
+
+#include "avx10v2aux-vcvtps2bf8s-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512VL
+#define AVX512F_LEN 256
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2bf8s-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512F_LEN 128
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2bf8s-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.h
new file mode 100644
index 000000000000..65347c6866ea
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2bf8s-2.h
@@ -0,0 +1,7 @@
+#define PS2FP8_I _cvts_ps_bf8
+#define PS2FP8_MASK_I _mask_cvts_ps_bf8
+#define PS2FP8_MASKZ_I _maskz_cvts_ps_bf8
+#define PS2FP8_HF8_BF8 1
+#define PS2FP8_SATURATE 1
+#define PS2FP8_ROUNDING FP8_ROUND_RTNE
+#include "avx10v2aux-ps2fp8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.c 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.c
new file mode 100644
index 000000000000..939e6ee1dcb8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.c
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=x86-64-v3 -mavx10v2aux" } */
+/* { dg-require-effective-target avx10v2aux } */
+
+#include "avx10v2aux-vcvtps2hf8-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512VL
+#define AVX512F_LEN 256
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2hf8-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512F_LEN 128
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2hf8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.h
new file mode 100644
index 000000000000..0f4eda55992d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8-2.h
@@ -0,0 +1,7 @@
+#define PS2FP8_I _cvtps_hf8
+#define PS2FP8_MASK_I _mask_cvtps_hf8
+#define PS2FP8_MASKZ_I _maskz_cvtps_hf8
+#define PS2FP8_HF8_BF8 0
+#define PS2FP8_SATURATE 0
+#define PS2FP8_ROUNDING FP8_ROUND_RTNE
+#include "avx10v2aux-ps2fp8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.c 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.c
new file mode 100644
index 000000000000..97368207345e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.c
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=x86-64-v3 -mavx10v2aux" } */
+/* { dg-require-effective-target avx10v2aux } */
+
+#include "avx10v2aux-vcvtps2hf8s-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512VL
+#define AVX512F_LEN 256
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2hf8s-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512F_LEN 128
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtps2hf8s-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.h
new file mode 100644
index 000000000000..5ef72280bfa0
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtps2hf8s-2.h
@@ -0,0 +1,7 @@
+#define PS2FP8_I _cvts_ps_hf8
+#define PS2FP8_MASK_I _mask_cvts_ps_hf8
+#define PS2FP8_MASKZ_I _maskz_cvts_ps_hf8
+#define PS2FP8_HF8_BF8 0
+#define PS2FP8_SATURATE 1
+#define PS2FP8_ROUNDING FP8_ROUND_RTNE
+#include "avx10v2aux-ps2fp8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.c 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.c
new file mode 100644
index 000000000000..b837f88bb743
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.c
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=x86-64-v3 -mavx10v2aux" } */
+/* { dg-require-effective-target avx10v2aux } */
+
+#include "avx10v2aux-vcvtrops2hf8-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512VL
+#define AVX512F_LEN 256
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtrops2hf8-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512F_LEN 128
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtrops2hf8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.h
new file mode 100644
index 000000000000..a4202fbb7c98
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8-2.h
@@ -0,0 +1,7 @@
+#define PS2FP8_I _cvtrops_hf8
+#define PS2FP8_MASK_I _mask_cvtrops_hf8
+#define PS2FP8_MASKZ_I _maskz_cvtrops_hf8
+#define PS2FP8_HF8_BF8 0
+#define PS2FP8_SATURATE 0
+#define PS2FP8_ROUNDING FP8_ROUND_RTO
+#include "avx10v2aux-ps2fp8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.c 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.c
new file mode 100644
index 000000000000..c6bf25573d35
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.c
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -march=x86-64-v3 -mavx10v2aux" } */
+/* { dg-require-effective-target avx10v2aux } */
+
+#include "avx10v2aux-vcvtrops2hf8s-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512VL
+#define AVX512F_LEN 256
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtrops2hf8s-2.h"
+
+#undef AVX512F_LEN
+#undef AVX512F_LEN_HALF
+
+#define AVX512F_LEN 128
+#define AVX512F_LEN_HALF 128
+#include "avx10v2aux-vcvtrops2hf8s-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.h 
b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.h
new file mode 100644
index 000000000000..89301da44374
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx10v2aux-vcvtrops2hf8s-2.h
@@ -0,0 +1,7 @@
+#define PS2FP8_I _cvts_rops_hf8
+#define PS2FP8_MASK_I _mask_cvts_rops_hf8
+#define PS2FP8_MASKZ_I _maskz_cvts_rops_hf8
+#define PS2FP8_HF8_BF8 0
+#define PS2FP8_SATURATE 1
+#define PS2FP8_ROUNDING FP8_ROUND_RTO
+#include "avx10v2aux-ps2fp8-2.h"
diff --git a/gcc/testsuite/gcc.target/i386/fp8-helper.h 
b/gcc/testsuite/gcc.target/i386/fp8-helper.h
index f113ec76f4aa..4a75359951f3 100644
--- a/gcc/testsuite/gcc.target/i386/fp8-helper.h
+++ b/gcc/testsuite/gcc.target/i386/fp8-helper.h
@@ -3,6 +3,7 @@
 
 #include <stdint.h>
 #include <inttypes.h>
+#include <stdbool.h>
 
 typedef union 
 {
@@ -274,4 +275,245 @@ shift_int128_to_fp32 (__int128_t in, int type1, int type2)
 }
 #endif
 
+enum fp8_rounding
+{
+  FP8_ROUND_RTNE,
+  FP8_ROUND_RTO,
+  FP8_ROUND_BIAS
+};
+
+static unsigned int
+mask_u32 (int bits)
+{
+  if (bits <= 0)
+    return 0;
+  if (bits >= 32)
+    return 0xffffffffu;
+  return (1u << bits) - 1u;
+}
+
+static unsigned char
+convert_fp32_to_bf8 (unsigned int x, unsigned int b, bool s,
+                    enum fp8_rounding r)
+{
+  unsigned int sign = (x >> 31) & 0x1;
+  unsigned int exp = (x >> 23) & 0xFF;
+  unsigned int mant = x & 0x7FFFFF;
+  unsigned int e_o = 0;
+  unsigned int m_o = 0;
+  int newexp;
+  int shift;
+
+  if (exp == 0xFF)
+    {
+      if (mant == 0 && s)
+       {
+         e_o = 0x1E;
+         m_o = 0x3;
+       }
+      else
+       {
+         e_o = 0x1F;
+         m_o = mant != 0 ? 0x2 | ((mant >> 21) & 0x1) : 0x0;
+       }
+    }
+  else if (exp == 0)
+    ;
+  else if (r == FP8_ROUND_BIAS)
+    {
+      mant += b & 0x1FFFFF;
+      if (mant & 0xFF800000)
+       exp++;
+      mant &= 0x7FFFFF;
+      newexp = (int) exp - 127 + 15;
+      if (newexp >= 31)
+       {
+         e_o = s ? 0x1E : 0x1F;
+         m_o = s ? 0x3 : 0x0;
+       }
+      else if (newexp <= 0)
+       {
+         shift = 22 - newexp;
+         if (shift <= 24)
+           m_o = (mant | 0x800000) >> shift;
+       }
+      else
+       {
+         e_o = newexp;
+         m_o = mant >> 21;
+       }
+    }
+  else
+    {
+      newexp = (int) exp - 127 + 15;
+      if (newexp >= 31)
+       {
+         e_o = s ? 0x1E : 0x1F;
+         m_o = s ? 0x3 : 0x0;
+       }
+      else if (newexp <= 0)
+       {
+         shift = 22 - newexp;
+         if (shift <= 24)
+           {
+             unsigned int mant_full = mant | 0x800000;
+             unsigned int low = mant_full & mask_u32 (shift);
+             unsigned int half = 1u << (shift - 1);
+
+             m_o = mant_full >> shift;
+             if (low > half || (low == half && (m_o & 1)))
+               {
+                 m_o++;
+                 if ((m_o & 0x3) == 0)
+                   e_o++;
+               }
+           }
+       }
+      else
+       {
+         e_o = newexp;
+         m_o = mant >> 21;
+         if ((mant & 0x100000)
+             && ((mant & 0x1FFFFF) > 0x100000 || (m_o & 1))
+             && !(s && e_o == 0x1E && m_o == 0x3))
+           {
+             m_o++;
+             if ((m_o & 0x3) == 0)
+               e_o++;
+           }
+       }
+    }
+
+  return (unsigned char) ((sign << 7) | ((e_o & 0x1F) << 2) | (m_o & 0x3));
+}
+
+static unsigned char
+convert_fp32_to_hf8 (unsigned int x, unsigned int b, bool s,
+                    enum fp8_rounding r)
+{
+  unsigned int sign = (x >> 31) & 0x1;
+  unsigned int exp = (x >> 23) & 0xFF;
+  unsigned int mant = x & 0x7FFFFF;
+  unsigned int e_o = 0;
+  unsigned int m_o = 0;
+  int newexp;
+  int shift;
+
+  if (exp == 0xFF)
+    {
+      e_o = 0xF;
+      m_o = s && mant == 0 ? 0x6 : 0x7;
+    }
+  else if (exp == 0)
+    ;
+  else if (r == FP8_ROUND_BIAS)
+    {
+      mant += b & 0xFFFFF;
+      if (mant & 0xFF800000)
+       exp++;
+      mant &= 0x7FFFFF;
+      newexp = (int) exp - 127 + 7;
+      if (newexp >= 16)
+       {
+         e_o = 0xF;
+         m_o = s ? 0x6 : 0x7;
+       }
+      else if (newexp > 0)
+       {
+         e_o = newexp;
+         m_o = mant >> 20;
+       }
+    }
+  else if (r == FP8_ROUND_RTO)
+    {
+      newexp = (int) exp - 127 + 7;
+      if (newexp >= 16)
+       {
+         e_o = 0xF;
+         m_o = s ? 0x6 : 0x7;
+       }
+      else if (newexp <= 0)
+       {
+         shift = 21 - newexp;
+         if (shift <= 24)
+           {
+             unsigned int mant_full = mant | 0x800000;
+
+             m_o = mant_full >> shift;
+             if (mant_full & mask_u32 (shift))
+               m_o |= 1;
+           }
+         else
+           m_o = 1;
+       }
+      else
+       {
+         e_o = newexp;
+         m_o = mant >> 20;
+         if (mant & 0xFFFFF)
+           m_o |= 1;
+         if (s && e_o == 0xF && m_o == 0x7)
+           m_o = 0x6;
+       }
+    }
+  else
+    {
+      newexp = (int) exp - 127 + 7;
+      if (newexp >= 16)
+       {
+         e_o = 0xF;
+         m_o = s ? 0x6 : 0x7;
+       }
+      else if (newexp <= 0)
+       {
+         shift = 21 - newexp;
+         if (shift <= 24)
+           {
+             unsigned int mant_full = mant | 0x800000;
+             unsigned int low = mant_full & mask_u32 (shift);
+             unsigned int half = 1u << (shift - 1);
+
+             m_o = mant_full >> shift;
+             if (low > half || (low == half && (m_o & 1)))
+               {
+                 m_o++;
+                 if ((m_o & 0x7) == 0)
+                   e_o++;
+               }
+           }
+       }
+      else
+       {
+         e_o = newexp;
+         m_o = mant >> 20;
+         if (s && e_o == 0xF && m_o == 0x7)
+           m_o = 0x6;
+         if (mant & 0x80000)
+           {
+             bool clamp_sat = s && e_o == 0xF && m_o == 0x6;
+             bool clamp_nan = !s && e_o == 0xF && m_o == 0x7;
+
+             if (((mant & 0xFFFFF) > 0x80000 || (m_o & 1))
+                 && !(clamp_sat || clamp_nan))
+               {
+                 m_o++;
+                 if ((m_o & 0x7) == 0)
+                   e_o++;
+               }
+           }
+       }
+    }
+
+  return (unsigned char) ((sign << 7) | ((e_o & 0xF) << 3) | (m_o & 0x7));
+}
+
+static unsigned char
+convert_fp32_to_fp8 (unsigned int x, unsigned int b, bool bf8, bool s,
+                    enum fp8_rounding r)
+{
+  return bf8 ? convert_fp32_to_bf8 (x, b, s, r)
+            : convert_fp32_to_hf8 (x, b, s, r);
+}
+
+
 #endif
-- 
2.34.1

Reply via email to