1/*===-- mulvdi3.c - Implement __mulvdi3 -----------------------------------===
2 *
3 *                     The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. 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
22di_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        abort();
33    }
34    if (b == MIN)
35    {
36        if (a == 0 || a == 1)
37            return a * b;
38        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            abort();
50    }
51    else
52    {
53        if (abs_a > MIN / -abs_b)
54            abort();
55    }
56    return a * b;
57}
58