Skip to contentSkip to conversation

Alternative softfloat 128-bit integer support

RFC PATCH In review [email protected] 3 participants 18 messages Mar 30, 2022 – Apr 20, 2022

lore
Share & tools
lore
#0 lore raw

cover Alternative softfloat 128-bit integer support

From: Matheus Ferst <[email protected]>

This RFC is an alternative to [1] using Int128 types to implement the 128-bit integer conversion routines in softfloat required by the xscv[su]qqp and xscvqp[su]qz instructions of PowerISA v3.1.

Some improvements to int128.h are made in patches 1 and 2. Patches 3-6 implement the conversion routines, and patches 7 and 8 implement the new instructions.

RFC: Int128 vs. pair of 64-bit values.
- Returning unsigned values through Int128 is not ideal, but creating an "UInt128" just for this case seems excessive.
- OTOH, there are fewer cases to handle, especially in float->int.

[1] https://lists.gnu.org/archive/html/qemu-ppc/2022-03/msg00520.html

Matheus Ferst (8):
qemu/int128: avoid undefined behavior in int128_lshift qemu/int128: add int128_urshift
softfloat: add uint128_to_float128
softfloat: add int128_to_float128
softfloat: add float128_to_uint128
softfloat: add float128_to_int128
target/ppc: implement xscv[su]qqp
target/ppc: implement xscvqp[su]qz

 fpu/softfloat.c                     | 183 ++++++++++++++++++++++++++++
 include/fpu/softfloat.h             |   7 ++
 include/qemu/int128.h               |  25 +++-
 target/ppc/fpu_helper.c             |  33 +++++
 target/ppc/helper.h                 |   4 +
 target/ppc/insn32.decode            |   7 ++
 target/ppc/translate/vsx-impl.c.inc |  22 ++++
 tests/unit/test-int128.c            |  32 +++++
 8 files changed, 311 insertions(+), 2 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/8 qemu/int128: avoid undefined behavior in int128_lshift

From: Matheus Ferst <[email protected]>

Avoid the left shift of negative values in int128_lshift by casting a/a.hi to unsigned.

---
 include/qemu/int128.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/qemu/int128.h +2 -2
index 2c4064256c..2a19558ac6 100644
@@ -85,7 +85,7 @@ static inline Int128 int128_rshift(Int128 a, int n)
 
 static inline Int128 int128_lshift(Int128 a, int n)
 {
-    return a << n;
+    return (__uint128_t)a << n;
 }
 
 static inline Int128 int128_add(Int128 a, Int128 b)
@@ -305,7 +305,7 @@ static inline Int128 int128_lshift(Int128 a, int n)
     if (n >= 64) {
         return int128_make128(0, l);
     } else if (n > 0) {
-        return int128_make128(l, (a.hi << n) | (a.lo >> (64 - n)));
+        return int128_make128(l, ((uint64_t)a.hi << n) | (a.lo >> (64 - n)));
     }
     return a;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH 1/8] qemu/int128: avoid undefined behavior in int128_lshift

Quoted 6 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst <[email protected]>

Avoid the left shift of negative values in int128_lshift by casting a/a.hi to unsigned.

Eh, maybe. We do this all over qemu, and I think any undefinedness you're thinking of in the base C standard is removed by the -fwrapv with which all files are compiled.

r~

Quoted 26 lines
---
  include/qemu/int128.h | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
include/qemu/int128.h +2 -2
index 2c4064256c..2a19558ac6 100644
@@ -85,7 +85,7 @@ static inline Int128 int128_rshift(Int128 a, int n)
 
  static inline Int128 int128_lshift(Int128 a, int n)
  {
-    return a << n;
+    return (__uint128_t)a << n;
  }
 
  static inline Int128 int128_add(Int128 a, Int128 b)
@@ -305,7 +305,7 @@ static inline Int128 int128_lshift(Int128 a, int n)
      if (n >= 64) {
          return int128_make128(0, l);
      } else if (n > 0) {
-        return int128_make128(l, (a.hi << n) | (a.lo >> (64 - n)));
+        return int128_make128(l, ((uint64_t)a.hi << n) | (a.lo >> (64 - n)));
      }
      return a;
  }
#3 lore raw

RFC PATCH 2/8 qemu/int128: add int128_urshift

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Implement an unsigned right shift for Int128 values and add the same tests cases of int128_rshift in the unit test.

---
 include/qemu/int128.h    | 19 +++++++++++++++++++
 tests/unit/test-int128.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
include/qemu/int128.h +19 -0
index 2a19558ac6..ca32b0b276 100644
@@ -83,6 +83,11 @@ static inline Int128 int128_rshift(Int128 a, int n)
     return a >> n;
 }
 
