[llvm-commits] [compiler-rt] r133284 - in /compiler-rt/trunk: README.txt lib/mulodi4.c lib/mulosi4.c lib/muloti4.c make/platform/clang_darwin.mk make/platform/darwin_bni.mk test/Unit/mulodi4_test.c test/Unit/mulosi4_test.c test/Unit/muloti4_test.c
Eric Christopher
echristo at apple.com
Fri Jun 17 13:27:10 PDT 2011
Forgot to mention. A chunk of this patch came from Nick Kledzik :)
-eric
On Jun 17, 2011, at 1:17 PM, Eric Christopher wrote:
> Author: echristo
> Date: Fri Jun 17 15:17:05 2011
> New Revision: 133284
>
> URL: http://llvm.org/viewvc/llvm-project?rev=133284&view=rev
> Log:
> Implement mulo<mode>4 for use in signed overflow checking.
>
> Fixes rdar://9219742 and rdar://9218244
>
> Added:
> compiler-rt/trunk/lib/mulodi4.c
> compiler-rt/trunk/lib/mulosi4.c
> compiler-rt/trunk/lib/muloti4.c
> compiler-rt/trunk/test/Unit/mulodi4_test.c
> compiler-rt/trunk/test/Unit/mulosi4_test.c
> compiler-rt/trunk/test/Unit/muloti4_test.c
> Modified:
> compiler-rt/trunk/README.txt
> compiler-rt/trunk/make/platform/clang_darwin.mk
> compiler-rt/trunk/make/platform/darwin_bni.mk
>
> Modified: compiler-rt/trunk/README.txt
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/README.txt?rev=133284&r1=133283&r2=133284&view=diff
> ==============================================================================
> --- compiler-rt/trunk/README.txt (original)
> +++ compiler-rt/trunk/README.txt Fri Jun 17 15:17:05 2011
> @@ -106,6 +106,15 @@
> 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
>
> Added: compiler-rt/trunk/lib/mulodi4.c
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/mulodi4.c?rev=133284&view=auto
> ==============================================================================
> --- compiler-rt/trunk/lib/mulodi4.c (added)
> +++ compiler-rt/trunk/lib/mulodi4.c Fri Jun 17 15:17:05 2011
> @@ -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 <stdlib.h>
> +
> +/* 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;
> +}
>
> Added: compiler-rt/trunk/lib/mulosi4.c
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/mulosi4.c?rev=133284&view=auto
> ==============================================================================
> --- compiler-rt/trunk/lib/mulosi4.c (added)
> +++ compiler-rt/trunk/lib/mulosi4.c Fri Jun 17 15:17:05 2011
> @@ -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 <stdlib.h>
> +
> +/* 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;
> +}
>
> Added: compiler-rt/trunk/lib/muloti4.c
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/muloti4.c?rev=133284&view=auto
> ==============================================================================
> --- compiler-rt/trunk/lib/muloti4.c (added)
> +++ compiler-rt/trunk/lib/muloti4.c Fri Jun 17 15:17:05 2011
> @@ -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 <stdlib.h>
> +
> +/* 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
>
> Modified: compiler-rt/trunk/make/platform/clang_darwin.mk
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/make/platform/clang_darwin.mk?rev=133284&r1=133283&r2=133284&view=diff
> ==============================================================================
> --- compiler-rt/trunk/make/platform/clang_darwin.mk (original)
> +++ compiler-rt/trunk/make/platform/clang_darwin.mk Fri Jun 17 15:17:05 2011
> @@ -42,6 +42,11 @@
> 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.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 @@
> 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 @@
> switch16 switch32 switch8 switchu8 \
> save_vfp_d8_d15_regs restore_vfp_d8_d15_regs
>
> +FUNCTIONS.osx := mulosi4 mulodi4 muloti4
> +
> CCKEXT_COMMON_FUNCTIONS := \
> absvdi2 \
> absvsi2 \
>
> Modified: compiler-rt/trunk/make/platform/darwin_bni.mk
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/make/platform/darwin_bni.mk?rev=133284&r1=133283&r2=133284&view=diff
> ==============================================================================
> --- compiler-rt/trunk/make/platform/darwin_bni.mk (original)
> +++ compiler-rt/trunk/make/platform/darwin_bni.mk Fri Jun 17 15:17:05 2011
> @@ -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 @@
>
>
> 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 @@
> 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 @@
> 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 @@
> truncdfsf2 \
> modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4 \
> switch8 switchu8 switch16 switch32 \
> - sync_synchronize
> + sync_synchronize
>
> FUNCTIONS.armv6 := $(FUNCTIONS) \
> comparedf2 comparesf2 \
> @@ -81,7 +81,7 @@
> 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 @@
> nedf2vfp nesf2vfp \
> subdf3vfp subsf3vfp truncdfsf2vfp unorddf2vfp unordsf2vfp \
> modsi3 umodsi3 udivsi3 divsi3 udivmodsi4 divmodsi4
> -
>
> Added: compiler-rt/trunk/test/Unit/mulodi4_test.c
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/Unit/mulodi4_test.c?rev=133284&view=auto
> ==============================================================================
> --- compiler-rt/trunk/test/Unit/mulodi4_test.c (added)
> +++ compiler-rt/trunk/test/Unit/mulodi4_test.c Fri Jun 17 15:17:05 2011
> @@ -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 <stdio.h>
> +
> +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;
> +}
>
> Added: compiler-rt/trunk/test/Unit/mulosi4_test.c
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/Unit/mulosi4_test.c?rev=133284&view=auto
> ==============================================================================
> --- compiler-rt/trunk/test/Unit/mulosi4_test.c (added)
> +++ compiler-rt/trunk/test/Unit/mulosi4_test.c Fri Jun 17 15:17:05 2011
> @@ -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 <stdio.h>
> +
> +// 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;
> +}
>
> Added: compiler-rt/trunk/test/Unit/muloti4_test.c
> URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/Unit/muloti4_test.c?rev=133284&view=auto
> ==============================================================================
> --- compiler-rt/trunk/test/Unit/muloti4_test.c (added)
> +++ compiler-rt/trunk/test/Unit/muloti4_test.c Fri Jun 17 15:17:05 2011
> @@ -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 <stdio.h>
> +
> +// 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;
> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list