blob: 82efe8caafa8bebefe0d0210d2910648742a77bd [file] [log] [blame]
Daniel Dunbarb3a69012009-06-26 16:47:03 +00001//===-- mulvti3.c - Implement __mulvti3 -----------------------------------===//
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 __mulvti3 for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#if __x86_64
15
16#include "int_lib.h"
17#include <stdlib.h>
18
19// Returns: a * b
20
21// Effects: aborts if a * b overflows
22
23ti_int
24__mulvti3(ti_int a, ti_int b)
25{
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)
30 {
31 if (b == 0 || b == 1)
32 return a * b;
33 abort();
34 }
35 if (b == MIN)
36 {
37 if (a == 0 || a == 1)
38 return a * b;
39 abort();
40 }
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)
48 {
49 if (abs_a > MAX / abs_b)
50 abort();
51 }
52 else
53 {
54 if (abs_a > MIN / -abs_b)
55 abort();
56 }
57 return a * b;
58}
59
60#endif