How to use _Q16reciprocalQ16()?

Author
aschen0866
Super Member
  • Total Posts : 4156
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
2009/10/07 18:49:07 (permalink)
0

How to use _Q16reciprocalQ16()?

The library guide is quite straightforward, you input Q16 x and get Q16 1/x back. However, I can't seem to get the right value out of this function.

#include <p24fxxxx.h>
#include <libq.h>

int main(void)
{
_Q16 foo = _Q16ftoi(0.125); /*foo = 0x0000_2000 */

bar = _Q16reciprocalQ16(foo); /*bar = 0x7FFF_FFFF ???*/


while (1);
}

Any ideas?
#1

10 Replies Related Threads

    Mike017
    Super Member
    • Total Posts : 2698
    • Reward points : 0
    • Joined: 2007/08/25 08:11:31
    • Location: Lincoln, RI, USA
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/10/07 20:27:05 (permalink)
    0
    Hi,
     
    It seems to go out of range for values from .5 to -.5  Sorry I can't be more help.
     
    Good Luck,
    Mike
    #2
    BenMichaud21
    Junior Member
    • Total Posts : 101
    • Reward points : 0
    • Joined: 2008/11/21 19:27:57
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/10/08 07:32:21 (permalink)
    0
    Maybe look at how a decimal value is represented in that architecture with that compiler. How many bits does the compiler use for the power and the number? Just throwing it out there.
    #3
    aschen0866
    Super Member
    • Total Posts : 4156
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/10/08 11:55:45 (permalink)
    0
    Thanks for replies. The more I look at it, the more it feels like a bug and I'll let the Tech Support deal with it. In the meantime, what I am trying to do is to handle the division of two Q16 numbers. Since the Q16 library doesn't have a function for doing Q16 division and the reciprocal function seems to be broken, do you guys have any suggestion how I can proceed my Q16 division?
    post edited by aschen0866 - 2009/10/08 12:04:02
    #4
    cawilkie
    Administrator
    • Total Posts : 1960
    • Reward points : 0
    • Joined: 2003/11/07 12:49:11
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/10/08 15:28:47 (permalink)
    +2 (1)
    Believe it or not... I once wrote a test divide routine. I haven't used it in a long time and don't remember the state it was in when I last looked at it. Caveat emptor.

    You are welcome to them. I am surprised that there is no Q16 divide.

    Regards
    Calum
    #5
    aschen0866
    Super Member
    • Total Posts : 4156
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/10/09 07:48:28 (permalink)
    0
    Calum,
    Thanks for those two routines. They work very well for me. Do you see them being added to the fixed-point library in the future release?

    Best Regards,

    Alan
    #6
    cawilkie
    Administrator
    • Total Posts : 1960
    • Reward points : 0
    • Joined: 2003/11/07 12:49:11
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/10/09 10:06:29 (permalink)
    0
    Alan,

    Glad they work out for you. I'll talk to the responsible team and see what the scoop is.

    Regards
    Calum
    #7
    Isaac_Sewell
    Starting Member
    • Total Posts : 90
    • Reward points : 0
    • Joined: 2003/11/25 06:26:50
    • Location: Sheffield, England
    • Status: offline
    RE: How to use _Q16reciprocalQ16()? 2009/12/18 03:38:35 (permalink)
    0
    Thanks a lot for making these available. you've saved me a major headache!
    #8
    awns729
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/03/01 23:05:30
    • Location: 0
    • Status: offline
    Re: RE: How to use _Q16reciprocalQ16()? 2011/03/04 19:07:52 (permalink)
    0
    How do you go about adding those files to a C project? (Student here)

    Also, one is for signed Q16 and the other unsigned, correct?

    Thanks,
    Arwin
    #9
    aschen0866
    Super Member
    • Total Posts : 4156
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re: RE: How to use _Q16reciprocalQ16()? 2011/03/05 08:39:06 (permalink)
    0
    awns729

    How do you go about adding those files to a C project? (Student here)


    Read C30 User's Guide Section 9.3. first. Here is what you can do in this case:
    1. Add a new file, for example, call it 'fixed_point_math.s', to your project. Copy those assembly routines to fixed_point_math.s.
    2. Add another new file, for example, call it 'fixed_point_math.h', to your project. Add these lines to fixed_point_math.h:

    #ifndef FIXED_POINT_MATH_H
    #define FIXED_POINT_MATH_H
    #include <libq.h>
    _Q16 _uIQ16div(_Q16, _Q16);
    _Q16 _IQ16div(_Q16, _Q16);
    #endif

    Include fixed_point_math.h to any C file where you want to make _Q16 calls.
    #10
    awns729
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2011/03/01 23:05:30
    • Location: 0
    • Status: offline
    Re: RE: How to use _Q16reciprocalQ16()? 2011/03/06 12:29:29 (permalink)
    0
    aschen0866, thanks. that worked for me. didn't realize that document existed. i was looking at the 16 bit lang tools library. 

    calum, your routine has been a lot of help. i really appreciate you posting it.
    #11
    Jump to:
    © 2017 APG vNext Commercial Version 4.5