Skip to contentSkip to conversation

target/s390x: Implement DIVIDE TO INTEGER

PATCH Superseded Ilya Leoshkevich 3 participants 10 messages Jan 21 – Jan 22

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover target/s390x: Implement DIVIDE TO INTEGER

Hi,

This series implements DIVIDE TO INTEGER instruction, which is required to run LuaJIT.

Patch 1 is a debugging helper. Patch 2 is the implementation.

Since the instruction is quite complex, I've extensively tested it using a libFuzzer-based harness [1] that compares emulation with native execution at ~15k exec/s. The tests (patch 3) use data generated this way.

Best regards,
Ilya

[1] https://github.com/iii-i/qemu/commits/iii/wip/fuzz-tcg-v1/

Ilya Leoshkevich (3):
target/s390x: Dump Floating-Point-Control Register target/s390x: Implement DIVIDE TO INTEGER tests/tcg/s390x: Test DIVIDE TO INTEGER

 target/s390x/cpu-dump.c             |   1 +
 target/s390x/helper.h               |   2 +
 target/s390x/tcg/fpu_helper.c       | 199 +++++++++++++++++++++++++
 target/s390x/tcg/insn-data.h.inc    |   5 +-
 target/s390x/tcg/translate.c        |  26 ++++
 tests/tcg/s390x/Makefile.target     |   3 +
 tests/tcg/s390x/divide-to-integer.c | 215 ++++++++++++++++++++++++++++
 7 files changed, 450 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/s390x/divide-to-integer.c
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/3 target/s390x: Dump Floating-Point-Control Register

Knowing the value of this register is very useful for debugging floating-point code.

---
 target/s390x/cpu-dump.c | 1 +
 1 file changed, 1 insertion(+)
target/s390x/cpu-dump.c +1 -0
index 869d3a4ad54..5b852928031 100644
@@ -63,6 +63,7 @@ void s390_cpu_dump_state(CPUState *cs, FILE *f, int flags)
                              (i % 4) == 3 ? '\n' : ' ');
             }
         }
+        qemu_fprintf(f, "FPC=%08" PRIx32 "\n", env->fpc);
     }
 
 #ifndef CONFIG_USER_ONLY
Signature · 1 line
2.52.0
#3 lore raw

PATCH 2/3 target/s390x: Implement DIVIDE TO INTEGER

In reply to Ilya Leoshkevich

DIVIDE TO INTEGER computes floating point remainder and is used by LuaJIT, so add it to QEMU.

The instruction comes in two flavors: for floats and doubles, which are very similar. Since it's also quite complex, copy-pasting the implementation would result in barely maintainable code. Mitigate that using macros. An alternative would be an .inc file, but this looks like an overkill.

---
 target/s390x/helper.h            |   2 +
 target/s390x/tcg/fpu_helper.c    | 199 +++++++++++++++++++++++++++++++
 target/s390x/tcg/insn-data.h.inc |   5 +-
 target/s390x/tcg/translate.c     |  26 ++++
 4 files changed, 231 insertions(+), 1 deletion(-)
target/s390x/helper.h +2 -0
index 1a8a76abb98..f2b24c65a88 100644
@@ -46,6 +46,8 @@ DEF_HELPER_FLAGS_3(sxb, TCG_CALL_NO_WG, i128, env, i128, i128)
 DEF_HELPER_FLAGS_3(deb, TCG_CALL_NO_WG, i64, env, i64, i64)
 DEF_HELPER_FLAGS_3(ddb, TCG_CALL_NO_WG, i64, env, i64, i64)
 DEF_HELPER_FLAGS_3(dxb, TCG_CALL_NO_WG, i128, env, i128, i128)
+DEF_HELPER_5(didb, void, env, i32, i32, i32, i32)
+DEF_HELPER_5(dieb, void, env, i32, i32, i32, i32)
 DEF_HELPER_FLAGS_3(meeb, TCG_CALL_NO_WG, i64, env, i64, i64)
 DEF_HELPER_FLAGS_3(mdeb, TCG_CALL_NO_WG, i64, env, i64, i64)
 DEF_HELPER_FLAGS_3(mdb, TCG_CALL_NO_WG, i64, env, i64, i64)
target/s390x/tcg/fpu_helper.c +199 -0

205 lines not on this page · raw message

target/s390x/tcg/insn-data.h.inc +4 -1
index baaafe922e9..0d5392eac54 100644
@@ -9,7 +9,7 @@
  *  OPC  = (op << 8) | op2 where op is the major, op2 the minor opcode
  *  NAME = name of the opcode, used internally
  *  FMT  = format of the opcode (defined in insn-format.h.inc)
- *  FAC  = facility the opcode is available in (defined in DisasFacility)
+ *  FAC  = facility the opcode is available in (define in translate.c)
  *  I1   = func in1_xx fills o->in1
  *  I2   = func in2_xx fills o->in2
  *  P    = func prep_xx initializes o->*out*
@@ -361,6 +361,9 @@
     C(0xb91d, DSGFR,   RRE,   Z,   r1p1, r2_32s, r1_P, 0, divs64, 0)
     C(0xe30d, DSG,     RXY_a, Z,   r1p1, m2_64, r1_P, 0, divs64, 0)
     C(0xe31d, DSGF,    RXY_a, Z,   r1p1, m2_32s, r1_P, 0, divs64, 0)
+/* DIVIDE TO INTEGER */
+    D(0xb35b, DIDBR,   RRF_b, Z,   0, 0, 0, 0, dib, 0, 64)
+    D(0xb353, DIEBR,   RRF_b, Z,   0, 0, 0, 0, dib, 0, 32)
 
 /* EXCLUSIVE OR */
     C(0x1700, XR,      RR_a,  Z,   r1, r2, new, r1_32, xor, nz32)
target/s390x/tcg/translate.c +26 -0
index 540c5a569c0..a3b753bc829 100644
@@ -2283,6 +2283,32 @@ static DisasJumpType op_dxb(DisasContext *s, DisasOps *o)
     return DISAS_NEXT;
 }
 
+static DisasJumpType op_dib(DisasContext *s, DisasOps *o)
+{
+    const bool fpe = s390_has_feat(S390_FEAT_FLOATING_POINT_EXT);
+    uint8_t m4 = get_field(s, m4);
+
+    if (get_field(s, r1) == get_field(s, r2) ||
+        get_field(s, r1) == get_field(s, r3) ||
+        get_field(s, r2) == get_field(s, r3)) {
+        gen_program_exception(s, PGM_SPECIFICATION);
+        return DISAS_NORETURN;
+    }
+
+    if (m4 == 2 || (!fpe && m4 == 3) || m4 > 7) {
+        gen_program_exception(s, PGM_SPECIFICATION);
+        return DISAS_NORETURN;
+    }
+
+    (s->insn->data == 32 ? gen_helper_dieb : gen_helper_didb)(
+        tcg_env, tcg_constant_i32(get_field(s, r1)),
+        tcg_constant_i32(get_field(s, r2)),
+        tcg_constant_i32(get_field(s, r3)), tcg_constant_i32(m4));
+    set_cc_static(s);
+
+    return DISAS_NEXT;
+}
+
 static DisasJumpType op_ear(DisasContext *s, DisasOps *o)
 {
     int r2 = get_field(s, r2);
Signature · 1 line
2.52.0
#4 lore raw

target/s390x/tcg/fpu_helper.c Int128 HELPER(dxb)(CPUS390XState *env, Int128 a, Int128 b) · line 321

Quoted 4 lines · Ilya Leoshkevich
target/s390x/tcg/fpu_helper.c +4 -0
+static bool float128_is_int(float128 x)
+{
+    return extract64(x.high, 0, 48) == 0 && x.low == 0;
+}

This isn't testing for integer, it's testing for 1.0eNN, i.e. a power of two.

target/s390x/tcg/fpu_helper.c Int128 HELPER(dxb)(CPUS390XState *env, Int128 a, Int128 b) · line 392

Quoted 8 lines
target/s390x/tcg/fpu_helper.c +8 -0
+        /* Compute precise quotient */                                         \
+        a128 = floatN ## _to_float128(a, &env->fpu_status);                    \
+        b128 = floatN ## _to_float128(b, &env->fpu_status);                    \
+        q128 = float128_div(a128, b128, &env->fpu_status);                     \
+                                                                               \
+        /* Final or partial case? */                                           \
+        is_q128_smallish = float128_get_exp(q128) < p;                         \
+        is_final = is_q128_smallish || float128_is_int(q128);                  \

The language from the manual,

# If the precise quotient is not an integer and the two

# integers closest to this precise quotient cannot both
# be represented exactly in the precision of the quotient ...

does not appear to be what you are computing here. Certainly none of this relates to "precision of the quotient".

I would imagine that all of this would be easier to accomplish if you did this in fpu/ with FloatParts instead of continually swapping in and out of float128.

r~

#5 lore raw
Quoted 8 lines · Richard Henderson

target/s390x/tcg/fpu_helper.c Int128 HELPER(dxb)(CPUS390XState *env, Int128 a, Int128 b) · line 321

Quoted 4 lines · Ilya Leoshkevich
target/s390x/tcg/fpu_helper.c +4 -0
+static bool float128_is_int(float128 x)
+{
+    return extract64(x.high, 0, 48) == 0 && x.low == 0;
+}

This isn't testing for integer, it's testing for 1.0eNN, i.e. a power of two.

Whoops. Not sure what I was thinking here.

Quoted 16 lines
Quoted 7 lines

+        /* Compute precise quotient
*/                                         \
+        a128 = floatN ## _to_float128(a,
&env->fpu_status);                    \
+        b128 = floatN ## _to_float128(b,
&env->fpu_status);                    \
+        q128 = float128_div(a128, b128,

Quoted 8 lines

&env->fpu_status);                     \
+ \
+        /* Final or partial case?
*/                                           \
+        is_q128_smallish = float128_get_exp(q128) < p;                         \
+        is_final = is_q128_smallish || float128_is_int(q128);                  \

