[47c83bc] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
[47c83bc] | 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 |
|
---|
[6404aca] | 29 | /** @addtogroup kernel_genarch
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[9b2729c] | 35 | #include <genarch/softint/division.h>
|
---|
[47c83bc] | 36 |
|
---|
[5a6a42f] | 37 | #define ABSVAL(x) ((x) > 0 ? (x) : -(x))
|
---|
| 38 | #define SGN(x) ((x) >= 0 ? 1 : 0)
|
---|
| 39 |
|
---|
[7fe9c5b] | 40 | static unsigned int divandmod32(unsigned int a, unsigned int b,
|
---|
| 41 | unsigned int *remainder)
|
---|
[47c83bc] | 42 | {
|
---|
| 43 | unsigned int result;
|
---|
[1b20da0] | 44 | int steps = sizeof(unsigned int) * 8;
|
---|
[a35b458] | 45 |
|
---|
[47c83bc] | 46 | *remainder = 0;
|
---|
| 47 | result = 0;
|
---|
[a35b458] | 48 |
|
---|
[47c83bc] | 49 | if (b == 0) {
|
---|
| 50 | /* FIXME: division by zero */
|
---|
| 51 | return 0;
|
---|
| 52 | }
|
---|
[a35b458] | 53 |
|
---|
[7fe9c5b] | 54 | if (a < b) {
|
---|
[47c83bc] | 55 | *remainder = a;
|
---|
| 56 | return 0;
|
---|
| 57 | }
|
---|
[a35b458] | 58 |
|
---|
[7fe9c5b] | 59 | for (; steps > 0; steps--) {
|
---|
[47c83bc] | 60 | /* shift one bit to remainder */
|
---|
[3bacee1] | 61 | *remainder = ((*remainder) << 1) | ((a >> 31) & 0x1);
|
---|
[47c83bc] | 62 | result <<= 1;
|
---|
[a35b458] | 63 |
|
---|
[47c83bc] | 64 | if (*remainder >= b) {
|
---|
[7fe9c5b] | 65 | *remainder -= b;
|
---|
| 66 | result |= 0x1;
|
---|
[47c83bc] | 67 | }
|
---|
[9b2729c] | 68 | a <<= 1;
|
---|
[47c83bc] | 69 | }
|
---|
[a35b458] | 70 |
|
---|
[47c83bc] | 71 | return result;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[7fe9c5b] | 74 | static unsigned long long divandmod64(unsigned long long a,
|
---|
| 75 | unsigned long long b, unsigned long long *remainder)
|
---|
[47c83bc] | 76 | {
|
---|
[280a27e] | 77 | unsigned long long result;
|
---|
[5a6a42f] | 78 | int steps = sizeof(unsigned long long) * 8;
|
---|
[a35b458] | 79 |
|
---|
[47c83bc] | 80 | *remainder = 0;
|
---|
| 81 | result = 0;
|
---|
[a35b458] | 82 |
|
---|
[47c83bc] | 83 | if (b == 0) {
|
---|
| 84 | /* FIXME: division by zero */
|
---|
| 85 | return 0;
|
---|
| 86 | }
|
---|
[a35b458] | 87 |
|
---|
[7fe9c5b] | 88 | if (a < b) {
|
---|
[47c83bc] | 89 | *remainder = a;
|
---|
| 90 | return 0;
|
---|
| 91 | }
|
---|
[a35b458] | 92 |
|
---|
[7fe9c5b] | 93 | for (; steps > 0; steps--) {
|
---|
[47c83bc] | 94 | /* shift one bit to remainder */
|
---|
[7fe9c5b] | 95 | *remainder = ((*remainder) << 1) | ((a >> 63) & 0x1);
|
---|
[47c83bc] | 96 | result <<= 1;
|
---|
[a35b458] | 97 |
|
---|
[47c83bc] | 98 | if (*remainder >= b) {
|
---|
[7fe9c5b] | 99 | *remainder -= b;
|
---|
| 100 | result |= 0x1;
|
---|
[47c83bc] | 101 | }
|
---|
[9b2729c] | 102 | a <<= 1;
|
---|
[47c83bc] | 103 | }
|
---|
[a35b458] | 104 |
|
---|
[47c83bc] | 105 | return result;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /* 32bit integer division */
|
---|
[5a6a42f] | 109 | int __divsi3(int a, int b)
|
---|
[47c83bc] | 110 | {
|
---|
| 111 | unsigned int rem;
|
---|
[5a6a42f] | 112 | int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem);
|
---|
[a35b458] | 113 |
|
---|
[7fe9c5b] | 114 | if (SGN(a) == SGN(b))
|
---|
| 115 | return result;
|
---|
[a35b458] | 116 |
|
---|
[47c83bc] | 117 | return -result;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /* 64bit integer division */
|
---|
[5a6a42f] | 121 | long long __divdi3(long long a, long long b)
|
---|
[47c83bc] | 122 | {
|
---|
[280a27e] | 123 | unsigned long long rem;
|
---|
[5a6a42f] | 124 | long long result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
|
---|
[a35b458] | 125 |
|
---|
[7fe9c5b] | 126 | if (SGN(a) == SGN(b))
|
---|
| 127 | return result;
|
---|
[a35b458] | 128 |
|
---|
[47c83bc] | 129 | return -result;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /* 32bit unsigned integer division */
|
---|
| 133 | unsigned int __udivsi3(unsigned int a, unsigned int b)
|
---|
| 134 | {
|
---|
| 135 | unsigned int rem;
|
---|
| 136 | return divandmod32(a, b, &rem);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /* 64bit unsigned integer division */
|
---|
[280a27e] | 140 | unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
|
---|
[47c83bc] | 141 | {
|
---|
[5a6a42f] | 142 | unsigned long long rem;
|
---|
[47c83bc] | 143 | return divandmod64(a, b, &rem);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /* 32bit remainder of the signed division */
|
---|
| 147 | int __modsi3(int a, int b)
|
---|
| 148 | {
|
---|
| 149 | unsigned int rem;
|
---|
| 150 | divandmod32(a, b, &rem);
|
---|
[a35b458] | 151 |
|
---|
[9b2729c] | 152 | /* if divident is negative, remainder must be too */
|
---|
[5a6a42f] | 153 | if (!(SGN(a)))
|
---|
[7fe9c5b] | 154 | return -((int) rem);
|
---|
[a35b458] | 155 |
|
---|
[7fe9c5b] | 156 | return (int) rem;
|
---|
[47c83bc] | 157 | }
|
---|
| 158 |
|
---|
| 159 | /* 64bit remainder of the signed division */
|
---|
[5a6a42f] | 160 | long long __moddi3(long long a, long long b)
|
---|
[47c83bc] | 161 | {
|
---|
[280a27e] | 162 | unsigned long long rem;
|
---|
[47c83bc] | 163 | divandmod64(a, b, &rem);
|
---|
[a35b458] | 164 |
|
---|
[9b2729c] | 165 | /* if divident is negative, remainder must be too */
|
---|
[5a6a42f] | 166 | if (!(SGN(a)))
|
---|
[7fe9c5b] | 167 | return -((long long) rem);
|
---|
[a35b458] | 168 |
|
---|
[7fe9c5b] | 169 | return (long long) rem;
|
---|
[47c83bc] | 170 | }
|
---|
| 171 |
|
---|
| 172 | /* 32bit remainder of the unsigned division */
|
---|
| 173 | unsigned int __umodsi3(unsigned int a, unsigned int b)
|
---|
| 174 | {
|
---|
| 175 | unsigned int rem;
|
---|
| 176 | divandmod32(a, b, &rem);
|
---|
| 177 | return rem;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /* 64bit remainder of the unsigned division */
|
---|
[280a27e] | 181 | unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
|
---|
[47c83bc] | 182 | {
|
---|
[280a27e] | 183 | unsigned long long rem;
|
---|
[47c83bc] | 184 | divandmod64(a, b, &rem);
|
---|
| 185 | return rem;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[c7afcba7] | 188 | int __divmodsi3(int a, int b, int *c)
|
---|
| 189 | {
|
---|
| 190 | unsigned int rem;
|
---|
| 191 | int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem);
|
---|
[a35b458] | 192 |
|
---|
[c7afcba7] | 193 | if (SGN(a) == SGN(b)) {
|
---|
| 194 | *c = rem;
|
---|
| 195 | return result;
|
---|
| 196 | }
|
---|
[a35b458] | 197 |
|
---|
[c7afcba7] | 198 | *c = -rem;
|
---|
| 199 | return -result;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[5a6a42f] | 202 | unsigned int __udivmodsi3(unsigned int a, unsigned int b,
|
---|
| 203 | unsigned int *c)
|
---|
| 204 | {
|
---|
| 205 | return divandmod32(a, b, c);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[c7afcba7] | 208 | long long __divmoddi3(long long a, long long b, long long *c)
|
---|
| 209 | {
|
---|
| 210 | unsigned long long rem;
|
---|
| 211 | long long result = (int) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
|
---|
[a35b458] | 212 |
|
---|
[c7afcba7] | 213 | if (SGN(a) == SGN(b)) {
|
---|
| 214 | *c = rem;
|
---|
| 215 | return result;
|
---|
| 216 | }
|
---|
[a35b458] | 217 |
|
---|
[c7afcba7] | 218 | *c = -rem;
|
---|
| 219 | return -result;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[7fe9c5b] | 222 | unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b,
|
---|
| 223 | unsigned long long *c)
|
---|
[47c83bc] | 224 | {
|
---|
| 225 | return divandmod64(a, b, c);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[9abe2e5] | 228 | unsigned long long __udivmoddi4(unsigned long long a, unsigned long long b,
|
---|
| 229 | unsigned long long *c)
|
---|
| 230 | {
|
---|
| 231 | return divandmod64(a, b, c);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[06e1e95] | 234 | /** @}
|
---|
[b45c443] | 235 | */
|
---|