简体   繁体   中英

Passing an array of structure elements

I am doing some vector computations using the NE10 library . The library has a type for a complex number which is a struct like this:

typedef struct
{
    ne10_float32_t r;
    ne10_float32_t i;
} ne10_fft_cpx_float32_t;

I would like to have a function, which takes an array of these structs (1D array of complex numbers) as an argument and performs a vector computation for only the r or i elements of the array. Here is an example of such function to get an idea:

void multiple_real_part_by_two(ne10_fft_cpx_float32_t* output, 
    ne10_fft_cpx_float32_t* input, ne10_uint32_t array_length)
{
    ne10_mulc_float_c (&output->r, &input->r, 2.0, array_length)
}

So I would like the output array to be like the input array, but each of the r elements should be multiplied by two. The problem is, the way the function is written above does not work, and leads to a segmentation fault. I think the problem is in how I am trying to pass an array of the r elements to the ne10_mulc_float_c() function.

The ne10_mulc_float_c() function takes as arguments pointers to two arrays of type ne10_float32_t of size array_length . The elements of the input array are multiplied by the number passed as the third argument, and the result is stored in the output array. The documentation can be found here.

Is there a way I could do this? I know I could just do this in a for loop

for (int i = 0; i < array_length; i++) {
    output[i].r = input[i].r * 2.0
}

but I don't want to do this since performance is critical, which is why I am trying to use the vector operations provided by NE10 in the first place.

The problem is that you give ne10_mulc_float_c() arguments of the right type, but which do not match the assumptions.

According to the library's documentation page , the function is defined as:

ne10_result_t   ne10_mulc_float_c (ne10_float32_t *dst, ne10_float32_t *src, 
                                  const ne10_float32_t cst, ne10_uint32_t count)

with the following arguments:

[out]   dst Pointer to the destination array
[in]    src Pointer to the source array
[in]    cst The constant to multiply by
[in]    count   The number of scalar values to be processed

This means that the function assumes that dst and src are are pointers to arrays of count CONSECUTIVE floating point numbers.

Unfortunately, the is is not the case with the arguments that you pass: &output->r and &input->r are both pointers to one single float. So as soon at this function tries to access the second item in the array it expects, it code goes beyond the array's real bounds and this is UB. This is why it doesn't work and you get the segmentation fault.

Your for-loop is just fine. Don't forget:

Premature optimization is the root of all evil .
-- Donald Knuth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM