Rev 804 | Rev 865 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 804 | Rev 857 | ||
---|---|---|---|
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 "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; |
36 | 37 | ||
37 | result.parts.sign = a.parts.sign; |
38 | result.parts.sign = a.parts.sign; |
38 | result.parts.fraction = a.parts.fraction; |
39 | result.parts.fraction = a.parts.fraction; |
39 | result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE ); |
40 | result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE ); |
40 | 41 | ||
41 | if ((isFloat32Infinity(a))||(isFloat32NaN(a))) { |
42 | if ((isFloat32Infinity(a))||(isFloat32NaN(a))) { |
42 | result.parts.exp = 0x7FF; |
43 | result.parts.exp = 0x7FF; |
43 | /* TODO; check if its correct for SigNaNs*/ |
44 | /* TODO; check if its correct for SigNaNs*/ |
44 | return result; |
45 | return result; |
45 | }; |
46 | }; |
46 | 47 | ||
47 | result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS ); |
48 | result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS ); |
48 | if (a.parts.exp == 0) { |
49 | if (a.parts.exp == 0) { |
49 | /* normalize denormalized numbers */ |
50 | /* normalize denormalized numbers */ |
50 | 51 | ||
51 | if (result.parts.fraction == 0ll) { /* fix zero */ |
52 | if (result.parts.fraction == 0ll) { /* fix zero */ |
52 | result.parts.exp = 0ll; |
53 | result.parts.exp = 0ll; |
53 | return result; |
54 | return result; |
54 | } |
55 | } |
55 | 56 | ||
56 | frac = result.parts.fraction; |
57 | frac = result.parts.fraction; |
57 | 58 | ||
58 | while (!(frac & (0x10000000000000ll))) { |
59 | while (!(frac & (0x10000000000000ll))) { |
59 | frac <<= 1; |
60 | frac <<= 1; |
60 | --result.parts.exp; |
61 | --result.parts.exp; |
61 | }; |
62 | }; |
62 | 63 | ||
63 | ++result.parts.exp; |
64 | ++result.parts.exp; |
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; |
75 | __u64 frac; |
76 | __u64 frac; |
76 | 77 | ||
77 | result.parts.sign = a.parts.sign; |
78 | result.parts.sign = a.parts.sign; |
78 | 79 | ||
79 | if (isFloat64NaN(a)) { |
80 | if (isFloat64NaN(a)) { |
80 | 81 | ||
81 | result.parts.exp = 0xFF; |
82 | result.parts.exp = 0xFF; |
82 | 83 | ||
83 | if (isFloat64SigNaN(a)) { |
84 | if (isFloat64SigNaN(a)) { |
84 | result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */ |
85 | result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */ |
85 | return result; |
86 | return result; |
86 | } |
87 | } |
87 | 88 | ||
88 | result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */ |
89 | result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */ |
89 | return result; |
90 | return result; |
90 | }; |
91 | }; |
91 | 92 | ||
92 | if (isFloat64Infinity(a)) { |
93 | if (isFloat64Infinity(a)) { |
93 | result.parts.fraction = 0; |
94 | result.parts.fraction = 0; |
94 | result.parts.exp = 0xFF; |
95 | result.parts.exp = 0xFF; |
95 | return result; |
96 | return result; |
96 | }; |
97 | }; |
97 | 98 | ||
98 | exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS; |
99 | exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS; |
99 | 100 | ||
100 | if (exp >= 0xFF) { |
101 | if (exp >= 0xFF) { |
101 | /*FIXME: overflow*/ |
102 | /*FIXME: overflow*/ |
102 | result.parts.fraction = 0; |
103 | result.parts.fraction = 0; |
103 | result.parts.exp = 0xFF; |
104 | result.parts.exp = 0xFF; |
104 | return result; |
105 | return result; |
105 | 106 | ||
106 | } else if (exp <= 0 ) { |
107 | } else if (exp <= 0 ) { |
107 | 108 | ||
108 | /* underflow or denormalized */ |
109 | /* underflow or denormalized */ |
109 | 110 | ||
110 | result.parts.exp = 0; |
111 | result.parts.exp = 0; |
111 | 112 | ||
112 | exp *= -1; |
113 | exp *= -1; |
113 | if (exp > FLOAT32_FRACTION_SIZE ) { |
114 | if (exp > FLOAT32_FRACTION_SIZE ) { |
114 | /* FIXME: underflow */ |
115 | /* FIXME: underflow */ |
115 | result.parts.fraction = 0; |
116 | result.parts.fraction = 0; |
116 | return result; |
117 | return result; |
117 | }; |
118 | }; |
118 | 119 | ||
119 | /* denormalized */ |
120 | /* denormalized */ |
120 | 121 | ||
121 | frac = a.parts.fraction; |
122 | frac = a.parts.fraction; |
122 | frac |= 0x10000000000000ll; /* denormalize and set hidden bit */ |
123 | frac |= 0x10000000000000ll; /* denormalize and set hidden bit */ |
123 | 124 | ||
124 | frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1); |
125 | frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1); |
125 | 126 | ||
126 | while (exp > 0) { |
127 | while (exp > 0) { |
127 | --exp; |
128 | --exp; |
128 | frac >>= 1; |
129 | frac >>= 1; |
129 | }; |
130 | }; |
130 | result.parts.fraction = frac; |
131 | result.parts.fraction = frac; |
131 | 132 | ||
132 | return result; |
133 | return result; |
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 | ||
140 | 211 |