Reordering the formula, we have
OutClk/(Mult1*Mult2*Mult3) = InClk/(Div1*Div2);
Take a look at Mult1 = {1, 2} and Mult2 = {1, 2, 4, 8} , note that they all have the power of two.
Similarly, with Div1 and Div2 , they also have the power of two.
Mult3 = {3,5,7} , which are all primes.
So what we need to do is split both InClk and OutClk into their largest common divisor (GCD)
int g = gcd(InClk, OutClk); InClk /= g; OutClk/= g;
For InClk == OutClk we need to make both InClk/g and OutClk/g equal to 1.
And what remains of InClk after the division, we will try to divide it into the largest element in each div_vals , which InClk can divide. (Since each element in div_vals everything has a power of two, so we need to choose the largest).
for(int i = sizeof(div1_vals) - 1; i>= 0; i--) if(InClk % div1_vals[i] == 0){ InClk/= div1_vals[i]; break; } for(int i = sizeof(div2_vals) - 1; i>= 0; i--) if(InClk % div2_vals[i] == 0){ InClk/= div2_vals[i]; break; }
Similarly for OutClk
for(int i = sizeof(mul1_vals) - 1; i>= 0; i--) if(OutClk % mul1_vals[i] == 0){ OutClk/= mul1_vals[i]; break; } ....
In the end, if InClk == 1 and OutClk == 1 , we will return true, else false.
The complexity of the time is O (n) , where n is the maximum number of elements in all mul1_vals, ...
source share