You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

arithmetic_self.c 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <string.h>
  17. #include <math.h>
  18. #include "nnacl/fp32/arithmetic_self.h"
  19. // abs:
  20. int ElementAbs(float *input, float *output, int element_size) {
  21. for (int i = 0; i < element_size; i++) {
  22. output[i] = fabsf(input[i]);
  23. }
  24. return NNACL_OK;
  25. }
  26. // cos:
  27. int ElementCos(float *input, float *output, int element_size) {
  28. for (int i = 0; i < element_size; i++) {
  29. output[i] = cosf(input[i]);
  30. }
  31. return NNACL_OK;
  32. }
  33. // log:
  34. int ElementLog(float *input, float *output, int element_size) {
  35. for (int i = 0; i < element_size; i++) {
  36. if (input[i] <= 0) {
  37. return NNACL_ERRCODE_LOG_NEGATIVE_OR_ZERO;
  38. }
  39. output[i] = logf(input[i]);
  40. }
  41. return NNACL_OK;
  42. }
  43. // Square
  44. int ElementSquare(float *input, float *output, int element_size) {
  45. for (int i = 0; i < element_size; i++) {
  46. output[i] = input[i] * input[i];
  47. }
  48. return NNACL_OK;
  49. }
  50. // Sqrt
  51. int ElementSqrt(float *input, float *output, int element_size) {
  52. for (int i = 0; i < element_size; i++) {
  53. if (input[i] < 0) {
  54. return NNACL_ERRCODE_SQRT_NEGATIVE;
  55. }
  56. output[i] = sqrtf(input[i]);
  57. }
  58. return NNACL_OK;
  59. }
  60. // rsqrt
  61. int ElementRsqrt(float *input, float *output, int element_size) {
  62. for (int i = 0; i < element_size; i++) {
  63. if (input[i] <= 0) {
  64. return NNACL_ERRCODE_RSQRT_NEGATIVE_OR_ZERO;
  65. }
  66. output[i] = 1.f / sqrtf(input[i]);
  67. }
  68. return NNACL_OK;
  69. }
  70. // sin:
  71. int ElementSin(float *input, float *output, int element_size) {
  72. for (int i = 0; i < element_size; i++) {
  73. output[i] = sinf(input[i]);
  74. }
  75. return NNACL_OK;
  76. }
  77. // logical_not:
  78. int ElementLogicalNot(float *input, float *output, int element_size) {
  79. for (int i = 0; i < element_size; i++) {
  80. output[i] = (float)(!((bool)(input[i])));
  81. }
  82. return NNACL_OK;
  83. }
  84. // round:
  85. int ElementRound(float *input, float *output, int element_size) {
  86. for (int i = 0; i < element_size; i++) {
  87. output[i] = round(input[i]);
  88. }
  89. return NNACL_OK;
  90. }
  91. // floor:
  92. int ElementFloor(float *input, float *output, int element_size) {
  93. for (int i = 0; i < element_size; i++) {
  94. output[i] = floorf(input[i]);
  95. }
  96. return NNACL_OK;
  97. }
  98. int ElementCeil(float *input, float *output, int number) {
  99. for (int i = 0; i < number; ++i) {
  100. output[i] = ceil(input[i]);
  101. }
  102. return NNACL_OK;
  103. }
  104. int ElementNegative(float *input, float *output, int element_size) {
  105. for (int i = 0; i < element_size; ++i) {
  106. output[i] = -input[i];
  107. }
  108. return NNACL_OK;
  109. }