diff options
author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2015-08-09 07:18:48 -0400 |
---|---|---|
committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2015-08-09 06:21:18 -0400 |
commit | 8e048c798adaabef530a1526f7ce8c6c3cd3475e (patch) | |
tree | 5a96b3eaeaafecec1bf08ba71a9d0084d39d46eb /include/litmus/fpmath.h | |
parent | bd175e94795774908317a861a883761b75750e35 (diff) |
Add LITMUS^RT core implementation
This patch adds the core of LITMUS^RT:
- library functionality (heaps, rt_domain, prioritization, etc.)
- budget enforcement logic
- job management
- system call backends
- virtual devices (control page, etc.)
- scheduler plugin API (and dummy plugin)
This code compiles, but is not yet integrated with the rest of Linux.
Diffstat (limited to 'include/litmus/fpmath.h')
-rw-r--r-- | include/litmus/fpmath.h | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/include/litmus/fpmath.h b/include/litmus/fpmath.h new file mode 100644 index 000000000000..642de98542c8 --- /dev/null +++ b/include/litmus/fpmath.h | |||
@@ -0,0 +1,147 @@ | |||
1 | #ifndef __FP_MATH_H__ | ||
2 | #define __FP_MATH_H__ | ||
3 | |||
4 | #include <linux/math64.h> | ||
5 | |||
6 | #ifndef __KERNEL__ | ||
7 | #include <stdint.h> | ||
8 | #define abs(x) (((x) < 0) ? -(x) : x) | ||
9 | #endif | ||
10 | |||
11 | // Use 64-bit because we want to track things at the nanosecond scale. | ||
12 | // This can lead to very large numbers. | ||
13 | typedef int64_t fpbuf_t; | ||
14 | typedef struct | ||
15 | { | ||
16 | fpbuf_t val; | ||
17 | } fp_t; | ||
18 | |||
19 | #define FP_SHIFT 10 | ||
20 | #define ROUND_BIT (FP_SHIFT - 1) | ||
21 | |||
22 | #define _fp(x) ((fp_t) {x}) | ||
23 | |||
24 | #ifdef __KERNEL__ | ||
25 | static const fp_t LITMUS_FP_ZERO = {.val = 0}; | ||
26 | static const fp_t LITMUS_FP_ONE = {.val = (1 << FP_SHIFT)}; | ||
27 | #endif | ||
28 | |||
29 | static inline fp_t FP(fpbuf_t x) | ||
30 | { | ||
31 | return _fp(((fpbuf_t) x) << FP_SHIFT); | ||
32 | } | ||
33 | |||
34 | /* divide two integers to obtain a fixed point value */ | ||
35 | static inline fp_t _frac(fpbuf_t a, fpbuf_t b) | ||
36 | { | ||
37 | return _fp(div64_s64(FP(a).val, (b))); | ||
38 | } | ||
39 | |||
40 | static inline fpbuf_t _point(fp_t x) | ||
41 | { | ||
42 | return (x.val % (1 << FP_SHIFT)); | ||
43 | |||
44 | } | ||
45 | |||
46 | #define fp2str(x) x.val | ||
47 | /*(x.val >> FP_SHIFT), (x.val % (1 << FP_SHIFT)) */ | ||
48 | #define _FP_ "%ld/1024" | ||
49 | |||
50 | static inline fpbuf_t _floor(fp_t x) | ||
51 | { | ||
52 | return x.val >> FP_SHIFT; | ||
53 | } | ||
54 | |||
55 | /* FIXME: negative rounding */ | ||
56 | static inline fpbuf_t _round(fp_t x) | ||
57 | { | ||
58 | return _floor(x) + ((x.val >> ROUND_BIT) & 1); | ||
59 | } | ||
60 | |||
61 | /* multiply two fixed point values */ | ||
62 | static inline fp_t _mul(fp_t a, fp_t b) | ||
63 | { | ||
64 | return _fp((a.val * b.val) >> FP_SHIFT); | ||
65 | } | ||
66 | |||
67 | static inline fp_t _div(fp_t a, fp_t b) | ||
68 | { | ||
69 | #if !defined(__KERNEL__) && !defined(unlikely) | ||
70 | #define unlikely(x) (x) | ||
71 | #define DO_UNDEF_UNLIKELY | ||
72 | #endif | ||
73 | /* try not to overflow */ | ||
74 | if (unlikely( a.val > (2l << ((sizeof(fpbuf_t)*8) - FP_SHIFT)) )) | ||
75 | return _fp((a.val / b.val) << FP_SHIFT); | ||
76 | else | ||
77 | return _fp((a.val << FP_SHIFT) / b.val); | ||
78 | #ifdef DO_UNDEF_UNLIKELY | ||
79 | #undef unlikely | ||
80 | #undef DO_UNDEF_UNLIKELY | ||
81 | #endif | ||
82 | } | ||
83 | |||
84 | static inline fp_t _add(fp_t a, fp_t b) | ||
85 | { | ||
86 | return _fp(a.val + b.val); | ||
87 | } | ||
88 | |||
89 | static inline fp_t _sub(fp_t a, fp_t b) | ||
90 | { | ||
91 | return _fp(a.val - b.val); | ||
92 | } | ||
93 | |||
94 | static inline fp_t _neg(fp_t x) | ||
95 | { | ||
96 | return _fp(-x.val); | ||
97 | } | ||
98 | |||
99 | static inline fp_t _abs(fp_t x) | ||
100 | { | ||
101 | return _fp(abs(x.val)); | ||
102 | } | ||
103 | |||
104 | /* works the same as casting float/double to integer */ | ||
105 | static inline fpbuf_t _fp_to_integer(fp_t x) | ||
106 | { | ||
107 | return _floor(_abs(x)) * ((x.val > 0) ? 1 : -1); | ||
108 | } | ||
109 | |||
110 | static inline fp_t _integer_to_fp(fpbuf_t x) | ||
111 | { | ||
112 | return _frac(x,1); | ||
113 | } | ||
114 | |||
115 | static inline int _leq(fp_t a, fp_t b) | ||
116 | { | ||
117 | return a.val <= b.val; | ||
118 | } | ||
119 | |||
120 | static inline int _geq(fp_t a, fp_t b) | ||
121 | { | ||
122 | return a.val >= b.val; | ||
123 | } | ||
124 | |||
125 | static inline int _lt(fp_t a, fp_t b) | ||
126 | { | ||
127 | return a.val < b.val; | ||
128 | } | ||
129 | |||
130 | static inline int _gt(fp_t a, fp_t b) | ||
131 | { | ||
132 | return a.val > b.val; | ||
133 | } | ||
134 | |||
135 | static inline int _eq(fp_t a, fp_t b) | ||
136 | { | ||
137 | return a.val == b.val; | ||
138 | } | ||
139 | |||
140 | static inline fp_t _max(fp_t a, fp_t b) | ||
141 | { | ||
142 | if (a.val < b.val) | ||
143 | return b; | ||
144 | else | ||
145 | return a; | ||
146 | } | ||
147 | #endif | ||