473,394 Members | 1,737 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,394 software developers and data experts.

Bit twiddling/manipulation question

I need to store two float values (each of which requires less 16 bit
storage), in a double data type.

Can anyone give me some macros to retrive/store values in the double ?

something along the lines of

STORE_LOWWORD(storage, value)
GET_LOWWORD(storage) /* returns value */
STORE_HIGHWORD(storage, value)
GET_HIGHWORD(storage) /* returns value */
Jun 24 '07 #1
11 1959
Bartholomew Simpson <12**********@terrace.comwrote:
# I need to store two float values (each of which requires less 16 bit
# storage), in a double data type.
#
# Can anyone give me some macros to retrive/store values in the double ?

The format of float values is specific to the system, such DEC F or
IEEE single precision. You're not going to get a small bit of code that
runs everywhere. You can get a small bit of code that runs somewhere,
or a big chunk of code that runs everywhere.

You may end up doing some like
union {
struct {
unsigned signbit: 1;
int exponent: 6;
unsigned fraction: 9;
} parts;
short packed;
} smallreal;
union {
struct {
unsigned signbit: 1;
int exponent: 11;
unsigned fraction: 20;
} parts;
float packed;
} real;
real.packed = floatvalue;
smallreal.parts.signbit = real.parts.signbit;
smallreal.parts.exponent = real.parts.exponent-11;
smallreal.parts.fraction = real.parts.fraction>>11;

I used unions and structs because you cannot use a cast to convert
a float value to the same bit pattern now called an integer nor
vice versa. You have to pass through struct or char array using
union aliassing or memcpy.

In the DSP world there are small real formats, µlaw or something
like that. You can look that up and see if all these small
formats are acceptable and if transformers are already defined on
the systems that you want to run on.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
What kind of convenience store do you run here?
Jun 24 '07 #2

"Bartholomew Simpson" <12**********@terrace.comwrote in message
news:zr*********************@bt.com...
>I need to store two float values (each of which requires less 16 bit
storage), in a double data type.

Can anyone give me some macros to retrive/store values in the double ?

something along the lines of

STORE_LOWWORD(storage, value)
GET_LOWWORD(storage) /* returns value */
STORE_HIGHWORD(storage, value)
GET_HIGHWORD(storage) /* returns value */
#define STORE_LOWORD(storage, value) (((float *)&storage)[1] = value)
#define GET_LOWORD(storage) ((float *)&storage)[1]

hiword is left as an exercise. Swap the names if you are a little endian.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 24 '07 #3
On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
<re*******@btinternet.comwrote:
>
"Bartholomew Simpson" <12**********@terrace.comwrote in message
news:zr*********************@bt.com...
>>I need to store two float values (each of which requires less 16 bit
storage), in a double data type.

Can anyone give me some macros to retrive/store values in the double ?

something along the lines of

STORE_LOWWORD(storage, value)
GET_LOWWORD(storage) /* returns value */
STORE_HIGHWORD(storage, value)
GET_HIGHWORD(storage) /* returns value */

#define STORE_LOWORD(storage, value) (((float *)&storage)[1] = value)
#define GET_LOWORD(storage) ((float *)&storage)[1]

hiword is left as an exercise. Swap the names if you are a little endian.
Is there any guarantee that a double will be properly aligned for a
float?
Remove del for email
Jun 24 '07 #4
On Sun, 24 Jun 2007 14:20:42 +0100, Bartholomew Simpson
<12**********@terrace.comwrote:
>I need to store two float values (each of which requires less 16 bit
storage), in a double data type.

Can anyone give me some macros to retrive/store values in the double ?

something along the lines of

STORE_LOWWORD(storage, value)
GET_LOWWORD(storage) /* returns value */
STORE_HIGHWORD(storage, value)
GET_HIGHWORD(storage) /* returns value */
You actually have a system which stores a float in 16 bits? The
standard requires at least six significant digits. That means it must
store all integer values between -999,999 and +999,999 exactly. Since
there are only 65,536 possible values in a 16-bit object, could you
tell us how this works.
Remove del for email
Jun 25 '07 #5
Barry Schwarz wrote, On 25/06/07 00:50:
On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
<re*******@btinternet.comwrote:
>"Bartholomew Simpson" <12**********@terrace.comwrote in message
news:zr*********************@bt.com...
>>I need to store two float values (each of which requires less 16 bit
storage), in a double data type.

Can anyone give me some macros to retrive/store values in the double ?

something along the lines of

STORE_LOWWORD(storage, value)
GET_LOWWORD(storage) /* returns value */
STORE_HIGHWORD(storage, value)
GET_HIGHWORD(storage) /* returns value */
#define STORE_LOWORD(storage, value) (((float *)&storage)[1] = value)
#define GET_LOWORD(storage) ((float *)&storage)[1]

hiword is left as an exercise. Swap the names if you are a little endian.

Is there any guarantee that a double will be properly aligned for a
float?
No. There is also no guarantee that sizeof(double) >= 2*sizeof(float)
--
Flash Gordon
Jun 25 '07 #6
On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwrote:
>Barry Schwarz wrote, On 25/06/07 00:50:
>On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
<re*******@btinternet.comwrote:
>>"Bartholomew Simpson" <12**********@terrace.comwrote in message
news:zr*********************@bt.com...
I need to store two float values (each of which requires less 16 bit
storage), in a double data type.

Can anyone give me some macros to retrive/store values in the double ?

something along the lines of

