My Kernel v0.1.0
math.h
Go to the documentation of this file.
1
13#ifndef UTILS_MATH_H
14#define UTILS_MATH_H
15
16#include <kernel/types.h>
17
19#define MAX(_x, _y) \
20 ({ \
21 __typeof__(_x) _max1 = (_x); \
22 __typeof__(_y) _max2 = (_y); \
23 _max1 > _max2 ? _max1 : _max2; \
24 })
25
27#define MIN(_x, _y) \
28 ({ \
29 __typeof__(_x) _max1 = (_x); \
30 __typeof__(_y) _max2 = (_y); \
31 _max1 < _max2 ? _max1 : _max2; \
32 })
33
35#define ABS(_x) \
36 ({ \
37 __typeof__(_x) _tmp = (_x); \
38 _tmp < 0 ? -_tmp : _tmp; \
39 })
40
41#define __align_mask(_value, _power) ((__typeof__(_value))((_power)-1))
42
47#define align_up(_value, _power) \
48 ((((_value)-1) | __align_mask(_value, _power)) + 1)
49
54#define align_down(_value, _power) ((_value) & ~__align_mask(_value, _power))
55
57#define is_aligned(_value, _alignment) (!((_value) % (_alignment)))
58
59#define align_down_ptr(_ptr, _power) ((void *)align_down((vaddr_t)_ptr, _power))
60#define align_up_ptr(_ptr, _power) ((void *)align_up((vaddr_t)_ptr, _power))
61#define is_aligned_ptr(_ptr, _alignment) is_aligned((vaddr_t)_ptr, _alignment)
62
69static inline u32 round_up(u32 value, u32 alignment)
70{
71 if (alignment == 0)
72 return value;
73
74 u32 offset = value % alignment;
75 if (offset)
76 value += alignment - offset; // WARNING: Offset can occur here!
77
78 return value;
79}
80
85static inline u32 round_down(u32 value, u32 alignment)
86{
87 if (alignment == 0)
88 return value;
89
90 return value - (value % alignment);
91}
92
93#endif /* UTILS_MATH_H */
static u32 round_down(u32 value, u32 alignment)
Round value to the previous multiple of alignment.
Definition: math.h:85
static u32 round_up(u32 value, u32 alignment)
Round value to the next multiple of alignment.
Definition: math.h:69