Is Fixed point still better

Is Fixed point still better

Post by stev » Thu, 24 Sep 1998 04:00:00



 I have a coule of books on 3d programming, most of which say that
fixed point math is a good way to optimise any program (although none
of them actually tell me how), But I heard that on a pentium floating
point math is much faster, and the books are quite old. So, is it
worth going to all the trouble of learning how to use fixed point
math, or is it now redundant.

-Steve

 
 
 

Is Fixed point still better

Post by Q » Fri, 25 Sep 1998 04:00:00



> I have a coule of books on 3d programming, most of which say that
>fixed point math is a good way to optimise any program (although none
>of them actually tell me how), But I heard that on a pentium floating
>point math is much faster, and the books are quite old. So, is it
>worth going to all the trouble of learning how to use fixed point
>math, or is it now redundant.

Nope, fixed-point math is still VERY fast, and if you're going to create
somewhat fast 3D-engine you should intensively use it.
Of course, floating-point math is worth of attention for non-time critical
moments or when you use pairing for performing slow floating point division
while performing integer operations, so you get division for free.

A floating-point number is simply a 32-bit integer, most often in 16.16
format
for common computations (mean last 16 bits is for fractional part of number)

the C macros to convert from / to fixedpoint are:

------Fixed.h--------------
typedef long    int32;
typedef long    Fixedpoint;

#define INT_TO_FIXED(x)        ((int32)x << 16)
#define FIXED_TO_INT(x)        ((int32)x >> 16)
#define DOUBLE_TO_FIXED(x)     ((int32)(x*65536.0+0.5))
#define FIXED_TO_DOUBLE(x)     ((double)x/65536.0)
--------------------

In special cases you might be in need of different type of fixedpoint number
:
[8.24]  for greater precision or [24.8] for greater range of numbers
represented
This also would be helpful in assembler routines when you select the
division to integer/fractional parts for the best performance...

In C, to perform addition / subtraction of fixedpoint values, simply write

Fixedpoint a, b, c, d;
c = a + b;
d = a - b;

To perform multiplication / division, use the following  code:

-------Fixed.h ----------
extern "C" Fixedpoint FixedMul (Fixedpoint M1, Fixedpoint M2);
extern "C" Fixedpoint FixedDiv (Fixedpoint Dividdend, Fixedpoint Divisor);

------- Fixed.asm ------- (32-bit protected mode)

        .486p
        .model flat

        public FixedMul
        public FixedDiv

ROUNDING_ON equ 1      ; set to 0 to disable rounding (1 bit loss / 1 cycle
faster)

        .code
        align dword

FMparms struc
        dd 2 dup (?)
FMm1    dd ?
FMm2    dd ?
FMparms ends

;+-------------------------------------------------------------------
;| Fixedpoint FixedMul (Fixedpoint m1, Fixedpoint m2);
;+-------------------------------------------------------------------
FixedMul proc
          push ebp
          mov ebp, esp

          mov eax,[ebp+FMm1]
          imul [ebp+FMm2]
if ROUNDING_ON
          add eax,8000h
          adc edx,0
endif
          shrd eax, edx, 16
          pop ebp
          ret
FixedMul endp

FDparms  struc
         dd 2 dup(?)
Dividend dd ?
Divisor  dd ?
FDparms  ends

;+-----------------------------------------------------------------+
;| Fixedpoint FixedDiv (Fixedpoint Dividend, Fixedpoint Divisor);  |
;+-----------------------------------------------------------------+
FixedDiv proc
         push ebp
         mov ebp, esp

         mov ebx, [ebp+Divisor]
         mov eax, [ebp+Dividend]
         cdq
         shld edx, eax, 16
         shl eax, 16
         idiv ebx

         pop ebp
         ret
FixedDiv endp

end
--------------------

Hope that would help you to get used to loving fixedpoint math... :))
If have questions, feel free to mail me
CU

--

***> http://qdamage.webjump.com <*** collection of my demos / sources ***

 
 
 

Is Fixed point still better

Post by Jorrit Tyberghei » Fri, 25 Sep 1998 04:00:00




>> I have a coule of books on 3d programming, most of which say that
>>fixed point math is a good way to optimise any program (although none
>>of them actually tell me how), But I heard that on a pentium floating
>>point math is much faster, and the books are quite old. So, is it
>>worth going to all the trouble of learning how to use fixed point
>>math, or is it now redundant.

>Nope, fixed-point math is still VERY fast, and if you're going to create
>somewhat fast 3D-engine you should intensively use it.

This is not true if you're targetting your engine for a Pentium processor.
Floating point on a pentium is VERY fast and it is extremely hard to achieve
the same speed with fixed point (if not impossible in some cases).

