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
7 /* Copyright (C) 2005, 2008, 2009, 2010 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 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 Under Section 7 of GPL version 3, you are granted additional
22 permissions described in the GCC Runtime Library Exception, version
23 3.1, as published by the Free Software Foundation.
25 You should have received a copy of the GNU General Public License and
26 a copy of the GCC Runtime Library Exception along with this program;
27 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
28 <http://www.gnu.org/licenses/>. */
32 #include "coretypes.h"
35 #ifdef HAVE_GAS_HIDDEN
36 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
38 #define ATTRIBUTE_HIDDEN
41 #ifndef MIN_UNITS_PER_WORD
42 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
45 #ifndef LIBGCC2_UNITS_PER_WORD
46 # if MIN_UNITS_PER_WORD > 4
47 # define LIBGCC2_UNITS_PER_WORD 8
48 # elif (MIN_UNITS_PER_WORD > 2 \
49 || (MIN_UNITS_PER_WORD > 1 && LONG_LONG_TYPE_SIZE > 32))
50 # define LIBGCC2_UNITS_PER_WORD 4
52 # define LIBGCC2_UNITS_PER_WORD MIN_UNITS_PER_WORD
56 #define word_type Wtype
61 /* These prototypes would normally live in libgcc2.h, but this can
62 only happen once the code below is integrated into libgcc2.c. */
64 extern USItype udivmodsi4 (USItype, USItype, word_type);
65 extern SItype __divsi3 (SItype, SItype);
66 extern SItype __modsi3 (SItype, SItype);
67 extern SItype __udivsi3 (SItype, SItype);
68 extern SItype __umodsi3 (SItype, SItype);
69 extern SItype __ashlsi3 (SItype, SItype);
70 extern SItype __ashrsi3 (SItype, SItype);
71 extern USItype __lshrsi3 (USItype, USItype);
72 extern int __popcounthi2 (UHWtype);
73 extern int __parityhi2 (UHWtype);
74 extern int __clzhi2 (UHWtype);
75 extern int __ctzhi2 (UHWtype);
78 #ifdef XSTORMY16_UDIVMODSI4
80 udivmodsi4 (USItype num, USItype den, word_type modwanted)
85 while (den < num && bit && !(den & (1L << 31)))
107 #ifdef XSTORMY16_DIVSI3
109 __divsi3 (SItype a, SItype b)
126 res = udivmodsi4 (a, b, 0);
135 #ifdef XSTORMY16_MODSI3
137 __modsi3 (SItype a, SItype b)
151 res = udivmodsi4 (a, b, 1);
160 #ifdef XSTORMY16_UDIVSI3
162 __udivsi3 (SItype a, SItype b)
164 return udivmodsi4 (a, b, 0);
168 #ifdef XSTORMY16_UMODSI3
170 __umodsi3 (SItype a, SItype b)
172 return udivmodsi4 (a, b, 1);
176 #ifdef XSTORMY16_ASHLSI3
178 __ashlsi3 (SItype a, SItype b)
186 for (i = (b & 0x7); i > 0; --i)
192 #ifdef XSTORMY16_ASHRSI3
194 __ashrsi3 (SItype a, SItype b)
202 for (i = (b & 0x7); i > 0; --i)
208 #ifdef XSTORMY16_LSHRSI3
210 __lshrsi3 (USItype a, USItype b)
218 for (i = (b & 0x7); i > 0; --i)
224 #ifdef XSTORMY16_POPCOUNTHI2
225 /* Returns the number of set bits in X.
226 FIXME: The return type really should be "unsigned int"
227 but this is not how the builtin is prototyped. */
229 __popcounthi2 (UHWtype x)
233 ret = __popcount_tab [x & 0xff];
234 ret += __popcount_tab [(x >> 8) & 0xff];
240 #ifdef XSTORMY16_PARITYHI2
241 /* Returns the number of set bits in X, modulo 2.
242 FIXME: The return type really should be "unsigned int"
243 but this is not how the builtin is prototyped. */
246 __parityhi2 (UHWtype x)
251 return (0x6996 >> x) & 1;
255 #ifdef XSTORMY16_CLZHI2
256 /* Returns the number of zero-bits from the most significant bit to the
257 first nonzero bit in X. Returns 16 for X == 0. Implemented as a
258 simple for loop in order to save space by removing the need for
260 FIXME: The return type really should be "unsigned int" but this is
261 not how the builtin is prototyped. */
268 unsigned int value = x;
270 for (c = 0, i = 1 << 15; i; i >>= 1, c++)
277 #ifdef XSTORMY16_CTZHI2
278 /* Returns the number of trailing zero bits in X.
279 FIXME: The return type really should be "signed int" since
280 ctz(0) returns -1, but this is not how the builtin is prototyped. */
285 /* This is cunning. It converts X into a number with only the one bit
286 set, the bit that was the least significant bit in X. From this we
287 can use the count_leading_zeros to compute the number of trailing
291 return 15 - __builtin_clz (x);
295 #ifdef XSTORMY16_FFSHI2
296 /* Returns one plus the index of the least significant 1-bit of X,
297 or if X is zero, returns zero. FIXME: The return type really
298 should be "unsigned int" but this is not how the builtin is
309 return 16 - __builtin_clz (u & - u);
313 #ifdef XSTORMY16_UCMPSI2
314 /* Performs an unsigned comparison of two 32-bit values: A and B.
315 If A is less than B, then 0 is returned. If A is greater than B,
316 then 2 is returned. Otherwise A and B are equal and 1 is returned. */
319 __ucmpsi2 (USItype a, USItype b)
321 word_type hi_a = (a << 16);
322 word_type hi_b = (b << 16);
326 word_type low_a = (a & 0xff);
327 word_type low_b = (b & 0xff);
329 return low_a < low_b ? 0 : (low_a > low_b ? 2 : 1);
332 return hi_a < hi_b ? 0 : 2;