-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic.h
67 lines (58 loc) · 1.34 KB
/
arithmetic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Some arithmetic operations.
*
* Copyright (c) 2020-2022 Jiansheng Qiu <[email protected]>
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#ifndef __ARITHMETIC_H
#define __ARITHMETIC_H
#include <linux/types.h>
static inline uint32_t lowbit_u32(uint32_t x) {
return x & -x;
}
static inline bool is_pow_of_2_u32(uint32_t x) {
return lowbit_u32(x) == x;
}
static inline unsigned long ceil_log_2(unsigned long x)
{
unsigned long i = 0;
while ((1UL << i) < x)
++i;
return i;
}
static inline uint32_t ceil_div_u32(uint32_t a, uint32_t b)
{
return (a + b - 1) / b;
}
static inline unsigned long ceil_div_ul(unsigned long a, unsigned long b)
{
return (a + b - 1) / b;
}
static inline unsigned long min_ul(unsigned long a, unsigned long b)
{
return a < b ? a : b;
}
static inline size_t min_usize(size_t a, size_t b)
{
return a < b ? a : b;
}
static inline uint16_t min_u16(uint16_t a, uint16_t b)
{
return a < b ? a : b;
}
static inline uint32_t min_u32(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
static inline unsigned long max_ul(unsigned long a, unsigned long b)
{
return a < b ? b : a;
}
static inline size_t max_usize(size_t a, size_t b)
{
return a < b ? b : a;
}
#endif // __ARITHMETIC_H