Subversion Repositories HelenOS-historic

Rev

Rev 810 | Rev 1196 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 810 Rev 811
Line 24... Line 24...
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <softint/softint.h>
29
#include <genarch/softint/division.h>
30
 
30
 
31
#define ABSVAL(x) ( (x) > 0 ? (x) : -(x))
31
#define ABSVAL(x) ( (x) > 0 ? (x) : -(x))
32
#define SGN(x) ( (x) >= 0 ? 1 : 0 )
32
#define SGN(x) ( (x) >= 0 ? 1 : 0 )
33
                     
33
                     
34
static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder)
34
static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder)
35
{
35
{
36
    unsigned int result;
36
    unsigned int result;
37
    int steps = sizeof(unsigned int);
37
    int steps = sizeof(unsigned int) * 8;
38
   
38
   
39
    *remainder = 0;
39
    *remainder = 0;
40
    result = 0;
40
    result = 0;
41
   
41
   
42
    if (b == 0) {
42
    if (b == 0) {
Line 49... Line 49...
49
        return 0;
49
        return 0;
50
    }
50
    }
51
 
51
 
52
    for ( ; steps > 0; steps--) {
52
    for ( ; steps > 0; steps--) {
53
        /* shift one bit to remainder */
53
        /* shift one bit to remainder */
54
        *remainder = ( (*remainder) << 1) | (( divident >> 31) & 0x1);
54
        *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1);
55
        result <<= 1;
55
        result <<= 1;
56
       
56
       
57
        if (*remainder >= b) {
57
        if (*remainder >= b) {
58
                *remainder -= b;
58
                *remainder -= b;
59
                result |= 0x1;
59
                result |= 0x1;
60
        }
60
        }
61
        divident <<= 1;
61
        a <<= 1;
62
    }
62
    }
63
 
63
 
64
    return result;
64
    return result;
65
}
65
}
66
 
66
 
67
 
67
 
68
static unsigned long divandmod64(unsigned long a, unsigned long b, unsigned long *remainder)
68
static unsigned long divandmod64(unsigned long a, unsigned long b, unsigned long *remainder)
69
{
69
{
70
    unsigned long result;
70
    unsigned long result;
71
    int steps = sizeof(unsigned long);
71
    int steps = sizeof(unsigned long) * 8;
72
   
72
   
73
    *remainder = 0;
73
    *remainder = 0;
74
    result = 0;
74
    result = 0;
75
   
75
   
76
    if (b == 0) {
76
    if (b == 0) {
Line 83... Line 83...
83
        return 0;
83
        return 0;
84
    }
84
    }
85
 
85
 
86
    for ( ; steps > 0; steps--) {
86
    for ( ; steps > 0; steps--) {
87
        /* shift one bit to remainder */
87
        /* shift one bit to remainder */
88
        *remainder = ( (*remainder) << 1) | (( divident >> 63) & 0x1);
88
        *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1);
89
        result <<= 1;
89
        result <<= 1;
90
       
90
       
91
        if (*remainder >= b) {
91
        if (*remainder >= b) {
92
                *remainder -= b;
92
                *remainder -= b;
93
                result |= 0x1;
93
                result |= 0x1;
94
        }
94
        }
95
        divident <<= 1;
95
        a <<= 1;
96
    }
96
    }
97
 
97
 
98
    return result;
98
    return result;
99
}
99
}
100
 
100
 
Line 140... Line 140...
140
int __modsi3(int a, int b)
140
int __modsi3(int a, int b)
141
{
141
{
142
    unsigned int rem;
142
    unsigned int rem;
143
    divandmod32(a, b, &rem);
143
    divandmod32(a, b, &rem);
144
   
144
   
145
    /* if divident is negative, remainder must be too*/
145
    /* if divident is negative, remainder must be too */
146
    if (!(SGN(a))) {
146
    if (!(SGN(a))) {
147
        return -((int)rem);
147
        return -((int)rem);
148
    }
148
    }
149
   
149
   
150
    return (int)rem;
150
    return (int)rem;
Line 154... Line 154...
154
long __moddi3(long a, long b)
154
long __moddi3(long a, long b)
155
{
155
{
156
    unsigned long rem;
156
    unsigned long rem;
157
    divandmod64(a, b, &rem);
157
    divandmod64(a, b, &rem);
158
   
158
   
159
    /* if divident is negative, remainder must be too*/
159
    /* if divident is negative, remainder must be too */
160
    if (!(SGN(a))) {
160
    if (!(SGN(a))) {
161
        return -((long)rem);
161
        return -((long)rem);
162
    }
162
    }
163
   
163
   
164
    return (long)rem;
164
    return (long)rem;