[c3a2f0b] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
[c3a2f0b] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[231a60a] | 29 | /** @addtogroup softint
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * SW implementation of 32 and 64 bit division and modulo.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[8348846] | 37 | #include <alias.h>
|
---|
[c3a2f0b] | 38 | #include <division.h>
|
---|
| 39 |
|
---|
[5a6a42f] | 40 | #define ABSVAL(x) ((x) > 0 ? (x) : -(x))
|
---|
| 41 | #define SGN(x) ((x) >= 0 ? 1 : 0)
|
---|
| 42 |
|
---|
| 43 | static unsigned int divandmod32(unsigned int a, unsigned int b,
|
---|
| 44 | unsigned int *remainder)
|
---|
[c3a2f0b] | 45 | {
|
---|
| 46 | unsigned int result;
|
---|
| 47 | int steps = sizeof(unsigned int) * 8;
|
---|
| 48 |
|
---|
| 49 | *remainder = 0;
|
---|
| 50 | result = 0;
|
---|
| 51 |
|
---|
| 52 | if (b == 0) {
|
---|
| 53 | /* FIXME: division by zero */
|
---|
| 54 | return 0;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[5a6a42f] | 57 | if (a < b) {
|
---|
[c3a2f0b] | 58 | *remainder = a;
|
---|
| 59 | return 0;
|
---|
| 60 | }
|
---|
[5a6a42f] | 61 |
|
---|
| 62 | for (; steps > 0; steps--) {
|
---|
[c3a2f0b] | 63 | /* shift one bit to remainder */
|
---|
[5a6a42f] | 64 | *remainder = ((*remainder) << 1) | (( a >> 31) & 0x1);
|
---|
[c3a2f0b] | 65 | result <<= 1;
|
---|
| 66 |
|
---|
| 67 | if (*remainder >= b) {
|
---|
[5a6a42f] | 68 | *remainder -= b;
|
---|
| 69 | result |= 0x1;
|
---|
[c3a2f0b] | 70 | }
|
---|
| 71 | a <<= 1;
|
---|
| 72 | }
|
---|
[5a6a42f] | 73 |
|
---|
[c3a2f0b] | 74 | return result;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[5a6a42f] | 77 | static unsigned long long divandmod64(unsigned long long a,
|
---|
| 78 | unsigned long long b, unsigned long long *remainder)
|
---|
[c3a2f0b] | 79 | {
|
---|
| 80 | unsigned long long result;
|
---|
[5a6a42f] | 81 | int steps = sizeof(unsigned long long) * 8;
|
---|
[c3a2f0b] | 82 |
|
---|
| 83 | *remainder = 0;
|
---|
| 84 | result = 0;
|
---|
| 85 |
|
---|
| 86 | if (b == 0) {
|
---|
| 87 | /* FIXME: division by zero */
|
---|
| 88 | return 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[5a6a42f] | 91 | if (a < b) {
|
---|
[c3a2f0b] | 92 | *remainder = a;
|
---|
| 93 | return 0;
|
---|
| 94 | }
|
---|
[5a6a42f] | 95 |
|
---|
| 96 | for (; steps > 0; steps--) {
|
---|
[c3a2f0b] | 97 | /* shift one bit to remainder */
|
---|
[5a6a42f] | 98 | *remainder = ((*remainder) << 1) | ((a >> 63) & 0x1);
|
---|
[c3a2f0b] | 99 | result <<= 1;
|
---|
| 100 |
|
---|
| 101 | if (*remainder >= b) {
|
---|
[5a6a42f] | 102 | *remainder -= b;
|
---|
| 103 | result |= 0x1;
|
---|
[c3a2f0b] | 104 | }
|
---|
| 105 | a <<= 1;
|
---|
| 106 | }
|
---|
[5a6a42f] | 107 |
|
---|
[c3a2f0b] | 108 | return result;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /* 32bit integer division */
|
---|
[5a6a42f] | 112 | int __divsi3(int a, int b)
|
---|
[c3a2f0b] | 113 | {
|
---|
| 114 | unsigned int rem;
|
---|
[5a6a42f] | 115 | int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem);
|
---|
| 116 |
|
---|
| 117 | if (SGN(a) == SGN(b))
|
---|
| 118 | return result;
|
---|
[c3a2f0b] | 119 |
|
---|
| 120 | return -result;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /* 64bit integer division */
|
---|
[8348846] | 124 | long long __divti3(long long a, long long b)
|
---|
[c3a2f0b] | 125 | {
|
---|
| 126 | unsigned long long rem;
|
---|
[5a6a42f] | 127 | long long result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
|
---|
| 128 |
|
---|
| 129 | if (SGN(a) == SGN(b))
|
---|
| 130 | return result;
|
---|
[c3a2f0b] | 131 |
|
---|
| 132 | return -result;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /* 32bit unsigned integer division */
|
---|
| 136 | unsigned int __udivsi3(unsigned int a, unsigned int b)
|
---|
| 137 | {
|
---|
| 138 | unsigned int rem;
|
---|
| 139 | return divandmod32(a, b, &rem);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /* 64bit unsigned integer division */
|
---|
[8348846] | 143 | unsigned long long __udivti3(unsigned long long a, unsigned long long b)
|
---|
[c3a2f0b] | 144 | {
|
---|
[5a6a42f] | 145 | unsigned long long rem;
|
---|
[c3a2f0b] | 146 | return divandmod64(a, b, &rem);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /* 32bit remainder of the signed division */
|
---|
| 150 | int __modsi3(int a, int b)
|
---|
| 151 | {
|
---|
| 152 | unsigned int rem;
|
---|
| 153 | divandmod32(a, b, &rem);
|
---|
| 154 |
|
---|
| 155 | /* if divident is negative, remainder must be too */
|
---|
[5a6a42f] | 156 | if (!(SGN(a)))
|
---|
| 157 | return -((int) rem);
|
---|
[c3a2f0b] | 158 |
|
---|
[5a6a42f] | 159 | return (int) rem;
|
---|
[c3a2f0b] | 160 | }
|
---|
| 161 |
|
---|
| 162 | /* 64bit remainder of the signed division */
|
---|
[8348846] | 163 | long long __modti3(long long a, long long b)
|
---|
[c3a2f0b] | 164 | {
|
---|
| 165 | unsigned long long rem;
|
---|
| 166 | divandmod64(a, b, &rem);
|
---|
| 167 |
|
---|
| 168 | /* if divident is negative, remainder must be too */
|
---|
[5a6a42f] | 169 | if (!(SGN(a)))
|
---|
| 170 | return -((long long) rem);
|
---|
[c3a2f0b] | 171 |
|
---|
[5a6a42f] | 172 | return (long long) rem;
|
---|
[c3a2f0b] | 173 | }
|
---|
| 174 |
|
---|
| 175 | /* 32bit remainder of the unsigned division */
|
---|
| 176 | unsigned int __umodsi3(unsigned int a, unsigned int b)
|
---|
| 177 | {
|
---|
| 178 | unsigned int rem;
|
---|
| 179 | divandmod32(a, b, &rem);
|
---|
| 180 | return rem;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* 64bit remainder of the unsigned division */
|
---|
[8348846] | 184 | unsigned long long __umodti3(unsigned long long a, unsigned long long b)
|
---|
[c3a2f0b] | 185 | {
|
---|
| 186 | unsigned long long rem;
|
---|
| 187 | divandmod64(a, b, &rem);
|
---|
| 188 | return rem;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[88d5c1e] | 191 | int __divmodsi3(int a, int b, int *c)
|
---|
| 192 | {
|
---|
| 193 | unsigned int rem;
|
---|
| 194 | int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem);
|
---|
| 195 |
|
---|
| 196 | if (SGN(a) == SGN(b)) {
|
---|
| 197 | *c = rem;
|
---|
| 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | *c = -rem;
|
---|
| 202 | return -result;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[8348846] | 205 | int __divmodsi4(int a, int b, int *c)
|
---|
| 206 | {
|
---|
| 207 | return __divmodsi3(a, b, c);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[5a6a42f] | 210 | unsigned int __udivmodsi3(unsigned int a, unsigned int b,
|
---|
| 211 | unsigned int *c)
|
---|
| 212 | {
|
---|
| 213 | return divandmod32(a, b, c);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[8348846] | 216 | unsigned int __udivmodsi4(unsigned int a, unsigned int b,
|
---|
| 217 | unsigned int *c)
|
---|
| 218 | {
|
---|
| 219 | return divandmod32(a, b, c);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | long long __divmodti3(long long a, long long b, long long *c)
|
---|
[88d5c1e] | 223 | {
|
---|
| 224 | unsigned long long rem;
|
---|
| 225 | long long result = (int) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
|
---|
| 226 |
|
---|
| 227 | if (SGN(a) == SGN(b)) {
|
---|
| 228 | *c = rem;
|
---|
| 229 | return result;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | *c = -rem;
|
---|
| 233 | return -result;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[8348846] | 236 |
|
---|
| 237 | long long __divmodti4(long long a, long long b, long long *c)
|
---|
[9abe2e5] | 238 | {
|
---|
| 239 | unsigned long long rem;
|
---|
| 240 | long long result = (int) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
|
---|
| 241 |
|
---|
| 242 | if (SGN(a) == SGN(b)) {
|
---|
| 243 | *c = rem;
|
---|
| 244 | return result;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | *c = -rem;
|
---|
| 248 | return -result;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[8348846] | 251 | unsigned long long __udivmodti3(unsigned long long a, unsigned long long b,
|
---|
[5a6a42f] | 252 | unsigned long long *c)
|
---|
[c3a2f0b] | 253 | {
|
---|
| 254 | return divandmod64(a, b, c);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[8348846] | 257 | unsigned long long __udivmodti4(unsigned long long a, unsigned long long b,
|
---|
[9abe2e5] | 258 | unsigned long long *c)
|
---|
| 259 | {
|
---|
| 260 | return divandmod64(a, b, c);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[8348846] | 263 | long ALIAS(__div, i3);
|
---|
| 264 | long ALIAS(__mod, i3);
|
---|
| 265 | long ALIAS(__divmod, i3);
|
---|
| 266 | long ALIAS(__divmod, i4);
|
---|
| 267 | unsigned long ALIAS(__udiv, i3);
|
---|
| 268 | unsigned long ALIAS(__umod, i3);
|
---|
| 269 | unsigned long ALIAS(__udivmod, i3);
|
---|
| 270 | unsigned long ALIAS(__udivmod, i4);
|
---|
| 271 |
|
---|
[b2951e2] | 272 | /** @}
|
---|
| 273 | */
|
---|