1 /* ===-- mulvti3.c - Implement __mulvti3 -----------------------------------=== 2 * 3 * The LLVM Compiler Infrastructure 4 * 5 * This file is dual licensed under the MIT and the University of Illinois Open 6 * Source Licenses. See LICENSE.TXT for details. 7 * 8 * ===----------------------------------------------------------------------=== 9 * 10 * This file implements __mulvti3 for the compiler_rt library. 11 * 12 * ===----------------------------------------------------------------------=== 13 */ 14 15 #include "int_lib.h" 16 17 #ifdef CRT_HAS_128BIT 18 19 /* Returns: a * b */ 20 21 /* Effects: aborts if a * b overflows */ 22 23 COMPILER_RT_ABI ti_int __mulvti3(ti_int a,ti_int b)24__mulvti3(ti_int a, ti_int b) 25 { 26 const int N = (int)(sizeof(ti_int) * CHAR_BIT); 27 const ti_int MIN = (ti_int)1 << (N-1); 28 const ti_int MAX = ~MIN; 29 if (a == MIN) 30 { 31 if (b == 0 || b == 1) 32 return a * b; 33 compilerrt_abort(); 34 } 35 if (b == MIN) 36 { 37 if (a == 0 || a == 1) 38 return a * b; 39 compilerrt_abort(); 40 } 41 ti_int sa = a >> (N - 1); 42 ti_int abs_a = (a ^ sa) - sa; 43 ti_int sb = b >> (N - 1); 44 ti_int abs_b = (b ^ sb) - sb; 45 if (abs_a < 2 || abs_b < 2) 46 return a * b; 47 if (sa == sb) 48 { 49 if (abs_a > MAX / abs_b) 50 compilerrt_abort(); 51 } 52 else 53 { 54 if (abs_a > MIN / -abs_b) 55 compilerrt_abort(); 56 } 57 return a * b; 58 } 59 60 #endif /* CRT_HAS_128BIT */ 61