NAME¶
VistaIOConst - generate constants of the appropriate type
SYNOPSIS¶
VistaIOFloat VistaIOFloatConst (f)
VistaIODouble VistaIODoubleConst (f)
VistaIOBit VistaIOBitConst (i)
VistaIOUByte VistaIOUByteConst (i)
VistaIOSByte VistaIOSByteConst (i)
VistaIOShort VistaIOShortConst (i)
VistaIOLong VistaIOLongConst (i)
ARGUMENTS¶
- f
- Specifies a floating-point constant.
- i
- Specifies an integer constant.
DESCRIPTION¶
In expressions that get executed within inner loops, you may wish to avoid
unnecessary coercions between the C types
float and
double, or
between
int and
long. By allowing you to specify constants of
the appropriate type in a portable, platform-independent manner, these macros
allow you to avoid unintended coercions.
In ANSI C, one can specify that a floating point constant is of type
float be appending an
f character to it. This can be used to
avoid coercions in expressions involving other
float values. For
example, this fragment of code uses a
float constant within its loop:
int i;
float f = 0.0;
for (i = 0; i < 10000000; i++)
f += 1.0f;
It runs about three times faster than this one, which uses a
double
constant:
int i;
float f = 0.0;
for (i = 0; i < 10000000; i++)
f += 1.0;
The
VistaIOFloatConst and
VistaIODoubleConst macros allow you to
avoid unnecessary coercions in expressions involving values of type
VistaIOFloat and
VistaIODouble. Whether or not
VistaIOFloat and
VistaIODouble are equivalent to
float
and
double may depend on what platform your code is being compiled for.
Regardless, you can generate constants of the appropriate type by using the
VistaIOFloatConst and
VistaIODoubleConst macros. On a platform
where
VistaIOFloat is defined as
float,
VistaIOFloatConst(1.0) expands to
1.0f; where
VistaIOFloat is defined as
double, it expands to
1.0.
Similarly,
VistaIODoubleConst may or may not append an
f
character to the constant, depending on how
VistaIODouble is defined.
Similarly, the
VistaIOBitConst,
VistaIOUByteConst,
VistaIOSByteConst,
VistaIOShortConst, and
VistaIOLongConst take an integer constant; each one, depending on the
platform, may or may not append an
l character to the constant to
specify that it is of type
long. On platforms where
int and
long have different representations, using these macros may prevent
unintended coercions between the two representations.
So if your want your inner loops to run blindingly fast on any platform, you
should write them like this to avoid unnecessary coercions:
int i;
VistaIOFloat f = 0.0;
for (i = 0; i < 10000000; i++)
f += VistaIOFloatConst (1.0);
SEE ALSO¶
VistaIORepn(3),
NOTES¶
The macros can be used with a C compiler that doesn't meet the ANSI standard,
but in that case they may not eliminate all type coercions between
float and
double.
If you're more concerned about your code being easy to read, you may be better
off not using these macros.
AUTHOR¶
Art Pope <pope@cs.ubc.ca>
Adaption to vistaio: Gert Wollny <gw.fossdev@gmail.com>