Debian Codegraph @

lib/util/floatcode.h

raw
1 #ifndef FLOATCODE_H_INCLUDED
2 #define FLOATCODE_H_INCLUDED
3
4 #include "pm_config.h" /* BYTE_ORDER */
5
6 unsigned int const pm_byteOrder = BYTE_ORDER;
7
8
9 typedef struct {
10 /*----------------------------------------------------------------------------
11 This is a big-endian representation of a 32 bit floating point number.
12 I.e. bytes[0] contains the sign bit, etc.
13
14 On a big-endian machines, this is bit for bit identical to 'float'.
15 On a little-endian machine, it isn't.
16
17 This is an important data type because decent file formats use
18 big-endian -- they don't care if some CPU happens to use some other
19 code for its own work.
20 -----------------------------------------------------------------------------*/
21 unsigned char bytes[4];
22 } pm_bigendFloat;
23
24
25 static __inline__ float
26 pm_floatFromBigendFloat(pm_bigendFloat const arg) {
27
28 float retval;
29
30 switch (pm_byteOrder) {
31 case BIG_ENDIAN: {
32 union {
33 pm_bigendFloat bigend;
34 float native;
35 } converter;
36
37 converter.bigend = arg;
38
39 retval = converter.native;
40 }; break;
41 case LITTLE_ENDIAN: {
42 union {
43 unsigned char bytes[4];
44 float native;
45 } converter;
46
47 converter.bytes[0] = arg.bytes[3];
48 converter.bytes[1] = arg.bytes[2];
49 converter.bytes[2] = arg.bytes[1];
50 converter.bytes[3] = arg.bytes[0];
51
52 retval = converter.native;
53 } break;
54 }
55 return retval;
56 }
57
58
59
60 static __inline__ pm_bigendFloat
61 pm_bigendFloatFromFloat(float const arg) {
62
63 pm_bigendFloat retval;
64
65 switch (pm_byteOrder) {
66 case BIG_ENDIAN: {
67 union {
68 pm_bigendFloat bigend;
69 float native;
70 } converter;
71
72 converter.native = arg;
73
74 retval = converter.bigend;
75 } break;
76 case LITTLE_ENDIAN: {
77 union {
78 unsigned char bytes[4];
79 float native;
80 } converter;
81
82 converter.native = arg;
83
84 retval.bytes[0] = converter.bytes[3];
85 retval.bytes[1] = converter.bytes[2];
86 retval.bytes[2] = converter.bytes[1];
87 retval.bytes[3] = converter.bytes[0];
88 } break;
89 }
90 return retval;
91 }
92
93 typedef struct {
94 /*----------------------------------------------------------------------------
95 This is a big-endian representation of a 64 bit floating point number.
96 I.e. bytes[0] contains the sign bit, etc.
97
98 On a big-endian machines, this is bit for bit identical to 'float'.
99 On a little-endian machine, it isn't.
100
101 This is an important data type because decent file formats use
102 big-endian -- they don't care if some CPU happens to use some other
103 code for its own work.
104 -----------------------------------------------------------------------------*/
105 unsigned char bytes[8];
106 } pm_bigendDouble;
107
108
109 static __inline__ double
110 pm_doubleFromBigendDouble(pm_bigendDouble const arg) {
111
112 double retval;
113
114 switch (pm_byteOrder) {
115 case BIG_ENDIAN: {
116 union {
117 pm_bigendDouble bigend;
118 double native;
119 } converter;
120
121 converter.bigend = arg;
122
123 retval = converter.native;
124 }; break;
125 case LITTLE_ENDIAN: {
126 union {
127 unsigned char bytes[8];
128 double native;
129 } converter;
130
131 converter.bytes[0] = arg.bytes[7];
132 converter.bytes[1] = arg.bytes[6];
133 converter.bytes[2] = arg.bytes[5];
134 converter.bytes[3] = arg.bytes[4];
135 converter.bytes[4] = arg.bytes[3];
136 converter.bytes[5] = arg.bytes[2];
137 converter.bytes[6] = arg.bytes[1];
138 converter.bytes[7] = arg.bytes[0];
139
140 retval = converter.native;
141 } break;
142 }
143 return retval;
144 }
145
146
147
148 static __inline__ pm_bigendDouble
149 pm_bigendDoubleFromDouble(double const arg) {
150
151 pm_bigendDouble retval;
152
153 switch (pm_byteOrder) {
154 case BIG_ENDIAN: {
155 union {
156 pm_bigendDouble bigend;
157 double native;
158 } converter;
159
160 converter.native = arg;
161
162 retval = converter.bigend;
163 } break;
164 case LITTLE_ENDIAN: {
165 union {
166 unsigned char bytes[8];
167 double native;
168 } converter;
169
170 converter.native = arg;
171
172 retval.bytes[0] = converter.bytes[7];
173 retval.bytes[1] = converter.bytes[6];
174 retval.bytes[2] = converter.bytes[5];
175 retval.bytes[3] = converter.bytes[4];
176 retval.bytes[4] = converter.bytes[3];
177 retval.bytes[5] = converter.bytes[2];
178 retval.bytes[6] = converter.bytes[1];
179 retval.bytes[7] = converter.bytes[0];
180 } break;
181 }
182 return retval;
183 }
184
185 #endif