FreeNOS
Macros.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __MACROS_H
19 #define __MACROS_H
20 
21 #include "Types.h"
22 
32 #define TRUE 1
33 
35 #define FALSE 0
36 
38 #ifndef NULL
39 #define NULL (void *)0
40 #endif
41 
43 #define ZERO 0
44 
46 #define QUOTE(x) \
47  #x
48 
50 #define IQUOTE(x) \
51  QUOTE(x)
52 
54 #define KiloByte(v) ((v) * 1024)
55 
57 #define MegaByte(v) ((v) * 1024 * 1024)
58 
60 #define GigaByte(v) ((v) * 1024 * 1024 * 1024)
61 
63 #define LONG_MIN -2147483647L
64 
66 #define LONG_MAX 2147483647L
67 
68 #define ULONG_MIN 0LU
69 
70 #define ULONG_MAX 4294967295LU
71 
72 #define INT_MIN -2147483647
73 
74 #define INT_MAX 2147483647
75 
76 #define UINT_MIN 0U
77 
78 #define UINT_MAX 4294967295U
79 
88 #define CEIL(number,divisor) \
89  ( (number) % (divisor) ? \
90  ((number) / (divisor)) + 1 : (number) / (divisor))
91 
92 #ifdef __cplusplus
93 
101 inline bool isPowerOfTwo(unsigned number)
102 {
103  return (number != 0) && ((number & (number - 1)) == 0);
104 }
105 
113 inline double doubleAbsolute(double number)
114 {
115  return number < 0 ? -number : number;
116 }
117 
121 inline bool doubleEquals(double a, double b, double epsilon)
122 {
123  return doubleAbsolute(a - b) < epsilon;
124 }
125 
126 #endif /* __cplusplus */
127 
129 #define offsetof(TYPE, MEMBER) \
130  ((Size) &((TYPE *)0)->MEMBER)
131 
133 #ifdef __cplusplus
134 #define C "C"
135 #define CPP
136 #else
137 #define C
138 #endif /* c_plusplus */
139 
145 #define SECTION(s) \
146  __attribute__((__section__(s)))
147 
151 #define USED \
152  __attribute__((__used__))
153 
159 #define PACKED \
160  __attribute__((__packed__))
161 
167 #define ALIGN(n) \
168  __attribute__((aligned(n)))
169 
175 #endif /* __MACROS_H */
Types.h
doubleAbsolute
double doubleAbsolute(double number)
Absolute value of a double.
Definition: Macros.h:113
doubleEquals
bool doubleEquals(double a, double b, double epsilon)
Compare two doubles using a epsilon number as precision indicator.
Definition: Macros.h:121
isPowerOfTwo
bool isPowerOfTwo(unsigned number)
Check if a number is power of two.
Definition: Macros.h:101