Rev 563 | Rev 647 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 563 | Rev 572 | ||
---|---|---|---|
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); |
32 | float32 addFloat32(float32 a, float32 b); |
33 | float32 subFloat32(float32 a, float32 b); |
33 | float32 subFloat32(float32 a, float32 b); |
34 | inline int isFloat32NaN(float32 f); |
34 | inline int isFloat32NaN(float32 f); |
35 | inline int isFloat32SigNaN(float32 f); |
35 | inline int isFloat32SigNaN(float32 f); |
36 | 36 | ||
37 | float __addsf3(float a, float b) |
37 | float __addsf3(float a, float b) |
38 | { |
38 | { |
39 | float32 fa, fb; |
39 | float32 fa, fb; |
40 | fa.f=a; |
40 | fa.f=a; |
41 | fb.f=b; |
41 | fb.f=b; |
42 | if (fa.parts.sign!=fb.parts.sign) return subFloat32(fa,fb).f; |
42 | if (fa.parts.sign!=fb.parts.sign) return subFloat32(fa,fb).f; |
43 | return addFloat32(fa,fb).f; |
43 | return addFloat32(fa,fb).f; |
44 | }; |
44 | }; |
45 | 45 | ||
46 | float __subsf3(float a, float b) |
46 | float __subsf3(float a, float b) |
47 | { |
47 | { |
48 | float32 fa, fb; |
48 | float32 fa, fb; |
49 | fa.f=a; |
49 | fa.f=a; |
50 | fb.f=b; |
50 | fb.f=b; |
51 | if (fa.parts.sign!=fb.parts.sign) return addFloat32(fa,fb).f; |
51 | if (fa.parts.sign!=fb.parts.sign) return addFloat32(fa,fb).f; |
52 | return subFloat32(fa,fb).f; |
52 | return subFloat32(fa,fb).f; |
53 | }; |
53 | }; |
54 | 54 | ||
55 | float __negsf2(float a) |
55 | float __negsf2(float a) |
56 | { |
56 | { |
57 | float32 fa; |
57 | float32 fa; |
58 | fa.f=a; |
58 | fa.f=a; |
59 | fa.parts.sign=!fa.parts.sign; |
59 | fa.parts.sign=!fa.parts.sign; |
60 | return fa.f; |
60 | return fa.f; |
61 | }; |
61 | }; |
62 | 62 | ||
63 | double __negdf2(double a) |
63 | double __negdf2(double a) |
64 | { |
64 | { |
65 | float64 fa; |
65 | float64 fa; |
66 | fa.d=a; |
66 | fa.d=a; |
67 | fa.parts.sign=!fa.parts.sign; |
67 | fa.parts.sign=!fa.parts.sign; |
68 | return fa.d; |
68 | return fa.d; |
69 | }; |
69 | }; |
70 | 70 | ||
71 | /** Add two Float32 numbers with same signs |
71 | /** Add two Float32 numbers with same signs |
72 | */ |
72 | */ |
73 | float32 addFloat32(float32 a, float32 b) |
73 | float32 addFloat32(float32 a, float32 b) |
74 | { |
74 | { |
75 | int expdiff; |
75 | int expdiff; |
76 | __u32 exp1,exp2,mant1,mant2; |
76 | __u32 exp1,exp2,mant1,mant2; |
77 | 77 | ||
78 | expdiff=a.parts.exp - b.parts.exp; |
78 | expdiff=a.parts.exp - b.parts.exp; |
79 | if (expdiff<0) { |
79 | if (expdiff<0) { |
80 | if (isFloat32NaN(b)) { |
80 | if (isFloat32NaN(b)) { |
81 | //TODO: fix SigNaN |
81 | //TODO: fix SigNaN |
82 | if (isFloat32SigNaN(b)) { |
82 | if (isFloat32SigNaN(b)) { |
83 | }; |
83 | }; |
84 | return b; |
84 | return b; |
85 | }; |
85 | }; |
86 | 86 | ||
87 | if (b.parts.exp==0xFF) { |
87 | if (b.parts.exp==0xFF) { |
88 | return b; |
88 | return b; |
89 | } |
89 | } |
90 | 90 | ||
91 | mant1=b.parts.mantisa; |
91 | mant1=b.parts.mantisa; |
92 | exp1=b.parts.exp; |
92 | exp1=b.parts.exp; |
93 | mant2=a.parts.mantisa; |
93 | mant2=a.parts.mantisa; |
94 | exp2=a.parts.exp; |
94 | exp2=a.parts.exp; |
95 | expdiff*=-1; |
95 | expdiff*=-1; |
96 | } else { |
96 | } else { |
97 | if (isFloat32NaN(a)) { |
97 | if (isFloat32NaN(a)) { |
98 | //TODO: fix SigNaN |
98 | //TODO: fix SigNaN |
99 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
99 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
100 | }; |
100 | }; |
101 | return a; |
101 | return a; |
102 | }; |
102 | }; |
103 | 103 | ||
104 | if (a.parts.exp==0xFF) { |
104 | if (a.parts.exp==0xFF) { |
105 | return a; |
105 | return a; |
106 | } |
106 | } |
107 | 107 | ||
108 | mant1=a.parts.mantisa; |
108 | mant1=a.parts.mantisa; |
109 | exp1=a.parts.exp; |
109 | exp1=a.parts.exp; |
110 | mant2=b.parts.mantisa; |
110 | mant2=b.parts.mantisa; |
111 | exp2=b.parts.exp; |
111 | exp2=b.parts.exp; |
112 | }; |
112 | }; |
113 | 113 | ||
114 | if (exp1==0) { |
114 | if (exp1==0) { |
115 | //both are denormalized |
115 | //both are denormalized |
116 | mant1+=mant2; |
116 | mant1+=mant2; |
117 | if (mant1&0xF00000) { |
117 | if (mant1&0xF00000) { |
118 | a.parts.exp=1; |
118 | a.parts.exp=1; |
119 | }; |
119 | }; |
120 | a.parts.mantisa=mant1; |
120 | a.parts.mantisa=mant1; |
121 | return a; |
121 | return a; |
122 | }; |
122 | }; |
123 | 123 | ||
124 | // create some space for rounding |
124 | // create some space for rounding |
125 | mant1<<=6; |
125 | mant1<<=6; |
126 | mant2<<=6; |
126 | mant2<<=6; |
127 | 127 | ||
128 | mant1|=0x20000000; //add hidden bit |
128 | mant1|=0x20000000; //add hidden bit |
129 | 129 | ||
130 | 130 | ||
131 | if (exp2==0) { |
131 | if (exp2==0) { |
132 | --expdiff; |
132 | --expdiff; |
133 | } else { |
133 | } else { |
134 | mant2|=0x20000000; //hidden bit |
134 | mant2|=0x20000000; //hidden bit |
135 | }; |
135 | }; |
136 | 136 | ||
- | 137 | if (expdiff>24) { |
|
- | 138 | goto done; |
|
- | 139 | }; |
|
- | 140 | ||
137 | mant2>>=expdiff; |
141 | mant2>>=expdiff; |
138 | mant1+=mant2; |
142 | mant1+=mant2; |
139 | done: |
143 | done: |
140 | if (mant1&0x40000000) { |
144 | if (mant1&0x40000000) { |
141 | ++exp1; |
145 | ++exp1; |
142 | mant1>>=1; |
146 | mant1>>=1; |
143 | }; |
147 | }; |
144 | 148 | ||
145 | //rounding - if first bit after mantisa is set then round up |
149 | //rounding - if first bit after mantisa is set then round up |
146 | mant1+=0x20; |
150 | mant1+=0x20; |
147 | 151 | ||
148 | a.parts.exp=exp1; |
152 | a.parts.exp=exp1; |
149 | a.parts.mantisa=mant1>>6; |
153 | a.parts.mantisa=mant1>>6; |
150 | return a; |
154 | return a; |
151 | }; |
155 | }; |
152 | 156 | ||
153 | /** Substract two float32 numbers with same signs |
157 | /** Substract two float32 numbers with same signs |
154 | */ |
158 | */ |
155 | float32 subFloat32(float32 a, float32 b) |
159 | float32 subFloat32(float32 a, float32 b) |
156 | { |
160 | { |
157 | 161 | ||
158 | 162 | ||
159 | }; |
163 | }; |
160 | 164 | ||
161 | inline int isFloat32NaN(float32 f) |
165 | inline int isFloat32NaN(float32 f) |
162 | { /* NaN : exp = 0xff and nonzero mantisa */ |
166 | { /* NaN : exp = 0xff and nonzero mantisa */ |
163 | return ((f.parts.exp==0xFF)&&(f.parts.mantisa)); |
167 | return ((f.parts.exp==0xFF)&&(f.parts.mantisa)); |
164 | }; |
168 | }; |
165 | 169 | ||
166 | inline int isFloat32SigNaN(float32 f) |
170 | inline int isFloat32SigNaN(float32 f) |
167 | { /* SigNaN : exp = 0xff mantisa = 1xxxxx..x (binary), where at least one x is nonzero */ |
171 | { /* SigNaN : exp = 0xff mantisa = 1xxxxx..x (binary), where at least one x is nonzero */ |
168 | return ((f.parts.exp==0xFF)&&(f.parts.mantisa>0x400000)); |
172 | return ((f.parts.exp==0xFF)&&(f.parts.mantisa>0x400000)); |
169 | }; |
173 | }; |
170 | 174 | ||
171 | 175 |