You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
8.0KB

  1. /* http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/tgmath.h.html */
  2. /*-
  3. * Copyright (c) 2004 Stefan Farfeleder.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #ifndef _TGMATH_H_
  30. #define _TGMATH_H_
  31. #include <complex.h>
  32. #include <math.h>
  33. #ifdef log2
  34. #undef log2
  35. #endif
  36. /*
  37. * This implementation of <tgmath.h> requires two implementation-dependent
  38. * macros to be defined:
  39. * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...)
  40. * Invokes fnl() if the corresponding real type of x, y or z is long
  41. * double, fn() if it is double or any has an integer type, and fnf()
  42. * otherwise.
  43. * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)
  44. * Invokes [c]fnl() if the corresponding real type of x, y or z is long
  45. * double, [c]fn() if it is double or any has an integer type, and
  46. * [c]fnf() otherwise. The function with the 'c' prefix is called if
  47. * any of x, y or z is a complex number.
  48. * Both macros call the chosen function with all additional arguments passed
  49. * to them, as given by __VA_ARGS__.
  50. *
  51. * Note that these macros cannot be implemented with C's ?: operator,
  52. * because the return type of the whole expression would incorrectly be long
  53. * double complex regardless of the argument types.
  54. */
  55. /* requires GCC >= 3.1 */
  56. #if !__GNUC_PREREQ (3, 1)
  57. #error "<tgmath.h> not implemented for this compiler"
  58. #endif
  59. #define __tg_type(__e, __t) \
  60. __builtin_types_compatible_p(__typeof__(__e), __t)
  61. #define __tg_type3(__e1, __e2, __e3, __t) \
  62. (__tg_type(__e1, __t) || __tg_type(__e2, __t) || \
  63. __tg_type(__e3, __t))
  64. #define __tg_type_corr(__e1, __e2, __e3, __t) \
  65. (__tg_type3(__e1, __e2, __e3, __t) || \
  66. __tg_type3(__e1, __e2, __e3, __t _Complex))
  67. #define __tg_integer(__e1, __e2, __e3) \
  68. (((__typeof__(__e1))1.5 == 1) || ((__typeof__(__e2))1.5 == 1) || \
  69. ((__typeof__(__e3))1.5 == 1))
  70. #define __tg_is_complex(__e1, __e2, __e3) \
  71. (__tg_type3(__e1, __e2, __e3, float _Complex) || \
  72. __tg_type3(__e1, __e2, __e3, double _Complex) || \
  73. __tg_type3(__e1, __e2, __e3, long double _Complex) || \
  74. __tg_type3(__e1, __e2, __e3, __typeof__(_Complex_I)))
  75. #if defined (_LDBL_EQ_DBL) || defined (__CYGWIN__)
  76. #define __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) \
  77. __builtin_choose_expr(__tg_type_corr(x, y, z, long double), \
  78. fnl(__VA_ARGS__), __builtin_choose_expr( \
  79. __tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\
  80. fn(__VA_ARGS__), fnf(__VA_ARGS__)))
  81. #else
  82. #define __tg_impl_simple(__x, __y, __z, __fn, __fnf, __fnl, ...) \
  83. (__tg_type_corr(__x, __y, __z, double) || __tg_integer(__x, __y, __z)) \
  84. ? __fn(__VA_ARGS__) : __fnf(__VA_ARGS__)
  85. #endif
  86. #define __tg_impl_full(__x, __y, __z, __fn, __fnf, __fnl, __cfn, __cfnf, __cfnl, ...) \
  87. __builtin_choose_expr(__tg_is_complex(__x, __y, __z), \
  88. __tg_impl_simple(__x, __y, __z, __cfn, __cfnf, __cfnl, __VA_ARGS__), \
  89. __tg_impl_simple(__x, __y, __z, __fn, __fnf, __fnl, __VA_ARGS__))
  90. /* Macros to save lots of repetition below */
  91. #define __tg_simple(__x, __fn) \
  92. __tg_impl_simple(__x, __x, __x, __fn, __fn##f, __fn##l, __x)
  93. #define __tg_simple2(__x, __y, __fn) \
  94. __tg_impl_simple(__x, __x, __y, __fn, __fn##f, __fn##l, __x, __y)
  95. #define __tg_simplev(__x, __fn, ...) \
  96. __tg_impl_simple(__x, __x, __x, __fn, __fn##f, __fn##l, __VA_ARGS__)
  97. #define __tg_full(__x, __fn) \
  98. __tg_impl_full(__x, __x, __x, __fn, __fn##f, __fn##l, c##__fn, c##__fn##f, c##__fn##l, __x)
  99. /* 7.22#4 -- These macros expand to real or complex functions, depending on
  100. * the type of their arguments. */
  101. #define acos(__x) __tg_full(__x, acos)
  102. #define asin(__x) __tg_full(__x, asin)
  103. #define atan(__x) __tg_full(__x, atan)
  104. #define acosh(__x) __tg_full(__x, acosh)
  105. #define asinh(__x) __tg_full(__x, asinh)
  106. #define atanh(__x) __tg_full(__x, atanh)
  107. #define cos(__x) __tg_full(__x, cos)
  108. #define sin(__x) __tg_full(__x, sin)
  109. #define tan(__x) __tg_full(__x, tan)
  110. #define cosh(__x) __tg_full(__x, cosh)
  111. #define sinh(__x) __tg_full(__x, sinh)
  112. #define tanh(__x) __tg_full(__x, tanh)
  113. #define exp(__x) __tg_full(__x, exp)
  114. #define log(__x) __tg_full(__x, log)
  115. #define pow(__x, __y) __tg_impl_full(__x, __x, __y, pow, powf, powl, \
  116. cpow, cpowf, cpowl, __x, __y)
  117. #define sqrt(__x) __tg_full(__x, sqrt)
  118. /* "The corresponding type-generic macro for fabs and cabs is fabs." */
  119. #define fabs(__x) __tg_impl_full(__x, __x, __x, fabs, fabsf, fabsl, \
  120. cabs, cabsf, cabsl, __x)
  121. /* 7.22#5 -- These macros are only defined for arguments with real type. */
  122. #define atan2(__x, __y) __tg_simple2(__x, __y, atan2)
  123. #define cbrt(__x) __tg_simple(__x, cbrt)
  124. #define ceil(__x) __tg_simple(__x, ceil)
  125. #define copysign(__x, __y) __tg_simple2(__x, __y, copysign)
  126. #define erf(__x) __tg_simple(__x, erf)
  127. #define erfc(__x) __tg_simple(__x, erfc)
  128. #define exp2(__x) __tg_simple(__x, exp2)
  129. #define expm1(__x) __tg_simple(__x, expm1)
  130. #define fdim(__x, __y) __tg_simple2(__x, __y, fdim)
  131. #define floor(__x) __tg_simple(__x, floor)
  132. #define fma(__x, __y, __z) __tg_impl_simple(__x, __y, __z, fma, fmaf, fmal, \
  133. __x, __y, __z)
  134. #define fmax(__x, __y) __tg_simple2(__x, __y, fmax)
  135. #define fmin(__x, __y) __tg_simple2(__x, __y, fmin)
  136. #define fmod(__x, __y) __tg_simple2(__x, __y, fmod)
  137. #define frexp(__x, __y) __tg_simplev(__x, frexp, __x, __y)
  138. #define hypot(__x, __y) __tg_simple2(__x, __y, hypot)
  139. #define ilogb(__x) __tg_simple(__x, ilogb)
  140. #define ldexp(__x, __y) __tg_simplev(__x, ldexp, __x, __y)
  141. #define lgamma(__x) __tg_simple(__x, lgamma)
  142. #define llrint(__x) __tg_simple(__x, llrint)
  143. #define llround(__x) __tg_simple(__x, llround)
  144. #define log10(__x) __tg_simple(__x, log10)
  145. #define log1p(__x) __tg_simple(__x, log1p)
  146. #define log2(__x) __tg_simple(__x, log2)
  147. #define logb(__x) __tg_simple(__x, logb)
  148. #define lrint(__x) __tg_simple(__x, lrint)
  149. #define lround(__x) __tg_simple(__x, lround)
  150. #define nearbyint(__x) __tg_simple(__x, nearbyint)
  151. #define nextafter(__x, __y) __tg_simple2(__x, __y, nextafter)
  152. /* not yet implemented even for _LDBL_EQ_DBL platforms */
  153. #ifdef __CYGWIN__
  154. #define nexttoward(__x, __y) __tg_simplev(__x, nexttoward, __x, __y)
  155. #endif
  156. #define remainder(__x, __y) __tg_simple2(__x, __y, remainder)
  157. #define remquo(__x, __y, __z) __tg_impl_simple(__x, __x, __y, remquo, remquof, \
  158. remquol, __x, __y, __z)
  159. #define rint(__x) __tg_simple(__x, rint)
  160. #define round(__x) __tg_simple(__x, round)
  161. #define scalbn(__x, __y) __tg_simplev(__x, scalbn, __x, __y)
  162. #define scalbln(__x, __y) __tg_simplev(__x, scalbln, __x, __y)
  163. #define tgamma(__x) __tg_simple(__x, tgamma)
  164. #define trunc(__x) __tg_simple(__x, trunc)
  165. /* 7.22#6 -- These macros always expand to complex functions. */
  166. #define carg(__x) __tg_simple(__x, carg)
  167. #define cimag(__x) __tg_simple(__x, cimag)
  168. #define conj(__x) __tg_simple(__x, conj)
  169. #define cproj(__x) __tg_simple(__x, cproj)
  170. #define creal(__x) __tg_simple(__x, creal)
  171. #endif /* !_TGMATH_H_ */