Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bit twiddling/manipulation question

Reply
Thread Tools

Bit twiddling/manipulation question

 
 
Bartholomew Simpson
Guest
Posts: n/a
 
      06-24-2007
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 */
 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      06-24-2007
Bartholomew Simpson <(E-Mail Removed)> wrote:
# 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?
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      06-24-2007

"Bartholomew Simpson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2007
On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
<(E-Mail Removed)> wrote:

>
>"Bartholomew Simpson" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>>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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-24-2007
On Sun, 24 Jun 2007 14:20:42 +0100, Bartholomew Simpson
<(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      06-25-2007
Barry Schwarz wrote, On 25/06/07 00:50:
> On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
> <(E-Mail Removed)> wrote:
>
>> "Bartholomew Simpson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-25-2007
On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
<(E-Mail Removed)> wrote:

>Barry Schwarz wrote, On 25/06/07 00:50:
>> On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
>> <(E-Mail Removed)> wrote:
>>
>>> "Bartholomew Simpson" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-25-2007
Barry Schwarz <(E-Mail Removed)> writes:
> On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
> <(E-Mail Removed)> wrote:
>>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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-25-2007
On Mon, 25 Jun 2007 01:55:04 -0700, Keith Thompson <(E-Mail Removed)>
wrote:

>Barry Schwarz <(E-Mail Removed)> writes:
>> On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
>> <(E-Mail Removed)> wrote:
>>>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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      06-25-2007
In article <(E-Mail Removed)>,
SM Ryan <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 benn686@hotmail.com C++ 9 08-22-2007 12:13 AM
"LoadLibrary" of a 32 bit so with 64 bit java on a 64 bit machine markryde@gmail.com Java 3 01-19-2007 10:30 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments