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