about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2021-07-02 10:17:28 +0200
committerptitSeb <sebastien.chev@gmail.com>2021-07-02 10:17:28 +0200
commit140254f311eead840969229040c807ad3da3f177 (patch)
tree9aa0454f457504de7967c3d3522ad29ed580264a
parent56401f667525063eac16081d19395527d3403e60 (diff)
downloadbox64-140254f311eead840969229040c807ad3da3f177.tar.gz
box64-140254f311eead840969229040c807ad3da3f177.zip
[DYNAREC] Fixed DIV opcode, and added test17 about div/idiv
-rwxr-xr-xCMakeLists.txt5
-rwxr-xr-xsrc/dynarec/dynarec_arm64_00.c11
-rw-r--r--tests/ref17.txt12
-rwxr-xr-xtests/test17bin0 -> 19976 bytes
-rw-r--r--tests/test17.c50
5 files changed, 73 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 49577c1c..ceb21e68 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -513,6 +513,11 @@ add_test(test16 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX64}
     -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref16.txt
     -P ${CMAKE_SOURCE_DIR}/runTest.cmake )
 
+add_test(test17 ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX64} 
+    -D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test17 -D TEST_OUTPUT=tmpfile.txt 
+    -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref17.txt
+    -P ${CMAKE_SOURCE_DIR}/runTest.cmake )
+
 file(GLOB extension_tests "${CMAKE_SOURCE_DIR}/tests/extensions/*.c")
 foreach(file ${extension_tests})
     get_filename_component(testname "${file}" NAME_WE)
diff --git a/src/dynarec/dynarec_arm64_00.c b/src/dynarec/dynarec_arm64_00.c
index 31605840..6e1c67e7 100755
--- a/src/dynarec/dynarec_arm64_00.c
+++ b/src/dynarec/dynarec_arm64_00.c
@@ -2237,7 +2237,7 @@ uintptr_t dynarec64_00(dynarec_arm_t* dyn, uintptr_t addr, uintptr_t ip, int nin
                         } else {
                             GETEDH(x1, 0);  // get edd changed addr, so cannot be called 2 times for same op...
                             CBZxw_MARK(xRDX);
-                            if(ed!=x1) {MOVxw_REG(x1, ed);}
+                            if(ed!=x1) {MOVx_REG(x1, ed);}
                             CALL(div64, -1);
                             B_NEXT_nocond;
                             MARK;
@@ -2263,24 +2263,25 @@ uintptr_t dynarec64_00(dynarec_arm_t* dyn, uintptr_t addr, uintptr_t ip, int nin
                     } else {
                         if(ninst && dyn->insts
                            &&  dyn->insts[ninst-1].x64.addr 
-                           && *(uint8_t*)(dyn->insts[ninst-1].x64.addr)==0x99) {
+                           && *(uint8_t*)(dyn->insts[ninst-1].x64.addr)==0x48
+                           && *(uint8_t*)(dyn->insts[ninst-1].x64.addr+1)==0x99) {
                             SET_DFNONE(x2)
                             GETED(0);
                             SDIVx(x2, xRAX, ed);
                             MSUBx(xRDX, x2, ed, xRAX);
-                            MOVw_REG(xRAX, x2);
+                            MOVx_REG(xRAX, x2);
                         } else {
                             GETEDH(x1, 0);  // get edd changed addr, so cannot be called 2 times for same op...
                             CBZxw_MARK(xRDX);
                             MVNx_REG(x2, xRDX);
                             CBZxw_MARK(x2);
-                            if(ed!=x1) {MOVxw_REG(x1, ed);}
+                            if(ed!=x1) {MOVx_REG(x1, ed);}
                             CALL((void*)idiv64, -1);
                             B_NEXT_nocond;
                             MARK;
                             SDIVx(x2, xRAX, ed);
                             MSUBx(xRDX, x2, ed, xRAX);
-                            MOVw_REG(xRAX, x2);
+                            MOVx_REG(xRAX, x2);
                             SET_DFNONE(x2)
                         }
                     }
diff --git a/tests/ref17.txt b/tests/ref17.txt
new file mode 100644
index 00000000..349d50d0
--- /dev/null
+++ b/tests/ref17.txt
@@ -0,0 +1,12 @@
+div 1, 1 => 1 / 0
+div 10, 5 => 2 / 0
+div 10, 3 => 3 / 1
+div 1, 18446744073709551615 => 0 / 1
+div 10, 18446744073709551613 => 0 / 10
+div 18446744073709551606, 18446744073709551613 => 0 / 18446744073709551606
+idiv 1, 1 => 1 / 0
+idiv 10, 5 => 2 / 0
+idiv 10, 3 => 3 / 1
+idiv 1, -1 => -1 / 0
+idiv 10, -3 => -3 / 1
+idiv -10, -3 => 3 / -1
diff --git a/tests/test17 b/tests/test17
new file mode 100755
index 00000000..bcfd74f8
--- /dev/null
+++ b/tests/test17
Binary files differdiff --git a/tests/test17.c b/tests/test17.c
new file mode 100644
index 00000000..799c1368
--- /dev/null
+++ b/tests/test17.c
@@ -0,0 +1,50 @@
+#include <string.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <math.h>
+
+#if defined(__x86_64__)
+uint64_t _div_(uint64_t a, uint64_t b, uint64_t *r)
+{
+    uint64_t ret, rem;
+    asm volatile (
+    "xor %%rdx, %%rdx\n"
+    "div %%rcx\n"
+    "mov %%rdx, %%rbx\n"
+    :"=a" (ret), "=b" (rem):"a" (a), "c" (b):"rdx","cc");
+    *r = rem;
+    return ret;
+}
+uint64_t _idiv_(uint64_t a, uint64_t b, uint64_t *r)
+{
+    uint64_t ret, rem;
+    asm volatile (
+    "cqo\n"
+    "idiv %%rcx\n"
+    "mov %%rdx, %%rbx\n"
+    :"=a" (ret), "=b" (rem):"a" (a), "c" (b):"rdx","cc");
+    *r = rem;
+    return ret;
+}
+#else
+#endif
+
+int main(int argc, const char** argv)
+{
+  uint64_t datas[][2] = {{1,1},{10,5},{10,3},{1, (uint64_t)-1}, {10, (uint64_t)-3}, {(uint64_t)-10, (uint64_t)-3}};
+
+  int sz = sizeof(datas)/sizeof(datas[0]);
+  for(int i=0; i<sz; ++i) {
+   uint64_t rem = 0;
+   uint64_t d = _div_(datas[i][0], datas[i][1], &rem);
+   printf("div %llu, %llu => %llu / %llu\n", datas[i][0], datas[i][1], d, rem);
+ }
+  for(int i=0; i<sz; ++i) {
+   uint64_t rem = 0;
+   uint64_t d = _idiv_(datas[i][0], datas[i][1], &rem);
+   printf("idiv %lld, %lld => %lld / %lld\n", datas[i][0], datas[i][1], d, rem);
+ }
+  return 0;
+}