[comp.sys.amiga.programmer] Switching between integer and floating point

ben@epmooch.UUCP (Rev. Ben A. Mesander) (04/24/91)

>In article <1991Apr24.041205.1588@netcom.COM> rodent@netcom.COM (Ben Discoe) writes:
>
>  I've been working on a 3D object viewer intended for PD release, with
>source, as soon as it's stable.  My problem is this: when the project was
>started, I only had a 1000 to work on, so I wrote all the math using
>two kinds of fixed-point integer math (one kind for angles, another for
>coordinates).
>  Now I (and many people who may want the code) are on accelerated
>systems with 6888x's, so I'd like to include a compile-time flag to
>compile with floating-point math instead of the integers.  I'm sure many
>other people have faced this same problem: how do you keep one source
>with two different kinds of math involved?
>  There are three options I can think of, and none are appealing.
>
>  1. use a lot of #defines:
>
>#if INTEGER
>  #define mult(a, b) ((a*b)>>8)
>#else
>  #define mult(a, b) (a*b)
>#endif

Or, you could use pointers to functions, and allow the same object module
to be used on machines that have a 6888x and those that don't. I don't
find either that solution or the one your propose above "ugly", although
I guess you do...


>  This makes code very ugly and hard to understand/debug.

I'm not sure how you mean, Ben..

--
| ben@epmooch.UUCP   (Ben Mesander)       | "Cash is more important than |
| ben%servalan.UUCP@uokmax.ecn.uoknor.edu |  your mother." - Al Shugart, |
| !chinet!uokmax!servalan!epmooch!ben     |  CEO, Seagate Technologies   |

rodent@netcom.COM (Ben Discoe) (04/24/91)

  I've been working on a 3D object viewer intended for PD release, with
source, as soon as it's stable.  My problem is this: when the project was
started, I only had a 1000 to work on, so I wrote all the math using
two kinds of fixed-point integer math (one kind for angles, another for
coordinates).
  Now I (and many people who may want the code) are on accelerated
systems with 6888x's, so I'd like to include a compile-time flag to
compile with floating-point math instead of the integers.  I'm sure many
other people have faced this same problem: how do you keep one source
with two different kinds of math involved?
  There are three options I can think of, and none are appealing.

  1. use a lot of #defines:

#if INTEGER
  #define mult(a, b) ((a*b)>>8)
#else
  #define mult(a, b) (a*b)
#endif

  This makes code very ugly and hard to understand/debug.

  2. Use C++.  I'm not positive, but it seems likely that you could introduce
a fixed-point integer class that would know how to operate on itself and
other classes.  This would allow us to keep one, elegant C-like source,
but would limit the number of people who could make use of the source,
and make it less portable (since you'd have to find a C++ compiler on
the machines you want to take it to).

  3. Maintain two seperate sources.  This has great potential for confusion
unless you are extremely careful to keep the sources functionally identical.
Since the fixed-point integer is full of shift-operations, it bears
only a little resembleance to the floating point code, making things difficult.

  Does anyone have other ideas or comments about these approaches?

----------------
Ben in San Jose, an Amigoid in a non-Amiga city.

chrisg@cbmvax.commodore.com (Chris Green) (04/25/91)

In article <1991Apr24.041205.1588@netcom.COM> rodent@netcom.COM (Ben Discoe) writes:
>
>  I've been working on a 3D object viewer intended for PD release, with
>source, as soon as it's stable.  My problem is this: when the project was
>started, I only had a 1000 to work on, so I wrote all the math using
>two kinds of fixed-point integer math (one kind for angles, another for
>coordinates).
>  Now I (and many people who may want the code) are on accelerated
>systems with 6888x's, so I'd like to include a compile-time flag to
>compile with floating-point math instead of the integers.  I'm sure many
>other people have faced this same problem: how do you keep one source
>with two different kinds of math involved?

	Unless you have precision problems with your fixed point, you'd be better
off keeping it, even on a 68882 system, since it will still be faster.
	A far better option for people with 020's and 030's is keeping the fixed
point, but going to 32 bit precision, and using the 32x32->64 multiply
and 64/32->32:32 divide instructions available on these processors.


-- 
*-------------------------------------------*---------------------------*
|Chris Green - Graphics Software Engineer   - chrisg@commodore.COM      f
|                  Commodore-Amiga          - uunet!cbmvax!chrisg       n
|My opinions are my own, and do not         - killyouridolssonicdeath   o
|necessarily represent those of my employer.- itstheendoftheworld       r
*-------------------------------------------*---------------------------d

frank@morpheus.UUCP (Frank McPherson) (04/25/91)

In article <1991Apr24.041205.1588@netcom.COM> rodent@netcom.COM (Ben Discoe) writes:
>
>  Does anyone have other ideas or comments about these approaches?
>----------------
>Ben in San Jose, an Amigoid in a non-Amiga city.

--
Seperate the program into modules, using functionality to determine what
goes where.  So, for example, you'd end up with a bunch of modules which
control your user interface, read from disk, etc, along with the math
modules.  For example, you could have integer.c and float.c.  You could
then include two seperate makefiles, with the only difference being that
one of them would compile integer.c, while the other would compile float.c.
There are many possible modifications you could make to this idea, most of
which should work pretty well as long as you document the difference well.
Make the users aware that they must choose which type of math they'd like
to use, and tell them some advantages and disadvantages of each.


-- Frank McPherson		 INTERNET : emcphers@fox.cs.vt.edu	--
--				AmigaUUCP : uunet!vtserf!morpheus!frank --

jcs@crash.cts.com (John Schultz) (04/26/91)

In <1991Apr24.041205.1588@netcom.COM> rodent@netcom.COM (Ben Discoe) writes:


>  I've been working on a 3D object viewer intended for PD release, with
>source, as soon as it's stable.  My problem is this: when the project was
>started, I only had a 1000 to work on, so I wrote all the math using
>two kinds of fixed-point integer math (one kind for angles, another for
>coordinates).
>  Now I (and many people who may want the code) are on accelerated
>systems with 6888x's, so I'd like to include a compile-time flag to
>compile with floating-point math instead of the integers.  I'm sure many
>other people have faced this same problem: how do you keep one source
>with two different kinds of math involved?


  Leave it in integer. The integer code will run much faster than floating
point on a 68020/030.  No need to waste your time with floats for computer
graphics work on an Amiga.



  John