XilinxProcessorIPLib/drivers/v_hdmitxss1/examples/xhdmi_example/si5324drv.c
void Si5324_RatApprox(u64 f, u64 md, u32 *num, u32 *denom)
{
    /*  a: Continued fraction coefficients. */
    u64 a, h[3] = { 0, 1, 0 }, k[3] = { 1, 0, 0 };
    u64 x, d, n = 1;
    int i = 0;

    // Degenerate case: only n/1 solution allowed. Return trunc(f)/1.
    if (md <= 1) {
        *denom = 1;
        *num = (u32)(f >> 28);
        return;
    }

    // Multiply fraction until there are no more digits after the decimal point
    n <<= 28;
    for (i = 0; i < 28; i++) {
        if ((f & 0x1) == 0) {
            n >>= 1;
            f >>= 1;
        } else {
            break;
        }
    }
    d = f;

    /* Continued fraction and check denominator each step */
    for (i = 0; i < 64; i++) {
        a = n ? d / n : 0;
        if (i && !a) {
            break;
        }

        x = d;
        d = n;
        n = x % n;

        x = a;
        if (k[1] * a + k[0] >= md) {
            x = (md - k[0]) / k[1];
            if (x * 2 >= a || k[1] >= md) {
                i = 65;
            } else {
                break;
            }
        }

        h[2] = x * h[1] + h[0];
        h[0] = h[1];
        h[1] = h[2];
        k[2] = x * k[1] + k[0];
        k[0] = k[1];
        k[1] = k[2];
    }
    *denom = (u32)k[1];
    *num   = (u32)h[1];
}
/*****************************************************************************/
/**
 * Find the closest rational approximation for the N2_LS/N3 fraction.
 *
 * @param f  Holds the N2_LS/N3 fraction in 36.28 fixed point notation.
 * @param md Holds the maximum denominator (N3) value allowed.
 * @param num Will store the numinator (N2_LS) found.
 * @param denom Will store the denominator (N3) found.
 */
Find the closest rational approximation for the N2_LS/N3 fraction.
@param f  Holds the N2_LS/N3 fraction in 36.28 fixed point notation.
@param md Holds the maximum denominator (N3) value allowed.
@param num Will store the numinator (N2_LS) found.
@param denom Will store the denominator (N3) found.
Find the closest rational approximation for the N2_LS / N3 fraction . @param f Holds the N2_LS / N3 fraction in 36 . 28 fixed point notation . @param md Holds the maximum denominator ( N3 ) value allowed . @param num Will store the numinator ( N2_LS ) found . @param denom Will store the denominator ( N3 ) found .
void Si5324_RatApprox(u64 f, u64 md, u32 *num, u32 *denom)
{
    u64 a, h[3] = { 0, 1, 0 }, k[3] = { 1, 0, 0 };
    u64 x, d, n = 1;
    int i = 0;
    if (md <= 1) {
        *denom = 1;
        *num = (u32)(f >> 28);
        return;
    }
    n <<= 28;
    for (i = 0; i < 28; i++) {
        if ((f & 0x1) == 0) {
            n >>= 1;
            f >>= 1;
        } else {
            break;
        }
    }
    d = f;
    for (i = 0; i < 64; i++) {
        a = n ? d / n : 0;
        if (i && !a) {
            break;
        }
        x = d;
        d = n;
        n = x % n;
        x = a;
        if (k[1] * a + k[0] >= md) {
            x = (md - k[0]) / k[1];
            if (x * 2 >= a || k[1] >= md) {
                i = 65;
            } else {
                break;
            }
        }
        h[2] = x * h[1] + h[0];
        h[0] = h[1];
        h[1] = h[2];
        k[2] = x * k[1] + k[0];
        k[0] = k[1];
        k[1] = k[2];
    }
    *denom = (u32)k[1];
    *num   = (u32)h[1];
}
void Si5324_RatApprox ( u64 f , u64 md , u32 * num , u32 * denom ) { u64 a , h [ 3 ] = { 0 , 1 , 0 } , k [ 3 ] = { 1 , 0 , 0 } ; u64 x , d , n = 1 ; int i = 0 ; if ( md <= 1 ) { * denom = 1 ; * num = ( u32 ) ( f >> 28 ) ; return ; } n <<= 28 ; for ( i = 0 ; i < 28 ; i ++ ) { if ( ( f & 0x1 ) == 0 ) { n >>= 1 ; f >>= 1 ; } else { break ; } } d = f ; for ( i = 0 ; i < 64 ; i ++ ) { a = n ? d / n : 0 ; if ( i && ! a ) { break ; } x = d ; d = n ; n = x % n ; x = a ; if ( k [ 1 ] * a + k [ 0 ] >= md ) { x = ( md - k [ 0 ] ) / k [ 1 ] ; if ( x * 2 >= a || k [ 1 ] >= md ) { i = 65 ; } else { break ; } } h [ 2 ] = x * h [ 1 ] + h [ 0 ] ; h [ 0 ] = h [ 1 ] ; h [ 1 ] = h [ 2 ] ; k [ 2 ] = x * k [ 1 ] + k [ 0 ] ; k [ 0 ] = k [ 1 ] ; k [ 1 ] = k [ 2 ] ; } * denom = ( u32 ) k [ 1 ] ; * num = ( u32 ) h [ 1 ] ; }
Find the closest rational approximation for the N2_LS/N3 fraction.
Find the closest rational approximation for the N2_LS / N3 fraction .
/*  a: Continued fraction coefficients. */ // Degenerate case: only n/1 solution allowed. Return trunc(f)/1. // Multiply fraction until there are no more digits after the decimal point /* Continued fraction and check denominator each step */