mathfunc.m4 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # mathfunc.m4 serial 12
  2. dnl Copyright (C) 2010-2023 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. # gl_MATHFUNC(FUNC, RETTYPE, PARAMTYPES [, INCLUDES] [, EXTRA-CODE])
  7. # ------------------------------------------------------------------
  8. # tests whether the function FUNC is available in libc or libm.
  9. # RETTYPE is the return type. PARAMTYPES is a parameter list, with parentheses.
  10. # It sets FUNC_LIBM to empty or "-lm" accordingly.
  11. AC_DEFUN([gl_MATHFUNC],
  12. [
  13. dnl We need the RETTYPE and PARAMTYPES in order to force linking with the
  14. dnl function.
  15. dnl 1) With gcc >= 4.3 on glibc/x86_64, calls to the 'fabs' function
  16. dnl are inlined by the compiler, therefore linking of these calls does
  17. dnl not require -lm, but taking the function pointer of 'fabs' does.
  18. dnl 2) On MSVC 9, many math functions exist only as macros with arguments,
  19. dnl whereas the function pointer is undefined.
  20. dnl On the other hand, taking just the function pointer is not enough.
  21. dnl 1) On AIX 7.1, when 'long double' is 128 bit large ("xlc -qldbl128" or
  22. dnl "xlc -qlongdouble" or "gcc -mlong-double-128") many math functions
  23. dnl exist as macros with arguments, that may reference libm or even
  24. dnl completely undefined functions such as __rint128.
  25. dnl 2) In AIX 7.1 with gcc 4.2, when optimization is turned on, calls to
  26. dnl rint() with simple arguments are turned into rintf() calls by the
  27. dnl compiler. But while rint() is resides in libc, rintf() is in libm.
  28. m4_pushdef([func], [$1])
  29. m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
  30. [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
  31. m4_pushdef([ARGS], [m4_bpatsubst(
  32. [m4_bpatsubst(
  33. [m4_bpatsubst(
  34. [m4_bpatsubst(
  35. [m4_bpatsubst(
  36. [m4_bpatsubst(
  37. [m4_bpatsubst(
  38. [m4_bpatsubst(
  39. [m4_bpatsubst(
  40. [$3],
  41. [int \*], [&i_ret])],
  42. [float \*], [&f_ret])],
  43. [double \*], [&d_ret])],
  44. [long double \*], [&l_ret])],
  45. [int], [2])],
  46. [float], [1.618034f])],
  47. [long double], [1.618033988749894848L])],
  48. [double], [1.6180339887])],
  49. [void], [])])
  50. FUNC[]_LIBM=
  51. AC_CACHE_CHECK([whether func() can be used without linking with libm],
  52. [gl_cv_func_]func[_no_libm],
  53. [
  54. AC_LINK_IFELSE(
  55. [AC_LANG_PROGRAM(
  56. [[#ifndef __NO_MATH_INLINES
  57. # define __NO_MATH_INLINES 1 /* for glibc */
  58. #endif
  59. #include <math.h>
  60. $4
  61. $2 (* volatile funcptr) $3 = ]func[;
  62. int i_ret;
  63. float f_ret;
  64. double d_ret;
  65. long double l_ret;]],
  66. [[$2 y = funcptr ]ARGS[ + ]func[ ]ARGS[;
  67. $5
  68. return y < 0.3 || y > 1.7;
  69. ]])],
  70. [gl_cv_func_]func[_no_libm=yes],
  71. [gl_cv_func_]func[_no_libm=no])
  72. ])
  73. if test $gl_cv_func_[]func[]_no_libm = no; then
  74. AC_CACHE_CHECK([whether func() can be used with libm],
  75. [gl_cv_func_]func[_in_libm],
  76. [
  77. save_LIBS="$LIBS"
  78. LIBS="$LIBS -lm"
  79. AC_LINK_IFELSE(
  80. [AC_LANG_PROGRAM(
  81. [[#ifndef __NO_MATH_INLINES
  82. # define __NO_MATH_INLINES 1 /* for glibc */
  83. #endif
  84. #include <math.h>
  85. $4
  86. $2 (* volatile funcptr) $3 = ]func[;
  87. int i_ret;
  88. float f_ret;
  89. double d_ret;
  90. long double l_ret;]],
  91. [[$2 y = funcptr ]ARGS[ + ]func[ ]ARGS[;
  92. $5
  93. return y < 0.3 || y > 1.7;
  94. ]])],
  95. [gl_cv_func_]func[_in_libm=yes],
  96. [gl_cv_func_]func[_in_libm=no])
  97. LIBS="$save_LIBS"
  98. ])
  99. if test $gl_cv_func_[]func[]_in_libm = yes; then
  100. FUNC[]_LIBM=-lm
  101. fi
  102. fi
  103. AC_SUBST(FUNC[_LIBM])
  104. m4_popdef([ARGS])
  105. m4_popdef([FUNC])
  106. m4_popdef([func])
  107. ])
  108. # gl_COMMON_DOUBLE_MATHFUNC(FUNC)
  109. # -------------------------------
  110. # tests whether the function FUNC is available in libc or libm.
  111. # It sets FUNC_LIBM to empty or "-lm" accordingly.
  112. # FUNC must be one of the following functions, that are present on all systems
  113. # and provided by libm on all systems except Mac OS X, BeOS, Haiku:
  114. # acos asin atan atan2 cbrt cos cosh erf erfc exp fmod hypot j0 j1 jn lgamma
  115. # log log10 log1p pow remainder sin sinh sqrt tan tanh y0 y1 yn
  116. AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC],
  117. [
  118. AC_REQUIRE([gl_COMMON_DOUBLE_MATHFUNC_TEST])
  119. m4_pushdef([FUNC], [m4_translit([$1],[abcdefghijklmnopqrstuvwxyz],
  120. [ABCDEFGHIJKLMNOPQRSTUVWXYZ])])
  121. FUNC[]_LIBM="$POW_LIBM"
  122. AC_SUBST(FUNC[_LIBM])
  123. m4_popdef([FUNC])
  124. ])
  125. AC_DEFUN([gl_COMMON_DOUBLE_MATHFUNC_TEST],
  126. [
  127. dnl We could use any of the following:
  128. dnl gl_MATHFUNC([acos], [double], [(double)])
  129. dnl gl_MATHFUNC([asin], [double], [(double)])
  130. dnl gl_MATHFUNC([atan], [double], [(double)])
  131. dnl gl_MATHFUNC([atan2], [double], [(double, double)])
  132. dnl gl_MATHFUNC([cbrt], [double], [(double)])
  133. dnl gl_MATHFUNC([cos], [double], [(double)])
  134. dnl gl_MATHFUNC([cosh], [double], [(double)])
  135. dnl gl_MATHFUNC([erf], [double], [(double)])
  136. dnl gl_MATHFUNC([erfc], [double], [(double)])
  137. dnl gl_MATHFUNC([exp], [double], [(double)])
  138. dnl gl_MATHFUNC([fmod], [double], [(double, double)])
  139. dnl gl_MATHFUNC([hypot], [double], [(double, double)])
  140. dnl gl_MATHFUNC([j0], [double], [(double)])
  141. dnl gl_MATHFUNC([j1], [double], [(double)])
  142. dnl gl_MATHFUNC([jn], [double], [(int, double)])
  143. dnl gl_MATHFUNC([lgamma], [double], [(double)])
  144. dnl gl_MATHFUNC([log], [double], [(double)])
  145. dnl gl_MATHFUNC([log10], [double], [(double)])
  146. dnl gl_MATHFUNC([log1p], [double], [(double)])
  147. dnl gl_MATHFUNC([pow], [double], [(double, double)])
  148. dnl gl_MATHFUNC([remainder], [double], [(double, double)])
  149. dnl gl_MATHFUNC([sin], [double], [(double)])
  150. dnl gl_MATHFUNC([sinh], [double], [(double)])
  151. dnl gl_MATHFUNC([sqrt], [double], [(double)])
  152. dnl gl_MATHFUNC([tan], [double], [(double)])
  153. dnl gl_MATHFUNC([tanh], [double], [(double)])
  154. dnl gl_MATHFUNC([y0], [double], [(double)])
  155. dnl gl_MATHFUNC([y1], [double], [(double)])
  156. dnl gl_MATHFUNC([yn], [double], [(int, double)])
  157. gl_MATHFUNC([pow], [double], [(double, double)])
  158. ])