Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
535 cejka 1
 
2
/*
3
 * Copyright (C) 2005 Josef Cejka
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
#include<softfloat.h>
31
 
542 cejka 32
 
33
float __addsf3(float a, float b)
34
{
35
    float32 fa, fb;
36
    a.f=a;
37
    b.f=b;
38
    if (a.parts.sign!=b.parts.sign) return subFloat32(a,b).f;
39
    return addFloat32(a,b).f;
40
};
41
 
42
float __subsf3(float a, float b)
43
{
44
    float32 fa, fb;
45
    a.f=a;
46
    b.f=b;
47
    if (a.parts.sign!=b.parts.sign) return addFloat32(a,b).f;
48
    return subFloat32(a,b).f;
49
};
50
 
51
float __negsf2(float a)
52
{
53
    float32 fa;
54
    fa.f=a;
55
    fa.parts.sign=!fa.parts.sign;
56
    return fa.f;
57
};
58
 
59
double __negdf2(double a)
60
{
61
    float64 fa;
62
    fa.f=a;
63
    fa.parts.sign=!fa.parts.sign;
64
    return fa.f;
65
};
66
 
67
float32 addFloat32(float32 a, float32 b)
68
{
69
    __u32 expdiff;
70
    __u32 exp1,exp2,mant1,mant2;
71
 
72
    expdiff=a.parts.exp - b.parts.exp;
73
    if (expdiff<0) {
74
        if (isFloat32NaN(a)) {
75
            //TODO: fix it
76
            return a;
77
        };
78
 
79
        mant1=b.parts.mantisa;
80
        exp1=b.parts.exp;
81
        mant2=a.parts.mantisa;
82
        exp2=a.parts.exp;
83
        expdiff*=-1;
84
    } else {
85
        if (isFloat32NaN(b)) {
86
            //TODO: fix it
87
            return b;
88
        };
89
        mant1=a.parts.mantisa;
90
        exp1=a.parts.exp;
91
        mant2=b.parts.mantisa;
92
        exp2=b.parts.exp;
93
    };
94
 
95
    // create some space for rounding
96
    mant1<<=6;
97
    mant2<<=6;
98
 
99
    if (exp1!=0) {
100
        mant1|=0x20000000; //add hidden bit
101
    };
102
 
103
    if (exp2==0) {
104
        --expdiff; 
105
    } else {
106
        mant2|=0x20000000; //hidden bit
107
    };
108
 
109
 
110
    if (expdiff>24) {
111
        goto done;
112
    };
113
 
114
    mant2>>=expdiff;
115
    mant1+=mant2;
116
done:
117
    //TODO: round mant1
118
    a.parts.exp=exp1;
119
    a.parts.mantisa=mant1>>6;
120
    return a;
121
};
122
 
123
float32 subFloat32(float32 a, float32 b)
124
{
125
 
126
 
127
};
128
 
129
inline int isFloat32NaN(float32 f)
130
{   /* NaN : exp = 0xff and nonzero mantisa */
131
    float32 fa;
132
    fa.f=f;
133
    return ((fa.parts.exp==0xFF)&&(fa.parts.mantisa));
134
};
135
 
136
inline int isFloat32SigNaN(float32 f)
137
{   /* SigNaN : exp = 0xff mantisa = 1xxxxx..x (binary), where at least one x is nonzero */
138
    float32 fa;
139
    fa.f=f;
140
    return ((fa.parts.exp==0xFF)&&(fa.parts.mantisa>0x400000));
141
};
142