NEMath.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2016, 2019 ARM Limited.
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef __ARM_COMPUTE_NEMATH_H__
  25. #define __ARM_COMPUTE_NEMATH_H__
  26. #if defined(ARM_MATH_NEON)
  27. /** Calculate floor of a vector.
  28. *
  29. * @param[in] val Input vector value in F32 format.
  30. *
  31. * @return The calculated floor vector.
  32. */
  33. static inline float32x4_t vfloorq_f32(float32x4_t val);
  34. /** Calculate inverse square root.
  35. *
  36. * @param[in] x Input value.
  37. *
  38. * @return The calculated inverse square root.
  39. */
  40. static inline float32x2_t vinvsqrt_f32(float32x2_t x);
  41. /** Calculate inverse square root.
  42. *
  43. * @param[in] x Input value.
  44. *
  45. * @return The calculated inverse square root.
  46. */
  47. static inline float32x4_t vinvsqrtq_f32(float32x4_t x);
  48. /** Calculate reciprocal.
  49. *
  50. * @param[in] x Input value.
  51. *
  52. * @return The calculated reciprocal.
  53. */
  54. static inline float32x2_t vinv_f32(float32x2_t x);
  55. /** Calculate reciprocal.
  56. *
  57. * @param[in] x Input value.
  58. *
  59. * @return The calculated reciprocal.
  60. */
  61. static inline float32x4_t vinvq_f32(float32x4_t x);
  62. /** Perform a 7th degree polynomial approximation using Estrin's method.
  63. *
  64. * @param[in] x Input vector value in F32 format.
  65. * @param[in] coeffs Polynomial coefficients table. (array of flattened float32x4_t vectors)
  66. *
  67. * @return The calculated approximation.
  68. */
  69. static inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const float32_t *coeffs);
  70. /** Calculate exponential
  71. *
  72. * @param[in] x Input vector value in F32 format.
  73. *
  74. * @return The calculated exponent.
  75. */
  76. static inline float32x4_t vexpq_f32(float32x4_t x);
  77. /** Calculate logarithm
  78. *
  79. * @param[in] x Input vector value in F32 format.
  80. *
  81. * @return The calculated logarithm.
  82. */
  83. static inline float32x4_t vlogq_f32(float32x4_t x);
  84. /** Calculate hyperbolic tangent.
  85. *
  86. * tanh(x) = (e^2x - 1)/(e^2x + 1)
  87. *
  88. * @note We clamp x to [-5,5] to avoid overflowing issues.
  89. *
  90. * @param[in] val Input vector value in F32 format.
  91. *
  92. * @return The calculated Hyperbolic Tangent.
  93. */
  94. static inline float32x4_t vtanhq_f32(float32x4_t val);
  95. /** Calculate n power of a number.
  96. *
  97. * pow(x,n) = e^(n*log(x))
  98. *
  99. * @param[in] val Input vector value in F32 format.
  100. * @param[in] n Powers to raise the input to.
  101. *
  102. * @return The calculated power.
  103. */
  104. static inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n);
  105. #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  106. /** Calculate hyperbolic tangent.
  107. *
  108. * tanh(x) = (e^2x - 1)/(e^2x + 1)
  109. *
  110. * @note We clamp x to [-5,5] to avoid overflowing issues.
  111. *
  112. * @param[in] val Input vector value in F32 format.
  113. *
  114. * @return The calculated Hyperbolic Tangent.
  115. */
  116. static inline float16x8_t vtanhq_f16(float16x8_t val);
  117. /** Calculate reciprocal.
  118. *
  119. * @param[in] x Input value.
  120. *
  121. * @return The calculated reciprocal.
  122. */
  123. static inline float16x4_t vinv_f16(float16x4_t x);
  124. /** Calculate reciprocal.
  125. *
  126. * @param[in] x Input value.
  127. *
  128. * @return The calculated reciprocal.
  129. */
  130. static inline float16x8_t vinvq_f16(float16x8_t x);
  131. /** Calculate inverse square root.
  132. *
  133. * @param[in] x Input value.
  134. *
  135. * @return The calculated inverse square root.
  136. */
  137. static inline float16x4_t vinvsqrt_f16(float16x4_t x);
  138. /** Calculate inverse square root.
  139. *
  140. * @param[in] x Input value.
  141. *
  142. * @return The calculated inverse square root.
  143. */
  144. static inline float16x8_t vinvsqrtq_f16(float16x8_t x);
  145. /** Calculate exponential
  146. *
  147. * @param[in] x Input vector value in F16 format.
  148. *
  149. * @return The calculated exponent.
  150. */
  151. static inline float16x8_t vexpq_f16(float16x8_t x);
  152. /** Calculate n power of a number.
  153. *
  154. * pow(x,n) = e^(n*log(x))
  155. *
  156. * @param[in] val Input vector value in F16 format.
  157. * @param[in] n Powers to raise the input to.
  158. *
  159. * @return The calculated power.
  160. */
  161. static inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n);
  162. #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
  163. /** Exponent polynomial coefficients */
  164. extern const float32_t exp_tab[4*8];
  165. /** Logarithm polynomial coefficients */
  166. extern const float32_t log_tab[4*8];
  167. #ifndef DOXYGEN_SKIP_THIS
  168. inline float32x4_t vfloorq_f32(float32x4_t val)
  169. {
  170. static const float32_t CONST_1[4] = {1.f,1.f,1.f,1.f};
  171. const int32x4_t z = vcvtq_s32_f32(val);
  172. const float32x4_t r = vcvtq_f32_s32(z);
  173. return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, vld1q_f32(CONST_1)), r);
  174. }
  175. inline float32x2_t vinvsqrt_f32(float32x2_t x)
  176. {
  177. float32x2_t sqrt_reciprocal = vrsqrte_f32(x);
  178. sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  179. sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  180. return sqrt_reciprocal;
  181. }
  182. inline float32x4_t vinvsqrtq_f32(float32x4_t x)
  183. {
  184. float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
  185. sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  186. sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  187. return sqrt_reciprocal;
  188. }
  189. inline float32x2_t vinv_f32(float32x2_t x)
  190. {
  191. float32x2_t recip = vrecpe_f32(x);
  192. recip = vmul_f32(vrecps_f32(x, recip), recip);
  193. recip = vmul_f32(vrecps_f32(x, recip), recip);
  194. return recip;
  195. }
  196. inline float32x4_t vinvq_f32(float32x4_t x)
  197. {
  198. float32x4_t recip = vrecpeq_f32(x);
  199. recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
  200. recip = vmulq_f32(vrecpsq_f32(x, recip), recip);
  201. return recip;
  202. }
  203. inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const float32_t *coeffs)
  204. {
  205. float32x4_t A = vmlaq_f32(vld1q_f32(&coeffs[4*0]), vld1q_f32(&coeffs[4*4]), x);
  206. float32x4_t B = vmlaq_f32(vld1q_f32(&coeffs[4*2]), vld1q_f32(&coeffs[4*6]), x);
  207. float32x4_t C = vmlaq_f32(vld1q_f32(&coeffs[4*1]), vld1q_f32(&coeffs[4*5]), x);
  208. float32x4_t D = vmlaq_f32(vld1q_f32(&coeffs[4*3]), vld1q_f32(&coeffs[4*7]), x);
  209. float32x4_t x2 = vmulq_f32(x, x);
  210. float32x4_t x4 = vmulq_f32(x2, x2);
  211. float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4);
  212. return res;
  213. }
  214. inline float32x4_t vexpq_f32(float32x4_t x)
  215. {
  216. static const float32_t CONST_LN2[4] = {0.6931471805f,0.6931471805f,0.6931471805f,0.6931471805f}; // ln(2)
  217. static const float32_t CONST_INV_LN2[4] = {1.4426950408f,1.4426950408f,1.4426950408f,1.4426950408f}; // 1/ln(2)
  218. static const float32_t CONST_0[4] = {0.f,0.f,0.f,0.f};
  219. static const int32_t CONST_NEGATIVE_126[4] = {-126,-126,-126,-126};
  220. // Perform range reduction [-log(2),log(2)]
  221. int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, vld1q_f32(CONST_INV_LN2)));
  222. float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), vld1q_f32(CONST_LN2));
  223. // Polynomial Approximation
  224. float32x4_t poly = vtaylor_polyq_f32(val, exp_tab);
  225. // Reconstruct
  226. poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23)));
  227. poly = vbslq_f32(vcltq_s32(m, vld1q_s32(CONST_NEGATIVE_126)), vld1q_f32(CONST_0), poly);
  228. return poly;
  229. }
  230. inline float32x4_t vlogq_f32(float32x4_t x)
  231. {
  232. static const int32_t CONST_127[4] = {127,127,127,127}; // 127
  233. static const float32_t CONST_LN2[4] = {0.6931471805f,0.6931471805f,0.6931471805f,0.6931471805f}; // ln(2)
  234. // Extract exponent
  235. int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), vld1q_s32(CONST_127));
  236. float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23)));
  237. // Polynomial Approximation
  238. float32x4_t poly = vtaylor_polyq_f32(val, log_tab);
  239. // Reconstruct
  240. poly = vmlaq_f32(poly, vcvtq_f32_s32(m), vld1q_f32(CONST_LN2));
  241. return poly;
  242. }
  243. inline float32x4_t vtanhq_f32(float32x4_t val)
  244. {
  245. static const float32_t CONST_1[4] = {1.f,1.f,1.f,1.f};
  246. static const float32_t CONST_2[4] = {2.f,2.f,2.f,2.f};
  247. static const float32_t CONST_MIN_TANH[4] = {-10.f,-10.f,-10.f,-10.f};
  248. static const float32_t CONST_MAX_TANH[4] = {10.f,10.f,10.f,10.f};
  249. float32x4_t x = vminq_f32(vmaxq_f32(val, vld1q_f32(CONST_MIN_TANH)), vld1q_f32(CONST_MAX_TANH));
  250. float32x4_t exp2x = vexpq_f32(vmulq_f32(vld1q_f32(CONST_2), x));
  251. float32x4_t num = vsubq_f32(exp2x, vld1q_f32(CONST_1));
  252. float32x4_t den = vaddq_f32(exp2x, vld1q_f32(CONST_1));
  253. float32x4_t tanh = vmulq_f32(num, vinvq_f32(den));
  254. return tanh;
  255. }
  256. inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n)
  257. {
  258. return vexpq_f32(vmulq_f32(n, vlogq_f32(val)));
  259. }
  260. #endif /* DOXYGEN_SKIP_THIS */
  261. #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  262. /** Exponent polynomial coefficients */
  263. /** Logarithm polynomial coefficients */
  264. #ifndef DOXYGEN_SKIP_THIS
  265. inline float16x8_t vfloorq_f16(float16x8_t val)
  266. {
  267. static const float16_t CONST_1[8] = {1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f};
  268. const int16x8_t z = vcvtq_s16_f16(val);
  269. const float16x8_t r = vcvtq_f16_s16(z);
  270. return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, vld1q_f16(CONST_1)), r);
  271. }
  272. inline float16x4_t vinvsqrt_f16(float16x4_t x)
  273. {
  274. float16x4_t sqrt_reciprocal = vrsqrte_f16(x);
  275. sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  276. sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  277. return sqrt_reciprocal;
  278. }
  279. inline float16x8_t vinvsqrtq_f16(float16x8_t x)
  280. {
  281. float16x8_t sqrt_reciprocal = vrsqrteq_f16(x);
  282. sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  283. sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
  284. return sqrt_reciprocal;
  285. }
  286. inline float16x4_t vinv_f16(float16x4_t x)
  287. {
  288. float16x4_t recip = vrecpe_f16(x);
  289. recip = vmul_f16(vrecps_f16(x, recip), recip);
  290. recip = vmul_f16(vrecps_f16(x, recip), recip);
  291. return recip;
  292. }
  293. inline float16x8_t vinvq_f16(float16x8_t x)
  294. {
  295. float16x8_t recip = vrecpeq_f16(x);
  296. recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
  297. recip = vmulq_f16(vrecpsq_f16(x, recip), recip);
  298. return recip;
  299. }
  300. inline float16x8_t vtanhq_f16(float16x8_t val)
  301. {
  302. const float16_t CONST_1[8] = {1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f};
  303. const float16_t CONST_2[8] = {2.f,2.f,2.f,2.f,2.f,2.f,2.f,2.f};
  304. const float16_t CONST_MIN_TANH[8] = {-10.f,-10.f,-10.f,-10.f,-10.f,-10.f,-10.f,-10.f};
  305. const float16_t CONST_MAX_TANH[8] = {10.f,10.f,10.f,10.f,10.f,10.f,10.f,10.f};
  306. const float16x8_t x = vminq_f16(vmaxq_f16(val, vld1q_f16(CONST_MIN_TANH)), vld1q_f16(CONST_MAX_TANH));
  307. const float16x8_t exp2x = vexpq_f16(vmulq_f16(vld1q_f16(CONST_2), x));
  308. const float16x8_t num = vsubq_f16(exp2x, vld1q_f16(CONST_1));
  309. const float16x8_t den = vaddq_f16(exp2x, vld1q_f16(CONST_1));
  310. const float16x8_t tanh = vmulq_f16(num, vinvq_f16(den));
  311. return tanh;
  312. }
  313. inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const float16_t *coeffs)
  314. {
  315. const float16x8_t A = vaddq_f16(&coeffs[8*0], vmulq_f16(&coeffs[8*4], x));
  316. const float16x8_t B = vaddq_f16(&coeffs[8*2], vmulq_f16(&coeffs[8*6], x));
  317. const float16x8_t C = vaddq_f16(&coeffs[8*1], vmulq_f16(&coeffs[8*5], x));
  318. const float16x8_t D = vaddq_f16(&coeffs[8*3], vmulq_f16(&coeffs[8*7], x));
  319. const float16x8_t x2 = vmulq_f16(x, x);
  320. const float16x8_t x4 = vmulq_f16(x2, x2);
  321. const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4));
  322. return res;
  323. }
  324. inline float16x8_t vexpq_f16(float16x8_t x)
  325. {
  326. // TODO (COMPMID-1535) : Revisit FP16 approximations
  327. const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
  328. const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
  329. const float16x8_t res = vcvt_high_f16_f32(vcvt_f16_f32(vexpq_f32(x_low)), vexpq_f32(x_high));
  330. return res;
  331. }
  332. inline float16x8_t vlogq_f16(float16x8_t x)
  333. {
  334. // TODO (COMPMID-1535) : Revisit FP16 approximations
  335. const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x));
  336. const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x));
  337. const float16x8_t res = vcvt_high_f16_f32(vcvt_f16_f32(vlogq_f32(x_low)), vlogq_f32(x_high));
  338. return res;
  339. }
  340. inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n)
  341. {
  342. // TODO (giaiod01) - COMPMID-1535
  343. float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n));
  344. float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n));
  345. float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val));
  346. float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val));
  347. float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32)));
  348. float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32)));
  349. return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32));
  350. }
  351. #endif /* DOXYGEN_SKIP_THIS */
  352. #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
  353. #endif
  354. #endif /* __ARM_COMPUTE_NEMATH_H__ */