mathf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /****************************************************************************/
  2. /* mathf.h v8.2.2 */
  3. /* */
  4. /* Copyright (c) 1997-2017 Texas Instruments Incorporated */
  5. /* http://www.ti.com/ */
  6. /* */
  7. /* Redistribution and use in source and binary forms, with or without */
  8. /* modification, are permitted provided that the following conditions */
  9. /* are met: */
  10. /* */
  11. /* Redistributions of source code must retain the above copyright */
  12. /* notice, this list of conditions and the following disclaimer. */
  13. /* */
  14. /* Redistributions in binary form must reproduce the above copyright */
  15. /* notice, this list of conditions and the following disclaimer in */
  16. /* the documentation and/or other materials provided with the */
  17. /* distribution. */
  18. /* */
  19. /* Neither the name of Texas Instruments Incorporated nor the names */
  20. /* of its contributors may be used to endorse or promote products */
  21. /* derived from this software without specific prior written */
  22. /* permission. */
  23. /* */
  24. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
  25. /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
  26. /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
  27. /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
  28. /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
  29. /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
  30. /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  31. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
  32. /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  33. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
  34. /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  35. /* */
  36. /****************************************************************************/
  37. #ifndef __mathf__
  38. #define __mathf__
  39. #pragma diag_push
  40. #pragma CHECK_MISRA("-6.3") /* standard types required for standard headers */
  41. #pragma CHECK_MISRA("-19.1") /* #includes required for implementation */
  42. #pragma CHECK_MISRA("-19.7") /* macros required for implementation */
  43. #pragma CHECK_MISRA("-20.1") /* standard headers must define standard names */
  44. #pragma CHECK_MISRA("-20.2") /* standard headers must define standard names */
  45. #ifdef __cplusplus
  46. //----------------------------------------------------------------------------
  47. // <cmathf> IS RECOMMENDED OVER <mathf.h>. <mathf.h> IS PROVIDED FOR
  48. // COMPATIBILITY WITH C AND THIS USAGE IS DEPRECATED IN C++
  49. //----------------------------------------------------------------------------
  50. extern "C" namespace std {
  51. #endif /* !__cplusplus */
  52. #include <linkage.h>
  53. _CODE_ACCESS float sqrtf (float x);
  54. _CODE_ACCESS float expf (float x);
  55. _CODE_ACCESS float logf (float x);
  56. _CODE_ACCESS float log10f(float x);
  57. _CODE_ACCESS float powf (float x, float y);
  58. _CODE_ACCESS float sinf (float x);
  59. _CODE_ACCESS float cosf (float x);
  60. _CODE_ACCESS float tanf (float x);
  61. _CODE_ACCESS float asinf (float x);
  62. _CODE_ACCESS float acosf (float x);
  63. _CODE_ACCESS float atanf (float x);
  64. _CODE_ACCESS float atan2f(float y, float x);
  65. _CODE_ACCESS float sinhf (float x);
  66. _CODE_ACCESS float coshf (float x);
  67. _CODE_ACCESS float tanhf (float x);
  68. _IDECL float ceilf (float x);
  69. _IDECL float floorf(float x);
  70. #pragma diag_push
  71. #pragma CHECK_MISRA("-16.4") /* false positives due to builtin declarations */
  72. _CODE_ACCESS float fabsf (float x);
  73. #pragma diag_pop
  74. _CODE_ACCESS float ldexpf(float x, int n);
  75. _CODE_ACCESS float frexpf(float x, int *_exp);
  76. _CODE_ACCESS float modff (float x, float *ip);
  77. _CODE_ACCESS float fmodf (float x, float y);
  78. /* these present in many linked images, so we'll tell you about them. */
  79. _CODE_ACCESS float _roundf(float x); /* round-to-nearest */
  80. _CODE_ACCESS float _truncf(float x); /* truncate towards 0 */
  81. /* ------------------------------------------------- */
  82. /* Routines below are an addition to ANSI math.h */
  83. /* Some (noted with "9x" in comment) will become ANSI*/
  84. /* once C9x is approved. */
  85. /* ------------------------------------------------- */
  86. #define HUGE_VALF (__INFINITY__)
  87. _CODE_ACCESS float rsqrtf(float x); /* == 1/sqrtf(x) but *MUCH* faster */
  88. _CODE_ACCESS float exp2f (float x); /*9x mathematically equiv to powf(2.0 ,x) */
  89. _CODE_ACCESS float exp10f(float x); /* mathematically equiv to powf(10.0,x) */
  90. _CODE_ACCESS float log2f (float x); /*9x mathematically equiv to logf(x)/logf(2.)*/
  91. _CODE_ACCESS float powif (float x, int i); /* equiv to powf(x,(float)i) */
  92. _CODE_ACCESS float cotf (float x);
  93. _CODE_ACCESS float acotf (float x);
  94. _CODE_ACCESS float acot2f(float x, float y);
  95. _CODE_ACCESS float cothf (float x);
  96. _CODE_ACCESS float asinhf(float x); /* 9x */
  97. _CODE_ACCESS float acoshf(float x); /* 9x */
  98. _CODE_ACCESS float atanhf(float x); /* 9x */
  99. _CODE_ACCESS float acothf(float x);
  100. _CODE_ACCESS float log1pf(float x);
  101. _CODE_ACCESS float hypotf(float x, float y);
  102. _CODE_ACCESS float copysignf(float x, float y);
  103. #ifndef __INLINE_ISINF__
  104. #define __INLINE_ISINF__ 0
  105. #endif
  106. #if __INLINE_ISINF__
  107. _IDECL int __isinff(float x);
  108. #else
  109. _CODE_ACCESS int __isinff(float x);
  110. #endif
  111. _IDECL int __isnanf(float x);
  112. _IDECL int __isfinitef(float x);
  113. _IDECL int __isnormalf(float x);
  114. _CODE_ACCESS int __fpclassifyf(float x);
  115. #define isinf(x) (sizeof(x) == sizeof(double) ? __isinf(x) : \
  116. sizeof(x) == sizeof(float) ? __isinff(x) : __isinfl(x))
  117. #define isnan(x) (sizeof(x) == sizeof(double) ? __isnan(x) : \
  118. sizeof(x) == sizeof(float) ? __isnanf(x) : __isnanl(x))
  119. #define isfinite(x) (sizeof(x) == sizeof(double) ? __isfinite(x) : \
  120. sizeof(x) == sizeof(float) ? __isfinitef(x) : \
  121. __isfinitel(x))
  122. #define isnormal(x) (sizeof(x) == sizeof(double) ? __isnormal(x) : \
  123. sizeof(x) == sizeof(float) ? __isnormalf(x) : \
  124. __isnormall(x))
  125. #define fpclassify(x) (sizeof(x) == sizeof(double) ? __fpclassify(x) : \
  126. sizeof(x) == sizeof(float) ? __fpclassifyf(x) : \
  127. __fpclassifyl(x))
  128. #pragma diag_push
  129. /* keep macros as direct #defines and not function-like macros or function
  130. names surrounded by parentheses to support all original supported use cases
  131. including taking their address through the macros and prefixing with
  132. namespace macros */
  133. #pragma CHECK_MISRA("-19.4")
  134. #define roundf _roundf /* 9x round-to-nearest */
  135. #define truncf _truncf /* 9x truncate towards 0 */
  136. #pragma diag_pop
  137. #ifdef _INLINE
  138. #pragma diag_push /* functions */
  139. /* MISRA exceptions to avoid changing inline versions of the functions that
  140. would be linked in instead of included inline at different mf levels */
  141. /* these functions are very well-tested, stable, and efficient; it would
  142. introduce a high risk to implement new, separate MISRA versions just for the
  143. inline headers */
  144. #pragma CHECK_MISRA("-5.7") /* keep names intact */
  145. #pragma CHECK_MISRA("-8.5") /* need to define inline functions */
  146. #pragma CHECK_MISRA("-9.1") /* false positives on use after indirect assign */
  147. #pragma CHECK_MISRA("-10.1") /* use implicit casts */
  148. #pragma CHECK_MISRA("-10.2") /* use implicit casts */
  149. #pragma CHECK_MISRA("-12.1") /* avoid changing expressions */
  150. #pragma CHECK_MISRA("-12.2") /* avoid changing expressions */
  151. #pragma CHECK_MISRA("-12.4") /* avoid changing expressions */
  152. #pragma CHECK_MISRA("-12.5") /* avoid changing expressions */
  153. #pragma CHECK_MISRA("-13.3") /* need FP compares */
  154. #pragma CHECK_MISRA("-14.7") /* use multiple return points */
  155. #pragma CHECK_MISRA("-14.9") /* use non-compound statements */
  156. #pragma diag_suppress 551 /* false positives on use after indirect assign */
  157. /****************************************************************************/
  158. /* Inline versions of floorf, ceilf, fmodf */
  159. /****************************************************************************/
  160. static __inline float floorf(float x)
  161. {
  162. float y;
  163. return (modff(x, &y) < 0 ? y - 1 : y);
  164. }
  165. static __inline float ceilf(float x)
  166. {
  167. float y;
  168. return (modff(x, &y) > 0 ? y + 1 : y);
  169. }
  170. #if __INLINE_ISINF__
  171. #ifndef REAL_TO_REALNUM
  172. #error isinf can only be inlined in the compilation of the rts
  173. #endif
  174. static __inline int __isinff(float x)
  175. {
  176. realnum _x;
  177. REAL_TO_REALNUM(x, _x);
  178. return _x.exp == (REAL_EMAX + 1) && (_x.mantissa << 1) == 0;
  179. }
  180. #endif /* __INLINE_ISINF__ */
  181. static __inline int __isnanf(volatile float x)
  182. {
  183. return x != x;
  184. }
  185. static __inline int __isfinitef(float x)
  186. {
  187. return (!__isinff(x) && !__isnanf(x));
  188. }
  189. static __inline int __isnormalf(float x)
  190. {
  191. return (__isfinitef(x) && x != 0.0);
  192. }
  193. #pragma diag_pop
  194. #endif /* _INLINE */
  195. #ifdef __cplusplus
  196. } /* extern "C" namespace std */
  197. #endif /* __cplusplus */
  198. #pragma diag_pop
  199. #endif /* __mathf__ */
  200. #pragma diag_push
  201. /* using declarations must occur outside header guard to support including both
  202. C and C++-wrapped version of header; see _CPP_STYLE_HEADER check */
  203. /* this code is for C++ mode only and thus also not relevant for MISRA */
  204. #pragma CHECK_MISRA("-19.15")
  205. #if defined(__cplusplus) && !defined(_CPP_STYLE_HEADER)
  206. using std::sqrtf;
  207. using std::expf;
  208. using std::logf;
  209. using std::log10f;
  210. using std::powf;
  211. using std::sinf;
  212. using std::cosf;
  213. using std::tanf;
  214. using std::asinf;
  215. using std::acosf;
  216. using std::atanf;
  217. using std::atan2f;
  218. using std::sinhf;
  219. using std::coshf;
  220. using std::tanhf;
  221. using std::ceilf;
  222. using std::floorf;
  223. using std::fabsf;
  224. using std::ldexpf;
  225. using std::frexpf;
  226. using std::modff;
  227. using std::fmodf;
  228. using std::log1pf;
  229. using std::copysignf;
  230. using std::hypotf;
  231. using std::rsqrtf; /* == 1/sqrtf(x) but *MUCH* faster */
  232. using std::exp2f; /*9x mathematically equiv to powf(2.0 ,x) */
  233. using std::exp10f; /* mathematically equiv to powf(10.0,x) */
  234. using std::log2f; /*9x mathematically equiv to logf(x)/logf(2.)*/
  235. using std::powif; /* equiv to powf(x,(float)i) */
  236. using std::cotf;
  237. using std::acotf;
  238. using std::acot2f;
  239. using std::cothf;
  240. using std::asinhf; /* 9x */
  241. using std::acoshf; /* 9x */
  242. using std::atanhf; /* 9x */
  243. using std::acothf;
  244. #endif /* ! _CPP_STYLE_HEADER */
  245. #if defined(__cplusplus)
  246. using std::__isnanf;
  247. using std::__isinff;
  248. using std::__isfinitef;
  249. using std::__isnormalf;
  250. using std::__fpclassifyf;
  251. using std::_roundf; /* round-to-nearest */
  252. using std::_truncf; /* truncate towards 0 */
  253. #endif /* __cplusplus */
  254. #pragma diag_pop