]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/compiler-rt/lib/mulvdi3.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / compiler-rt / lib / mulvdi3.c
1 /*===-- mulvdi3.c - Implement __mulvdi3 -----------------------------------===
2  *
3  *                     The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===----------------------------------------------------------------------===
9  *
10  * This file implements __mulvdi3 for the compiler_rt library.
11  *
12  * ===----------------------------------------------------------------------===
13  */
14
15 #include "int_lib.h"
16 #include <stdlib.h>
17
18 /* Returns: a * b */
19
20 /* Effects: aborts if a * b overflows */
21
22 di_int
23 __mulvdi3(di_int a, di_int b)
24 {
25     const int N = (int)(sizeof(di_int) * CHAR_BIT);
26     const di_int MIN = (di_int)1 << (N-1);
27     const di_int MAX = ~MIN;
28     if (a == MIN)
29     {
30         if (b == 0 || b == 1)
31             return a * b;
32         compilerrt_abort();
33     }
34     if (b == MIN)
35     {
36         if (a == 0 || a == 1)
37             return a * b;
38         compilerrt_abort();
39     }
40     di_int sa = a >> (N - 1);
41     di_int abs_a = (a ^ sa) - sa;
42     di_int sb = b >> (N - 1);
43     di_int abs_b = (b ^ sb) - sb;
44     if (abs_a < 2 || abs_b < 2)
45         return a * b;
46     if (sa == sb)
47     {
48         if (abs_a > MAX / abs_b)
49             compilerrt_abort();
50     }
51     else
52     {
53         if (abs_a > MIN / -abs_b)
54             compilerrt_abort();
55     }
56     return a * b;
57 }