2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Library <2002> Steve Baker <stevebaker_org@yahoo.co.uk>
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.
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.
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.
21 #ifndef __FLOATCAST_H__
22 #define __FLOATCAST_H__
29 #include <glib/gtypes.h>
33 #if (HAVE_LRINT && HAVE_LRINTF)
35 /* These defines enable functionality introduced with the 1999 ISO C
36 ** standard. They must be defined before the inclusion of math.h to
37 ** engage them. If optimisation is enabled, these functions will be
38 ** inlined. With optimisation switched off, you have to link in the
39 ** maths library using -lm.
42 #define _ISOC9X_SOURCE 1
43 #define _ISOC99_SOURCE 1
45 #define __USE_ISOC9X 1
46 #define __USE_ISOC99 1
50 #define gst_cast_float(x) ((gint)lrintf(x))
51 #define gst_cast_double(x) ((gint)lrint(x))
54 /* use a standard c cast, but do rounding correctly */
55 #define gst_cast_float(x) ((gint)floor((x)+0.5))
56 #define gst_cast_double(x) ((gint)floor((x)+0.5))
61 GFLOAT_SWAP_LE_BE(gfloat in)
65 memcpy(&swap, &in, 4);
66 swap = GUINT32_SWAP_LE_BE_CONSTANT (swap);
67 memcpy(&out, &swap, 4);
72 GDOUBLE_SWAP_LE_BE(gdouble in)
76 memcpy(&swap, &in, 8);
77 swap = GUINT64_SWAP_LE_BE_CONSTANT (swap);
78 memcpy(&out, &swap, 8);
82 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
83 #define GFLOAT_TO_LE(val) ((gfloat) (val))
84 #define GFLOAT_TO_BE(val) (GFLOAT_SWAP_LE_BE (val))
85 #define GDOUBLE_TO_LE(val) ((gdouble) (val))
86 #define GDOUBLE_TO_BE(val) (GDOUBLE_SWAP_LE_BE (val))
88 #elif G_BYTE_ORDER == G_BIG_ENDIAN
89 #define GFLOAT_TO_LE(val) (GFLOAT_SWAP_LE_BE (val))
90 #define GFLOAT_TO_BE(val) ((gfloat) (val))
91 #define GDOUBLE_TO_LE(val) (GDOUBLE_SWAP_LE_BE (val))
92 #define GDOUBLE_TO_BE(val) ((gdouble) (val))
94 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
95 #error unknown ENDIAN type
96 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
98 #define GFLOAT_FROM_LE(val) (GFLOAT_TO_LE (val))
99 #define GFLOAT_FROM_BE(val) (GDOUBLE_TO_BE (val))
100 #define GDOUBLE_FROM_LE(val) (GFLOAT_TO_LE (val))
101 #define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
105 #endif /* __FLOATCAST_H__ */