mbgtools-lx  4.2.8
mbg_arch.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbg_arch.h 1.6 2017/01/27 09:03:16 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Definitions to support different computer hardware architectures.
10  *
11  * For a good summary of predefined macros which can be used to determine
12  * the build environment, the target environment, and architecture, see:
13  * http://sourceforge.net/p/predef/wiki/Home/
14  *
15  * -----------------------------------------------------------------------
16  * $Log: mbg_arch.h $
17  * Revision 1.6 2017/01/27 09:03:16 martin
18  * Added macros _mbg_swab8() and _mbg_swab64().
19  * Fixed macro syntax.
20  * Modified _swab_dummy() to avoid compiler warnings due to unused variables.
21  * Revision 1.5 2014/03/11 16:01:55 martin
22  * Added a comment.
23  * Revision 1.4 2012/10/02 18:32:00 martin
24  * Include words.h and, conditionally, stdlib.h.
25  * Use generic preprocessor symbol MBG_TGT_KERNEL.
26  * Revision 1.3 2009/06/12 13:12:37Z martin
27  * Fixed compiler warning.
28  * Revision 1.2 2009/03/19 15:14:15 martin
29  * Fixed byte swapping of doubles for SPARC architecture.
30  * Revision 1.1 2008/12/05 13:47:42 martin
31  * Initial revision.
32  *
33  **************************************************************************/
34 
35 #ifndef _MBG_ARCH_H
36 #define _MBG_ARCH_H
37 
38 #include <mbg_tgt.h>
39 #include <words.h>
40 
41 #if !defined( MBG_TGT_KERNEL )
42  #include <stdlib.h>
43 #endif
44 
45 
46 #if defined( MBG_ARCH_SPARC )
47  #define MBG_ARCH_BIG_ENDIAN 1
48 #endif
49 
50 
51 #if !defined( MBG_ARCH_BIG_ENDIAN )
52  #define MBG_ARCH_LITTLE_ENDIAN 1
53 #endif
54 
55 
56 
57 #if defined( MBG_TGT_LINUX )
58 
59  #include <asm/byteorder.h>
60 
61  #if defined( MBG_TGT_KERNEL )
62  #include <asm/unaligned.h>
63 
64  #define _mbg_put_unaligned( _v, _p ) put_unaligned( _v, _p )
65  #define _mbg_get_unaligned( _p ) get_unaligned( _p )
66  #endif
67 
68 #endif
69 
70 
71 
72 // If no macros required to access unaligned data have yet been defined,
73 // define some default macros assuming no special handling is required
74 // to access unaligned data.
75 
76 #if !defined( _mbg_put_unaligned )
77  #define _mbg_put_unaligned( _v, _p ) do { ((void)( *(_p) = (_v) )); } while ( 0 )
78 #endif
79 
80 #if !defined( _mbg_get_unaligned )
81  #define _mbg_get_unaligned( _p ) (*(_p))
82 #endif
83 
84 
85 
86 // If no macros to convert endianess have yet been defined, define
87 // some default macros assuming endianess conversion is not required.
88 
89 #if !defined( __le16_to_cpu )
90  #define __le16_to_cpu( _x ) (_x)
91 #endif
92 
93 #if !defined( __le32_to_cpu )
94  #define __le32_to_cpu( _x ) (_x)
95 #endif
96 
97 #if !defined( __le64_to_cpu )
98  #define __le64_to_cpu( _x ) (_x)
99 #endif
100 
101 #if !defined( __cpu_to_le16 )
102  #define __cpu_to_le16( _x ) (_x)
103 #endif
104 
105 #if !defined( __cpu_to_le32 )
106  #define __cpu_to_le32( _x ) (_x)
107 #endif
108 
109 #if !defined( __cpu_to_le64 )
110  #define __cpu_to_le64( _x ) (_x)
111 #endif
112 
113 
114 
115 // The macros below are used to convert the endianess
116 // of the plug-in cards to the endianess of the host CPU
117 
118 #define _mbg8_to_cpu( _x ) ( _x )
119 #define _mbg16_to_cpu( _x ) __le16_to_cpu( _x )
120 #define _mbg32_to_cpu( _x ) __le32_to_cpu( _x )
121 #define _mbg64_to_cpu( _x ) __le64_to_cpu( _x )
122 
123 #define _cpu_to_mbg8( _x ) ( _x )
124 #define _cpu_to_mbg16( _x ) __cpu_to_le16( _x )
125 #define _cpu_to_mbg32( _x ) __cpu_to_le32( _x )
126 #define _cpu_to_mbg64( _x ) __cpu_to_le64( _x )
127 
128 
129 
133 static __mbg_inline
134 void mbg_swab_double( double *p )
135 {
136 #if 0 // The __swab64() may not work correctly for whatever reason ...
137  __swab64p( p );
138 #else // ... so we do the swapping manually
139  double d = 0;
140  size_t i;
141 
142  for ( i = 0; i < sizeof( double); i++ )
143  BYTE_OF( d, i ) = BYTE_OF( *p, ( sizeof( double) - 1 - i ) );
144 
145  for ( i = 0; i < sizeof( double); i++ )
146  BYTE_OF( *p, i ) = BYTE_OF( d, i );
147 #endif
148 
149 } // mbg_swab_double
150 
151 
152 
153 #if defined( MBG_ARCH_BIG_ENDIAN )
154 
155  #define _mbg_swab8( _p ) _nop_macro_fnc() // always a dummy, but for completeness ...
156  #define _mbg_swab16( _p ) do { *(_p) = __swab16( *(_p) ); } while ( 0 )
157  #define _mbg_swab32( _p ) do { *(_p) = __swab32( *(_p) ); } while ( 0 )
158  #define _mbg_swab64( _p ) do { *(_p) = __swab64( *(_p) ); } while ( 0 )
159 
160  #define _mbg_swab_double( _p ) mbg_swab_double( _p )
161 
162  #define _mbg_swab_doubles( _p, _n ) \
163  do \
164  { \
165  int i; \
166  for ( i = 0; i < (_n); i++ ) \
167  _mbg_swab_double( &_p[i] ); \
168  } while ( 0 )
169 
170 #else
171 
172  #define _mbg_swab8( _p ) _nop_macro_fnc()
173  #define _mbg_swab16( _p ) _nop_macro_fnc()
174  #define _mbg_swab32( _p ) _nop_macro_fnc()
175  #define _mbg_swab64( _p ) _nop_macro_fnc()
176 
177  #define _mbg_swab_double( _p ) _nop_macro_fnc()
178 
179  #define _mbg_swab_doubles( _p, _n ) _nop_macro_fnc()
180 
181 #endif
182 
183 
184 
185 
192 #define _mbg_swab_dummy( _x ) do { (void) _x; } while ( 0 )
193 
194 
195 #endif /* _MBG_ARCH_H */
#define BYTE_OF(_v, _n)
Definition: words.h:440
static __mbg_inline void mbg_swab_double(double *p)
Swap a &#39;double&#39; type variable bytewise e.g. to convert the endianess.
Definition: mbg_arch.h:134