| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Josef Cejka
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup genarch
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <genarch/softint/division.h>
|
|---|
| 36 |
|
|---|
| 37 | #define ABSVAL(x) ((x) > 0 ? (x) : -(x))
|
|---|
| 38 | #define SGN(x) ((x) >= 0 ? 1 : 0)
|
|---|
| 39 |
|
|---|
| 40 | static unsigned int divandmod32(unsigned int a, unsigned int b,
|
|---|
| 41 | unsigned int *remainder)
|
|---|
| 42 | {
|
|---|
| 43 | unsigned int result;
|
|---|
| 44 | int steps = sizeof(unsigned int) * 8;
|
|---|
| 45 |
|
|---|
| 46 | *remainder = 0;
|
|---|
| 47 | result = 0;
|
|---|
| 48 |
|
|---|
| 49 | if (b == 0) {
|
|---|
| 50 | /* FIXME: division by zero */
|
|---|
| 51 | return 0;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | if (a < b) {
|
|---|
| 55 | *remainder = a;
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | for (; steps > 0; steps--) {
|
|---|
| 60 | /* shift one bit to remainder */
|
|---|
| 61 | *remainder = ((*remainder) << 1) | (( a >> 31) & 0x1);
|
|---|
| 62 | result <<= 1;
|
|---|
| 63 |
|
|---|
| 64 | if (*remainder >= b) {
|
|---|
| 65 | *remainder -= b;
|
|---|
| 66 | result |= 0x1;
|
|---|
| 67 | }
|
|---|
| 68 | a <<= 1;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return result;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | static unsigned long long divandmod64(unsigned long long a,
|
|---|
| 75 | unsigned long long b, unsigned long long *remainder)
|
|---|
| 76 | {
|
|---|
| 77 | unsigned long long result;
|
|---|
| 78 | int steps = sizeof(unsigned long long) * 8;
|
|---|
| 79 |
|
|---|
| 80 | *remainder = 0;
|
|---|
| 81 | result = 0;
|
|---|
| 82 |
|
|---|
| 83 | if (b == 0) {
|
|---|
| 84 | /* FIXME: division by zero */
|
|---|
| 85 | return 0;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if (a < b) {
|
|---|
| 89 | *remainder = a;
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | for (; steps > 0; steps--) {
|
|---|
| 94 | /* shift one bit to remainder */
|
|---|
| 95 | *remainder = ((*remainder) << 1) | ((a >> 63) & 0x1);
|
|---|
| 96 | result <<= 1;
|
|---|
| 97 |
|
|---|
| 98 | if (*remainder >= b) {
|
|---|
| 99 | *remainder -= b;
|
|---|
| 100 | result |= 0x1;
|
|---|
| 101 | }
|
|---|
| 102 | a <<= 1;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return result;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /* 32bit integer division */
|
|---|
| 109 | int __divsi3(int a, int b)
|
|---|
| 110 | {
|
|---|
| 111 | unsigned int rem;
|
|---|
| 112 | int result = (int) divandmod32(ABSVAL(a), ABSVAL(b), &rem);
|
|---|
| 113 |
|
|---|
| 114 | if (SGN(a) == SGN(b))
|
|---|
| 115 | return result;
|
|---|
| 116 |
|
|---|
| 117 | return -result;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /* 64bit integer division */
|
|---|
| 121 | long long __divdi3(long long a, long long b)
|
|---|
| 122 | {
|
|---|
| 123 | unsigned long long rem;
|
|---|
| 124 | long long result = (long long) divandmod64(ABSVAL(a), ABSVAL(b), &rem);
|
|---|
| 125 |
|
|---|
| 126 | if (SGN(a) == SGN(b))
|
|---|
| 127 | return result;
|
|---|
| 128 |
|
|---|
| 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 */
|
|---|
| 140 | unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
|
|---|
| 141 | {
|
|---|
| 142 | unsigned long long rem;
|
|---|
| 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);
|
|---|
| 151 |
|
|---|
| 152 | /* if divident is negative, remainder must be too */
|
|---|
| 153 | if (!(SGN(a)))
|
|---|
| 154 | return -((int) rem);
|
|---|
| 155 |
|
|---|
| 156 | return (int) rem;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /* 64bit remainder of the signed division */
|
|---|
| 160 | long long __moddi3(long long a, long long b)
|
|---|
| 161 | {
|
|---|
| 162 | unsigned long long rem;
|
|---|
| 163 | divandmod64(a, b, &rem);
|
|---|
| 164 |
|
|---|
| 165 | /* if divident is negative, remainder must be too */
|
|---|
| 166 | if (!(SGN(a)))
|
|---|
| 167 | return -((long long) rem);
|
|---|
| 168 |
|
|---|
| 169 | return (long long) rem;
|
|---|
| 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 */
|
|---|
| 181 | unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
|
|---|
| 182 | {
|
|---|
| 183 | unsigned long long rem;
|
|---|
| 184 | divandmod64(a, b, &rem);
|
|---|
| 185 | return rem;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | unsigned int __udivmodsi3(unsigned int a, unsigned int b,
|
|---|
| 189 | unsigned int *c)
|
|---|
| 190 | {
|
|---|
| 191 | return divandmod32(a, b, c);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b,
|
|---|
| 195 | unsigned long long *c)
|
|---|
| 196 | {
|
|---|
| 197 | return divandmod64(a, b, c);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** @}
|
|---|
| 201 | */
|
|---|