• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "nnacl/fp16/power_fp16.h"
18 #include "nnacl/errorcode.h"
19 
20 #if defined(ENABLE_NEON)
OptimizedPowerSimdFp16(float16x8_t x,const void * exponent)21 float16x8_t OptimizedPowerSimdFp16(float16x8_t x, const void *exponent) {
22   int tmp = (int)(*(float16_t *)exponent);
23   int exp = abs(tmp);
24   float16x8_t result = vmovq_n_f16(1.0f);
25   while (exp) {
26     if (exp % 2) {
27       result *= x;
28     }
29     x *= x;
30     exp = exp / 2;
31   }
32   if (tmp >= 0) {
33     return result;
34   }
35   return 1 / result;
36 }
37 #endif
38 
OptimizedPowerScalarFp16(float16_t x,const void * exponent)39 float16_t OptimizedPowerScalarFp16(float16_t x, const void *exponent) {
40   int tmp = *(float16_t *)exponent;
41   int exp = abs(tmp);
42   float16_t result = 1;
43   while (exp) {
44     if (exp % 2) {
45       result *= x;
46     }
47     x *= x;
48     exp = exp / 2;
49   }
50   return tmp >= 0 ? result : 1 / result;
51 }
52 
PowerBroadCastFp16(const float16_t * input,const float16_t * exponent,float16_t * output,int len,float scale,float shift)53 void PowerBroadCastFp16(const float16_t *input, const float16_t *exponent, float16_t *output, int len, float scale,
54                         float shift) {
55   PowerScalarFunFp16 PowerScalarFunFp16_ = NULL;
56 #if defined(ENABLE_NEON)
57   PowerSimdFunFp16 PowerSimdFunFp16_ = NULL;
58 #endif
59 
60   if (CheckIntegerFp16(*exponent)) {
61 #if defined(ENABLE_NEON)
62     PowerSimdFunFp16_ = OptimizedPowerSimdFp16;
63 #endif
64     PowerScalarFunFp16_ = OptimizedPowerScalarFp16;
65   } else {
66 #if defined(ENABLE_NEON)
67     PowerSimdFunFp16_ = StdPowerSimdFp16;
68 #endif
69     PowerScalarFunFp16_ = StdPowerScalarFp16;
70   }
71   int i = 0;
72 #ifdef ENABLE_NEON
73   int len_c8 = DOWN_ROUND(len, C8NUM);
74   float16x8_t scale_8 = vmovq_n_f16(scale);
75   float16x8_t shift_8 = vmovq_n_f16(shift);
76   for (; i < len_c8; i += C8NUM) {
77     float16x8_t result = PowerSimdFunFp16_(scale_8 * vld1q_f16(input + i) + shift_8, exponent);
78     vst1q_f16(output + i, result);
79   }
80 #endif
81   for (; i < len; ++i) {
82     output[i] = PowerScalarFunFp16_(scale * input[i] + shift, exponent);
83   }
84 }
85 
PowerSingleFp16(const float16_t * input,const float16_t * exponent,float16_t * output,int len,float scale,float shift)86 void PowerSingleFp16(const float16_t *input, const float16_t *exponent, float16_t *output, int len, float scale,
87                      float shift) {
88   int i = 0;
89   PowerScalarFunFp16 PowerScalarFunFp16_ = NULL;
90 #ifdef ENABLE_NEON
91   int len_c8 = DOWN_ROUND(len, C8NUM);
92   float16x8_t scale_8 = vmovq_n_f16(scale);
93   float16x8_t shift_8 = vmovq_n_f16(shift);
94   for (; i < len_c8; i += C8NUM) {
95     float16x8_t tmp_8 = scale_8 * vld1q_f16(input + i) + shift_8;
96     for (int j = 0; j < 8; ++j) {
97       PowerScalarFunFp16_ = CheckIntegerFp16(exponent[i + j]) ? OptimizedPowerScalarFp16 : StdPowerScalarFp16;
98       output[i + j] = PowerScalarFunFp16_(tmp_8[j], exponent + i + j);
99     }
100   }
101 #endif
102   for (; i < len; ++i) {
103     PowerScalarFunFp16_ = CheckIntegerFp16(exponent[i]) ? OptimizedPowerScalarFp16 : StdPowerScalarFp16;
104     output[i] = PowerScalarFunFp16_(scale * input[i] + shift, exponent + i);
105   }
106 }
107 
PowerFp16(const float16_t * input,const float16_t * exponent,float16_t * output,int len,float scale,float shift,bool broadcast)108 int PowerFp16(const float16_t *input, const float16_t *exponent, float16_t *output, int len, float scale, float shift,
109               bool broadcast) {
110   if (input == NULL || exponent == NULL || output == NULL) {
111     return NNACL_NULL_PTR;
112   }
113   PowerFunFp16 PowerFunFp16_ = NULL;
114   PowerFunFp16_ = broadcast ? PowerBroadCastFp16 : PowerSingleFp16;
115   PowerFunFp16_(input, exponent, output, len, scale, shift);
116   return NNACL_OK;
117 }
118