STORE_LOWWORD(storage, value)
GET_LOWWORD(storage) /* returns value */
STORE_HIGHWORD(storage, value)
GET_HIGHWORD(storage) /* returns value */
#define STORE_LOWORD(storage, value) (((float *)&storage)[1] = value)
#define GET_LOWORD(storage) ((float *)&storage)[1]

hiword is left as an exercise. Swap the names if you are a little endian.

Is there any guarantee that a double will be properly aligned for a
float?

No. There is also no guarantee that sizeof(double) >= 2*sizeof(float)
My question was about alignment, not size. To rephrase as a concrete
example, is it legal for a system to align doubles on 4-byte
boundaries but require a float to be on an 8-byte boundary.
Remove del for email
Jun 25 '07 #7
Barry Schwarz <sc******@doezl.netwrites:
On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwrote:
>>Barry Schwarz wrote, On 25/06/07 00:50:
[...]
>>Is there any guarantee that a double will be properly aligned for a
float?

No. There is also no guarantee that sizeof(double) >= 2*sizeof(float)

My question was about alignment, not size. To rephrase as a concrete
example, is it legal for a system to align doubles on 4-byte
boundaries but require a float to be on an 8-byte boundary.
Flash already answered your question (see "No", above); he also added
some information about size.

Yes, alignment requirements such as you describe are legal, and even
plausible. For example, suppose type float is 8 bytes, and the
hardware requires 8-byte alignment, but double is, say, 12 bytes, but
is implemented in software, so it only requires 4-byte alignment.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 25 '07 #8
On Mon, 25 Jun 2007 01:55:04 -0700, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@doezl.netwrites:
>On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwrote:
>>>Barry Schwarz wrote, On 25/06/07 00:50:
[...]
>>>Is there any guarantee that a double will be properly aligned for a
float?

No. There is also no guarantee that sizeof(double) >= 2*sizeof(float)

My question was about alignment, not size. To rephrase as a concrete
example, is it legal for a system to align doubles on 4-byte
boundaries but require a float to be on an 8-byte boundary.

Flash already answered your question (see "No", above); he also added
some information about size.
Yes he did; I glossed over the "also".

This entire subthread was meant as a rhetorical question criticizing
Malcolm McLean's solution which cast a double* to a float*. I guess
I'm unknowingly approaching the RH level of subtlety.

Sorry Richard but you have company whether you want it or not.
Remove del for email
Jun 25 '07 #9
In article <13*************@corp.supernews.com>,
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
>You may end up doing some like
union {
struct {
unsigned signbit: 1;
int exponent: 6;
unsigned fraction: 9;
} parts;
short packed;
} smallreal;
union {
struct {
unsigned signbit: 1;
int exponent: 11;
unsigned fraction: 20;
} parts;
float packed;
} real;
real.packed = floatvalue;
smallreal.parts.signbit = real.parts.signbit;
If you store into a union via one type, you can't portably
retrieve from it using a different type, except in the case where
the two types field types are the same and share a common "prefix".

There is also no certainty of support for a bitfield of 20 bits.
"unsigned fraction" is "unsigned int fraction", and C only requires
int to be 16 bits wide (but larger is allowed for implementations.)
C89 at least does not support bitfields of type 'long' or
'unsigned long', the minimum required to be certain of 20 bit fields.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jun 25 '07 #10
Barry Schwarz wrote, On 25/06/07 18:11:
On Mon, 25 Jun 2007 01:55:04 -0700, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@doezl.netwrites:
>>On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwrote:
Barry Schwarz wrote, On 25/06/07 00:50:
[...]
>>>>Is there any guarantee that a double will be properly aligned for a
float?
No. There is also no guarantee that sizeof(double) >= 2*sizeof(float)
My question was about alignment, not size. To rephrase as a concrete
example, is it legal for a system to align doubles on 4-byte
boundaries but require a float to be on an 8-byte boundary.
Flash already answered your question (see "No", above); he also added
some information about size.
Yes he did; I glossed over the "also".

This entire subthread was meant as a rhetorical question criticizing
Malcolm McLean's solution which cast a double* to a float*. I guess
I'm unknowingly approaching the RH level of subtlety.
Actually, I realised that, I was just supporting the possibility of
there being problems by suggesting yet another problem with his solution.

Of course, this stems from the OP trying to do something that may not be
possible.
Sorry Richard but you have company whether you want it or not.
Perhaps I am getting too subtle as well :-)
--
Flash Gordon
Jun 25 '07 #11
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
There is also no certainty of support for a bitfield of 20 bits.
"unsigned fraction" is "unsigned int fraction", and C only requires
int to be 16 bits wide (but larger is allowed for implementations.)
C89 at least does not support bitfields of type 'long' or
'unsigned long', the minimum required to be certain of 20 bit fields.
That hasn't changed in C99; the only types guaranteed to be supported
for bit fields are int, unsigned int, signed int, and _Bool.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 25 '07 #12

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

Similar topics

58
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
4
by: Elijah Bailey | last post by:
I have a long x; I want to write a function long f(long x, int k) such that it extracts every k-th bit of x, concatenates them and returns it. Anyone can help me in writing this function? ...
9
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
7
by: Jeff.Goldfinkle | last post by:
Hi All Is there a simple way to twiddle the bits of a float? In particular, I would like to round my float to the n most significant bits. For example - 0.123 in binary is 0.000111111...
1
by: nick777 | last post by:
Hope the Community can bear with me as I muddle with the vocabulary since I am not really sure if I am going about this the correct way. My question is as follows: If I had some sample data in...
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.