expand tabs
[platform/upstream/gstreamer.git] / gst-libs / gst / floatcast / floatcast.h
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 #ifndef __FLOATCAST_H__
22 #define __FLOATCAST_H__
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <string.h>
29 #include <glib/gtypes.h>
30
31 G_BEGIN_DECLS
32
33 #if (HAVE_LRINT && HAVE_LRINTF)
34
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.
40         */
41
42         #define _ISOC9X_SOURCE  1
43         #define _ISOC99_SOURCE  1
44
45         #define __USE_ISOC9X    1
46         #define __USE_ISOC99    1
47
48         #include        <math.h>
49
50         #define gst_cast_float(x)       ((gint)lrintf(x))
51         #define gst_cast_double(x)      ((gint)lrint(x))
52
53 #else
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))
57
58 #endif
59
60 inline static gfloat
61 GFLOAT_SWAP_LE_BE(gfloat in)
62 {
63   gint32 swap;
64   gfloat out;
65   memcpy(&swap, &in, 4);
66   swap = GUINT32_SWAP_LE_BE_CONSTANT (swap);
67   memcpy(&out, &swap, 4);
68   return out;
69 }
70
71 inline static gdouble
72 GDOUBLE_SWAP_LE_BE(gdouble in)
73 {
74   gint64 swap;
75   gdouble out;
76   memcpy(&swap, &in, 8);
77   swap = GUINT64_SWAP_LE_BE_CONSTANT (swap);
78   memcpy(&out, &swap, 8);
79   return out;
80 }
81
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))
87
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))
93
94 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
95 #error unknown ENDIAN type
96 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
97
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))
102
103 G_END_DECLS
104
105 #endif /* __FLOATCAST_H__ */
106