Motr  M0
arith.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #pragma once
24 
25 #ifndef __MOTR_LIB_ARITH_H__
26 #define __MOTR_LIB_ARITH_H__
27 
28 #include "lib/types.h"
29 #include "lib/assert.h"
30 
36 static inline int32_t min32(int32_t a, int32_t b)
37 {
38  return a < b ? a : b;
39 }
40 
41 static inline int32_t max32(int32_t a, int32_t b)
42 {
43  return a > b ? a : b;
44 }
45 
46 static inline int64_t min64(int64_t a, int64_t b)
47 {
48  return a < b ? a : b;
49 }
50 
51 static inline int64_t max64(int64_t a, int64_t b)
52 {
53  return a > b ? a : b;
54 }
55 
56 static inline uint32_t min32u(uint32_t a, uint32_t b)
57 {
58  return a < b ? a : b;
59 }
60 
61 static inline uint32_t max32u(uint32_t a, uint32_t b)
62 {
63  return a > b ? a : b;
64 }
65 
66 static inline uint64_t min64u(uint64_t a, uint64_t b)
67 {
68  return a < b ? a : b;
69 }
70 
71 static inline uint64_t max64u(uint64_t a, uint64_t b)
72 {
73  return a > b ? a : b;
74 }
75 
76 #define min_type(t, a, b) ({ \
77  t __a = (a); \
78  t __b = (b); \
79  __a < __b ? __a : __b; \
80 })
81 
82 #define max_type(t, a, b) ({ \
83  t __a = (a); \
84  t __b = (b); \
85  __a > __b ? __a : __b; \
86 })
87 
88 #define min_check(a, b) ({ \
89  typeof(a) __a = (a); \
90  typeof(b) __b = (b); \
91  (void)(&__a == &__b); \
92  __a < __b ? __a : __b; \
93 })
94 
95 #define max_check(a, b) ({ \
96  typeof(a) __a = (a); \
97  typeof(b) __b = (b); \
98  (void)(&__a == &__b); \
99  __a > __b ? __a : __b; \
100 })
101 
102 #ifndef __KERNEL__
103 # define min3(a, b, c) (min_check((a), min_check((b), (c))))
104 # define max3(a, b, c) (max_check((a), max_check((b), (c))))
105 #endif
106 
122 M0_INTERNAL bool m0_mod_gt(uint64_t x0, uint64_t x1);
123 
129 M0_INTERNAL bool m0_mod_ge(uint64_t x0, uint64_t x1);
130 
131 static inline uint64_t m0_clip64u(uint64_t lo, uint64_t hi, uint64_t x)
132 {
133  M0_PRE(lo <= hi);
134  return min64u(max64u(lo, x), hi);
135 }
136 
145 M0_INTERNAL uint64_t m0_rnd(uint64_t max, uint64_t * seed);
146 M0_INTERNAL uint64_t m0_rnd64(uint64_t *seed);
147 
151 M0_INTERNAL uint64_t m0_gcd64(uint64_t p, uint64_t q);
152 
153 static inline bool m0_is_po2(uint64_t val)
154 {
155  return (val & (val - 1)) == 0;
156 }
157 
161 static inline unsigned m0_log2(uint64_t val)
162 {
163  unsigned shift = 0;
164 
165  for (val >>= 1; val != 0; val >>= 1)
166  ++shift;
167  return shift;
168 }
169 
170 static inline uint64_t m0_align(uint64_t val, uint64_t alignment)
171 {
172  uint64_t mask;
173 
174  M0_PRE(m0_is_po2(alignment));
175  mask = alignment - 1;
176  return (val + mask) & ~mask;
177 }
178 
179 static inline bool m0_is_aligned(uint64_t val, uint64_t alignment)
180 {
181  uint64_t mask;
182 
183  M0_PRE(m0_is_po2(alignment));
184  mask = alignment - 1;
185  return (val & mask) == 0;
186 }
187 
190 #define M0_IS_8ALIGNED(val) ((((uint64_t)(val)) & 07) == 0)
191 
199 #define M0_3WAY(v0, v1) \
200 ({ \
201  typeof(v0) __a0 = (v0); \
202  typeof(v1) __a1 = (v1); \
203  \
204  (__a0 < __a1) ? -1 : __a0 != __a1; \
205 })
206 
207 #define M0_SWAP(v0, v1) \
208 ({ \
209  typeof(v0) *__a0 = &(v0); \
210  typeof(v1) *__a1 = &(v1); \
211  typeof(v0) __tmp = *__a0; \
212  (void)(__a0 == __a1); \
213  \
214  *__a0 = *__a1; \
215  *__a1 = __tmp; \
216 })
217 
219 #define M0_CNT_DEC(cnt) \
220 ({ \
221  M0_ASSERT((cnt) != 0); \
222  --cnt; \
223 })
224 
226 #define M0_CNT_INC(cnt) \
227 ({ \
228  ++cnt; \
229  M0_ASSERT((cnt) != 0); \
230 })
231 
235 static inline bool m0_addu64_will_overflow(uint64_t a, uint64_t b)
236 {
237  return a + b < a;
238 }
239 
245 static inline uint64_t m0_enc(uint64_t width, uint64_t row, uint64_t column)
246 {
247  M0_PRE(column < width);
248  return row * width + column;
249 }
250 
256 static inline void m0_dec(uint64_t width, uint64_t pos, uint64_t *row,
257  uint64_t *column)
258 {
259  *row = pos / width;
260  *column = pos % width;
261 }
262 
264 #endif /* __MOTR_LIB_ARITH_H__ */
265 
266 /*
267  * Local variables:
268  * c-indentation-style: "K&R"
269  * c-basic-offset: 8
270  * tab-width: 8
271  * fill-column: 80
272  * scroll-step: 1
273  * End:
274  */
static struct m0_addb2_philter p
Definition: consumer.c:40
#define M0_PRE(cond)
static struct m0_semaphore q
Definition: rwlock.c:55
Definition: idx_mock.c:52
static bool x
Definition: sm.c:168
static bool m0_is_po2(uint64_t val)
Definition: arith.h:153
static bool m0_addu64_will_overflow(uint64_t a, uint64_t b)
Definition: arith.h:235
M0_INTERNAL bool m0_mod_ge(uint64_t x0, uint64_t x1)
Definition: misc.c:176
static int32_t max32(int32_t a, int32_t b)
Definition: arith.h:41
static uint64_t m0_clip64u(uint64_t lo, uint64_t hi, uint64_t x)
Definition: arith.h:131
static int64_t max64(int64_t a, int64_t b)
Definition: arith.h:51
M0_INTERNAL uint64_t m0_rnd(uint64_t max, uint64_t *seed)
Definition: misc.c:115
static bool m0_is_aligned(uint64_t val, uint64_t alignment)
Definition: arith.h:179
static long long max(long long a, long long b)
Definition: crate.c:196
static uint64_t min64u(uint64_t a, uint64_t b)
Definition: arith.h:66
M0_INTERNAL uint64_t m0_gcd64(uint64_t p, uint64_t q)
Definition: misc.c:128
M0_INTERNAL bool m0_mod_gt(uint64_t x0, uint64_t x1)
Definition: misc.c:171
static uint64_t m0_enc(uint64_t width, uint64_t row, uint64_t column)
Definition: arith.h:245
static uint32_t min32u(uint32_t a, uint32_t b)
Definition: arith.h:56
M0_INTERNAL uint64_t m0_rnd64(uint64_t *seed)
Definition: misc.c:100
static void m0_dec(uint64_t width, uint64_t pos, uint64_t *row, uint64_t *column)
Definition: arith.h:256
static int64_t min64(int64_t a, int64_t b)
Definition: arith.h:46
static uint32_t max32u(uint32_t a, uint32_t b)
Definition: arith.h:61
static unsigned m0_log2(uint64_t val)
Definition: arith.h:161
static int32_t min32(int32_t a, int32_t b)
Definition: arith.h:36
static void hi(void)
Definition: nucleus.c:93
static uint64_t m0_align(uint64_t val, uint64_t alignment)
Definition: arith.h:170
static uint64_t max64u(uint64_t a, uint64_t b)
Definition: arith.h:71