Quoted 8 lines

The language from the manual,

# If the precise quotient is not an integer and the two

# integers closest to this precise quotient cannot both
# be represented exactly in the precision of the quotient ...

does not appear to be what you are computing here. Certainly none of this relates to "precision of the quotient".

I was rather following the tables, I think they are more precise w.r.t. what needs to be checked.

float128_is_int(q128) was supposed to replace the r=0 check, but it's probably unnecessary here altogether, because if the division result is precise, it doesn't matter which way we round. And I'm explicitly checking for r=0 in other places.

Quoted 1 line

I would imagine that all of this would be easier to accomplish if you did this in fpu/ with FloatParts instead of continually swapping in and out of float128.

That sounds good, I think I will also be able to reuse pick_nan and simplify exponent manipulations then. I will give it a try.

Quoted 1 line

r~

#6 lore raw

PATCH 3/3 tests/tcg/s390x: Test DIVIDE TO INTEGER

In reply to Ilya Leoshkevich

Add a test to prevent regressions. Data is generated using a libFuzzer-based fuzzer and hopefully covers all the important corner cases.

---
 tests/tcg/s390x/Makefile.target     |   3 +
 tests/tcg/s390x/divide-to-integer.c | 215 ++++++++++++++++++++++++++++
 2 files changed, 218 insertions(+)
 create mode 100644 tests/tcg/s390x/divide-to-integer.c
tests/tcg/s390x/Makefile.target +3 -0
index da5fe71a407..d5ec01d04fd 100644
@@ -49,14 +49,17 @@ TESTS+=cvd
 TESTS+=cvb
 TESTS+=ts
 TESTS+=ex-smc
+TESTS+=divide-to-integer
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
 
 rxsbg: CFLAGS+=-O2
+divide-to-integer: CFLAGS+=-O2
 
 cgebra: LDFLAGS+=-lm
 clgebr: LDFLAGS+=-lm
+divide-to-integer: LDFLAGS+=-lm
 
 include $(S390X_SRC)/pgm-specification.mak
 $(PGM_SPECIFICATION_TESTS): pgm-specification-user.o
tests/tcg/s390x/divide-to-integer.c new file +215 -0

215 lines not on this page · raw message

Signature · 1 line
2.52.0
#7 lore raw

Re: [PATCH 3/3] tests/tcg/s390x: Test DIVIDE TO INTEGER

Quoted 16 lines · Ilya Leoshkevich

Add a test to prevent regressions. Data is generated using a libFuzzer-based fuzzer and hopefully covers all the important corner cases.

---
 tests/tcg/s390x/Makefile.target     |   3 +
 tests/tcg/s390x/divide-to-integer.c | 215 ++++++++++++++++++++++++++++
 2 files changed, 218 insertions(+)
 create mode 100644 tests/tcg/s390x/divide-to-integer.c