+static inline Int128 int128_urshift(Int128 a, int n)
+{
+    return (__uint128_t)a >> n;
+}
+
 static inline Int128 int128_lshift(Int128 a, int n)
 {
     return (__uint128_t)a << n;
@@ -299,6 +304,20 @@ static inline Int128 int128_rshift(Int128 a, int n)
     }
 }
 
+static inline Int128 int128_urshift(Int128 a, int n)
+{
+    uint64_t h = a.hi;
+    if (!n) {
+        return a;
+    }
+    h = h >> (n & 63);
+    if (n >= 64) {
+        return int128_make64(h);
+    } else {
+        return int128_make128((a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h);
+    }
+}
+
 static inline Int128 int128_lshift(Int128 a, int n)
 {
     uint64_t l = a.lo << (n & 63);
tests/unit/test-int128.c +32 -0
index b86a3c76e6..ae0f552193 100644
@@ -206,6 +206,37 @@ static void test_rshift(void)
     test_rshift_one(0xFFFE8000U,  0, 0xFFFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
 }
 
+static void __attribute__((__noinline__)) ATTRIBUTE_NOCLONE
+test_urshift_one(uint32_t x, int n, uint64_t h, uint64_t l)
+{
+    Int128 a = expand(x);
+    Int128 r = int128_urshift(a, n);
+    g_assert_cmpuint(int128_getlo(r), ==, l);
+    g_assert_cmpuint(int128_gethi(r), ==, h);
+}
+
+static void test_urshift(void)
+{
+    test_urshift_one(0x00010000U, 64, 0x0000000000000000ULL, 0x0000000000000001ULL);
+    test_urshift_one(0x80010000U, 64, 0x0000000000000000ULL, 0x8000000000000001ULL);
+    test_urshift_one(0x7FFE0000U, 64, 0x0000000000000000ULL, 0x7FFFFFFFFFFFFFFEULL);
+    test_urshift_one(0xFFFE0000U, 64, 0x0000000000000000ULL, 0xFFFFFFFFFFFFFFFEULL);
+    test_urshift_one(0x00010000U, 60, 0x0000000000000000ULL, 0x0000000000000010ULL);
+    test_urshift_one(0x80010000U, 60, 0x0000000000000008ULL, 0x0000000000000010ULL);
+    test_urshift_one(0x00018000U, 60, 0x0000000000000000ULL, 0x0000000000000018ULL);
+    test_urshift_one(0x80018000U, 60, 0x0000000000000008ULL, 0x0000000000000018ULL);
+    test_urshift_one(0x7FFE0000U, 60, 0x0000000000000007ULL, 0xFFFFFFFFFFFFFFE0ULL);
+    test_urshift_one(0xFFFE0000U, 60, 0x000000000000000FULL, 0xFFFFFFFFFFFFFFE0ULL);
+    test_urshift_one(0x7FFE8000U, 60, 0x0000000000000007ULL, 0xFFFFFFFFFFFFFFE8ULL);
+    test_urshift_one(0xFFFE8000U, 60, 0x000000000000000FULL, 0xFFFFFFFFFFFFFFE8ULL);
+    test_urshift_one(0x00018000U,  0, 0x0000000000000001ULL, 0x8000000000000000ULL);
+    test_urshift_one(0x80018000U,  0, 0x8000000000000001ULL, 0x8000000000000000ULL);
+    test_urshift_one(0x7FFE0000U,  0, 0x7FFFFFFFFFFFFFFEULL, 0x0000000000000000ULL);
+    test_urshift_one(0xFFFE0000U,  0, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL);
+    test_urshift_one(0x7FFE8000U,  0, 0x7FFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
+    test_urshift_one(0xFFFE8000U,  0, 0xFFFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -219,5 +250,6 @@ int main(int argc, char **argv)
     g_test_add_func("/int128/int128_ge", test_ge);
     g_test_add_func("/int128/int128_gt", test_gt);
     g_test_add_func("/int128/int128_rshift", test_rshift);
+    g_test_add_func("/int128/int128_urshift", test_urshift);
     return g_test_run();
 }
Signature · 1 line
2.25.1
Reviewed-by on v1 2/8
#4 lore raw

Re: [RFC PATCH 2/8] qemu/int128: add int128_urshift

Quoted 10 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Implement an unsigned right shift for Int128 values and add the same tests cases of int128_rshift in the unit test.

---
  include/qemu/int128.h    | 19 +++++++++++++++++++
  tests/unit/test-int128.c | 32 ++++++++++++++++++++++++++++++++
  2 files changed, 51 insertions(+)

r~

#5 lore raw

RFC PATCH 3/8 softfloat: add uint128_to_float128

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Based on parts_uint_to_float, implements uint128_to_float128 to convert an unsigned 128-bit value received through an Int128 argument.

---
 fpu/softfloat.c         | 25 +++++++++++++++++++++++++
 include/fpu/softfloat.h |  2 ++
 2 files changed, 27 insertions(+)
fpu/softfloat.c +25 -0
index 7f524d4377..57445b36e7 100644
@@ -3969,6 +3969,31 @@ float128 uint64_to_float128(uint64_t a, float_status *status)
     return float128_round_pack_canonical(&p, status);
 }
 
+float128 uint128_to_float128(Int128 a, float_status *status)
+{
+    FloatParts128 p = { };
+    int shift;
+
+    if (int128_nz(a)) {
+        p.cls = float_class_normal;
+
+        shift = clz64(int128_gethi(a));
+        if (shift == 64) {
+            shift += clz64(int128_getlo(a));
+        }
+
+        p.exp = 127 - shift;
+        a = int128_lshift(a, shift);
+
+        p.frac_hi = int128_gethi(a);
+        p.frac_lo = int128_getlo(a);
+    } else {
+        p.cls = float_class_zero;
+    }
+
+    return float128_round_pack_canonical(&p, status);
+}
+
 /*
  * Minimum and maximum
  */
include/fpu/softfloat.h +2 -0
index d34b2c44d2..8e026e5610 100644
@@ -95,6 +95,7 @@ typedef enum {
 
 #include "fpu/softfloat-types.h"
 #include "fpu/softfloat-helpers.h"
+#include "qemu/int128.h"
 
 /*----------------------------------------------------------------------------
 | Routine to raise any or all of the software IEC/IEEE floating-point
@@ -183,6 +184,7 @@ floatx80 int64_to_floatx80(int64_t, float_status *status);
 float128 int32_to_float128(int32_t, float_status *status);
 float128 int64_to_float128(int64_t, float_status *status);
 float128 uint64_to_float128(uint64_t, float_status *status);
+float128 uint128_to_float128(Int128, float_status *status);
 
 /*----------------------------------------------------------------------------
 | Software half-precision conversion routines.
Signature · 1 line
2.25.1
Reviewed-by on v1 3/8
#6 lore raw

Re: [RFC PATCH 3/8] softfloat: add uint128_to_float128

Quoted 10 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Based on parts_uint_to_float, implements uint128_to_float128 to convert an unsigned 128-bit value received through an Int128 argument.

---
  fpu/softfloat.c         | 25 +++++++++++++++++++++++++
  include/fpu/softfloat.h |  2 ++
  2 files changed, 27 insertions(+)

r~

#7 lore raw

RFC PATCH 4/8 softfloat: add int128_to_float128

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Based on parts_sint_to_float, implements int128_to_float128 to convert a signed 128-bit value received through an Int128 argument.

---
 fpu/softfloat.c         | 29 +++++++++++++++++++++++++++++
 include/fpu/softfloat.h |  1 +
 2 files changed, 30 insertions(+)
fpu/softfloat.c +29 -0
index 57445b36e7..60b4702945 100644
@@ -3780,6 +3780,35 @@ bfloat16 int16_to_bfloat16(int16_t a, float_status *status)
     return int64_to_bfloat16_scalbn(a, 0, status);
 }
 
+float128 int128_to_float128(Int128 a, float_status *status)
+{
+    FloatParts128 p = { };
+    int shift;
+
+    if (int128_nz(a)) {
+        p.cls = float_class_normal;
+        if (!int128_nonneg(a)) {
+            p.sign = true;
+            a = int128_neg(a);
+        }
+
+        shift = clz64(int128_gethi(a));
+        if (shift == 64) {
+            shift += clz64(int128_getlo(a));
+        }
+
+        p.exp = 127 - shift;
+        a = int128_lshift(a, shift);
+
+        p.frac_hi = int128_gethi(a);
+        p.frac_lo = int128_getlo(a);
+    } else {
+        p.cls = float_class_zero;
+    }
+
+    return float128_round_pack_canonical(&p, status);
+}
+
 float128 int64_to_float128(int64_t a, float_status *status)
 {
     FloatParts128 p;
include/fpu/softfloat.h +1 -0
index 8e026e5610..3994b7235d 100644
@@ -183,6 +183,7 @@ floatx80 int64_to_floatx80(int64_t, float_status *status);
 
 float128 int32_to_float128(int32_t, float_status *status);
 float128 int64_to_float128(int64_t, float_status *status);
+float128 int128_to_float128(Int128, float_status *status);
 float128 uint64_to_float128(uint64_t, float_status *status);
 float128 uint128_to_float128(Int128, float_status *status);
 
Signature · 1 line
2.25.1
Reviewed-by on v1 4/8
#8 lore raw

Re: [RFC PATCH 4/8] softfloat: add int128_to_float128

Quoted 10 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Based on parts_sint_to_float, implements int128_to_float128 to convert a signed 128-bit value received through an Int128 argument.

---
  fpu/softfloat.c         | 29 +++++++++++++++++++++++++++++
  include/fpu/softfloat.h |  1 +
  2 files changed, 30 insertions(+)

r~

#9 lore raw

RFC PATCH 5/8 softfloat: add float128_to_uint128

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Implements float128_to_uint128 based on parts_float_to_uint logic.

---
 fpu/softfloat.c         | 65 +++++++++++++++++++++++++++++++++++++++++
 include/fpu/softfloat.h |  2 ++
 2 files changed, 67 insertions(+)
fpu/softfloat.c +65 -0
index 60b4702945..ce21b64e4f 100644
@@ -3480,6 +3480,61 @@ static uint64_t float128_to_uint64_scalbn(float128 a, FloatRoundMode rmode,
     return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
 }
 
+static Int128 float128_to_uint128_scalbn(float128 a, FloatRoundMode rmode,
+                                         int scale, float_status *s)
+{
+    int flags = 0;
+    Int128 r;
+    FloatParts128 p;
+
+    float128_unpack_canonical(&p, a, s);
+
+    switch (p.cls) {
+    case float_class_snan:
+        flags |= float_flag_invalid_snan;
+        /* fall through */
+    case float_class_qnan:
+        flags |= float_flag_invalid;
+        r = UINT128_MAX;
+        break;
+
+    case float_class_inf:
+        flags = float_flag_invalid | float_flag_invalid_cvti;
+        r = p.sign ? int128_zero() : UINT128_MAX;
+        break;
+
+    case float_class_zero:
+        return int128_zero();
+
+    case float_class_normal:
+        if (parts_round_to_int_normal(&p, rmode, scale, 128 - 2)) {
+            flags = float_flag_inexact;
+            if (p.cls == float_class_zero) {
+                r = int128_zero();
+                break;
+            }
+        }
+
+        if (p.sign) {
+            flags = float_flag_invalid | float_flag_invalid_cvti;
+            r = int128_zero();
+        } else if (p.exp <= 127) {
+            int shift = 127 - p.exp;
+            r = int128_urshift(int128_make128(p.frac_lo, p.frac_hi), shift);
+        } else {
+            flags = float_flag_invalid | float_flag_invalid_cvti;
+            r = UINT128_MAX;
+        }
+        break;
+
+    default:
+        g_assert_not_reached();
+    }
+
+    float_raise(flags, s);
+    return r;
+}
+
 uint8_t float16_to_uint8(float16 a, float_status *s)
 {
     return float16_to_uint8_scalbn(a, s->float_rounding_mode, 0, s);
@@ -3540,6 +3595,11 @@ uint64_t float128_to_uint64(float128 a, float_status *s)
     return float128_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
 }
 
+Int128 float128_to_uint128(float128 a, float_status *s)
+{
+    return float128_to_uint128_scalbn(a, s->float_rounding_mode, 0, s);
+}
+
 uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *s)
 {
     return float16_to_uint16_scalbn(a, float_round_to_zero, 0, s);
@@ -3595,6 +3655,11 @@ uint64_t float128_to_uint64_round_to_zero(float128 a, float_status *s)
     return float128_to_uint64_scalbn(a, float_round_to_zero, 0, s);
 }
 
+Int128 float128_to_uint128_round_to_zero(float128 a, float_status *s)
+{
+    return float128_to_uint128_scalbn(a, float_round_to_zero, 0, s);
+}
+
 uint16_t bfloat16_to_uint16(bfloat16 a, float_status *s)
 {
     return bfloat16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
include/fpu/softfloat.h +2 -0
index 3994b7235d..6cfe9ee474 100644
@@ -1206,7 +1206,9 @@ int32_t float128_to_int32_round_to_zero(float128, float_status *status);
 int64_t float128_to_int64(float128, float_status *status);
 int64_t float128_to_int64_round_to_zero(float128, float_status *status);
 uint64_t float128_to_uint64(float128, float_status *status);
+Int128 float128_to_uint128(float128, float_status *status);
 uint64_t float128_to_uint64_round_to_zero(float128, float_status *status);
+Int128 float128_to_uint128_round_to_zero(float128, float_status *status);
 uint32_t float128_to_uint32(float128, float_status *status);
 uint32_t float128_to_uint32_round_to_zero(float128, float_status *status);
 float32 float128_to_float32(float128, float_status *status);
Signature · 1 line
2.25.1
Reviewed-by on v1 5/8
#10 lore raw

Re: [RFC PATCH 5/8] softfloat: add float128_to_uint128

Quoted 9 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Implements float128_to_uint128 based on parts_float_to_uint logic.

---
  fpu/softfloat.c         | 65 +++++++++++++++++++++++++++++++++++++++++
  include/fpu/softfloat.h |  2 ++
  2 files changed, 67 insertions(+)

r~

#11 lore raw

RFC PATCH 6/8 softfloat: add float128_to_int128

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Implements float128_to_int128 based on parts_float_to_int logic.

---
 fpu/softfloat.c         | 64 +++++++++++++++++++++++++++++++++++++++++
 include/fpu/softfloat.h |  2 ++
 include/qemu/int128.h   |  2 ++
 3 files changed, 68 insertions(+)
fpu/softfloat.c +64 -0
index ce21b64e4f..5e2cf20448 100644
@@ -3154,6 +3154,60 @@ static int64_t float128_to_int64_scalbn(float128 a, FloatRoundMode rmode,
     return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
 }
 
+static Int128 float128_to_int128_scalbn(float128 a, FloatRoundMode rmode,
+                                        int scale, float_status *s)
+{
+    int flags = 0;
+    Int128 r;
+    FloatParts128 p;
+
+    float128_unpack_canonical(&p, a, s);
+
+    switch (p.cls) {
+    case float_class_snan:
+        flags |= float_flag_invalid_snan;
+        /* fall through */
+    case float_class_qnan:
+        flags |= float_flag_invalid;
+        r = UINT128_MAX;
+        break;
+
+    case float_class_inf:
+        flags = float_flag_invalid | float_flag_invalid_cvti;
+        r = p.sign ? INT128_MIN : INT128_MAX;
+        break;
+
+    case float_class_zero:
+        return int128_zero();
+
+    case float_class_normal:
+        if (parts_round_to_int_normal(&p, rmode, scale, 128 - 2)) {
+            flags = float_flag_inexact;
+        }
+
+        if (p.exp < 127) {
+            int shift = 127 - p.exp;
+            r = int128_urshift(int128_make128(p.frac_lo, p.frac_hi), shift);
+            if (p.sign) {
+                r = int128_neg(r);
+            }
+        } else if (p.exp == 127 && p.sign && p.frac_lo == 0 &&
+                   p.frac_hi == DECOMPOSED_IMPLICIT_BIT) {
+            r = INT128_MIN;
+        } else {
+            flags = float_flag_invalid | float_flag_invalid_cvti;
+            r = p.sign ? INT128_MIN : INT128_MAX;
+        }
+        break;
+
+    default:
+        g_assert_not_reached();
+    }
+
+    float_raise(flags, s);
+    return r;
+}
+
 static int32_t floatx80_to_int32_scalbn(floatx80 a, FloatRoundMode rmode,
                                         int scale, float_status *s)
 {
@@ -3236,6 +3290,11 @@ int64_t float128_to_int64(float128 a, float_status *s)
     return float128_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
 }
 
+Int128 float128_to_int128(float128 a, float_status *s)
+{
+    return float128_to_int128_scalbn(a, s->float_rounding_mode, 0, s);
+}
+
 int32_t floatx80_to_int32(floatx80 a, float_status *s)
 {
     return floatx80_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
@@ -3301,6 +3360,11 @@ int64_t float128_to_int64_round_to_zero(float128 a, float_status *s)
     return float128_to_int64_scalbn(a, float_round_to_zero, 0, s);
 }
 
+Int128 float128_to_int128_round_to_zero(float128 a, float_status *s)
+{
+    return float128_to_int128_scalbn(a, float_round_to_zero, 0, s);
+}
+
 int32_t floatx80_to_int32_round_to_zero(floatx80 a, float_status *s)
 {
     return floatx80_to_int32_scalbn(a, float_round_to_zero, 0, s);
include/fpu/softfloat.h +2 -0
index 6cfe9ee474..3dcf20e3a2 100644
@@ -1204,7 +1204,9 @@ floatx80 floatx80_default_nan(float_status *status);
 int32_t float128_to_int32(float128, float_status *status);
 int32_t float128_to_int32_round_to_zero(float128, float_status *status);
 int64_t float128_to_int64(float128, float_status *status);
+Int128 float128_to_int128(float128, float_status *status);
 int64_t float128_to_int64_round_to_zero(float128, float_status *status);
+Int128 float128_to_int128_round_to_zero(float128, float_status *status);
 uint64_t float128_to_uint64(float128, float_status *status);
 Int128 float128_to_uint128(float128, float_status *status);
 uint64_t float128_to_uint64_round_to_zero(float128, float_status *status);
include/qemu/int128.h +2 -0
index ca32b0b276..17ea5f1372 100644
@@ -431,5 +431,7 @@ static inline void bswap128s(Int128 *s)
 }
 
 #define UINT128_MAX int128_make128(~0LL, ~0LL)
+#define INT128_MAX int128_make128(UINT64_MAX, INT64_MAX)
+#define INT128_MIN int128_make128(0, INT64_MIN)
 
 #endif /* INT128_H */
Signature · 1 line
2.25.1
Reviewed-by on v1 6/8
#12 lore raw

Re: [RFC PATCH 6/8] softfloat: add float128_to_int128

Quoted 10 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Implements float128_to_int128 based on parts_float_to_int logic.

---
  fpu/softfloat.c         | 64 +++++++++++++++++++++++++++++++++++++++++
  include/fpu/softfloat.h |  2 ++
  include/qemu/int128.h   |  2 ++
  3 files changed, 68 insertions(+)

r~

#13 lore raw

RFC PATCH 7/8 target/ppc: implement xscv[su]qqp

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Implement the following PowerISA v3.1 instructions:
xscvsqqp: VSX Scalar Convert with round Signed Quadword to
Quad-Precision
xscvuqqp: VSX Scalar Convert with round Unsigned Quadword to
Quad-Precision format

---
 target/ppc/fpu_helper.c             | 12 ++++++++++++
 target/ppc/helper.h                 |  2 ++
 target/ppc/insn32.decode            |  5 +++++
 target/ppc/translate/vsx-impl.c.inc | 20 ++++++++++++++++++++
 4 files changed, 39 insertions(+)
target/ppc/fpu_helper.c +12 -0
index 7e8be99cc0..97892afa95 100644
@@ -3058,6 +3058,18 @@ void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)        \
 VSX_CVT_INT_TO_FP2(xvcvsxdsp, int64, float32)
 VSX_CVT_INT_TO_FP2(xvcvuxdsp, uint64, float32)
 
+#define VSX_CVT_INT128_TO_FP(op, tp)                            \
+void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)\
+{                                                               \
+    helper_reset_fpstatus(env);                                 \
+    xt->f128 = tp##_to_float128(xb->s128, &env->fp_status);     \
+    helper_compute_fprf_float128(env, xt->f128);                \
+    do_float_check_status(env, GETPC());                        \
+}
+
+VSX_CVT_INT128_TO_FP(XSCVUQQP, uint128);
+VSX_CVT_INT128_TO_FP(XSCVSQQP, int128);
+
 /*
  * VSX_CVT_INT_TO_FP_VECTOR - VSX integer to floating point conversion
  *   op    - instruction mnemonic
target/ppc/helper.h +2 -0
index 57da11c77e..7df0c01819 100644
@@ -388,6 +388,8 @@ DEF_HELPER_4(xscvqpsdz, void, env, i32, vsr, vsr)
 DEF_HELPER_4(xscvqpswz, void, env, i32, vsr, vsr)
 DEF_HELPER_4(xscvqpudz, void, env, i32, vsr, vsr)
 DEF_HELPER_4(xscvqpuwz, void, env, i32, vsr, vsr)
+DEF_HELPER_3(XSCVUQQP, void, env, vsr, vsr)
+DEF_HELPER_3(XSCVSQQP, void, env, vsr, vsr)
 DEF_HELPER_3(xscvhpdp, void, env, vsr, vsr)
 DEF_HELPER_4(xscvsdqp, void, env, i32, vsr, vsr)
 DEF_HELPER_3(xscvspdp, void, env, vsr, vsr)
target/ppc/insn32.decode +5 -0
index ac2d3da9a7..6fb568c1fe 100644
@@ -91,6 +91,9 @@
 
 @X_tp_a_bp_rc   ...... ....0 ra:5 ....0 .......... rc:1         &X_rc rt=%x_frtp rb=%x_frbp
 
+&X_tb           rt rb
+@X_tb           ...... rt:5 ..... rb:5 .......... .             &X_tb
+
 &X_tb_rc        rt rb rc:bool
 @X_tb_rc        ...... rt:5 ..... rb:5 .......... rc:1          &X_tb_rc
 
@@ -692,6 +695,8 @@ XSCMPGTQP       111111 ..... ..... ..... 0011100100 -   @X
 ## VSX Binary Floating-Point Convert Instructions
 
 XSCVQPDP        111111 ..... 10100 ..... 1101000100 .   @X_tb_rc
+XSCVUQQP        111111 ..... 00011 ..... 1101000100 -   @X_tb
+XSCVSQQP        111111 ..... 01011 ..... 1101000100 -   @X_tb
 XVCVBF16SPN     111100 ..... 10000 ..... 111011011 ..   @XX2
 XVCVSPBF16      111100 ..... 10001 ..... 111011011 ..   @XX2
 
target/ppc/translate/vsx-impl.c.inc +20 -0
index d1f6333314..a305579ecc 100644
@@ -838,6 +838,26 @@ static bool trans_XSCVQPDP(DisasContext *ctx, arg_X_tb_rc *a)
     return true;
 }
 
+static bool do_helper_env_X_tb(DisasContext *ctx, arg_X_tb *a,
+                               void (*gen_helper)(TCGv_ptr, TCGv_ptr, TCGv_ptr))
+{
+    TCGv_ptr xt, xb;
+
+    REQUIRE_INSNS_FLAGS2(ctx, ISA310);
+    REQUIRE_VSX(ctx);
+
+    xt = gen_avr_ptr(a->rt);
+    xb = gen_avr_ptr(a->rb);
+    gen_helper(cpu_env, xt, xb);
+    tcg_temp_free_ptr(xt);
+    tcg_temp_free_ptr(xb);
+
+    return true;
+}
+
+TRANS(XSCVUQQP, do_helper_env_X_tb, gen_helper_XSCVUQQP)
+TRANS(XSCVSQQP, do_helper_env_X_tb, gen_helper_XSCVSQQP)
+
 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
 static void gen_##name(DisasContext *ctx)                                     \
 {                                                                             \
Signature · 1 line
2.25.1
Reviewed-by on v1 7/8
#14 lore raw

Re: [RFC PATCH 7/8] target/ppc: implement xscv[su]qqp

Quoted 8 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Implement the following PowerISA v3.1 instructions:
xscvsqqp: VSX Scalar Convert with round Signed Quadword to
Quad-Precision
xscvuqqp: VSX Scalar Convert with round Unsigned Quadword to
Quad-Precision format

Quoted 7 lines
---
  target/ppc/fpu_helper.c             | 12 ++++++++++++
  target/ppc/helper.h                 |  2 ++
  target/ppc/insn32.decode            |  5 +++++
  target/ppc/translate/vsx-impl.c.inc | 20 ++++++++++++++++++++
  4 files changed, 39 insertions(+)

r~

#15 lore raw

RFC PATCH 8/8 target/ppc: implement xscvqp[su]qz

In reply to [email protected]

From: Matheus Ferst <[email protected]>

Implement the following PowerISA v3.1 instructions:
xscvqpsqz: VSX Scalar Convert with round to zero Quad-Precision to
Signed Quadword
xscvqpuqz: VSX Scalar Convert with round to zero Quad-Precision to
Unsigned Quadword

---
 target/ppc/fpu_helper.c             | 21 +++++++++++++++++++++
 target/ppc/helper.h                 |  2 ++
 target/ppc/insn32.decode            |  2 ++
 target/ppc/translate/vsx-impl.c.inc |  2 ++
 4 files changed, 27 insertions(+)
target/ppc/fpu_helper.c +21 -0
index 97892afa95..99281cc37a 100644
@@ -2925,6 +2925,27 @@ VSX_CVT_FP_TO_INT(xvcvspsxws, 4, float32, int32, VsrW(i), VsrW(i), 0x80000000U)
 VSX_CVT_FP_TO_INT(xvcvspuxds, 2, float32, uint64, VsrW(2 * i), VsrD(i), 0ULL)
 VSX_CVT_FP_TO_INT(xvcvspuxws, 4, float32, uint32, VsrW(i), VsrW(i), 0U)
 
+#define VSX_CVT_FP_TO_INT128(op, tp, rnan)                                     \
+void helper_##op(CPUPPCState *env, ppc_vsr_t *xt, ppc_vsr_t *xb)               \
+{                                                                              \
+    ppc_vsr_t t;                                                               \
+    int flags;                                                                 \
+                                                                               \
+    helper_reset_fpstatus(env);                                                \
+    t.s128 = float128_to_##tp##_round_to_zero(xb->f128, &env->fp_status);      \
+    flags = get_float_exception_flags(&env->fp_status);                        \
+    if (unlikely(flags & float_flag_invalid)) {                                \
+        t.VsrD(0) = float_invalid_cvt(env, flags, t.VsrD(0), rnan, 0, GETPC());\
+        t.VsrD(1) = -(t.VsrD(0) & 1);                                          \
+    }                                                                          \
+                                                                               \
+    *xt = t;                                                                   \
+    do_float_check_status(env, GETPC());                                       \
+}
+
+VSX_CVT_FP_TO_INT128(XSCVQPUQZ, uint128, 0)
+VSX_CVT_FP_TO_INT128(XSCVQPSQZ, int128, 0x8000000000000000ULL);
+
 /*
  * Likewise, except that the result is duplicated into both subwords.
  * Power ISA v3.1 has Programming Notes for these insns:
target/ppc/helper.h +2 -0
index 7df0c01819..aa6773c4a5 100644
@@ -388,6 +388,8 @@ DEF_HELPER_4(xscvqpsdz, void, env, i32, vsr, vsr)
 DEF_HELPER_4(xscvqpswz, void, env, i32, vsr, vsr)
 DEF_HELPER_4(xscvqpudz, void, env, i32, vsr, vsr)
 DEF_HELPER_4(xscvqpuwz, void, env, i32, vsr, vsr)
+DEF_HELPER_3(XSCVQPUQZ, void, env, vsr, vsr)
+DEF_HELPER_3(XSCVQPSQZ, void, env, vsr, vsr)
 DEF_HELPER_3(XSCVUQQP, void, env, vsr, vsr)
 DEF_HELPER_3(XSCVSQQP, void, env, vsr, vsr)
 DEF_HELPER_3(xscvhpdp, void, env, vsr, vsr)
target/ppc/insn32.decode +2 -0
index 6fb568c1fe..39372fe673 100644
@@ -695,6 +695,8 @@ XSCMPGTQP       111111 ..... ..... ..... 0011100100 -   @X
 ## VSX Binary Floating-Point Convert Instructions
 
 XSCVQPDP        111111 ..... 10100 ..... 1101000100 .   @X_tb_rc
+XSCVQPUQZ       111111 ..... 00000 ..... 1101000100 -   @X_tb
+XSCVQPSQZ       111111 ..... 01000 ..... 1101000100 -   @X_tb
 XSCVUQQP        111111 ..... 00011 ..... 1101000100 -   @X_tb
 XSCVSQQP        111111 ..... 01011 ..... 1101000100 -   @X_tb
 XVCVBF16SPN     111100 ..... 10000 ..... 111011011 ..   @XX2
target/ppc/translate/vsx-impl.c.inc +2 -0
index a305579ecc..6e8a3342dc 100644
@@ -857,6 +857,8 @@ static bool do_helper_env_X_tb(DisasContext *ctx, arg_X_tb *a,
 
 TRANS(XSCVUQQP, do_helper_env_X_tb, gen_helper_XSCVUQQP)
 TRANS(XSCVSQQP, do_helper_env_X_tb, gen_helper_XSCVSQQP)
+TRANS(XSCVQPUQZ, do_helper_env_X_tb, gen_helper_XSCVQPUQZ)
+TRANS(XSCVQPSQZ, do_helper_env_X_tb, gen_helper_XSCVQPSQZ)
 
 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
 static void gen_##name(DisasContext *ctx)                                     \
Signature · 1 line
2.25.1
Reviewed-by on v1 8/8
#16 lore raw

Re: [RFC PATCH 8/8] target/ppc: implement xscvqp[su]qz

Quoted 8 lines · On 3/30/22 11:59, [email protected] wrote:

From: Matheus Ferst<[email protected]>

Implement the following PowerISA v3.1 instructions:
xscvqpsqz: VSX Scalar Convert with round to zero Quad-Precision to
Signed Quadword
xscvqpuqz: VSX Scalar Convert with round to zero Quad-Precision to
Unsigned Quadword

Quoted 7 lines
---
  target/ppc/fpu_helper.c             | 21 +++++++++++++++++++++
  target/ppc/helper.h                 |  2 ++
  target/ppc/insn32.decode            |  2 ++
  target/ppc/translate/vsx-impl.c.inc |  2 ++
  4 files changed, 27 insertions(+)

r~

#17 lore raw
In reply to [email protected]

All patches but patch 01 queued in gitlab.com/danielhb/qemu/tree/ppc-next.

Thanks,

Daniel

Quoted 37 lines · On 3/30/22 14:59, [email protected] wrote:

From: Matheus Ferst <[email protected]>

This RFC is an alternative to [1] using Int128 types to implement the 128-bit integer conversion routines in softfloat required by the xscv[su]qqp and xscvqp[su]qz instructions of PowerISA v3.1.

Some improvements to int128.h are made in patches 1 and 2. Patches 3-6 implement the conversion routines, and patches 7 and 8 implement the new instructions.

RFC: Int128 vs. pair of 64-bit values.
- Returning unsigned values through Int128 is not ideal, but creating an "UInt128" just for this case seems excessive.
- OTOH, there are fewer cases to handle, especially in float->int.

[1] https://lists.gnu.org/archive/html/qemu-ppc/2022-03/msg00520.html

Matheus Ferst (8):

   qemu/int128: avoid undefined behavior in int128_lshift
   qemu/int128: add int128_urshift
   softfloat: add uint128_to_float128
   softfloat: add int128_to_float128
   softfloat: add float128_to_uint128
   softfloat: add float128_to_int128
   target/ppc: implement xscv[su]qqp
   target/ppc: implement xscvqp[su]qz
  fpu/softfloat.c                     | 183 ++++++++++++++++++++++++++++
  include/fpu/softfloat.h             |   7 ++
  include/qemu/int128.h               |  25 +++-
  target/ppc/fpu_helper.c             |  33 +++++
  target/ppc/helper.h                 |   4 +
  target/ppc/insn32.decode            |   7 ++
  target/ppc/translate/vsx-impl.c.inc |  22 ++++
  tests/unit/test-int128.c            |  32 +++++
  8 files changed, 311 insertions(+), 2 deletions(-)