typedef union
{
struct
{
#ifdef LITTLE_ENDIAN
unsigned short lower;
short upper;
#else
short upper;
unsigned short lower;
#endif
} f16;
long f32;
} fixed_t;
Will this speed things up by avoiding the shifts, or is there some
overhead for using unions/structs?
Thanks
_______________________________
/ \ The Knight Who Says "Ni!" \
\_| |
| I'm not nearly as think |
| as you confused I am. |
| ___________________________|_
\_/____________________________/
: typedef union
: {
: struct
: {
: #ifdef LITTLE_ENDIAN
: unsigned short lower;
: short upper;
: #else
: short upper;
: unsigned short lower;
: #endif
: } f16;
: long f32;
: } fixed_t;
: Will this speed things up by avoiding the shifts, or is there some
: overhead for using unions/structs?
: Thanks
structs are usually passed by pointer, even if they are only 32 bits
long. I guess it depends on the compiler. I would simply use a long
and hope the compiler can optimize thre shifts out, or go to assembly.
--
+------------------------------------------------------+
| "Fumbling in frustration, inside soul torn apart |
| Feel the loss of paradise, leave an empty heart |
| Closing eyes will shut out, the warm light of a life |
| Grip is fading slowly, for each day passing by" |
| - Desultory, "A Closing Eye" |
+------------------------------------------------------+
| Rainer Deyke (rai...@mdddhd.fc.hp.com) |
+------------------------------------------------------+
Actually structures are not necessarily and probably almost never passed
as pointers unless the user specifically does so. For example,
void foo(union fixed_t bar)
{ bar.f32 = 0; }
int main(int argc, char ** argv)
{
union fixed_t f;
f.32 = 10;
foo(f);
printf("%ld\n", f.32);
return(0);
}
should print 10. If it does not then your compiler is broken!
Cheers,
Chris.
--
Mail: crpa...@undergrad.uwaterloo.ca
Homepage: http://www.undergrad.math.uwaterloo.ca/~crpalmer/
Of course. What I meant was that a pointer to a copy of the struct/union is
passed. Now that I think about, that's probably not what happens. But I do
know that all functions that RETURN structs/unions actually are passed a
hidden pointer argument to the location at which the struct/union is to be
stored.