Possible to pack smaller data types into larger ones?

This is a discussion on Possible to pack smaller data types into larger ones? within the C Programming forums, part of the General Programming Boards category; Hi, I had a thought the other day, about packing two or more data type's into another larger data type. ...

  1. #1
    Heraclitus
    Guest

    Possible to pack smaller data types into larger ones?

    Hi,
    I had a thought the other day, about packing two or more data type's into another larger data type. Then perform some addition/subraction operations on the larger data type, so you basically get two operations for the price of one.

    Here is an example of what i mean:

    short int a,b,c,d;
    unsigned short int ab,cd;
    long int e,f,result;

    /* pack a & b into e */
    /* pack c & d into f */

    result = e + f;

    /* unpack e and f, store in ab and cd */

    Would ab now hold a+b, and cd hold c+d?

    Say two chars were packed into a short...
    12345678 | 12345678
    ----------------------------
    00000001 | 00000001 dec 513 OR 1 and 1 as 2 chars
    00000001 | 00000001 dec 513 OR 1 and 1 as 2 chars
    *PERFORM*ADDITION*
    00000010 | 00000010 dec 1026 OR 2 and 2 as two chars

    I take it that the above would save an addition operation, instead of adding 2 chars to one another, then adding another 2 chars to one another.
    Assuming that all this is possible, I would guess that there would be some sort of overhead involved in packing and unpacking, but how much?

    I am quite new to C, so if I appear to have been on the crack pipe, please say.
    I am just interested in the proof of concept of doing this, or ideally, any real world examples of it.

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    This is possible, and it might be worth the trouble if you do a significant number of operations with the packed data, so long as you don't overflow.

    I don't know the MMX instructions by heart, but I've flipped through the manual for them and most of them seem to be designed to deal with packed integers of different sizes. If intel thinks it's worthwhile, it probably is :l
    .sect signature

  3. #3
    Heraclitus
    Guest
    <kicks himself>
    Well, I found that union's are able to do this very easily, so I shall experiment with them for a while.

    Anybody think of any other ways?

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    If you are on the Intel/AMD plattform, have a look at the following webpages:

    http://gcc.gnu.org/onlinedocs/gcc/X8...Functions.html
    http://gcc.gnu.org/onlinedocs/gcc/Ve...r%20Extensions

    example source code for SSE instructions:
    Code:
    typedef float v4sf __attribute__ ((mode(V4SF)));
    
    v4sf v1, v2;
    float data[8], tmp[4]; 
    /* fill array data */
    ...
    v1 = __builtin_ia32_loadaps(data);
    v2 = __builtin_ia32_loadaps(&data[4]);
    v1 = __builtin_ia32_mulps(v1, v2);
    __builtin_ia32_storeaps(tmp, v1);
    Now, the content of the array tmp is as follows:
    tmp[0] = data[0] * data[4];
    tmp[1] = data[1] * data[5];
    tmp[1] = data[2] * data[6];
    tmp[1] = data[3] * data[7];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21