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