tgmath.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2004 Stefan Farfeleder.
  5. * All rights reserved.
  6. *
  7. * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #ifndef _TGMATH_H_
  32. #define _TGMATH_H_
  33. #include <complex.h>
  34. #include <math.h>
  35. /*
  36. * This implementation of <tgmath.h> uses the two following macros,
  37. * which are based on the macros described in C11 proposal N1404:
  38. * __tg_impl_simple(x, y, z, fnl, fn, fnf, ...)
  39. * Invokes fnl() if the corresponding real type of x, y or z is long
  40. * double, fn() if it is double or any has an integer type, and fnf()
  41. * otherwise.
  42. * __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)
  43. * Invokes [c]fnl() if the corresponding real type of x or y is long
  44. * double, [c]fn() if it is double or any has an integer type, and
  45. * [c]fnf() otherwise. The function with the 'c' prefix is called if
  46. * any of x or y is a complex number.
  47. * Both macros call the chosen function with all additional arguments passed
  48. * to them, as given by __VA_ARGS__.
  49. *
  50. * Note that these macros cannot be implemented with C's ?: operator,
  51. * because the return type of the whole expression would incorrectly be long
  52. * double complex regardless of the argument types.
  53. *
  54. * The structure of the C11 implementation of these macros can in
  55. * principle be reused for non-C11 compilers, but due to an integer
  56. * promotion bug for complex types in GCC 4.2, simply let non-C11
  57. * compilers use an inefficient yet reliable version.
  58. */
  59. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
  60. __has_extension(c_generic_selections)
  61. #define __tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf) \
  62. _Generic(x, \
  63. long double _Complex: cfnl, \
  64. double _Complex: cfn, \
  65. float _Complex: cfnf, \
  66. long double: fnl, \
  67. default: fn, \
  68. float: fnf \
  69. )
  70. #define __tg_type(x) \
  71. __tg_generic(x, (long double _Complex)0, (double _Complex)0, \
  72. (float _Complex)0, (long double)0, (double)0, (float)0)
  73. #define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \
  74. __tg_generic( \
  75. __tg_type(x) + __tg_type(y) + __tg_type(z), \
  76. fnl, fn, fnf, fnl, fn, fnf)(__VA_ARGS__)
  77. #define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \
  78. __tg_generic( \
  79. __tg_type(x) + __tg_type(y), \
  80. cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__)
  81. #elif defined(__generic)
  82. #define __tg_generic_simple(x, fnl, fn, fnf) \
  83. __generic(x, long double _Complex, fnl, \
  84. __generic(x, double _Complex, fn, \
  85. __generic(x, float _Complex, fnf, \
  86. __generic(x, long double, fnl, \
  87. __generic(x, float, fnf, fn)))))
  88. #define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \
  89. __tg_generic_simple(x, \
  90. __tg_generic_simple(y, \
  91. __tg_generic_simple(z, fnl, fnl, fnl), \
  92. __tg_generic_simple(z, fnl, fnl, fnl), \
  93. __tg_generic_simple(z, fnl, fnl, fnl)), \
  94. __tg_generic_simple(y, \
  95. __tg_generic_simple(z, fnl, fnl, fnl), \
  96. __tg_generic_simple(z, fnl, fn , fn ), \
  97. __tg_generic_simple(z, fnl, fn , fn )), \
  98. __tg_generic_simple(y, \
  99. __tg_generic_simple(z, fnl, fnl, fnl), \
  100. __tg_generic_simple(z, fnl, fn , fn ), \
  101. __tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__)
  102. #define __tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf) \
  103. __generic(x, long double _Complex, cfnl, \
  104. __generic(x, double _Complex, cfn, \
  105. __generic(x, float _Complex, cfnf, \
  106. __generic(x, long double, fnl, \
  107. __generic(x, float, fnf, fn)))))
  108. #define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \
  109. __tg_generic_full(x, \
  110. __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl), \
  111. __tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ), \
  112. __tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf), \
  113. __tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ), \
  114. __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn , fn ), \
  115. __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn , fnf )) \
  116. (__VA_ARGS__)
  117. #else
  118. #error "<tgmath.h> not implemented for this compiler"
  119. #endif
  120. /* Macros to save lots of repetition below */
  121. #define __tg_simple(x, fn) \
  122. __tg_impl_simple(x, x, x, fn##l, fn, fn##f, x)
  123. #define __tg_simple2(x, y, fn) \
  124. __tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y)
  125. #define __tg_simple3(x, y, z, fn) \
  126. __tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z)
  127. #define __tg_simplev(x, fn, ...) \
  128. __tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__)
  129. #define __tg_full(x, fn) \
  130. __tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x)
  131. #define __tg_full2(x, y, fn) \
  132. __tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y)
  133. /* 7.22#4 -- These macros expand to real or complex functions, depending on
  134. * the type of their arguments. */
  135. #define acos(x) __tg_full(x, acos)
  136. #define asin(x) __tg_full(x, asin)
  137. #define atan(x) __tg_full(x, atan)
  138. #define acosh(x) __tg_full(x, acosh)
  139. #define asinh(x) __tg_full(x, asinh)
  140. #define atanh(x) __tg_full(x, atanh)
  141. #define cos(x) __tg_full(x, cos)
  142. #define sin(x) __tg_full(x, sin)
  143. #define tan(x) __tg_full(x, tan)
  144. #define cosh(x) __tg_full(x, cosh)
  145. #define sinh(x) __tg_full(x, sinh)
  146. #define tanh(x) __tg_full(x, tanh)
  147. #define exp(x) __tg_full(x, exp)
  148. #define log(x) __tg_full(x, log)
  149. #define pow(x, y) __tg_full2(x, y, pow)
  150. #define sqrt(x) __tg_full(x, sqrt)
  151. /* "The corresponding type-generic macro for fabs and cabs is fabs." */
  152. #define fabs(x) __tg_impl_full(x, x, cabsl, cabs, cabsf, \
  153. fabsl, fabs, fabsf, x)
  154. /* 7.22#5 -- These macros are only defined for arguments with real type. */
  155. #define atan2(x, y) __tg_simple2(x, y, atan2)
  156. #define cbrt(x) __tg_simple(x, cbrt)
  157. #define ceil(x) __tg_simple(x, ceil)
  158. #define copysign(x, y) __tg_simple2(x, y, copysign)
  159. #define erf(x) __tg_simple(x, erf)
  160. #define erfc(x) __tg_simple(x, erfc)
  161. #define exp2(x) __tg_simple(x, exp2)
  162. #define expm1(x) __tg_simple(x, expm1)
  163. #define fdim(x, y) __tg_simple2(x, y, fdim)
  164. #define floor(x) __tg_simple(x, floor)
  165. #define fma(x, y, z) __tg_simple3(x, y, z, fma)
  166. #define fmax(x, y) __tg_simple2(x, y, fmax)
  167. #define fmin(x, y) __tg_simple2(x, y, fmin)
  168. #define fmod(x, y) __tg_simple2(x, y, fmod)
  169. #define frexp(x, y) __tg_simplev(x, frexp, x, y)
  170. #define hypot(x, y) __tg_simple2(x, y, hypot)
  171. #define ilogb(x) __tg_simple(x, ilogb)
  172. #define ldexp(x, y) __tg_simplev(x, ldexp, x, y)
  173. #define lgamma(x) __tg_simple(x, lgamma)
  174. #define llrint(x) __tg_simple(x, llrint)
  175. #define llround(x) __tg_simple(x, llround)
  176. #define log10(x) __tg_simple(x, log10)
  177. #define log1p(x) __tg_simple(x, log1p)
  178. #define log2(x) __tg_simple(x, log2)
  179. #define logb(x) __tg_simple(x, logb)
  180. #define lrint(x) __tg_simple(x, lrint)
  181. #define lround(x) __tg_simple(x, lround)
  182. #define nearbyint(x) __tg_simple(x, nearbyint)
  183. #define nextafter(x, y) __tg_simple2(x, y, nextafter)
  184. #define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
  185. #define remainder(x, y) __tg_simple2(x, y, remainder)
  186. #define remquo(x, y, z) __tg_impl_simple(x, x, y, remquol, remquo, \
  187. remquof, x, y, z)
  188. #define rint(x) __tg_simple(x, rint)
  189. #define round(x) __tg_simple(x, round)
  190. #define scalbn(x, y) __tg_simplev(x, scalbn, x, y)
  191. #define scalbln(x, y) __tg_simplev(x, scalbln, x, y)
  192. #define tgamma(x) __tg_simple(x, tgamma)
  193. #define trunc(x) __tg_simple(x, trunc)
  194. /* 7.22#6 -- These macros always expand to complex functions. */
  195. #define carg(x) __tg_simple(x, carg)
  196. #define cimag(x) __tg_simple(x, cimag)
  197. #define conj(x) __tg_simple(x, conj)
  198. #define cproj(x) __tg_simple(x, cproj)
  199. #define creal(x) __tg_simple(x, creal)
  200. #endif /* !_TGMATH_H_ */