Subversion Repositories HelenOS-historic

Rev

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

Rev 1653 Rev 1657
1
/*
1
/*
2
 * Copyright (C) 2005 Josef Cejka
2
 * Copyright (C) 2005 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 softfloat 
-
 
30
 * @{
-
 
31
 */
-
 
32
/** @file
-
 
33
 */
-
 
34
 
29
#include<sftypes.h>
35
#include<sftypes.h>
30
#include<common.h>
36
#include<common.h>
31
 
37
 
32
/* Table for fast leading zeroes counting */
38
/* Table for fast leading zeroes counting */
33
char zeroTable[256] = {
39
char zeroTable[256] = {
34
    8, 7, 7, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, \
40
    8, 7, 7, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, \
35
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 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 0, 0, 0, 0, 0, 0, 0, \
51
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
46
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
52
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
47
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
53
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
48
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
54
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
49
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
55
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
50
};
56
};
51
 
57
 
52
 
58
 
53
 
59
 
54
/** Take fraction shifted by 10 bits to left, round it, normalize it and detect exceptions
60
/** Take fraction shifted by 10 bits to left, round it, normalize it and detect exceptions
55
 * @param cexp exponent with bias
61
 * @param cexp exponent with bias
56
 * @param cfrac fraction shifted 10 places left with added hidden bit
62
 * @param cfrac fraction shifted 10 places left with added hidden bit
57
 * @param sign
63
 * @param sign
58
 * @return valied float64
64
 * @return valied float64
59
 */
65
 */
60
float64 finishFloat64(int32_t cexp, uint64_t cfrac, char sign)
66
float64 finishFloat64(int32_t cexp, uint64_t cfrac, char sign)
61
{
67
{
62
    float64 result;
68
    float64 result;
63
 
69
 
64
    result.parts.sign = sign;
70
    result.parts.sign = sign;
65
 
71
 
66
    /* find first nonzero digit and shift result and detect possibly underflow */
72
    /* find first nonzero digit and shift result and detect possibly underflow */
67
    while ((cexp > 0) && (cfrac) && (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1 ) )))) {
73
    while ((cexp > 0) && (cfrac) && (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1 ) )))) {
68
        cexp--;
74
        cexp--;
69
        cfrac <<= 1;
75
        cfrac <<= 1;
70
            /* TODO: fix underflow */
76
            /* TODO: fix underflow */
71
    };
77
    };
72
   
78
   
73
    if ((cexp < 0) || ( cexp == 0 && (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1)))))) {
79
    if ((cexp < 0) || ( cexp == 0 && (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1)))))) {
74
        /* FIXME: underflow */
80
        /* FIXME: underflow */
75
        result.parts.exp = 0;
81
        result.parts.exp = 0;
76
        if ((cexp + FLOAT64_FRACTION_SIZE + 1) < 0) { /* +1 is place for rounding */
82
        if ((cexp + FLOAT64_FRACTION_SIZE + 1) < 0) { /* +1 is place for rounding */
77
            result.parts.fraction = 0;
83
            result.parts.fraction = 0;
78
            return result;
84
            return result;
79
        }
85
        }
80
       
86
       
81
        while (cexp < 0) {
87
        while (cexp < 0) {
82
            cexp++;
88
            cexp++;
83
            cfrac >>= 1;
89
            cfrac >>= 1;
84
        }
90
        }
85
   
91
   
86
        cfrac += (0x1 << (64 - FLOAT64_FRACTION_SIZE - 3));
92
        cfrac += (0x1 << (64 - FLOAT64_FRACTION_SIZE - 3));
87
       
93
       
88
        if (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1)))) {
94
        if (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1)))) {
89
           
95
           
90
            result.parts.fraction = ((cfrac >>(64 - FLOAT64_FRACTION_SIZE - 2) ) & (~FLOAT64_HIDDEN_BIT_MASK));
96
            result.parts.fraction = ((cfrac >>(64 - FLOAT64_FRACTION_SIZE - 2) ) & (~FLOAT64_HIDDEN_BIT_MASK));
91
            return result;
97
            return result;
92
        }  
