10b57cec5SDimitry Andric //===-- multi3.c - Implement __multi3 -------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements __multi3 for the compiler_rt library. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "int_lib.h" 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #ifdef CRT_HAS_128BIT 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric // Returns: a * b 180b57cec5SDimitry Andric __mulddi3(du_int a,du_int b)190b57cec5SDimitry Andricstatic ti_int __mulddi3(du_int a, du_int b) { 200b57cec5SDimitry Andric twords r; 210b57cec5SDimitry Andric const int bits_in_dword_2 = (int)(sizeof(di_int) * CHAR_BIT) / 2; 220b57cec5SDimitry Andric const du_int lower_mask = (du_int)~0 >> bits_in_dword_2; 230b57cec5SDimitry Andric r.s.low = (a & lower_mask) * (b & lower_mask); 240b57cec5SDimitry Andric du_int t = r.s.low >> bits_in_dword_2; 250b57cec5SDimitry Andric r.s.low &= lower_mask; 260b57cec5SDimitry Andric t += (a >> bits_in_dword_2) * (b & lower_mask); 270b57cec5SDimitry Andric r.s.low += (t & lower_mask) << bits_in_dword_2; 280b57cec5SDimitry Andric r.s.high = t >> bits_in_dword_2; 290b57cec5SDimitry Andric t = r.s.low >> bits_in_dword_2; 300b57cec5SDimitry Andric r.s.low &= lower_mask; 310b57cec5SDimitry Andric t += (b >> bits_in_dword_2) * (a & lower_mask); 320b57cec5SDimitry Andric r.s.low += (t & lower_mask) << bits_in_dword_2; 330b57cec5SDimitry Andric r.s.high += t >> bits_in_dword_2; 340b57cec5SDimitry Andric r.s.high += (a >> bits_in_dword_2) * (b >> bits_in_dword_2); 350b57cec5SDimitry Andric return r.all; 360b57cec5SDimitry Andric } 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric // Returns: a * b 390b57cec5SDimitry Andric __multi3(ti_int a,ti_int b)400b57cec5SDimitry AndricCOMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b) { 410b57cec5SDimitry Andric twords x; 420b57cec5SDimitry Andric x.all = a; 430b57cec5SDimitry Andric twords y; 440b57cec5SDimitry Andric y.all = b; 450b57cec5SDimitry Andric twords r; 460b57cec5SDimitry Andric r.all = __mulddi3(x.s.low, y.s.low); 470b57cec5SDimitry Andric r.s.high += x.s.high * y.s.low + x.s.low * y.s.high; 480b57cec5SDimitry Andric return r.all; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric #endif // CRT_HAS_128BIT 52