Subversion Repositories HelenOS-historic

Rev

Rev 811 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
810 cejka 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
 
811 jermar 29
#include <genarch/softint/division.h>
810 cejka 30
 
31
#define ABSVAL(x) ( (x) > 0 ? (x) : -(x))
32
#define SGN(x) ( (x) >= 0 ? 1 : 0 )
33
 
34
static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder)
35
{
36
    unsigned int result;
811 jermar 37
    int steps = sizeof(unsigned int) * 8;
810 cejka 38
 
39
    *remainder = 0;
40
    result = 0;
41
 
42
    if (b == 0) {
43
        /* FIXME: division by zero */
44
        return 0;
45
    }
46
 
47
    if ( a < b) {
48
        *remainder = a;
49
        return 0;
50
    }
51
 
52
    for ( ; steps > 0; steps--) {
53
        /* shift one bit to remainder */
811 jermar 54
        *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1);
810 cejka 55
        result <<= 1;
56
 
57
        if (*remainder >= b) {
58
                *remainder -= b;
59
                result |= 0x1;
60
        }
811 jermar 61
        a <<= 1;
810 cejka 62
    }
63
 
64
    return result;
65
}
66
 
67
 
1196 cejka 68
static unsigned long long divandmod64(unsigned long long a, unsigned long long b, unsigned long long *remainder)
810 cejka 69
{
1196 cejka 70
    unsigned long long result;
71
    int steps = sizeof(unsigned long long) * 8;
810 cejka 72
 
73
    *remainder = 0;
74
    result = 0;
75
 
76
    if (b == 0) {
77
        /* FIXME: division by zero */
78
        return 0;
79
    }
80
 
81
    if ( a < b) {
82
        *remainder = a;
83
        return 0;
84
    }
85
 
86
    for ( ; steps > 0; steps--) {
87
        /* shift one bit to remainder */
811 jermar 88
        *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1);
810 cejka 89
        result <<= 1;
90
 
91
        if (*remainder >= b) {
92
                *remainder -= b;
93
                result |= 0x1;
94
        }
811 jermar 95
        a <<= 1;
810 cejka 96
    }
97
 
98
    return result;
99
}
100
 
101
/* 32bit integer division */
102
int __divsi3(int a, int b)
103
{
104
    unsigned int rem;
105
    int result;
106
 
107
    result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem);
108
 
109
    if ( SGN(a) == SGN(b)) return result;
110
    return -result;
111
}
112
 
113
/* 64bit integer division */
1196 cejka 114
long long __divdi3(long long a, long long b)
810 cejka 115
{
1196 cejka 116
    unsigned long long rem;
117
    long long result;
810 cejka 118
 
1196 cejka 119
    result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem);
810 cejka 120
 
121
    if ( SGN(a) == SGN(b)) return result;
122
    return -result;
123
}
124
 
125
/* 32bit unsigned integer division */
126
unsigned int __udivsi3(unsigned int a, unsigned int b)
127
{
128
    unsigned int rem;
129
    return divandmod32(a, b, &rem);
130
}
131
 
132
/* 64bit unsigned integer division */
1196 cejka 133
unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
810 cejka 134
{
1196 cejka 135
    unsigned long long  rem;
810 cejka 136
    return divandmod64(a, b, &rem);
137
}
138
 
139
/* 32bit remainder of the signed division */
140
int __modsi3(int a, int b)
141
{
142
    unsigned int rem;
143
    divandmod32(a, b, &rem);
144
 
811 jermar 145
    /* if divident is negative, remainder must be too */
810 cejka 146
    if (!(SGN(a))) {
147
        return -((int)rem);
148
    }
149
 
150
    return (int)rem;
151
}
152
 
153
/* 64bit remainder of the signed division */
1196 cejka 154
long long __moddi3(long long a,long  long b)
810 cejka 155
{
1196 cejka 156
    unsigned long long rem;
810 cejka 157
    divandmod64(a, b, &rem);
158
 
811 jermar 159
    /* if divident is negative, remainder must be too */
810 cejka 160
    if (!(SGN(a))) {
1196 cejka 161
        return -((long long)rem);
810 cejka 162
    }
163
 
1196 cejka 164
    return (long long)rem;
810 cejka 165
}
166
 
167
/* 32bit remainder of the unsigned division */
168
unsigned int __umodsi3(unsigned int a, unsigned int b)
169
{
170
    unsigned int rem;
171
    divandmod32(a, b, &rem);
172
    return rem;
173
}
174
 
175
/* 64bit remainder of the unsigned division */
1196 cejka 176
unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
810 cejka 177
{
1196 cejka 178
    unsigned long long rem;
810 cejka 179
    divandmod64(a, b, &rem);
180
    return rem;
181
}
182
 
1196 cejka 183
unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c)
810 cejka 184
{
185
    return divandmod64(a, b, c);
186
}
187
 
188