Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fixed point-union vs. long

3 views
Skip to first unread message

The Knight Who Says Ni!

unread,
Dec 9, 1995, 3:00:00 AM12/9/95
to
I have been using longs for all my fixed-point numbers, but recently i
saw some code that used this:

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. |
| ___________________________|_
\_/____________________________/


Rainer Deyke

unread,
Dec 21, 1995, 3:00:00 AM12/21/95
to
The Knight Who Says Ni! (knigh...@usa.pipeline.com) wrote:
: I have been using longs for all my fixed-point numbers, but recently i

: saw some code that used this:

: 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) |
+------------------------------------------------------+

Chris Palmer

unread,
Dec 26, 1995, 3:00:00 AM12/26/95
to
In article <4bcdu6$i...@deyke3.fc.hp.com>,

Rainer Deyke <rai...@mdddhd.fc.hp.com> wrote:
>The Knight Who Says Ni! (knigh...@usa.pipeline.com) wrote:
>: I have been using longs for all my fixed-point numbers, but recently i
>: saw some code that used this:
>
>: 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.

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/

Rainer Deyke

unread,
Jan 3, 1996, 3:00:00 AM1/3/96
to
Chris Palmer (crpa...@solo.uwaterloo.ca) wrote:
: In article <4bcdu6$i...@deyke3.fc.hp.com>,

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.

0 new messages