libmetal
utilities.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of Xilinx nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * @file utilities.h
33  * @brief Utility routines for libmetal.
34  */
35 
36 #ifndef __METAL_UTILITIES__H__
37 #define __METAL_UTILITIES__H__
38 
39 #include <assert.h>
40 #include <stdint.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
50 #define metal_unused(x) do { (x) = (x); } while (0)
51 
53 #define metal_dim(x) (sizeof(x) / sizeof(x[0]))
54 
56 #define metal_min(x, y) ((x) < (y) ? (x) : (y))
57 
59 #define metal_max(x, y) ((x) > (y) ? (x) : (y))
60 
62 #define metal_sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
63 
65 #define metal_align_down(size, align) \
66  ((size) & ~((align) - 1))
67 
69 #define metal_align_up(size, align) \
70  metal_align_down((size) + (align) - 1, align)
71 
73 #define metal_div_round_down(num, den) \
74  ((num) / (den))
75 
77 #define metal_div_round_up(num, den) \
78  metal_div_round_down((num) + (den) - 1, (den))
79 
81 #define metal_ptr_align_down(ptr, align) \
82  (void *)(metal_align_down((uintptr_t)(ptr), (uintptr_t)(align)))
83 
85 #define metal_ptr_align_up(ptr, align) \
86  (void *)(metal_align_up((uintptr_t)(ptr), (uintptr_t)(align)))
87 
89 #define metal_offset_of(structure, member) \
90  ((uintptr_t) &(((structure *) 0)->member))
91 
93 #define metal_container_of(ptr, structure, member) \
94  (void *)((uintptr_t)(ptr) - metal_offset_of(structure, member))
95 
96 #define METAL_BITS_PER_ULONG (8 * sizeof(unsigned long))
97 
98 #define metal_bit(bit) (1UL << (bit))
99 
100 #define metal_bitmap_longs(x) metal_div_round_up((x), METAL_BITS_PER_ULONG)
101 
102 static inline void metal_bitmap_set_bit(unsigned long *bitmap, int bit)
103 {
104  bitmap[bit / METAL_BITS_PER_ULONG] |=
105  metal_bit(bit & (METAL_BITS_PER_ULONG - 1));
106 }
107 
108 static inline int metal_bitmap_is_bit_set(unsigned long *bitmap, int bit)
109 {
110  return bitmap[bit / METAL_BITS_PER_ULONG] &
111  metal_bit(bit & (METAL_BITS_PER_ULONG - 1));
112 }
113 
114 static inline void metal_bitmap_clear_bit(unsigned long *bitmap, int bit)
115 {
116  bitmap[bit / METAL_BITS_PER_ULONG] &=
117  ~metal_bit(bit & (METAL_BITS_PER_ULONG - 1));
118 }
119 
120 static inline int metal_bitmap_is_bit_clear(unsigned long *bitmap, int bit)
121 {
122  return !metal_bitmap_is_bit_set(bitmap, bit);
123 }
124 
125 static inline unsigned int
126 metal_bitmap_next_set_bit(unsigned long *bitmap, unsigned int start,
127  unsigned int max)
128 {
129  unsigned int bit;
130  for (bit = start;
131  bit < max && !metal_bitmap_is_bit_set(bitmap, bit);
132  bit ++)
133  ;
134  return bit;
135 }
136 
137 #define metal_bitmap_for_each_set_bit(bitmap, bit, max) \
138  for ((bit) = metal_bitmap_next_set_bit((bitmap), 0, (max)); \
139  (bit) < (max); \
140  (bit) = metal_bitmap_next_set_bit((bitmap), (bit), (max)))
141 
142 static inline unsigned int
143 metal_bitmap_next_clear_bit(unsigned long *bitmap, unsigned int start,
144  unsigned int max)
145 {
146  unsigned int bit;
147  for (bit = start;
148  bit < max && !metal_bitmap_is_bit_clear(bitmap, bit);
149  bit ++)
150  ;
151  return bit;
152 }
153 
154 #define metal_bitmap_for_each_clear_bit(bitmap, bit, max) \
155  for ((bit) = metal_bitmap_next_clear_bit((bitmap), 0, (max)); \
156  (bit) < (max); \
157  (bit) = metal_bitmap_next_clear_bit((bitmap), (bit), (max)))
158 
159 static inline unsigned long metal_log2(unsigned long in)
160 {
161  unsigned long result;
162 
163  assert((in & (in - 1)) == 0);
164 
165  for (result = 0; (1UL << result) < in; result ++)
166  ;
167  return result;
168 }
169 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif /* __METAL_UTILITIES__H__ */
static unsigned long metal_log2(unsigned long in)
Definition: utilities.h:159
#define METAL_BITS_PER_ULONG
Definition: utilities.h:96
static void metal_bitmap_clear_bit(unsigned long *bitmap, int bit)
Definition: utilities.h:114
static void metal_bitmap_set_bit(unsigned long *bitmap, int bit)
Definition: utilities.h:102
#define metal_bit(bit)
Definition: utilities.h:98
static unsigned int metal_bitmap_next_clear_bit(unsigned long *bitmap, unsigned int start, unsigned int max)
Definition: utilities.h:143
static unsigned int metal_bitmap_next_set_bit(unsigned long *bitmap, unsigned int start, unsigned int max)
Definition: utilities.h:126
static int metal_bitmap_is_bit_clear(unsigned long *bitmap, int bit)
Definition: utilities.h:120
static int metal_bitmap_is_bit_set(unsigned long *bitmap, int bit)
Definition: utilities.h:108