[vorbis-dev] optimizing float to int conversions

illiminable ogg at illiminable.com
Wed May 5 09:59:12 PDT 2004



I gave this a test compiling with VS2002 (VC7.0)... optimised the build
settings as much as possible.

My test is below (using C++)... To alternate tests just comment out one test
or the other.

On my Athlon 2500 your macro runs in just over 50% of the time of teh
cast... ie just under 50% improvement with the most heavily optimised
compiler settings for each test.

I just made another chagne to your macro and got it up to a 55%
improvement... Probably a bit more tweaking can happen too. See where you
had the
if (d>31)
  MyInt = 0;

Assuming your ints are of size 32 bits (I'm pretty sure this is a safe
assumption in ogg), this is not necessary if you shift any 32 bit int by 32
or more bits in either direction you get zero anyway (is there some case i'm
not thinking of that this is not true ?)... so you can remove a a comparison
and jump there. All bits left of the 24th bit would be zero anyway.

But either way... it's a whole bunch faster than casting !

<p>Zen...

Test follows.

#define CONVERT_FLT2INT(MyFloat,MyInt) \
{ \
   int FltInt = *(int *)&MyFloat; \
\
   int mantissa = (FltInt & 0x07fffff) | 0x800000; \
   int exponent = ((FltInt >> 23) & 0xff) - 0x7f; \
   int d = 23-exponent;\
   if ((d) < 0) \
   {\
     MyInt = (mantissa << -(d)); \
  d = -d;\
   }\
   else \
     MyInt = (mantissa >> (d)); \
\
   if (FltInt & 0x80000000) \
     MyInt = -MyInt; \
}

<p>int _tmain(int argc, _TCHAR* argv[])
{
 const ASIZE = 500000000;
 float* fa = new float[ASIZE];
 int* ia = new int[ASIZE];

//Create some numbers
 for (int i = 0; i < ASIZE; i++) {
  int a = rand();
  fa[i] = (float)a;
  if (i % 2 == 1) fa[i] = -fa[i];
 }

   clock_t start, finish;

   /* Measure the duration of an event. */
   start = clock();

   //Test 1
   for (int i = 0; i < ASIZE; i++) {
    ia[i] = (float)fa[i];
   }

   //Test 2
   //for (int i = 0; i < ASIZE; i++) {
     //CONVERT_FLT2INT(fa[i], ia[i]);
   //}
   finish = clock();

   cout << "Duration of "<<ASIZE<<" conversions is "<<finish - start<<endl;

 return 0;
}
----- Original Message -----
From: "dean gaudet" <dean-list-vorbis-dev at arctic.org>
To: <vorbis-dev at xiph.org>
Sent: Thursday, May 06, 2004 12:29 AM
Subject: Re: [vorbis-dev] optimizing float to int conversions

<p>> On Wed, 5 May 2004 shabi at t2.technion.ac.il wrote:
>
> > Hi,
> > We compiled libvorbis with vc6 enviroment.
> > We've discovered that changing float to int conversion results in a 4%
speedup
> > on a benchmark we've come with on pentium4 3GHz.
>
> i'm curious where the real gains are coming from here -- is your compiler
> generating cmov instructions after your patches?  it looks like most
> of the potential for gains are in parallelising the code in and around
> where you've made these float to int conversions -- p4 is a lot happier
> parallizing cmov than it is fcmov.
>
> i gather vc6 is too old to generate sse code instead of x87 code?  because
> the places where you're changing things are vectorizable.
>
> also -- you might want to try out this change to psy.c as well:
>
> /* this is for per-channel noise normalization */
> static int apsort(const void *a, const void *b){
> #if 0
>   float f1=fabs(**(float**)a);
>   float f2=fabs(**(float**)b);
>   return (f1<f2)-(f1>f2);
> #else
>   uint32_t u1 = **(uint32_t **)a;
>   uint32_t u2 = **(uint32_t **)b;
>   u1 *= 2; // clear sign bit
>   u2 *= 2;
>   //return (u1<u2)-(u1>u2);
>   return (int32_t)u2 - (int32_t)u1;
> #endif
> }
>
> -dean
> --- >8 ----
> List archives:  http://www.xiph.org/archives/
> Ogg project homepage: http://www.xiph.org/ogg/
> To unsubscribe from this list, send a message to
'vorbis-dev-request at xiph.org'
> containing only the word 'unsubscribe' in the body.  No subject is needed.
> Unsubscribe messages sent to the list will be ignored/filtered.
>
>
>

<p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'vorbis-dev-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the Vorbis-dev mailing list