Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
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.

169 lines
7.1 KiB

  1. /* $OpenBSD: tgmath.h,v 1.1 2011/07/08 19:28:06 martynas Exp $ */
  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: src/include/tgmath.h,v 1.5 2007/02/02 18:30:23 schweikh Exp $
  28. */
  29. #ifndef _TGMATH_H_
  30. #define _TGMATH_H_
  31. #include <complex.h>
  32. #include <math.h>
  33. /*
  34. * This implementation of <tgmath.h> requires two implementation-dependent
  35. * macros to be defined:
  36. * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...)
  37. * Invokes fnl() if the corresponding real type of x, y or z is long
  38. * double, fn() if it is double or any has an integer type, and fnf()
  39. * otherwise.
  40. * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)
  41. * Invokes [c]fnl() if the corresponding real type of x, y or z is long
  42. * double, [c]fn() if it is double or any has an integer type, and
  43. * [c]fnf() otherwise. The function with the 'c' prefix is called if
  44. * any of x, y or z is a complex number.
  45. * Both macros call the chosen function with all additional arguments passed
  46. * to them, as given by __VA_ARGS__.
  47. *
  48. * Note that these macros cannot be implemented with C's ?: operator,
  49. * because the return type of the whole expression would incorrectly be long
  50. * double complex regardless of the argument types.
  51. */
  52. #if __GNUC_PREREQ__(3, 1)
  53. #define __tg_type(e, t) __builtin_types_compatible_p(__typeof__(e), t)
  54. #define __tg_type3(e1, e2, e3, t) \
  55. (__tg_type(e1, t) || __tg_type(e2, t) || __tg_type(e3, t))
  56. #define __tg_type_corr(e1, e2, e3, t) \
  57. (__tg_type3(e1, e2, e3, t) || __tg_type3(e1, e2, e3, t _Complex))
  58. #define __tg_integer(e1, e2, e3) \
  59. (((__typeof__(e1))1.5 == 1) || ((__typeof__(e2))1.5 == 1) || \
  60. ((__typeof__(e3))1.5 == 1))
  61. #define __tg_is_complex(e1, e2, e3) \
  62. (__tg_type3(e1, e2, e3, float _Complex) || \
  63. __tg_type3(e1, e2, e3, double _Complex) || \
  64. __tg_type3(e1, e2, e3, long double _Complex) || \
  65. __tg_type3(e1, e2, e3, __typeof__(_Complex_I)))
  66. #define __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) \
  67. __builtin_choose_expr(__tg_type_corr(x, y, z, long double), \
  68. fnl(__VA_ARGS__), __builtin_choose_expr( \
  69. __tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\
  70. fn(__VA_ARGS__), fnf(__VA_ARGS__)))
  71. #define __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...) \
  72. __builtin_choose_expr(__tg_is_complex(x, y, z), \
  73. __tg_impl_simple(x, y, z, cfn, cfnf, cfnl, __VA_ARGS__), \
  74. __tg_impl_simple(x, y, z, fn, fnf, fnl, __VA_ARGS__))
  75. #else /* __GNUC__ */
  76. #error "<tgmath.h> not implemented for this compiler"
  77. #endif /* !__GNUC__ */
  78. /* Macros to save lots of repetition below */
  79. #define __tg_simple(x, fn) \
  80. __tg_impl_simple(x, x, x, fn, fn##f, fn##l, x)
  81. #define __tg_simple2(x, y, fn) \
  82. __tg_impl_simple(x, x, y, fn, fn##f, fn##l, x, y)
  83. #define __tg_simplev(x, fn, ...) \
  84. __tg_impl_simple(x, x, x, fn, fn##f, fn##l, __VA_ARGS__)
  85. #define __tg_full(x, fn) \
  86. __tg_impl_full(x, x, x, fn, fn##f, fn##l, c##fn, c##fn##f, c##fn##l, x)
  87. /* 7.22#4 -- These macros expand to real or complex functions, depending on
  88. * the type of their arguments. */
  89. #define acos(x) __tg_full(x, acos)
  90. #define asin(x) __tg_full(x, asin)
  91. #define atan(x) __tg_full(x, atan)
  92. #define acosh(x) __tg_full(x, acosh)
  93. #define asinh(x) __tg_full(x, asinh)
  94. #define atanh(x) __tg_full(x, atanh)
  95. #define cos(x) __tg_full(x, cos)
  96. #define sin(x) __tg_full(x, sin)
  97. #define tan(x) __tg_full(x, tan)
  98. #define cosh(x) __tg_full(x, cosh)
  99. #define sinh(x) __tg_full(x, sinh)
  100. #define tanh(x) __tg_full(x, tanh)
  101. #define exp(x) __tg_full(x, exp)
  102. #define log(x) __tg_full(x, log)
  103. #define pow(x, y) __tg_impl_full(x, x, y, pow, powf, powl, \
  104. cpow, cpowf, cpowl, x, y)
  105. #define sqrt(x) __tg_full(x, sqrt)
  106. /* "The corresponding type-generic macro for fabs and cabs is fabs." */
  107. #define fabs(x) __tg_impl_full(x, x, x, fabs, fabsf, fabsl, \
  108. cabs, cabsf, cabsl, x)
  109. /* 7.22#5 -- These macros are only defined for arguments with real type. */
  110. #define atan2(x, y) __tg_simple2(x, y, atan2)
  111. #define cbrt(x) __tg_simple(x, cbrt)
  112. #define ceil(x) __tg_simple(x, ceil)
  113. #define copysign(x, y) __tg_simple2(x, y, copysign)
  114. #define erf(x) __tg_simple(x, erf)
  115. #define erfc(x) __tg_simple(x, erfc)
  116. #define exp2(x) __tg_simple(x, exp2)
  117. #define expm1(x) __tg_simple(x, expm1)
  118. #define fdim(x, y) __tg_simple2(x, y, fdim)
  119. #define floor(x) __tg_simple(x, floor)
  120. #define fma(x, y, z) __tg_impl_simple(x, y, z, fma, fmaf, fmal, x, y, z)
  121. #define fmax(x, y) __tg_simple2(x, y, fmax)
  122. #define fmin(x, y) __tg_simple2(x, y, fmin)
  123. #define fmod(x, y) __tg_simple2(x, y, fmod)
  124. #define frexp(x, y) __tg_simplev(x, frexp, x, y)
  125. #define hypot(x, y) __tg_simple2(x, y, hypot)
  126. #define ilogb(x) __tg_simple(x, ilogb)
  127. #define ldexp(x, y) __tg_simplev(x, ldexp, x, y)
  128. #define lgamma(x) __tg_simple(x, lgamma)
  129. #define llrint(x) __tg_simple(x, llrint)
  130. #define llround(x) __tg_simple(x, llround)
  131. #define log10(x) __tg_simple(x, log10)
  132. #define log1p(x) __tg_simple(x, log1p)
  133. #define log2(x) __tg_simple(x, log2)
  134. #define logb(x) __tg_simple(x, logb)
  135. #define lrint(x) __tg_simple(x, lrint)
  136. #define lround(x) __tg_simple(x, lround)
  137. #define nearbyint(x) __tg_simple(x, nearbyint)
  138. #define nextafter(x, y) __tg_simple2(x, y, nextafter)
  139. #define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
  140. #define remainder(x, y) __tg_simple2(x, y, remainder)
  141. #define remquo(x, y, z) __tg_impl_simple(x, x, y, remquo, remquof, \
  142. remquol, x, y, z)
  143. #define rint(x) __tg_simple(x, rint)
  144. #define round(x) __tg_simple(x, round)
  145. #define scalbn(x, y) __tg_simplev(x, scalbn, x, y)
  146. #define scalbln(x, y) __tg_simplev(x, scalbln, x, y)
  147. #define tgamma(x) __tg_simple(x, tgamma)
  148. #define trunc(x) __tg_simple(x, trunc)
  149. /* 7.22#6 -- These macros always expand to complex functions. */
  150. #define carg(x) __tg_simple(x, carg)
  151. #define cimag(x) __tg_simple(x, cimag)
  152. #define conj(x) __tg_simple(x, conj)
  153. #define cproj(x) __tg_simple(x, cproj)
  154. #define creal(x) __tg_simple(x, creal)
  155. #endif /* !_TGMATH_H_ */