1 // SPDX-License-Identifier: MIT
2 //
3 // Copyright 2024 Advanced Micro Devices, Inc.
4 
5 #include "lib_float_math.h"
6 
7 #define ASSERT(condition)
8 
9 #define isNaN(number) ((number) != (number))
10 
11  /*
12   * NOTE:
13   *   This file is gcc-parseable HW gospel, coming straight from HW engineers.
14   *
15   * It doesn't adhere to Linux kernel style and sometimes will do things in odd
16   * ways. Unless there is something clearly wrong with it the code should
17   * remain as-is as it provides us with a guarantee from HW that it is correct.
18   */
19 
math_mod(const double arg1,const double arg2)20 double math_mod(const double arg1, const double arg2)
21 {
22 	if (isNaN(arg1))
23 		return arg2;
24 	if (isNaN(arg2))
25 		return arg1;
26 	return arg1 - arg1 * ((int)(arg1 / arg2));
27 }
28 
math_min2(const double arg1,const double arg2)29 double math_min2(const double arg1, const double arg2)
30 {
31 	if (isNaN(arg1))
32 		return arg2;
33 	if (isNaN(arg2))
34 		return arg1;
35 	return arg1 < arg2 ? arg1 : arg2;
36 }
37 
math_max2(const double arg1,const double arg2)38 double math_max2(const double arg1, const double arg2)
39 {
40 	if (isNaN(arg1))
41 		return arg2;
42 	if (isNaN(arg2))
43 		return arg1;
44 	return arg1 > arg2 ? arg1 : arg2;
45 }
46 
math_floor2(const double arg,const double significance)47 double math_floor2(const double arg, const double significance)
48 {
49 	ASSERT(significance != 0);
50 
51 	return ((int)(arg / significance)) * significance;
52 }
53 
math_floor(const double arg)54 double math_floor(const double arg)
55 {
56 	return ((int)(arg));
57 }
58 
math_ceil(const double arg)59 double math_ceil(const double arg)
60 {
61 	return (int)(arg + 0.99999);
62 }
63 
math_ceil2(const double arg,const double significance)64 double math_ceil2(const double arg, const double significance)
65 {
66 	return ((int)(arg / significance + 0.99999)) * significance;
67 }
68 
math_max3(double v1,double v2,double v3)69 double math_max3(double v1, double v2, double v3)
70 {
71 	return v3 > math_max2(v1, v2) ? v3 : math_max2(v1, v2);
72 }
73 
math_max4(double v1,double v2,double v3,double v4)74 double math_max4(double v1, double v2, double v3, double v4)
75 {
76 	return v4 > math_max3(v1, v2, v3) ? v4 : math_max3(v1, v2, v3);
77 }
78 
math_max5(double v1,double v2,double v3,double v4,double v5)79 double math_max5(double v1, double v2, double v3, double v4, double v5)
80 {
81 	return math_max3(v1, v2, v3) > math_max2(v4, v5) ? math_max3(v1, v2, v3) : math_max2(v4, v5);
82 }
83 
math_pow(float a,float exp)84 float math_pow(float a, float exp)
85 {
86 	double temp;
87 	if ((int)exp == 0)
88 		return 1;
89 	temp = math_pow(a, (float)((int)(exp / 2)));
90 	if (((int)exp % 2) == 0) {
91 		return (float)(temp * temp);
92 	} else {
93 		if ((int)exp > 0)
94 			return (float)(a * temp * temp);
95 		else
96 			return (float)((temp * temp) / a);
97 	}
98 }
99 
math_fabs(double a)100 double math_fabs(double a)
101 {
102 	if (a > 0)
103 		return (a);
104 	else
105 		return (-a);
106 }
107 
math_log(float a,float b)108 float math_log(float a, float b)
109 {
110 	int *const exp_ptr = (int *)(&a);
111 	int x = *exp_ptr;
112 	const int log_2 = ((x >> 23) & 255) - 128;
113 	x &= ~(255 << 23);
114 	x += 127 << 23;
115 	*exp_ptr = x;
116 
117 	a = ((-1.0f / 3) * a + 2) * a - 2.0f / 3;
118 
119 	if (b > 2.00001 || b < 1.99999)
120 		return (a + log_2) / math_log(b, 2);
121 	else
122 		return (a + log_2);
123 }
124 
math_log2(float a)125 float math_log2(float a)
126 {
127 	return math_log(a, 2.0);
128 }
129 
130 // approximate log2 value of a input
131 //  - precise if the input pwr of 2, else the approximation will be an integer = floor(actual_log2)
math_log2_approx(unsigned int a)132 unsigned int math_log2_approx(unsigned int a)
133 {
134 	unsigned int log2_val = 0;
135 	while (a > 1) {
136 		a = a >> 1;
137 		log2_val++;
138 	}
139 	return log2_val;
140 }
141 
math_round(double a)142 double math_round(double a)
143 {
144 	const double round_pt = 0.5;
145 
146 	return math_floor(a + round_pt);
147 }
148