KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
macro.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : utils.h
11  * Created : 22 feb. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #ifndef MACRO_H_
17 #define MACRO_H_
18 
19 /**
20  * @file
21  *
22  * @ingroup util
23  *
24  * Provides common macros.
25  */
26 #define _EVAL(MACRO) MACRO
27 
28 #define EVAL(MACRO) _EVAL(MACRO)
29 
30 #define _STR(EXPR) #EXPR
31 
32 /**
33  * Stringyfies an expression.
34  *
35  * @param EXPR The expression to turn into a string
36  */
37 #define STR(EXPR) _STR(EXPR)
38 
39 /**
40  * Re-scales the input value into the range of the output value.
41  */
42 #define RESCALE(INPUT, IN_MIN, IN_MAX, OUT_MIN, OUT_MAX) \
43  ( ( ( ( ( INPUT ) - ( IN_MIN ) ) * \
44  ( ( OUT_MAX ) - ( OUT_MIN ) ) + ( ( IN_MAX ) - ( IN_MIN ) ) / 2 ) / \
45  ( ( IN_MAX ) - ( IN_MIN ) ) ) + ( OUT_MIN ) )
46 
47 
48 #define COERCE(INPUT, MIN, MAX) \
49  ( ( INPUT ) > ( MAX ) ? ( MAX ) : ( ( INPUT ) < ( MIN ) ? ( MIN ) : ( INPUT ) ) )
50 
51 /// Private Cyclic Comparison macro, please don't use.
52 #define _CCMP(A, B, BITS) \
53  (((A) - (B)) & (1 << (BITS - 1)))
54 
55 
56 
57 /**
58  * \brief Cyclic comparison function Greater Than.
59  *
60  * Evaluates if A > B, but only within bit bounds.
61  *
62  * @param A Value A
63  * @param B Value B
64  * @param BITS Bits to check.
65  */
66 #define CGT(A, B, BITS) \
67  (_CCMP(B, A, BITS) != 0)
68 
69 /**
70  * \brief Cyclic comparison function Greater Of Equal To.
71  *
72  * Evaluates if A >= B, but only within bit bounds.
73  *
74  * @param A Value A
75  * @param B Value B
76  * @param BITS Bits to check.
77  */
78 #define CGE(A, B, BITS) \
79  (_CCMP(A, B, BITS) == 0)
80 
81 /**
82  * \brief Cyclic comparison function Less Than.
83  *
84  * Evaluates if A < B, but only within bit bounds.
85  *
86  * @param A Value A
87  * @param B Value B
88  * @param BITS Bits to check.
89  */
90 #define CLT(A, B, BITS) \
91  (_CCMP(A, B, BITS) != 0)
92 
93 /**
94  * \brief Cyclic comparison function Less Or Equal To.
95  *
96  * Evaluates if A <= B, but only within bit bounds.
97  *
98  * @param A Value A
99  * @param B Value B
100  * @param BITS Bits to check.
101  */
102 #define CLE(A, B, BITS) \
103  (_CCMP(B, A, BITS) == 0)
104 
105 /**
106  * Makes a value with the specified bit set.
107  */
108 #define BIT(N) ( 1 << ( N ) )
109 
110 /**
111  * Creates a mask with the specified offset and length.
112  */
113 #define MASK(LEN) ( ( 1 << ( LEN ) ) - 1 )
114 
115 /**
116  * Extracts a bitfield
117  */
118 #define EXT_BITFIELD(VAL, OFF, LEN) \
119  ( ( ( VAL ) >> ( OFF ) ) & MASK( LEN ) )
120 
121 /**
122  * Little Endian to native short.
123  */
124 #define _BR_SHORT(SHORT) \
125  ( ( ( 0xFF00L & SHORT ) >> 8 ) | ( ( 0x00FFL & SHORT ) << 8 ) )
126 
127 #define _BR_LONG(LONG) \
128  ( _BR_SHORT ( ( 0xFFFF0000L & LONG ) >> 16 ) | ( ( 0x0000FFFFL & _BR_SHORT(SHORT) ) << 16 ) )
129 
130 
131 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
132 
133 #define N2LES(SHORT) _BR_SHORT(SHORT)
134 #define LE2NS(SHORT) _BR_SHORT(SHORT)
135 #define N2LEL(LONG) _BR_LONG(LONG)
136 #define LE2NL(LONG) _BR_LONG(LONG)
137 
138 #define N2BES(SHORT) ( SHORT )
139 #define BE2NS(SHORT) ( SHORT )
140 #define N2BEL(LONG) ( LONG )
141 #define BE2NL(LONG) ( LONG )
142 
143 
144 #else
145 
146 #if __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
147 
148 #define N2BES(SHORT) _BR_SHORT(SHORT)
149 #define BE2NS(SHORT) _BR_SHORT(SHORT)
150 #define N2BEL(LONG) _BR_LONG(LONG)
151 #define BE2NL(LONG) _BR_LONG(LONG)
152 
153 #define N2LES(SHORT) ( SHORT )
154 #define LE2NS(SHORT) ( SHORT )
155 #define N2LEL(LONG) ( LONG )
156 #define LE2NL(LONG) ( LONG )
157 
158 #else
159 
160 #error "Platform endianess could not be detected"
161 
162 #endif
163 
164 #endif
165 
166 //! Returns the length of the array
167 #define arraylength(N) ( sizeof(N) / sizeof(*(N)) )
168 
169 #endif /* UTILS_H_ */