Another big advantage of floating point on the pentium is that many operations
run in parallel with the integer unit meaning that you can gain even more speed.
For example, a FP division takes a lot of cycles (approx 30-40 I think) but the
division can run in parallel with the integer unit which means that you do other
things in the mean time. A good pentium aware C compiler will make use of
that fact.

My recommendation is to use floating point wherever possible but keep in
mind that conversion from floating point to integer is VERY SLOW so this should
be avoided as much as possible. The consequence of this is that the inner
texture mapper loop should possibly be written in fixed point since you'll have
to use integers to write to the screen.

In my Crystal Space engine I use floating point everywhere except in the final
texture mapping inner loop and the texture cache calculations.

Quote:>A floating-point number is simply a 32-bit integer, most often in 16.16
>format
>for common computations (mean last 16 bits is for fractional part of number)

This is not true. The IEEE format for floating point is not just 16.16. There are
also bits for the exponent and so on. Or perhaps you mean a 'fixed-point' number
instead of floating-point here?

Greetings,

--
---------------------------------------------------------------------------

Project Manager of Crystal Space, a free and portable 3D 6DOF portal engine.
More info at: http://crystal.linuxgames.com
---------------------------------------------------------------------------

 
 
 

Is Fixed point still better

Post by Sasko Celakovsk » Fri, 25 Sep 1998 04:00:00


Quote:>  I have a coule of books on 3d programming, most of which say that
> fixed point math is a good way to optimise any program (although none
> of them actually tell me how), But I heard that on a pentium floating
> point math is much faster, and the books are quite old. So, is it
> worth going to all the trouble of learning how to use fixed point
> math, or is it now redundant.

> -Steve

Pentium's FPU is much faster than fixed point arithmetics....    But you
have to know the optimization tips...   Search developer.intel.com site for
seom optimization manuals for Pentium procesor...    

Sasko

 
 
 

Is Fixed point still better

Post by Mark Jongerma » Fri, 25 Sep 1998 04:00:00


Quote:> So, is it worth going to all the trouble of learning how to use fixed point
> math, or is it now redundant.

First of all it's no trouble at all, fixed point math is very easy. Most people
already use fixed point in real life when they're calculating with money.
(Overhere in the netherlands for example guilders are in floating point
format while cents are in fixed point format, 3.45 guilders is 345
cents, piece of cake, even my grandmother knows how to use it ;-)

Second of all, yes, it's worth it... Most people use a combination of fixed
point and floating point nowadays... And it's pretty basic mathematical
knowledge in the 3d programming world, so it never hurts to understand
how to use it anyhow...

          Mark

 
 
 

Is Fixed point still better

Post by Andreas Till » Fri, 25 Sep 1998 04:00:00


Just my two cents to this discussion:

I've heard, that using the MMX instruction set makes integer
much faster then before.  So if you are able to use the MMX set
of your processor (don't ask me how ... I would be interested myself)
you can gain a big performance shift.

Otherwise MMX is just something for the sellers to tell you
why you need a new computer :)

Kind regards

          Andreas.

 
 
 

Is Fixed point still better

Post by Q » Fri, 25 Sep 1998 04:00:00



>>Nope, fixed-point math is still VERY fast, and if you're going to create
>>somewhat fast 3D-engine you should intensively use it.

>This is not true if you're targetting your engine for a Pentium processor.
>Floating point on a pentium is VERY fast and it is extremely hard to
achieve
>the same speed with fixed point (if not impossible in some cases).

integer ADD takes 1 CPU cycle and may be paired with another integer
operation
while FADD takes 74 cycles (hovewer it's still can be paired with a lots
of integer instructions)

Quote:>Another big advantage of floating point on the pentium is that many
operations
>run in parallel with the integer unit meaning that you can gain even more

speed.

That's what i'd suggested

Quote:>My recommendation is to use floating point wherever possible but keep in
>mind that conversion from floating point to integer is VERY SLOW so this
should
>be avoided as much as possible. The consequence of this is that the inner
>texture mapper loop should possibly be written in fixed point since you'll
have
>to use integers to write to the screen.

I'd assume that you write texture filler in assembler so that compiler dont
make
conversion slow.

Quote:>>A floating-point number is simply a 32-bit integer, most often in 16.16
>>format
>>for common computations (mean last 16 bits is for fractional part of
number)

>This is not true. The IEEE format for floating point is not just 16.16.
There are
>also bits for the exponent and so on. Or perhaps you mean a 'fixed-point'
number
>instead of floating-point here?

sorry, i mean fixed-point indeed

--

***> http://qdamage.webjump.com <*** collection of my demos / sources ***

 
 
 

