[ASan/Win tests] Bring back -GS- as SEH tests fail otherwise
[blocksruntime.git] / lib / builtins / mulvti3.c
blobf4c7d1612ba93a9940826e02f84a705aa257a2d8
1 /* ===-- mulvti3.c - Implement __mulvti3 -----------------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __mulvti3 for the compiler_rt library.
12 * ===----------------------------------------------------------------------===
15 #include "int_lib.h"
17 #ifdef CRT_HAS_128BIT
19 /* Returns: a * b */
21 /* Effects: aborts if a * b overflows */
23 COMPILER_RT_ABI ti_int
24 __mulvti3(ti_int a, ti_int b)
26 const int N = (int)(sizeof(ti_int) * CHAR_BIT);
27 const ti_int MIN = (ti_int)1 << (N-1);
28 const ti_int MAX = ~MIN;
29 if (a == MIN)
31 if (b == 0 || b == 1)
32 return a * b;
33 compilerrt_abort();
35 if (b == MIN)
37 if (a == 0 || a == 1)
38 return a * b;
39 compilerrt_abort();
41 ti_int sa = a >> (N - 1);
42 ti_int abs_a = (a ^ sa) - sa;
43 ti_int sb = b >> (N - 1);
44 ti_int abs_b = (b ^ sb) - sb;
45 if (abs_a < 2 || abs_b < 2)
46 return a * b;
47 if (sa == sb)
49 if (abs_a > MAX / abs_b)
50 compilerrt_abort();
52 else
54 if (abs_a > MIN / -abs_b)
55 compilerrt_abort();
57 return a * b;
60 #endif /* CRT_HAS_128BIT */