All of lore.kernel.org
 help / color / mirror / Atom feed
From: Helge Deller <deller@kernel.org>
To: qemu-devel@nongnu.org
Cc: "Kirill A . Korinsky" <kirill@korins.ky>, Helge Deller <deller@gmx.de>
Subject: [PULL for 11.2 1/4] target/hppa: Emulate FREM
Date: Wed,  2 Dec 2026 00:21:51 +0100	[thread overview]
Message-ID: <20261201232154.3178-2-deller@kernel.org> (raw)
In-Reply-To: <20261201232154.3178-1-deller@kernel.org>

From: "Kirill A. Korinsky" <kirill@korins.ky>

OpenBSD/hppa's libm remainder tests terminate with SIGILL on FREM
because QEMU's HPPA decoder omits class 3 subopcode 4 under both
major opcodes, 0x0c and 0x0e, sending both encodings through
gen_illegal() to raise EXCP_ILL when the FPU is enabled.

Decode both encodings and compute IEEE remainder in single and
double precision with SoftFloat, with qtest coverage for exact
result bits at quotient ties under directed rounding and for the
FPSR invalid operation flag on zero divisors and infinite dividends.

Signed-off-by: Kirill A. Korinsky <kirill@korins.ky>
Signed-off-by: Helge Deller <deller@gmx.de>
---
 target/hppa/fpu_helper.c    |  14 +++++
 target/hppa/helper.h        |   2 +
 target/hppa/insns.decode    |   4 ++
 target/hppa/translate.c     |  10 ++++
 tests/qtest/hppa-fpu-test.c | 105 +++++++++++++++++++++++++++++++++++-
 5 files changed, 134 insertions(+), 1 deletion(-)

diff --git a/target/hppa/fpu_helper.c b/target/hppa/fpu_helper.c
index a0c8c91ee5..f42044cdef 100644
--- a/target/hppa/fpu_helper.c
+++ b/target/hppa/fpu_helper.c
@@ -177,6 +177,13 @@ float32 HELPER(fdiv_s)(CPUHPPAState *env, float32 a, float32 b)
     return ret;
 }
 
+float32 HELPER(frem_s)(CPUHPPAState *env, float32 a, float32 b)
+{
+    float32 ret = float32_rem(a, b, &env->fp_status);
+    update_fr0_op(env, GETPC());
+    return ret;
+}
+
 float64 HELPER(fsqrt_d)(CPUHPPAState *env, float64 arg)
 {
     float64 ret = float64_sqrt(arg, &env->fp_status);
@@ -219,6 +226,13 @@ float64 HELPER(fdiv_d)(CPUHPPAState *env, float64 a, float64 b)
     return ret;
 }
 
