converts a packed array of bytes into a 32bit float. there can be an exponent overflow the mantissa is truncated to 23 bits.
| 146 | // there can be an exponent overflow |
| 147 | // the mantissa is truncated to 23 bits. |
| 148 | float doublePacked2Float(byte* bar, int byteOrder = LSBFIRST) |
| 149 | { |
| 150 | _FLOATCONV fl; |
| 151 | _DBLCONV dbl; |
| 152 | |
| 153 | #ifdef IEEE754_ENABLE_MSB |
| 154 | if (byteOrder == LSBFIRST) |
| 155 | { |
| 156 | #endif |
| 157 | for (int i = 0; i < 8; i++) |
| 158 | { |
| 159 | dbl.b[i] = bar[i]; |
| 160 | } |
| 161 | #ifdef IEEE754_ENABLE_MSB |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | for (int i = 0; i < 8; i++) |
| 166 | { |
| 167 | dbl.b[i] = bar[7-i]; |
| 168 | } |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | int e = dbl.p.e - 1023 + 127; // exponent adjust |
| 173 | // TODO check exponent overflow. |
| 174 | if (e >=0 || e <= 255) |
| 175 | { |
| 176 | fl.p.s = dbl.p.s; |
| 177 | fl.p.e = e; |
| 178 | fl.p.m = dbl.p.m; // note this one clips the mantisse |
| 179 | return fl.f; |
| 180 | } |
| 181 | return NAN; // OR +-INF? |
| 182 | // return (fl.p.s) ? -INFINITY : INFINITY; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | // |