libm.h 14 KB

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