/src/gdal/frmts/grib/degrib/g2clib/mkieee.c
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | #include <math.h> |
3 | | #include "grib2.h" |
4 | | |
5 | | |
6 | | void mkieee(g2float *a,g2int *rieee,g2int num) |
7 | | //$$$ SUBPROGRAM DOCUMENTATION BLOCK |
8 | | // . . . . |
9 | | // SUBPROGRAM: mkieee |
10 | | // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-10-29 |
11 | | // |
12 | | // ABSTRACT: This subroutine stores a list of real values in |
13 | | // 32-bit IEEE floating point format. |
14 | | // |
15 | | // PROGRAM HISTORY LOG: |
16 | | // 2002-10-29 Gilbert |
17 | | // |
18 | | // USAGE: mkieee(g2float *a,g2int *rieee,g2int num); |
19 | | // INPUT ARGUMENT LIST: |
20 | | // a - Input array of floating point values. |
21 | | // num - Number of floating point values to convert. |
22 | | // |
23 | | // OUTPUT ARGUMENT LIST: |
24 | | // rieee - Output array of data values in 32-bit IEEE format |
25 | | // stored in g2int integer array. rieee must be allocated |
26 | | // with at least 4*num bytes of memory before calling this |
27 | | // function. |
28 | | // |
29 | | // REMARKS: None |
30 | | // |
31 | | // ATTRIBUTES: |
32 | | // LANGUAGE: C |
33 | | // MACHINE: |
34 | | // |
35 | | //$$$ |
36 | 30 | { |
37 | | |
38 | 30 | g2int j,n,ieee,iexp,imant; |
39 | 30 | double /* alog2, */ atemp; |
40 | | |
41 | 30 | static const double two23 = 8388608.0; // pow(2,23) |
42 | 30 | static const double two126 = 8.507059173023462e+37; // pow(2,126) |
43 | | |
44 | | //g2intu msk1=0x80000000; // 10000000000000000000000000000000 binary |
45 | | //g2int msk2=0x7F800000; // 01111111100000000000000000000000 binary |
46 | | //g2int msk3=0x007FFFFF; // 00000000011111111111111111111111 binary |
47 | | |
48 | | // alog2=0.69314718; // ln(2.0) |
49 | | |
50 | 60 | for (j=0;j<num;j++) { |
51 | | |
52 | 30 | ieee=0; |
53 | | |
54 | 30 | if (a[j] == 0.0) { |
55 | 19 | rieee[j]=ieee; |
56 | 19 | continue; |
57 | 19 | } |
58 | | |
59 | | // |
60 | | // Set Sign bit (bit 31 - leftmost bit) |
61 | | // |
62 | 11 | if (a[j] < 0.0) { |
63 | 0 | ieee= (g2int) (1U << 31); |
64 | 0 | atemp=-1.0*a[j]; |
65 | 0 | } |
66 | 11 | else { |
67 | 11 | ieee= 0 << 31; |
68 | 11 | atemp=a[j]; |
69 | 11 | } |
70 | | //printf("sign %ld %x \n",ieee,ieee); |
71 | | // |
72 | | // Determine exponent n with base 2 |
73 | | // |
74 | 11 | if ( atemp >= 1.0 ) { |
75 | 11 | n = 0; |
76 | 39 | while ( int_power(2.0,n+1) <= atemp ) { |
77 | 28 | n++; |
78 | 28 | } |
79 | 11 | } |
80 | 0 | else { |
81 | 0 | n = -1; |
82 | 0 | while ( int_power(2.0,n) > atemp ) { |
83 | 0 | n--; |
84 | 0 | } |
85 | 0 | } |
86 | | //n=(g2int)floor(log(atemp)/alog2); |
87 | 11 | iexp=n+127; |
88 | 11 | if (n > 127) iexp=255; // overflow |
89 | 11 | if (n < -127) iexp=0; |
90 | | //printf("exp %ld %ld \n",iexp,n); |
91 | | // set exponent bits ( bits 30-23 ) |
92 | 11 | ieee = ieee | ( iexp << 23 ); |
93 | | // |
94 | | // Determine Mantissa |
95 | | // |
96 | 11 | if (iexp != 255) { |
97 | 11 | if (iexp != 0) |
98 | 11 | atemp=(atemp/int_power(2.0,n))-1.0; |
99 | 0 | else |
100 | 0 | atemp=atemp*two126; |
101 | 11 | imant=(g2int)RINT(atemp*two23); |
102 | 11 | } |
103 | 0 | else { |
104 | 0 | imant=0; |
105 | 0 | } |
106 | | //printf("mant %ld %x \n",imant,imant); |
107 | | // set mantissa bits ( bits 22-0 ) |
108 | 11 | ieee = ieee | imant; |
109 | | // |
110 | | // Transfer IEEE bit string to rieee array |
111 | | // |
112 | 11 | rieee[j]=ieee; |
113 | | |
114 | 11 | } |
115 | | |
116 | 30 | return; |
117 | | |
118 | 30 | } |