Subversion Repositories HelenOS-historic

Rev

Rev 991 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 991 Rev 1653
1
/*
1
/*
2
 * Copyright (C) 2006 Josef Cejka
2
 * Copyright (C) 2006 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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
/** @addtogroup softint SoftInt
-
 
30
 * @brief   Software implementation of basic arithmetic operations.
-
 
31
 * @{
-
 
32
 */
-
 
33
/**
-
 
34
 * @file
-
 
35
 * SW implementation of 32 and 64 bit division and modulo.
-
 
36
 */
-
 
37
 
29
#include <division.h>
38
#include <division.h>
30
 
39
 
31
#define ABSVAL(x) ( (x) > 0 ? (x) : -(x))
40
#define ABSVAL(x) ( (x) > 0 ? (x) : -(x))
32
#define SGN(x) ( (x) >= 0 ? 1 : 0 )
41
#define SGN(x) ( (x) >= 0 ? 1 : 0 )
33
                     
42
                     
34
static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder)
43
static unsigned int divandmod32(unsigned int a, unsigned int b, unsigned int *remainder)
35
{
44
{
36
    unsigned int result;
45
    unsigned int result;
37
    int steps = sizeof(unsigned int) * 8;
46
    int steps = sizeof(unsigned int) * 8;
38
   
47
   
39
    *remainder = 0;
48
    *remainder = 0;
40
    result = 0;
49
    result = 0;
41
   
50
   
42
    if (b == 0) {
51
    if (b == 0) {
43
        /* FIXME: division by zero */
52
        /* FIXME: division by zero */
44
        return 0;
53
        return 0;
45
    }
54
    }
46
   
55
   
47
    if ( a < b) {
56
    if ( a < b) {
48
        *remainder = a;
57
        *remainder = a;
49
        return 0;
58
        return 0;
50
    }
59
    }
51
 
60
 
52
    for ( ; steps > 0; steps--) {
61
    for ( ; steps > 0; steps--) {
53
        /* shift one bit to remainder */
62
        /* shift one bit to remainder */
54
        *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1);
63
        *remainder = ( (*remainder) << 1) | (( a >> 31) & 0x1);
55
        result <<= 1;
64
        result <<= 1;
56
       
65
       
57
        if (*remainder >= b) {
66
        if (*remainder >= b) {
58
                *remainder -= b;
67
                *remainder -= b;
59
                result |= 0x1;
68
                result |= 0x1;
60
        }
69
        }
61
        a <<= 1;
70
        a <<= 1;
62
    }
71
    }
63
 
72
 
64
    return result;
73
    return result;
65
}
74
}
66
 
75
 
67
 
76
 
68
static unsigned long long divandmod64(unsigned long long a, unsigned long long b, unsigned long long *remainder)
77
static unsigned long long divandmod64(unsigned long long a, unsigned long long b, unsigned long long *remainder)
69
{
78
{
70
    unsigned long long result;
79
    unsigned long long result;
71
    int steps = sizeof(unsigned long long) * 8;
80
    int steps = sizeof(unsigned long long) * 8;
72
   
81
   
73
    *remainder = 0;
82
    *remainder = 0;
74
    result = 0;
83
    result = 0;
75
   
84
   
76
    if (b == 0) {
85
    if (b == 0) {
77
        /* FIXME: division by zero */
86
        /* FIXME: division by zero */
78
        return 0;
87
        return 0;
79
    }
88
    }
80
   
89
   
81
    if ( a < b) {
90
    if ( a < b) {
82
        *remainder = a;
91
        *remainder = a;
83
        return 0;
92
        return 0;
84
    }
93
    }
85
 
94
 
86
    for ( ; steps > 0; steps--) {
95
    for ( ; steps > 0; steps--) {
87
        /* shift one bit to remainder */
96
        /* shift one bit to remainder */
88
        *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1);
97
        *remainder = ( (*remainder) << 1) | ((a >> 63) & 0x1);
89
        result <<= 1;
98
        result <<= 1;
90
       
99
       
91
        if (*remainder >= b) {
100
        if (*remainder >= b) {
92
                *remainder -= b;
101
                *remainder -= b;
93
                result |= 0x1;
102
                result |= 0x1;
94
        }
103
        }
95
        a <<= 1;
104
        a <<= 1;
96
    }
105
    }
97
 
106
 
98
    return result;
107
    return result;
99
}
108
}
100
 
109
 
