Rev 542 | Rev 572 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 542 | Rev 563 | ||
---|---|---|---|
1 | 1 | ||
2 | /* |
2 | /* |
3 | * Copyright (C) 2005 Josef Cejka |
3 | * Copyright (C) 2005 Josef Cejka |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #include<softfloat.h> |
30 | #include<softfloat.h> |
31 | 31 | ||
- | 32 | float32 addFloat32(float32 a, float32 b); |
|
- | 33 | float32 subFloat32(float32 a, float32 b); |
|
- | 34 | inline int isFloat32NaN(float32 f); |
|
- | 35 | inline int isFloat32SigNaN(float32 f); |
|
32 | 36 | ||
33 | float __addsf3(float a, float b) |
37 | float __addsf3(float a, float b) |
34 | { |
38 | { |
35 | float32 fa, fb; |
39 | float32 fa, fb; |
36 | a.f=a; |
40 | fa.f=a; |
37 | b.f=b; |
41 | fb.f=b; |
38 | if (a.parts.sign!=b.parts.sign) return subFloat32(a,b).f; |
42 | if (fa.parts.sign!=fb.parts.sign) return subFloat32(fa,fb).f; |
39 | return addFloat32(a,b).f; |
43 | return addFloat32(fa,fb).f; |
40 | }; |
44 | }; |
41 | 45 | ||
42 | float __subsf3(float a, float b) |
46 | float __subsf3(float a, float b) |
43 | { |
47 | { |
44 | float32 fa, fb; |
48 | float32 fa, fb; |
45 | a.f=a; |
49 | fa.f=a; |
46 | b.f=b; |
50 | fb.f=b; |
47 | if (a.parts.sign!=b.parts.sign) return addFloat32(a,b).f; |
51 | if (fa.parts.sign!=fb.parts.sign) return addFloat32(fa,fb).f; |
48 | return subFloat32(a,b).f; |
52 | return subFloat32(fa,fb).f; |
49 | }; |
53 | }; |
50 | 54 | ||
51 | float __negsf2(float a) |
55 | float __negsf2(float a) |
52 | { |
56 | { |
53 | float32 fa; |
57 | float32 fa; |
54 | fa.f=a; |
58 | fa.f=a; |
55 | fa.parts.sign=!fa.parts.sign; |
59 | fa.parts.sign=!fa.parts.sign; |
56 | return fa.f; |
60 | return fa.f; |
57 | }; |
61 | }; |
58 | 62 | ||
59 | double __negdf2(double a) |
63 | double __negdf2(double a) |
60 | { |
64 | { |
61 | float64 fa; |
65 | float64 fa; |
62 | fa.f=a; |
66 | fa.d=a; |
63 | fa.parts.sign=!fa.parts.sign; |
67 | fa.parts.sign=!fa.parts.sign; |
64 | return fa.f; |
68 | return fa.d; |
65 | }; |
69 | }; |
66 | 70 | ||
- | 71 | /** Add two Float32 numbers with same signs |
|
- | 72 | */ |
|
67 | float32 addFloat32(float32 a, float32 b) |
73 | float32 addFloat32(float32 a, float32 b) |
68 | { |
74 | { |
69 | __u32 expdiff; |
75 | int expdiff; |
70 | __u32 exp1,exp2,mant1,mant2; |
76 | __u32 exp1,exp2,mant1,mant2; |
71 | 77 | ||
72 | expdiff=a.parts.exp - b.parts.exp; |
78 | expdiff=a.parts.exp - b.parts.exp; |
73 | if (expdiff<0) { |
79 | if (expdiff<0) { |
74 | if (isFloat32NaN(a)) { |
80 | if (isFloat32NaN(b)) { |
75 | //TODO: fix it |
81 | //TODO: fix SigNaN |
- | 82 | if (isFloat32SigNaN(b)) { |
|
- | 83 | }; |
|
76 | return a; |
84 | return b; |
77 | }; |
85 | }; |
- | 86 | ||
- | 87 | if (b.parts.exp==0xFF) { |
|
- | 88 | return b; |
|
- | 89 | } |
|
78 | 90 | ||
79 | mant1=b.parts.mantisa; |
91 | mant1=b.parts.mantisa; |
80 | exp1=b.parts.exp; |
92 | exp1=b.parts.exp; |
81 | mant2=a.parts.mantisa; |
93 | mant2=a.parts.mantisa; |
82 | exp2=a.parts.exp; |
94 | exp2=a.parts.exp; |
83 | expdiff*=-1; |
95 | expdiff*=-1; |
84 | } else { |
96 | } else { |
85 | if (isFloat32NaN(b)) { |
97 | if (isFloat32NaN(a)) { |
86 | //TODO: fix it |
98 | //TODO: fix SigNaN |
- | 99 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
|
- | 100 | }; |
|
87 | return b; |
101 | return a; |
88 | }; |
102 | }; |
- | 103 | ||
- | 104 | if (a.parts.exp==0xFF) { |
|
- | 105 | return a; |
|
- | 106 | } |
|
- | 107 | ||
89 | mant1=a.parts.mantisa; |
108 | mant1=a.parts.mantisa; |
90 | exp1=a.parts.exp; |
109 | exp1=a.parts.exp; |
91 | mant2=b.parts.mantisa; |
110 | mant2=b.parts.mantisa; |
92 | exp2=b.parts.exp; |
111 | exp2=b.parts.exp; |
93 | }; |
112 | }; |
94 | 113 | ||
- | 114 | if (exp1==0) { |
|
- | 115 | //both are denormalized |
|
- | 116 | mant1+=mant2; |
|
- | 117 | if (mant1&0xF00000) { |
|
- | 118 | a.parts.exp=1; |
|
- | 119 | }; |
|
- | 120 | a.parts.mantisa=mant1; |
|
- | 121 | return a; |
|
- | 122 | }; |
|
- | 123 | ||
95 | // create some space for rounding |
124 | // create some space for rounding |
96 | mant1<<=6; |
125 | mant1<<=6; |
97 | mant2<<=6; |
126 | mant2<<=6; |
98 | 127 | ||
99 | if (exp1!=0) { |
- | |
100 | mant1|=0x20000000; //add hidden bit |
128 | mant1|=0x20000000; //add hidden bit |
101 | }; |
129 | |
102 | 130 | ||
103 | if (exp2==0) { |
131 | if (exp2==0) { |
104 | --expdiff; |
132 | --expdiff; |
105 | } else { |
133 | } else { |
106 | mant2|=0x20000000; //hidden bit |
134 | mant2|=0x20000000; //hidden bit |
107 | }; |
135 | }; |
108 | 136 | ||
109 | - | ||
110 | if (expdiff>24) { |
- | |
111 | goto done; |
- | |
112 | }; |
- | |
113 | - | ||
114 | mant2>>=expdiff; |
137 | mant2>>=expdiff; |
115 | mant1+=mant2; |
138 | mant1+=mant2; |
116 | done: |
139 | done: |
117 | //TODO: round mant1 |
140 | if (mant1&0x40000000) { |
- | 141 | ++exp1; |
|
- | 142 | mant1>>=1; |
|
- | 143 | }; |
|
- | 144 | ||
- | 145 | //rounding - if first bit after mantisa is set then round up |
|
- | 146 | mant1+=0x20; |
|
- | 147 | ||
118 | a.parts.exp=exp1; |
148 | a.parts.exp=exp1; |
119 | a.parts.mantisa=mant1>>6; |
149 | a.parts.mantisa=mant1>>6; |
120 | return a; |
150 | return a; |
121 | }; |
151 | }; |
122 | 152 | ||
- | 153 | /** Substract two float32 numbers with same signs |
|
- | 154 | */ |
|
123 | float32 subFloat32(float32 a, float32 b) |
155 | float32 subFloat32(float32 a, float32 b) |
124 | { |
156 | { |
125 | 157 | ||
126 | 158 | ||
127 | }; |
159 | }; |
128 | 160 | ||
129 | inline int isFloat32NaN(float32 f) |
161 | inline int isFloat32NaN(float32 f) |
130 | { /* NaN : exp = 0xff and nonzero mantisa */ |
162 | { /* NaN : exp = 0xff and nonzero mantisa */ |
131 | float32 fa; |
- | |
132 | fa.f=f; |
- | |
133 | return ((fa.parts.exp==0xFF)&&(fa.parts.mantisa)); |
163 | return ((f.parts.exp==0xFF)&&(f.parts.mantisa)); |
134 | }; |
164 | }; |
135 | 165 | ||
136 | inline int isFloat32SigNaN(float32 f) |
166 | inline int isFloat32SigNaN(float32 f) |
137 | { /* SigNaN : exp = 0xff mantisa = 1xxxxx..x (binary), where at least one x is nonzero */ |
167 | { /* 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)); |
168 | return ((f.parts.exp==0xFF)&&(f.parts.mantisa>0x400000)); |
141 | }; |
169 | }; |
142 | 170 | ||
143 | 171 |