Rev 698 | Rev 857 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
647 | cejka | 1 | /* |
2 | * Copyright (C) 2005 Josef Cejka |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
697 | cejka | 29 | #include "sftypes.h" |
30 | #include "conversion.h" |
||
31 | |||
32 | float64 convertFloat32ToFloat64(float32 a) |
||
33 | { |
||
34 | float64 result; |
||
804 | cejka | 35 | __u64 frac; |
697 | cejka | 36 | |
37 | result.parts.sign = a.parts.sign; |
||
804 | cejka | 38 | result.parts.fraction = a.parts.fraction; |
39 | result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE ); |
||
697 | cejka | 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 | |||
804 | cejka | 51 | if (result.parts.fraction == 0ll) { /* fix zero */ |
697 | cejka | 52 | result.parts.exp = 0ll; |
53 | return result; |
||
54 | } |
||
55 | |||
804 | cejka | 56 | frac = result.parts.fraction; |
697 | cejka | 57 | |
804 | cejka | 58 | while (!(frac & (0x10000000000000ll))) { |
59 | frac <<= 1; |
||
697 | cejka | 60 | --result.parts.exp; |
61 | }; |
||
698 | cejka | 62 | |
63 | ++result.parts.exp; |
||
804 | cejka | 64 | result.parts.fraction = frac; |
697 | cejka | 65 | }; |
66 | |||
67 | return result; |
||
68 | |||
69 | }; |
||
70 | |||
71 | float32 convertFloat64ToFloat32(float64 a) |
||
72 | { |
||
73 | float32 result; |
||
74 | __s32 exp; |
||
804 | cejka | 75 | __u64 frac; |
697 | cejka | 76 | |
77 | result.parts.sign = a.parts.sign; |
||
78 | |||
79 | if (isFloat64NaN(a)) { |
||
80 | |||
81 | result.parts.exp = 0xFF; |
||
82 | |||
83 | if (isFloat64SigNaN(a)) { |
||
804 | cejka | 84 | result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */ |
697 | cejka | 85 | return result; |
86 | } |
||
87 | |||
804 | cejka | 88 | result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */ |
697 | cejka | 89 | return result; |
90 | }; |
||
91 | |||
92 | if (isFloat64Infinity(a)) { |
||
804 | cejka | 93 | result.parts.fraction = 0; |
697 | cejka | 94 | result.parts.exp = 0xFF; |
95 | return result; |
||
96 | }; |
||
97 | |||
98 | exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS; |
||
99 | |||
100 | if (exp >= 0xFF) { |
||
101 | /*FIXME: overflow*/ |
||
804 | cejka | 102 | result.parts.fraction = 0; |
697 | cejka | 103 | result.parts.exp = 0xFF; |
104 | return result; |
||
105 | |||
106 | } else if (exp <= 0 ) { |
||
107 | |||
108 | /* underflow or denormalized */ |
||
109 | |||
110 | result.parts.exp = 0; |
||
111 | |||
112 | exp *= -1; |
||
804 | cejka | 113 | if (exp > FLOAT32_FRACTION_SIZE ) { |
697 | cejka | 114 | /* FIXME: underflow */ |
804 | cejka | 115 | result.parts.fraction = 0; |
697 | cejka | 116 | return result; |
117 | }; |
||
118 | |||
119 | /* denormalized */ |
||
120 | |||
804 | cejka | 121 | frac = a.parts.fraction; |
122 | frac |= 0x10000000000000ll; /* denormalize and set hidden bit */ |
||
697 | cejka | 123 | |
804 | cejka | 124 | frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1); |
698 | cejka | 125 | |
697 | cejka | 126 | while (exp > 0) { |
127 | --exp; |
||
804 | cejka | 128 | frac >>= 1; |
697 | cejka | 129 | }; |
804 | cejka | 130 | result.parts.fraction = frac; |
697 | cejka | 131 | |
132 | return result; |
||
133 | }; |
||
134 | |||
135 | result.parts.exp = exp; |
||
804 | cejka | 136 | result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE); |
697 | cejka | 137 | return result; |
138 | }; |
||
139 |