From 1ace4055f79f304750839d73c46bbcaeb994f1b5 Mon Sep 17 00:00:00 2001 From: Eric Christopher Date: Fri, 17 Jun 2011 20:17:05 +0000 Subject: Implement mulo4 for use in signed overflow checking. Fixes rdar://9219742 and rdar://9218244 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@133284 91177308-0d34-0410-b5e6-96231b3b80d8 --- README.txt | 9 ++ lib/mulodi4.c | 59 +++++++++ lib/mulosi4.c | 59 +++++++++ lib/muloti4.c | 63 ++++++++++ make/platform/clang_darwin.mk | 13 +- make/platform/darwin_bni.mk | 15 ++- test/Unit/mulodi4_test.c | 178 +++++++++++++++++++++++++++ test/Unit/mulosi4_test.c | 156 +++++++++++++++++++++++ test/Unit/muloti4_test.c | 280 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 823 insertions(+), 9 deletions(-) create mode 100644 lib/mulodi4.c create mode 100644 lib/mulosi4.c create mode 100644 lib/muloti4.c create mode 100644 test/Unit/mulodi4_test.c create mode 100644 test/Unit/mulosi4_test.c create mode 100644 test/Unit/muloti4_test.c diff --git a/README.txt b/README.txt index cbeb10cf9..b37c0aecd 100644 --- a/README.txt +++ b/README.txt @@ -106,6 +106,15 @@ si_int __mulvsi3(si_int a, si_int b); // a * b di_int __mulvdi3(di_int a, di_int b); // a * b ti_int __mulvti3(ti_int a, ti_int b); // a * b + +// Integral arithmetic which returns if overflow + +si_int __mulosi4(si_int a, si_int b, int* overflow); // a * b, overflow set to one if result not in signed range +di_int __mulodi4(di_int a, di_int b, int* overflow); // a * b, overflow set to one if result not in signed range +ti_int __muloti4(ti_int a, ti_int b, int* overflow); // a * b, overflow set to + one if result not in signed range + + // Integral comparison: a < b -> 0 // a == b -> 1 // a > b -> 2 diff --git a/lib/mulodi4.c b/lib/mulodi4.c new file mode 100644 index 000000000..dcaf6ab86 --- /dev/null +++ b/lib/mulodi4.c @@ -0,0 +1,59 @@ +/*===-- mulodi4.c - Implement __mulodi4 -----------------------------------=== + * + * 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 __mulodi4 for the compiler_rt library. + * + * ===----------------------------------------------------------------------=== + */ + +#include "int_lib.h" +#include + +/* Returns: a * b */ + +/* Effects: sets *overflow to 1 if a * b overflows */ + +di_int +__mulodi4(di_int a, di_int b, int* overflow) +{ + const int N = (int)(sizeof(di_int) * CHAR_BIT); + const di_int MIN = (di_int)1 << (N-1); + const di_int MAX = ~MIN; + *overflow = 0; + di_int result = a * b; + if (a == MIN) + { + if (b != 0 && b != 1) + *overflow = 1; + return result; + } + if (b == MIN) + { + if (a != 0 && a != 1) + *overflow = 1; + return result; + } + di_int sa = a >> (N - 1); + di_int abs_a = (a ^ sa) - sa; + di_int sb = b >> (N - 1); + di_int abs_b = (b ^ sb) - sb; + if (abs_a < 2 || abs_b < 2) + return result; + if (sa == sb) + { + if (abs_a > MAX / abs_b) + *overflow = 1; + } + else + { + if (abs_a > MIN / -abs_b) + *overflow = 1; + } + return result; +} diff --git a/lib/mulosi4.c b/lib/mulosi4.c new file mode 100644 index 000000000..a1717d81f --- /dev/null +++ b/lib/mulosi4.c @@ -0,0 +1,59 @@ +/*===-- mulosi4.c - Implement __mulosi4 -----------------------------------=== + * + * 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 __mulosi4 for the compiler_rt library. + * + * ===----------------------------------------------------------------------=== + */ + +#include "int_lib.h" +#include + +/* Returns: a * b */ + +/* Effects: sets *overflow to 1 if a * b overflows */ + +si_int +__mulosi4(si_int a, si_int b, int* overflow) +{ + const int N = (int)(sizeof(si_int) * CHAR_BIT); + const si_int MIN = (si_int)1 << (N-1); + const si_int MAX = ~MIN; + *overflow = 0; + si_int result = a * b; + if (a == MIN) + { + if (b != 0 && b != 1) + *overflow = 1; + return result; + } + if (b == MIN) + { + if (a != 0 && a != 1) + *overflow = 1; + return result; + } + si_int sa = a >> (N - 1); + si_int abs_a = (a ^ sa) - sa; + si_int sb = b >> (N - 1); + si_int abs_b = (b ^ sb) - sb; + if (abs_a < 2 || abs_b < 2) + return result; + if (sa == sb) + { + if (abs_a > MAX / abs_b) + *overflow = 1; + } + else + { + if (abs_a > MIN / -abs_b) + *overflow = 1; + } + return result; +} diff --git a/lib/muloti4.c b/lib/muloti4.c new file mode 100644 index 000000000..bf1ab0469 --- /dev/null +++ b/lib/muloti4.c @@ -0,0 +1,63 @@ +/*===-- muloti4.c - Implement __muloti4 -----------------------------------=== + * + * 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 __muloti4 for the compiler_rt library. + * + * ===----------------------------------------------------------------------=== + */ + +#if __x86_64 + +#include "int_lib.h" +#include + +/* Returns: a * b */ + +/* Effects: sets *overflow to 1 if a * b overflows */ + +ti_int +__muloti4(ti_int a, ti_int b, int* overflow) +{ + const int N = (int)(sizeof(ti_int) * CHAR_BIT); + const ti_int MIN = (ti_int)1 << (N-1); + const ti_int MAX = ~MIN; + *overflow = 0; + ti_int result = a * b; + if (a == MIN) + { + if (b != 0 && b != 1) + *overflow = 1; + return result; + } + if (b == MIN) + { + if (a != 0 && a != 1) + *overflow = 1; + return result; + } + ti_int sa = a >> (N - 1); + ti_int abs_a = (a ^ sa) - sa; + ti_int sb = b >> (N - 1); + ti_int abs_b = (b ^ sb) - sb; + if (abs_a < 2 || abs_b < 2) + return result; + if (sa == sb) + { + if (abs_a > MAX / abs_b) + *overflow = 1; + } + else + { + if (abs_a > MIN / -abs_b) + *overflow = 1; + } + return result; +} + +#endif diff --git a/make/platform/clang_darwin.mk b/make/platform/clang_darwin.mk index 99baacff3..4cbceee6c 100644 --- a/make/platform/clang_darwin.mk +++ b/make/platform/clang_darwin.mk @@ -42,6 +42,11 @@ UniversalArchs.10.4 := $(call CheckArches,i386 x86_64) Configs += ios UniversalArchs.ios := $(call CheckArches,i386 x86_64 armv6 armv7) +# Configuration for targetting OSX. These functions may not be in libSystem +# so we should provide our own. +Configs += osx +UniversalArchs.osx := $(call CheckArches,i386 x86_64) + # Configuration for use with kernel/kexts. Configs += cc_kext UniversalArchs.cc_kext := $(call CheckArches,armv6 armv7 i386 x86_64) @@ -74,6 +79,10 @@ CFLAGS.ios.i386 := $(CFLAGS) $(X86_DEPLOYMENT_ARGS) CFLAGS.ios.x86_64 := $(CFLAGS) $(X86_DEPLOYMENT_ARGS) CFLAGS.ios.armv6 := $(CFLAGS) $(ARM_DEPLOYMENT_ARGS) CFLAGS.ios.armv7 := $(CFLAGS) $(ARM_DEPLOYMENT_ARGS) +CFLAGS.osx.i386 := $(CFLAGS) $(X86_DEPLOYMENT_ARGS) +CFLAGS.osx.x86_64 := $(CFLAGS) $(X86_DEPLOYMENT_ARGS) +CFLAGS.osx.armv6 := $(CFLAGS) $(ARM_DEPLOYMENT_ARGS) +CFLAGS.osx.armv7 := $(CFLAGS) $(ARM_DEPLOYMENT_ARGS) CFLAGS.cc_kext.i386 := $(CFLAGS) $(X86_DEPLOYMENT_ARGS) CFLAGS.cc_kext.x86_64 := $(CFLAGS) $(X86_DEPLOYMENT_ARGS) CFLAGS.cc_kext.armv6 := $(CFLAGS) $(ARM_DEPLOYMENT_ARGS) -mthumb @@ -82,7 +91,7 @@ CFLAGS.cc_kext.armv7 := $(CFLAGS) $(ARM_DEPLOYMENT_ARGS) -mthumb FUNCTIONS.eprintf := eprintf FUNCTIONS.10.4 := eprintf floatundidf floatundisf floatundixf -FUNCTIONS.ios := divmodsi4 udivmodsi4 +FUNCTIONS.ios := divmodsi4 udivmodsi4 mulosi4 mulodi4 muloti4 # On x86, the divmod functions reference divsi. FUNCTIONS.ios.i386 := $(FUNCTIONS.ios) \ divsi3 udivsi3 @@ -93,6 +102,8 @@ FUNCTIONS.ios.armv6 := $(FUNCTIONS.ios) \ switch16 switch32 switch8 switchu8 \ save_vfp_d8_d15_regs restore_vfp_d8_d15_regs +FUNCTIONS.osx := mulosi4 mulodi4 muloti4 + CCKEXT_COMMON_FUNCTIONS := \ absvdi2 \ absvsi2 \ diff --git a/make/platform/darwin_bni.mk b/make/platform/darwin_bni.mk index 14a1c197e..45a6879d1 100644 --- a/make/platform/darwin_bni.mk +++ b/make/platform/darwin_bni.mk @@ -1,7 +1,7 @@ Description := Target for Darwin using an Apple-style build. -Configs := Debug Release Profile Static +Configs := Debug Release Profile Static # We override this with RC_ARCHS because B&I may want to build on an ARCH we # haven't explicitly defined support for. If all goes well, this will just work @@ -18,7 +18,7 @@ endif CFLAGS := -Wall -Os -fomit-frame-pointer -g -CFLAGS.Static := $(CFLAGS) -static +CFLAGS.Static := $(CFLAGS) -static VISIBILITY_HIDDEN := 0 VISIBILITY_HIDDEN.Static := 1 @@ -29,8 +29,8 @@ FUNCTIONS := absvdi2 absvsi2 addvdi3 addvsi3 ashldi3 ashrdi3 \ divdc3 divdi3 divsc3 ffsdi2 \ fixdfdi fixsfdi fixunsdfdi fixunsdfsi fixunssfdi \ fixunssfsi floatdidf floatdisf floatundidf floatundisf \ - gcc_personality_v0 lshrdi3 moddi3 muldc3 muldi3 \ - mulsc3 mulvdi3 mulvsi3 negdi2 negvdi2 negvsi2 \ + gcc_personality_v0 lshrdi3 moddi3 muldc3 muldi3 mulosi3 \ + mulodi3 muloti3 mulsc3 mulvdi3 mulvsi3 negdi2 negvdi2 negvsi2 \ paritydi2 paritysi2 popcountdi2 popcountsi2 powidf2 \ powisf2 subvdi3 subvsi3 ucmpdi2 udivdi3 \ udivmoddi4 umoddi3 apple_versioning eprintf @@ -51,7 +51,7 @@ FUNCTIONS.x86_64 := $(FUNCTIONS) \ fixunsxfti fixxfdi fixxfti floatdixf floattidf \ floattisf floattixf floatundixf floatuntidf \ floatuntisf floatuntixf lshrti3 modti3 multi3 \ - mulvti3 mulxc3 negti2 negvti2 parityti2 \ + muloti3 mulvti3 mulxc3 negti2 negvti2 parityti2 \ popcountti2 powixf2 subvti3 ucmpti2 udivmodti4 \ udivti3 umodti3 clear_cache enable_execute_stack FUNCTIONS.armv5 := $(FUNCTIONS) \ @@ -65,7 +65,7 @@ FUNCTIONS.armv5 := $(FUNCTIONS) \ truncdfsf2 \ modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 \ switch8 switchu8 switch16 switch32 \ - sync_synchronize + sync_synchronize FUNCTIONS.armv6 := $(FUNCTIONS) \ comparedf2 comparesf2 \ @@ -81,7 +81,7 @@ FUNCTIONS.armv6 := $(FUNCTIONS) \ modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 \ switch8 switchu8 switch16 switch32 \ restore_vfp_d8_d15_regs save_vfp_d8_d15_regs \ - sync_synchronize + sync_synchronize FUNCTIONS.armv7 := $(FUNCTIONS) \ comparedf2 comparesf2 \ @@ -95,4 +95,3 @@ FUNCTIONS.armv7 := $(FUNCTIONS) \ nedf2vfp nesf2vfp \ subdf3vfp subsf3vfp truncdfsf2vfp unorddf2vfp unordsf2vfp \ modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 - diff --git a/test/Unit/mulodi4_test.c b/test/Unit/mulodi4_test.c new file mode 100644 index 000000000..10a0eaac6 --- /dev/null +++ b/test/Unit/mulodi4_test.c @@ -0,0 +1,178 @@ +//===-- mulodi4_test.c - Test __mulodi4 -----------------------------------===// +// +// 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 __mulodi4 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#include + +extern di_int __mulodi4(di_int a, di_int b, int* overflow); + +int test__mulodi4(di_int a, di_int b, di_int expected, int expected_overflow) +{ + int ov; + di_int x = __mulodi4(a, b, &ov); + if (ov != expected_overflow) + printf("error in __mulodi4: overflow=%d expected=%d\n", + ov, expected_overflow); + else if (!expected_overflow && x != expected) { + printf("error in __mulodi4: 0x%llX * 0x%llX = 0x%llX (overflow=%d), " + "expected 0x%llX (overflow=%d)\n", + a, b, x, ov, expected, expected_overflow); + return 1; + } + return 0; +} + +int main() +{ + if (test__mulodi4(0, 0, 0, 0)) + return 1; + if (test__mulodi4(0, 1, 0, 0)) + return 1; + if (test__mulodi4(1, 0, 0, 0)) + return 1; + if (test__mulodi4(0, 10, 0, 0)) + return 1; + if (test__mulodi4(10, 0, 0, 0)) + return 1; + if (test__mulodi4(0, 81985529216486895LL, 0, 0)) + return 1; + if (test__mulodi4(81985529216486895LL, 0, 0, 0)) + return 1; + + if (test__mulodi4(0, -1, 0, 0)) + return 1; + if (test__mulodi4(-1, 0, 0, 0)) + return 1; + if (test__mulodi4(0, -10, 0, 0)) + return 1; + if (test__mulodi4(-10, 0, 0, 0)) + return 1; + if (test__mulodi4(0, -81985529216486895LL, 0, 0)) + return 1; + if (test__mulodi4(-81985529216486895LL, 0, 0, 0)) + return 1; + + if (test__mulodi4(1, 1, 1, 0)) + return 1; + if (test__mulodi4(1, 10, 10, 0)) + return 1; + if (test__mulodi4(10, 1, 10, 0)) + return 1; + if (test__mulodi4(1, 81985529216486895LL, 81985529216486895LL, 0)) + return 1; + if (test__mulodi4(81985529216486895LL, 1, 81985529216486895LL, 0)) + return 1; + + if (test__mulodi4(1, -1, -1, 0)) + return 1; + if (test__mulodi4(1, -10, -10, 0)) + return 1; + if (test__mulodi4(-10, 1, -10, 0)) + return 1; + if (test__mulodi4(1, -81985529216486895LL, -81985529216486895LL, 0)) + return 1; + if (test__mulodi4(-81985529216486895LL, 1, -81985529216486895LL, 0)) + return 1; + + if (test__mulodi4(3037000499LL, 3037000499LL, 9223372030926249001LL, 0)) + return 1; + if (test__mulodi4(-3037000499LL, 3037000499LL, -9223372030926249001LL, 0)) + return 1; + if (test__mulodi4(3037000499LL, -3037000499LL, -9223372030926249001LL, 0)) + return 1; + if (test__mulodi4(-3037000499LL, -3037000499LL, 9223372030926249001LL, 0)) + return 1; + + if (test__mulodi4(4398046511103LL, 2097152LL, 9223372036852678656LL, 0)) + return 1; + if (test__mulodi4(-4398046511103LL, 2097152LL, -9223372036852678656LL, 0)) + return 1; + if (test__mulodi4(4398046511103LL, -2097152LL, -9223372036852678656LL, 0)) + return 1; + if (test__mulodi4(-4398046511103LL, -2097152LL, 9223372036852678656LL, 0)) + return 1; + + if (test__mulodi4(2097152LL, 4398046511103LL, 9223372036852678656LL, 0)) + return 1; + if (test__mulodi4(-2097152LL, 4398046511103LL, -9223372036852678656LL, 0)) + return 1; + if (test__mulodi4(2097152LL, -4398046511103LL, -9223372036852678656LL, 0)) + return 1; + if (test__mulodi4(-2097152LL, -4398046511103LL, 9223372036852678656LL, 0)) + return 1; + + if (test__mulodi4(0x7FFFFFFFFFFFFFFFLL, -2, 2, 1)) + return 1; + if (test__mulodi4(-2, 0x7FFFFFFFFFFFFFFFLL, 2, 1)) + return 1; + if (test__mulodi4(0x7FFFFFFFFFFFFFFFLL, -1, 0x8000000000000001LL, 0)) + return 1; + if (test__mulodi4(-1, 0x7FFFFFFFFFFFFFFFLL, 0x8000000000000001LL, 0)) + return 1; + if (test__mulodi4(0x7FFFFFFFFFFFFFFFLL, 0, 0, 0)) + return 1; + if (test__mulodi4(0, 0x7FFFFFFFFFFFFFFFLL, 0, 0)) + return 1; + if (test__mulodi4(0x7FFFFFFFFFFFFFFFLL, 1, 0x7FFFFFFFFFFFFFFFLL, 0)) + return 1; + if (test__mulodi4(1, 0x7FFFFFFFFFFFFFFFLL, 0x7FFFFFFFFFFFFFFFLL, 0)) + return 1; + if (test__mulodi4(0x7FFFFFFFFFFFFFFFLL, 2, 0x8000000000000001LL, 1)) + return 1; + if (test__mulodi4(2, 0x7FFFFFFFFFFFFFFFLL, 0x8000000000000001LL, 1)) + return 1; + + if (test__mulodi4(0x8000000000000000LL, -2, 0x8000000000000000LL, 1)) + return 1; + if (test__mulodi4(-2, 0x8000000000000000LL, 0x8000000000000000LL, 1)) + return 1; + if (test__mulodi4(0x8000000000000000LL, -1, 0x8000000000000000LL, 1)) + return 1; + if (test__mulodi4(-1, 0x8000000000000000LL, 0x8000000000000000LL, 1)) + return 1; + if (test__mulodi4(0x8000000000000000LL, 0, 0, 0)) + return 1; + if (test__mulodi4(0, 0x8000000000000000LL, 0, 0)) + return 1; + if (test__mulodi4(0x8000000000000000LL, 1, 0x8000000000000000LL, 0)) + return 1; + if (test__mulodi4(1, 0x8000000000000000LL, 0x8000000000000000LL, 0)) + return 1; + if (test__mulodi4(0x8000000000000000LL, 2, 0x8000000000000000LL, 1)) + return 1; + if (test__mulodi4(2, 0x8000000000000000LL, 0x8000000000000000LL, 1)) + return 1; + + if (test__mulodi4(0x8000000000000001LL, -2, 0x8000000000000001LL, 1)) + return 1; + if (test__mulodi4(-2, 0x8000000000000001LL, 0x8000000000000001LL, 1)) + return 1; + if (test__mulodi4(0x8000000000000001LL, -1, 0x7FFFFFFFFFFFFFFFLL, 0)) + return 1; + if (test__mulodi4(-1, 0x8000000000000001LL, 0x7FFFFFFFFFFFFFFFLL, 0)) + return 1; + if (test__mulodi4(0x8000000000000001LL, 0, 0, 0)) + return 1; + if (test__mulodi4(0, 0x8000000000000001LL, 0, 0)) + return 1; + if (test__mulodi4(0x8000000000000001LL, 1, 0x8000000000000001LL, 0)) + return 1; + if (test__mulodi4(1, 0x8000000000000001LL, 0x8000000000000001LL, 0)) + return 1; + if (test__mulodi4(0x8000000000000001LL, 2, 0x8000000000000000LL, 1)) + return 1; + if (test__mulodi4(2, 0x8000000000000001LL, 0x8000000000000000LL, 1)) + return 1; + + return 0; +} diff --git a/test/Unit/mulosi4_test.c b/test/Unit/mulosi4_test.c new file mode 100644 index 000000000..fc509db2c --- /dev/null +++ b/test/Unit/mulosi4_test.c @@ -0,0 +1,156 @@ +//===-- mulosi4_test.c - Test __mulosi4 -----------------------------------===// +// +// 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 __mulosi4 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#include "int_lib.h" +#include + +// Returns: a * b + +// Effects: aborts if a * b overflows + +si_int __mulosi4(si_int a, si_int b, int *overflow); + +int test__mulosi4(si_int a, si_int b, si_int expected, int expected_overflow) +{ + int ov; + si_int x = __mulosi4(a, b, &ov); + if (ov != expected_overflow) + printf("error in __mulosi4: overflow=%d expected=%d\n", + ov, expected_overflow); + else if (!expected_overflow && x != expected) { + printf("error in __mulosi4: 0x%X * 0x%X = 0x%X (overflow=%d), " + "expected 0x%X (overflow=%d)\n", + a, b, x, ov, expected, expected_overflow); + return 1; + } + return 0; +} + + +int main() +{ + if (test__mulosi4(0, 0, 0, 0)) + return 1; + if (test__mulosi4(0, 1, 0, 0)) + return 1; + if (test__mulosi4(1, 0, 0, 0)) + return 1; + if (test__mulosi4(0, 10, 0, 0)) + return 1; + if (test__mulosi4(10, 0, 0, 0)) + return 1; + if (test__mulosi4(0, 0x1234567, 0, 0)) + return 1; + if (test__mulosi4(0x1234567, 0, 0, 0)) + return 1; + + if (test__mulosi4(0, -1, 0, 0)) + return 1; + if (test__mulosi4(-1, 0, 0, 0)) + return 1; + if (test__mulosi4(0, -10, 0, 0)) + return 1; + if (test__mulosi4(-10, 0, 0, 0)) + return 1; + if (test__mulosi4(0, -0x1234567, 0, 0)) + return 1; + if (test__mulosi4(-0x1234567, 0, 0, 0)) + return 1; + + if (test__mulosi4(1, 1, 1, 0)) + return 1; + if (test__mulosi4(1, 10, 10, 0)) + return 1; + if (test__mulosi4(10, 1, 10, 0)) + return 1; + if (test__mulosi4(1, 0x1234567, 0x1234567, 0)) + return 1; + if (test__mulosi4(0x1234567, 1, 0x1234567, 0)) + return 1; + + if (test__mulosi4(1, -1, -1, 0)) + return 1; + if (test__mulosi4(1, -10, -10, 0)) + return 1; + if (test__mulosi4(-10, 1, -10, 0)) + return 1; + if (test__mulosi4(1, -0x1234567, -0x1234567, 0)) + return 1; + if (test__mulosi4(-0x1234567, 1, -0x1234567, 0)) + return 1; + + if (test__mulosi4(0x7FFFFFFF, -2, 0x80000001, 1)) + return 1; + if (test__mulosi4(-2, 0x7FFFFFFF, 0x80000001, 1)) + return 1; + if (test__mulosi4(0x7FFFFFFF, -1, 0x80000001, 0)) + return 1; + if (test__mulosi4(-1, 0x7FFFFFFF, 0x80000001, 0)) + return 1; + if (test__mulosi4(0x7FFFFFFF, 0, 0, 0)) + return 1; + if (test__mulosi4(0, 0x7FFFFFFF, 0, 0)) + return 1; + if (test__mulosi4(0x7FFFFFFF, 1, 0x7FFFFFFF, 0)) + return 1; + if (test__mulosi4(1, 0x7FFFFFFF, 0x7FFFFFFF, 0)) + return 1; + if (test__mulosi4(0x7FFFFFFF, 2, 0x80000001, 1)) + return 1; + if (test__mulosi4(2, 0x7FFFFFFF, 0x80000001, 1)) + return 1; + + if (test__mulosi4(0x80000000, -2, 0x80000000, 1)) + return 1; + if (test__mulosi4(-2, 0x80000000, 0x80000000, 1)) + return 1; + if (test__mulosi4(0x80000000, -1, 0x80000000, 1)) + return 1; + if (test__mulosi4(-1, 0x80000000, 0x80000000, 1)) + return 1; + if (test__mulosi4(0x80000000, 0, 0, 0)) + return 1; + if (test__mulosi4(0, 0x80000000, 0, 0)) + return 1; + if (test__mulosi4(0x80000000, 1, 0x80000000, 0)) + return 1; + if (test__mulosi4(1, 0x80000000, 0x80000000, 0)) + return 1; + if (test__mulosi4(0x80000000, 2, 0x80000000, 1)) + return 1; + if (test__mulosi4(2, 0x80000000, 0x80000000, 1)) + return 1; + + if (test__mulosi4(0x80000001, -2, 0x80000001, 1)) + return 1; + if (test__mulosi4(-2, 0x80000001, 0x80000001, 1)) + return 1; + if (test__mulosi4(0x80000001, -1, 0x7FFFFFFF, 0)) + return 1; + if (test__mulosi4(-1, 0x80000001, 0x7FFFFFFF, 0)) + return 1; + if (test__mulosi4(0x80000001, 0, 0, 0)) + return 1; + if (test__mulosi4(0, 0x80000001, 0, 0)) + return 1; + if (test__mulosi4(0x80000001, 1, 0x80000001, 0)) + return 1; + if (test__mulosi4(1, 0x80000001, 0x80000001, 0)) + return 1; + if (test__mulosi4(0x80000001, 2, 0x80000000, 1)) + return 1; + if (test__mulosi4(2, 0x80000001, 0x80000000, 1)) + return 1; + + return 0; +} diff --git a/test/Unit/muloti4_test.c b/test/Unit/muloti4_test.c new file mode 100644 index 000000000..44abddf26 --- /dev/null +++ b/test/Unit/muloti4_test.c @@ -0,0 +1,280 @@ +//===-- muloti4_test.c - Test __muloti4 -----------------------------------===// +// +// 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 __muloti3 for the compiler_rt library. +// +//===----------------------------------------------------------------------===// + +#if __x86_64 + +#include "int_lib.h" +#include + +// Returns: a * b + +// Effects: sets overflow if a * b overflows + +ti_int __muloti4(ti_int a, ti_int b, int *overflow); + +int test__muloti4(ti_int a, ti_int b, ti_int expected, int expected_overflow) +{ + int ov; + ti_int x = __muloti4(a, b, &ov); + if (ov != expected_overflow) { + twords at; + at.all = a; + twords bt; + bt.all = b; + twords xt; + xt.all = x; + twords expectedt; + expectedt.all = expected; + + printf("error in __muloti4: overflow=%d expected=%d\n", + ov, expected_overflow); + printf("error in __muloti4: 0x%.16llX%.16llX * 0x%.16llX%.16llX = " + "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n", + at.s.high, at.s.low, bt.s.high, bt.s.low, xt.s.high, xt.s.low, + expectedt.s.high, expectedt.s.low); + return 1; + } + else if (!expected_overflow && x != expected) + { + twords at; + at.all = a; + twords bt; + bt.all = b; + twords xt; + xt.all = x; + twords expectedt; + expectedt.all = expected; + printf("error in __muloti4: 0x%.16llX%.16llX * 0x%.16llX%.16llX = " + "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n", + at.s.high, at.s.low, bt.s.high, bt.s.low, xt.s.high, xt.s.low, + expectedt.s.high, expectedt.s.low); + return 1; + } + return 0; +} + +#endif + +int main() +{ +#if __x86_64 + if (test__muloti4(0, 0, 0, 0)) + return 1; + if (test__muloti4(0, 1, 0, 0)) + return 1; + if (test__muloti4(1, 0, 0, 0)) + return 1; + if (test__muloti4(0, 10, 0, 0)) + return 1; + if (test__muloti4(10, 0, 0, 0)) + return 1; + if (test__muloti4(0, 81985529216486895LL, 0, 0)) + return 1; + if (test__muloti4(81985529216486895LL, 0, 0, 0)) + return 1; + + if (test__muloti4(0, -1, 0, 0)) + return 1; + if (test__muloti4(-1, 0, 0, 0)) + return 1; + if (test__muloti4(0, -10, 0, 0)) + return 1; + if (test__muloti4(-10, 0, 0, 0)) + return 1; + if (test__muloti4(0, -81985529216486895LL, 0, 0)) + return 1; + if (test__muloti4(-81985529216486895LL, 0, 0, 0)) + return 1; + + if (test__muloti4(1, 1, 1, 0)) + return 1; + if (test__muloti4(1, 10, 10, 0)) + return 1; + if (test__muloti4(10, 1, 10, 0)) + return 1; + if (test__muloti4(1, 81985529216486895LL, 81985529216486895LL, 0)) + return 1; + if (test__muloti4(81985529216486895LL, 1, 81985529216486895LL, 0)) + return 1; + + if (test__muloti4(1, -1, -1, 0)) + return 1; + if (test__muloti4(1, -10, -10, 0)) + return 1; + if (test__muloti4(-10, 1, -10, 0)) + return 1; + if (test__muloti4(1, -81985529216486895LL, -81985529216486895LL, 0)) + return 1; + if (test__muloti4(-81985529216486895LL, 1, -81985529216486895LL, 0)) + return 1; + + if (test__muloti4(3037000499LL, 3037000499LL, 9223372030926249001LL, 0)) + return 1; + if (test__muloti4(-3037000499LL, 3037000499LL, -9223372030926249001LL, 0)) + return 1; + if (test__muloti4(3037000499LL, -3037000499LL, -9223372030926249001LL, 0)) + return 1; + if (test__muloti4(-3037000499LL, -3037000499LL, 9223372030926249001LL, 0)) + return 1; + + if (test__muloti4(4398046511103LL, 2097152LL, 9223372036852678656LL, 0)) + return 1; + if (test__muloti4(-4398046511103LL, 2097152LL, -9223372036852678656LL, 0)) + return 1; + if (test__muloti4(4398046511103LL, -2097152LL, -9223372036852678656LL, 0)) + return 1; + if (test__muloti4(-4398046511103LL, -2097152LL, 9223372036852678656LL, 0)) + return 1; + + if (test__muloti4(2097152LL, 4398046511103LL, 9223372036852678656LL, 0)) + return 1; + if (test__muloti4(-2097152LL, 4398046511103LL, -9223372036852678656LL, 0)) + return 1; + if (test__muloti4(2097152LL, -4398046511103LL, -9223372036852678656LL, 0)) + return 1; + if (test__muloti4(-2097152LL, -4398046511103LL, 9223372036852678656LL, 0)) + return 1; + + if (test__muloti4(make_ti(0x00000000000000B5LL, 0x04F333F9DE5BE000LL), + make_ti(0x0000000000000000LL, 0x00B504F333F9DE5BLL), + make_ti(0x7FFFFFFFFFFFF328LL, 0xDF915DA296E8A000LL), 0)) + return 1; + + if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + -2, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1)) + return 1; + if (test__muloti4(-2, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1)) + return 1; + if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + -1, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0)) + return 1; + if (test__muloti4(-1, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0)) + return 1; + if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + 0, + 0, 0)) + return 1; + if (test__muloti4(0, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + 0, 0)) + return 1; + if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + 1, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0)) + return 1; + if (test__muloti4(1, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0)) + return 1; + if (test__muloti4(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + 2, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1)) + return 1; + if (test__muloti4(2, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1)) + return 1; + + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL), + -2, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + if (test__muloti4(-2, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL), + -1, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + if (test__muloti4(-1, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL), + 0, + 0, 0)) + return 1; + if (test__muloti4(0, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), + 0, 0)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL), + 1, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 0)) + return 1; + if (test__muloti4(1, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 0)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000000LL), + 2, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + if (test__muloti4(2, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL), + -2, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1)) + return 1; + if (test__muloti4(-2, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 1)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL), + -1, + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0)) + return 1; + if (test__muloti4(-1, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), + make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL), + 0, + 0, 0)) + return 1; + if (test__muloti4(0, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), + 0, 0)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL), + 1, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0)) + return 1; + if (test__muloti4(1, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), + make_ti(0x8000000000000000LL, 0x0000000000000001LL), 0)) + return 1; + if (test__muloti4(make_ti(0x8000000000000000LL, 0x0000000000000001LL), + 2, + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + if (test__muloti4(2, + make_ti(0x8000000000000000LL, 0x0000000000000001LL), + make_ti(0x8000000000000000LL, 0x0000000000000000LL), 1)) + return 1; + +#else + printf("skipped\n"); +#endif + return 0; +} -- cgit v1.2.3