Mathematical formula implementation

Discussion in 'VHDL' started by vhdl_newbie, Mar 14, 2013.

  1. vhdl_newbie

    vhdl_newbie Guest

    Hello,
    I am a beginner with learn VHDL and I need to implement a mathematical formula for a job interview.
    I have one day to solve this problem so if there is any good samaritan that could help me with this problem, I would really appreciate it.

    solve this formula for Y

    Y=(P^2+K*P)/16

    with

    P = [0:63]
    K = [0:4]
    Y = [0:512]

    Have a nice day !
     
    vhdl_newbie, Mar 14, 2013
    #1
    1. Advertising

  2. vhdl_newbie

    vhdl_newbie Guest

    *I am a beginner with VHDL (sorry)
     
    vhdl_newbie, Mar 14, 2013
    #2
    1. Advertising

  3. vhdl_newbie

    Fredxx Guest

    On 14/03/2013 22:23, vhdl_newbie wrote:
    > Hello, I am a beginner with learn VHDL and I need to implement a
    > mathematical formula for a job interview. I have one day to solve
    > this problem so if there is any good samaritan that could help me
    > with this problem, I would really appreciate it.
    >
    > solve this formula for Y
    >
    > Y=(P^2+K*P)/16
    >
    > with
    >
    > P = [0:63] K = [0:4] Y = [0:512]
    >
    > Have a nice day !
    >


    That looks like 4 variables and a single equation linking them together.

    Have a great interview.
     
    Fredxx, Mar 14, 2013
    #3
  4. vhdl_newbie

    vhdl_newbie Guest

    Le jeudi 14 mars 2013 19:51:46 UTC-4, Fredxx a écrit :
    > On 14/03/2013 22:23, vhdl_newbie wrote:
    >
    > > Hello, I am a beginner with learn VHDL and I need to implement a

    >
    > > mathematical formula for a job interview. I have one day to solve

    >
    > > this problem so if there is any good samaritan that could help me

    >
    > > with this problem, I would really appreciate it.

    >
    > >

    >
    > > solve this formula for Y

    >
    > >

    >
    > > Y=(P^2+K*P)/16

    >
    > >

    >
    > > with

    >
    > >

    >
    > > P = [0:63] K = [0:4] Y = [0:512]

    >
    > >

    >
    > > Have a nice day !

    >
    > >

    >
    >
    >
    > That looks like 4 variables and a single equation linking them together.
    >
    >
    >
    > Have a great interview.


    Thank you for your interest, the problem may be unclear but that is the wayit was written.
    I think they want me to design an architecture with 9 bits output (Y), 2 bits (K) input and 5 bits input (P)
    Do you have any idea of the functions I could use for product, division andeponentiation ?
    (I am new to VHDL)
     
    vhdl_newbie, Mar 15, 2013
    #4
  5. vhdl_newbie

    rickman Guest

    On 3/14/2013 6:23 PM, vhdl_newbie wrote:
    > Hello,
    > I am a beginner with learn VHDL and I need to implement a mathematical formula for a job interview.
    > I have one day to solve this problem so if there is any good samaritan that could help me with this problem, I would really appreciate it.
    >
    > solve this formula for Y
    >
    > Y=(P^2+K*P)/16
    >
    > with
    >
    > P = [0:63]
    > K = [0:4]
    > Y = [0:512]
    >
    > Have a nice day !


    If this were a real requirement, I would say there was something wrong
    with it. It could be coded, but it would be hard to actually build. If
    P is a 64 bit integer, it has a range of 0 to 2^64-1. As an exponent of
    K, the result would have 2^64+4 bits. I don't know how to represent
    that in a way that VHDL would understand. Heck, my calculator threw up
    when I tried to calculate it.

    Are you sure this is the right question? I can't see any value in
    asking a job candidate such a question.

    WAIT! The notation is wonkey! You mean the variables have his range,
    not that many bits, right? That's different. But it still doesn't fit.
    You can't shove the result into the range for Y.

    Maybe that's what they want you to tell them?

    --

    Rick
     
    rickman, Mar 15, 2013
    #5
  6. vhdl_newbie wrote:

    > Hello,
    > I am a beginner with learn VHDL and I need to implement a mathematical
    > formula for a job interview. I have one day to solve this problem so if
    > there is any good samaritan that could help me with this problem, I would
    > really appreciate it.
    >
    > solve this formula for Y
    >
    > Y=(P^2+K*P)/16
    >
    > with
    >
    > P = [0:63]
    > K = [0:4]
    > Y = [0:512]


    Assuming [0:x] are ranges,

    FUNCTION y
    (
    p: natural range 0 TO 63;
    k: natural range 0 TO 4
    ) RETURN natural IS
    VARIABLE result: natural;
    BEGIN
    result := (p**2 + k*p)/16;
    IF result > 512 THEN
    result := 512;
    END IF;
    RETURN result;
    END FUNCTION y;

    Is this synthesizable? No idea, probably not, but it was not a requirement.

    > Have a nice day !


    And you!

    --
    Paul Uiterlinden
     
    Paul Uiterlinden, Mar 15, 2013
    #6
  7. vhdl_newbie

    Fredxx Guest

    On 15/03/2013 13:11, Paul Uiterlinden wrote:
    > vhdl_newbie wrote:
    >
    >> Hello,
    >> I am a beginner with learn VHDL and I need to implement a mathematical
    >> formula for a job interview. I have one day to solve this problem so if
    >> there is any good samaritan that could help me with this problem, I would
    >> really appreciate it.
    >>
    >> solve this formula for Y
    >>
    >> Y=(P^2+K*P)/16
    >>
    >> with
    >>
    >> P = [0:63]
    >> K = [0:4]
    >> Y = [0:512]

    >
    > Assuming [0:x] are ranges,
    >
    > FUNCTION y
    > (
    > p: natural range 0 TO 63;
    > k: natural range 0 TO 4
    > ) RETURN natural IS
    > VARIABLE result: natural;
    > BEGIN
    > result := (p**2 + k*p)/16;
    > IF result > 512 THEN
    > result := 512;
    > END IF;
    > RETURN result;
    > END FUNCTION y;
    >
    > Is this synthesizable? No idea, probably not, but it was not a requirement.
    >
    >> Have a nice day !

    >
    > And you!
    >


    I would have treated P, K and Y as 64, 5 and 513 element arrays
    respectively?
     
    Fredxx, Mar 15, 2013
    #7
  8. vhdl_newbie

    Rob Gaddi Guest

    On Thu, 14 Mar 2013 22:13:53 -0400
    rickman <> wrote:

    > On 3/14/2013 6:23 PM, vhdl_newbie wrote:
    > > Hello,
    > > I am a beginner with learn VHDL and I need to implement a mathematical formula for a job interview.
    > > I have one day to solve this problem so if there is any good samaritan that could help me with this problem, I would really appreciate it.
    > >
    > > solve this formula for Y
    > >
    > > Y=(P^2+K*P)/16
    > >
    > > with
    > >
    > > P = [0:63]
    > > K = [0:4]
    > > Y = [0:512]
    > >
    > > Have a nice day !

    >
    > If this were a real requirement, I would say there was something wrong
    > with it. It could be coded, but it would be hard to actually build. If
    > P is a 64 bit integer, it has a range of 0 to 2^64-1. As an exponent of
    > K, the result would have 2^64+4 bits. I don't know how to represent
    > that in a way that VHDL would understand. Heck, my calculator threw up
    > when I tried to calculate it.
    >
    > Are you sure this is the right question? I can't see any value in
    > asking a job candidate such a question.
    >
    > WAIT! The notation is wonkey! You mean the variables have his range,
    > not that many bits, right? That's different. But it still doesn't fit.
    > You can't shove the result into the range for Y.
    >
    > Maybe that's what they want you to tell them?
    >


    I think you're misreading. That's (P^P+K*P)/16, or P(P+K)/16

    > --
    >
    > Rick



    --
    Rob Gaddi, Highland Technology -- www.highlandtechnology.com
    Email address domain is currently out of order. See above to fix.
     
    Rob Gaddi, Mar 15, 2013
    #8
  9. vhdl_newbie

    rickman Guest

    On 3/15/2013 6:25 PM, Rob Gaddi wrote:
    > On Thu, 14 Mar 2013 22:13:53 -0400
    > rickman<> wrote:
    >
    >> On 3/14/2013 6:23 PM, vhdl_newbie wrote:
    >>> Hello,
    >>> I am a beginner with learn VHDL and I need to implement a mathematical formula for a job interview.
    >>> I have one day to solve this problem so if there is any good samaritan that could help me with this problem, I would really appreciate it.
    >>>
    >>> solve this formula for Y
    >>>
    >>> Y=(P^2+K*P)/16
    >>>
    >>> with
    >>>
    >>> P = [0:63]
    >>> K = [0:4]
    >>> Y = [0:512]
    >>>
    >>> Have a nice day !

    >>
    >> If this were a real requirement, I would say there was something wrong
    >> with it. It could be coded, but it would be hard to actually build. If
    >> P is a 64 bit integer, it has a range of 0 to 2^64-1. As an exponent of
    >> K, the result would have 2^64+4 bits. I don't know how to represent
    >> that in a way that VHDL would understand. Heck, my calculator threw up
    >> when I tried to calculate it.
    >>
    >> Are you sure this is the right question? I can't see any value in
    >> asking a job candidate such a question.
    >>
    >> WAIT! The notation is wonkey! You mean the variables have his range,
    >> not that many bits, right? That's different. But it still doesn't fit.
    >> You can't shove the result into the range for Y.
    >>
    >> Maybe that's what they want you to tell them?
    >>

    >
    > I think you're misreading. That's (P^P+K*P)/16, or P(P+K)/16


    Yes, you are right. I misread the problem. Wouldn't be the first time
    and won't be the last...

    But the problem remains. If those are ranges, the general result won't
    fit in the variable Y.

    --

    Rick
     
    rickman, Mar 15, 2013
    #9
  10. vhdl_newbie

    chrisabele Guest

    On 3/15/2013 6:29 PM, rickman wrote:
    > On 3/15/2013 6:25 PM, Rob Gaddi wrote:
    >> On Thu, 14 Mar 2013 22:13:53 -0400
    >> rickman<> wrote:
    >>
    >>> On 3/14/2013 6:23 PM, vhdl_newbie wrote:
    >>>> Hello,
    >>>> I am a beginner with learn VHDL and I need to implement a
    >>>> mathematical formula for a job interview.
    >>>> I have one day to solve this problem so if there is any good
    >>>> samaritan that could help me with this problem, I would really
    >>>> appreciate it.
    >>>>
    >>>> solve this formula for Y
    >>>>
    >>>> Y=(P^2+K*P)/16
    >>>>
    >>>> with
    >>>>
    >>>> P = [0:63]
    >>>> K = [0:4]
    >>>> Y = [0:512]
    >>>>
    >>>> Have a nice day !
    >>>
    >>> If this were a real requirement, I would say there was something wrong
    >>> with it. It could be coded, but it would be hard to actually build. If
    >>> P is a 64 bit integer, it has a range of 0 to 2^64-1. As an exponent of
    >>> K, the result would have 2^64+4 bits. I don't know how to represent
    >>> that in a way that VHDL would understand. Heck, my calculator threw up
    >>> when I tried to calculate it.
    >>>
    >>> Are you sure this is the right question? I can't see any value in
    >>> asking a job candidate such a question.
    >>>
    >>> WAIT! The notation is wonkey! You mean the variables have his range,
    >>> not that many bits, right? That's different. But it still doesn't fit.
    >>> You can't shove the result into the range for Y.
    >>>
    >>> Maybe that's what they want you to tell them?
    >>>

    >>
    >> I think you're misreading. That's (P^P+K*P)/16, or P(P+K)/16

    >
    > Yes, you are right. I misread the problem. Wouldn't be the first time
    > and won't be the last...
    >
    > But the problem remains. If those are ranges, the general result won't
    > fit in the variable Y.
    >


    I think Rob's rewriting is really helpful (aside from his forgetting to
    change the ^ to a *). And if we assume that the values in [] are value
    ranges rather than bit index ranges then the maximum Y value would be
    63*(63+4)/16 = 264 which certainly is within the range [0:512]. Of
    course I've assumed that Y must be an integer and truncated (actually
    rounded) the result, plus the intermediate result is certainly much
    larger than 512. But given those assumptions the problem is not
    unreasonable. So perhaps the code that Paul posted is the best answer.

    Perhaps the point of the interview question was to see if the candidate
    would raise exactly the issues that have been discussed this thread...

    Chris
     
    chrisabele, Mar 16, 2013
    #10
  11. vhdl_newbie

    rickman Guest

    On 3/15/2013 10:07 PM, chrisabele wrote:
    > On 3/15/2013 6:29 PM, rickman wrote:
    >> On 3/15/2013 6:25 PM, Rob Gaddi wrote:
    >>> On Thu, 14 Mar 2013 22:13:53 -0400
    >>> rickman<> wrote:
    >>>
    >>>> On 3/14/2013 6:23 PM, vhdl_newbie wrote:
    >>>>> Hello,
    >>>>> I am a beginner with learn VHDL and I need to implement a
    >>>>> mathematical formula for a job interview.
    >>>>> I have one day to solve this problem so if there is any good
    >>>>> samaritan that could help me with this problem, I would really
    >>>>> appreciate it.
    >>>>>
    >>>>> solve this formula for Y
    >>>>>
    >>>>> Y=(P^2+K*P)/16
    >>>>>
    >>>>> with
    >>>>>
    >>>>> P = [0:63]
    >>>>> K = [0:4]
    >>>>> Y = [0:512]
    >>>>>
    >>>>> Have a nice day !
    >>>>
    >>>> If this were a real requirement, I would say there was something wrong
    >>>> with it. It could be coded, but it would be hard to actually build. If
    >>>> P is a 64 bit integer, it has a range of 0 to 2^64-1. As an exponent of
    >>>> K, the result would have 2^64+4 bits. I don't know how to represent
    >>>> that in a way that VHDL would understand. Heck, my calculator threw up
    >>>> when I tried to calculate it.
    >>>>
    >>>> Are you sure this is the right question? I can't see any value in
    >>>> asking a job candidate such a question.
    >>>>
    >>>> WAIT! The notation is wonkey! You mean the variables have his range,
    >>>> not that many bits, right? That's different. But it still doesn't fit.
    >>>> You can't shove the result into the range for Y.
    >>>>
    >>>> Maybe that's what they want you to tell them?
    >>>>
    >>>
    >>> I think you're misreading. That's (P^P+K*P)/16, or P(P+K)/16

    >>
    >> Yes, you are right. I misread the problem. Wouldn't be the first time
    >> and won't be the last...
    >>
    >> But the problem remains. If those are ranges, the general result won't
    >> fit in the variable Y.
    >>

    >
    > I think Rob's rewriting is really helpful (aside from his forgetting to
    > change the ^ to a *). And if we assume that the values in [] are value
    > ranges rather than bit index ranges then the maximum Y value would be
    > 63*(63+4)/16 = 264 which certainly is within the range [0:512]. Of
    > course I've assumed that Y must be an integer and truncated (actually
    > rounded) the result, plus the intermediate result is certainly much
    > larger than 512. But given those assumptions the problem is not
    > unreasonable. So perhaps the code that Paul posted is the best answer.
    >
    > Perhaps the point of the interview question was to see if the candidate
    > would raise exactly the issues that have been discussed this thread...


    Yes, I forgot to divide by 16, I was only looking at the multiplications.

    I still think it is a bit of an odd question for a job interview, but I
    suppose it must relate to something the company does, or maybe the
    interviewer just picked this as an example question without any real
    reason.

    I had an interview once where the company president asked me about how I
    had solved a very tough problem before. Immediately a problem came to
    mind that I had done a couple of years before and had stumped me for a
    few days before coming up with a solution. Unfortunately I couldn't
    recall the solution! So much for thinking on your feet.

    Needless to say, I didn't get the job...

    --

    Rick
     
    rickman, Mar 16, 2013
    #11
  12. vhdl_newbie

    rickman Guest

    On 3/14/2013 6:23 PM, vhdl_newbie wrote:
    > Hello,
    > I am a beginner with learn VHDL and I need to implement a mathematical formula for a job interview.
    > I have one day to solve this problem so if there is any good samaritan that could help me with this problem, I would really appreciate it.
    >
    > solve this formula for Y
    >
    > Y=(P^2+K*P)/16
    >
    > with
    >
    > P = [0:63]
    > K = [0:4]
    > Y = [0:512]
    >
    > Have a nice day !


    I've been thinking a bit about this and I would not advise that you go
    into the interview trying to give the impression that you "know" VHDL.
    You are clearly a beginner and that will show in the interview. What
    you should consider is trying to learn a bit about VHDL and let them
    know that you are coming up the learning curve.

    One of the big difficulties beginners have with VHDL is the strong
    typing. Learn how to convert between the different types, when you need
    to do what type of conversion and why. If you can explain all this in
    an interview I think it will impress anyone. If I were interviewing
    someone as a VHDL designer and they could articulate a good approach to
    type conversions I know I would be favorably impressed. I expect a lot
    of people here can't do that.

    --

    Rick
     
    rickman, Mar 16, 2013
    #12
  13. vhdl_newbie

    vhdl_newbie Guest

    Hello,
    Thanks a lot for all of you answer, it was pretty helpful for me. So, for those asking, I already did the interview. It was only afterwards that they asked me to solve a VHDL problem. Also, I think the [] was meant to be the range for the integers. So I came up with this, from your what I had the time to learn in one day. Don't bother correcting it, I had to send it quickly so it is too late to change it now. I send it in case it could interest someone.
    Thanks again for the help !


    library IEEE;
    use IEEE.Std_Logic_1164.all;
    use IEEE.Numeric_Std.all;

    entity formule is
    port (
    K : in Unsigned(2 downto 0); -- 3 bits for [0:4]
    P : in Unsigned(5 downto 0); -- 6 bits for [0:64]
    Y : out Unsigned(9 downto 0); -- 10 bits for [0:512]
    );
    end formule;

    architecture arch of formule is

    -- refactorizing : Y=P*(P+K)/16

    Signal sum : Unsigned(6 downto 0); -- 7 bits for (P+K)
    Signal numerator : Unsigned(12 downto 0); -- 13 bits for P*(P+K)

    begin

    sum <= P + K;
    numerator <= P * sum;
    Y <= numerator srl 4; -- I shift the numbers 4 bits to the right to divide by 16 (someone told me "/" doesnt work all the time)

    end arch;
     
    vhdl_newbie, Mar 18, 2013
    #13
  14. vhdl_newbie

    rickman Guest

    On 3/17/2013 10:47 PM, vhdl_newbie wrote:
    >
    > Hello,
    > Thanks a lot for all of you answer, it was pretty helpful for me. So, for those asking, I already did the interview. It was only afterwards that they asked me to solve a VHDL problem. Also, I think the [] was meant to be the range for the integers. So I came up with this, from your what I had the time to learn in one day. Don't bother correcting it, I had to send it quickly so it is too late to change it now. I send it in case it could interest someone.
    > Thanks again for the help !
    >
    >
    > library IEEE;
    > use IEEE.Std_Logic_1164.all;
    > use IEEE.Numeric_Std.all;
    >
    > entity formule is
    > port (
    > K : in Unsigned(2 downto 0); -- 3 bits for [0:4]
    > P : in Unsigned(5 downto 0); -- 6 bits for [0:64]
    > Y : out Unsigned(9 downto 0); -- 10 bits for [0:512]
    > );
    > end formule;
    >
    > architecture arch of formule is
    >
    > -- refactorizing : Y=P*(P+K)/16
    >
    > Signal sum : Unsigned(6 downto 0); -- 7 bits for (P+K)
    > Signal numerator : Unsigned(12 downto 0); -- 13 bits for P*(P+K)
    >
    > begin
    >
    > sum<= P + K;
    > numerator<= P * sum;
    > Y<= numerator srl 4; -- I shift the numbers 4 bits to the right to divide by 16 (someone told me "/" doesnt work all the time)
    >
    > end arch;


    I can't resist. This is pretty good, but you didn't allow enough range
    for P. It is 0 to 64, not 0 to 63. So you need 7 bits. sum still only
    needs 7 bits because the sum won't exceed this range given the input
    constraints.

    Otherwise this is good for a beginner.

    --

    Rick
     
    rickman, Mar 19, 2013
    #14
  15. vhdl_newbie

    Andy Guest

    Me neither (can't resist).

    Given the non-power-of two ranges involved, I would keep this in the integer domain:

    K: in natural range 0 to 4;
    P: in natural range 0 to 64;
    Y: out natural range 0 to 64 * (64 + 4) / 16; -- 9 bits is enough
    ....

    Y <= P * (P + K) / 16;

    Synthesis tools recognize a multiply/divide by a constant power of two as a shift operation anyway.

    Keep in mind, if you are trying to use built-in MAC blocks in an FPGA, this needs to be pipelined across multiple clock cycles. Otherwise a combinatorial implementation is going to be very slow and large.

    Andy
     
    Andy, Mar 19, 2013
    #15
  16. vhdl_newbie

    vhdl_newbie Guest

    Hello,
    Thanks for the advices, Andy and Rick, i'll try the keep them in mind.
    Again, thanks a lot for your help, you are a great community !
     
    vhdl_newbie, Mar 22, 2013
    #16
    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. MtnSurf8

    Mathematical Operations in VHDL

    MtnSurf8, Apr 25, 2004, in forum: VHDL
    Replies:
    1
    Views:
    5,858
    Egbert Molenkamp
    Apr 25, 2004
  2. Replies:
    0
    Views:
    385
  3. kris Palmby

    mathematical String Evaluation

    kris Palmby, Jan 24, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    576
    Scott Allen
    Jan 24, 2006
  4. Replies:
    16
    Views:
    1,022
    Chris Uppal
    Apr 6, 2006
  5. Louis-Vincent Guay

    Mathematical formula used in dynamically rendered DataGrid

    Louis-Vincent Guay, May 21, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    203
    Louis-Vincent Guay
    May 25, 2004
Loading...

Share This Page