This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

IQMath function IQNdiv blows up when den = 0

Hi

IQMath lib 2.1.4.0

C64xplus

IQNdiv

According to the header on this function in IQmath_inline.h

;; On Exit: A4 = Num/Den result in IQ format
;; = 0x7FFFFFFF if +ve overflow
;; = 0x80000000 if -ve overflow
;; = 0x7FFFFFFF if Den == 0

However, if den = 0, the offset in the code below becomes 0xffffff81 and is a really bad offset to use in IQDivTable[] and will give you a memory exception.

t1 = qfmt + sbits;
var2 = (59U - t1);
offset = (U32_IQ)Dnorm >> 23U;
offset = offset - 127U;

Dm=IQdivTable[offset];

Cheers2u

Eddie

  • Hi Eddie,

    Thanks for your post.

    In my opinion, the IQNdiv module divide two IQN number and provide the result in 32-bit aligned IQN format as per newton raphson technique and with this context and as per your code snippet "offset = (U32_IQ)Dnorm >> 23U", the left side operand "offset" and the right side operand "Dnorm" variable rightshifted by 23 should both be 32-bit aligned and typecasted with IQN format. Please ensure both the operands are 32-bit aligned and typecasted with IQN format appropriately, so that, you could avoid memory exception issues.

    For more details, please refer section IQNdiv module C64x+ IQmath library user guide as below:

    http://www.ti.com/lit/ug/sprugg9/sprugg9.pdf (page no 39)

     

    Thanks & regards,

    Sivaraj K

    -------------------------------------------------------------------------------------------------------

    Please click the Verify Answer button on this post if it answers your question.

    -------------------------------------------------------------------------------------------------------

  • Hi

    Thanks for the reply

    I've stepped through the code and this is what I see happening:

    If den = 0, then hi1 = 0, and consequently Dnorm also equals zero. Then offset then computes to

    offset = (U32_IQ)Dnorm >> 23U
    = 0 >> 23
    = 0

    Then offset is adjusted

    offset = offset - 127U;
    = 0 - 127
    = -127

    Using the negative number -127 to index the table results in an invalid memory access.

    The IQN format for zero is 0. True?

    Cheers

    Eddie

        sbits = _norm(den);   
        hi1 = den;     
        Dnorm = (I32_IQ)(hi1 << sbits);
    
        /*==========================================================================*/
        /* Step 2)  Obtain initial estimate of Dm = 1/Dnorm from                    */
        /*          "_IQdivTable" using the upper 8-bits of the                     */
        /*          normalized value.                                               */
        /* =========================================================================*/
    
        t1 = qfmt + sbits;
        var2 = (59U - t1);
        offset = (U32_IQ)Dnorm >> 23U; 
        offset = offset - 127U;    
    
        Dm=IQdivTable[offset];