简体   繁体   English

传递结构元素数组

[英]Passing an array of structure elements

I am doing some vector computations using the NE10 library . 我正在使用NE10库进行一些矢量计算。 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. 我想要一个函数,该函数将这些结构的数组(复数的一维数组)作为参数,并仅对数组的r或i元素执行矢量计算。 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. 所以我希望输出数组像输入数组一样,但是r元素中的每一个都应该乘以2。 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. 我认为问题在于我如何尝试将r元素的数组传递给ne10_mulc_float_c()函数。

The ne10_mulc_float_c() function takes as arguments pointers to two arrays of type ne10_float32_t of size array_length . ne10_mulc_float_c()函数将指向大小为array_length两个ne10_float32_t类型的数组的指针作为参数。 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循环中执行此操作

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. 但由于性能至关重要,因此我不想这样做,这就是为什么我首先尝试使用NE10提供的矢量运算的原因。

The problem is that you give ne10_mulc_float_c() arguments of the right type, but which do not match the assumptions. 问题是您提供了正确类型的ne10_mulc_float_c()参数,但与假设不符。

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. 这意味着该函数假定dst和src是指向count CONSECUTIVE浮点数数组的指针。

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. 不幸的是,您传递的参数并非如此: &output->r&input->r都是指向单个浮点数的指针。 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. 因此,一旦此函数尝试访问它期望的数组中的第二项,它的代码就会超出数组的实际范围,这就是UB。 This is why it doesn't work and you get the segmentation fault. 这就是为什么它不起作用,并且您得到细分错误的原因。

Your for-loop is just fine. 您的for循环就很好了。 Don't forget: 不要忘记:

Premature optimization is the root of all evil . 过早的优化是万恶之源
-- Donald Knuth - 唐纳德·努斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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