You are not logged in.
Lost Password?


Register To Post



 Bottom   Previous Topic   Next Topic

#1
int vs float
Posted on: 2008/9/24 2:17
Nintendoid!
Joined 2006/3/15
Ecuador
234 Posts
PVBCC 3rdCoderLong Time User (13 Years)
Hi guys, I'm struggling to get those extra FPS to make my demo run smoothly on hardware, so after doing a lot of tricks and algorithm optimizations I'm running out of ideas, and since I don't think I'm able to write better asm code than that produced by the compiler any time soon, I was thinking about changing my whole arithmetic from float to int, using a scale to achieve the precision I need:

1.67f = 167

and going to float as much late in the execution as possible.

The question is: will this make any significant difference to make worth the effort? I really don't know how much overhead it is to work with floats rather than with ints in the V810 so I can't make educated guess on this matter.

thanks for your responses.

jorgeche
Edited by jorgeche on 2008/9/24 3:54
Top

#2
Re: int vs float
Posted on: 2008/9/24 5:45
PVB Elite
Joined 2003/7/26
USA
1465 Posts
PVBCC EntryCoderContributorSpecial AchievementTop10 PosterHOTY09 EntryLong Time User (15 Years) App Coder20+ Game RatingsPVBCC 2013 Entry
I would definitely wait for DogP to weigh in, but here's what I have:

This file from David "Reality Boy" Tucker's site includes some fixed point math routines using the VB's native 13.3 and 7.9 fixed point formats (used for affine, as you already know).
Top

#3
Re: int vs float
Posted on: 2008/9/24 6:10
Nintendoid!
Joined 2006/3/15
Ecuador
234 Posts
PVBCC 3rdCoderLong Time User (13 Years)
Yup, I knew about that, but going back and forth fixed format to floats involves multiplications and divisions which I don't want, and doing fixed multiplication is really costly:

FIX7_9_MULT(a,b) (fix7_9) ((((s32)(a))*((s32)(b)))>>9)

if you compare it with a multiplication between ints:

a * b; // here you dont have the shift

so I would just scale everything by 100 (seems enough) and just do int operations, and when I really need the float, for setting the scale in affine mode for example, only at that point I divide my int by the scale, 98 / 100.0f... I think it is faster (but more cumbersome to program) than using fixed point formats.

jorgeche
Top

#4
Re: int vs float
Posted on: 2008/9/24 6:23
PVB Elite
Joined 2003/7/26
USA
1465 Posts
PVBCC EntryCoderContributorSpecial AchievementTop10 PosterHOTY09 EntryLong Time User (15 Years) App Coder20+ Game RatingsPVBCC 2013 Entry
Isn't that for multiplying two ints to get a fix7_9? If you used two fix7_9's it seems like it would keep dividing a number by 512 each time you used it...

Multiplying two fixed point nums is no different than what you describe; i.e. using the inbuilt * operator.
Top

#5
Re: int vs float
Posted on: 2008/9/24 6:31
PVB Elite
Joined 2003/7/25
USA
1507 Posts
PVBCC 1stCoderContributor#3 PosterHOTY09 EntryLong Time User (15 Years) App CoderPVBCC 2010 EntryPVBCC 2013 Entry
DEFINITELY use fixed point for as much stuff as possible... floating point is horribly slow. My first Mario Kart engine was floating point (so I could get a working prototype which I could then convert small pieces at a time to fixed point), and it barely ran (just a few FPS). In fixed point, it runs full speed with no problem. I've also done some raycaster stuff with floating point and it really is slow.

I don't know what you're using floating point stuff for, but I'd assume you can get the precision you need from a 32 bit fixed point number... just use a bunch of bit shifts and keep track of where the decimal is. Oh... and for Affine... you really don't need to use floating point. The Affine hardware is 16 bit fixed point, so using a 32 bit fixed point number for the calculations leading up to it is more than enough.

