xref: /netbsd-src/external/gpl3/gcc.old/dist/libgcc/config/arc/lib2funcs.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1*4c3eb207Smrg /* libgcc routines for ARC
2*4c3eb207Smrg    Copyright (C) 2019 Free Software Foundation, Inc.
3*4c3eb207Smrg 
4*4c3eb207Smrg    This file is part of GCC.
5*4c3eb207Smrg 
6*4c3eb207Smrg    GCC is free software; you can redistribute it and/or modify it
7*4c3eb207Smrg    under the terms of the GNU General Public License as published
8*4c3eb207Smrg    by the Free Software Foundation; either version 3, or (at your
9*4c3eb207Smrg    option) any later version.
10*4c3eb207Smrg 
11*4c3eb207Smrg    GCC is distributed in the hope that it will be useful, but WITHOUT
12*4c3eb207Smrg    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13*4c3eb207Smrg    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14*4c3eb207Smrg    License for more details.
15*4c3eb207Smrg 
16*4c3eb207Smrg    Under Section 7 of GPL version 3, you are granted additional
17*4c3eb207Smrg    permissions described in the GCC Runtime Library Exception, version
18*4c3eb207Smrg    3.1, as published by the Free Software Foundation.
19*4c3eb207Smrg 
20*4c3eb207Smrg    You should have received a copy of the GNU General Public License and
21*4c3eb207Smrg    a copy of the GCC Runtime Library Exception along with this program;
22*4c3eb207Smrg    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*4c3eb207Smrg    <http://www.gnu.org/licenses/>.  */
24*4c3eb207Smrg 
25*4c3eb207Smrg 
26*4c3eb207Smrg typedef          int  sint64_t   __attribute__ ((mode (DI)));
27*4c3eb207Smrg typedef unsigned int  uint64_t   __attribute__ ((mode (DI)));
28*4c3eb207Smrg typedef unsigned int  nint32_t   __attribute__ ((mode (SI)));
29*4c3eb207Smrg typedef int           word_t     __attribute__ ((mode (__word__)));
30*4c3eb207Smrg 
31*4c3eb207Smrg sint64_t __muldi3 (sint64_t, sint64_t);
32*4c3eb207Smrg nint32_t __umodsi3 (nint32_t, nint32_t);
33*4c3eb207Smrg 
34*4c3eb207Smrg #ifdef __ARC_RF16__
35*4c3eb207Smrg 
36*4c3eb207Smrg /* Generic multiplication procedure. No mpy operation involved.  */
37*4c3eb207Smrg sint64_t
__muldi3(sint64_t a,sint64_t b)38*4c3eb207Smrg __muldi3 (sint64_t a, sint64_t b)
39*4c3eb207Smrg {
40*4c3eb207Smrg   sint64_t res = 0;
41*4c3eb207Smrg   uint64_t cnt = a;
42*4c3eb207Smrg 
43*4c3eb207Smrg   while (cnt)
44*4c3eb207Smrg     {
45*4c3eb207Smrg       if (cnt & 1)
46*4c3eb207Smrg         res += b;
47*4c3eb207Smrg       b <<= 1;
48*4c3eb207Smrg       cnt >>= 1;
49*4c3eb207Smrg     }
50*4c3eb207Smrg   return res;
51*4c3eb207Smrg }
52*4c3eb207Smrg 
53*4c3eb207Smrg /* Unsigned 32bit integer division/modulus.  */
54*4c3eb207Smrg 
55*4c3eb207Smrg static inline __attribute__ ((__always_inline__))
56*4c3eb207Smrg nint32_t
udivmodsi4(nint32_t num,nint32_t den,word_t modwanted)57*4c3eb207Smrg udivmodsi4 (nint32_t num, nint32_t den, word_t modwanted)
58*4c3eb207Smrg {
59*4c3eb207Smrg   nint32_t bit = 1;
60*4c3eb207Smrg   nint32_t res = 0;
61*4c3eb207Smrg 
62*4c3eb207Smrg   while (den < num && bit && !(den & (1LL << 63)))
63*4c3eb207Smrg     {
64*4c3eb207Smrg       den <<= 1;
65*4c3eb207Smrg       bit <<= 1;
66*4c3eb207Smrg     }
67*4c3eb207Smrg   while (bit)
68*4c3eb207Smrg     {
69*4c3eb207Smrg       if (num >= den)
70*4c3eb207Smrg 	{
71*4c3eb207Smrg 	  num -= den;
72*4c3eb207Smrg 	  res |= bit;
73*4c3eb207Smrg 	}
74*4c3eb207Smrg       bit >>= 1;
75*4c3eb207Smrg       den >>= 1;
76*4c3eb207Smrg     }
77*4c3eb207Smrg   if (modwanted)
78*4c3eb207Smrg     return num;
79*4c3eb207Smrg   return res;
80*4c3eb207Smrg }
81*4c3eb207Smrg 
82*4c3eb207Smrg nint32_t
__umodsi3(nint32_t a,nint32_t b)83*4c3eb207Smrg __umodsi3 (nint32_t a, nint32_t b)
84*4c3eb207Smrg {
85*4c3eb207Smrg   return udivmodsi4 (a, b, 1);
86*4c3eb207Smrg }
87*4c3eb207Smrg 
88*4c3eb207Smrg #endif
89