|
1 /* GStreamer |
|
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu> |
|
3 * Library <2002> Steve Baker <stevebaker_org@yahoo.co.uk> |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Library General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Library General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Library General Public |
|
16 * License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 /** |
|
22 * SECTION:gstfloatcast |
|
23 * @short_description: Floating point platform independence macros |
|
24 * |
|
25 * The floatcast.h header file contains a couple of convenience macros for |
|
26 * floating point numbers. |
|
27 * |
|
28 * If you include this header, your application or library must link against |
|
29 * libm (for maths.h support). |
|
30 * |
|
31 * For optimal results, your application's or library's build |
|
32 * system should check whether the C99 functions lrint and lrintf are supported |
|
33 * and define the preprocessor symbols HAVE_LRINT and HAVE_LRINTF if so. If |
|
34 * you are using autoconf, you can do this by using the AC_C99_FUNC_LRINT and |
|
35 * AC_C99_FUNC_LRINTF checks in your configure.ac or configure.in file and |
|
36 * including your application's config.h header before you include floatcast.h. |
|
37 */ |
|
38 |
|
39 #ifndef __FLOATCAST_H__ |
|
40 #define __FLOATCAST_H__ |
|
41 |
|
42 #include <string.h> |
|
43 #include <glib/gtypes.h> |
|
44 #include <glib/gutils.h> /* to make sure inline is defined properly */ |
|
45 |
|
46 #if defined (_MSC_VER) && !defined (inline) |
|
47 #define inline __inline |
|
48 #endif |
|
49 |
|
50 G_BEGIN_DECLS |
|
51 |
|
52 /* FIXME 0.11: these gst_cast_*() functions are not used anywhere, so we could |
|
53 * just as well get rid of them and move the float/double swap macros into |
|
54 * gstutils.h in core */ |
|
55 |
|
56 /** |
|
57 * gst_cast_float: |
|
58 * @x: input value |
|
59 * |
|
60 * Casts a 32-bit floating point value (float) to an integer without bias. |
|
61 */ |
|
62 /** |
|
63 * gst_cast_double: |
|
64 * @x: input value |
|
65 * |
|
66 * Casts a 64-bit floating point value (double) to an integer without bias. |
|
67 */ |
|
68 |
|
69 /* FIXME: HAVE_LRINT && HAVE_LRINTF are defined by config.h - they should |
|
70 * not be used in an installed header. */ |
|
71 #if defined(HAVE_LRINT) && defined(HAVE_LRINTF) |
|
72 |
|
73 /* These defines enable functionality introduced with the 1999 ISO C |
|
74 ** standard. They must be defined before the inclusion of math.h to |
|
75 ** engage them. If optimisation is enabled, these functions will be |
|
76 ** inlined. With optimisation switched off, you have to link in the |
|
77 ** maths library using -lm. |
|
78 */ |
|
79 |
|
80 #define _ISOC9X_SOURCE 1 |
|
81 #define _ISOC99_SOURCE 1 |
|
82 |
|
83 #define __USE_ISOC9X 1 |
|
84 #define __USE_ISOC99 1 |
|
85 |
|
86 #include <math.h> |
|
87 |
|
88 #define gst_cast_float(x) ((gint)lrintf(x)) |
|
89 #define gst_cast_double(x) ((gint)lrint(x)) |
|
90 |
|
91 #else |
|
92 #include <math.h> |
|
93 |
|
94 /* use a standard c cast, but do rounding correctly */ |
|
95 #define gst_cast_float(x) ((gint)floor((x)+0.5)) |
|
96 #define gst_cast_double(x) ((gint)floor((x)+0.5)) |
|
97 |
|
98 #endif |
|
99 |
|
100 /* FIXME 0.11: don't use GLib namespace (GDOUBLE_SWAP_LE_BE, GFLOAT_TO_LE, |
|
101 * GFLOAT_TO_BE, GDOUBLE_TO_LE, GDOUBLE_TO_BE) */ |
|
102 |
|
103 /** |
|
104 * GFLOAT_SWAP_LE_BE: |
|
105 * @in: input value |
|
106 * |
|
107 * Swap byte order of a 32-bit floating point value (float). |
|
108 */ |
|
109 inline static gfloat |
|
110 GFLOAT_SWAP_LE_BE(gfloat in) |
|
111 { |
|
112 union |
|
113 { |
|
114 guint32 i; |
|
115 gfloat f; |
|
116 } u; |
|
117 |
|
118 u.f = in; |
|
119 u.i = GUINT32_SWAP_LE_BE (u.i); |
|
120 return u.f; |
|
121 } |
|
122 |
|
123 /** |
|
124 * GDOUBLE_SWAP_LE_BE: |
|
125 * @in: input value |
|
126 * |
|
127 * Swap byte order of a 64-bit floating point value (double). |
|
128 */ |
|
129 inline static gdouble |
|
130 GDOUBLE_SWAP_LE_BE(gdouble in) |
|
131 { |
|
132 union |
|
133 { |
|
134 guint64 i; |
|
135 gdouble d; |
|
136 } u; |
|
137 |
|
138 u.d = in; |
|
139 u.i = GUINT64_SWAP_LE_BE (u.i); |
|
140 return u.d; |
|
141 } |
|
142 |
|
143 /** |
|
144 * GDOUBLE_TO_LE: |
|
145 * @val: value |
|
146 * |
|
147 * Convert 64-bit floating point value (double) from native byte order into |
|
148 * little endian byte order. |
|
149 */ |
|
150 /** |
|
151 * GDOUBLE_TO_BE: |
|
152 * @val: value |
|
153 * |
|
154 * Convert 64-bit floating point value (double) from native byte order into |
|
155 * big endian byte order. |
|
156 */ |
|
157 /** |
|
158 * GDOUBLE_FROM_LE: |
|
159 * @val: value |
|
160 * |
|
161 * Convert 64-bit floating point value (double) from little endian byte order |
|
162 * into native byte order. |
|
163 */ |
|
164 /** |
|
165 * GDOUBLE_FROM_BE: |
|
166 * @val: value |
|
167 * |
|
168 * Convert 64-bit floating point value (double) from big endian byte order |
|
169 * into native byte order. |
|
170 */ |
|
171 |
|
172 /** |
|
173 * GFLOAT_TO_LE: |
|
174 * @val: value |
|
175 * |
|
176 * Convert 32-bit floating point value (float) from native byte order into |
|
177 * little endian byte order. |
|
178 */ |
|
179 /** |
|
180 * GFLOAT_TO_BE: |
|
181 * @val: value |
|
182 * |
|
183 * Convert 32-bit floating point value (float) from native byte order into |
|
184 * big endian byte order. |
|
185 */ |
|
186 /** |
|
187 * GFLOAT_FROM_LE: |
|
188 * @val: value |
|
189 * |
|
190 * Convert 32-bit floating point value (float) from little endian byte order |
|
191 * into native byte order. |
|
192 */ |
|
193 /** |
|
194 * GFLOAT_FROM_BE: |
|
195 * @val: value |
|
196 * |
|
197 * Convert 32-bit floating point value (float) from big endian byte order |
|
198 * into native byte order. |
|
199 */ |
|
200 |
|
201 #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
|
202 #define GFLOAT_TO_LE(val) ((gfloat) (val)) |
|
203 #define GFLOAT_TO_BE(val) (GFLOAT_SWAP_LE_BE (val)) |
|
204 #define GDOUBLE_TO_LE(val) ((gdouble) (val)) |
|
205 #define GDOUBLE_TO_BE(val) (GDOUBLE_SWAP_LE_BE (val)) |
|
206 |
|
207 #elif G_BYTE_ORDER == G_BIG_ENDIAN |
|
208 #define GFLOAT_TO_LE(val) (GFLOAT_SWAP_LE_BE (val)) |
|
209 #define GFLOAT_TO_BE(val) ((gfloat) (val)) |
|
210 #define GDOUBLE_TO_LE(val) (GDOUBLE_SWAP_LE_BE (val)) |
|
211 #define GDOUBLE_TO_BE(val) ((gdouble) (val)) |
|
212 |
|
213 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */ |
|
214 #error unknown ENDIAN type |
|
215 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */ |
|
216 |
|
217 #define GFLOAT_FROM_LE(val) (GFLOAT_TO_LE (val)) |
|
218 #define GFLOAT_FROM_BE(val) (GFLOAT_TO_BE (val)) |
|
219 #define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val)) |
|
220 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val)) |
|
221 |
|
222 G_END_DECLS |
|
223 |
|
224 #endif /* __FLOATCAST_H__ */ |
|
225 |