Subversion Repositories HelenOS-historic

Rev

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

Rev 647 Rev 660
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<arithmetic.h>
30
#include<arithmetic.h>
-
 
31
#include<comparison.h>
31
 
32
 
32
/** Add two Float32 numbers with same signs
33
/** Add two Float32 numbers with same signs
33
 */
34
 */
34
float32 addFloat32(float32 a, float32 b)
35
float32 addFloat32(float32 a, float32 b)
35
{
36
{
36
    int expdiff;
37
    int expdiff;
37
    __u32 exp1,exp2,mant1,mant2;
38
    __u32 exp1,exp2,mant1,mant2;
38
   
39
   
39
    expdiff=a.parts.exp - b.parts.exp;
40
    expdiff=a.parts.exp - b.parts.exp;
40
    if (expdiff<0) {
41
    if (expdiff<0) {
41
        if (isFloat32NaN(b)) {
42
        if (isFloat32NaN(b)) {
42
            //TODO: fix SigNaN
43
            //TODO: fix SigNaN
43
            if (isFloat32SigNaN(b)) {
44
            if (isFloat32SigNaN(b)) {
44
            };
45
            };
45
            return b;
46
            return b;
46
        };
47
        };
47
       
48
       
48
        if (b.parts.exp==0xFF) {
49
        if (b.parts.exp==0xFF) {
49
            return b;
50
            return b;
50
        }
51
        }
51
       
52
       
52
        mant1=b.parts.mantisa;
53
        mant1=b.parts.mantisa;
53
        exp1=b.parts.exp;
54
        exp1=b.parts.exp;
54
        mant2=a.parts.mantisa;
55
        mant2=a.parts.mantisa;
55
        exp2=a.parts.exp;
56
        exp2=a.parts.exp;
56
        expdiff*=-1;
57
        expdiff*=-1;
57
    } else {
58
    } else {
58
        if (isFloat32NaN(a)) {
59
        if (isFloat32NaN(a)) {
59
            //TODO: fix SigNaN
60
            //TODO: fix SigNaN
60
            if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) {
61
            if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) {
61
            };
62
            };
62
            return a;
63
            return a;
63
        };
64
        };
64
       
65
       
65
        if (a.parts.exp==0xFF) {
66
        if (a.parts.exp==0xFF) {
66
            return a;
67
            return a;
67
        }
68
        }
68
       
69
       
69
        mant1=a.parts.mantisa;
70
        mant1=a.parts.mantisa;
70
        exp1=a.parts.exp;
71
        exp1=a.parts.exp;
71
        mant2=b.parts.mantisa;
72
        mant2=b.parts.mantisa;
72
        exp2=b.parts.exp;
73
        exp2=b.parts.exp;
73
    };
74
    };
74
   
75
   
75
    if (exp1==0) {
76
    if (exp1==0) {
76
        //both are denormalized
77
        //both are denormalized
77
        mant1+=mant2;
78
        mant1+=mant2;
78
        if (mant1&0xF00000) {
79
        if (mant1&0xF00000) {
79
            a.parts.exp=1;
80
            a.parts.exp=1;
80
        };
81
        };
81
        a.parts.mantisa=mant1;
82
        a.parts.mantisa=mant1;
82
        return a;
83
        return a;
83
    };
84
    };
84
   
85
   
85
    // create some space for rounding
86
    // create some space for rounding
86
    mant1<<=6;
87
    mant1<<=6;
87
    mant2<<=6;
88
    mant2<<=6;
88
   
89
   
89
    mant1|=0x20000000; //add hidden bit
90
    mant1|=0x20000000; //add hidden bit
90
   
91
   
91
   
92
   
92
    if (exp2==0) {
93
    if (exp2==0) {
93
        --expdiff; 
94
        --expdiff; 
94
    } else {
95
    } else {
95
        mant2|=0x20000000; //hidden bit
96
        mant2|=0x20000000; //hidden bit
96
    };
97
    };
97
   
98
   
98
    if (expdiff>24) {
99
    if (expdiff>24) {
99
         goto done;
100
         goto done;
100
         };
101
         };
101
   
102
   
102
    mant2>>=expdiff;
103
    mant2>>=expdiff;
103
    mant1+=mant2;
104
    mant1+=mant2;
104
done:
105
done:
105
    if (mant1&0x40000000) {
106
    if (mant1&0x40000000) {
106
        ++exp1;
107
        ++exp1;
107
        mant1>>=1;
108
        mant1>>=1;
108
    };
109
    };
109
   
110
   
110
    //rounding - if first bit after mantisa is set then round up
111
    //rounding - if first bit after mantisa is set then round up
111
    mant1+=0x20;
112
    mant1+=0x20;
112
   
113
   
113
    a.parts.exp=exp1;
114
    a.parts.exp=exp1;
114
    a.parts.mantisa=mant1>>6;
115
    a.parts.mantisa=mant1>>6;
115
    return a;
116
    return a;
116
};
117
};
117
 
118
 
118
/** Substract two float32 numbers with same signs
119
/** Substract two float32 numbers with same signs
119
 */
120
 */
120
float32 subFloat32(float32 a, float32 b)
121
float32 subFloat32(float32 a, float32 b)
121
{
122
{
122
   
123
   
123
   
124
   
124
};
125
};
125
 
126
 
126
 
127