trigd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Implementation of the degree trignometric functions COSD, SIND, TAND.
  2. Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. Contributed by Steven G. Kargl <kargl@gcc.gnu.org>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <math.h>
  22. /* Body of library functions which are cannot be implemented on the current
  23. * platform because it lacks a capability, such as an underlying trigonometric
  24. * function (sin, cos, tan) or C99 floating-point function (fabs, fmod). */
  25. #define STRINGIFY_EXPAND(x) #x
  26. #define ERROR_RETURN(f, k, x) runtime_error (#f " is unavailable for" \
  27. " REAL(KIND=" STRINGIFY_EXPAND(k) ") because the system math library" \
  28. " lacks support for it"); \
  29. RETURN(x)
  30. /*
  31. For real x, let {x}_P or x_P be the closest representible number in the
  32. floating point representation which uses P binary bits of fractional
  33. precision (with IEEE rounding semantics).
  34. Similarly, let f_P(x) be shorthand for {f(x)}_P.
  35. Let ulp_P(x) be the unit of least precision for x: in other words the
  36. maximal value of |a_P - b_P| where a_P <= x <= b_P and a_P != b_P.
  37. Let x ~= y <-> | x - y | < ulp_P(x - y).
  38. Let deg(x) be the value of x radians in degrees.
  39. Values for each precision P were selected as follows.
  40. COSD_SMALL = 2**{-N} such that for all x <= COSD_SMALL:
  41. * cos(deg(x)) ~= 1, or equivalently:
  42. | 1 - cos(deg(x)) | < ulp_P(1).
  43. Unfortunately for SIND (and therefore TAND) a similar relation is only
  44. possible for REAL(4) and REAL(8). With REAL(10) and REAL(16), enough
  45. precision is available such that sin_P(x) != x_P for some x less than any
  46. value. (There are values where this equality holds, but the distance has
  47. inflection points.)
  48. For REAL(4) and REAL(8), we can select SIND_SMALL such that:
  49. * sin(deg(x)) ~= deg(x), or equivalently:
  50. | deg(x) - sin(deg(x)) | < ulp_P(deg(x)).
  51. */
  52. #ifdef HAVE_GFC_REAL_4
  53. /* Build _gfortran_sind_r4, _gfortran_cosd_r4, and _gfortran_tand_r4 */
  54. #define KIND 4
  55. #define TINY 0x1.p-100 /* ~= 7.889e-31 */
  56. #define COSD_SMALL 0x1.p-7 /* = 7.8125e-3 */
  57. #define SIND_SMALL 0x1.p-5 /* = 3.125e-2 */
  58. #define COSD30 8.66025388e-01
  59. #define PIO180H 1.74560547e-02 /* high 12 bits. */
  60. #define PIO180L -2.76216747e-06 /* Next 24 bits. */
  61. #if defined(HAVE_FABSF) && defined(HAVE_FMODF) && defined(HAVE_COPYSIGNF)
  62. #ifdef HAVE_SINF
  63. #define ENABLE_SIND
  64. #endif
  65. #ifdef HAVE_COSF
  66. #define ENABLE_COSD
  67. #endif
  68. #ifdef HAVE_TANF
  69. #define ENABLE_TAND
  70. #endif
  71. #endif /* HAVE_FABSF && HAVE_FMODF && HAVE_COPYSIGNF */
  72. #ifdef GFC_REAL_4_INFINITY
  73. #define HAVE_INFINITY_KIND
  74. #endif
  75. #include "trigd_lib.inc"
  76. #undef KIND
  77. #undef TINY
  78. #undef COSD_SMALL
  79. #undef SIND_SMALL
  80. #undef COSD30
  81. #undef PIO180H
  82. #undef PIO180L
  83. #undef ENABLE_SIND
  84. #undef ENABLE_COSD
  85. #undef ENABLE_TAND
  86. #undef HAVE_INFINITY_KIND
  87. #endif /* HAVE_GFC_REAL_4... */
  88. #ifdef HAVE_GFC_REAL_8
  89. /* Build _gfortran_sind_r8, _gfortran_cosd_r8, and _gfortran_tand_r8 */
  90. #define KIND 8
  91. #define TINY 0x1.p-1000 /* ~= 9.33e-302 (min exp -1074) */
  92. #define COSD_SMALL 0x1.p-21 /* ~= 4.768e-7 */
  93. #define SIND_SMALL 0x1.p-19 /* ~= 9.537e-7 */
  94. #define COSD30 8.6602540378443860e-01
  95. #define PIO180H 1.7453283071517944e-02 /* high 21 bits. */
  96. #define PIO180L 9.4484253514332993e-09 /* Next 53 bits. */
  97. #if defined(HAVE_FABS) && defined(HAVE_FMOD) && defined(HAVE_COPYSIGN)
  98. #ifdef HAVE_SIN
  99. #define ENABLE_SIND
  100. #endif
  101. #ifdef HAVE_COS
  102. #define ENABLE_COSD
  103. #endif
  104. #ifdef HAVE_TAN
  105. #define ENABLE_TAND
  106. #endif
  107. #endif /* HAVE_FABS && HAVE_FMOD && HAVE_COPYSIGN */
  108. #ifdef GFC_REAL_8_INFINITY
  109. #define HAVE_INFINITY_KIND
  110. #endif
  111. #include "trigd_lib.inc"
  112. #undef KIND
  113. #undef TINY
  114. #undef COSD_SMALL
  115. #undef SIND_SMALL
  116. #undef COSD30
  117. #undef PIO180H
  118. #undef PIO180L
  119. #undef ENABLE_SIND
  120. #undef ENABLE_COSD
  121. #undef ENABLE_TAND
  122. #undef HAVE_INFINITY_KIND
  123. #endif /* HAVE_GFC_REAL_8... */
  124. #ifdef HAVE_GFC_REAL_10
  125. /* Build _gfortran_sind_r10, _gfortran_cosd_r10, and _gfortran_tand_r10 */
  126. #define KIND 10
  127. #define TINY 0x1.p-16400 /* ~= 1.28e-4937 (min exp -16494) */
  128. #define COSD_SMALL 0x1.p-26 /* ~= 1.490e-8 */
  129. #undef SIND_SMALL /* not precise */
  130. #define COSD30 8.66025403784438646787e-01
  131. #define PIO180H 1.74532925229868851602e-02 /* high 32 bits */
  132. #define PIO180L -3.04358939097084072823e-12 /* Next 64 bits */
  133. #if defined(HAVE_FABSL) && defined(HAVE_FMODL) && defined(HAVE_COPYSIGNL)
  134. #ifdef HAVE_SINL
  135. #define ENABLE_SIND
  136. #endif
  137. #ifdef HAVE_COSL
  138. #define ENABLE_COSD
  139. #endif
  140. #ifdef HAVE_TANL
  141. #define ENABLE_TAND
  142. #endif
  143. #endif /* HAVE_FABSL && HAVE_FMODL && HAVE_COPYSIGNL */
  144. #ifdef GFC_REAL_10_INFINITY
  145. #define HAVE_INFINITY_KIND
  146. #endif
  147. #include "trigd_lib.inc"
  148. #undef KIND
  149. #undef TINY
  150. #undef COSD_SMALL
  151. #undef SIND_SMALL
  152. #undef COSD30
  153. #undef PIO180H
  154. #undef PIO180L
  155. #undef ENABLE_SIND
  156. #undef ENABLE_COSD
  157. #undef ENABLE_TAND
  158. #undef HAVE_INFINITY_KIND
  159. #endif /* HAVE_GFC_REAL_10 */
  160. #ifdef HAVE_GFC_REAL_16
  161. /* Build _gfortran_sind_r16, _gfortran_cosd_r16, and _gfortran_tand_r16 */
  162. #define KIND 16
  163. #define TINY 0x1.p-16400 /* ~= 1.28e-4937 */
  164. #undef SIND_SMALL /* not precise */
  165. #if GFC_REAL_16_DIGITS == 64
  166. /* 80 bit precision, use constants from REAL(10). */
  167. #define COSD_SMALL 0x1.p-26 /* ~= 1.490e-8 */
  168. #define COSD30 8.66025403784438646787e-01
  169. #define PIO180H 1.74532925229868851602e-02 /* high 32 bits */
  170. #define PIO180L -3.04358939097084072823e-12 /* Next 64 bits */
  171. #else
  172. /* Proper float128 precision. */
  173. #define COSD_SMALL 0x1.p-51 /* ~= 4.441e-16 */
  174. #define COSD30 8.66025403784438646763723170752936183e-01
  175. #define PIO180H 1.74532925199433197605003442731685936e-02
  176. #define PIO180L -2.39912634365882824665106671063098954e-17
  177. #endif
  178. #ifdef GFC_REAL_16_IS_LONG_DOUBLE
  179. #if defined(HAVE_FABSL) && defined(HAVE_FMODL) && defined(HAVE_COPYSIGNL)
  180. #ifdef HAVE_SINL
  181. #define ENABLE_SIND
  182. #endif
  183. #ifdef HAVE_COSL
  184. #define ENABLE_COSD
  185. #endif
  186. #ifdef HAVE_TANL
  187. #define ENABLE_TAND
  188. #endif
  189. #endif /* HAVE_FABSL && HAVE_FMODL && HAVE_COPYSIGNL */
  190. #else
  191. /* libquadmath: HAVE_*Q are never defined. They must be available. */
  192. #define ENABLE_SIND
  193. #define ENABLE_COSD
  194. #define ENABLE_TAND
  195. #endif /* GFC_REAL_16_IS_LONG_DOUBLE */
  196. #ifdef GFC_REAL_16_INFINITY
  197. #define HAVE_INFINITY_KIND
  198. #endif
  199. #include "trigd_lib.inc"
  200. #undef KIND
  201. #undef TINY
  202. #undef COSD_SMALL
  203. #undef SIND_SMALL
  204. #undef COSD30
  205. #undef PIO180H
  206. #undef PIO180L
  207. #undef ENABLE_SIND
  208. #undef ENABLE_COSD
  209. #undef ENABLE_TAND
  210. #undef HAVE_INFINITY_KIND
  211. #endif /* HAVE_GFC_REAL_16 */
  212. #ifdef HAVE_GFC_REAL_17
  213. /* Build _gfortran_sind_r17, _gfortran_cosd_r17, and _gfortran_tand_r17 */
  214. #define KIND 17
  215. #define TINY 0x1.p-16400 /* ~= 1.28e-4937 */
  216. #undef SIND_SMALL /* not precise */
  217. /* Proper float128 precision. */
  218. #define COSD_SMALL 0x1.p-51 /* ~= 4.441e-16 */
  219. #define COSD30 8.66025403784438646763723170752936183e-01
  220. #define PIO180H 1.74532925199433197605003442731685936e-02
  221. #define PIO180L -2.39912634365882824665106671063098954e-17
  222. /* libquadmath or glibc 2.32+: HAVE_*Q are never defined. They must be available. */
  223. #define ENABLE_SIND
  224. #define ENABLE_COSD
  225. #define ENABLE_TAND
  226. #ifdef GFC_REAL_17_INFINITY
  227. #define HAVE_INFINITY_KIND
  228. #endif
  229. #ifdef POWER_IEEE128
  230. #define COPYSIGN __copysignieee128
  231. #define FMOD __fmodieee128
  232. #define FABS __fabsieee128
  233. #define FMA __fmaieee128
  234. #define SIN __sinieee128
  235. #define COS __cosieee128
  236. #define TAN __tanieee128
  237. #endif
  238. #include "trigd_lib.inc"
  239. #undef KIND
  240. #undef TINY
  241. #undef COSD_SMALL
  242. #undef SIND_SMALL
  243. #undef COSD30
  244. #undef PIO180H
  245. #undef PIO180L
  246. #undef ENABLE_SIND
  247. #undef ENABLE_COSD
  248. #undef ENABLE_TAND
  249. #undef HAVE_INFINITY_KIND
  250. #endif /* HAVE_GFC_REAL_17 */