98
        }  
93
    } else {
99
    } else {
94
        cfrac += (0x1 << (64 - FLOAT64_FRACTION_SIZE - 3));
100
        cfrac += (0x1 << (64 - FLOAT64_FRACTION_SIZE - 3));
95
    }
101
    }
96
   
102
   
97
    ++cexp;
103
    ++cexp;
98
 
104
 
99
    if (cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1 ))) {
105
    if (cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1 ))) {
100
        ++cexp;
106
        ++cexp;
101
        cfrac >>= 1;
107
        cfrac >>= 1;
102
    }  
108
    }  
103
 
109
 
104
    /* check overflow */
110
    /* check overflow */
105
    if (cexp >= FLOAT64_MAX_EXPONENT ) {
111
    if (cexp >= FLOAT64_MAX_EXPONENT ) {
106
        /* FIXME: overflow, return infinity */
112
        /* FIXME: overflow, return infinity */
107
        result.parts.exp = FLOAT64_MAX_EXPONENT;
113
        result.parts.exp = FLOAT64_MAX_EXPONENT;
108
        result.parts.fraction = 0;
114
        result.parts.fraction = 0;
109
        return result;
115
        return result;
110
    }
116
    }
111
 
117
 
112
    result.parts.exp = (uint32_t)cexp;
118
    result.parts.exp = (uint32_t)cexp;
113
   
119
   
114
    result.parts.fraction = ((cfrac >>(64 - FLOAT64_FRACTION_SIZE - 2 ) ) & (~FLOAT64_HIDDEN_BIT_MASK));
120
    result.parts.fraction = ((cfrac >>(64 - FLOAT64_FRACTION_SIZE - 2 ) ) & (~FLOAT64_HIDDEN_BIT_MASK));
115
   
121
   
116
    return result; 
122
    return result; 
117
}
123
}
118
 
124
 
119
/** Counts leading zeroes in 64bit unsigned integer
125
/** Counts leading zeroes in 64bit unsigned integer
120
 * @param i
126
 * @param i
121
 */
127
 */
122
int countZeroes64(uint64_t i)
128
int countZeroes64(uint64_t i)
123
{
129
{
124
    int j;
130
    int j;
125
    for (j =0; j < 64; j += 8) {
131
    for (j =0; j < 64; j += 8) {
126
        if ( i & (0xFFll << (56 - j))) {
132
        if ( i & (0xFFll << (56 - j))) {
127
            return (j + countZeroes8(i >> (56 - j)));
133
            return (j + countZeroes8(i >> (56 - j)));
128
        }
134
        }
129
    }
135
    }
130
 
136
 
131
    return 64;
137
    return 64;
132
}
138
}
133
 
139
 
134
/** Counts leading zeroes in 32bit unsigned integer
140
/** Counts leading zeroes in 32bit unsigned integer
135
 * @param i
141
 * @param i
136
 */
142
 */
137
int countZeroes32(uint32_t i)
143
int countZeroes32(uint32_t i)
138
{
144
{
139
    int j;
145
    int j;
140
    for (j =0; j < 32; j += 8) {
146
    for (j =0; j < 32; j += 8) {
141
        if ( i & (0xFF << (24 - j))) {
147
        if ( i & (0xFF << (24 - j))) {
142
            return (j + countZeroes8(i >> (24 - j)));
148
            return (j + countZeroes8(i >> (24 - j)));
143
        }
149
        }
144
    }
150
    }
145
 
151
 
146
    return 32;
152
    return 32;
147
}
153
}
148
 
154
 
149
/** Counts leading zeroes in byte
155
/** Counts leading zeroes in byte
150
 * @param i
156
 * @param i
151
 */
157
 */
152
int countZeroes8(uint8_t i)
158
int countZeroes8(uint8_t i)
153
{
159
{
154
    return zeroTable[i];
160
    return zeroTable[i];
155
}
161
}
156
 
162
 
