2 powerof (x) - where x fixed point value

Discussion in 'VHDL' started by MariuszK, Oct 6, 2006.

  1. MariuszK

    MariuszK Guest

    Hello,

    How can I calculate (optimal) 2 power of x ( 2^x)?

    Where x is signed fixed point value ( for example 1.5 or -1.75 etc...).


    Best regards
    Mariusz
     
    MariuszK, Oct 6, 2006
    #1
    1. Advertising

  2. MariuszK

    MariuszK Guest

    Hello,

    Actually I found next solution, but it is not precise:

    2^x ~ 2^INT(x)*(1+FRAC(x))
    Where:
    INT(X) - integer part of x
    FRAC(X) - fraction part of x

    example x= 43.125
    INT(43.125) = 43
    FRAC(43.125)= 0.125

    Any other idea?

    Mariusz
     
    MariuszK, Oct 6, 2006
    #2
    1. Advertising

  3. MariuszK

    David Ashley Guest

    MariuszK wrote:
    > Hello,
    >
    > Actually I found next solution, but it is not precise:
    >
    > 2^x ~ 2^INT(x)*(1+FRAC(x))
    > Where:
    > INT(X) - integer part of x
    > FRAC(X) - fraction part of x
    >
    > example x= 43.125
    > INT(43.125) = 43
    > FRAC(43.125)= 0.125
    >
    > Any other idea?
    >
    > Mariusz
    >


    I think you can use the fact that
    2^(a+b) = 2^a * 2^b

    You only have to deal with the fractional part
    by subtracting out the integer part of the
    exponent, and that's how many places you shift
    the result left.

    To produce the effect of the fractional part,
    you could use an array of multipliers, each bit
    in the fractional part either multiplying by 1 or
    by a factor.
    So in binary fractions:
    ..1 = 2 raised to the power of 1/2.
    ..01 = 2 raised to the power of 1/4
    ..001 = 2 raised to the power of 1/8
    You have a fixed point representation of 2^.5, 2^.25,
    2^.125, etc. If the bit in the exponent is 1, you
    multiply by the appropriate constant, otherwise
    you multiply by 1, and feed the result forward.

    so in binary: 10^.1001
    is in decimal 2^.5 * 2^.0625


    This is similiar to a multiplier circuit itself,
    where it's a serial array of optional adders,
    only it's a serial array of optional multipliers.
    You're helped because each multiply has one
    side always a constant value.

    IIRC, in one of these newsgroups, someone else
    recently suggested something very similiar, but
    if I read his suggestion correctly his approach was
    to do additions for each fractional bit. This wouldn't
    work.

    Hope this is useful.

    -Dave

    --
    David Ashley http://www.xdr.com/dash
    Embedded linux, device drivers, system architecture
     
    David Ashley, Oct 7, 2006
    #3
  4. MariuszK

    David Bishop Guest

    MariuszK wrote:
    > Hello,
    >
    > How can I calculate (optimal) 2 power of x ( 2^x)?
    >
    > Where x is signed fixed point value ( for example 1.5 or -1.75 etc...).


    Go to:
    http://www.vhdl.org/vhdl-200x/vhdl-200x-ft/packages/
    you will need
    math_utility_pkg.vhdl
    fixed_pkg_c.vhdl

    Then you can say:

    use ieee_proposed.fixed_pkg.all;
    ....
    signal xfixed : sfixed (3 downto -3);
    signal xpower : integer;

    xfixed <= to_sfixed (1.5, 3, -3);

    xpower <= find_msb (abs(xfixed));

    All synthesizable, and it should give you what you want.
     
    David Bishop, Oct 11, 2006
    #4
  5. MariuszK

    MariuszK Guest


    >
    > use ieee_proposed.fixed_pkg.all;
    > ...
    > signal xfixed : sfixed (3 downto -3);
    > signal xpower : integer;
    >
    > xfixed <= to_sfixed (1.5, 3, -3);
    >
    > xpower <= find_msb (abs(xfixed));
    >
    > All synthesizable, and it should give you what you want.


    Thank you David for answer.
    Currently I use your fixed_pkg package.
    It is the best fixed package which I have ever seen :)

    but...

    I want calculate 2^x with use vhdl
    for example:
    2^1.5 = 2.828427....
    2^(-1.75) = 0.2973017.....
    etc.....
    2 - is constant
    x- signed fixed point value

    find_msb return 0 for your example.
    ====================================
    by the way.......
    I started my implementation with use version 16 fixed_pkg few months
    ago.
    I have few question about your package. Especially I am interested in
    vhdl-93 compatibility fixed point version.

    1. Can I use your package without any restriction? ;)
    2. Where can I find the latest version of your package?
    Only here?: http://www.vhdl.org/vhdl-200x/vhdl-200x-ft/packages/

    3. Is any change log between versions (What was changed etc....) any
    source.
    4. What tools (version etc) are used or recommended to synthesis,
    implementation vhdl-93 compatibility version of package? (I have some
    problem with synthesis version with use ISE WebPack 8.2)
    5. The latest version 17 has not implemented find_msb function. ;)

    Best regards
    Mariusz
     
    MariuszK, Oct 11, 2006
    #5
  6. MariuszK

    David Bishop Guest

    MariuszK wrote:

    > I want calculate 2^x with use vhdl
    > for example:
    > 2^1.5 = 2.828427....
    > 2^(-1.75) = 0.2973017.....
    > etc.....
    > 2 - is constant
    > x- signed fixed point value
    >
    > find_msb return 0 for your example.


    I see. Different from what I thought. A few thoughts here. You could
    do a modified X^Y algorithm. However, considering the bit width I'd do
    a lookup table.

    > ====================================
    > by the way.......
    > I started my implementation with use version 16 fixed_pkg few months
    > ago.
    > I have few question about your package. Especially I am interested in
    > vhdl-93 compatibility fixed point version.
    >
    > 1. Can I use your package without any restriction? ;)


    Yes. That was the whole idea of giving it to the IEEE.

    > 2. Where can I find the latest version of your package?
    > Only here?: http://www.vhdl.org/vhdl-200x/vhdl-200x-ft/packages/


    Yes. At some time (once the package is fully published) I will probably
    put up another page with more algorithms I have been working on. Useful?

    > 3. Is any change log between versions (What was changed etc....) any
    > source.


    Everything is under RCS control. I can publish the logs if you like.

    > 4. What tools (version etc) are used or recommended to synthesis,
    > implementation vhdl-93 compatibility version of package? (I have some
    > problem with synthesis version with use ISE WebPack 8.2)


    I've tested these packages with Xilinx, Altera, Synplicity, Precision,
    Synopsys, and Cadence RC with varying success. I have "modified"
    versions of the packages which I got to work with all of these packages.
    I could put these on a web page if necessary.

    > 5. The latest version 17 has not implemented find_msb function. ;)


    The IEEE committee renamed it to be "find_leftmost". They do things
    like that to me.... Sorry forgot to put that in the last e-mail.
     
    David Bishop, Oct 15, 2006
    #6
  7. David Bishop wrote:

    > Yes. At some time (once the package is fully published) I will probably
    > put up another page with more algorithms I have been working on. Useful?


    Absolutely. Thanks. Excellent work.

    > Everything is under RCS control. I can publish the logs if you like.
    >


    That would be useful to the readers of this newsgroup.


    -- Mike Treseler
     
    Mike Treseler, Oct 16, 2006
    #7
  8. MariuszK

    MariuszK Guest

    David Bishop napisal(a):
    > MariuszK wrote:
    >
    > > I want calculate 2^x with use vhdl
    > > for example:
    > > 2^1.5 = 2.828427....
    > > 2^(-1.75) = 0.2973017.....
    > > etc.....
    > > 2 - is constant
    > > x- signed fixed point value
    > >
    > > find_msb return 0 for your example.

    >
    > I see. Different from what I thought. A few thoughts here. You could
    > do a modified X^Y algorithm. However, considering the bit width I'd do
    > a lookup table.
    >
    > > ====================================
    > > by the way.......
    > > I started my implementation with use version 16 fixed_pkg few months
    > > ago.
    > > I have few question about your package. Especially I am interested in
    > > vhdl-93 compatibility fixed point version.
    > >
    > > 1. Can I use your package without any restriction? ;)

    >
    > Yes. That was the whole idea of giving it to the IEEE.
    >
    > > 2. Where can I find the latest version of your package?
    > > Only here?: http://www.vhdl.org/vhdl-200x/vhdl-200x-ft/packages/

    >
    > Yes. At some time (once the package is fully published) I will probably
    > put up another page with more algorithms I have been working on. Useful?
    >
    > > 3. Is any change log between versions (What was changed etc....) any
    > > source.

    >
    > Everything is under RCS control. I can publish the logs if you like.
    >
    > > 4. What tools (version etc) are used or recommended to synthesis,
    > > implementation vhdl-93 compatibility version of package? (I have some
    > > problem with synthesis version with use ISE WebPack 8.2)

    >
    > I've tested these packages with Xilinx, Altera, Synplicity, Precision,
    > Synopsys, and Cadence RC with varying success. I have "modified"
    > versions of the packages which I got to work with all of these packages.
    > I could put these on a web page if necessary.
    >
    > > 5. The latest version 17 has not implemented find_msb function. ;)

    >
    > The IEEE committee renamed it to be "find_leftmost". They do things
    > like that to me.... Sorry forgot to put that in the last e-mail.


    David,
    I will be grateful to you for:
    1. publish change log
    2. publish modified version of package with information of synthesis
    tool (with version)

    Please, give sign where I can find that information. (if it possible of
    course)

    Best regards
    Mariusz.
     
    MariuszK, Oct 20, 2006
    #8
  9. MariuszK

    wallge Guest

    Read this paper:
    A Nonlinear Technique for Enhancement of Color Images:
    An Architectural Perspective for Real-Time Applications
    you can find it in IEEE xplore.

    They implement a linear interpolation of log2 and log2_inv.
    where if you want to find log2_inv(x.y),
    you just find 2^x and 2^(x + 1) through bit shifting and then linearly
    interpolate
    between the two using y



    MariuszK wrote:
    > Hello,
    >
    > How can I calculate (optimal) 2 power of x ( 2^x)?
    >
    > Where x is signed fixed point value ( for example 1.5 or -1.75 etc...).
    >
    >
    > Best regards
    > Mariusz
     
    wallge, Oct 22, 2006
    #9
  10. MariuszK

    MariuszK Guest

    wallge napisal(a):
    > Read this paper:
    > A Nonlinear Technique for Enhancement of Color Images:
    > An Architectural Perspective for Real-Time Applications
    > you can find it in IEEE xplore.
    >
    > They implement a linear interpolation of log2 and log2_inv.
    > where if you want to find log2_inv(x.y),
    > you just find 2^x and 2^(x + 1) through bit shifting and then linearly
    > interpolate
    > between the two using y
    >
    >
    >
    > MariuszK wrote:
    > > Hello,
    > >
    > > How can I calculate (optimal) 2 power of x ( 2^x)?
    > >
    > > Where x is signed fixed point value ( for example 1.5 or -1.75 etc...).
    > >
    > >
    > > Best regards
    > > Mariusz


    Wallge,
    Thank you for your answer. It is look intersting. Unfortunately, I can
    download that document. Could you send me that document on email?

    Mariusz
     
    MariuszK, Oct 23, 2006
    #10
  11. MariuszK

    wallge Guest

    i dont think i can give you the paper (copyright etc), and if you're a
    student you should be able to access the paper through your school
    library, or if it's for your work, have your boss buy it for you.
    I'll tell you how to do the calculation though. It will help if you do
    it in C or matlab first.

    you want to do 2^(x.y) first find
    2^x and 2^(x+1) through bit shifting 1 to the left by x and (x + 1)
    places

    now you do linear interpolation by drawing a line through these two
    points
    2^x, 2^(x+1), then use f = m*x+b.
    the slope, m = rise over run = (2(x+1) - 2^x)/((x+1) - (x)) = 2^(x) *
    (2 - 1) / 1 = 2^(x)
    b = 2^x, so to linearly interpolate do
    f = x^(x) * (y) + 2^x
    where y is the fractional part.
    if you plot this in matlab it will look like 2^x with lines drawn
    between the parts of the curve
    where x is integer valued.



    MariuszK wrote:
    > wallge napisal(a):
    > > Read this paper:
    > > A Nonlinear Technique for Enhancement of Color Images:
    > > An Architectural Perspective for Real-Time Applications
    > > you can find it in IEEE xplore.
    > >
    > > They implement a linear interpolation of log2 and log2_inv.
    > > where if you want to find log2_inv(x.y),
    > > you just find 2^x and 2^(x + 1) through bit shifting and then linearly
    > > interpolate
    > > between the two using y
    > >
    > >
    > >
    > > MariuszK wrote:
    > > > Hello,
    > > >
    > > > How can I calculate (optimal) 2 power of x ( 2^x)?
    > > >
    > > > Where x is signed fixed point value ( for example 1.5 or -1.75 etc...).
    > > >
    > > >
    > > > Best regards
    > > > Mariusz

    >
    > Wallge,
    > Thank you for your answer. It is look intersting. Unfortunately, I can
    > download that document. Could you send me that document on email?
    >
    > Mariusz
     
    wallge, Oct 24, 2006
    #11
  12. MariuszK

    wallge Guest

    that last bit was a typo, should say
    f = 2^(x) * (y) + 2^x = 2^x * (y + 1)
    where x is integer part, y is fractional part

    wallge wrote:
    > i dont think i can give you the paper (copyright etc), and if you're a
    > student you should be able to access the paper through your school
    > library, or if it's for your work, have your boss buy it for you.
    > I'll tell you how to do the calculation though. It will help if you do
    > it in C or matlab first.
    >
    > you want to do 2^(x.y) first find
    > 2^x and 2^(x+1) through bit shifting 1 to the left by x and (x + 1)
    > places
    >
    > now you do linear interpolation by drawing a line through these two
    > points
    > 2^x, 2^(x+1), then use f = m*x+b.
    > the slope, m = rise over run = (2(x+1) - 2^x)/((x+1) - (x)) = 2^(x) *
    > (2 - 1) / 1 = 2^(x)
    > b = 2^x, so to linearly interpolate do
    > f = x^(x) * (y) + 2^x
    > where y is the fractional part.
    > if you plot this in matlab it will look like 2^x with lines drawn
    > between the parts of the curve
    > where x is integer valued.
    >
    >
    >
    > MariuszK wrote:
    > > wallge napisal(a):
    > > > Read this paper:
    > > > A Nonlinear Technique for Enhancement of Color Images:
    > > > An Architectural Perspective for Real-Time Applications
    > > > you can find it in IEEE xplore.
    > > >
    > > > They implement a linear interpolation of log2 and log2_inv.
    > > > where if you want to find log2_inv(x.y),
    > > > you just find 2^x and 2^(x + 1) through bit shifting and then linearly
    > > > interpolate
    > > > between the two using y
    > > >
    > > >
    > > >
    > > > MariuszK wrote:
    > > > > Hello,
    > > > >
    > > > > How can I calculate (optimal) 2 power of x ( 2^x)?
    > > > >
    > > > > Where x is signed fixed point value ( for example 1.5 or -1.75 etc...).
    > > > >
    > > > >
    > > > > Best regards
    > > > > Mariusz

    > >
    > > Wallge,
    > > Thank you for your answer. It is look intersting. Unfortunately, I can
    > > download that document. Could you send me that document on email?
    > >
    > > Mariusz
     
    wallge, Oct 24, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,824
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,552
  3. johnp
    Replies:
    4
    Views:
    3,719
    Toby Inkster
    May 23, 2005
  4. Replies:
    4
    Views:
    1,334
    Default User
    Feb 22, 2006
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,412
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page