Rev 804 | Rev 865 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 804 | Rev 857 | ||
---|---|---|---|
Line 26... | Line 26... | ||
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 | #include "comparison.h" |
|
31 | 32 | ||
32 | float64 convertFloat32ToFloat64(float32 a) |
33 | float64 convertFloat32ToFloat64(float32 a) |
33 | { |
34 | { |
34 | float64 result; |
35 | float64 result; |
35 | __u64 frac; |
36 | __u64 frac; |
Line 64... | Line 65... | ||
64 | result.parts.fraction = frac; |
65 | result.parts.fraction = frac; |
65 | }; |
66 | }; |
66 | 67 | ||
67 | return result; |
68 | return result; |
68 | 69 | ||
69 | }; |
70 | } |
70 | 71 | ||
71 | float32 convertFloat64ToFloat32(float64 a) |
72 | float32 convertFloat64ToFloat32(float64 a) |
72 | { |
73 | { |
73 | float32 result; |
74 | float32 result; |
74 | __s32 exp; |
75 | __s32 exp; |
Line 133... | Line 134... | ||
133 | }; |
134 | }; |
134 | 135 | ||
135 | result.parts.exp = exp; |
136 | result.parts.exp = exp; |
136 | result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE); |
137 | result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE); |
137 | return result; |
138 | return result; |
- | 139 | } |
|
- | 140 | ||
- | 141 | ||
- | 142 | /** Helping procedure for converting float32 to uint32 |
|
- | 143 | * @param a floating point number in normalized form (no NaNs or Inf are checked ) |
|
- | 144 | * @return unsigned integer |
|
- | 145 | */ |
|
- | 146 | static __u32 _float32_to_uint32_helper(float32 a) |
|
- | 147 | { |
|
- | 148 | __u32 frac; |
|
- | 149 | ||
- | 150 | if (a.parts.exp < FLOAT32_BIAS) { |
|
- | 151 | /*TODO: rounding*/ |
|
- | 152 | return 0; |
|
- | 153 | } |
|
- | 154 | ||
- | 155 | frac = a.parts.fraction; |
|
- | 156 | ||
- | 157 | frac |= FLOAT32_HIDDEN_BIT_MASK; |
|
- | 158 | /* shift fraction to left so hidden bit will be the most significant bit */ |
|
- | 159 | frac <<= 32 - FLOAT32_FRACTION_SIZE - 1; |
|
- | 160 | ||
- | 161 | frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1; |
|
- | 162 | if ((a.parts.sign == 1) && (frac != 0)) { |
|
- | 163 | frac = ~frac; |
|
- | 164 | ++frac; |
|
- | 165 | } |
|
- | 166 | ||
- | 167 | return frac; |
|
- | 168 | } |
|
- | 169 | ||
- | 170 | /* Convert float to unsigned int32 |
|
- | 171 | * FIXME: Im not sure what to return if overflow/underflow happens |
|
- | 172 | * - now its the biggest or the smallest int |
|
- | 173 | */ |
|
- | 174 | __u32 float32_to_uint32(float32 a) |
|
- | 175 | { |
|
- | 176 | if (isFloat32NaN(a)) { |
|
- | 177 | return MAX_UINT32; |
|
- | 178 | } |
|
- | 179 | ||
- | 180 | if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) { |
|
- | 181 | if (a.parts.sign) { |
|
- | 182 | return MIN_UINT32; |
|
- | 183 | } |
|
- | 184 | return MAX_UINT32; |
|
- | 185 | } |
|
- | 186 | ||
- | 187 | return _float32_to_uint32_helper(a); |
|
- | 188 | } |
|
- | 189 | ||
- | 190 | /* Convert float to signed int32 |
|
- | 191 | * FIXME: Im not sure what to return if overflow/underflow happens |
|
- | 192 | * - now its the biggest or the smallest int |
|
- | 193 | */ |
|
- | 194 | __s32 float32_to_int32(float32 a) |
|
- | 195 | { |
|
- | 196 | if (isFloat32NaN(a)) { |
|
- | 197 | return MAX_INT32; |
|
- | 198 | } |
|
- | 199 | ||
- | 200 | if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) { |
|
- | 201 | if (a.parts.sign) { |
|
- | 202 | return MIN_INT32; |
|
- | 203 | } |
|
- | 204 | return MAX_INT32; |
|
- | 205 | } |
|
- | 206 | return _float32_to_uint32_helper(a); |
|
138 | }; |
207 | } |
- | 208 | ||
- | 209 | ||
139 | 210 |