1*0b57cec5SDimitry Andric //===-- multi3.c - Implement __multi3 -------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements __multi3 for the compiler_rt library. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "int_lib.h" 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #ifdef CRT_HAS_128BIT 16*0b57cec5SDimitry Andric 17*0b57cec5SDimitry Andric // Returns: a * b 18*0b57cec5SDimitry Andric __mulddi3(du_int a,du_int b)19*0b57cec5SDimitry Andricstatic ti_int __mulddi3(du_int a, du_int b) { 20*0b57cec5SDimitry Andric twords r; 21*0b57cec5SDimitry Andric const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2; 22*0b57cec5SDimitry Andric const du_int lower_mask = (du_int)~0 >> bits_in_dword_2; 23*0b57cec5SDimitry Andric r.s.low = (a & lower_mask) * (b & lower_mask); 24*0b57cec5SDimitry Andric du_int t = r.s.low >> bits_in_dword_2; 25*0b57cec5SDimitry Andric r.s.low &= lower_mask; 26*0b57cec5SDimitry Andric t += (a >> bits_in_dword_2) * (b & lower_mask); 27*0b57cec5SDimitry Andric r.s.low += (t & lower_mask) << bits_in_dword_2; 28*0b57cec5SDimitry Andric r.s.high = t >> bits_in_dword_2; 29*0b57cec5SDimitry Andric t = r.s.low >> bits_in_dword_2; 30*0b57cec5SDimitry Andric r.s.low &= lower_mask; 31*0b57cec5SDimitry Andric t += (b >> bits_in_dword_2) * (a & lower_mask); 32*0b57cec5SDimitry Andric r.s.low += (t & lower_mask) << bits_in_dword_2; 33*0b57cec5SDimitry Andric r.s.high += t >> bits_in_dword_2; 34*0b57cec5SDimitry Andric r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2); 35*0b57cec5SDimitry Andric return r.all; 36*0b57cec5SDimitry Andric } 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric // Returns: a * b 39*0b57cec5SDimitry Andric __multi3(ti_int a,ti_int b)40*0b57cec5SDimitry AndricCOMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b) { 41*0b57cec5SDimitry Andric twords x; 42*0b57cec5SDimitry Andric x.all = a; 43*0b57cec5SDimitry Andric twords y; 44*0b57cec5SDimitry Andric y.all = b; 45*0b57cec5SDimitry Andric twords r; 46*0b57cec5SDimitry Andric r.all = __mulddi3(x.s.low, y.s.low); 47*0b57cec5SDimitry Andric r.s.high += x.s.high * y.s.low + x.s.low * y.s.high; 48*0b57cec5SDimitry Andric return r.all; 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric #endif // CRT_HAS_128BIT 52