ia64/xen-unstable
changeset 2077:65dffdb7668a
bitkeeper revision 1.1144.1.1 (41110cafHhGko2QnIa3NEGARA_3f3Q)
Addition of libc functions which allow to do 64 bit mod operations.
Addition of libc functions which allow to do 64 bit mod operations.
author | gm281@boulderdash.cl.cam.ac.uk |
---|---|
date | Wed Aug 04 16:19:59 2004 +0000 (2004-08-04) |
parents | a3e585a0c6ca |
children | c8aef6c7b1a5 |
files | xen/common/lib.c |
line diff
1.1 --- a/xen/common/lib.c Wed Aug 04 10:16:21 2004 +0000 1.2 +++ b/xen/common/lib.c Wed Aug 04 16:19:59 2004 +0000 1.3 @@ -354,10 +354,10 @@ u64 1.4 return (tmp.q); 1.5 } 1.6 1.7 - 1.8 /* 1.9 * Divide two signed quads. 1.10 * ??? if -1/2 should produce -1 on this machine, this code is wrong 1.11 + * (Grzegorz Milos) Note for the above: -1/2 is 0. And so it should. 1.12 */ 1.13 s64 1.14 __divdi3(s64 a, s64 b) 1.15 @@ -377,6 +377,7 @@ s64 1.16 return (neg ? -uq : uq); 1.17 } 1.18 1.19 + 1.20 /* 1.21 * Divide two unsigned quads. 1.22 */ 1.23 @@ -388,4 +389,55 @@ u64 1.24 return (__qdivrem(a, b, (u64 *)0)); 1.25 } 1.26 1.27 +/* 1.28 + * Remainder of unsigned quad division 1.29 + */ 1.30 +u64 __umoddi3(u64 a, u64 b) 1.31 +{ 1.32 + u64 rem; 1.33 + __qdivrem(a, b, &rem); 1.34 + return rem; 1.35 +} 1.36 + 1.37 +/* 1.38 + * Remainder of signed quad division. 1.39 + * The result of mod is not always equal to division 1.40 + * remainder. The following example shows the result for all 1.41 + * four possible cases: 1.42 + * 11 % 5 = 1 1.43 + * -11 % 5 = 4 1.44 + * 11 % -5 = -4 1.45 + * -11 % -5 = -1 1.46 + */ 1.47 +s64 __moddi3(s64 a, s64 b) 1.48 +{ 1.49 + u64 ua, ub, urem; 1.50 + int neg1, neg2; 1.51 + 1.52 + if (a < 0) 1.53 + ua = -(u64)a, neg1 = 1; 1.54 + else 1.55 + ua = a, neg1 = 0; 1.56 + 1.57 + if (b < 0) 1.58 + ub = -(u64)b, neg2 = 1; 1.59 + else 1.60 + ub = b, neg2 = 0; 1.61 + __qdivrem(ua, ub, &urem); 1.62 + 1.63 + /* There 4 different cases: */ 1.64 + if(neg1) 1.65 + { 1.66 + if(neg2) 1.67 + return -urem; 1.68 + else 1.69 + return ub - urem; 1.70 + } 1.71 + else 1.72 + if(neg2) 1.73 + return -ub + urem; 1.74 + else 1.75 + return urem; 1.76 +} 1.77 + 1.78 #endif /* BITS_PER_LONG == 32 */