101
/* 32bit integer division */
110
/* 32bit integer division */
102
int __divsi3(int a, int b)
111
int __divsi3(int a, int b)
103
{
112
{
104
    unsigned int rem;
113
    unsigned int rem;
105
    int result;
114
    int result;
106
   
115
   
107
    result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem);
116
    result = (int)divandmod32(ABSVAL(a), ABSVAL(b), &rem);
108
 
117
 
109
    if ( SGN(a) == SGN(b)) return result;
118
    if ( SGN(a) == SGN(b)) return result;
110
    return -result;
119
    return -result;
111
}
120
}
112
 
121
 
113
/* 64bit integer division */
122
/* 64bit integer division */
114
long long __divdi3(long long a, long long b)
123
long long __divdi3(long long a, long long b)
115
{
124
{
116
    unsigned long long rem;
125
    unsigned long long rem;
117
    long long result;
126
    long long result;
118
   
127
   
119
    result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem);
128
    result = (long long)divandmod64(ABSVAL(a), ABSVAL(b), &rem);
120
 
129
 
121
    if ( SGN(a) == SGN(b)) return result;
130
    if ( SGN(a) == SGN(b)) return result;
122
    return -result;
131
    return -result;
123
}
132
}
124
 
133
 
125
/* 32bit unsigned integer division */
134
/* 32bit unsigned integer division */
126
unsigned int __udivsi3(unsigned int a, unsigned int b)
135
unsigned int __udivsi3(unsigned int a, unsigned int b)
127
{
136
{
128
    unsigned int rem;
137
    unsigned int rem;
129
    return divandmod32(a, b, &rem);
138
    return divandmod32(a, b, &rem);
130
}
139
}
131
 
140
 
132
/* 64bit unsigned integer division */
141
/* 64bit unsigned integer division */
133
unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
142
unsigned long long __udivdi3(unsigned long long a, unsigned long long b)
134
{
143
{
135
    unsigned long long  rem;
144
    unsigned long long  rem;
136
    return divandmod64(a, b, &rem);
145
    return divandmod64(a, b, &rem);
137
}
146
}
138
 
147
 
139
/* 32bit remainder of the signed division */
148
/* 32bit remainder of the signed division */
140
int __modsi3(int a, int b)
149
int __modsi3(int a, int b)
141
{
150
{
142
    unsigned int rem;
151
    unsigned int rem;
143
    divandmod32(a, b, &rem);
152
    divandmod32(a, b, &rem);
144
   
153
   
145
    /* if divident is negative, remainder must be too */
154
    /* if divident is negative, remainder must be too */
146
    if (!(SGN(a))) {
155
    if (!(SGN(a))) {
147
        return -((int)rem);
156
        return -((int)rem);
148
    }
157
    }
149
   
158
   
150
    return (int)rem;
159
    return (int)rem;
151
}
160
}
152
 
161
 
153
/* 64bit remainder of the signed division */
162
/* 64bit remainder of the signed division */
154
long long __moddi3(long long a,long  long b)
163
long long __moddi3(long long a,long  long b)
155
{
164
{
156
    unsigned long long rem;
165
    unsigned long long rem;
157
    divandmod64(a, b, &rem);
166
    divandmod64(a, b, &rem);
158
   
167
   
159
    /* if divident is negative, remainder must be too */
168
    /* if divident is negative, remainder must be too */
160
    if (!(SGN(a))) {
169
    if (!(SGN(a))) {
161
        return -((long long)rem);
170
        return -((long long)rem);
162
    }
171
    }
163
   
172
   
164
    return (long long)rem;
173
    return (long long)rem;
165
}
174
}
166
 
175
 
167
/* 32bit remainder of the unsigned division */
176
/* 32bit remainder of the unsigned division */
168
unsigned int __umodsi3(unsigned int a, unsigned int b)
177
unsigned int __umodsi3(unsigned int a, unsigned int b)
169
{
178
{
170
    unsigned int rem;
179
    unsigned int rem;
171
    divandmod32(a, b, &rem);
180
    divandmod32(a, b, &rem);
172
    return rem;
181
    return rem;
173
}
182
}
174
 
183
 
175
/* 64bit remainder of the unsigned division */
184
/* 64bit remainder of the unsigned division */
176
unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
185
unsigned long long __umoddi3(unsigned long long a, unsigned long long b)
177
{
186
{
178
    unsigned long long rem;
187
    unsigned long long rem;
179
    divandmod64(a, b, &rem);
188
    divandmod64(a, b, &rem);
180
    return rem;
189
    return rem;
181
}
190
}
182
 
191
 
183
unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c)
192
unsigned long long __udivmoddi3(unsigned long long a, unsigned long long b, unsigned long long *c)
184
{
193
{
185
    return divandmod64(a, b, c);
194
    return divandmod64(a, b, c);
186
}
195
}
187
 
196
 
-
 
197
/** @}
188
 
198
 */
189
 
199