tests/tcg/s390x/Makefile.target +0 -0
index da5fe71a407..d5ec01d04fd 100644

tests/tcg/s390x/Makefile.target TESTS+=cvd · line 58

Quoted 10 lines
tests/tcg/s390x/Makefile.target +2 -0
@@ -49,14 +49,17 @@ TESTS+=cvd
 TESTS+=cvb
 TESTS+=ts
 TESTS+=ex-smc
+TESTS+=divide-to-integer
 
 cdsg: CFLAGS+=-pthread
 cdsg: LDFLAGS+=-pthread
 
 rxsbg: CFLAGS+=-O2
+divide-to-integer: CFLAGS+=-O2

As we generally compile -O0 to make life easier for people debugging behaviour via gdbstub could we have an explanation of why -O2 is needed here? Is it the same reason as rxsbg?

<snip>

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#8 lore raw

Re: [PATCH 3/3] tests/tcg/s390x: Test DIVIDE TO INTEGER

Quoted 26 lines · Alex Bennée
Quoted 24 lines · Ilya Leoshkevich

Add a test to prevent regressions. Data is generated using a libFuzzer-based fuzzer and hopefully covers all the important corner cases.

---
  tests/tcg/s390x/Makefile.target     |   3 +
  tests/tcg/s390x/divide-to-integer.c | 215 ++++++++++++++++++++++++++++
  2 files changed, 218 insertions(+)

create mode 100644 tests/tcg/s390x/divide-to-integer.c

tests/tcg/s390x/Makefile.target +1 -0
index da5fe71a407..d5ec01d04fd 100644
@@ -49,14 +49,17 @@ TESTS+=cvd
  TESTS+=cvb
  TESTS+=ts
  TESTS+=ex-smc
+TESTS+=divide-to-integer
 
  cdsg: CFLAGS+=-pthread
  cdsg: LDFLAGS+=-pthread
 
Quoted 7 lines

tests/tcg/s390x/Makefile.target TESTS+=cvd · line 58

Quoted 2 lines
tests/tcg/s390x/Makefile.target +1 -0
  rxsbg: CFLAGS+=-O2
+divide-to-integer: CFLAGS+=-O2

As we generally compile -O0 to make life easier for people debugging behaviour via gdbstub could we have an explanation of why -O2 is needed here? Is it the same reason as rxsbg?

<snip>

Yes, this is because inlining is mandatory for the mask argument, and -O0 does not honor always_inline.

Acked-by
#9 lore raw

Re: [PATCH 3/3] tests/tcg/s390x: Test DIVIDE TO INTEGER

Quoted 31 lines · Ilya Leoshkevich
Quoted 30 lines · Alex Bennée
Quoted 16 lines · Ilya Leoshkevich

Add a test to prevent regressions. Data is generated using a libFuzzer-based fuzzer and hopefully covers all the important corner cases.

---
  tests/tcg/s390x/Makefile.target     |   3 +
  tests/tcg/s390x/divide-to-integer.c | 215 ++++++++++++++++++++++++++++
  2 files changed, 218 insertions(+)

create mode 100644 tests/tcg/s390x/divide-to-integer.c

tests/tcg/s390x/Makefile.target +0 -0
index da5fe71a407..d5ec01d04fd 100644

tests/tcg/s390x/Makefile.target TESTS+=cvd · line 56

Quoted 8 lines
tests/tcg/s390x/Makefile.target +2 -0
@@ -49,14 +49,17 @@ TESTS+=cvd
  TESTS+=cvb
  TESTS+=ts
  TESTS+=ex-smc
+TESTS+=divide-to-integer
    cdsg: CFLAGS+=-pthread
  cdsg: LDFLAGS+=-pthread
    rxsbg: CFLAGS+=-O2
+divide-to-integer: CFLAGS+=-O2

As we generally compile -O0 to make life easier for people debugging behaviour via gdbstub could we have an explanation of why -O2 is needed here? Is it the same reason as rxsbg?

Quoted 4 lines
Quoted 1 line

<snip>

Yes, this is because inlining is mandatory for the mask argument, and -O0 does not honor always_inline.

Could we add a comment to that effect in the Makefile. Otherwise looks fine to me:

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro