diff options
author | Ed Schouten <ed@FreeBSD.org> | 2011-12-31 14:55:23 +0000 |
---|---|---|
committer | Ed Schouten <ed@FreeBSD.org> | 2011-12-31 14:55:23 +0000 |
commit | 219fb0488994a289f1087f7b3da18789e068da12 (patch) | |
tree | d59ced7e19afaab87432b7610faefff3080e8d2a /lib/mulosi4.c | |
parent | b0a04aaa595ba76468e521f12727a872d144d6d0 (diff) | |
download | src-219fb0488994a289f1087f7b3da18789e068da12.tar.gz src-219fb0488994a289f1087f7b3da18789e068da12.zip |
Import compiler-rt r147390.vendor/compiler-rt/compiler-rt-r147390
Notes
Notes:
svn path=/vendor/compiler-rt/dist/; revision=229109
svn path=/vendor/compiler-rt/compiler-rt-r147390/; revision=229110; tag=vendor/compiler-rt/compiler-rt-r147390
Diffstat (limited to 'lib/mulosi4.c')
-rw-r--r-- | lib/mulosi4.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/mulosi4.c b/lib/mulosi4.c new file mode 100644 index 000000000000..f3398d1fc7bd --- /dev/null +++ b/lib/mulosi4.c @@ -0,0 +1,58 @@ +/*===-- 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" + +/* 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; +} |