• HomeBoards
  • RulesRules
  • HelpHelp
  • WikiWiki
  • Donate

Author Topic: [QUESTION] Fast floating point to integer conversion?  (Read 3310 times)

the_gama (OP)

  • Posts: 155
[QUESTION] Fast floating point to integer conversion?
« on: October 28, 2013, 06:40:41 pm »
Hi, I'm trying to optimize some code that needs to convert from floating point to integer.  Currently it does it with a simple cast like this:

Code: [Select]
FORCEINLINE u32 u32floor(float f)
{
#ifdef ENABLE_SSE2
return (u32)_mm_cvtt_ss2si(_mm_set_ss(f));
#else
return (u32)f;
#endif
}
FORCEINLINE u32 u32floor(double d)
{
#ifdef ENABLE_SSE2
return (u32)_mm_cvttsd_si32(_mm_set_sd(d));
#else
return (u32)d;
#endif
}

//same as above but works for negative values too.
//be sure that the results are the same thing as floorf!
FORCEINLINE s32 s32floor(float f)
{
#ifdef ENABLE_SSE2
return _mm_cvtss_si32( _mm_add_ss(_mm_set_ss(-0.5f),_mm_add_ss(_mm_set_ss(f), _mm_set_ss(f))) ) >> 1;
#else
return (s32)floorf(f);
#endif
}

Testing the app in windows and enabling sse instructions, reduces execution time ~8 %. 

Is there a similar instruction (SIMD) that does fast floating point convertion for MIPS?  Or what is the faster way to do those conversions?

Thanks in advance.

pcercuei

  • Posts: 1708
    • My devblog
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #1 on: October 28, 2013, 06:51:39 pm »
Just cast. GCC will know what to do.

the_gama (OP)

  • Posts: 155
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #2 on: October 29, 2013, 04:00:36 am »
Oh, ok.  That function is called so many times in the program that I thought using a faster implementation would help to increase performance.


mth

  • Posts: 319
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #3 on: October 30, 2013, 01:59:24 pm »
I don't know what kind of computations you're using, but maybe using fixed point instead of floating point would be an option?

the_gama (OP)

  • Posts: 155
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #4 on: October 30, 2013, 03:38:30 pm »
I'm using vio2sf sources to write a 2sf plugin for oldplay, and vio2sf is based on desmume.  The SPU code uses lot's of double and floating point operations, I don't know how difficult would it be to convert them to fixed point operations.

The most demanding operation are on the MMU code though.


mth

  • Posts: 319
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #5 on: November 05, 2013, 04:09:38 am »
In general fixed point is fine for sound synthesis, but it could be quite some effort to adapt all the code when you're not writing it from scratch. If it's C++, you could use operator overloading to get the same syntax as using floating point, but you'd still have to check that you allocate enough bits so you don't get any overflows and not too many underflows.

Another thing you could do is check how much precision you really need and replace doubles by floats where you don't need maximum precision. This won't make as much of a difference as switching to fixed point, but it might be enough.

the_gama (OP)

  • Posts: 155
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #6 on: November 06, 2013, 04:01:03 am »
Thanks for the explanation mth.  Apparently someone already did the job, I found some repo with viogsf sources, where the SPU code use fixed point operations.

And as expected, it improves performance.  But not that much, I guess because the zero does floating point operations by hardware, there is not much difference by using fixed point operations.

mth

  • Posts: 319
Re: [QUESTION] Fast floating point to integer conversion?
« Reply #7 on: November 06, 2013, 04:51:08 am »
Yes, the speed difference between fixed point and software floating point is dramatic, the difference between fixed point and hardware floating point is much smaller.

 

Post a new topic