【问题标题】:Not able to load Floating point Values using NEON Intrinsics无法使用 NEON Intrinsics 加载浮点值
【发布时间】:2013-01-13 21:00:11
【问题描述】:


无论如何,我都无法将浮点值加载到 NEON 128 位寄存器中! 我尝试了所有可能的方法来加载浮点数,但寄存器仍然为零(通过调试找到)。

/* neon_example.c - Neon intrinsics example program */
    #include <stdint.h>
    #include <stdio.h>
    #include <arm_neon.h>

    /* fill array with increasing integers beginning with 0 */
    void fill_array(float32_t *array, int size)
    {    int i;
        for (i = 0; i < size; i++)
        {
             array[i] = 2.0f;
             printf("%f",array[i]);
        }
    }

    /* return the sum of all elements in an array. This works by calculating 4 totals (one for each lane) and adding those at the end to get the final total */
    float sum_array(float32_t *array, int size)
    {    
         float32_t a,b,c,d,add;
         float32x4_t acc= vdupq_n_f32(0.0);
         for (; size != 0; size -= 4)
         {
           float32x4_t vec =vdupq_n_f32(0.0f);;
           vec=vld1q_f32(array);
    //The above operation does not load values??????????????????????
           array += 4;
           acc = vaddq_f32(acc,vec);
          }

         vst1q_lane_f32(&a,acc,0);
         vst1q_lane_f32(&a,acc,1);
         vst1q_lane_f32(&a,acc,2);
         vst1q_lane_f32(&a,acc,3);
         add=a+b+c+d;

          //return (int)vget_lane_s64(acc2, 0);
         return add;

    }
    /* main function */
    int main()
    {
          float32_t my_array[100];
          fill_array(my_array, 100);
          printf("Sum was %f\n", sum_array(my_array, 100));
          return 0;
    }

【问题讨论】:

  • 你不是想要 &b, &c, &d 吗? (vst1q_lane_f32) - vst1q_f32 会更快...(变成一个大小为 4 的浮点数组)
  • 在循环中你不需要在加载之前将 vec 归零。这里已经很晚了,但您的代码应该可以工作 - 特别是负载应该可以工作。为了保持一致性,我将 sum_array 的返回类型设为 float32_t。这是否在没有警告的情况下编译?也可以试试这个更简单的示例:gist.github.com/1064261

标签: c floating-point arm neon intrinsics


【解决方案1】:

我刚刚运行了您的代码,它正确地加载到了寄存器中。我在 Xcode 4.6 中使用 LLVM 4.2 构建了代码。

实施 Guy Sirton 的更改修复了错误并产生了更具可读性的函数:

float32_t sum_array(float32_t *array, int size)
{
    float32_t arr[4],add;
    float32x4_t acc= vdupq_n_f32(0.0);
    for (; size != 0; size -= 4)
    {
        float32x4_t vec=vld1q_f32(array);
        array += 4;
        acc = vaddq_f32(acc,vec);
    }

    vst1q_f32(arr, acc);

    add = arr[0] + arr[1] + arr[2] + arr[3];

    return add; 
}

【讨论】:

    猜你喜欢
    • 2012-07-11
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多