xref: /aosp_15_r20/external/compiler-rt/lib/builtins/mulodi4.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /*===-- mulodi4.c - Implement __mulodi4 -----------------------------------===
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 __mulodi4 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 /* Returns: a * b */
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot /* Effects: sets *overflow to 1  if a * b overflows */
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot COMPILER_RT_ABI di_int
__mulodi4(di_int a,di_int b,int * overflow)22*7c3d14c8STreehugger Robot __mulodi4(di_int a, di_int b, int* overflow)
23*7c3d14c8STreehugger Robot {
24*7c3d14c8STreehugger Robot     const int N = (int)(sizeof(di_int) * CHAR_BIT);
25*7c3d14c8STreehugger Robot     const di_int MIN = (di_int)1 << (N-1);
26*7c3d14c8STreehugger Robot     const di_int MAX = ~MIN;
27*7c3d14c8STreehugger Robot     *overflow = 0;
28*7c3d14c8STreehugger Robot     di_int result = a * b;
29*7c3d14c8STreehugger Robot     if (a == MIN)
30*7c3d14c8STreehugger Robot     {
31*7c3d14c8STreehugger Robot         if (b != 0 && b != 1)
32*7c3d14c8STreehugger Robot 	    *overflow = 1;
33*7c3d14c8STreehugger Robot 	return result;
34*7c3d14c8STreehugger Robot     }
35*7c3d14c8STreehugger Robot     if (b == MIN)
36*7c3d14c8STreehugger Robot     {
37*7c3d14c8STreehugger Robot         if (a != 0 && a != 1)
38*7c3d14c8STreehugger Robot 	    *overflow = 1;
39*7c3d14c8STreehugger Robot         return result;
40*7c3d14c8STreehugger Robot     }
41*7c3d14c8STreehugger Robot     di_int sa = a >> (N - 1);
42*7c3d14c8STreehugger Robot     di_int abs_a = (a ^ sa) - sa;
43*7c3d14c8STreehugger Robot     di_int sb = b >> (N - 1);
44*7c3d14c8STreehugger Robot     di_int abs_b = (b ^ sb) - sb;
45*7c3d14c8STreehugger Robot     if (abs_a < 2 || abs_b < 2)
46*7c3d14c8STreehugger Robot         return result;
47*7c3d14c8STreehugger Robot     if (sa == sb)
48*7c3d14c8STreehugger Robot     {
49*7c3d14c8STreehugger Robot         if (abs_a > MAX / abs_b)
50*7c3d14c8STreehugger Robot             *overflow = 1;
51*7c3d14c8STreehugger Robot     }
52*7c3d14c8STreehugger Robot     else
53*7c3d14c8STreehugger Robot     {
54*7c3d14c8STreehugger Robot         if (abs_a > MIN / -abs_b)
55*7c3d14c8STreehugger Robot             *overflow = 1;
56*7c3d14c8STreehugger Robot     }
57*7c3d14c8STreehugger Robot     return result;
58*7c3d14c8STreehugger Robot }
59