xref: /openbsd/sys/dev/pci/drm/amd/display/dc/inc/bw_fixed.h (revision c349dbc7)
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)
32 struct 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 
bw_min2(const struct bw_fixed arg1,const struct bw_fixed arg2)42 static 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 
bw_max2(const struct bw_fixed arg1,const struct bw_fixed arg2)48 static 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 
bw_min3(struct bw_fixed v1,struct bw_fixed v2,struct bw_fixed v3)54 static 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 
bw_max3(struct bw_fixed v1,struct bw_fixed v2,struct bw_fixed v3)61 static 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 
68 struct bw_fixed bw_int_to_fixed_nonconst(int64_t value);
bw_int_to_fixed(int64_t value)69 static 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 
bw_fixed_to_int(struct bw_fixed value)82 static inline int32_t bw_fixed_to_int(struct bw_fixed value)
83 {
84 	return BW_FIXED_GET_INTEGER_PART(value.value);
85 }
86 
87 struct bw_fixed bw_frc_to_fixed(int64_t num, int64_t denum);
88 
fixed31_32_to_bw_fixed(int64_t raw)89 static 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 
bw_add(const struct bw_fixed arg1,const struct bw_fixed arg2)103 static 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 
bw_sub(const struct bw_fixed arg1,const struct bw_fixed arg2)113 static 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 
122 struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2);
bw_div(const struct bw_fixed arg1,const struct bw_fixed arg2)123 static 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 
bw_mod(const struct bw_fixed arg1,const struct bw_fixed arg2)128 static 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 
135 struct bw_fixed bw_floor2(const struct bw_fixed arg, const struct bw_fixed significance);
136 struct bw_fixed bw_ceil2(const struct bw_fixed arg, const struct bw_fixed significance);
137 
bw_equ(const struct bw_fixed arg1,const struct bw_fixed arg2)138 static inline bool bw_equ(const struct bw_fixed arg1, const struct bw_fixed arg2)
139 {
140 	return arg1.value == arg2.value;
141 }
142 
bw_neq(const struct bw_fixed arg1,const struct bw_fixed arg2)143 static inline bool bw_neq(const struct bw_fixed arg1, const struct bw_fixed arg2)
144 {
145 	return arg1.value != arg2.value;
146 }
147 
bw_leq(const struct bw_fixed arg1,const struct bw_fixed arg2)148 static inline bool bw_leq(const struct bw_fixed arg1, const struct bw_fixed arg2)
149 {
150 	return arg1.value <= arg2.value;
151 }
152 
bw_meq(const struct bw_fixed arg1,const struct bw_fixed arg2)153 static inline bool bw_meq(const struct bw_fixed arg1, const struct bw_fixed arg2)
154 {
155 	return arg1.value >= arg2.value;
156 }
157 
bw_ltn(const struct bw_fixed arg1,const struct bw_fixed arg2)158 static inline bool bw_ltn(const struct bw_fixed arg1, const struct bw_fixed arg2)
159 {
160 	return arg1.value < arg2.value;
161 }
162 
bw_mtn(const struct bw_fixed arg1,const struct bw_fixed arg2)163 static 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