Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / gcc / config / stormy16 / stormy16-lib2.c
blob2c60353062c6f0921140b5fdbfc179349f8feb78
1 /* This file contains 16-bit versions of some of the functions found in
2 libgcc2.c. Really libgcc ought to be moved out of the gcc directory
3 and into its own top level directory, and then split up into multiple
4 files. On this glorious day maybe this code can be integrated into
5 it too. */
7 /* Copyright (C) 2005, 2008 Free Software Foundation, Inc.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
14 version.
16 In addition to the permissions in the GNU General Public License, the
17 Free Software Foundation gives you unlimited permission to link the
18 compiled version of this file into combinations with other programs,
19 and to distribute those combinations without any restriction coming
20 from the use of this file. (The General Public License restrictions
21 do apply in other respects; for example, they cover modification of
22 the file, and distribution when not linked into a combine
23 executable.)
25 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
26 WARRANTY; without even the implied warranty of MERCHANTABILITY or
27 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 for more details.
30 You should have received a copy of the GNU General Public License
31 along with GCC; see the file COPYING. If not, write to the Free
32 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
33 02110-1301, USA. */
35 #include "tconfig.h"
36 #include "tsystem.h"
37 #include "coretypes.h"
38 #include "tm.h"
40 #ifdef HAVE_GAS_HIDDEN
41 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
42 #else
43 #define ATTRIBUTE_HIDDEN
44 #endif
46 #ifndef MIN_UNITS_PER_WORD
47 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
48 #endif
50 #ifndef LIBGCC2_UNITS_PER_WORD
51 # if MIN_UNITS_PER_WORD > 4
52 # define LIBGCC2_UNITS_PER_WORD 8
53 # elif (MIN_UNITS_PER_WORD > 2 \
54 || (MIN_UNITS_PER_WORD > 1 && LONG_LONG_TYPE_SIZE > 32))
55 # define LIBGCC2_UNITS_PER_WORD 4
56 # else
57 # define LIBGCC2_UNITS_PER_WORD MIN_UNITS_PER_WORD
58 # endif
59 #endif
61 #define word_type Wtype
63 #include "libgcc2.h"
64 #undef int
66 /* These prototypes would normally live in libgcc2.h, but this can
67 only happen once the code below is integrated into libgcc2.c. */
69 extern USItype udivmodsi4 (USItype, USItype, word_type);
70 extern SItype __divsi3 (SItype, SItype);
71 extern SItype __modsi3 (SItype, SItype);
72 extern SItype __udivsi3 (SItype, SItype);
73 extern SItype __umodsi3 (SItype, SItype);
74 extern SItype __ashlsi3 (SItype, SItype);
75 extern SItype __ashrsi3 (SItype, SItype);
76 extern USItype __lshrsi3 (USItype, USItype);
77 extern int __popcounthi2 (UHWtype);
78 extern int __parityhi2 (UHWtype);
79 extern int __clzhi2 (UHWtype);
80 extern int __ctzhi2 (UHWtype);
84 USItype
85 udivmodsi4 (USItype num, USItype den, word_type modwanted)
87 USItype bit = 1;
88 USItype res = 0;
90 while (den < num && bit && !(den & (1L << 31)))
92 den <<= 1;
93 bit <<= 1;
95 while (bit)
97 if (num >= den)
99 num -= den;
100 res |= bit;
102 bit >>= 1;
103 den >>= 1;
106 if (modwanted)
107 return num;
108 return res;
111 SItype
112 __divsi3 (SItype a, SItype b)
114 word_type neg = 0;
115 SItype res;
117 if (a < 0)
119 a = -a;
120 neg = !neg;
123 if (b < 0)
125 b = -b;
126 neg = !neg;
129 res = udivmodsi4 (a, b, 0);
131 if (neg)
132 res = -res;
134 return res;
137 SItype
138 __modsi3 (SItype a, SItype b)
140 word_type neg = 0;
141 SItype res;
143 if (a < 0)
145 a = -a;
146 neg = 1;
149 if (b < 0)
150 b = -b;
152 res = udivmodsi4 (a, b, 1);
154 if (neg)
155 res = -res;
157 return res;
160 SItype
161 __udivsi3 (SItype a, SItype b)
163 return udivmodsi4 (a, b, 0);
166 SItype
167 __umodsi3 (SItype a, SItype b)
169 return udivmodsi4 (a, b, 1);
172 SItype
173 __ashlsi3 (SItype a, SItype b)
175 word_type i;
177 if (b & 16)
178 a <<= 16;
179 if (b & 8)
180 a <<= 8;
181 for (i = (b & 0x7); i > 0; --i)
182 a <<= 1;
183 return a;
186 SItype
187 __ashrsi3 (SItype a, SItype b)
189 word_type i;
191 if (b & 16)
192 a >>= 16;
193 if (b & 8)
194 a >>= 8;
195 for (i = (b & 0x7); i > 0; --i)
196 a >>= 1;
197 return a;
200 USItype
201 __lshrsi3 (USItype a, USItype b)
203 word_type i;
205 if (b & 16)
206 a >>= 16;
207 if (b & 8)
208 a >>= 8;
209 for (i = (b & 0x7); i > 0; --i)
210 a >>= 1;
211 return a;
214 /* Returns the number of set bits in X.
215 FIXME: The return type really should be unsigned,
216 but this is not how the builtin is prototyped. */
218 __popcounthi2 (UHWtype x)
220 int ret;
222 ret = __popcount_tab [x & 0xff];
223 ret += __popcount_tab [(x >> 8) & 0xff];
225 return ret;
228 /* Returns the number of set bits in X, modulo 2.
229 FIXME: The return type really should be unsigned,
230 but this is not how the builtin is prototyped. */
233 __parityhi2 (UHWtype x)
235 x ^= x >> 8;
236 x ^= x >> 4;
237 x &= 0xf;
238 return (0x6996 >> x) & 1;
241 /* Returns the number of leading zero bits in X.
242 FIXME: The return type really should be unsigned,
243 but this is not how the builtin is prototyped. */
246 __clzhi2 (UHWtype x)
248 if (x > 0xff)
249 return 8 - __clz_tab[x >> 8];
250 return 16 - __clz_tab[x];
253 /* Returns the number of trailing zero bits in X.
254 FIXME: The return type really should be unsigned,
255 but this is not how the builtin is prototyped. */
258 __ctzhi2 (UHWtype x)
260 /* This is cunning. It converts X into a number with only the one bit
261 set, the bit was the least significant bit in X. From this we can
262 use the __clz_tab[] array to compute the number of trailing bits. */
263 x &= - x;
265 if (x > 0xff)
266 return __clz_tab[x >> 8] + 7;
267 return __clz_tab[x] - 1;