Merge pull request #22 from wirc-sjsu/develop-w21
[WRF-SFIRE.git] / external / io_grib1 / WGRIB / ibm2flt.c
blob769bd1debfbed19ac0996edbe73804773ae2e121
1 #include <stdio.h>
2 #include <stdlib.h>
4 /* ibm2flt wesley ebisuzaki
6 * v1.1 .. faster
7 * v1.1 .. if mant == 0 -> quick return
9 */
12 double ibm2flt(unsigned char *ibm) {
14 int positive, power;
15 unsigned int abspower;
16 long int mant;
17 double value, exp;
19 mant = (ibm[1] << 16) + (ibm[2] << 8) + ibm[3];
20 if (mant == 0) return 0.0;
22 positive = (ibm[0] & 0x80) == 0;
23 power = (int) (ibm[0] & 0x7f) - 64;
24 abspower = power > 0 ? power : -power;
27 /* calc exp */
28 exp = 16.0;
29 value = 1.0;
30 while (abspower) {
31 if (abspower & 1) {
32 value *= exp;
34 exp = exp * exp;
35 abspower >>= 1;
38 if (power < 0) value = 1.0 / value;
39 value = value * mant / 16777216.0;
40 if (positive == 0) value = -value;
41 return value;