xref: /openbmc/linux/drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e098bc96SEvan Quan /*
2e098bc96SEvan Quan  * Copyright 2015 Advanced Micro Devices, Inc.
3e098bc96SEvan Quan  *
4e098bc96SEvan Quan  * Permission is hereby granted, free of charge, to any person obtaining a
5e098bc96SEvan Quan  * copy of this software and associated documentation files (the "Software"),
6e098bc96SEvan Quan  * to deal in the Software without restriction, including without limitation
7e098bc96SEvan Quan  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e098bc96SEvan Quan  * and/or sell copies of the Software, and to permit persons to whom the
9e098bc96SEvan Quan  * Software is furnished to do so, subject to the following conditions:
10e098bc96SEvan Quan  *
11e098bc96SEvan Quan  * The above copyright notice and this permission notice shall be included in
12e098bc96SEvan Quan  * all copies or substantial portions of the Software.
13e098bc96SEvan Quan  *
14e098bc96SEvan Quan  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15e098bc96SEvan Quan  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16e098bc96SEvan Quan  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17e098bc96SEvan Quan  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18e098bc96SEvan Quan  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19e098bc96SEvan Quan  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20e098bc96SEvan Quan  * OTHER DEALINGS IN THE SOFTWARE.
21e098bc96SEvan Quan  *
22e098bc96SEvan Quan  */
23e098bc96SEvan Quan #include <asm/div64.h>
24e098bc96SEvan Quan 
25e098bc96SEvan Quan #define SHIFT_AMOUNT 16 /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */
26e098bc96SEvan Quan 
27e098bc96SEvan Quan #define PRECISION 5 /* Change this value to change the number of decimal places in the final output - 5 is a good default */
28e098bc96SEvan Quan 
29e098bc96SEvan Quan #define SHIFTED_2 (2 << SHIFT_AMOUNT)
30e098bc96SEvan Quan #define MAX (1 << (SHIFT_AMOUNT - 1)) - 1 /* 32767 - Might change in the future */
31e098bc96SEvan Quan 
32e098bc96SEvan Quan /* -------------------------------------------------------------------------------
33e098bc96SEvan Quan  * NEW TYPE - fINT
34e098bc96SEvan Quan  * -------------------------------------------------------------------------------
35e098bc96SEvan Quan  * A variable of type fInt can be accessed in 3 ways using the dot (.) operator
36e098bc96SEvan Quan  * fInt A;
37e098bc96SEvan Quan  * A.full => The full number as it is. Generally not easy to read
38e098bc96SEvan Quan  * A.partial.real => Only the integer portion
39e098bc96SEvan Quan  * A.partial.decimal => Only the fractional portion
40e098bc96SEvan Quan  */
41e098bc96SEvan Quan typedef union _fInt {
42e098bc96SEvan Quan     int full;
43e098bc96SEvan Quan     struct _partial {
44e098bc96SEvan Quan         unsigned int decimal: SHIFT_AMOUNT; /*Needs to always be unsigned*/
45e098bc96SEvan Quan         int real: 32 - SHIFT_AMOUNT;
46e098bc96SEvan Quan     } partial;
47e098bc96SEvan Quan } fInt;
48e098bc96SEvan Quan 
49e098bc96SEvan Quan /* -------------------------------------------------------------------------------
50e098bc96SEvan Quan  * Function Declarations
51e098bc96SEvan Quan  *  -------------------------------------------------------------------------------
52e098bc96SEvan Quan  */
53e098bc96SEvan Quan static fInt ConvertToFraction(int);                       /* Use this to convert an INT to a FINT */
54e098bc96SEvan Quan static fInt Convert_ULONG_ToFraction(uint32_t);           /* Use this to convert an uint32_t to a FINT */
55e098bc96SEvan Quan static fInt GetScaledFraction(int, int);                  /* Use this to convert an INT to a FINT after scaling it by a factor */
56e098bc96SEvan Quan static int ConvertBackToInteger(fInt);                    /* Convert a FINT back to an INT that is scaled by 1000 (i.e. last 3 digits are the decimal digits) */
57e098bc96SEvan Quan 
58e098bc96SEvan Quan static fInt fNegate(fInt);                                /* Returns -1 * input fInt value */
59e098bc96SEvan Quan static fInt fAdd (fInt, fInt);                            /* Returns the sum of two fInt numbers */
60e098bc96SEvan Quan static fInt fSubtract (fInt A, fInt B);                   /* Returns A-B - Sometimes easier than Adding negative numbers */
61e098bc96SEvan Quan static fInt fMultiply (fInt, fInt);                       /* Returns the product of two fInt numbers */
62e098bc96SEvan Quan static fInt fDivide (fInt A, fInt B);                     /* Returns A/B */
63e098bc96SEvan Quan static fInt fGetSquare(fInt);                             /* Returns the square of a fInt number */
64e098bc96SEvan Quan static fInt fSqrt(fInt);                                  /* Returns the Square Root of a fInt number */
65e098bc96SEvan Quan 
66e098bc96SEvan Quan static int uAbs(int);                                     /* Returns the Absolute value of the Int */
67e098bc96SEvan Quan static int uPow(int base, int exponent);                  /* Returns base^exponent an INT */
68e098bc96SEvan Quan 
69e098bc96SEvan Quan static void SolveQuadracticEqn(fInt, fInt, fInt, fInt[]); /* Returns the 2 roots via the array */
70e098bc96SEvan Quan static bool Equal(fInt, fInt);                            /* Returns true if two fInts are equal to each other */
71e098bc96SEvan Quan static bool GreaterThan(fInt A, fInt B);                  /* Returns true if A > B */
72e098bc96SEvan Quan 
73e098bc96SEvan Quan static fInt fExponential(fInt exponent);                  /* Can be used to calculate e^exponent */
74e098bc96SEvan Quan static fInt fNaturalLog(fInt value);                      /* Can be used to calculate ln(value) */
75e098bc96SEvan Quan 
76e098bc96SEvan Quan /* Fuse decoding functions
77e098bc96SEvan Quan  * -------------------------------------------------------------------------------------
78e098bc96SEvan Quan  */
79e098bc96SEvan Quan static fInt fDecodeLinearFuse(uint32_t fuse_value, fInt f_min, fInt f_range, uint32_t bitlength);
80e098bc96SEvan Quan static fInt fDecodeLogisticFuse(uint32_t fuse_value, fInt f_average, fInt f_range, uint32_t bitlength);
81e098bc96SEvan Quan static fInt fDecodeLeakageID (uint32_t leakageID_fuse, fInt ln_max_div_min, fInt f_min, uint32_t bitlength);
82e098bc96SEvan Quan 
83e098bc96SEvan Quan /* Internal Support Functions - Use these ONLY for testing or adding to internal functions
84e098bc96SEvan Quan  * -------------------------------------------------------------------------------------
85e098bc96SEvan Quan  * Some of the following functions take two INTs as their input - This is unsafe for a variety of reasons.
86e098bc96SEvan Quan  */
87e098bc96SEvan Quan static fInt Divide (int, int);                            /* Divide two INTs and return result as FINT */
88e098bc96SEvan Quan static fInt fNegate(fInt);
89e098bc96SEvan Quan 
90e098bc96SEvan Quan static int uGetScaledDecimal (fInt);                      /* Internal function */
91e098bc96SEvan Quan static int GetReal (fInt A);                              /* Internal function */
92e098bc96SEvan Quan 
93e098bc96SEvan Quan /* -------------------------------------------------------------------------------------
94e098bc96SEvan Quan  * TROUBLESHOOTING INFORMATION
95e098bc96SEvan Quan  * -------------------------------------------------------------------------------------
96e098bc96SEvan Quan  * 1) ConvertToFraction - InputOutOfRangeException: Only accepts numbers smaller than MAX (default: 32767)
97e098bc96SEvan Quan  * 2) fAdd - OutputOutOfRangeException: Output bigger than MAX (default: 32767)
98e098bc96SEvan Quan  * 3) fMultiply - OutputOutOfRangeException:
99e098bc96SEvan Quan  * 4) fGetSquare - OutputOutOfRangeException:
100e098bc96SEvan Quan  * 5) fDivide - DivideByZeroException
101e098bc96SEvan Quan  * 6) fSqrt - NegativeSquareRootException: Input cannot be a negative number
102e098bc96SEvan Quan  */
103e098bc96SEvan Quan 
104e098bc96SEvan Quan /* -------------------------------------------------------------------------------------
105e098bc96SEvan Quan  * START OF CODE
106e098bc96SEvan Quan  * -------------------------------------------------------------------------------------
107e098bc96SEvan Quan  */
fExponential(fInt exponent)108e098bc96SEvan Quan static fInt fExponential(fInt exponent)        /*Can be used to calculate e^exponent*/
109e098bc96SEvan Quan {
110e098bc96SEvan Quan 	uint32_t i;
111e098bc96SEvan Quan 	bool bNegated = false;
112e098bc96SEvan Quan 
113e098bc96SEvan Quan 	fInt fPositiveOne = ConvertToFraction(1);
114e098bc96SEvan Quan 	fInt fZERO = ConvertToFraction(0);
115e098bc96SEvan Quan 
116e098bc96SEvan Quan 	fInt lower_bound = Divide(78, 10000);
117e098bc96SEvan Quan 	fInt solution = fPositiveOne; /*Starting off with baseline of 1 */
118e098bc96SEvan Quan 	fInt error_term;
119e098bc96SEvan Quan 
120e098bc96SEvan Quan 	static const uint32_t k_array[11] = {55452, 27726, 13863, 6931, 4055, 2231, 1178, 606, 308, 155, 78};
121e098bc96SEvan Quan 	static const uint32_t expk_array[11] = {2560000, 160000, 40000, 20000, 15000, 12500, 11250, 10625, 10313, 10156, 10078};
122e098bc96SEvan Quan 
123e098bc96SEvan Quan 	if (GreaterThan(fZERO, exponent)) {
124e098bc96SEvan Quan 		exponent = fNegate(exponent);
125e098bc96SEvan Quan 		bNegated = true;
126e098bc96SEvan Quan 	}
127e098bc96SEvan Quan 
128e098bc96SEvan Quan 	while (GreaterThan(exponent, lower_bound)) {
129e098bc96SEvan Quan 		for (i = 0; i < 11; i++) {
130e098bc96SEvan Quan 			if (GreaterThan(exponent, GetScaledFraction(k_array[i], 10000))) {
131e098bc96SEvan Quan 				exponent = fSubtract(exponent, GetScaledFraction(k_array[i], 10000));
132e098bc96SEvan Quan 				solution = fMultiply(solution, GetScaledFraction(expk_array[i], 10000));
133e098bc96SEvan Quan 			}
134e098bc96SEvan Quan 		}
135e098bc96SEvan Quan 	}
136e098bc96SEvan Quan 
137e098bc96SEvan Quan 	error_term = fAdd(fPositiveOne, exponent);
138e098bc96SEvan Quan 
139e098bc96SEvan Quan 	solution = fMultiply(solution, error_term);
140e098bc96SEvan Quan 
141e098bc96SEvan Quan 	if (bNegated)
142e098bc96SEvan Quan 		solution = fDivide(fPositiveOne, solution);
143e098bc96SEvan Quan 
144e098bc96SEvan Quan 	return solution;
145e098bc96SEvan Quan }
146e098bc96SEvan Quan 
fNaturalLog(fInt value)147e098bc96SEvan Quan static fInt fNaturalLog(fInt value)
148e098bc96SEvan Quan {
149e098bc96SEvan Quan 	uint32_t i;
150e098bc96SEvan Quan 	fInt upper_bound = Divide(8, 1000);
151e098bc96SEvan Quan 	fInt fNegativeOne = ConvertToFraction(-1);
152e098bc96SEvan Quan 	fInt solution = ConvertToFraction(0); /*Starting off with baseline of 0 */
153e098bc96SEvan Quan 	fInt error_term;
154e098bc96SEvan Quan 
155e098bc96SEvan Quan 	static const uint32_t k_array[10] = {160000, 40000, 20000, 15000, 12500, 11250, 10625, 10313, 10156, 10078};
156e098bc96SEvan Quan 	static const uint32_t logk_array[10] = {27726, 13863, 6931, 4055, 2231, 1178, 606, 308, 155, 78};
157e098bc96SEvan Quan 
158e098bc96SEvan Quan 	while (GreaterThan(fAdd(value, fNegativeOne), upper_bound)) {
159e098bc96SEvan Quan 		for (i = 0; i < 10; i++) {
160e098bc96SEvan Quan 			if (GreaterThan(value, GetScaledFraction(k_array[i], 10000))) {
161e098bc96SEvan Quan 				value = fDivide(value, GetScaledFraction(k_array[i], 10000));
162e098bc96SEvan Quan 				solution = fAdd(solution, GetScaledFraction(logk_array[i], 10000));
163e098bc96SEvan Quan 			}
164e098bc96SEvan Quan 		}
165e098bc96SEvan Quan 	}
166e098bc96SEvan Quan 
167e098bc96SEvan Quan 	error_term = fAdd(fNegativeOne, value);
168e098bc96SEvan Quan 
169*16d12233SRan Sun 	return fAdd(solution, error_term);
170e098bc96SEvan Quan }
171e098bc96SEvan Quan 
fDecodeLinearFuse(uint32_t fuse_value,fInt f_min,fInt f_range,uint32_t bitlength)172e098bc96SEvan Quan static fInt fDecodeLinearFuse(uint32_t fuse_value, fInt f_min, fInt f_range, uint32_t bitlength)
173e098bc96SEvan Quan {
174e098bc96SEvan Quan 	fInt f_fuse_value = Convert_ULONG_ToFraction(fuse_value);
175e098bc96SEvan Quan 	fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);
176e098bc96SEvan Quan 
177e098bc96SEvan Quan 	fInt f_decoded_value;
178e098bc96SEvan Quan 
179e098bc96SEvan Quan 	f_decoded_value = fDivide(f_fuse_value, f_bit_max_value);
180e098bc96SEvan Quan 	f_decoded_value = fMultiply(f_decoded_value, f_range);
181e098bc96SEvan Quan 	f_decoded_value = fAdd(f_decoded_value, f_min);
182e098bc96SEvan Quan 
183e098bc96SEvan Quan 	return f_decoded_value;
184e098bc96SEvan Quan }
185e098bc96SEvan Quan 
186e098bc96SEvan Quan 
fDecodeLogisticFuse(uint32_t fuse_value,fInt f_average,fInt f_range,uint32_t bitlength)187e098bc96SEvan Quan static fInt fDecodeLogisticFuse(uint32_t fuse_value, fInt f_average, fInt f_range, uint32_t bitlength)
188e098bc96SEvan Quan {
189e098bc96SEvan Quan 	fInt f_fuse_value = Convert_ULONG_ToFraction(fuse_value);
190e098bc96SEvan Quan 	fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);
191e098bc96SEvan Quan 
192e098bc96SEvan Quan 	fInt f_CONSTANT_NEG13 = ConvertToFraction(-13);
193e098bc96SEvan Quan 	fInt f_CONSTANT1 = ConvertToFraction(1);
194e098bc96SEvan Quan 
195e098bc96SEvan Quan 	fInt f_decoded_value;
196e098bc96SEvan Quan 
197e098bc96SEvan Quan 	f_decoded_value = fSubtract(fDivide(f_bit_max_value, f_fuse_value), f_CONSTANT1);
198e098bc96SEvan Quan 	f_decoded_value = fNaturalLog(f_decoded_value);
199e098bc96SEvan Quan 	f_decoded_value = fMultiply(f_decoded_value, fDivide(f_range, f_CONSTANT_NEG13));
200e098bc96SEvan Quan 	f_decoded_value = fAdd(f_decoded_value, f_average);
201e098bc96SEvan Quan 
202e098bc96SEvan Quan 	return f_decoded_value;
203e098bc96SEvan Quan }
204e098bc96SEvan Quan 
fDecodeLeakageID(uint32_t leakageID_fuse,fInt ln_max_div_min,fInt f_min,uint32_t bitlength)205e098bc96SEvan Quan static fInt fDecodeLeakageID (uint32_t leakageID_fuse, fInt ln_max_div_min, fInt f_min, uint32_t bitlength)
206e098bc96SEvan Quan {
207e098bc96SEvan Quan 	fInt fLeakage;
208e098bc96SEvan Quan 	fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);
209e098bc96SEvan Quan 
210e098bc96SEvan Quan 	fLeakage = fMultiply(ln_max_div_min, Convert_ULONG_ToFraction(leakageID_fuse));
211e098bc96SEvan Quan 	fLeakage = fDivide(fLeakage, f_bit_max_value);
212e098bc96SEvan Quan 	fLeakage = fExponential(fLeakage);
213e098bc96SEvan Quan 	fLeakage = fMultiply(fLeakage, f_min);
214e098bc96SEvan Quan 
215e098bc96SEvan Quan 	return fLeakage;
216e098bc96SEvan Quan }
217e098bc96SEvan Quan 
ConvertToFraction(int X)218e098bc96SEvan Quan static fInt ConvertToFraction(int X) /*Add all range checking here. Is it possible to make fInt a private declaration? */
219e098bc96SEvan Quan {
220e098bc96SEvan Quan 	fInt temp;
221e098bc96SEvan Quan 
222e098bc96SEvan Quan 	if (X <= MAX)
223e098bc96SEvan Quan 		temp.full = (X << SHIFT_AMOUNT);
224e098bc96SEvan Quan 	else
225e098bc96SEvan Quan 		temp.full = 0;
226e098bc96SEvan Quan 
227e098bc96SEvan Quan 	return temp;
228e098bc96SEvan Quan }
229e098bc96SEvan Quan 
fNegate(fInt X)230e098bc96SEvan Quan static fInt fNegate(fInt X)
231e098bc96SEvan Quan {
232e098bc96SEvan Quan 	fInt CONSTANT_NEGONE = ConvertToFraction(-1);
233*16d12233SRan Sun 	return fMultiply(X, CONSTANT_NEGONE);
234e098bc96SEvan Quan }
235e098bc96SEvan Quan 
Convert_ULONG_ToFraction(uint32_t X)236e098bc96SEvan Quan static fInt Convert_ULONG_ToFraction(uint32_t X)
237e098bc96SEvan Quan {
238e098bc96SEvan Quan 	fInt temp;
239e098bc96SEvan Quan 
240e098bc96SEvan Quan 	if (X <= MAX)
241e098bc96SEvan Quan 		temp.full = (X << SHIFT_AMOUNT);
242e098bc96SEvan Quan 	else
243e098bc96SEvan Quan 		temp.full = 0;
244e098bc96SEvan Quan 
245e098bc96SEvan Quan 	return temp;
246e098bc96SEvan Quan }
247e098bc96SEvan Quan 
GetScaledFraction(int X,int factor)248e098bc96SEvan Quan static fInt GetScaledFraction(int X, int factor)
249e098bc96SEvan Quan {
250e098bc96SEvan Quan 	int times_shifted, factor_shifted;
251e098bc96SEvan Quan 	bool bNEGATED;
252e098bc96SEvan Quan 	fInt fValue;
253e098bc96SEvan Quan 
254e098bc96SEvan Quan 	times_shifted = 0;
255e098bc96SEvan Quan 	factor_shifted = 0;
256e098bc96SEvan Quan 	bNEGATED = false;
257e098bc96SEvan Quan 
258e098bc96SEvan Quan 	if (X < 0) {
259e098bc96SEvan Quan 		X = -1*X;
260e098bc96SEvan Quan 		bNEGATED = true;
261e098bc96SEvan Quan 	}
262e098bc96SEvan Quan 
263e098bc96SEvan Quan 	if (factor < 0) {
264e098bc96SEvan Quan 		factor = -1*factor;
265e098bc96SEvan Quan 		bNEGATED = !bNEGATED; /*If bNEGATED = true due to X < 0, this will cover the case of negative cancelling negative */
266e098bc96SEvan Quan 	}
267e098bc96SEvan Quan 
268e098bc96SEvan Quan 	if ((X > MAX) || factor > MAX) {
269e098bc96SEvan Quan 		if ((X/factor) <= MAX) {
270e098bc96SEvan Quan 			while (X > MAX) {
271e098bc96SEvan Quan 				X = X >> 1;
272e098bc96SEvan Quan 				times_shifted++;
273e098bc96SEvan Quan 			}
274e098bc96SEvan Quan 
275e098bc96SEvan Quan 			while (factor > MAX) {
276e098bc96SEvan Quan 				factor = factor >> 1;
277e098bc96SEvan Quan 				factor_shifted++;
278e098bc96SEvan Quan 			}
279e098bc96SEvan Quan 		} else {
280e098bc96SEvan Quan 			fValue.full = 0;
281e098bc96SEvan Quan 			return fValue;
282e098bc96SEvan Quan 		}
283e098bc96SEvan Quan 	}
284e098bc96SEvan Quan 
285e098bc96SEvan Quan 	if (factor == 1)
286e098bc96SEvan Quan 		return ConvertToFraction(X);
287e098bc96SEvan Quan 
288e098bc96SEvan Quan 	fValue = fDivide(ConvertToFraction(X * uPow(-1, bNEGATED)), ConvertToFraction(factor));
289e098bc96SEvan Quan 
290e098bc96SEvan Quan 	fValue.full = fValue.full << times_shifted;
291e098bc96SEvan Quan 	fValue.full = fValue.full >> factor_shifted;
292e098bc96SEvan Quan 
293e098bc96SEvan Quan 	return fValue;
294e098bc96SEvan Quan }
295e098bc96SEvan Quan 
296e098bc96SEvan Quan /* Addition using two fInts */
fAdd(fInt X,fInt Y)297e098bc96SEvan Quan static fInt fAdd (fInt X, fInt Y)
298e098bc96SEvan Quan {
299e098bc96SEvan Quan 	fInt Sum;
300e098bc96SEvan Quan 
301e098bc96SEvan Quan 	Sum.full = X.full + Y.full;
302e098bc96SEvan Quan 
303e098bc96SEvan Quan 	return Sum;
304e098bc96SEvan Quan }
305e098bc96SEvan Quan 
306e098bc96SEvan Quan /* Addition using two fInts */
fSubtract(fInt X,fInt Y)307e098bc96SEvan Quan static fInt fSubtract (fInt X, fInt Y)
308e098bc96SEvan Quan {
309e098bc96SEvan Quan 	fInt Difference;
310e098bc96SEvan Quan 
311e098bc96SEvan Quan 	Difference.full = X.full - Y.full;
312e098bc96SEvan Quan 
313e098bc96SEvan Quan 	return Difference;
314e098bc96SEvan Quan }
315e098bc96SEvan Quan 
Equal(fInt A,fInt B)316e098bc96SEvan Quan static bool Equal(fInt A, fInt B)
317e098bc96SEvan Quan {
318e098bc96SEvan Quan 	if (A.full == B.full)
319e098bc96SEvan Quan 		return true;
320e098bc96SEvan Quan 	else
321e098bc96SEvan Quan 		return false;
322e098bc96SEvan Quan }
323e098bc96SEvan Quan 
GreaterThan(fInt A,fInt B)324e098bc96SEvan Quan static bool GreaterThan(fInt A, fInt B)
325e098bc96SEvan Quan {
326e098bc96SEvan Quan 	if (A.full > B.full)
327e098bc96SEvan Quan 		return true;
328e098bc96SEvan Quan 	else
329e098bc96SEvan Quan 		return false;
330e098bc96SEvan Quan }
331e098bc96SEvan Quan 
fMultiply(fInt X,fInt Y)332e098bc96SEvan Quan static fInt fMultiply (fInt X, fInt Y) /* Uses 64-bit integers (int64_t) */
333e098bc96SEvan Quan {
334e098bc96SEvan Quan 	fInt Product;
335e098bc96SEvan Quan 	int64_t tempProduct;
3364c3508feSLee Jones 
3374c3508feSLee Jones 	/*The following is for a very specific common case: Non-zero number with ONLY fractional portion*/
3384c3508feSLee Jones 	/* TEMPORARILY DISABLED - CAN BE USED TO IMPROVE PRECISION
339e098bc96SEvan Quan 	bool X_LessThanOne, Y_LessThanOne;
340e098bc96SEvan Quan 
341e098bc96SEvan Quan 	X_LessThanOne = (X.partial.real == 0 && X.partial.decimal != 0 && X.full >= 0);
342e098bc96SEvan Quan 	Y_LessThanOne = (Y.partial.real == 0 && Y.partial.decimal != 0 && Y.full >= 0);
343e098bc96SEvan Quan 
344e098bc96SEvan Quan 	if (X_LessThanOne && Y_LessThanOne) {
345e098bc96SEvan Quan 		Product.full = X.full * Y.full;
346e098bc96SEvan Quan 		return Product
347e098bc96SEvan Quan 	}*/
348e098bc96SEvan Quan 
349e098bc96SEvan Quan 	tempProduct = ((int64_t)X.full) * ((int64_t)Y.full); /*Q(16,16)*Q(16,16) = Q(32, 32) - Might become a negative number! */
350e098bc96SEvan Quan 	tempProduct = tempProduct >> 16; /*Remove lagging 16 bits - Will lose some precision from decimal; */
351e098bc96SEvan Quan 	Product.full = (int)tempProduct; /*The int64_t will lose the leading 16 bits that were part of the integer portion */
352e098bc96SEvan Quan 
353e098bc96SEvan Quan 	return Product;
354e098bc96SEvan Quan }
355e098bc96SEvan Quan 
fDivide(fInt X,fInt Y)356e098bc96SEvan Quan static fInt fDivide (fInt X, fInt Y)
357e098bc96SEvan Quan {
358e098bc96SEvan Quan 	fInt fZERO, fQuotient;
359e098bc96SEvan Quan 	int64_t longlongX, longlongY;
360e098bc96SEvan Quan 
361e098bc96SEvan Quan 	fZERO = ConvertToFraction(0);
362e098bc96SEvan Quan 
363e098bc96SEvan Quan 	if (Equal(Y, fZERO))
364e098bc96SEvan Quan 		return fZERO;
365e098bc96SEvan Quan 
366e098bc96SEvan Quan 	longlongX = (int64_t)X.full;
367e098bc96SEvan Quan 	longlongY = (int64_t)Y.full;
368e098bc96SEvan Quan 
369e098bc96SEvan Quan 	longlongX = longlongX << 16; /*Q(16,16) -> Q(32,32) */
370e098bc96SEvan Quan 
371e098bc96SEvan Quan 	div64_s64(longlongX, longlongY); /*Q(32,32) divided by Q(16,16) = Q(16,16) Back to original format */
372e098bc96SEvan Quan 
373e098bc96SEvan Quan 	fQuotient.full = (int)longlongX;
374e098bc96SEvan Quan 	return fQuotient;
375e098bc96SEvan Quan }
376e098bc96SEvan Quan 
ConvertBackToInteger(fInt A)377e098bc96SEvan Quan static int ConvertBackToInteger (fInt A) /*THIS is the function that will be used to check with the Golden settings table*/
378e098bc96SEvan Quan {
379e098bc96SEvan Quan 	fInt fullNumber, scaledDecimal, scaledReal;
380e098bc96SEvan Quan 
381e098bc96SEvan Quan 	scaledReal.full = GetReal(A) * uPow(10, PRECISION-1); /* DOUBLE CHECK THISSSS!!! */
382e098bc96SEvan Quan 
383e098bc96SEvan Quan 	scaledDecimal.full = uGetScaledDecimal(A);
384e098bc96SEvan Quan 
385e098bc96SEvan Quan 	fullNumber = fAdd(scaledDecimal, scaledReal);
386e098bc96SEvan Quan 
387e098bc96SEvan Quan 	return fullNumber.full;
388e098bc96SEvan Quan }
389e098bc96SEvan Quan 
fGetSquare(fInt A)390e098bc96SEvan Quan static fInt fGetSquare(fInt A)
391e098bc96SEvan Quan {
392e098bc96SEvan Quan 	return fMultiply(A, A);
393e098bc96SEvan Quan }
394e098bc96SEvan Quan 
395e098bc96SEvan Quan /* x_new = x_old - (x_old^2 - C) / (2 * x_old) */
fSqrt(fInt num)396e098bc96SEvan Quan static fInt fSqrt(fInt num)
397e098bc96SEvan Quan {
398e098bc96SEvan Quan 	fInt F_divide_Fprime, Fprime;
399e098bc96SEvan Quan 	fInt test;
400e098bc96SEvan Quan 	fInt twoShifted;
401e098bc96SEvan Quan 	int seed, counter, error;
402e098bc96SEvan Quan 	fInt x_new, x_old, C, y;
403e098bc96SEvan Quan 
404e098bc96SEvan Quan 	fInt fZERO = ConvertToFraction(0);
405e098bc96SEvan Quan 
406e098bc96SEvan Quan 	/* (0 > num) is the same as (num < 0), i.e., num is negative */
407e098bc96SEvan Quan 
408e098bc96SEvan Quan 	if (GreaterThan(fZERO, num) || Equal(fZERO, num))
409e098bc96SEvan Quan 		return fZERO;
410e098bc96SEvan Quan 
411e098bc96SEvan Quan 	C = num;
412e098bc96SEvan Quan 
413e098bc96SEvan Quan 	if (num.partial.real > 3000)
414e098bc96SEvan Quan 		seed = 60;
415e098bc96SEvan Quan 	else if (num.partial.real > 1000)
416e098bc96SEvan Quan 		seed = 30;
417e098bc96SEvan Quan 	else if (num.partial.real > 100)
418e098bc96SEvan Quan 		seed = 10;
419e098bc96SEvan Quan 	else
420e098bc96SEvan Quan 		seed = 2;
421e098bc96SEvan Quan 
422e098bc96SEvan Quan 	counter = 0;
423e098bc96SEvan Quan 
424e098bc96SEvan Quan 	if (Equal(num, fZERO)) /*Square Root of Zero is zero */
425e098bc96SEvan Quan 		return fZERO;
426e098bc96SEvan Quan 
427e098bc96SEvan Quan 	twoShifted = ConvertToFraction(2);
428e098bc96SEvan Quan 	x_new = ConvertToFraction(seed);
429e098bc96SEvan Quan 
430e098bc96SEvan Quan 	do {
431e098bc96SEvan Quan 		counter++;
432e098bc96SEvan Quan 
433e098bc96SEvan Quan 		x_old.full = x_new.full;
434e098bc96SEvan Quan 
435e098bc96SEvan Quan 		test = fGetSquare(x_old); /*1.75*1.75 is reverting back to 1 when shifted down */
436e098bc96SEvan Quan 		y = fSubtract(test, C); /*y = f(x) = x^2 - C; */
437e098bc96SEvan Quan 
438e098bc96SEvan Quan 		Fprime = fMultiply(twoShifted, x_old);
439e098bc96SEvan Quan 		F_divide_Fprime = fDivide(y, Fprime);
440e098bc96SEvan Quan 
441e098bc96SEvan Quan 		x_new = fSubtract(x_old, F_divide_Fprime);
442e098bc96SEvan Quan 
443e098bc96SEvan Quan 		error = ConvertBackToInteger(x_new) - ConvertBackToInteger(x_old);
444e098bc96SEvan Quan 
445e098bc96SEvan Quan 		if (counter > 20) /*20 is already way too many iterations. If we dont have an answer by then, we never will*/
446e098bc96SEvan Quan 			return x_new;
447e098bc96SEvan Quan 
448e098bc96SEvan Quan 	} while (uAbs(error) > 0);
449e098bc96SEvan Quan 
450*16d12233SRan Sun 	return x_new;
451e098bc96SEvan Quan }
452e098bc96SEvan Quan 
SolveQuadracticEqn(fInt A,fInt B,fInt C,fInt Roots[])453e098bc96SEvan Quan static void SolveQuadracticEqn(fInt A, fInt B, fInt C, fInt Roots[])
454e098bc96SEvan Quan {
455e098bc96SEvan Quan 	fInt *pRoots = &Roots[0];
456e098bc96SEvan Quan 	fInt temp, root_first, root_second;
457e098bc96SEvan Quan 	fInt f_CONSTANT10, f_CONSTANT100;
458e098bc96SEvan Quan 
459e098bc96SEvan Quan 	f_CONSTANT100 = ConvertToFraction(100);
460e098bc96SEvan Quan 	f_CONSTANT10 = ConvertToFraction(10);
461e098bc96SEvan Quan 
462e098bc96SEvan Quan 	while (GreaterThan(A, f_CONSTANT100) || GreaterThan(B, f_CONSTANT100) || GreaterThan(C, f_CONSTANT100)) {
463e098bc96SEvan Quan 		A = fDivide(A, f_CONSTANT10);
464e098bc96SEvan Quan 		B = fDivide(B, f_CONSTANT10);
465e098bc96SEvan Quan 		C = fDivide(C, f_CONSTANT10);
466e098bc96SEvan Quan 	}
467e098bc96SEvan Quan 
468e098bc96SEvan Quan 	temp = fMultiply(ConvertToFraction(4), A); /* root = 4*A */
469e098bc96SEvan Quan 	temp = fMultiply(temp, C); /* root = 4*A*C */
470e098bc96SEvan Quan 	temp = fSubtract(fGetSquare(B), temp); /* root = b^2 - 4AC */
471e098bc96SEvan Quan 	temp = fSqrt(temp); /*root = Sqrt (b^2 - 4AC); */
472e098bc96SEvan Quan 
473e098bc96SEvan Quan 	root_first = fSubtract(fNegate(B), temp); /* b - Sqrt(b^2 - 4AC) */
474e098bc96SEvan Quan 	root_second = fAdd(fNegate(B), temp); /* b + Sqrt(b^2 - 4AC) */
475e098bc96SEvan Quan 
476e098bc96SEvan Quan 	root_first = fDivide(root_first, ConvertToFraction(2)); /* [b +- Sqrt(b^2 - 4AC)]/[2] */
477e098bc96SEvan Quan 	root_first = fDivide(root_first, A); /*[b +- Sqrt(b^2 - 4AC)]/[2*A] */
478e098bc96SEvan Quan 
479e098bc96SEvan Quan 	root_second = fDivide(root_second, ConvertToFraction(2)); /* [b +- Sqrt(b^2 - 4AC)]/[2] */
480e098bc96SEvan Quan 	root_second = fDivide(root_second, A); /*[b +- Sqrt(b^2 - 4AC)]/[2*A] */
481e098bc96SEvan Quan 
482e098bc96SEvan Quan 	*(pRoots + 0) = root_first;
483e098bc96SEvan Quan 	*(pRoots + 1) = root_second;
484e098bc96SEvan Quan }
485e098bc96SEvan Quan 
486e098bc96SEvan Quan /* -----------------------------------------------------------------------------
487e098bc96SEvan Quan  * SUPPORT FUNCTIONS
488e098bc96SEvan Quan  * -----------------------------------------------------------------------------
489e098bc96SEvan Quan  */
490e098bc96SEvan Quan 
491e098bc96SEvan Quan /* Conversion Functions */
GetReal(fInt A)492e098bc96SEvan Quan static int GetReal (fInt A)
493e098bc96SEvan Quan {
494e098bc96SEvan Quan 	return (A.full >> SHIFT_AMOUNT);
495e098bc96SEvan Quan }
496e098bc96SEvan Quan 
Divide(int X,int Y)497e098bc96SEvan Quan static fInt Divide (int X, int Y)
498e098bc96SEvan Quan {
499e098bc96SEvan Quan 	fInt A, B, Quotient;
500e098bc96SEvan Quan 
501e098bc96SEvan Quan 	A.full = X << SHIFT_AMOUNT;
502e098bc96SEvan Quan 	B.full = Y << SHIFT_AMOUNT;
503e098bc96SEvan Quan 
504e098bc96SEvan Quan 	Quotient = fDivide(A, B);
505e098bc96SEvan Quan 
506e098bc96SEvan Quan 	return Quotient;
507e098bc96SEvan Quan }
508e098bc96SEvan Quan 
uGetScaledDecimal(fInt A)509e098bc96SEvan Quan static int uGetScaledDecimal (fInt A) /*Converts the fractional portion to whole integers - Costly function */
510e098bc96SEvan Quan {
511e098bc96SEvan Quan 	int dec[PRECISION];
512e098bc96SEvan Quan 	int i, scaledDecimal = 0, tmp = A.partial.decimal;
513e098bc96SEvan Quan 
514e098bc96SEvan Quan 	for (i = 0; i < PRECISION; i++) {
515e098bc96SEvan Quan 		dec[i] = tmp / (1 << SHIFT_AMOUNT);
516e098bc96SEvan Quan 		tmp = tmp - ((1 << SHIFT_AMOUNT)*dec[i]);
517e098bc96SEvan Quan 		tmp *= 10;
518e098bc96SEvan Quan 		scaledDecimal = scaledDecimal + dec[i]*uPow(10, PRECISION - 1 - i);
519e098bc96SEvan Quan 	}
520e098bc96SEvan Quan 
521e098bc96SEvan Quan 	return scaledDecimal;
522e098bc96SEvan Quan }
523e098bc96SEvan Quan 
uPow(int base,int power)524e098bc96SEvan Quan static int uPow(int base, int power)
525e098bc96SEvan Quan {
526e098bc96SEvan Quan 	if (power == 0)
527e098bc96SEvan Quan 		return 1;
528e098bc96SEvan Quan 	else
529e098bc96SEvan Quan 		return (base)*uPow(base, power - 1);
530e098bc96SEvan Quan }
531e098bc96SEvan Quan 
uAbs(int X)532e098bc96SEvan Quan static int uAbs(int X)
533e098bc96SEvan Quan {
534e098bc96SEvan Quan 	if (X < 0)
535e098bc96SEvan Quan 		return (X * -1);
536e098bc96SEvan Quan 	else
537e098bc96SEvan Quan 		return X;
538e098bc96SEvan Quan }
539e098bc96SEvan Quan 
fRoundUpByStepSize(fInt A,fInt fStepSize,bool error_term)540e098bc96SEvan Quan static fInt fRoundUpByStepSize(fInt A, fInt fStepSize, bool error_term)
541e098bc96SEvan Quan {
542e098bc96SEvan Quan 	fInt solution;
543e098bc96SEvan Quan 
544e098bc96SEvan Quan 	solution = fDivide(A, fStepSize);
545e098bc96SEvan Quan 	solution.partial.decimal = 0; /*All fractional digits changes to 0 */
546e098bc96SEvan Quan 
547e098bc96SEvan Quan 	if (error_term)
548e098bc96SEvan Quan 		solution.partial.real += 1; /*Error term of 1 added */
549e098bc96SEvan Quan 
550e098bc96SEvan Quan 	solution = fMultiply(solution, fStepSize);
551e098bc96SEvan Quan 	solution = fAdd(solution, fStepSize);
552e098bc96SEvan Quan 
553e098bc96SEvan Quan 	return solution;
554e098bc96SEvan Quan }
555e098bc96SEvan Quan 
556