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