+float64 HELPER(frem_d)(CPUHPPAState *env, float64 a, float64 b)
+{
+    float64 ret = float64_rem(a, b, &env->fp_status);
+    update_fr0_op(env, GETPC());
+    return ret;
+}
+
 float64 HELPER(fcnv_s_d)(CPUHPPAState *env, float32 arg)
 {
     float64 ret = float32_to_float64(arg, &env->fp_status);
diff --git a/target/hppa/helper.h b/target/hppa/helper.h
index f6c7e16f0f..b99e298ee8 100644
--- a/target/hppa/helper.h
+++ b/target/hppa/helper.h
@@ -30,6 +30,7 @@ DEF_HELPER_FLAGS_3(fadd_s, TCG_CALL_NO_RWG, f32, env, f32, f32)
 DEF_HELPER_FLAGS_3(fsub_s, TCG_CALL_NO_RWG, f32, env, f32, f32)
 DEF_HELPER_FLAGS_3(fmpy_s, TCG_CALL_NO_RWG, f32, env, f32, f32)
 DEF_HELPER_FLAGS_3(fdiv_s, TCG_CALL_NO_RWG, f32, env, f32, f32)
+DEF_HELPER_FLAGS_3(frem_s, TCG_CALL_NO_RWG, f32, env, f32, f32)
 
 DEF_HELPER_FLAGS_2(fsqrt_d, TCG_CALL_NO_RWG, f64, env, f64)
 DEF_HELPER_FLAGS_2(frnd_d, TCG_CALL_NO_RWG, f64, env, f64)
@@ -37,6 +38,7 @@ DEF_HELPER_FLAGS_3(fadd_d, TCG_CALL_NO_RWG, f64, env, f64, f64)
 DEF_HELPER_FLAGS_3(fsub_d, TCG_CALL_NO_RWG, f64, env, f64, f64)
 DEF_HELPER_FLAGS_3(fmpy_d, TCG_CALL_NO_RWG, f64, env, f64, f64)
 DEF_HELPER_FLAGS_3(fdiv_d, TCG_CALL_NO_RWG, f64, env, f64, f64)
+DEF_HELPER_FLAGS_3(frem_d, TCG_CALL_NO_RWG, f64, env, f64, f64)
 
 DEF_HELPER_FLAGS_2(fcnv_s_d, TCG_CALL_NO_RWG, f64, env, f32)
 DEF_HELPER_FLAGS_2(fcnv_d_s, TCG_CALL_NO_RWG, f32, env, f64)
diff --git a/target/hppa/insns.decode b/target/hppa/insns.decode
index 135f563c88..ebc290d830 100644
--- a/target/hppa/insns.decode
+++ b/target/hppa/insns.decode
@@ -620,21 +620,25 @@ fadd_f          001100 ..... ..... 000 00 ...... .....  @f0c_3
 fsub_f          001100 ..... ..... 001 00 ...... .....  @f0c_3
 fmpy_f          001100 ..... ..... 010 00 ...... .....  @f0c_3
 fdiv_f          001100 ..... ..... 011 00 ...... .....  @f0c_3
+frem_f          001100 ..... ..... 100 00 ...... .....  @f0c_3
 
 fadd_d          001100 ..... ..... 000 01 ...... .....  @f0c_3
 fsub_d          001100 ..... ..... 001 01 ...... .....  @f0c_3
 fmpy_d          001100 ..... ..... 010 01 ...... .....  @f0c_3
 fdiv_d          001100 ..... ..... 011 01 ...... .....  @f0c_3
+frem_d          001100 ..... ..... 100 01 ...... .....  @f0c_3
 
 fadd_f          001110 ..... ..... 000 ..... ... .....  @f0e_f_3
 fsub_f          001110 ..... ..... 001 ..... ... .....  @f0e_f_3
 fmpy_f          001110 ..... ..... 010 ..... ... .....  @f0e_f_3
 fdiv_f          001110 ..... ..... 011 ..... ... .....  @f0e_f_3
+frem_f          001110 ..... ..... 100 ..... ... .....  @f0e_f_3
 
 fadd_d          001110 ..... ..... 000 ..... ... .....  @f0e_d_3
 fsub_d          001110 ..... ..... 001 ..... ... .....  @f0e_d_3
 fmpy_d          001110 ..... ..... 010 ..... ... .....  @f0e_d_3
 fdiv_d          001110 ..... ..... 011 ..... ... .....  @f0e_d_3
+frem_d          001110 ..... ..... 100 ..... ... .....  @f0e_d_3
 
 xmpyu           001110 ..... ..... 010 .0111 .00 t:5    r1=%ra64 r2=%rb64
 
diff --git a/target/hppa/translate.c b/target/hppa/translate.c
index ab3505f9c9..0713112063 100644
--- a/target/hppa/translate.c
+++ b/target/hppa/translate.c
@@ -4498,6 +4498,16 @@ static bool trans_fdiv_d(DisasContext *ctx, arg_fclass3 *a)
     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_d);
 }
 
