1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2017 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  * Authors: AMD
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev 
26*b843c749SSergey Zigachev #include "dcn_calc_math.h"
27*b843c749SSergey Zigachev 
28*b843c749SSergey Zigachev #define isNaN(number) ((number) != (number))
29*b843c749SSergey Zigachev 
30*b843c749SSergey Zigachev /*
31*b843c749SSergey Zigachev  * NOTE:
32*b843c749SSergey Zigachev  *   This file is gcc-parseable HW gospel, coming straight from HW engineers.
33*b843c749SSergey Zigachev  *
34*b843c749SSergey Zigachev  * It doesn't adhere to Linux kernel style and sometimes will do things in odd
35*b843c749SSergey Zigachev  * ways. Unless there is something clearly wrong with it the code should
36*b843c749SSergey Zigachev  * remain as-is as it provides us with a guarantee from HW that it is correct.
37*b843c749SSergey Zigachev  */
38*b843c749SSergey Zigachev 
dcn_bw_mod(const float arg1,const float arg2)39*b843c749SSergey Zigachev float dcn_bw_mod(const float arg1, const float arg2)
40*b843c749SSergey Zigachev {
41*b843c749SSergey Zigachev 	if (isNaN(arg1))
42*b843c749SSergey Zigachev 		return arg2;
43*b843c749SSergey Zigachev 	if (isNaN(arg2))
44*b843c749SSergey Zigachev 		return arg1;
45*b843c749SSergey Zigachev 	return arg1 - arg1 * ((int) (arg1 / arg2));
46*b843c749SSergey Zigachev }
47*b843c749SSergey Zigachev 
dcn_bw_min2(const float arg1,const float arg2)48*b843c749SSergey Zigachev float dcn_bw_min2(const float arg1, const float arg2)
49*b843c749SSergey Zigachev {
50*b843c749SSergey Zigachev 	if (isNaN(arg1))
51*b843c749SSergey Zigachev 		return arg2;
52*b843c749SSergey Zigachev 	if (isNaN(arg2))
53*b843c749SSergey Zigachev 		return arg1;
54*b843c749SSergey Zigachev 	return arg1 < arg2 ? arg1 : arg2;
55*b843c749SSergey Zigachev }
56*b843c749SSergey Zigachev 
dcn_bw_max(const unsigned int arg1,const unsigned int arg2)57*b843c749SSergey Zigachev unsigned int dcn_bw_max(const unsigned int arg1, const unsigned int arg2)
58*b843c749SSergey Zigachev {
59*b843c749SSergey Zigachev 	return arg1 > arg2 ? arg1 : arg2;
60*b843c749SSergey Zigachev }
dcn_bw_max2(const float arg1,const float arg2)61*b843c749SSergey Zigachev float dcn_bw_max2(const float arg1, const float arg2)
62*b843c749SSergey Zigachev {
63*b843c749SSergey Zigachev 	if (isNaN(arg1))
64*b843c749SSergey Zigachev 		return arg2;
65*b843c749SSergey Zigachev 	if (isNaN(arg2))
66*b843c749SSergey Zigachev 		return arg1;
67*b843c749SSergey Zigachev 	return arg1 > arg2 ? arg1 : arg2;
68*b843c749SSergey Zigachev }
69*b843c749SSergey Zigachev 
dcn_bw_floor2(const float arg,const float significance)70*b843c749SSergey Zigachev float dcn_bw_floor2(const float arg, const float significance)
71*b843c749SSergey Zigachev {
72*b843c749SSergey Zigachev 	if (significance == 0)
73*b843c749SSergey Zigachev 		return 0;
74*b843c749SSergey Zigachev 	return ((int) (arg / significance)) * significance;
75*b843c749SSergey Zigachev }
76*b843c749SSergey Zigachev 
dcn_bw_ceil2(const float arg,const float significance)77*b843c749SSergey Zigachev float dcn_bw_ceil2(const float arg, const float significance)
78*b843c749SSergey Zigachev {
79*b843c749SSergey Zigachev 	float flr = dcn_bw_floor2(arg, significance);
80*b843c749SSergey Zigachev 	if (significance == 0)
81*b843c749SSergey Zigachev 		return 0;
82*b843c749SSergey Zigachev 	return flr + 0.00001 >= arg ? arg : flr + significance;
83*b843c749SSergey Zigachev }
84*b843c749SSergey Zigachev 
dcn_bw_max3(float v1,float v2,float v3)85*b843c749SSergey Zigachev float dcn_bw_max3(float v1, float v2, float v3)
86*b843c749SSergey Zigachev {
87*b843c749SSergey Zigachev 	return v3 > dcn_bw_max2(v1, v2) ? v3 : dcn_bw_max2(v1, v2);
88*b843c749SSergey Zigachev }
89*b843c749SSergey Zigachev 
dcn_bw_max5(float v1,float v2,float v3,float v4,float v5)90*b843c749SSergey Zigachev float dcn_bw_max5(float v1, float v2, float v3, float v4, float v5)
91*b843c749SSergey Zigachev {
92*b843c749SSergey Zigachev 	return dcn_bw_max3(v1, v2, v3) > dcn_bw_max2(v4, v5) ? dcn_bw_max3(v1, v2, v3) : dcn_bw_max2(v4, v5);
93*b843c749SSergey Zigachev }
94*b843c749SSergey Zigachev 
dcn_bw_pow(float a,float exp)95*b843c749SSergey Zigachev float dcn_bw_pow(float a, float exp)
96*b843c749SSergey Zigachev {
97*b843c749SSergey Zigachev 	float temp;
98*b843c749SSergey Zigachev 	/*ASSERT(exp == (int)exp);*/
99*b843c749SSergey Zigachev 	if ((int)exp == 0)
100*b843c749SSergey Zigachev 		return 1;
101*b843c749SSergey Zigachev 	temp = dcn_bw_pow(a, (int)(exp / 2));
102*b843c749SSergey Zigachev 	if (((int)exp % 2) == 0) {
103*b843c749SSergey Zigachev 		return temp * temp;
104*b843c749SSergey Zigachev 	} else {
105*b843c749SSergey Zigachev 		if ((int)exp > 0)
106*b843c749SSergey Zigachev 			return a * temp * temp;
107*b843c749SSergey Zigachev 		else
108*b843c749SSergey Zigachev 			return (temp * temp) / a;
109*b843c749SSergey Zigachev 	}
110*b843c749SSergey Zigachev }
111*b843c749SSergey Zigachev 
dcn_bw_log(float a,float b)112*b843c749SSergey Zigachev float dcn_bw_log(float a, float b)
113*b843c749SSergey Zigachev {
114*b843c749SSergey Zigachev 	int * const exp_ptr = (int *)(&a);
115*b843c749SSergey Zigachev 	int x = *exp_ptr;
116*b843c749SSergey Zigachev 	const int log_2 = ((x >> 23) & 255) - 128;
117*b843c749SSergey Zigachev 	x &= ~(255 << 23);
118*b843c749SSergey Zigachev 	x += 127 << 23;
119*b843c749SSergey Zigachev 	*exp_ptr = x;
120*b843c749SSergey Zigachev 
121*b843c749SSergey Zigachev 	a = ((-1.0f / 3) * a + 2) * a - 2.0f / 3;
122*b843c749SSergey Zigachev 
123*b843c749SSergey Zigachev 	if (b > 2.00001 || b < 1.99999)
124*b843c749SSergey Zigachev 		return (a + log_2) / dcn_bw_log(b, 2);
125*b843c749SSergey Zigachev 	else
126*b843c749SSergey Zigachev 		return (a + log_2);
127*b843c749SSergey Zigachev }
128