Changes in uspace/lib/softint/generic/division.c [9d58539:88d5c1e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/softint/generic/division.c
r9d58539 r88d5c1e 37 37 #include <division.h> 38 38 39 #define ABSVAL(x) ( (x) > 0 ? (x) : -(x)) 40 #define SGN(x) ( (x) >= 0 ? 1 : 0 ) 41 42 static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder) 39 #define ABSVAL(x) ((x) > 0 ? (x) : -(x)) 40 #define SGN(x) ((x) >= 0 ? 1 : 0) 41 42 static unsigned int divandmod32(unsigned int a, unsigned int b, 43 unsigned int *remainder) 43 44 { 44 45 unsigned int result; … … 53 54 } 54 55 55 if ( 56 if (a < b) { 56 57 *remainder = a; 57 58 return 0; 58 59 } 59 60 for ( 60 61 for (; steps > 0; steps--) { 61 62 /* shift one bit to remainder */ 62 *remainder = ( 63 *remainder = ((*remainder) << 1) | (( a >> 31) & 0x1); 63 64 result <<= 1; 64 65 65 66 if (*remainder >= b) { 66 67 67 *remainder -= b; 68 result |= 0x1; 68 69 } 69 70 a <<= 1; 70 71 } 71 72 72 73 return result; 73 74 } 74 75 75 76 static unsigned long long divandmod64(unsigned long long a,unsigned long long b, unsigned long long *remainder)76 static unsigned long long divandmod64(unsigned long long a, 77 unsigned long long b, unsigned long long *remainder) 77 78 { 78 79 unsigned long long result; 79 int steps = sizeof(unsigned long long) * 8; 80 int steps = sizeof(unsigned long long) * 8; 80 81 81 82 *remainder = 0; … … 87 88 } 88 89 89 if ( 90 if (a < b) { 90 91 *remainder = a; 91 92 return 0; 92 93 } 93 94 for ( 94 95 for (; steps > 0; steps--) { 95 96 /* shift one bit to remainder */ 96 *remainder = ( 97 *remainder = ((*remainder) << 1) | ((a >> 63) & 0x1); 97 98 result <<= 1; 98 99 99 100 if (*remainder >= b) { 100 101 101 *remainder -= b; 102 result |= 0x1; 102 103 } 103 104 a <<= 1; 104 105 } 105 106 106 107 return result; 107 108 } 108 109 109 110 /* 32bit integer division */ 110 int __divsi3(int a, int b) 111 { 112 unsigned int rem; 113 int result ;114 115 result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem);116 117 if ( SGN(a) == SGN(b)) return result;111 int __divsi3(int a, int b) 112 { 113 unsigned int rem; 114 int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem); 115 116 if (SGN(a) == SGN(b)) 117 return result; 118 118 119 return -result; 119 120 } 120 121 121 122 /* 64bit integer division */ 122 long long __divdi3(long long a, long long b) 123 { 124 unsigned long long rem; 125 long long result ;126 127 result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem);128 129 if ( SGN(a) == SGN(b)) return result;123 long long __divdi3(long long a, long long b) 124 { 125 unsigned long long rem; 126 long long result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem); 127 128 if (SGN(a) == SGN(b)) 129 return result; 130 130 131 return -result; 131 132 } … … 141 142 unsigned long long __udivdi3(unsigned long long a, unsigned long long b) 142 143 { 143 unsigned long long 144 unsigned long long rem; 144 145 return divandmod64(a, b, &rem); 145 146 } … … 152 153 153 154 /* if divident is negative, remainder must be too */ 154 if (!(SGN(a))) { 155 return -((int)rem); 156 } 157 158 return (int)rem; 155 if (!(SGN(a))) 156 return -((int) rem); 157 158 return (int) rem; 159 159 } 160 160 161 161 /* 64bit remainder of the signed division */ 162 long long __moddi3(long long a, longlong b)162 long long __moddi3(long long a, long long b) 163 163 { 164 164 unsigned long long rem; … … 166 166 167 167 /* if divident is negative, remainder must be too */ 168 if (!(SGN(a))) { 169 return -((long long)rem); 170 } 171 172 return (long long)rem; 168 if (!(SGN(a))) 169 return -((long long) rem); 170 171 return (long long) rem; 173 172 } 174 173 … … 189 188 } 190 189 191 unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c) 190 int __divmodsi3(int a, int b, int *c) 191 { 192 unsigned int rem; 193 int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem); 194 195 if (SGN(a) == SGN(b)) { 196 *c = rem; 197 return result; 198 } 199 200 *c = -rem; 201 return -result; 202 } 203 204 unsigned int __udivmodsi3(unsigned int a, unsigned int b, 205 unsigned int *c) 206 { 207 return divandmod32(a, b, c); 208 } 209 210 long long __divmoddi3(long long a, long long b, long long *c) 211 { 212 unsigned long long rem; 213 long long result = (int) divandmod64(ABSVAL(a), ABSVAL(b), &rem); 214 215 if (SGN(a) == SGN(b)) { 216 *c = rem; 217 return result; 218 } 219 220 *c = -rem; 221 return -result; 222 } 223 224 unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, 225 unsigned long long *c) 192 226 { 193 227 return divandmod64(a, b, c);
Note:
See TracChangeset
for help on using the changeset viewer.