<rdar://problem/8914924> implement udivmodsi4 and divmodsi4 for ARM

llvm-svn: 127778
GitOrigin-RevId: 47a823b28134b274fafa266518cec57fac916ae5
diff --git a/README.txt b/README.txt
index ab389d2..cbeb10c 100644
--- a/README.txt
+++ b/README.txt
@@ -77,8 +77,12 @@
 su_int __umodsi3   (su_int a, su_int b);               // a % b   unsigned
 du_int __umoddi3   (du_int a, du_int b);               // a % b   unsigned
 tu_int __umodti3   (tu_int a, tu_int b);               // a % b   unsigned
-du_int __udivmoddi4(du_int a, du_int b, du_int* rem);  // a / b, *rem = a % b
-tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);  // a / b, *rem = a % b
+du_int __udivmoddi4(du_int a, du_int b, du_int* rem);  // a / b, *rem = a % b  unsigned
+tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);  // a / b, *rem = a % b  unsigned
+su_int __udivmodsi4(su_int a, su_int b, su_int* rem);  // a / b, *rem = a % b  unsigned
+si_int __divmodsi4(si_int a, si_int b, si_int* rem);   // a / b, *rem = a % b  signed
+
+
 
 //  Integral arithmetic with trapping overflow
 
diff --git a/lib/divmodsi4.c b/lib/divmodsi4.c
new file mode 100644
index 0000000..2ec3dd4
--- /dev/null
+++ b/lib/divmodsi4.c
@@ -0,0 +1,30 @@
+/*===-- divmodsi4.c - Implement __divmodsi4 --------------------------------===
+ *
+ *                    The LLVM Compiler Infrastructure
+ *
+ * This file is dual licensed under the MIT and the University of Illinois Open
+ * Source Licenses. See LICENSE.TXT for details.
+ *
+ * ===----------------------------------------------------------------------===
+ *
+ * This file implements __divmodsi4 for the compiler_rt library.
+ *
+ * ===----------------------------------------------------------------------===
+ */
+
+#include "int_lib.h"
+
+extern si_int __divsi3(si_int a, si_int b);
+
+
+/* Returns: a / b, *rem = a % b  */
+
+si_int
+__divmodsi4(si_int a, si_int b, si_int* rem)
+{
+  si_int d = __divsi3(a,b);
+  *rem = a - (d*b);
+  return d; 
+}
+
+
diff --git a/lib/udivmodsi4.c b/lib/udivmodsi4.c
new file mode 100644
index 0000000..38b5bd4
--- /dev/null
+++ b/lib/udivmodsi4.c
@@ -0,0 +1,30 @@
+/*===-- udivmodsi4.c - Implement __udivmodsi4 ------------------------------===
+ *
+ *                    The LLVM Compiler Infrastructure
+ *
+ * This file is dual licensed under the MIT and the University of Illinois Open
+ * Source Licenses. See LICENSE.TXT for details.
+ *
+ * ===----------------------------------------------------------------------===
+ *
+ * This file implements __udivmodsi4 for the compiler_rt library.
+ *
+ * ===----------------------------------------------------------------------===
+ */
+
+#include "int_lib.h"
+
+extern su_int __udivsi3(su_int n, su_int d);
+
+
+/* Returns: a / b, *rem = a % b  */
+
+su_int 
+__udivmodsi4(su_int a, su_int b, su_int* rem)
+{
+  si_int d = __udivsi3(a,b);
+  *rem = a - (d*b);
+  return d; 
+}
+
+
diff --git a/make/platform/darwin_bni.mk b/make/platform/darwin_bni.mk
index ec859f3..f15334f 100644
--- a/make/platform/darwin_bni.mk
+++ b/make/platform/darwin_bni.mk
@@ -61,10 +61,10 @@
                 muldf3 mulsf3 \
                 negdf2 negsf2 \
                 truncdfsf2  \
-                modsi3 umodsi3 udivsi3 divsi3 \
+                modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 \
                 switch8 switchu8 switch16 switch32 \
                 sync_synchronize 
-			
+
 FUNCTIONS.armv6 := $(FUNCTIONS) \
 				comparedf2 comparesf2 \
                 adddf3vfp addsf3vfp bswapdi2 bswapsi2 divdf3vfp \
@@ -76,10 +76,11 @@
                 muldf3vfp mulsf3vfp \
                 nedf2vfp nesf2vfp \
                 subdf3vfp subsf3vfp truncdfsf2vfp unorddf2vfp unordsf2vfp \