Is Fixed point still better

Post by Q » Fri, 25 Sep 1998 04:00:00


Andreas Tille wrote ...

Quote:>I've heard, that using the MMX instruction set makes integer
>much faster then before.  So if you are able to use the MMX set
>of your processor (don't ask me how ... I would be interested myself)
>you can gain a big performance shift.

I'd like to experiment with MMX too, but i've got no MMX CPU
MMX adds 8 64-bit integer registers to your CPU and a lotsa of
instructions.....
(you'd better check Intel's developers page for docs)
http://developer.intel.com

--

***> http://qdamage.webjump.com <*** collection of my demos / sources ***

 
 
 

Is Fixed point still better

Post by Jorrit Tyberghei » Fri, 25 Sep 1998 04:00:00




>>>Nope, fixed-point math is still VERY fast, and if you're going to create
>>>somewhat fast 3D-engine you should intensively use it.

>>This is not true if you're targetting your engine for a Pentium processor.
>>Floating point on a pentium is VERY fast and it is extremely hard to
>achieve
>>the same speed with fixed point (if not impossible in some cases).

>integer ADD takes 1 CPU cycle and may be paired with another integer
>operation
>while FADD takes 74 cycles (hovewer it's still can be paired with a lots
>of integer instructions)

I really doubt that a FADD takes 74 cycles when the more complex FDIV
takes only about 30-40 (forgot the exact number but it is around that).

And you're right. For simply adding fixed pointer numbers compared to
floating point numbers fixed point will win. But adding is not the only operation
I do on floating/fixed point numbers. Multiplication and division is more
difficult with fixed point than just a matter of taking integer mult/div.

Quote:>I'd assume that you write texture filler in assembler so that compiler dont
>make
>conversion slow.

No I use another trick to convert floating point to integer:

#define FIST_MAGIC ((((65536.0 * 65536.0 * 16)+(65536.0 * 0.5))* 65536.0))
inline long QuickInt (float inval)
{
  double dtemp = FIST_MAGIC + inval;
  return ((*(long *)&dtemp) - 0x80000000);

Quote:}

Or the next one which has exactly the same speed but converts directly
to fixed point 16:16 (no need to multiply the float with 65536.).

#define FIST_MAGIC2 ((((65536.0 * 16)+(0.5))* 65536.0))
inline long QuickInt16 (float inval)
{
  double dtemp = FIST_MAGIC2 + inval;
  return ((*(long *)&dtemp) - 0x80000000);

Quote:}

Greetings,

--
---------------------------------------------------------------------------

Project Manager of Crystal Space, a free and portable 3D 6DOF portal engine.
More info at: http://crystal.linuxgames.com
---------------------------------------------------------------------------

 
 
 

Is Fixed point still better

Post by Defee Paw » Fri, 25 Sep 1998 04:00:00



>integer ADD takes 1 CPU cycle and may be paired with another integer
>operation
>while FADD takes 74 cycles (hovewer it's still can be paired with a lots
>of integer instructions)

Please visit developer.intel.com and read those numbers once again
(effective clock count is 1 CPU cycle for both). No need to pair with
_integer_ instructions either, the FPU is fully pipelined. If not
pipelined, both take the same number of cycles which will be (around) the
length of the pipeline (~5).

--
******************************************************************************
Pawel Defee

******************************************************************************

 
 
 

Is Fixed point still better

Post by James Sharma » Fri, 25 Sep 1998 04:00:00





>>>>Nope, fixed-point math is still VERY fast, and if you're going to create
>>>>somewhat fast 3D-engine you should intensively use it.

>>>This is not true if you're targetting your engine for a Pentium
processor.
>>>Floating point on a pentium is VERY fast and it is extremely hard to
>>achieve
>>>the same speed with fixed point (if not impossible in some cases).

>>integer ADD takes 1 CPU cycle and may be paired with another integer
>>operation
>>while FADD takes 74 cycles (hovewer it's still can be paired with a lots
>>of integer instructions)

Some cycle counts for floating point and integer on teh pentium.

(int) ADD 1
(float) FADD 1-3
(int) DIV 17-41
(float) FDIV 39
(int) MUL 8-11
(float) MUL 1-3

Where the figures are variable The last of these is of course the most
significant,  a well optimimised 3vec dot product in asssembler can be done
in 5 cycles but in integer it will take a minimum of 26 cycles + some
shifting to take accont of any fixed point. The real benfits can be taken
from mixing float and integer to gain the paralelism although anyone who has
had do this will know that it's very difficult, but it can be done.

It's best not to ask if it's better to use floating point or fixed but to
work out how you can use both at the same time most efficently.

The mmx extensions are very interesting.  Remember the mmx instructions
can't be used in the same peice of code as the fp instructions since
reseting the fp unit from mmx back to fp is very expensive. But it can once
again be a big performance increase when done right.  The mmx gives you
eight new registers (essentialy new forms of the fp registers) which can be
worked on in a number of different formats: 1*64bit register, 2*32bits,
4*16bits and 8*8bits. An eight bit add instruction on the mmx performs 8
paralel adds on the 8 values that make up the register.  Although there is
no 8 bit multiply there is 16bit multiply hardware present so you can do 4
symaltanious 16 bit multiplys at once in 3 cycles.

The biggest problem with mmx is getting data to and from the mmx format,
since the shufling the data in and out of the required format can be very
expensive it takes carefull plaing to do it well. In adition to this it can
ofton take a significant re-think on the algorithums involved to get the
kind of paralelism out of the chip it is capable of.

Recently AMD started shipping the K6-2 with its 3D-now technology,  this
works on a very similer basis to the mmx system but gives enhanced floating
point. This time the floating point register space is split into 2 32bit
floating point numbers with appropriate adds, muls etc.. Once again this can
be very usefull particuarly when you can rework your algorithms to support
it.

In the case of both mmx and 3d-now you can not presume their presence so it
is necisary to provide code that can handle multiple levels of hardware
functionality.  It was not that long ago when we had to use similer measures
to work on processors with fp and without fp support and I for one look
forward to the time when I can list mmx as a minimum requirement on a game
spec and not have it laughed at in selection stage :)