157
/** Round and normalize number expressed by exponent and fraction with first bit (equal to hidden bit) at 30. bit
163
/** Round and normalize number expressed by exponent and fraction with first bit (equal to hidden bit) at 30. bit
158
 * @param exp exponent
164
 * @param exp exponent
159
 * @param fraction part with hidden bit shifted to 30. bit
165
 * @param fraction part with hidden bit shifted to 30. bit
160
 */
166
 */
161
void roundFloat32(int32_t *exp, uint32_t *fraction)
167
void roundFloat32(int32_t *exp, uint32_t *fraction)
162
{
168
{
163
    /* rounding - if first bit after fraction is set then round up */
169
    /* rounding - if first bit after fraction is set then round up */
164
    (*fraction) += (0x1 << 6);
170
    (*fraction) += (0x1 << 6);
165
   
171
   
166
    if ((*fraction) & (FLOAT32_HIDDEN_BIT_MASK << 8)) {
172
    if ((*fraction) & (FLOAT32_HIDDEN_BIT_MASK << 8)) {
167
        /* rounding overflow */
173
        /* rounding overflow */
168
        ++(*exp);
174
        ++(*exp);
169
        (*fraction) >>= 1;
175
        (*fraction) >>= 1;
170
    };
176
    };
171
   
177
   
172
    if (((*exp) >= FLOAT32_MAX_EXPONENT ) || ((*exp) < 0)) {
178
    if (((*exp) >= FLOAT32_MAX_EXPONENT ) || ((*exp) < 0)) {
173
        /* overflow - set infinity as result */
179
        /* overflow - set infinity as result */
174
        (*exp) = FLOAT32_MAX_EXPONENT;
180
        (*exp) = FLOAT32_MAX_EXPONENT;
175
        (*fraction) = 0;
181
        (*fraction) = 0;
176
        return;
182
        return;
177
    }
183
    }
178
 
184
 
179
    return;
185
    return;
180
}
186
}
181
 
187
 
182
/** Round and normalize number expressed by exponent and fraction with first bit (equal to hidden bit) at 62. bit
188
/** Round and normalize number expressed by exponent and fraction with first bit (equal to hidden bit) at 62. bit
183
 * @param exp exponent
189
 * @param exp exponent
184
 * @param fraction part with hidden bit shifted to 62. bit
190
 * @param fraction part with hidden bit shifted to 62. bit
185
 */
191
 */
186
void roundFloat64(int32_t *exp, uint64_t *fraction)
192
void roundFloat64(int32_t *exp, uint64_t *fraction)
187
{
193
{
188
    /* rounding - if first bit after fraction is set then round up */
194
    /* rounding - if first bit after fraction is set then round up */
189
    (*fraction) += (0x1 << 9);
195
    (*fraction) += (0x1 << 9);
190
   
196
   
191
    if ((*fraction) & (FLOAT64_HIDDEN_BIT_MASK << 11)) {
197
    if ((*fraction) & (FLOAT64_HIDDEN_BIT_MASK << 11)) {
192
        /* rounding overflow */
198
        /* rounding overflow */
193
        ++(*exp);
199
        ++(*exp);
194
        (*fraction) >>= 1;
200
        (*fraction) >>= 1;
195
    };
201
    };
196
   
202
   
197
    if (((*exp) >= FLOAT64_MAX_EXPONENT ) || ((*exp) < 0)) {
203
    if (((*exp) >= FLOAT64_MAX_EXPONENT ) || ((*exp) < 0)) {
198
        /* overflow - set infinity as result */
204
        /* overflow - set infinity as result */
199
        (*exp) = FLOAT64_MAX_EXPONENT;
205
        (*exp) = FLOAT64_MAX_EXPONENT;
200
        (*fraction) = 0;
206
        (*fraction) = 0;
201
        return;
207
        return;
202
    }
208
    }
203
 
209
 
204
    return;
210
    return;
205
}
211
}
206
 
212
 
-
 
213
 
-
 
214
 /** @}
-
 
215
 */
-
 
216
 
207
 
217