On a side note... I've never looked at the gcc output from floating point code... it's possible that it's emulating the floating point in fixed point hardware rather than using the floating point instructions, which could explain why it's so slow (this is how it's done on fixed point processors). The floating point instructions should take nearly the same amount of time as the fixed point mul/div instructions (8 to 30 clocks compared to 13 for multiply, 44 clocks compared to 38 for divide) although the add and subtract take much longer (9 to 28 clocks compared to 1 for add and 12 to 28 compared to 1 for sub). But there shouldn't be THAT many instructions that a those few clock cycles pile up... but the software emulation is always REALLY slow. And conversion from floating to fixed and back using the dedicated CPU instruction is also relatively quick (5 to 16 and 9 to 14 cycles).

DogP
Top

#6
Re: int vs float
Posted on: 2008/9/24 6:36
Nintendoid!
Joined 2006/3/15
Ecuador
234 Posts
PVBCC 3rdCoderLong Time User (13 Years)
Ok guys thanks a lot for your replies, I will begin changing as much of my maths as I can to fixed point math. I will try to have it ready for the competition deadline... Hope to see your programs soon!.

jorgeche
Top

#7
Re: int vs float
Posted on: 2008/9/24 11:19
Nintendoid!
Joined 2007/8/8
Great Britain
201 Posts
CoderContributorHOTY09 EntryLong Time User (12 Years) App Coder
Edit: See section 3.3, General notes on GBA programming. It's nearly all applicable to the VB.
Edited by dasi on 2008/9/24 11:41
Edited by dasi on 2008/10/1 17:37
Edited by dasi on 2008/10/5 12:04
Top

#8
Re: int vs float
Posted on: 2008/9/24 19:10
Nintendoid!
Joined 2006/3/15
Ecuador
234 Posts
PVBCC 3rdCoderLong Time User (13 Years)
Another question, what happens with the number sign when going back and forth fixed point format from an int? with floats there is no problem since it always divides or multiplies, but since the conversion to int is done with shifts.... must I take car of sign?


About using everywhere int instead of u8, etc... I need to save as much memory as possible, because of it, I can't waste space for example for a flag an use an int to hold it when I can use a bit field in a u8, I will not make that concession, so the question is: is it worth to operate over those values (u8, u16, etc) as ints at the expense of making the code a lot less readable and making it less type safe?

jorgeche
Top

#9
Re: int vs float
Posted on: 2008/9/25 2:35
PVB Elite
Joined 2003/7/25
USA
1507 Posts
PVBCC 1stCoderContributor#3 PosterHOTY09 EntryLong Time User (15 Years) App CoderPVBCC 2010 EntryPVBCC 2013 Entry
I'm not sure if I understand your questions... fixed point is the same as integers... you're just interpreting where the point is. If you do shift a signed number to the right (division by 2), you need to make sure you sign extend (or arithmetic shift), but there's no way to do that with an instruction in C, except division. Most CPUs have an instruction SAR (shift arithmetic right), which the optimizer may take advantage of if the divisor is a power of 2. But if you're shifting right to interpret as a 16 bit number (like in the FIX7_9_MULT you posted above), you don't need to worry about it, because the result from the multiply will be sign extended to 32 bits, and shifting right 9 won't lose the sign bit.

About the bit fields, what is the problem? I would use ints when possible unless you're using several bytes in a row since integers must be word aligned (and shorts must be half-word aligned), so you're wasting the space between a byte and an int if you put them consecutively.

DogP
Top

#10
Re: int vs float
Posted on: 2008/9/25 3:53
Nintendoid!
Joined 2006/3/15
Ecuador
234 Posts
PVBCC 3rdCoderLong Time User (13 Years)
Ok, I understand, I was just worried because of the bit sign, but as you say, it is clear as using integers.

About the int vs byte, I must revisit the memory alignment stuff, I will check where I can group bytes and will use just int were not.

Thanks again for your help DogP.

BTW I'm half way changing everything from float to fixed point, it would be great to see the FPS boosted by the change... fingers crossed.

jorgeche
Top

 Top   Previous Topic   Next Topic


Register To Post