1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can
5  * be found in the LICENSE file.
6  *
7  */
8 
9 #pragma once
10 
11 //
12 //
13 //
14 
15 #include <stdint.h>
16 
17 //
18 //
19 //
20 
21 #define ARRAY_LENGTH_MACRO(x)   (sizeof(x)/sizeof(x[0]))
22 #define OFFSET_OF_MACRO(t,m)    ((size_t)&(((t*)0)->m))
23 #define MEMBER_SIZE_MACRO(t,m)  sizeof(((t*)0)->m)
24 
25 
26 //
27 //
28 //
29 
30 #define MAX_MACRO(a,b)          (((a) > (b)) ? (a) : (b))
31 #define MIN_MACRO(a,b)          (((a) < (b)) ? (a) : (b))
32 #define GTE_MACRO(a,b)          ((a) >= (b))
33 #define LT_MACRO(a,b)           ((a) <  (b))
34 
35 //
36 //
37 //
38 
39 #if defined( _MSC_VER )
40 #define ALLOCA_MACRO(n)         _alloca(n)
41 #else
42 #include <alloca.h>
43 #define ALLOCA_MACRO(n)         alloca(n)
44 #endif
45 
46 //
47 //
48 //
49 
50 #define BITS_TO_MASK(n)         (((uint32_t)1<<(n))-1)
51 #define BITS_TO_MASK_64(n)      (((uint64_t)1<<(n))-1)
52 
53 #define BITS_TO_MASK_AT(n,b)    (BITS_TO_MASK(n)<<(b))
54 #define BITS_TO_MASK_AT_64(n,b) (BITS_TO_MASK_64(n)<<(b))
55 
56 //
57 //
58 //
59 
60 #define STRINGIFY_MACRO_2(a)    #a
61 #define STRINGIFY_MACRO(a)      STRINGIFY_MACRO_2(a)
62 
63 //
64 //
65 //
66 
67 #define CONCAT_MACRO_2(a,b)     a ## b
68 #define CONCAT_MACRO(a,b)       CONCAT_MACRO_2(a,b)
69 
70 //
71 // Convert byte pointer to a network order 32-bit integer to host
72 // order.
73 //
74 
75 #define NPBTOHL_MACRO(pb4)      ((((pb4)[0])<<24) | (((pb4)[1])<<16) |  \
76                                  (((pb4)[2])<< 8) |   (pb4)[3])
77 
78 //
79 //
80 //
81 
82 #if   defined( _MSC_VER )
83 
84 #if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
85 #define NTOHL_MACRO(x)          _byteswap_ulong(x)
86 #else
87 #define NTOHL_MACRO(x)          x
88 #endif
89 
90 #elif defined( __GNUC__ )
91 
92 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
93 #define NTOHL_MACRO(x)          __builtin_bswap32(x)
94 #else
95 #define NTOHL_MACRO(x)          x
96 #endif
97 
98 #else
99 //
100 // FIXME -- CLANG, etc.
101 //
102 #endif
103 
104 //
105 //
106 //
107 
108 #if   defined( _MSC_VER )
109 
110 #define STATIC_ASSERT_MACRO(...) static_assert(__VA_ARGS__)
111 
112 #elif defined( __GNUC__ )
113 
114 #define STATIC_ASSERT_MACRO(...) _Static_assert(__VA_ARGS__)
115 
116 #else
117 //
118 // FIXME -- CLANG, etc.
119 //
120 #endif
121 
122 //
123 //
124 //
125 
126 #if   defined( _MSC_VER )
127 
128 #define POPCOUNT_MACRO(...) __popcnt(__VA_ARGS__)
129 
130 #elif defined( __GNUC__ )
131 
132 #define POPCOUNT_MACRO(...) __builtin_popcount(__VA_ARGS__)
133 
134 #else
135 //
136 // FIXME -- CLANG, etc.
137 //
138 #endif
139 
140 //
141 //
142 //
143