James,

James,

 
 
 

Is Fixed point still better

Post by dake/calod » Fri, 25 Sep 1998 04:00:00



> I have a coule of books on 3d programming, most of which say that
>fixed point math is a good way to optimise any program (although none
>of them actually tell me how), But I heard that on a pentium floating
>point math is much faster, and the books are quite old. So, is it
>worth going to all the trouble of learning how to use fixed point
>math, or is it now redundant.

http://developer.intel.com/design/litcentr/CDORDER.HTM

order this CD; it's TOTALLY free (at least, when i ordered it,
about 3 months ago) and you'll have all docs and sets of
opcodes. it's a mirror of intel developper site.

dake/calodox
www.space.ch/scene/calodox

 
 
 

Is Fixed point still better

Post by Michael Choc » Fri, 25 Sep 1998 04:00:00



>Andreas Tille wrote ...
>>I've heard, that using the MMX instruction set makes integer
>>much faster then before.  So if you are able to use the MMX set
>>of your processor (don't ask me how ... I would be interested myself)
>>you can gain a big performance shift.

>I'd like to experiment with MMX too, but i've got no MMX CPU
>MMX adds 8 64-bit integer registers to your CPU and a lotsa of
>instructions.....

To toss in a couple more pennies, MMX does not add any registers,
but instead makes use of the floating point registers.  The context
switch between floating point and MMX instructions is very slow, so
the two should be mixed as little as possible, if at all.

    - Mike

 
 
 

Is Fixed point still better

Post by C.P.I » Sat, 26 Sep 1998 04:00:00



> Just my two cents to this discussion:

> I've heard, that using the MMX instruction set makes integer
> much faster then before.  So if you are able to use the MMX set
> of your processor (don't ask me how ... I would be interested myself)
> you can gain a big performance shift.

        MMX instructions themselves aren't much faster than FPU instructions
for the most part...  The thing that makes MMX fast is that it is able
to do its operations on many operands.  e.g. An entire dot product can
be done in only a few lines of code...
        Converting between MMX and floating point is slow as hell...  Never mix
those two unless you have NO choice.  I usually wouldn't go for MMX code
because many people don't have MMX processors.  (regular Pentiums and
PPros are NOT MMX, 6x86's are not MMX (the 6x86MX is))

- C.P.I.

 
 
 

1. fixed-point vs. floating-point

Everybody knows that floating point operations are slow except for on
newer processors but why? Why is fixed point used instead? Does anyone
know of an online analysis or comparison between fixed- and
floating-point ops? -not just a source that states the fact.

Thanks in advance,

Micke

2. HELP ME WITH MY PAGE!!!

3. Floating point...Fixed Point?

4. PI5 and Gif-X plug-in problem. Please help.

5. Closest Point ON the surface to a fixed 3D point

6. Vancouver Cyber-Technology Seminar

7. fixed-point vs. floating-point

8. Full-screen, better-than-average stills on AV

9. Are Digital-Video-cameras good enough for stills?

10. Video Camera for GOOD Stills?

11. Am I missing the point using glCopyPixels????

12. Poser - How to remove Fixed Point?