diff mbox

[08/10,v11] target-tilegx: Add several helpers for instructions translation

Message ID BLU436-SMTP234E1D6DF2E928D390FE99AB9C80@phx.gbl
State New
Headers show

Commit Message

Chen Gang May 30, 2015, 9:17 p.m. UTC
The related instructions are exception, cntlz, cnttz, and shufflebytes.

Signed-off-by: Chen Gang <gang.chen.5i5j@gmail.com>
---
 target-tilegx/helper.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
 target-tilegx/helper.h |  4 +++
 2 files changed, 77 insertions(+)
 create mode 100644 target-tilegx/helper.c
 create mode 100644 target-tilegx/helper.h

Comments

Richard Henderson June 1, 2015, 4:02 p.m. UTC | #1
On 05/30/2015 02:17 PM, Chen Gang wrote:
> +    for (count = 0; count < 8; count++) {
> +        sel = (rsrcb >> (count * 8)) & 0xf;
> +        if (sel < 8) {
> +            vdst |= ((rdst >> (8 * sel)) & 0xff) << (count * 8);
> +        } else {
> +            vdst |= ((rsrc >> (8 * (8 - sel))) & 0xff) << (count * 8);

8 - sel is wrong; you wanted sel - 8.

That said, you can do better with masking operations.  And for brevity, let
count increment by 8.  E.g.

  uint64_t vdst = 0;
  int count;

  for (count = 0; count < 64; count += 8) {
    uint64_t sel = rsrcb >> count;
    uint64_t src = (sel & 8 ? rsrc : rdst);
    vdst |= ((src >> ((sel & 7) * 8)) & 0xff) << count;
  }


r~
Chen Gang June 1, 2015, 6:47 p.m. UTC | #2
On 6/2/15 00:02, Richard Henderson wrote:
> On 05/30/2015 02:17 PM, Chen Gang wrote:
>> +    for (count = 0; count < 8; count++) {
>> +        sel = (rsrcb >> (count * 8)) & 0xf;
>> +        if (sel < 8) {
>> +            vdst |= ((rdst >> (8 * sel)) & 0xff) << (count * 8);
>> +        } else {
>> +            vdst |= ((rsrc >> (8 * (8 - sel))) & 0xff) << (count * 8);
> 
> 8 - sel is wrong; you wanted sel - 8.
> 

OK, thank you very much.

> That said, you can do better with masking operations.  And for brevity, let
> count increment by 8.  E.g.
> 
>   uint64_t vdst = 0;
>   int count;
> 
>   for (count = 0; count < 64; count += 8) {
>     uint64_t sel = rsrcb >> count;
>     uint64_t src = (sel & 8 ? rsrc : rdst);
>     vdst |= ((src >> ((sel & 7) * 8)) & 0xff) << count;
>   }
> 

OK, thank.
diff mbox

Patch

diff --git a/target-tilegx/helper.c b/target-tilegx/helper.c
new file mode 100644
index 0000000..f125dc8
--- /dev/null
+++ b/target-tilegx/helper.c
@@ -0,0 +1,73 @@ 
+/*
+ * QEMU TILE-Gx helpers
+ *
+ *  Copyright (c) 2015 Chen Gang
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * <http://www.gnu.org/licenses/lgpl-2.1.html>
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "exec/helper-proto.h"
+
+void helper_exception(CPUTLGState *env, uint32_t excp)
+{
+    CPUState *cs = CPU(tilegx_env_get_cpu(env));
+
+    cs->exception_index = excp;
+    cpu_loop_exit(cs);
+}
+
+uint64_t helper_cntlz(uint64_t arg)
+{
+    return clz64(arg);
+}
+
+uint64_t helper_cnttz(uint64_t arg)
+{
+    return ctz64(arg);
+}
+
+/*
+   Functional Description
+       uint64_t a = rf[SrcA];
+       uint64_t b = rf[SrcB];
+       uint64_t d = rf[Dest];
+       uint64_t output = 0;
+       unsigned int counter;
+       for (counter = 0; counter < (WORD_SIZE / BYTE_SIZE); counter++)
+       {
+           int sel = getByte (b, counter) & 0xf;
+           uint8_t byte = (sel < 8) ? getByte (d, sel) : getByte (a, (sel - 8));
+           output = setByte (output, counter, byte);
+       }
+       rf[Dest] = output;
+ */
+uint64_t helper_shufflebytes(uint64_t rdst, uint64_t rsrc, uint64_t rsrcb)
+{
+    uint64_t vdst = 0;
+    int count, sel;
+
+    for (count = 0; count < 8; count++) {
+        sel = (rsrcb >> (count * 8)) & 0xf;
+        if (sel < 8) {
+            vdst |= ((rdst >> (8 * sel)) & 0xff) << (count * 8);
+        } else {
+            vdst |= ((rsrc >> (8 * (8 - sel))) & 0xff) << (count * 8);
+        }
+    }
+
+    return vdst;
+}
diff --git a/target-tilegx/helper.h b/target-tilegx/helper.h
new file mode 100644
index 0000000..fd5517e
--- /dev/null
+++ b/target-tilegx/helper.h
@@ -0,0 +1,4 @@ 
+DEF_HELPER_2(exception, noreturn, env, i32)
+DEF_HELPER_FLAGS_1(cntlz, TCG_CALL_NO_RWG_SE, i64, i64)
+DEF_HELPER_FLAGS_1(cnttz, TCG_CALL_NO_RWG_SE, i64, i64)
+DEF_HELPER_FLAGS_3(shufflebytes, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64)