chiark / gitweb /
[PATCH] added klibc version 0.82 (cvs tree) to the udev tree.
[elogind.git] / klibc / klibc / arch / alpha / divide.c
1 #include <stdint.h>
2 #include <asm/gentrap.h>
3 #include <asm/pal.h>
4
5 #if BITS == 64
6 typedef uint64_t uint;
7 typedef int64_t  sint;
8 #else
9 typedef uint32_t uint;
10 typedef int32_t  sint;
11 #endif
12
13 #ifdef SIGNED
14 typedef sint xint;
15 #else
16 typedef uint xint;
17 #endif
18
19 xint NAME (uint num, uint den)
20 {
21   uint quot = 0, qbit = 1;
22   int minus = 0;
23   xint v;
24   
25   if ( den == 0 ) {
26     /* This is really $16, but $16 and $24 are exchanged by a script */
27     register unsigned long cause asm("$24") = GEN_INTDIV;
28     asm volatile("call_pal %0" :: "i" (PAL_gentrap), "r" (cause));
29     return 0;                   /* If trap returns... */
30   }
31
32 #if SIGNED
33   if ( (sint)(num^den) < 0 )
34     minus = 1;
35   if ( (sint)num < 0 ) num = -num;
36   if ( (sint)den < 0 ) den = -den;
37 #endif
38
39   /* Left-justify denominator and count shift */
40   while ( (sint)den >= 0 ) {
41     den <<= 1;
42     qbit <<= 1;
43   }
44
45   while ( qbit ) {
46     if ( den <= num ) {
47       num -= den;
48       quot += qbit;
49     }
50     den >>= 1;
51     qbit >>= 1;
52   }
53
54   v = (xint)(REM ? num : quot);
55   if ( minus ) v = -v;
56   return v;
57 }