]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/compiler-rt/lib/mulvsi3.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 / mulvsi3.c
1 /* ===-- mulvsi3.c - Implement __mulvsi3 -----------------------------------===
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 __mulvsi3 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 si_int
23 __mulvsi3(si_int a, si_int b)
24 {
25     const int N = (int)(sizeof(si_int) * CHAR_BIT);
26     const si_int MIN = (si_int)1 << (N-1);
27     const si_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     si_int sa = a >> (N - 1);
41     si_int abs_a = (a ^ sa) - sa;
42     si_int sb = b >> (N - 1);
43     si_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 }