1/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: AMD
23 *
24 */
25
26#ifndef BW_FIXED_H_
27#define BW_FIXED_H_
28
29#define BW_FIXED_BITS_PER_FRACTIONAL_PART 24
30
31#define BW_FIXED_GET_INTEGER_PART(x) ((x) >> BW_FIXED_BITS_PER_FRACTIONAL_PART)
32struct bw_fixed {
33	int64_t value;
34};
35
36#define BW_FIXED_MIN_I32 \
37	(int64_t)(-(1LL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)))
38
39#define BW_FIXED_MAX_I32 \
40	(int64_t)((1ULL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)) - 1)
41
42static inline struct bw_fixed bw_min2(const struct bw_fixed arg1,
43				      const struct bw_fixed arg2)
44{
45	return (arg1.value <= arg2.value) ? arg1 : arg2;
46}
47
48static inline struct bw_fixed bw_max2(const struct bw_fixed arg1,
49				      const struct bw_fixed arg2)
50{
51	return (arg2.value <= arg1.value) ? arg1 : arg2;
52}
53
54static inline struct bw_fixed bw_min3(struct bw_fixed v1,
55				      struct bw_fixed v2,
56				      struct bw_fixed v3)
57{
58	return bw_min2(bw_min2(v1, v2), v3);
59}
60
61static inline struct bw_fixed bw_max3(struct bw_fixed v1,
62				      struct bw_fixed v2,
63				      struct bw_fixed v3)
64{
65	return bw_max2(bw_max2(v1, v2), v3);
66}
67
68struct bw_fixed bw_int_to_fixed_nonconst(int64_t value);
69static inline struct bw_fixed bw_int_to_fixed(int64_t value)
70{
71	if (__builtin_constant_p(value)) {
72		struct bw_fixed res;
73#ifdef notyet
74		BUILD_BUG_ON(value > BW_FIXED_MAX_I32 || value < BW_FIXED_MIN_I32);
75#endif
76		res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
77		return res;
78	} else
79		return bw_int_to_fixed_nonconst(value);
80}
81
82static inline int32_t bw_fixed_to_int(struct bw_fixed value)
83{
84	return BW_FIXED_GET_INTEGER_PART(value.value);
85}
86
87struct bw_fixed bw_frc_to_fixed(int64_t num, int64_t denum);
88
89static inline struct bw_fixed fixed31_32_to_bw_fixed(int64_t raw)
90{
91	struct bw_fixed result = { 0 };
92
93	if (raw < 0) {
94		raw = -raw;
95		result.value = -(raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART));
96	} else {
97		result.value = raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART);
98	}
99
100	return result;
101}
102
103static inline struct bw_fixed bw_add(const struct bw_fixed arg1,
104				     const struct bw_fixed arg2)
105{
106	struct bw_fixed res;
107
108	res.value = arg1.value + arg2.value;
109
110	return res;
111}
112
113static inline struct bw_fixed bw_sub(const struct bw_fixed arg1, const struct bw_fixed arg2)
114{
115	struct bw_fixed res;
116
117	res.value = arg1.value - arg2.value;
118
119	return res;
120}
121
122struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2);
123static inline struct bw_fixed bw_div(const struct bw_fixed arg1, const struct bw_fixed arg2)
124{
125	return bw_frc_to_fixed(arg1.value, arg2.value);
126}
127
128static inline struct bw_fixed bw_mod(const struct bw_fixed arg1, const struct bw_fixed arg2)
129{
130	struct bw_fixed res;
131	div64_u64_rem(arg1.value, arg2.value, (uint64_t *)&res.value);
132	return res;
133}
134
135struct bw_fixed bw_floor2(const struct bw_fixed arg, const struct bw_fixed significance);
136struct bw_fixed bw_ceil2(const struct bw_fixed arg, const struct bw_fixed significance);
137
138static inline bool bw_equ(const struct bw_fixed arg1, const struct bw_fixed arg2)
139{
140	return arg1.value == arg2.value;
141}
142
143static inline bool bw_neq(const struct bw_fixed arg1, const struct bw_fixed arg2)
144{
145	return arg1.value != arg2.value;
146}
147
148static inline bool bw_leq(const struct bw_fixed arg1, const struct bw_fixed arg2)
149{
150	return arg1.value <= arg2.value;
151}
152
153static inline bool bw_meq(const struct bw_fixed arg1, const struct bw_fixed arg2)
154{
155	return arg1.value >= arg2.value;
156}
157
158static inline bool bw_ltn(const struct bw_fixed arg1, const struct bw_fixed arg2)
159{
160	return arg1.value < arg2.value;
161}
162
163static inline bool bw_mtn(const struct bw_fixed arg1, const struct bw_fixed arg2)
164{
165	return arg1.value > arg2.value;
166}
167
168#endif //BW_FIXED_H_
169