-                modsi3 umodsi3 udivsi3 divsi3 \
+                modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 \
                 switch8 switchu8 switch16 switch32 \
                 restore_vfp_d8_d15_regs save_vfp_d8_d15_regs \
                 sync_synchronize 
+
 FUNCTIONS.armv7 := $(FUNCTIONS) \
 				comparedf2 comparesf2 \
                 adddf3vfp addsf3vfp bswapdi2 bswapsi2 divdf3vfp \
@@ -91,5 +92,5 @@
                 muldf3vfp mulsf3vfp \
                 nedf2vfp nesf2vfp \
                 subdf3vfp subsf3vfp truncdfsf2vfp unorddf2vfp unordsf2vfp \
-                modsi3 umodsi3 udivsi3 divsi3 
+                modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4
 
diff --git a/test/Unit/divmodsi4_test.c b/test/Unit/divmodsi4_test.c
new file mode 100644
index 0000000..bea31ea
--- /dev/null
+++ b/test/Unit/divmodsi4_test.c
@@ -0,0 +1,73 @@
+//===-- divmodsi4_test.c - Test __divmodsi4 -------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file tests __divmodsi4 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+#include <stdio.h>
+
+// Returns: a / b
+
+extern si_int __divmodsi4(si_int a, si_int b, si_int* rem);
+
+
+int test__divmodsi4(si_int a, si_int b, 
+						si_int expected_result, si_int expected_rem)
+{
+	si_int rem;
+    si_int result = __divmodsi4(a, b, &rem);
+    if (result != expected_result) {
+        printf("error in __divmodsi4: %d / %d = %d, expected %d\n",
+               a, b, result, expected_result);
+		return 1;
+	}
+    if (rem != expected_rem) {
+        printf("error in __divmodsi4: %d mod %d = %d, expected %d\n",
+               a, b, rem, expected_rem);
+		return 1;
+	}
+	
+    return 0;
+}
+
+
+int main()
+{
+    if (test__divmodsi4(0, 1, 0, 0))
+        return 1;
+    if (test__divmodsi4(0, -1, 0, 0))
+        return 1;
+
+    if (test__divmodsi4(2, 1, 2, 0))
+        return 1;
+    if (test__divmodsi4(2, -1, -2, 0))
+        return 1;
+    if (test__divmodsi4(-2, 1, -2, 0))
+        return 1;
+    if (test__divmodsi4(-2, -1, 2, 0))
+        return 1;
+
+	if (test__divmodsi4(7, 5, 1, 2))
+        return 1;
+	if (test__divmodsi4(-7, 5, -1, -2))
+        return 1;
+	if (test__divmodsi4(19, 5, 3, 4))
+        return 1;
+	if (test__divmodsi4(19, -5, -3, 4))
+        return 1;
+  	
+	if (test__divmodsi4(0x80000000, 8, 0xf0000000, 0))
+        return 1;
+	if (test__divmodsi4(0x80000007, 8, 0xf0000001, -1))
+        return 1;
+
+    return 0;
+}
diff --git a/test/Unit/udivmodsi4_test.c b/test/Unit/udivmodsi4_test.c
new file mode 100644
index 0000000..d734cd1
--- /dev/null
+++ b/test/Unit/udivmodsi4_test.c
@@ -0,0 +1,59 @@
+//===-- udivmodsi4_test.c - Test __udivmodsi4 -----------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file tests __udivmodsi4 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include "int_lib.h"
+#include <stdio.h>
+
+// Returns: a / b
+
+extern su_int __udivmodsi4(su_int a, su_int b, su_int* rem);
+
+int test__udivmodsi4(su_int a, su_int b, 
+						su_int expected_result, su_int expected_rem)
+{
+	su_int rem;
+    su_int result = __udivmodsi4(a, b, &rem);
+    if (result != expected_result) {
+        printf("error in __udivmodsi4: %u / %u = %u, expected %u\n",
+               a, b, result, expected_result);
+		return 1;
+	}
+    if (rem != expected_rem) {
+        printf("error in __udivmodsi4: %u mod %u = %u, expected %u\n",
+               a, b, rem, expected_rem);
+		return 1;
+	}
+	
+    return 0;
+}
+
+
+int main()
+{
+    if (test__udivmodsi4(0, 1, 0, 0))
+        return 1;
+
+    if (test__udivmodsi4(2, 1, 2, 0))
+        return 1;
+
+	if (test__udivmodsi4(19, 5, 3, 4))
+        return 1;
+
+	if (test__udivmodsi4(0x80000000, 8, 0x10000000, 0))
+        return 1;
+  	
+ 	if (test__udivmodsi4(0x80000003, 8, 0x10000000, 3))
+        return 1;
+
+	return 0;
+}