Code optimization of inner loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eli Horn
    New Member
    • Sep 2006
    • 2

    Code optimization of inner loop

    Dear all,

    I would appriciate any input you may have on how to optimize this line which appears in my most inner loop:

    *pG=(short)(((i nt)w1*V1+w2*V2+ w3*V3+w4*V4)/(w1+w2+w3+w4));

    w1,w2,w3,w4,V1, V2,V3,V4 are all short and the result is also short.
    If it makes any difference, all variables are positive and smaller than 4095.

    Thanks for any input,
    Eli.
  • pukur123
    New Member
    • Sep 2006
    • 61

    #2
    If all the variables are initialised with the value = 4094, the value of

    (int)w1*V1+w2*V 2+w3*V3+w4*V4)

    will exceed the maximum number that an integer can store and so the result will be a negative one.

    So you can change the typecasting to float. Now the statement will become

    *pG=(short)(((f loat)w1*V1+w2*V 2+w3*V3+w4*V4)/(w1+w2+w3+w4));

    Comment

    • Eli Horn
      New Member
      • Sep 2006
      • 2

      #3
      This is not a problem since this case cannot happen.
      Let me not bother you with the details but just say that I don't have range problems and my only problem is how to speed up this code.

      Comment

      Working...