Rev 698 | Rev 857 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 698 | Rev 804 | ||
---|---|---|---|
Line 30... | Line 30... | ||
30 | #include "conversion.h" |
30 | #include "conversion.h" |
31 | 31 | ||
32 | float64 convertFloat32ToFloat64(float32 a) |
32 | float64 convertFloat32ToFloat64(float32 a) |
33 | { |
33 | { |
34 | float64 result; |
34 | float64 result; |
35 | __u64 mant; |
35 | __u64 frac; |
36 | 36 | ||
37 | result.parts.sign = a.parts.sign; |
37 | result.parts.sign = a.parts.sign; |
38 | result.parts.mantisa = a.parts.mantisa; |
38 | result.parts.fraction = a.parts.fraction; |
39 | result.parts.mantisa <<= (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE ); |
39 | result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE ); |
40 | 40 | ||
41 | if ((isFloat32Infinity(a))||(isFloat32NaN(a))) { |
41 | if ((isFloat32Infinity(a))||(isFloat32NaN(a))) { |
42 | result.parts.exp = 0x7FF; |
42 | result.parts.exp = 0x7FF; |
43 | /* TODO; check if its correct for SigNaNs*/ |
43 | /* TODO; check if its correct for SigNaNs*/ |
44 | return result; |
44 | return result; |
Line 46... | Line 46... | ||
46 | 46 | ||
47 | result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS ); |
47 | result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS ); |
48 | if (a.parts.exp == 0) { |
48 | if (a.parts.exp == 0) { |
49 | /* normalize denormalized numbers */ |
49 | /* normalize denormalized numbers */ |
50 | 50 | ||
51 | if (result.parts.mantisa == 0ll) { /* fix zero */ |
51 | if (result.parts.fraction == 0ll) { /* fix zero */ |
52 | result.parts.exp = 0ll; |
52 | result.parts.exp = 0ll; |
53 | return result; |
53 | return result; |
54 | } |
54 | } |
55 | 55 | ||
56 | mant = result.parts.mantisa; |
56 | frac = result.parts.fraction; |
57 | 57 | ||
58 | while (!(mant & (0x10000000000000ll))) { |
58 | while (!(frac & (0x10000000000000ll))) { |
59 | mant <<= 1; |
59 | frac <<= 1; |
60 | --result.parts.exp; |
60 | --result.parts.exp; |
61 | }; |
61 | }; |
62 | 62 | ||
63 | ++result.parts.exp; |
63 | ++result.parts.exp; |
64 | result.parts.mantisa = mant; |
64 | result.parts.fraction = frac; |
65 | }; |
65 | }; |
66 | 66 | ||
67 | return result; |
67 | return result; |
68 | 68 | ||
69 | }; |
69 | }; |
70 | 70 | ||
71 | float32 convertFloat64ToFloat32(float64 a) |
71 | float32 convertFloat64ToFloat32(float64 a) |
72 | { |
72 | { |
73 | float32 result; |
73 | float32 result; |
74 | __s32 exp; |
74 | __s32 exp; |
75 | __u64 mant; |
75 | __u64 frac; |
76 | 76 | ||
77 | result.parts.sign = a.parts.sign; |
77 | result.parts.sign = a.parts.sign; |
78 | 78 | ||
79 | if (isFloat64NaN(a)) { |
79 | if (isFloat64NaN(a)) { |
80 | 80 | ||
81 | result.parts.exp = 0xFF; |
81 | result.parts.exp = 0xFF; |
82 | 82 | ||
83 | if (isFloat64SigNaN(a)) { |
83 | if (isFloat64SigNaN(a)) { |
84 | result.parts.mantisa = 0x800000; /* set first bit of mantisa nonzero */ |
84 | result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */ |
85 | return result; |
85 | return result; |
86 | } |
86 | } |
87 | 87 | ||
88 | result.parts.mantisa = 0x1; /* mantisa nonzero but its first bit is zero */ |
88 | result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */ |
89 | return result; |
89 | return result; |
90 | }; |
90 | }; |
91 | 91 | ||
92 | if (isFloat64Infinity(a)) { |
92 | if (isFloat64Infinity(a)) { |
93 | result.parts.mantisa = 0; |
93 | result.parts.fraction = 0; |
94 | result.parts.exp = 0xFF; |
94 | result.parts.exp = 0xFF; |
95 | return result; |
95 | return result; |
96 | }; |
96 | }; |
97 | 97 | ||
98 | exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS; |
98 | exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS; |
99 | 99 | ||
100 | if (exp >= 0xFF) { |
100 | if (exp >= 0xFF) { |
101 | /*FIXME: overflow*/ |
101 | /*FIXME: overflow*/ |
102 | result.parts.mantisa = 0; |
102 | result.parts.fraction = 0; |
103 | result.parts.exp = 0xFF; |
103 | result.parts.exp = 0xFF; |
104 | return result; |
104 | return result; |
105 | 105 | ||
106 | } else if (exp <= 0 ) { |
106 | } else if (exp <= 0 ) { |
107 | 107 | ||
108 | /* underflow or denormalized */ |
108 | /* underflow or denormalized */ |
109 | 109 | ||
110 | result.parts.exp = 0; |
110 | result.parts.exp = 0; |
111 | 111 | ||
112 | exp *= -1; |
112 | exp *= -1; |
113 | if (exp > FLOAT32_MANTISA_SIZE ) { |
113 | if (exp > FLOAT32_FRACTION_SIZE ) { |
114 | /* FIXME: underflow */ |
114 | /* FIXME: underflow */ |
115 | result.parts.mantisa = 0; |
115 | result.parts.fraction = 0; |
116 | return result; |
116 | return result; |
117 | }; |
117 | }; |
118 | 118 | ||
119 | /* denormalized */ |
119 | /* denormalized */ |
120 | 120 | ||
121 | mant = a.parts.mantisa; |
121 | frac = a.parts.fraction; |
122 | mant |= 0x10000000000000ll; /* denormalize and set hidden bit */ |
122 | frac |= 0x10000000000000ll; /* denormalize and set hidden bit */ |
123 | 123 | ||
124 | mant >>= (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE + 1); |
124 | frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1); |
125 | 125 | ||
126 | while (exp > 0) { |
126 | while (exp > 0) { |
127 | --exp; |
127 | --exp; |
128 | mant >>= 1; |
128 | frac >>= 1; |
129 | }; |
129 | }; |
130 | result.parts.mantisa = mant; |
130 | result.parts.fraction = frac; |
131 | 131 | ||
132 | return result; |
132 | return result; |
133 | }; |
133 | }; |
134 | 134 | ||
135 | result.parts.exp = exp; |
135 | result.parts.exp = exp; |
136 | result.parts.mantisa = a.parts.mantisa >> (FLOAT64_MANTISA_SIZE - FLOAT32_MANTISA_SIZE); |
136 | result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE); |
137 | return result; |
137 | return result; |
138 | }; |
138 | }; |
139 | 139 |