1 1.1 joerg /* ===-- multi3.c - Implement __multi3 -------------------------------------=== 2 1.1 joerg * 3 1.1 joerg * The LLVM Compiler Infrastructure 4 1.1 joerg * 5 1.1 joerg * This file is dual licensed under the MIT and the University of Illinois Open 6 1.1 joerg * Source Licenses. See LICENSE.TXT for details. 7 1.1 joerg * 8 1.1 joerg * ===----------------------------------------------------------------------=== 9 1.1 joerg 10 1.1 joerg * This file implements __multi3 for the compiler_rt library. 11 1.1 joerg * 12 1.1 joerg * ===----------------------------------------------------------------------=== 13 1.1 joerg */ 14 1.1 joerg 15 1.1 joerg #include "int_lib.h" 16 1.1 joerg 17 1.1 joerg #ifdef CRT_HAS_128BIT 18 1.1 joerg 19 1.1 joerg /* Returns: a * b */ 20 1.1 joerg 21 1.1 joerg static 22 1.1 joerg ti_int 23 1.1 joerg __mulddi3(du_int a, du_int b) 24 1.1 joerg { 25 1.1 joerg twords r; 26 1.1 joerg const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2; 27 1.1 joerg const du_int lower_mask = (du_int)~0 >> bits_in_dword_2; 28 1.1 joerg r.s.low = (a & lower_mask) * (b & lower_mask); 29 1.1 joerg du_int t = r.s.low >> bits_in_dword_2; 30 1.1 joerg r.s.low &= lower_mask; 31 1.1 joerg t += (a >> bits_in_dword_2) * (b & lower_mask); 32 1.1 joerg r.s.low += (t & lower_mask) << bits_in_dword_2; 33 1.1 joerg r.s.high = t >> bits_in_dword_2; 34 1.1 joerg t = r.s.low >> bits_in_dword_2; 35 1.1 joerg r.s.low &= lower_mask; 36 1.1 joerg t += (b >> bits_in_dword_2) * (a & lower_mask); 37 1.1 joerg r.s.low += (t & lower_mask) << bits_in_dword_2; 38 1.1 joerg r.s.high += t >> bits_in_dword_2; 39 1.1 joerg r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2); 40 1.1 joerg return r.all; 41 1.1 joerg } 42 1.1 joerg 43 1.1 joerg /* Returns: a * b */ 44 1.1 joerg 45 1.1.1.2 joerg COMPILER_RT_ABI ti_int 46 1.1 joerg __multi3(ti_int a, ti_int b) 47 1.1 joerg { 48 1.1 joerg twords x; 49 1.1 joerg x.all = a; 50 1.1 joerg twords y; 51 1.1 joerg y.all = b; 52 1.1 joerg twords r; 53 1.1 joerg r.all = __mulddi3(x.s.low, y.s.low); 54 1.1 joerg r.s.high += x.s.high * y.s.low + x.s.low * y.s.high; 55 1.1 joerg return r.all; 56 1.1 joerg } 57 1.1 joerg 58 1.1 joerg #endif /* CRT_HAS_128BIT */ 59