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