Subversion Repositories HelenOS-historic

Rev

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

Rev 697 Rev 698
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
#include "sftypes.h"
29
#include "sftypes.h"
30
#include "conversion.h"
30
#include "conversion.h"
31
 
31
 
32
float64 convertFloat32ToFloat64(float32 a)
32
float64 convertFloat32ToFloat64(float32 a)
33
{
33
{
34
    float64 result;
34
    float64 result;
35
    __u64 mant;
35
    __u64 mant;
36
   
36
   
37
    result.parts.sign = a.parts.sign;
37
    result.parts.sign = a.parts.sign;
38
    result.parts.mantisa = a.parts.mantisa;
38
    result.parts.mantisa = a.parts.mantisa;
39
    result.parts.mantisa <<= (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE );
39
    result.parts.mantisa <<= (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE );
40
   
40
   
41
    if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
41
    if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
42
        result.parts.exp = 0x7FF;
42
        result.parts.exp = 0x7FF;
43
        /* TODO; check if its correct for SigNaNs*/
43
        /* TODO; check if its correct for SigNaNs*/
44
        return result;
44
        return result;
45
    };
45
    };
46
   
46
   
47
    result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
47
    result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
48
    if (a.parts.exp == 0) {
48
    if (a.parts.exp == 0) {
49
        /* normalize denormalized numbers */
49
        /* normalize denormalized numbers */
50
 
50
 
51
        if (result.parts.mantisa == 0ll) { /* fix zero */
51
        if (result.parts.mantisa == 0ll) { /* fix zero */
52
            result.parts.exp = 0ll;
52
            result.parts.exp = 0ll;
53
            return result;
53
            return result;
54
        }
54
        }
55
           
55
           
56
        mant = result.parts.mantisa;
56
        mant = result.parts.mantisa;
57
       
57
       
58
        while (!(mant & (0x10000000000000ll))) {
58
        while (!(mant & (0x10000000000000ll))) {
59
            mant <<= 1;
59
            mant <<= 1;
60
            --result.parts.exp;
60
            --result.parts.exp;
61
        };
61
        };
-
 
62
       
-
 
63
        ++result.parts.exp;
62
        result.parts.mantisa = mant;
64
        result.parts.mantisa = mant;
63
    };
65
    };
64
   
66
   
65
    return result;
67
    return result;
66
   
68
   
67
};
69
};
68
 
70
 
69
float32 convertFloat64ToFloat32(float64 a)
71
float32 convertFloat64ToFloat32(float64 a)
70
{
72
{
71
    float32 result;
73
    float32 result;
72
    __s32 exp;
74
    __s32 exp;
73
    __u64 mant;
75
    __u64 mant;
74
   
76
   
75
    result.parts.sign = a.parts.sign;
77
    result.parts.sign = a.parts.sign;
76
   
78
   
77
    if (isFloat64NaN(a)) {
79
    if (isFloat64NaN(a)) {
78
       
80
       
79
        result.parts.exp = 0xFF;
81
        result.parts.exp = 0xFF;
80
       
82
       
81
        if (isFloat64SigNaN(a)) {
83
        if (isFloat64SigNaN(a)) {
82
            result.parts.mantisa = 0x800000; /* set first bit of mantisa nonzero */
84
            result.parts.mantisa = 0x800000; /* set first bit of mantisa nonzero */
83
            return result;
85
            return result;
84
        }
86
        }
85
   
87
   
86
        result.parts.mantisa = 0x1; /* mantisa nonzero but its first bit is zero */
88
        result.parts.mantisa = 0x1; /* mantisa nonzero but its first bit is zero */
87
        return result;
89
        return result;
88
    };
90
    };
89
 
91
 
90
    if (isFloat64Infinity(a)) {
92
    if (isFloat64Infinity(a)) {
91
        result.parts.mantisa = 0;
93
        result.parts.mantisa = 0;
92
        result.parts.exp = 0xFF;
94
        result.parts.exp = 0xFF;
93
        return result;
95
        return result;
94
    };
96
    };
95
 
97
 
96
    exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
98
    exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
97
   
99
   
98
    if (exp >= 0xFF) {
100
    if (exp >= 0xFF) {
99
        /*FIXME: overflow*/
101
        /*FIXME: overflow*/
100
        result.parts.mantisa = 0;
102
        result.parts.mantisa = 0;
101
        result.parts.exp = 0xFF;
103
        result.parts.exp = 0xFF;
102
        return result;
104
        return result;
103
       
105
       
104
    } else if (exp <= 0 ) {
106
    } else if (exp <= 0 ) {
105
       
107
       
106
        /* underflow or denormalized */
108
        /* underflow or denormalized */
107
       
109
       
108
        result.parts.exp = 0;
110
        result.parts.exp = 0;
109
       
111
       
110
        exp *= -1; 
112
        exp *= -1; 
111
       
-
 
112
        if (exp > FLOAT32_MANTISA_SIZE ) {
113
        if (exp > FLOAT32_MANTISA_SIZE ) {
113
            /* FIXME: underflow */
114
            /* FIXME: underflow */
114
            result.parts.mantisa = 0;
115
            result.parts.mantisa = 0;
115
            return result;
116
            return result;
116
        };
117
        };
117
       
118
       
118
        /* denormalized */
119
        /* denormalized */
119
       
120
       
120
        mant = result.parts.mantisa >> 1;
121
        mant = a.parts.mantisa;
121
        mant |= 0x10000000000000ll; /* denormalize and set hidden bit */
122
        mant |= 0x10000000000000ll; /* denormalize and set hidden bit */
122
       
123
       
-
 
124
        mant >>= (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE + 1);
-
 
125
       
123
        while (exp > 0) {
126
        while (exp > 0) {
124
            --exp;
127
            --exp;
125
            mant >>= 1;
128
            mant >>= 1;
126
        };
129
        };
127
        result.parts.mantisa = mant;
130
        result.parts.mantisa = mant;
128
       
131
       
129
        return result;
132
        return result;
130
    };
133
    };
131
 
134
 
132
    result.parts.exp = exp;
135
    result.parts.exp = exp;
133
    result.parts.mantisa = a.parts.mantisa >> (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE);
136
    result.parts.mantisa = a.parts.mantisa >> (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE);
134
    return result;
137
    return result;
135
};
138
};
136
 
139
 
137
 
140