summaryrefslogtreecommitdiff
path: root/gcc/config/stormy16/stormy16-lib2.c
blob: e3c16435471f2d32859dbbf9cdcc071dabd62ce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* This file contains 16-bit versions of some of the functions found in
   libgcc2.c.  Really libgcc ought to be moved out of the gcc directory
   and into its own top level directory, and then split up into multiple
   files.  On this glorious day maybe this code can be integrated into
   it too.  */

/* Copyright (C) 2005, 2008, 2009, 2010  Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   version.

   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include "tconfig.h"
#include "tsystem.h"
#include "coretypes.h"
#include "tm.h"

#ifdef HAVE_GAS_HIDDEN
#define ATTRIBUTE_HIDDEN  __attribute__ ((__visibility__ ("hidden")))
#else
#define ATTRIBUTE_HIDDEN
#endif

#ifndef MIN_UNITS_PER_WORD
#define MIN_UNITS_PER_WORD UNITS_PER_WORD
#endif

#ifndef LIBGCC2_UNITS_PER_WORD
# if MIN_UNITS_PER_WORD > 4
#  define LIBGCC2_UNITS_PER_WORD 8
# elif (MIN_UNITS_PER_WORD > 2 \
        || (MIN_UNITS_PER_WORD > 1 && LONG_LONG_TYPE_SIZE > 32))
#  define LIBGCC2_UNITS_PER_WORD 4
# else
#  define LIBGCC2_UNITS_PER_WORD MIN_UNITS_PER_WORD
# endif
#endif

#define word_type Wtype

#include "libgcc2.h"
#undef int

/* These prototypes would normally live in libgcc2.h, but this can
   only happen once the code below is integrated into libgcc2.c.  */

extern USItype udivmodsi4 (USItype, USItype, word_type);
extern SItype __divsi3 (SItype, SItype);
extern SItype __modsi3 (SItype, SItype);
extern SItype __udivsi3 (SItype, SItype);
extern SItype __umodsi3 (SItype, SItype);
extern SItype __ashlsi3 (SItype, SItype);
extern SItype __ashrsi3 (SItype, SItype);
extern USItype __lshrsi3 (USItype, USItype);
extern int __popcounthi2 (UHWtype);
extern int __parityhi2 (UHWtype);
extern int __clzhi2 (UHWtype);
extern int __ctzhi2 (UHWtype);


#ifdef XSTORMY16_UDIVMODSI4
USItype
udivmodsi4 (USItype num, USItype den, word_type modwanted)
{
  USItype bit = 1;
  USItype res = 0;

  while (den < num && bit && !(den & (1L << 31)))
    {
      den <<= 1;
      bit <<= 1;
    }
  while (bit)
    {
      if (num >= den)
	{
	  num -= den;
	  res |= bit;
	}
      bit >>= 1;
      den >>= 1;
    }

  if (modwanted)
    return num;
  return res;
}
#endif

#ifdef XSTORMY16_DIVSI3
SItype
__divsi3 (SItype a, SItype b)
{
  word_type neg = 0;
  SItype res;

  if (a < 0)
    {
      a = -a;
      neg = !neg;
    }

  if (b < 0)
    {
      b = -b;
      neg = !neg;
    }

  res = udivmodsi4 (a, b, 0);

  if (neg)
    res = -res;

  return res;
}
#endif

#ifdef XSTORMY16_MODSI3
SItype
__modsi3 (SItype a, SItype b)
{
  word_type neg = 0;
  SItype res;

  if (a < 0)
    {
      a = -a;
      neg = 1;
    }

  if (b < 0)
    b = -b;

  res = udivmodsi4 (a, b, 1);

  if (neg)
    res = -res;

  return res;
}
#endif

#ifdef XSTORMY16_UDIVSI3
SItype
__udivsi3 (SItype a, SItype b)
{
  return udivmodsi4 (a, b, 0);
}
#endif

#ifdef XSTORMY16_UMODSI3
SItype
__umodsi3 (SItype a, SItype b)
{
  return udivmodsi4 (a, b, 1);
}
#endif

#ifdef XSTORMY16_ASHLSI3
SItype
__ashlsi3 (SItype a, SItype b)
{
  word_type i;
  
  if (b & 16)
    a <<= 16;
  if (b & 8)
    a <<= 8;
  for (i = (b & 0x7); i > 0; --i)
    a <<= 1;
  return a;
}
#endif

