---
title: Sqr
description: "这些函数计算src指向的每个元素的平方，然后把数据存放到dst指向的内存中，计算过程为：。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC2/accel/kunpengaccel_hmpp_06_0092.html
sourcePath: /source/zh/kunpengboostkithistory/240RC2/accel/kunpengaccel_hmpp_06_0092.html
indexId: fc2492070d4d705bcfbb569f935e697830f39ce8e8333dde0b214dce4930ea9077
---
# Sqr

这些函数计算src指向的每个元素的平方，然后把数据存放到dst指向的内存中，计算过程为：。

使用in-place方式进行计算时，函数内部从srcDst中取出数据进行计算，获得的计算结果又写入到srcDst指向的内存区域中，即可表示为：。

计算一个整数的平方时，输出结果可能会超出对应数据类型的取值范围而饱和，为了获取更精确的结果，需要使用比例因子。

函数接口声明如下：

- 整型数的缩放操作：
  HmppResult HMPPS_Sqr_8u_S(const uint8_t* src, uint8_t * dst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_8u_IS(uint8_t * srcDst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_16u_S(const uint16_t* src, uint16_t * dst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_16u_IS(uint16_t *srcDst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_16sc_S(const Hmpp16sc * src, Hmpp16sc * dst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_16sc_IS(Hmpp16sc * srcDst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_16s_S(const int16_t *src, int16_t *dst, int32_t len, double scale);

  HmppResult HMPPS_Sqr_16s_IS(int16_t *srcDst, int32_t len, doublescale);

- 浮点数的缩放操作：
  HmppResult HMPPS_Sqr_32f(const float *src, float *dst, int32_t len);

  HmppResult HMPPS_Sqr_64f(const double *src, double *dst, int32_t len);

  HmppResult HMPPS_Sqr_32fc(const Hmpp32fc *src, Hmpp32fc *dst, int32_t len);

  HmppResult HMPPS_Sqr_64fc(const Hmpp64fc *src, Hmpp64fc *dst, int32_t len);

- 浮点数的原址操作：
  HmppResult HMPPS_Sqr_32f_I(float *srcDst, int32_t len);

  HmppResult HMPPS_Sqr_32fc_I(Hmpp32fc *srcDst, int32_t len);

  HmppResult HMPPS_Sqr_64f_I(double *srcDst, int32_t len);

  HmppResult HMPPS_Sqr_64fc_I(Hmpp64fc *srcDst, int32_t len);


#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| dst | 指向目的向量的指针。 | 非空 | 输入 |
| src | 指向源向量的指针。 | 非空 | 输入 |
| srcDst | 指向源数据目的地址的指针。 | 非空 | 输入/输出 |
| len | 向量长度。 | (0,INT\_MAX] | 输入 |
| scale | 缩放因数。 | (0,INF)且输入为2^n | 输入 |


#### 返回值

- 成功：返回HMPP_STS_NO_ERR。
- 失败：返回错误码。


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| HMPP\_STS\_NULL\_PTR\_ERR | src、dst、srcDst这几个入参中存在空指针。 |
| HMPP\_STS\_SIZE\_ERR | 参数len不允许小于或者等于0。 |
| HMPP\_STS\_SCALE\_ERR | scale不在(0,INF)范围内或输入为nan。 |


#### 示例

```
#define BUFFER_SIZE_T 10
void SqrExample(void)
{
float src[BUFFER_SIZE_T] = {1.64, 1.63, -1.09, 0.71, -3.20, -0.43, 0.41, -4.83, 5.36, -4.40};
float dst[BUFFER_SIZE_T];
(void)HMPPS_Zero_32f(dst, BUFFER_SIZE_T); // 数组初始化，将dst所有元素初始化为0。
HmppResult result = HMPPS_Sqr_32f(src, dst, BUFFER_SIZE_T);
printf("result = %d\n", result);
if (result != HMPP_STS_NO_ERR) {
return;
}
printf("dst =");
for (int i = 0; i < BUFFER_SIZE_T; i++) {
printf(" %f", dst[i]);
}
printf("\n");
}
```

运行结果：

```
result = 0
dst = 2.689600 2.656900 1.188100 0.504100 10.240001 0.184900 0.168100 23.328899 28.729601 19.360001
```
