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