NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
mkieee.c
Go to the documentation of this file.
1
6#include "grib2_int.h"
7#include <math.h>
8#include <stdlib.h>
9
21void
22mkieee(float *a, g2int *rieee, g2int num)
23{
24 g2int j, n, ieee, iexp, imant;
25 double atemp;
26 static double two23, two126;
27 static g2int test = 0;
28
29 if (test == 0)
30 {
31 two23 = (double)int_power(2.0, 23);
32 two126 = (double)int_power(2.0, 126);
33 test = 1;
34 }
35
36 for (j = 0; j < num; j++)
37 {
38
39 ieee = 0;
40
41 if (a[j] == 0.0)
42 {
43 rieee[j] = ieee;
44 continue;
45 }
46
47 // Set Sign bit (bit 31 - leftmost bit).
48 if (a[j] < 0.0)
49 {
50 ieee = 1 << 31;
51 atemp = -1.0 * a[j];
52 }
53 else
54 {
55 ieee = 0 << 31;
56 atemp = a[j];
57 }
58
59 // Determine exponent n with base 2.
60 if (atemp >= 1.0)
61 {
62 n = 0;
63 while (int_power(2.0, n + 1) <= atemp)
64 {
65 n++;
66 }
67 }
68 else
69 {
70 n = -1;
71 while (int_power(2.0, n) > atemp)
72 n--;
73 }
74 iexp = n + 127;
75 if (n > 127)
76 iexp = 255; // overflow
77 if (n < -127)
78 iexp = 0;
79
80 // set exponent bits ( bits 30-23 )
81 ieee = ieee | (iexp << 23);
82
83 // Determine Mantissa
84 if (iexp != 255)
85 {
86 if (iexp != 0)
87 atemp = (atemp / int_power(2.0, n)) - 1.0;
88 else
89 atemp = atemp * two126;
90 imant = (g2int)rint(atemp * two23);
91 }
92 else
93 {
94 imant = 0;
95 }
96
97 // set mantissa bits ( bits 22-0 )
98 ieee = ieee | imant;
99
100 // Transfer IEEE bit string to rieee array
101 rieee[j] = ieee;
102 }
103
104 return;
105}
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition int_power.c:18
void mkieee(float *a, g2int *rieee, g2int num)
Store a list of real values in 32-bit IEEE floating point format.
Definition mkieee.c:22