cmath 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // -*- C++ -*- C forwarding header.
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/cmath
  21. * This is a Standard C++ Library file. You should @c #include this file
  22. * in your programs, rather than any of the @a *.h implementation files.
  23. *
  24. * This is the C++ version of the Standard C Library header @c math.h,
  25. * and its contents are (mostly) the same as that header, but are all
  26. * contained in the namespace @c std (except for names which are defined
  27. * as macros in C).
  28. */
  29. //
  30. // ISO C++ 14882: 26.5 C library
  31. //
  32. #ifndef _GLIBCXX_CMATH
  33. #define _GLIBCXX_CMATH 1
  34. #pragma GCC system_header
  35. #include <bits/c++config.h>
  36. #include <bits/cpp_type_traits.h>
  37. #include <ext/type_traits.h>
  38. #include <math.h>
  39. // Get rid of those macros defined in <math.h> in lieu of real functions.
  40. #undef abs
  41. #undef div
  42. #undef acos
  43. #undef asin
  44. #undef atan
  45. #undef atan2
  46. #undef ceil
  47. #undef cos
  48. #undef cosh
  49. #undef exp
  50. #undef fabs
  51. #undef floor
  52. #undef fmod
  53. #undef frexp
  54. #undef ldexp
  55. #undef log
  56. #undef log10
  57. #undef modf
  58. #undef pow
  59. #undef sin
  60. #undef sinh
  61. #undef sqrt
  62. #undef tan
  63. #undef tanh
  64. namespace std _GLIBCXX_VISIBILITY(default)
  65. {
  66. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  67. inline double
  68. abs(double __x)
  69. { return __builtin_fabs(__x); }
  70. inline float
  71. abs(float __x)
  72. { return __builtin_fabsf(__x); }
  73. inline long double
  74. abs(long double __x)
  75. { return __builtin_fabsl(__x); }
  76. template<typename _Tp>
  77. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  78. double>::__type
  79. abs(_Tp __x)
  80. { return __builtin_fabs(__x); }
  81. using ::acos;
  82. inline float
  83. acos(float __x)
  84. { return __builtin_acosf(__x); }
  85. inline long double
  86. acos(long double __x)
  87. { return __builtin_acosl(__x); }
  88. template<typename _Tp>
  89. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  90. double>::__type
  91. acos(_Tp __x)
  92. { return __builtin_acos(__x); }
  93. using ::asin;
  94. inline float
  95. asin(float __x)
  96. { return __builtin_asinf(__x); }
  97. inline long double
  98. asin(long double __x)
  99. { return __builtin_asinl(__x); }
  100. template<typename _Tp>
  101. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  102. double>::__type
  103. asin(_Tp __x)
  104. { return __builtin_asin(__x); }
  105. using ::atan;
  106. inline float
  107. atan(float __x)
  108. { return __builtin_atanf(__x); }
  109. inline long double
  110. atan(long double __x)
  111. { return __builtin_atanl(__x); }
  112. template<typename _Tp>
  113. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  114. double>::__type
  115. atan(_Tp __x)
  116. { return __builtin_atan(__x); }
  117. using ::atan2;
  118. inline float
  119. atan2(float __y, float __x)
  120. { return __builtin_atan2f(__y, __x); }
  121. inline long double
  122. atan2(long double __y, long double __x)
  123. { return __builtin_atan2l(__y, __x); }
  124. template<typename _Tp, typename _Up>
  125. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value
  126. && __is_integer<_Up>::__value,
  127. double>::__type
  128. atan2(_Tp __y, _Up __x)
  129. { return __builtin_atan2(__y, __x); }
  130. using ::ceil;
  131. inline float
  132. ceil(float __x)
  133. { return __builtin_ceilf(__x); }
  134. inline long double
  135. ceil(long double __x)
  136. { return __builtin_ceill(__x); }
  137. template<typename _Tp>
  138. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  139. double>::__type
  140. ceil(_Tp __x)
  141. { return __builtin_ceil(__x); }
  142. using ::cos;
  143. inline float
  144. cos(float __x)
  145. { return __builtin_cosf(__x); }
  146. inline long double
  147. cos(long double __x)
  148. { return __builtin_cosl(__x); }
  149. template<typename _Tp>
  150. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  151. double>::__type
  152. cos(_Tp __x)
  153. { return __builtin_cos(__x); }
  154. using ::cosh;
  155. inline float
  156. cosh(float __x)
  157. { return __builtin_coshf(__x); }
  158. inline long double
  159. cosh(long double __x)
  160. { return __builtin_coshl(__x); }
  161. template<typename _Tp>
  162. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  163. double>::__type
  164. cosh(_Tp __x)
  165. { return __builtin_cosh(__x); }
  166. using ::exp;
  167. inline float
  168. exp(float __x)
  169. { return __builtin_expf(__x); }
  170. inline long double
  171. exp(long double __x)
  172. { return __builtin_expl(__x); }
  173. template<typename _Tp>
  174. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  175. double>::__type
  176. exp(_Tp __x)
  177. { return __builtin_exp(__x); }
  178. using ::fabs;
  179. inline float
  180. fabs(float __x)
  181. { return __builtin_fabsf(__x); }
  182. inline long double
  183. fabs(long double __x)
  184. { return __builtin_fabsl(__x); }
  185. template<typename _Tp>
  186. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  187. double>::__type
  188. fabs(_Tp __x)
  189. { return __builtin_fabs(__x); }
  190. using ::floor;
  191. inline float
  192. floor(float __x)
  193. { return __builtin_floorf(__x); }
  194. inline long double
  195. floor(long double __x)
  196. { return __builtin_floorl(__x); }
  197. template<typename _Tp>
  198. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  199. double>::__type
  200. floor(_Tp __x)
  201. { return __builtin_floor(__x); }
  202. using ::fmod;
  203. inline float
  204. fmod(float __x, float __y)
  205. { return __builtin_fmodf(__x, __y); }
  206. inline long double
  207. fmod(long double __x, long double __y)
  208. { return __builtin_fmodl(__x, __y); }
  209. template<typename _Tp, typename _Up>
  210. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value
  211. && __is_integer<_Up>::__value,
  212. double>::__type
  213. fmod(_Tp __x, _Up __y)
  214. { return __builtin_fmod(__x, __y); }
  215. using ::frexp;
  216. inline float
  217. frexp(float __x, int* __exp)
  218. { return __builtin_frexpf(__x, __exp); }
  219. inline long double
  220. frexp(long double __x, int* __exp)
  221. { return __builtin_frexpl(__x, __exp); }
  222. template<typename _Tp>
  223. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  224. double>::__type
  225. frexp(_Tp __x, int* __exp)
  226. { return __builtin_frexp(__x, __exp); }
  227. using ::ldexp;
  228. inline float
  229. ldexp(float __x, int __exp)
  230. { return __builtin_ldexpf(__x, __exp); }
  231. inline long double
  232. ldexp(long double __x, int __exp)
  233. { return __builtin_ldexpl(__x, __exp); }
  234. template<typename _Tp>
  235. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  236. double>::__type
  237. ldexp(_Tp __x, int __exp)
  238. { return __builtin_ldexp(__x, __exp); }
  239. using ::log;
  240. inline float
  241. log(float __x)
  242. { return __builtin_logf(__x); }
  243. inline long double
  244. log(long double __x)
  245. { return __builtin_logl(__x); }
  246. template<typename _Tp>
  247. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  248. double>::__type
  249. log(_Tp __x)
  250. { return __builtin_log(__x); }
  251. using ::log10;
  252. inline float
  253. log10(float __x)
  254. { return __builtin_log10f(__x); }
  255. inline long double
  256. log10(long double __x)
  257. { return __builtin_log10l(__x); }
  258. template<typename _Tp>
  259. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  260. double>::__type
  261. log10(_Tp __x)
  262. { return __builtin_log10(__x); }
  263. using ::modf;
  264. inline float
  265. modf(float __x, float* __iptr)
  266. { return __builtin_modff(__x, __iptr); }
  267. inline long double
  268. modf(long double __x, long double* __iptr)
  269. { return __builtin_modfl(__x, __iptr); }
  270. using ::pow;
  271. inline float
  272. pow(float __x, float __y)
  273. { return __builtin_powf(__x, __y); }
  274. inline long double
  275. pow(long double __x, long double __y)
  276. { return __builtin_powl(__x, __y); }
  277. inline double
  278. pow(double __x, int __i)
  279. { return __builtin_powi(__x, __i); }
  280. inline float
  281. pow(float __x, int __n)
  282. { return __builtin_powif(__x, __n); }
  283. inline long double
  284. pow(long double __x, int __n)
  285. { return __builtin_powil(__x, __n); }
  286. using ::sin;
  287. inline float
  288. sin(float __x)
  289. { return __builtin_sinf(__x); }
  290. inline long double
  291. sin(long double __x)
  292. { return __builtin_sinl(__x); }
  293. template<typename _Tp>
  294. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  295. double>::__type
  296. sin(_Tp __x)
  297. { return __builtin_sin(__x); }
  298. using ::sinh;
  299. inline float
  300. sinh(float __x)
  301. { return __builtin_sinhf(__x); }
  302. inline long double
  303. sinh(long double __x)
  304. { return __builtin_sinhl(__x); }
  305. template<typename _Tp>
  306. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  307. double>::__type
  308. sinh(_Tp __x)
  309. { return __builtin_sinh(__x); }
  310. using ::sqrt;
  311. inline float
  312. sqrt(float __x)
  313. { return __builtin_sqrtf(__x); }
  314. inline long double
  315. sqrt(long double __x)
  316. { return __builtin_sqrtl(__x); }
  317. template<typename _Tp>
  318. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  319. double>::__type
  320. sqrt(_Tp __x)
  321. { return __builtin_sqrt(__x); }
  322. using ::tan;
  323. inline float
  324. tan(float __x)
  325. { return __builtin_tanf(__x); }
  326. inline long double
  327. tan(long double __x)
  328. { return __builtin_tanl(__x); }
  329. template<typename _Tp>
  330. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  331. double>::__type
  332. tan(_Tp __x)
  333. { return __builtin_tan(__x); }
  334. using ::tanh;
  335. inline float
  336. tanh(float __x)
  337. { return __builtin_tanhf(__x); }
  338. inline long double
  339. tanh(long double __x)
  340. { return __builtin_tanhl(__x); }
  341. template<typename _Tp>
  342. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  343. double>::__type
  344. tanh(_Tp __x)
  345. { return __builtin_tanh(__x); }
  346. #if _GLIBCXX_USE_C99_MATH
  347. #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
  348. // These are possible macros imported from C99-land.
  349. #undef fpclassify
  350. #undef isfinite
  351. #undef isinf
  352. #undef isnan
  353. #undef isnormal
  354. #undef signbit
  355. #undef isgreater
  356. #undef isgreaterequal
  357. #undef isless
  358. #undef islessequal
  359. #undef islessgreater
  360. #undef isunordered
  361. template<typename _Tp>
  362. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  363. int>::__type
  364. fpclassify(_Tp __f)
  365. {
  366. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  367. return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
  368. FP_SUBNORMAL, FP_ZERO, __type(__f));
  369. }
  370. template<typename _Tp>
  371. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  372. int>::__type
  373. isfinite(_Tp __f)
  374. {
  375. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  376. return __builtin_isfinite(__type(__f));
  377. }
  378. template<typename _Tp>
  379. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  380. int>::__type
  381. isinf(_Tp __f)
  382. {
  383. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  384. return __builtin_isinf(__type(__f));
  385. }
  386. template<typename _Tp>
  387. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  388. int>::__type
  389. isnan(_Tp __f)
  390. {
  391. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  392. return __builtin_isnan(__type(__f));
  393. }
  394. template<typename _Tp>
  395. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  396. int>::__type
  397. isnormal(_Tp __f)
  398. {
  399. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  400. return __builtin_isnormal(__type(__f));
  401. }
  402. template<typename _Tp>
  403. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  404. int>::__type
  405. signbit(_Tp __f)
  406. {
  407. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  408. return __builtin_signbit(__type(__f));
  409. }
  410. template<typename _Tp>
  411. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  412. int>::__type
  413. isgreater(_Tp __f1, _Tp __f2)
  414. {
  415. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  416. return __builtin_isgreater(__type(__f1), __type(__f2));
  417. }
  418. template<typename _Tp>
  419. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  420. int>::__type
  421. isgreaterequal(_Tp __f1, _Tp __f2)
  422. {
  423. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  424. return __builtin_isgreaterequal(__type(__f1), __type(__f2));
  425. }
  426. template<typename _Tp>
  427. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  428. int>::__type
  429. isless(_Tp __f1, _Tp __f2)
  430. {
  431. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  432. return __builtin_isless(__type(__f1), __type(__f2));
  433. }
  434. template<typename _Tp>
  435. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  436. int>::__type
  437. islessequal(_Tp __f1, _Tp __f2)
  438. {
  439. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  440. return __builtin_islessequal(__type(__f1), __type(__f2));
  441. }
  442. template<typename _Tp>
  443. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  444. int>::__type
  445. islessgreater(_Tp __f1, _Tp __f2)
  446. {
  447. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  448. return __builtin_islessgreater(__type(__f1), __type(__f2));
  449. }
  450. template<typename _Tp>
  451. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  452. int>::__type
  453. isunordered(_Tp __f1, _Tp __f2)
  454. {
  455. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  456. return __builtin_isunordered(__type(__f1), __type(__f2));
  457. }
  458. #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
  459. #endif /* _GLIBCXX_USE_C99_MATH */
  460. _GLIBCXX_END_NAMESPACE_VERSION
  461. } // namespace std
  462. #endif