GCN libgcc.
[official-gcc.git] / libgcc / config / gcn / lib2-divmod-hi.c
blob15ea910ff5fcd11c822dc74e4ddf6beb30c76f6d
1 /* Copyright (C) 2012-2019 Free Software Foundation, Inc.
2 Contributed by Altera and Mentor Graphics, Inc.
4 This file is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
9 This file is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 Under Section 7 of GPL version 3, you are granted additional
15 permissions described in the GCC Runtime Library Exception, version
16 3.1, as published by the Free Software Foundation.
18 You should have received a copy of the GNU General Public License and
19 a copy of the GCC Runtime Library Exception along with this program;
20 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 <http://www.gnu.org/licenses/>. */
23 #include "lib2-gcn.h"
25 /* 16-bit HI divide and modulo as used in gcn. */
27 static UHItype
28 udivmodhi4 (UHItype num, UHItype den, word_type modwanted)
30 UHItype bit = 1;
31 UHItype res = 0;
33 while (den < num && bit && !(den & (1L<<15)))
35 den <<=1;
36 bit <<=1;
38 while (bit)
40 if (num >= den)
42 num -= den;
43 res |= bit;
45 bit >>=1;
46 den >>=1;
48 if (modwanted)
49 return num;
50 return res;
54 HItype
55 __divhi3 (HItype a, HItype b)
57 word_type neg = 0;
58 HItype res;
60 if (a < 0)
62 a = -a;
63 neg = !neg;
66 if (b < 0)
68 b = -b;
69 neg = !neg;
72 res = udivmodhi4 (a, b, 0);
74 if (neg)
75 res = -res;
77 return res;
81 HItype
82 __modhi3 (HItype a, HItype b)
84 word_type neg = 0;
85 HItype res;
87 if (a < 0)
89 a = -a;
90 neg = 1;
93 if (b < 0)
94 b = -b;
96 res = udivmodhi4 (a, b, 1);
98 if (neg)
99 res = -res;
101 return res;
105 UHItype
106 __udivhi3 (UHItype a, UHItype b)
108 return udivmodhi4 (a, b, 0);
112 UHItype
113 __umodhi3 (UHItype a, UHItype b)
115 return udivmodhi4 (a, b, 1);