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