• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 #include <math.h>
17 #include "nnacl/fp16/arithmetic_self_fp16.h"
18 
ElementAbsFp16(const float16_t * input,float16_t * output,int element_size)19 int ElementAbsFp16(const float16_t *input, float16_t *output, int element_size) {
20   for (int i = 0; i < element_size; i++) {
21     output[i] = fabsf(input[i]);
22   }
23   return NNACL_OK;
24 }
25 
ElementCosFp16(const float16_t * input,float16_t * output,int element_size)26 int ElementCosFp16(const float16_t *input, float16_t *output, int element_size) {
27   for (int i = 0; i < element_size; i++) {
28     output[i] = cosf(input[i]);
29   }
30   return NNACL_OK;
31 }
32 
ElementLogFp16(const float16_t * input,float16_t * output,int element_size)33 int ElementLogFp16(const float16_t *input, float16_t *output, int element_size) {
34   for (int i = 0; i < element_size; i++) {
35     if (input[i] <= 0) {
36       return NNACL_ERRCODE_LOG_NEGATIVE_OR_ZERO;
37     }
38     output[i] = logf(input[i]);
39   }
40   return NNACL_OK;
41 }
42 
ElementSquareFp16(const float16_t * input,float16_t * output,int element_size)43 int ElementSquareFp16(const float16_t *input, float16_t *output, int element_size) {
44   for (int i = 0; i < element_size; i++) {
45     output[i] = input[i] * input[i];
46   }
47   return NNACL_OK;
48 }
49 
ElementSqrtFp16(const float16_t * input,float16_t * output,int element_size)50 int ElementSqrtFp16(const float16_t *input, float16_t *output, int element_size) {
51   for (int i = 0; i < element_size; i++) {
52     if (input[i] < 0) {
53       return NNACL_ERRCODE_SQRT_NEGATIVE;
54     }
55     output[i] = sqrtf(input[i]);
56   }
57   return NNACL_OK;
58 }
59 
ElementRsqrtFp16(const float16_t * input,float16_t * output,int element_size)60 int ElementRsqrtFp16(const float16_t *input, float16_t *output, int element_size) {
61   for (int i = 0; i < element_size; i++) {
62     output[i] = 1.f / sqrtf(input[i]);
63   }
64   return NNACL_OK;
65 }
66 
ElementSinFp16(const float16_t * input,float16_t * output,int element_size)67 int ElementSinFp16(const float16_t *input, float16_t *output, int element_size) {
68   for (int i = 0; i < element_size; i++) {
69     output[i] = sinf(input[i]);
70   }
71   return NNACL_OK;
72 }
73 
ElementLogicalNotFp16(const float16_t * input,float16_t * output,int element_size)74 int ElementLogicalNotFp16(const float16_t *input, float16_t *output, int element_size) {
75   for (int i = 0; i < element_size; i++) {
76     output[i] = (float)(!((bool)(input[i])));
77   }
78   return NNACL_OK;
79 }
80 
ElementRoundFp16(const float16_t * input,float16_t * output,int element_size)81 int ElementRoundFp16(const float16_t *input, float16_t *output, int element_size) {
82   for (int i = 0; i < element_size; i++) {
83     output[i] = roundf(input[i]);
84   }
85   return NNACL_OK;
86 }
87 
ElementFloorFp16(const float16_t * input,float16_t * output,int element_size)88 int ElementFloorFp16(const float16_t *input, float16_t *output, int element_size) {
89   for (int i = 0; i < element_size; i++) {
90     output[i] = floorf(input[i]);
91   }
92   return NNACL_OK;
93 }
94 
ElementCeilFp16(const float16_t * input,float16_t * output,int number)95 int ElementCeilFp16(const float16_t *input, float16_t *output, int number) {
96   for (int i = 0; i < number; ++i) {
97     output[i] = ceilf(input[i]);
98   }
99   return NNACL_OK;
100 }
101 
ElementNegativeFp16(const float16_t * input,float16_t * output,int element_size)102 int ElementNegativeFp16(const float16_t *input, float16_t *output, int element_size) {
103   for (int i = 0; i < element_size; ++i) {
104     output[i] = -input[i];
105   }
106   return NNACL_OK;
107 }
108 
ElementReciprocalFp16(const float16_t * input,float16_t * output,int element_size)109 int ElementReciprocalFp16(const float16_t *input, float16_t *output, int element_size) {
110   for (int i = 0; i < element_size; ++i) {
111     if (input[i] == 0.0f) {
112       return NNACL_ERR;
113     }
114     output[i] = 1.f / input[i];
115   }
116   return NNACL_OK;
117 }
118 
ElementErfFp16(const float16_t * input,float16_t * output,int element_size)119 int ElementErfFp16(const float16_t *input, float16_t *output, int element_size) {
120   for (int i = 0; i < element_size; i++) {
121     output[i] = erff(input[i]);
122   }
123   return NNACL_OK;
124 }
125