+static bool trans_frem_f(DisasContext *ctx, arg_fclass3 *a)
+{
+    return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_frem_s);
+}
+
+static bool trans_frem_d(DisasContext *ctx, arg_fclass3 *a)
+{
+    return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_frem_d);
+}
+
 static bool trans_xmpyu(DisasContext *ctx, arg_xmpyu *a)
 {
     TCGv_i64 x, y;
diff --git a/tests/qtest/hppa-fpu-test.c b/tests/qtest/hppa-fpu-test.c
index af4ff2a162..40e097dbbe 100644
--- a/tests/qtest/hppa-fpu-test.c
+++ b/tests/qtest/hppa-fpu-test.c
@@ -1,5 +1,5 @@
 /*
- * HPPA CCR floating-point enable and lazy context switching tests.
+ * HPPA floating-point instruction and context switching tests.
  *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
@@ -296,6 +296,104 @@ static void test_lazy(void)
     qtest_quit(qts);
 }
 
+typedef struct FremTest {
+    const char *name;
+    uint32_t instruction;
+    uint64_t dividend;
+    uint64_t divisor;
+    uint64_t result;
+    uint32_t fpsr;
+    uint32_t flags;
+} FremTest;
+
+static const FremTest frem_tests[] = {
+    {
+        .name = "single-tie",
+        .instruction = 0x318d860e, /* frem,sgl fr12,fr13,fr14 */
+        .dividend = 0x40e0000000000000ULL, /* 7.0f */
+        .divisor = 0x4000000000000000ULL, /* 2.0f */
+        .result = 0xbf80000089abcdefULL, /* -1.0f */
+        .fpsr = 0x200, /* round toward zero */
+    },
+    {
+        .name = "double-tie",
+        .instruction = 0x318d8e0e, /* frem,dbl fr12,fr13,fr14 */
+        .dividend = 0x4014000000000000ULL, /* 5.0 */
+        .divisor = 0x4000000000000000ULL, /* 2.0 */
+        .result = 0x3ff0000000000000ULL, /* 1.0 */
+        .fpsr = 0x400, /* round upward */
+    },
+    {
+        .name = "single-extended-subnormal",
+        .instruction = 0x398d96ce, /* frem,sgl fr12R,fr13R,fr14R */
+        .dividend = 0x0000000000000001ULL, /* smallest subnormal */
+        .divisor = 0x000000003f800000ULL, /* 1.0f */
+        .result = 0x0123456700000001ULL,
+        .fpsr = 0x600, /* round downward */
+    },
+    {
+        .name = "double-extended-negative-zero",
+        .instruction = 0x398d8e0e, /* frem,dbl fr12,fr13,fr14 */
+        .dividend = 0xc010000000000000ULL, /* -4.0 */
+        .divisor = 0x4000000000000000ULL, /* 2.0 */
+        .result = 0x8000000000000000ULL, /* -0.0 */
+    },
+    {
+        .name = "single-zero-divisor",
+        .instruction = 0x318d860e,
+        .dividend = 0x3f80000000000000ULL, /* 1.0f */
+        .divisor = 0,
+        .result = 0x7fa0000089abcdefULL, /* quiet NaN */
+        .flags = 0x80000000, /* invalid operation */
+    },
+    {
+        .name = "double-infinite-dividend",
+        .instruction = 0x398d8e0e,
+        .dividend = 0x7ff0000000000000ULL, /* infinity */
+        .divisor = 0x3ff0000000000000ULL, /* 1.0 */
+        .result = 0x7ff4000000000000ULL, /* quiet NaN */
+        .flags = 0x80000000, /* invalid operation */
+    },
+};
+
+static void test_frem(const void *opaque)
+{
+    const FremTest *test = opaque;
+    const uint32_t guest[] = {
+        0x34011000, /* ldi 0x800,r1 */
+        0x01c11840, /* mtctl r1,iva */
+        0x34103000, /* ldi 0x1800,r16 */
+        0x34113200, /* ldi 0x1900,r17 */
+        0x34010180, /* ldi 0xc0,r1 */
+        0x01411840, /* mtctl r1,ccr */
+        0x2e00100c, /* fldd 0(r16),fr12 */
+        0x2e10100d, /* fldd 8(r16),fr13 */
+        0x36100020, /* ldo 16(r16),r16 */
+        0x2e001000, /* fldd 0(r16),fr0 */
+        0x2e10100e, /* fldd 8(r16),fr14 */
+        test->instruction,
+        0x2e10120e, /* fstd fr14,8(r16) */
+        0x2e001200, /* fstd fr0,0(r16) */
+        0x34010002, /* ldi 1,r1 */
+        0x6a210000, /* stw r1,0(r17): completion */
+    };
+    QTestState *qts = start_guest(false);
+    uint32_t end = CODE + sizeof(guest);
+
+    write_words(qts, CODE, guest, G_N_ELEMENTS(guest));
+    qtest_writel(qts, end, branch(end, HALT));
+    qtest_writeq(qts, DATA, test->dividend);
+    qtest_writeq(qts, DATA + 8, test->divisor);
+    qtest_writeq(qts, DATA + 16, (uint64_t)test->fpsr << 32);
+    /* A single-precision write must preserve the other half of FR14. */
+    qtest_writeq(qts, DATA + 24, 0x0123456789abcdefULL);
+
+    run_guest(qts, 1);
+    g_assert_cmphex(qtest_readq(qts, DATA + 24), ==, test->result);
+    g_assert_cmphex(qtest_readl(qts, DATA + 16), ==, test->fpsr | test->flags);
+    qtest_quit(qts);
+}
+
 static const uint32_t fault_guest[] = {
     0x34011000, /* ldi 0x800,r1 */
     0x01c11840, /* mtctl r1,iva */
@@ -362,6 +460,11 @@ int main(int argc, char **argv)
         qtest_add_data_func(name, &fp_tests[i], test_fp);
     }
     qtest_add_func("fpu/lazy-contexts", test_lazy);
+    for (size_t i = 0; i < G_N_ELEMENTS(frem_tests); i++) {
+        g_autofree char *name = g_strdup_printf("fpu/frem/%s", frem_tests[i].name);
+
+        qtest_add_data_func(name, &frem_tests[i], test_frem);
+    }
     for (size_t i = 0; i < G_N_ELEMENTS(fault_tests); i++) {
         g_autofree char *name = g_strdup_printf("fpu/%s", fault_tests[i].name);
 
-- 
2.54.0



  reply	other threads:[~2026-12-01 23:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:21 [PULL for 11.2 0/4] Hppa fixes patches Helge Deller
2026-12-01 23:21 ` Helge Deller [this message]
2026-12-01 23:21 ` [PULL for 11.2 2/4] target/hppa: Defer PA7300LC FCNVFF on NaNs Helge Deller
2026-12-01 23:21 ` [PULL for 11.2 3/4] target/hppa: Defer PA7300LC multiply assists Helge Deller
2026-12-01 23:21 ` [PULL for 11.2 4/4] target/hppa: Fix tcg_gen_goto_tb assertion Helge Deller

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=20261201232154.3178-2-deller@kernel.org \
    --to=deller@kernel.org \
    --cc=deller@gmx.de \
    --cc=kirill@korins.ky \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.