#ifdef XSTORMY16_ASHRSI3
SItype
__ashrsi3 (SItype a, SItype b)
{
  word_type i;
  
  if (b & 16)
    a >>= 16;
  if (b & 8)
    a >>= 8;
  for (i = (b & 0x7); i > 0; --i)
    a >>= 1;
  return a;
}
#endif

#ifdef XSTORMY16_LSHRSI3
USItype
__lshrsi3 (USItype a, USItype b)
{
  word_type i;
  
  if (b & 16)
    a >>= 16;
  if (b & 8)
    a >>= 8;
  for (i = (b & 0x7); i > 0; --i)
    a >>= 1;
  return a;
}
#endif

#ifdef XSTORMY16_POPCOUNTHI2
/* Returns the number of set bits in X.
   FIXME:  The return type really should be "unsigned int"
   but this is not how the builtin is prototyped.  */
int
__popcounthi2 (UHWtype x)
{
  int ret;

  ret = __popcount_tab [x & 0xff];
  ret += __popcount_tab [(x >> 8) & 0xff];

  return ret;
}
#endif

#ifdef XSTORMY16_PARITYHI2
/* Returns the number of set bits in X, modulo 2.
   FIXME:  The return type really should be "unsigned int"
   but this is not how the builtin is prototyped.  */

int
__parityhi2 (UHWtype x)
{
  x ^= x >> 8;
  x ^= x >> 4;
  x &= 0xf;
  return (0x6996 >> x) & 1;
}
#endif

#ifdef XSTORMY16_CLZHI2
/* Returns the number of zero-bits from the most significant bit to the
   first nonzero bit in X.  Returns 16 for X == 0.  Implemented as a
   simple for loop in order to save space by removing the need for
   the __clz_tab array.
   FIXME:  The return type really should be "unsigned int" but this is
   not how the builtin is prototyped.  */
#undef unsigned
int
__clzhi2 (UHWtype x)
{
  unsigned int i;
  unsigned int c;
  unsigned int value = x;

  for (c = 0, i = 1 << 15; i; i >>= 1, c++)
    if (i & value)
      break;
  return c;
}
#endif

#ifdef XSTORMY16_CTZHI2
/* Returns the number of trailing zero bits in X.
   FIXME:  The return type really should be "signed int" since
   ctz(0) returns -1, but this is not how the builtin is prototyped.  */

int
__ctzhi2 (UHWtype x)
{
  /* This is cunning.  It converts X into a number with only the one bit
     set, the bit that was the least significant bit in X.  From this we
     can use the count_leading_zeros to compute the number of trailing
     bits.  */
  x &= - x;

  return 15 - __builtin_clz (x);
}
#endif

#ifdef XSTORMY16_FFSHI2
/* Returns one plus the index of the least significant 1-bit of X,
   or if X is zero, returns zero.  FIXME:  The return type really
   should be "unsigned int" but this is not how the builtin is
   prototyped.  */

int
__ffshi2 (UHWtype u)
{
  UHWtype count;

  if (u == 0)
    return 0;

  return 16 - __builtin_clz (u & - u);
}
#endif

#ifdef XSTORMY16_UCMPSI2
/* Performs an unsigned comparison of two 32-bit values: A and B.
   If A is less than B, then 0 is returned.  If A is greater than B,
   then 2 is returned.  Otherwise A and B are equal and 1 is returned.  */

word_type
__ucmpsi2 (USItype a, USItype b)
{
  word_type hi_a = (a >> 16);
  word_type hi_b = (b >> 16);

  if (hi_a == hi_b)
    {
      word_type low_a = (a & 0xffff);
      word_type low_b = (b & 0xffff);

      return low_a < low_b ? 0 : (low_a > low_b ? 2 : 1);
    }

  return hi_a < hi_b ? 0 : 2;
}
#endif

#ifdef XSTORMY16_CMPSI2
/* Performs an signed comparison of two 32-bit values: A and B.
   If A is less than B, then 0 is returned.  If A is greater than B,
   then 2 is returned.  Otherwise A and B are equal and 1 is returned.  */

word_type
__cmpsi2 (SItype a, SItype b)
{
  word_type hi_a = (a >> 16);
  word_type hi_b = (b >> 16);

  if (hi_a == hi_b)
    {
      word_type low_a = (a & 0xffff);
      word_type low_b = (b & 0xffff);

      return low_a < low_b ? 0 : (low_a > low_b ? 2 : 1);
    }

  return hi_a < hi_b ? 0 : 2;
}
#endif