From 47a823b28134b274fafa266518cec57fac916ae5 Mon Sep 17 00:00:00 2001 From: Nick Kledzik Date: Thu, 17 Mar 2011 00:09:13 +0000 Subject: [PATCH] implement udivmodsi4 and divmodsi4 for ARM llvm-svn: 127778 --- compiler-rt/README.txt | 8 ++- compiler-rt/lib/divmodsi4.c | 30 ++++++++++ compiler-rt/lib/udivmodsi4.c | 30 ++++++++++ compiler-rt/make/platform/darwin_bni.mk | 9 +-- compiler-rt/test/Unit/divmodsi4_test.c | 73 +++++++++++++++++++++++++ compiler-rt/test/Unit/udivmodsi4_test.c | 59 ++++++++++++++++++++ 6 files changed, 203 insertions(+), 6 deletions(-) create mode 100644 compiler-rt/lib/divmodsi4.c create mode 100644 compiler-rt/lib/udivmodsi4.c create mode 100644 compiler-rt/test/Unit/divmodsi4_test.c create mode 100644 compiler-rt/test/Unit/udivmodsi4_test.c diff --git a/compiler-rt/README.txt b/compiler-rt/README.txt index ab389d2129ba..cbeb10cf918e 100644 --- a/compiler-rt/README.txt +++ b/compiler-rt/README.txt @@ -77,8 +77,12 @@ ti_int __modti3 (ti_int a, ti_int b); // a % b signed 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/compiler-rt/lib/divmodsi4.c b/compiler-rt/lib/divmodsi4.c new file mode 100644 index 000000000000..2ec3dd4a7e5a --- /dev/null +++ b/compiler-rt/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/compiler-rt/lib/udivmodsi4.c b/compiler-rt/lib/udivmodsi4.c new file mode 100644 index 000000000000..38b5bd43de67 --- /dev/null +++ b/compiler-rt/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/compiler-rt/make/platform/darwin_bni.mk b/compiler-rt/make/platform/darwin_bni.mk index ec859f3b5d6e..f15334f31943 100644 --- a/compiler-rt/make/platform/darwin_bni.mk +++ b/compiler-rt/make/platform/darwin_bni.mk @@ -61,10 +61,10 @@ FUNCTIONS.armv5 := $(FUNCTIONS) \ 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 @@ FUNCTIONS.armv6 := $(FUNCTIONS) \ 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 @@ FUNCTIONS.armv7 := $(FUNCTIONS) \ muldf3vfp mulsf3vfp \ nedf2vfp nesf2vfp \ subdf3vfp subsf3vfp truncdfsf2vfp unorddf2vfp unordsf2vfp \ - modsi3 umodsi3 udivsi3 divsi3 + modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 diff --git a/compiler-rt/test/Unit/divmodsi4_test.c b/compiler-rt/test/Unit/divmodsi4_test.c new file mode 100644 index 000000000000..bea31ea9a445 --- /dev/null +++ b/compiler-rt/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 + +// 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/compiler-rt/test/Unit/udivmodsi4_test.c b/compiler-rt/test/Unit/udivmodsi4_test.c new file mode 100644 index 000000000000..d734cd1fdf71 --- /dev/null +++ b/compiler-rt/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 + +// 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; +}