473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Bit-twiddling with floats

I have an application that creates lots of small objects that
represent a 2D axis-parallel plane. In short, I these little
objects look like this:

struct Plane {
float location;
enum { X, Y, Z } direction;
};

On my 32-bit platform this structure is 8 bytes, 4 of which
are used to store the direction, a 2-bit value. Memory is
very critical in my world and I'd like to reduce this struct
in size.

An interesting characteristic of my problem domain is that the
float in the Plane structure doesn't have to be exact. I can
sacrifice two LSB bits in the float's fractional part to store
the direction (X, Y, Z) -- and in the process reduce the struct
to 4 bytes, a big saving for my application.

Clearly this code will not be portable, so I'm looking for some
suggestions for alternative solutions or ways to make it less
kludgy:

class Plane {

public:
typedef enum { X, Y, Z } Direction;

Plane(float location, Direction direction) {
// Order matters -- set direction second
m_impl.location = location;
m_impl.direction.value = direction;
}

float getLocation() const {
return m_impl.location;
}

Direction getDirection() const {
return static_cast<Direction>(m_impl.direction.value);
}

private:
union Impl {
float location;
// Depends on the platform's IEEE float layout
struct Bits {
unsigned value : 2;
} direction;
} m_impl;
};
Jul 22 '05 #1
3 1495
Derek wrote:
I have an application that creates lots of small objects that
represent a 2D axis-parallel plane. In short, I these little
objects look like this:

struct Plane {
float location;
enum { X, Y, Z } direction;
};

On my 32-bit platform this structure is 8 bytes, 4 of which
are used to store the direction, a 2-bit value.
I would expect that enum to be only one byte big and the rest to be padding.
The problem is that a float might need to be aligned to a 4 byte boundary
on your system. On some platforms, unaligned accesses are possible, but
slow. Now consider what happens if you have an array of your struct. For
the second element's float to be correctly aligned, the size of the struct
must be a multiple of 4.
Memory is very critical in my world and I'd like to reduce this struct
in size.

An interesting characteristic of my problem domain is that the
float in the Plane structure doesn't have to be exact. I can
sacrifice two LSB bits in the float's fractional part to store
the direction (X, Y, Z) -- and in the process reduce the struct
to 4 bytes, a big saving for my application.

Clearly this code will not be portable, so I'm looking for some
suggestions for alternative solutions or ways to make it less
kludgy:

class Plane {

public:
typedef enum { X, Y, Z } Direction;

Plane(float location, Direction direction) {
// Order matters -- set direction second
m_impl.location = location;
m_impl.direction.value = direction;
}

float getLocation() const {
return m_impl.location;
}

Direction getDirection() const {
return static_cast<Direction>(m_impl.direction.value);
}

private:
union Impl {
float location;
// Depends on the platform's IEEE float layout
struct Bits {
unsigned value : 2;
} direction;
} m_impl;
};


If you don't care much about portability and your system supports unaligned
accesses to float values, there might be a way to make your compiler pack
the structure, so that it takes 5 bytes.
But maybe instead of fiddling around on byte level, maybe you can do a
change on a higher level. I don't know much about your problem, but maybe
instead of storing all the planes including thei direction in one big
container, you could simply use three containers - one for each direction -
and then just store the float values in it.

Jul 22 '05 #2
Rolf Magnus wrote:
struct Plane {
float location;
enum { X, Y, Z } direction;
};

On my 32-bit platform this structure is 8 bytes, 4 of which
are used to store the direction, a 2-bit value.
I would expect that enum to be only one byte big and the rest
to be padding. The problem is that a float might need to be
aligned to a 4 byte boundary on your system. On some platforms,
unaligned accesses are possible, but slow. Now consider what
happens if you have an array of your struct. For the second
element's float to be correctly aligned, the size of the struct
must be a multiple of 4.


That's what I figured. I suppose most compilers have #pragma
directives or other non-portable ways to pack structures, so I
could use that to reduce the struct to 5 bytes. However that's
not portable either, so as long as I'm writing platform-dependent
code, I might as well use my approach and get down to 4 bytes.
If you don't care much about portability and your system
supports unaligned accesses to float values, there might be a
way to make your compiler pack the structure, so that it takes
5 bytes.
That's what I figured. I'll look into it some more.
But maybe instead of fiddling around on byte level, maybe you
can do a change on a higher level. I don't know much about
your problem, but maybe instead of storing all the planes
including thei direction in one big container, you could simply
use three containers - one for each direction -
and then just store the float values in it.


Unfortunately not possible, but thanks for the suggestion. It's
always better to think about optimizations at the highest level
possible first, but I'm certain I can't factor anything out of
this structure.

Thanks.
Jul 22 '05 #3
> struct Plane {
float location;
enum { X, Y, Z } direction;
};

On my 32-bit platform this structure is 8 bytes, 4 of which
are used to store the direction, a 2-bit value. Memory is
very critical in my world and I'd like to reduce this struct
in size.


Have you considered creating a different type for each
direction? As in

struct dim_x {
float location ;
...
} ;

struct dim_y {
float location ;
...
} ;

struct dim_z {
float location ;
...
} ;

and then writing code such as

void draw (
dim_x x ,
dim_y y ,
dim_z z ) ...

This way your coordinates will be 4 bytes only, they will
be type safe, and you can avoid portability problems. This
solution has a number of benefits with respect to reducing
common coordinate related bugs like sending two x
coordinates rather than an x and y etc.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit...
13
by: Amy DBA | last post by:
I've been asked to administer a DB2 V 8 (32-bit install) on a Solaris 64-bit platform. It seems like whomever installed DB2 on the server, goofed for not installing DB2 v8 64 bit. Do I understand...
12
by: Jean-Marc Blaise | last post by:
Hi, Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of: - performance - configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for Bufferpools) - other ? ...
112
by: Carsten Hansen | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
4
by: tommydkat | last post by:
Well, I've finally gotten UDB 8.2 FixPak 3 up and running on my HP-UX 11i system, thanks to Mr McBride and IBM support. :) I created a 32-bit instance and that's running just fine. However, I...
14
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am using C# to develop DLL using Visual Studio 2005 and .Net 2.0, and I have no idea of how to make my DLL work with applications on 64-bit platform. Above all, I do not...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.