summaryrefslogtreecommitdiff
path: root/libgcc/config/tilepro/softdivide.c
blob: 19bbab58452a46f5371a8f2e823cf061ab0f7893 (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
/* Division and remainder routines for Tile.
   Copyright (C) 2011-2017 Free Software Foundation, Inc.
   Contributed by Walter Lee (walt@tilera.com)

   This file 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.

   This file 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/>.  */

typedef int int32_t;
typedef unsigned uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;

/* Raise signal 8 (SIGFPE) with code 1 (FPE_INTDIV).  */
static inline void
raise_intdiv (void)
{
  asm ("{ raise; moveli zero, 8 + (1 << 6) }");
}


#ifndef __tilegx__
/*__udivsi3 - 32 bit integer unsigned divide  */
static inline uint32_t __attribute__ ((always_inline))
__udivsi3_inline (uint32_t dividend, uint32_t divisor)
{
  /* Divide out any power of two factor from dividend and divisor.
     Note that when dividing by zero the divisor will remain zero,
     which is all we need to detect that case below.  */
  const int power_of_two_factor = __insn_ctz (divisor);
  divisor >>= power_of_two_factor;
  dividend >>= power_of_two_factor;

  /* Checks for division by power of two or division by zero.  */
  if (divisor <= 1)
    {
      if (divisor == 0)
	{
	  raise_intdiv ();
	  return 0;
	}
      return dividend;
    }

  /* Compute (a / b) by repeatedly finding the largest N
     such that (b << N) <= a. For each such N, set bit N in the
     quotient, subtract (b << N) from a, and keep going. Think of this as
     the reverse of the "shift-and-add" that a multiply does. The values
     of N are precisely those shift counts.

     Finding N is easy. First, use clz(b) - clz(a) to find the N
     that lines up the high bit of (b << N) with the high bit of a.
     Any larger value of N would definitely make (b << N) > a,
     which is too big.

     Then, if (b << N) > a (because it has larger low bits), decrement
     N by one.  This adjustment will definitely make (b << N) less
     than a, because a's high bit is now one higher than b's.  */

  /* Precomputing the max_ values allows us to avoid a subtract
     in the inner loop and just right shift by clz(remainder).  */
  const int divisor_clz = __insn_clz (divisor);
  const uint32_t max_divisor = divisor << divisor_clz;
  const uint32_t max_qbit = 1 << divisor_clz;

  uint32_t quotient = 0;
  uint32_t remainder = dividend;

  while (remainder >= divisor)
    {
      int shift = __insn_clz (remainder);
      uint32_t scaled_divisor = max_divisor >> shift;
      uint32_t quotient_bit = max_qbit >> shift;

      int too_big = (scaled_divisor > remainder);
      scaled_divisor >>= too_big;
      quotient_bit >>= too_big;
      remainder -= scaled_divisor;
      quotient |= quotient_bit;
    }
  return quotient;
}
#endif /* !__tilegx__ */


/* __udivdi3 - 64 bit integer unsigned divide  */
static inline uint64_t __attribute__ ((always_inline))
__udivdi3_inline (uint64_t dividend, uint64_t divisor)
{
  /* Divide out any power of two factor from dividend and divisor.
     Note that when dividing by zero the divisor will remain zero,
     which is all we need to detect that case below.  */
  const int power_of_two_factor = __builtin_ctzll (divisor);
  divisor >>= power_of_two_factor;
  dividend >>= power_of_two_factor;

  /* Checks for division by power of two or division by zero.  */
  if (divisor <= 1)
    {
      if (divisor == 0)
	{
	  raise_intdiv ();
	  return 0;
	}
      return dividend;
    }

#ifndef __tilegx__
  if (((uint32_t) (dividend >> 32) | ((uint32_t) (divisor >> 32))) == 0)
    {
      /* Operands both fit in 32 bits, so use faster 32 bit algorithm.  */
      return __udivsi3_inline ((uint32_t) dividend, (uint32_t) divisor);
    }
#endif /* !__tilegx__ */

  /* See algorithm description in __udivsi3  */

  const int divisor_clz = __builtin_clzll (divisor);
  const uint64_t max_divisor = divisor << divisor_clz;
  const uint64_t max_qbit = 1ULL << divisor_clz;

  uint64_t quotient = 0;
  uint64_t remainder = dividend;

  while (remainder >= divisor)
    {
      int shift = __builtin_clzll (remainder);
      uint64_t scaled_divisor = max_divisor >> shift;
      uint64_t quotient_bit = max_qbit >> shift;

      int too_big = (scaled_divisor > remainder);
      scaled_divisor >>= too_big;
      quotient_bit >>= too_big;
      remainder -= scaled_divisor;
      quotient |= quotient_bit;
    }
  return quotient;
}


#ifndef __tilegx__
/* __umodsi3 - 32 bit integer unsigned modulo  */
static inline uint32_t __attribute__ ((always_inline))
__umodsi3_inline (uint32_t dividend, uint32_t divisor)
{
  /* Shortcircuit mod by a power of two (and catch mod by zero).  */
  const uint32_t mask = divisor - 1;
  if ((divisor & mask) == 0)
    {
      if (divisor == 0)
	{
	  raise_intdiv ();
	  return 0;
	}
      return dividend & mask;
    }

  /* We compute the remainder (a % b) by repeatedly subtracting off
     multiples of b from a until a < b. The key is that subtracting
     off a multiple of b does not affect the result mod b.

     To make the algorithm run efficiently, we need to subtract
     off a large multiple of b at each step. We subtract the largest
     (b << N) that is <= a.

     Finding N is easy. First, use clz(b) - clz(a) to find the N
     that lines up the high bit of (b << N) with the high bit of a.
     Any larger value of N would definitely make (b << N) > a,
     which is too big.

     Then, if (b << N) > a (because it has larger low bits), decrement
     N by one.  This adjustment will definitely make (b << N) less
     than a, because a's high bit is now one higher than b's.  */
  const uint32_t max_divisor = divisor << __insn_clz (divisor);

  uint32_t remainder = dividend;
  while (remainder >= divisor)
    {
      const int shift = __insn_clz (remainder);
      uint32_t scaled_divisor = max_divisor >> shift;
      scaled_divisor >>= (scaled_divisor > remainder);
      remainder -= scaled_divisor;
    }

  return remainder;
}
#endif /* !__tilegx__ */


/* __umoddi3 - 64 bit integer unsigned modulo  */
static inline uint64_t __attribute__ ((always_inline))
__umoddi3_inline (uint64_t dividend, uint64_t divisor)
{
#ifndef __tilegx__
  if (((uint32_t) (dividend >> 32) | ((uint32_t) (divisor >> 32))) == 0)
    {
      /* Operands both fit in 32 bits, so use faster 32 bit algorithm.  */
      return __umodsi3_inline ((uint32_t) dividend, (uint32_t) divisor);
    }
#endif /* !__tilegx__ */

  /* Shortcircuit mod by a power of two (and catch mod by zero).  */
  const uint64_t mask = divisor - 1;
  if ((divisor & mask) == 0)
    {
      if (divisor == 0)
	{
	  raise_intdiv ();
	  return 0;
	}
      return dividend & mask;
    }

  /* See algorithm description in __umodsi3  */
  const uint64_t max_divisor = divisor << __builtin_clzll (divisor);

  uint64_t remainder = dividend;
  while (remainder >= divisor)
    {
      const int shift = __builtin_clzll (remainder);
      uint64_t scaled_divisor = max_divisor >> shift;
      scaled_divisor >>= (scaled_divisor > remainder);
      remainder -= scaled_divisor;
    }

  return remainder;
}


uint32_t __udivsi3 (uint32_t dividend, uint32_t divisor);
#ifdef L_tile_udivsi3
uint32_t
__udivsi3 (uint32_t dividend, uint32_t divisor)
{
#ifndef __tilegx__
  return __udivsi3_inline (dividend, divisor);
#else /* !__tilegx__ */
  uint64_t n = __udivdi3_inline (((uint64_t) dividend), ((uint64_t) divisor));
  return (uint32_t) n;
#endif /* !__tilegx__ */
}
#endif

#define ABS(x) ((x) >= 0 ? (x) : -(x))

int32_t __divsi3 (int32_t dividend, int32_t divisor);
#ifdef L_tile_divsi3
/* __divsi3 - 32 bit integer signed divide  */
int32_t
__divsi3 (int32_t dividend, int32_t divisor)
{
#ifndef __tilegx__
  uint32_t n = __udivsi3_inline (ABS (dividend), ABS (divisor));
#else /* !__tilegx__ */
  uint64_t n =
    __udivdi3_inline (ABS ((int64_t) dividend), ABS ((int64_t) divisor));
#endif /* !__tilegx__ */
  if ((dividend ^ divisor) < 0)
    n = -n;
  return (int32_t) n;
}
#endif


uint64_t __udivdi3 (uint64_t dividend, uint64_t divisor);
#ifdef L_tile_udivdi3
uint64_t
__udivdi3 (uint64_t dividend, uint64_t divisor)
{
  return __udivdi3_inline (dividend, divisor);
}
#endif

/*__divdi3 - 64 bit integer signed divide  */
int64_t __divdi3 (int64_t dividend, int64_t divisor);
#ifdef L_tile_divdi3
int64_t
__divdi3 (int64_t dividend, int64_t divisor)
{
  uint64_t n = __udivdi3_inline (ABS (dividend), ABS (divisor));
  if ((dividend ^ divisor) < 0)
    n = -n;
  return (int64_t) n;
}
#endif


uint32_t __umodsi3 (uint32_t dividend, uint32_t divisor);
#ifdef L_tile_umodsi3
uint32_t
__umodsi3 (uint32_t dividend, uint32_t divisor)
{
#ifndef __tilegx__
  return __umodsi3_inline (dividend, divisor);
#else /* !__tilegx__ */
  return __umoddi3_inline ((uint64_t) dividend, (uint64_t) divisor);
#endif /* !__tilegx__ */
}
#endif


/* __modsi3 - 32 bit integer signed modulo  */
int32_t __modsi3 (int32_t dividend, int32_t divisor);
#ifdef L_tile_modsi3
int32_t
__modsi3 (int32_t dividend, int32_t divisor)
{
#ifndef __tilegx__
  uint32_t remainder = __umodsi3_inline (ABS (dividend), ABS (divisor));
#else /* !__tilegx__ */
  uint64_t remainder =
    __umoddi3_inline (ABS ((int64_t) dividend), ABS ((int64_t) divisor));
#endif /* !__tilegx__ */
  return (int32_t) ((dividend >= 0) ? remainder : -remainder);
}
#endif


uint64_t __umoddi3 (uint64_t dividend, uint64_t divisor);
#ifdef L_tile_umoddi3
uint64_t
__umoddi3 (uint64_t dividend, uint64_t divisor)
{
  return __umoddi3_inline (dividend, divisor);
}
#endif


/* __moddi3 - 64 bit integer signed modulo  */
int64_t __moddi3 (int64_t dividend, int64_t divisor);
#ifdef L_tile_moddi3
int64_t
__moddi3 (int64_t dividend, int64_t divisor)
{
  uint64_t remainder = __umoddi3_inline (ABS (dividend), ABS (divisor));
  return (int64_t) ((dividend >= 0) ? remainder : -remainder);
}
#endif