1 | // -*- C++ -*- |
2 | //===---------------------------- cmath -----------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #ifndef _LIBCPP_CMATH |
11 | #define _LIBCPP_CMATH |
12 | |
13 | /* |
14 | cmath synopsis |
15 | |
16 | Macros: |
17 | |
18 | HUGE_VAL |
19 | HUGE_VALF // C99 |
20 | HUGE_VALL // C99 |
21 | INFINITY // C99 |
22 | NAN // C99 |
23 | FP_INFINITE // C99 |
24 | FP_NAN // C99 |
25 | FP_NORMAL // C99 |
26 | FP_SUBNORMAL // C99 |
27 | FP_ZERO // C99 |
28 | FP_FAST_FMA // C99 |
29 | FP_FAST_FMAF // C99 |
30 | FP_FAST_FMAL // C99 |
31 | FP_ILOGB0 // C99 |
32 | FP_ILOGBNAN // C99 |
33 | MATH_ERRNO // C99 |
34 | MATH_ERREXCEPT // C99 |
35 | math_errhandling // C99 |
36 | |
37 | namespace std |
38 | { |
39 | |
40 | Types: |
41 | |
42 | float_t // C99 |
43 | double_t // C99 |
44 | |
45 | // C90 |
46 | |
47 | floating_point abs(floating_point x); |
48 | |
49 | floating_point acos (arithmetic x); |
50 | float acosf(float x); |
51 | long double acosl(long double x); |
52 | |
53 | floating_point asin (arithmetic x); |
54 | float asinf(float x); |
55 | long double asinl(long double x); |
56 | |
57 | floating_point atan (arithmetic x); |
58 | float atanf(float x); |
59 | long double atanl(long double x); |
60 | |
61 | floating_point atan2 (arithmetic y, arithmetic x); |
62 | float atan2f(float y, float x); |
63 | long double atan2l(long double y, long double x); |
64 | |
65 | floating_point ceil (arithmetic x); |
66 | float ceilf(float x); |
67 | long double ceill(long double x); |
68 | |
69 | floating_point cos (arithmetic x); |
70 | float cosf(float x); |
71 | long double cosl(long double x); |
72 | |
73 | floating_point cosh (arithmetic x); |
74 | float coshf(float x); |
75 | long double coshl(long double x); |
76 | |
77 | floating_point exp (arithmetic x); |
78 | float expf(float x); |
79 | long double expl(long double x); |
80 | |
81 | floating_point fabs (arithmetic x); |
82 | float fabsf(float x); |
83 | long double fabsl(long double x); |
84 | |
85 | floating_point floor (arithmetic x); |
86 | float floorf(float x); |
87 | long double floorl(long double x); |
88 | |
89 | floating_point fmod (arithmetic x, arithmetic y); |
90 | float fmodf(float x, float y); |
91 | long double fmodl(long double x, long double y); |
92 | |
93 | floating_point frexp (arithmetic value, int* exp); |
94 | float frexpf(float value, int* exp); |
95 | long double frexpl(long double value, int* exp); |
96 | |
97 | floating_point ldexp (arithmetic value, int exp); |
98 | float ldexpf(float value, int exp); |
99 | long double ldexpl(long double value, int exp); |
100 | |
101 | floating_point log (arithmetic x); |
102 | float logf(float x); |
103 | long double logl(long double x); |
104 | |
105 | floating_point log10 (arithmetic x); |
106 | float log10f(float x); |
107 | long double log10l(long double x); |
108 | |
109 | floating_point modf (floating_point value, floating_point* iptr); |
110 | float modff(float value, float* iptr); |
111 | long double modfl(long double value, long double* iptr); |
112 | |
113 | floating_point pow (arithmetic x, arithmetic y); |
114 | float powf(float x, float y); |
115 | long double powl(long double x, long double y); |
116 | |
117 | floating_point sin (arithmetic x); |
118 | float sinf(float x); |
119 | long double sinl(long double x); |
120 | |
121 | floating_point sinh (arithmetic x); |
122 | float sinhf(float x); |
123 | long double sinhl(long double x); |
124 | |
125 | floating_point sqrt (arithmetic x); |
126 | float sqrtf(float x); |
127 | long double sqrtl(long double x); |
128 | |
129 | floating_point tan (arithmetic x); |
130 | float tanf(float x); |
131 | long double tanl(long double x); |
132 | |
133 | floating_point tanh (arithmetic x); |
134 | float tanhf(float x); |
135 | long double tanhl(long double x); |
136 | |
137 | // C99 |
138 | |
139 | bool signbit(arithmetic x); |
140 | |
141 | int fpclassify(arithmetic x); |
142 | |
143 | bool isfinite(arithmetic x); |
144 | bool isinf(arithmetic x); |
145 | bool isnan(arithmetic x); |
146 | bool isnormal(arithmetic x); |
147 | |
148 | bool isgreater(arithmetic x, arithmetic y); |
149 | bool isgreaterequal(arithmetic x, arithmetic y); |
150 | bool isless(arithmetic x, arithmetic y); |
151 | bool islessequal(arithmetic x, arithmetic y); |
152 | bool islessgreater(arithmetic x, arithmetic y); |
153 | bool isunordered(arithmetic x, arithmetic y); |
154 | |
155 | floating_point acosh (arithmetic x); |
156 | float acoshf(float x); |
157 | long double acoshl(long double x); |
158 | |
159 | floating_point asinh (arithmetic x); |
160 | float asinhf(float x); |
161 | long double asinhl(long double x); |
162 | |
163 | floating_point atanh (arithmetic x); |
164 | float atanhf(float x); |
165 | long double atanhl(long double x); |
166 | |
167 | floating_point cbrt (arithmetic x); |
168 | float cbrtf(float x); |
169 | long double cbrtl(long double x); |
170 | |
171 | floating_point copysign (arithmetic x, arithmetic y); |
172 | float copysignf(float x, float y); |
173 | long double copysignl(long double x, long double y); |
174 | |
175 | floating_point erf (arithmetic x); |
176 | float erff(float x); |
177 | long double erfl(long double x); |
178 | |
179 | floating_point erfc (arithmetic x); |
180 | float erfcf(float x); |
181 | long double erfcl(long double x); |
182 | |
183 | floating_point exp2 (arithmetic x); |
184 | float exp2f(float x); |
185 | long double exp2l(long double x); |
186 | |
187 | floating_point expm1 (arithmetic x); |
188 | float expm1f(float x); |
189 | long double expm1l(long double x); |
190 | |
191 | floating_point fdim (arithmetic x, arithmetic y); |
192 | float fdimf(float x, float y); |
193 | long double fdiml(long double x, long double y); |
194 | |
195 | floating_point fma (arithmetic x, arithmetic y, arithmetic z); |
196 | float fmaf(float x, float y, float z); |
197 | long double fmal(long double x, long double y, long double z); |
198 | |
199 | floating_point fmax (arithmetic x, arithmetic y); |
200 | float fmaxf(float x, float y); |
201 | long double fmaxl(long double x, long double y); |
202 | |
203 | floating_point fmin (arithmetic x, arithmetic y); |
204 | float fminf(float x, float y); |
205 | long double fminl(long double x, long double y); |
206 | |
207 | floating_point hypot (arithmetic x, arithmetic y); |
208 | float hypotf(float x, float y); |
209 | long double hypotl(long double x, long double y); |
210 | |
211 | double hypot(double x, double y, double z); // C++17 |
212 | float hypot(float x, float y, float z); // C++17 |
213 | long double hypot(long double x, long double y, long double z); // C++17 |
214 | |
215 | int ilogb (arithmetic x); |
216 | int ilogbf(float x); |
217 | int ilogbl(long double x); |
218 | |
219 | floating_point lgamma (arithmetic x); |
220 | float lgammaf(float x); |
221 | long double lgammal(long double x); |
222 | |
223 | long long llrint (arithmetic x); |
224 | long long llrintf(float x); |
225 | long long llrintl(long double x); |
226 | |
227 | long long llround (arithmetic x); |
228 | long long llroundf(float x); |
229 | long long llroundl(long double x); |
230 | |
231 | floating_point log1p (arithmetic x); |
232 | float log1pf(float x); |
233 | long double log1pl(long double x); |
234 | |
235 | floating_point log2 (arithmetic x); |
236 | float log2f(float x); |
237 | long double log2l(long double x); |
238 | |
239 | floating_point logb (arithmetic x); |
240 | float logbf(float x); |
241 | long double logbl(long double x); |
242 | |
243 | long lrint (arithmetic x); |
244 | long lrintf(float x); |
245 | long lrintl(long double x); |
246 | |
247 | long lround (arithmetic x); |
248 | long lroundf(float x); |
249 | long lroundl(long double x); |
250 | |
251 | double nan (const char* str); |
252 | float nanf(const char* str); |
253 | long double nanl(const char* str); |
254 | |
255 | floating_point nearbyint (arithmetic x); |
256 | float nearbyintf(float x); |
257 | long double nearbyintl(long double x); |
258 | |
259 | floating_point nextafter (arithmetic x, arithmetic y); |
260 | float nextafterf(float x, float y); |
261 | long double nextafterl(long double x, long double y); |
262 | |
263 | floating_point nexttoward (arithmetic x, long double y); |
264 | float nexttowardf(float x, long double y); |
265 | long double nexttowardl(long double x, long double y); |
266 | |
267 | floating_point remainder (arithmetic x, arithmetic y); |
268 | float remainderf(float x, float y); |
269 | long double remainderl(long double x, long double y); |
270 | |
271 | floating_point remquo (arithmetic x, arithmetic y, int* pquo); |
272 | float remquof(float x, float y, int* pquo); |
273 | long double remquol(long double x, long double y, int* pquo); |
274 | |
275 | floating_point rint (arithmetic x); |
276 | float rintf(float x); |
277 | long double rintl(long double x); |
278 | |
279 | floating_point round (arithmetic x); |
280 | float roundf(float x); |
281 | long double roundl(long double x); |
282 | |
283 | floating_point scalbln (arithmetic x, long ex); |
284 | float scalblnf(float x, long ex); |
285 | long double scalblnl(long double x, long ex); |
286 | |
287 | floating_point scalbn (arithmetic x, int ex); |
288 | float scalbnf(float x, int ex); |
289 | long double scalbnl(long double x, int ex); |
290 | |
291 | floating_point tgamma (arithmetic x); |
292 | float tgammaf(float x); |
293 | long double tgammal(long double x); |
294 | |
295 | floating_point trunc (arithmetic x); |
296 | float truncf(float x); |
297 | long double truncl(long double x); |
298 | |
299 | } // std |
300 | |
301 | */ |
302 | |
303 | #include <__config> |
304 | #include <math.h> |
305 | #include <version> |
306 | |
307 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
308 | #pragma GCC system_header |
309 | #endif |
310 | |
311 | _LIBCPP_BEGIN_NAMESPACE_STD |
312 | |
313 | using ::signbit; |
314 | using ::fpclassify; |
315 | using ::isfinite; |
316 | using ::isinf; |
317 | using ::isnan; |
318 | using ::isnormal; |
319 | using ::isgreater; |
320 | using ::isgreaterequal; |
321 | using ::isless; |
322 | using ::islessequal; |
323 | using ::islessgreater; |
324 | using ::isunordered; |
325 | using ::isunordered; |
326 | |
327 | using ::float_t; |
328 | using ::double_t; |
329 | |
330 | #ifndef _AIX |
331 | using ::abs; |
332 | #endif |
333 | |
334 | using ::acos; |
335 | using ::acosf; |
336 | using ::asin; |
337 | using ::asinf; |
338 | using ::atan; |
339 | using ::atanf; |
340 | using ::atan2; |
341 | using ::atan2f; |
342 | using ::ceil; |
343 | using ::ceilf; |
344 | using ::cos; |
345 | using ::cosf; |
346 | using ::cosh; |
347 | using ::coshf; |
348 | |
349 | using ::exp; |
350 | using ::expf; |
351 | |
352 | using ::fabs; |
353 | using ::fabsf; |
354 | using ::floor; |
355 | using ::floorf; |
356 | |
357 | using ::fmod; |
358 | using ::fmodf; |
359 | |
360 | using ::frexp; |
361 | using ::frexpf; |
362 | using ::ldexp; |
363 | using ::ldexpf; |
364 | |
365 | using ::log; |
366 | using ::logf; |
367 | |
368 | using ::log10; |
369 | using ::log10f; |
370 | using ::modf; |
371 | using ::modff; |
372 | |
373 | using ::pow; |
374 | using ::powf; |
375 | |
376 | using ::sin; |
377 | using ::sinf; |
378 | using ::sinh; |
379 | using ::sinhf; |
380 | |
381 | using ::sqrt; |
382 | using ::sqrtf; |
383 | using ::tan; |
384 | using ::tanf; |
385 | |
386 | using ::tanh; |
387 | using ::tanhf; |
388 | |
389 | using ::acosh; |
390 | using ::acoshf; |
391 | using ::asinh; |
392 | using ::asinhf; |
393 | using ::atanh; |
394 | using ::atanhf; |
395 | using ::cbrt; |
396 | using ::cbrtf; |
397 | |
398 | using ::copysign; |
399 | using ::copysignf; |
400 | |
401 | using ::erf; |
402 | using ::erff; |
403 | using ::erfc; |
404 | using ::erfcf; |
405 | using ::exp2; |
406 | using ::exp2f; |
407 | using ::expm1; |
408 | using ::expm1f; |
409 | using ::fdim; |
410 | using ::fdimf; |
411 | using ::fmaf; |
412 | using ::fma; |
413 | using ::fmax; |
414 | using ::fmaxf; |
415 | using ::fmin; |
416 | using ::fminf; |
417 | using ::hypot; |
418 | using ::hypotf; |
419 | using ::ilogb; |
420 | using ::ilogbf; |
421 | using ::lgamma; |
422 | using ::lgammaf; |
423 | using ::llrint; |
424 | using ::llrintf; |
425 | using ::llround; |
426 | using ::llroundf; |
427 | using ::log1p; |
428 | using ::log1pf; |
429 | using ::log2; |
430 | using ::log2f; |
431 | using ::logb; |
432 | using ::logbf; |
433 | using ::lrint; |
434 | using ::lrintf; |
435 | using ::lround; |
436 | using ::lroundf; |
437 | |
438 | using ::nan; |
439 | using ::nanf; |
440 | |
441 | using ::nearbyint; |
442 | using ::nearbyintf; |
443 | using ::nextafter; |
444 | using ::nextafterf; |
445 | using ::nexttoward; |
446 | using ::nexttowardf; |
447 | using ::remainder; |
448 | using ::remainderf; |
449 | using ::remquo; |
450 | using ::remquof; |
451 | using ::rint; |
452 | using ::rintf; |
453 | using ::round; |
454 | using ::roundf; |
455 | using ::scalbln; |
456 | using ::scalblnf; |
457 | using ::scalbn; |
458 | using ::scalbnf; |
459 | using ::tgamma; |
460 | using ::tgammaf; |
461 | using ::trunc; |
462 | using ::truncf; |
463 | |
464 | using ::acosl; |
465 | using ::asinl; |
466 | using ::atanl; |
467 | using ::atan2l; |
468 | using ::ceill; |
469 | using ::cosl; |
470 | using ::coshl; |
471 | using ::expl; |
472 | using ::fabsl; |
473 | using ::floorl; |
474 | using ::fmodl; |
475 | using ::frexpl; |
476 | using ::ldexpl; |
477 | using ::logl; |
478 | using ::log10l; |
479 | using ::modfl; |
480 | using ::powl; |
481 | using ::sinl; |
482 | using ::sinhl; |
483 | using ::sqrtl; |
484 | using ::tanl; |
485 | |
486 | using ::tanhl; |
487 | using ::acoshl; |
488 | using ::asinhl; |
489 | using ::atanhl; |
490 | using ::cbrtl; |
491 | |
492 | using ::copysignl; |
493 | |
494 | using ::erfl; |
495 | using ::erfcl; |
496 | using ::exp2l; |
497 | using ::expm1l; |
498 | using ::fdiml; |
499 | using ::fmal; |
500 | using ::fmaxl; |
501 | using ::fminl; |
502 | using ::hypotl; |
503 | using ::ilogbl; |
504 | using ::lgammal; |
505 | using ::llrintl; |
506 | using ::llroundl; |
507 | using ::log1pl; |
508 | using ::log2l; |
509 | using ::logbl; |
510 | using ::lrintl; |
511 | using ::lroundl; |
512 | using ::nanl; |
513 | using ::nearbyintl; |
514 | using ::nextafterl; |
515 | using ::nexttowardl; |
516 | using ::remainderl; |
517 | using ::remquol; |
518 | using ::rintl; |
519 | using ::roundl; |
520 | using ::scalblnl; |
521 | using ::scalbnl; |
522 | using ::tgammal; |
523 | using ::truncl; |
524 | |
525 | #if _LIBCPP_STD_VER > 14 |
526 | inline _LIBCPP_INLINE_VISIBILITY float hypot( float x, float y, float z ) { return sqrt(x*x + y*y + z*z); } |
527 | inline _LIBCPP_INLINE_VISIBILITY double hypot( double x, double y, double z ) { return sqrt(x*x + y*y + z*z); } |
528 | inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double x, long double y, long double z ) { return sqrt(x*x + y*y + z*z); } |
529 | |
530 | template <class _A1, class _A2, class _A3> |
531 | inline _LIBCPP_INLINE_VISIBILITY |
532 | typename _EnableIf |
533 | < |
534 | is_arithmetic<_A1>::value && |
535 | is_arithmetic<_A2>::value && |
536 | is_arithmetic<_A3>::value, |
537 | __promote<_A1, _A2, _A3> |
538 | >::type |
539 | hypot(_A1 __lcpp_x, _A2 __lcpp_y, _A3 __lcpp_z) _NOEXCEPT |
540 | { |
541 | typedef typename __promote<_A1, _A2, _A3>::type __result_type; |
542 | static_assert((!(is_same<_A1, __result_type>::value && |
543 | is_same<_A2, __result_type>::value && |
544 | is_same<_A3, __result_type>::value)), "" ); |
545 | return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z); |
546 | } |
547 | #endif |
548 | |
549 | template <class _A1> |
550 | _LIBCPP_INLINE_VISIBILITY |
551 | _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type |
552 | __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT |
553 | { |
554 | #if __has_builtin(__builtin_isnan) |
555 | return __builtin_isnan(__lcpp_x); |
556 | #else |
557 | return isnan(__lcpp_x); |
558 | #endif |
559 | } |
560 | |
561 | template <class _A1> |
562 | _LIBCPP_INLINE_VISIBILITY |
563 | _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type |
564 | __libcpp_isnan_or_builtin(_A1 __lcpp_x) _NOEXCEPT |
565 | { |
566 | return isnan(__lcpp_x); |
567 | } |
568 | |
569 | template <class _A1> |
570 | _LIBCPP_INLINE_VISIBILITY |
571 | _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type |
572 | __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT |
573 | { |
574 | #if __has_builtin(__builtin_isinf) |
575 | return __builtin_isinf(__lcpp_x); |
576 | #else |
577 | return isinf(__lcpp_x); |
578 | #endif |
579 | } |
580 | |
581 | template <class _A1> |
582 | _LIBCPP_INLINE_VISIBILITY |
583 | _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type |
584 | __libcpp_isinf_or_builtin(_A1 __lcpp_x) _NOEXCEPT |
585 | { |
586 | return isinf(__lcpp_x); |
587 | } |
588 | |
589 | template <class _A1> |
590 | _LIBCPP_INLINE_VISIBILITY |
591 | _LIBCPP_CONSTEXPR typename enable_if<is_floating_point<_A1>::value, bool>::type |
592 | __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT |
593 | { |
594 | #if __has_builtin(__builtin_isfinite) |
595 | return __builtin_isfinite(__lcpp_x); |
596 | #else |
597 | return isfinite(__lcpp_x); |
598 | #endif |
599 | } |
600 | |
601 | template <class _A1> |
602 | _LIBCPP_INLINE_VISIBILITY |
603 | _LIBCPP_CONSTEXPR typename enable_if<!is_floating_point<_A1>::value, bool>::type |
604 | __libcpp_isfinite_or_builtin(_A1 __lcpp_x) _NOEXCEPT |
605 | { |
606 | return isfinite(__lcpp_x); |
607 | } |
608 | |
609 | #if _LIBCPP_STD_VER > 17 |
610 | template <typename _Fp> |
611 | constexpr |
612 | _Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept { |
613 | if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0)) |
614 | return __t * __b + (1 - __t) * __a; |
615 | |
616 | if (__t == 1) return __b; |
617 | const _Fp __x = __a + __t * (__b - __a); |
618 | if (__t > 1 == __b > __a) |
619 | return __b < __x ? __x : __b; |
620 | else |
621 | return __x < __b ? __x : __b; |
622 | } |
623 | |
624 | constexpr float |
625 | lerp(float __a, float __b, float __t) _NOEXCEPT { return __lerp(__a, __b, __t); } |
626 | |
627 | constexpr double |
628 | lerp(double __a, double __b, double __t) _NOEXCEPT { return __lerp(__a, __b, __t); } |
629 | |
630 | constexpr long double |
631 | lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); } |
632 | |
633 | #endif // _LIBCPP_STD_VER > 17 |
634 | |
635 | _LIBCPP_END_NAMESPACE_STD |
636 | |
637 | #endif // _LIBCPP_CMATH |
638 | |