0

I'm trying to fill an array of unsigned integers (32-bit) with short integers from another array (size is not fixed). I put the short ints one after the other in the output array which contains random values.

Here is my code:

#define K_LENGTH (37) // Arbitrary

void computeInput(unsigned short* input) {
    unsigned int output[1000];
    unsigned int i, j, gap;

    j = 0;
    gap = 0;
    output[0] = 0;

    for (i = 0; i < K_LENGTH; i++) {
        output[j] |= (input[i] << gap);

        if (gap) {
            gap = 0;
            j += 1;
            output[j] = 0;
        } else {
            gap = 16;
        }
    }

    // The rest of output array is set to 0
    for (i = j, i < 1000, i++) {
        output[i] = 0;
    }

    // Other stuff
}

First, the part of algorithm checking the gap value is quite ugly but I don't how to perform this efficiently. Second, I don't know how to ensure that the random values are erased/replaced by the input values.

Should I set the entire output array to 0 before computing input values? It seems inefficient.

5
  • 1
    Why not to make array of short as the destination instead? Or union it with array of integers, if you really want the integer access.
    – Eugene Sh.
    Jul 10, 2015 at 16:24
  • @EugeneSh. I put some 32 bits float/integer after so I have to work with a 32 bits array Jul 10, 2015 at 16:28
  • 2
    @user2205092 just needs to be fixed in your post. Regardless. A short set of sample arrays and their values you have going in and desired going out would go a long way in picturing what your algorithm is trying to do.
    – WhozCraig
    Jul 10, 2015 at 16:29
  • I don't see a short [] in the posted code. And assigning signed to unsigned integers is problematic for negative values. Jul 10, 2015 at 16:42
  • @Olaf Corrected, Sorry for that I work with unsigned values. Jul 10, 2015 at 16:48

2 Answers 2

1

I assume you are trying to merge 16 bit unsigned integers pair-wise into an array of 32 bit unsigned integers:

#include <stdint.h>

void mergeShorts(size_t out_len, uint32_t output[out_len],
            size_t in_len, const uint16_t input[in_len] ) {

    size_t i;

    // output must have enough entries.
    assert( (in_len < SIZE_MAX) && ((in_len + 1) / 2 <= out_len) );

    for ( i = 0 ; i < in_len / 2 ; i += 1 )
        output[i] = ((uint32_t)input[i / 2] << 16) | (input[i / 2 + 1];

    // transfer last (odd) entry
    if ( (i * 2) < in_len )
        output[i++] = (uint32_t)input[in_len - 1] << 16;

    // zero the rest of the array
    for ( ; i < out_len ; i++ )
        output[i] = 0;
}

Using stdint.h types guarantees the proper sizes for the elements of both arrays and is likely what you actually want.

If you only have fixed sizes, replace out_len and in_len by constants. I used output as an argument, as the rest of code is missing. If that is not required, just make it a local again (the name output actually implies it is expected by the caller).

Normally, one would pack this into a function as I did and call that function from computeInput with the appropriate arguments:

mergeShorts(1000, output, K_LENGTH, input);
4
  • Thank you for the answer, but I can't ensure that the size will be an even number... Jul 10, 2015 at 17:08
  • @user2205092: I edited. Just check for boundary cases, these are notoriously critical. Jul 10, 2015 at 17:18
  • Pedantic: assert((in_len < SIZE_MAX) && ((in_len + 1) / 2 <= out_len)); or assert( (in_len/2 + (in_len%2 + 1)/2) <= out_len ) or etc. :-) Nice answer. Dec 21, 2016 at 3:33
  • @chux: Thanks, good point. I added the first version. I'm too much used not to be able to go to the limits. Dec 21, 2016 at 5:41
0

It seems that this is a work for Mr. memcpy.
I will try to handle your problem by using memcpy.
It's pretty sure that the copy will be efficient, since it copies the bits of one object into another, without any arithmetic at all, just bitwise.

Anyway, I can see that you are assuming a lot of non-portable issues.
For example, that unsigned int is 32 bits, without any padding bits.
The standard only ensures that unsigned int holds a minimun of 16 bits.
Maybe it would be a good idea to use the unsigned integer types uint_least32_t or uint_fast32_t defined in stdint.h, that have at least 32 bits always. If you are lucky, the type uint32_t will also be defined there, which has exactly 32 bits.

5
  • OP packs the 16 bit values big endian into the 32 bit types. memcpy will not work. That is a good example why you should not use memcpy between different types. Note that there are no padding bits between the elements in an array. Otherwise, malloc(sizeof(array_element) * num_elements) would not work (it is guaranted to work, however). Jul 11, 2015 at 15:32
  • @Olaf: Between elements in an array there are not padding bytes, which is not the same as padding bits. On the other hand, it seems that the OP is using an int array only for storing shorts. How do you know that the packing must be big-endian?
    – pablo1977
    Jul 11, 2015 at 15:48
  • Sorry, I somehow missread "bits" for "bytes" (yes, I know I also used "bits"). For the endianess: Hmm, his code seems to be even more complicated than I actually thought. You're right, It is LE packing (sorry, I was confused by the uncommon code). OTOH, memcpy does depend on full LE architecture and would not work with BE, making the code implementation defined at best. You also forgot about the zero-fill part. Jul 11, 2015 at 16:18
  • Since the purpose of the OP seems only to pack information, it's not important the BE/LE problem. When he needs the numbers, he will have to unpack the shorts anyway. The zero-fill thing, it's true. But I figure out that the OP can do it for himself. If the OP understand how memcpy works, he can decide if can use it or not, and how to fill with zeroes is for his own: it's not a hard issue for a developer. My answer will be the same, without changes.
    – pablo1977
    Jul 11, 2015 at 16:25
  • I strongly disagree. There are quite some applications to pack types, expecially as he seems to expect 16 and 32 types. Just think of Cortex-M4 (and other CPUs) SIMD instructions which operate on half-word pairs. However, it is all speculative, so it makes little sense to provide a restricted solution until OP clarifies. Jul 11, 2015 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.