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

453 lines
13KB

  1. /*
  2. * erf function: Copyright (c) 2006 John Maddock
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * Replacements for frequently missing libm functions
  22. */
  23. #ifndef AVUTIL_LIBM_H
  24. #define AVUTIL_LIBM_H
  25. #include <math.h>
  26. #include "config.h"
  27. #include "attributes.h"
  28. #include "intfloat.h"
  29. #if HAVE_MIPSFPU && HAVE_INLINE_ASM
  30. #include "libavutil/mips/libm_mips.h"
  31. #endif /* HAVE_MIPSFPU && HAVE_INLINE_ASM*/
  32. #if !HAVE_ATANF
  33. #undef atanf
  34. #define atanf(x) ((float)atan(x))
  35. #endif
  36. #if !HAVE_ATAN2F
  37. #undef atan2f
  38. #define atan2f(y, x) ((float)atan2(y, x))
  39. #endif
  40. #if !HAVE_POWF
  41. #undef powf
  42. #define powf(x, y) ((float)pow(x, y))
  43. #endif
  44. #if !HAVE_CBRT
  45. static av_always_inline double cbrt(double x)
  46. {
  47. return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
  48. }
  49. #endif
  50. #if !HAVE_CBRTF
  51. static av_always_inline float cbrtf(float x)
  52. {
  53. return x < 0 ? -powf(-x, 1.0 / 3.0) : powf(x, 1.0 / 3.0);
  54. }
  55. #endif
  56. #if !HAVE_COPYSIGN
  57. static av_always_inline double copysign(double x, double y)
  58. {
  59. uint64_t vx = av_double2int(x);
  60. uint64_t vy = av_double2int(y);
  61. return av_int2double((vx & UINT64_C(0x7fffffffffffffff)) | (vy & UINT64_C(0x8000000000000000)));
  62. }
  63. #endif
  64. #if !HAVE_COSF
  65. #undef cosf
  66. #define cosf(x) ((float)cos(x))
  67. #endif
  68. #if !HAVE_ERF
  69. static inline double ff_eval_poly(const double *coeff, int size, double x) {
  70. double sum = coeff[size-1];
  71. int i;
  72. for (i = size-2; i >= 0; --i) {
  73. sum *= x;
  74. sum += coeff[i];
  75. }
  76. return sum;
  77. }
  78. /**
  79. * erf function
  80. * Algorithm taken from the Boost project, source:
  81. * http://www.boost.org/doc/libs/1_46_1/boost/math/special_functions/erf.hpp
  82. * Use, modification and distribution are subject to the
  83. * Boost Software License, Version 1.0 (see notice below).
  84. * Boost Software License - Version 1.0 - August 17th, 2003
  85. Permission is hereby granted, free of charge, to any person or organization
  86. obtaining a copy of the software and accompanying documentation covered by
  87. this license (the "Software") to use, reproduce, display, distribute,
  88. execute, and transmit the Software, and to prepare derivative works of the
  89. Software, and to permit third-parties to whom the Software is furnished to
  90. do so, all subject to the following:
  91. The copyright notices in the Software and this entire statement, including
  92. the above license grant, this restriction and the following disclaimer,
  93. must be included in all copies of the Software, in whole or in part, and
  94. all derivative works of the Software, unless such copies or derivative
  95. works are solely in the form of machine-executable object code generated by
  96. a source language processor.
  97. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  98. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  99. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  100. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  101. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  102. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  103. DEALINGS IN THE SOFTWARE.
  104. */
  105. static inline double erf(double z)
  106. {
  107. #ifndef FF_ARRAY_ELEMS
  108. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  109. #endif
  110. double result;
  111. /* handle the symmetry: erf(-x) = -erf(x) */
  112. if (z < 0)
  113. return -erf(-z);
  114. /* branch based on range of z, and pick appropriate approximation */
  115. if (z == 0)
  116. return 0;
  117. else if (z < 1e-10)
  118. return z * 1.125 + z * 0.003379167095512573896158903121545171688;
  119. else if (z < 0.5) {
  120. // Maximum Deviation Found: 1.561e-17
  121. // Expected Error Term: 1.561e-17
  122. // Maximum Relative Change in Control Points: 1.155e-04
  123. // Max Error found at double precision = 2.961182e-17
  124. static const double y = 1.044948577880859375;
  125. static const double p[] = {
  126. 0.0834305892146531832907,
  127. -0.338165134459360935041,
  128. -0.0509990735146777432841,
  129. -0.00772758345802133288487,
  130. -0.000322780120964605683831,
  131. };
  132. static const double q[] = {
  133. 1,
  134. 0.455004033050794024546,
  135. 0.0875222600142252549554,
  136. 0.00858571925074406212772,
  137. 0.000370900071787748000569,
  138. };
  139. double zz = z * z;
  140. return z * (y + ff_eval_poly(p, FF_ARRAY_ELEMS(p), zz) / ff_eval_poly(q, FF_ARRAY_ELEMS(q), zz));
  141. }
  142. /* here onwards compute erfc */
  143. else if (z < 1.5) {
  144. // Maximum Deviation Found: 3.702e-17
  145. // Expected Error Term: 3.702e-17
  146. // Maximum Relative Change in Control Points: 2.845e-04
  147. // Max Error found at double precision = 4.841816e-17
  148. static const double y = 0.405935764312744140625;
  149. static const double p[] = {
  150. -0.098090592216281240205,
  151. 0.178114665841120341155,
  152. 0.191003695796775433986,
  153. 0.0888900368967884466578,
  154. 0.0195049001251218801359,
  155. 0.00180424538297014223957,
  156. };
  157. static const double q[] = {
  158. 1,
  159. 1.84759070983002217845,
  160. 1.42628004845511324508,
  161. 0.578052804889902404909,
  162. 0.12385097467900864233,
  163. 0.0113385233577001411017,
  164. 0.337511472483094676155e-5,
  165. };
  166. result = y + ff_eval_poly(p, FF_ARRAY_ELEMS(p), z - 0.5) / ff_eval_poly(q, FF_ARRAY_ELEMS(q), z - 0.5);
  167. result *= exp(-z * z) / z;
  168. return 1 - result;
  169. }
  170. else if (z < 2.5) {
  171. // Max Error found at double precision = 6.599585e-18
  172. // Maximum Deviation Found: 3.909e-18
  173. // Expected Error Term: 3.909e-18
  174. // Maximum Relative Change in Control Points: 9.886e-05
  175. static const double y = 0.50672817230224609375;
  176. static const double p[] = {
  177. -0.0243500476207698441272,
  178. 0.0386540375035707201728,
  179. 0.04394818964209516296,
  180. 0.0175679436311802092299,
  181. 0.00323962406290842133584,
  182. 0.000235839115596880717416,
  183. };
  184. static const double q[] = {
  185. 1,
  186. 1.53991494948552447182,
  187. 0.982403709157920235114,
  188. 0.325732924782444448493,
  189. 0.0563921837420478160373,
  190. 0.00410369723978904575884,
  191. };
  192. result = y + ff_eval_poly(p, FF_ARRAY_ELEMS(p), z - 1.5) / ff_eval_poly(q, FF_ARRAY_ELEMS(q), z - 1.5);
  193. result *= exp(-z * z) / z;
  194. return 1 - result;
  195. }
  196. else if (z < 4.5) {
  197. // Maximum Deviation Found: 1.512e-17
  198. // Expected Error Term: 1.512e-17
  199. // Maximum Relative Change in Control Points: 2.222e-04
  200. // Max Error found at double precision = 2.062515e-17
  201. static const double y = 0.5405750274658203125;
  202. static const double p[] = {
  203. 0.00295276716530971662634,
  204. 0.0137384425896355332126,
  205. 0.00840807615555585383007,
  206. 0.00212825620914618649141,
  207. 0.000250269961544794627958,
  208. 0.113212406648847561139e-4,
  209. };
  210. static const double q[] = {
  211. 1,
  212. 1.04217814166938418171,
  213. 0.442597659481563127003,
  214. 0.0958492726301061423444,
  215. 0.0105982906484876531489,
  216. 0.000479411269521714493907,
  217. };
  218. result = y + ff_eval_poly(p, FF_ARRAY_ELEMS(p), z - 3.5) / ff_eval_poly(q, FF_ARRAY_ELEMS(q), z - 3.5);
  219. result *= exp(-z * z) / z;
  220. return 1 - result;
  221. }
  222. /* differ from Boost here, the claim of underflow of erfc(x) past 5.8 is
  223. * slightly incorrect, change to 5.92
  224. * (really somewhere between 5.9125 and 5.925 is when it saturates) */
  225. else if (z < 5.92) {
  226. // Max Error found at double precision = 2.997958e-17
  227. // Maximum Deviation Found: 2.860e-17
  228. // Expected Error Term: 2.859e-17
  229. // Maximum Relative Change in Control Points: 1.357e-05
  230. static const double y = 0.5579090118408203125;
  231. static const double p[] = {
  232. 0.00628057170626964891937,
  233. 0.0175389834052493308818,
  234. -0.212652252872804219852,
  235. -0.687717681153649930619,
  236. -2.5518551727311523996,
  237. -3.22729451764143718517,
  238. -2.8175401114513378771,
  239. };
  240. static const double q[] = {
  241. 1,
  242. 2.79257750980575282228,
  243. 11.0567237927800161565,
  244. 15.930646027911794143,
  245. 22.9367376522880577224,
  246. 13.5064170191802889145,
  247. 5.48409182238641741584,
  248. };
  249. result = y + ff_eval_poly(p, FF_ARRAY_ELEMS(p), 1 / z) / ff_eval_poly(q, FF_ARRAY_ELEMS(q), 1 / z);
  250. result *= exp(-z * z) / z;
  251. return 1 - result;
  252. }
  253. /* handle the nan case, but don't use isnan for max portability */
  254. else if (z != z)
  255. return z;
  256. /* finally return saturated result */
  257. else
  258. return 1;
  259. }
  260. #endif
  261. #if !HAVE_EXPF
  262. #undef expf
  263. #define expf(x) ((float)exp(x))
  264. #endif
  265. #if !HAVE_EXP2
  266. #undef exp2
  267. #define exp2(x) exp((x) * 0.693147180559945)
  268. #endif /* HAVE_EXP2 */
  269. #if !HAVE_EXP2F
  270. #undef exp2f
  271. #define exp2f(x) ((float)exp2(x))
  272. #endif /* HAVE_EXP2F */
  273. #if !HAVE_ISINF
  274. #undef isinf
  275. /* Note: these do not follow the BSD/Apple/GNU convention of returning -1 for
  276. -Inf, +1 for Inf, 0 otherwise, but merely follow the POSIX/ISO mandated spec of
  277. returning a non-zero value for +/-Inf, 0 otherwise. */
  278. static av_always_inline av_const int avpriv_isinff(float x)
  279. {
  280. uint32_t v = av_float2int(x);
  281. if ((v & 0x7f800000) != 0x7f800000)
  282. return 0;
  283. return !(v & 0x007fffff);
  284. }
  285. static av_always_inline av_const int avpriv_isinf(double x)
  286. {
  287. uint64_t v = av_double2int(x);
  288. if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
  289. return 0;
  290. return !(v & 0x000fffffffffffff);
  291. }
  292. #define isinf(x) \
  293. (sizeof(x) == sizeof(float) \
  294. ? avpriv_isinff(x) \
  295. : avpriv_isinf(x))
  296. #endif /* HAVE_ISINF */
  297. #if !HAVE_ISNAN
  298. static av_always_inline av_const int avpriv_isnanf(float x)
  299. {
  300. uint32_t v = av_float2int(x);
  301. if ((v & 0x7f800000) != 0x7f800000)
  302. return 0;
  303. return v & 0x007fffff;
  304. }
  305. static av_always_inline av_const int avpriv_isnan(double x)
  306. {
  307. uint64_t v = av_double2int(x);
  308. if ((v & 0x7ff0000000000000) != 0x7ff0000000000000)
  309. return 0;
  310. return (v & 0x000fffffffffffff) && 1;
  311. }
  312. #define isnan(x) \
  313. (sizeof(x) == sizeof(float) \
  314. ? avpriv_isnanf(x) \
  315. : avpriv_isnan(x))
  316. #endif /* HAVE_ISNAN */
  317. #if !HAVE_HYPOT
  318. #undef hypot
  319. static inline av_const double hypot(double x, double y)
  320. {
  321. double ret, temp;
  322. x = fabs(x);
  323. y = fabs(y);
  324. if (isinf(x) || isinf(y))
  325. return av_int2double(0x7ff0000000000000);
  326. if (x == 0 || y == 0)
  327. return x + y;
  328. if (x < y) {
  329. temp = x;
  330. x = y;
  331. y = temp;
  332. }
  333. y = y/x;
  334. return x*sqrt(1 + y*y);
  335. }
  336. #endif /* HAVE_HYPOT */
  337. #if !HAVE_LDEXPF
  338. #undef ldexpf
  339. #define ldexpf(x, exp) ((float)ldexp(x, exp))
  340. #endif
  341. #if !HAVE_LLRINT
  342. #undef llrint
  343. #define llrint(x) ((long long)rint(x))
  344. #endif /* HAVE_LLRINT */
  345. #if !HAVE_LLRINTF
  346. #undef llrintf
  347. #define llrintf(x) ((long long)rint(x))
  348. #endif /* HAVE_LLRINT */
  349. #if !HAVE_LOG2
  350. #undef log2
  351. #define log2(x) (log(x) * 1.44269504088896340736)
  352. #endif /* HAVE_LOG2 */
  353. #if !HAVE_LOG2F
  354. #undef log2f
  355. #define log2f(x) ((float)log2(x))
  356. #endif /* HAVE_LOG2F */
  357. #if !HAVE_LOG10F
  358. #undef log10f
  359. #define log10f(x) ((float)log10(x))
  360. #endif
  361. #if !HAVE_SINF
  362. #undef sinf
  363. #define sinf(x) ((float)sin(x))
  364. #endif
  365. #if !HAVE_RINT
  366. static inline double rint(double x)
  367. {
  368. return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
  369. }
  370. #endif /* HAVE_RINT */
  371. #if !HAVE_LRINT
  372. static av_always_inline av_const long int lrint(double x)
  373. {
  374. return rint(x);
  375. }
  376. #endif /* HAVE_LRINT */
  377. #if !HAVE_LRINTF
  378. static av_always_inline av_const long int lrintf(float x)
  379. {
  380. return (int)(rint(x));
  381. }
  382. #endif /* HAVE_LRINTF */
  383. #if !HAVE_ROUND
  384. static av_always_inline av_const double round(double x)
  385. {
  386. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  387. }
  388. #endif /* HAVE_ROUND */
  389. #if !HAVE_ROUNDF
  390. static av_always_inline av_const float roundf(float x)
  391. {
  392. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  393. }
  394. #endif /* HAVE_ROUNDF */
  395. #if !HAVE_TRUNC
  396. static av_always_inline av_const double trunc(double x)
  397. {
  398. return (x > 0) ? floor(x) : ceil(x);
  399. }
  400. #endif /* HAVE_TRUNC */
  401. #if !HAVE_TRUNCF
  402. static av_always_inline av_const float truncf(float x)
  403. {
  404. return (x > 0) ? floor(x) : ceil(x);
  405. }
  406. #endif /* HAVE_TRUNCF */
  407. #endif /* AVUTIL_LIBM_H */