Bit twiddling/manipulation question

Discussion in 'C Programming' started by Bartholomew Simpson, Jun 24, 2007.

  1. 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 */
     
    Bartholomew Simpson, Jun 24, 2007
    #1
    1. Advertising

  2. Bartholomew Simpson

    SM Ryan Guest

    Bartholomew Simpson <> 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?
     
    SM Ryan, Jun 24, 2007
    #2
    1. Advertising

  3. "Bartholomew Simpson" <> wrote in message
    news:...
    >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
     
    Malcolm McLean, Jun 25, 2007
    #3
  4. On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
    <> wrote:

    >
    >"Bartholomew Simpson" <> wrote in message
    >news:...
    >>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
     
    Barry Schwarz, Jun 25, 2007
    #4
  5. On Sun, 24 Jun 2007 14:20:42 +0100, Bartholomew Simpson
    <> 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
     
    Barry Schwarz, Jun 25, 2007
    #5
  6. Bartholomew Simpson

    Flash Gordon Guest

    Barry Schwarz wrote, On 25/06/07 00:50:
    > On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
    > <> wrote:
    >
    >> "Bartholomew Simpson" <> wrote in message
    >> news:...
    >>> 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
     
    Flash Gordon, Jun 25, 2007
    #6
  7. On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
    <> wrote:

    >Barry Schwarz wrote, On 25/06/07 00:50:
    >> On Mon, 25 Jun 2007 00:02:11 +0100, "Malcolm McLean"
    >> <> wrote:
    >>
    >>> "Bartholomew Simpson" <> wrote in message
    >>> news:...
    >>>> 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
     
    Barry Schwarz, Jun 25, 2007
    #7
  8. Barry Schwarz <> writes:
    > On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
    > <> 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.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"
     
    Keith Thompson, Jun 25, 2007
    #8
  9. On Mon, 25 Jun 2007 01:55:04 -0700, Keith Thompson <>
    wrote:

    >Barry Schwarz <> writes:
    >> On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
    >> <> 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
     
    Barry Schwarz, Jun 25, 2007
    #9
  10. In article <>,
    SM Ryan <> 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
     
    Walter Roberson, Jun 25, 2007
    #10
  11. Bartholomew Simpson

    Flash Gordon Guest

    Barry Schwarz wrote, On 25/06/07 18:11:
    > On Mon, 25 Jun 2007 01:55:04 -0700, Keith Thompson <>
    > wrote:
    >
    >> Barry Schwarz <> writes:
    >>> On Mon, 25 Jun 2007 08:12:59 +0100, Flash Gordon
    >>> <> 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.


    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
     
    Flash Gordon, Jun 25, 2007
    #11
  12. -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) <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"
     
    Keith Thompson, Jun 26, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duncan Lyall

    bit twiddling in Java

    Duncan Lyall, Jan 16, 2005, in forum: Java
    Replies:
    2
    Views:
    1,387
    Duncan Lyall
    Jan 18, 2005
  2. matobinder

    bit twiddling in C++

    matobinder, Apr 26, 2004, in forum: C++
    Replies:
    5
    Views:
    1,456
    Bill Seurer
    Apr 27, 2004
  3. Derek

    Bit-twiddling with floats

    Derek, Oct 11, 2004, in forum: C++
    Replies:
    3
    Views:
    528
    Keith H Duggar
    Oct 12, 2004
  4. grid

    Bit twiddling

    grid, Jun 1, 2005, in forum: C Programming
    Replies:
    40
    Views:
    1,349
    Dave Thompson
    Jun 13, 2005
  5. Sal
    Replies:
    16
    Views:
    200
    Ilya Zakharevich
    May 25, 2010
Loading...

Share This Page