https://gcc.gnu.org/onlinedocs/gcc/MIPS-SIMD-Architecture-Built-in-Functions.html 中列出的 MIPS SIMD 的 GNU 内置函数
我测试了v4f32 __builtin_msa_fsqrt_w (v4f32);
如下。
#define _GNU_SOURCE
#include <msa.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define ALIGN16 __attribute__((aligned(16)))
ALIGN16 uint32_t a[] = {64, 128, 256, 512};
ALIGN16 uint32_t r[] = {64, 128, 256, 512};
ALIGN16 uint32_t sr[] = {64, 128, 256, 512};
static int verification_test(void)
{
int i = 0;
v4i32 va, fr;
for (i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
sr[i] = sqrt(a[i]);
}
// Get SQRT with MSA builtin functions
va = __builtin_msa_ld_w(a, 0);
fr = (v4i32)__builtin_msa_fsqrt_w((v4f32)va);
__builtin_msa_st_w(fr, r, 0); // Save result in fr to array of r.
for (i = 0; i < sizeof(r)/sizeof(r[0]); i++) {
printf("%d: %f\n", i, (double)sr[i]);
printf("%d: %f\n", i, (double)r[i]);
}
return 0;
}
int main()
{
verification_test();
return 0;
}
计算了4个数字的sqrt,但是我发现内置函数的结果与sqrt()
得到的结果不同,如下。
0: 8.000000
0: 464848115.000000
1: 11.000000
1: 469762048.000000
2: 16.000000
2: 473236723.000000
3: 22.000000
3: 478150656.000000
那么我的代码有什么问题,如何修复呢?
(v4i32)__builtin_msa_fsqrt_w((v4f32)va)
到
builtin_msa_ftint_u_w(__builtin_msa_fsqrt_w(__builtin_msa_ffint_u_w(va)))