Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame^] | 1 | //===-- multi3.c - Implement __multi3 -------------------------------------===// |
| 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 __multi3 for the compiler_rt library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #if __x86_64 |
| 15 | |
| 16 | #include "int_lib.h" |
| 17 | |
| 18 | // Returns: a * b |
| 19 | |
| 20 | static |
| 21 | ti_int |
| 22 | __mulddi3(du_int a, du_int b) |
| 23 | { |
| 24 | twords r; |
| 25 | const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2; |
| 26 | const du_int lower_mask = (du_int)~0 >> bits_in_dword_2; |
| 27 | r.low = (a & lower_mask) * (b & lower_mask); |
| 28 | du_int t = r.low >> bits_in_dword_2; |
| 29 | r.low &= lower_mask; |
| 30 | t += (a >> bits_in_dword_2) * (b & lower_mask); |
| 31 | r.low += (t & lower_mask) << bits_in_dword_2; |
| 32 | r.high = t >> bits_in_dword_2; |
| 33 | t = r.low >> bits_in_dword_2; |
| 34 | r.low &= lower_mask; |
| 35 | t += (b >> bits_in_dword_2) * (a & lower_mask); |
| 36 | r.low += (t & lower_mask) << bits_in_dword_2; |
| 37 | r.high += t >> bits_in_dword_2; |
| 38 | r.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2); |
| 39 | return r.all; |
| 40 | } |
| 41 | |
| 42 | // Returns: a * b |
| 43 | |
| 44 | ti_int |
| 45 | __multi3(ti_int a, ti_int b) |
| 46 | { |
| 47 | twords x; |
| 48 | x.all = a; |
| 49 | twords y; |
| 50 | y.all = b; |
| 51 | twords r; |
| 52 | r.all = __mulddi3(x.low, y.low); |
| 53 | r.high += x.high * y.low + x.low * y.high; |
| 54 | return r.all; |
| 55 | } |
| 56 | |
| 57 | #endif |