1 /* Implementation of the degree trignometric functions COSD, SIND, TAND.
2 Copyright (C) 2020 Free Software Foundation, Inc.
3 Contributed by Steven G. Kargl <kargl@gcc.gnu.org>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
32 For real x, let {x}_P or x_P be the closest representible number in the
33 floating point representation which uses P binary bits of fractional
34 precision (with IEEE rounding semantics).
36 Similarly, let f_P(x) be shorthand for {f(x)}_P.
38 Let ulp_P(x) be the unit of least precision for x: in other words the
39 maximal value of |a_P - b_P| where a_P <= x <= b_P and a_P != b_P.
41 Let x ~= y <-> | x - y | < ulp_P(x - y).
43 Let deg(x) be the value of x radians in degrees.
45 Values for each precision P were selected as follows.
48 COSD_SMALL = 2**{-N} such that for all x <= COSD_SMALL:
50 * cos(deg(x)) ~= 1, or equivalently:
52 | 1 - cos(deg(x)) | < ulp_P(1).
54 Unfortunately for SIND (and therefore TAND) a similar relation is only
55 possible for REAL(4) and REAL(8). With REAL(10) and REAL(16), enough
56 precision is available such that sin_P(x) != x_P for some x less than any
57 value. (There are values where this equality holds, but the distance has
60 For REAL(4) and REAL(8), we can select SIND_SMALL such that:
62 * sin(deg(x)) ~= deg(x), or equivalently:
64 | deg(x) - sin(deg(x)) | < ulp_P(deg(x)).
68 /* Build _gfortran_sind_r4, _gfortran_cosd_r4, and _gfortran_tand_r4 */
70 #define FTYPE GFC_REAL_4
74 #define SUFFIX(x) x ## f
76 #define TINY 0x1.p-100f /* ~= 7.889e-31 */
77 #define COSD_SMALL 0x1.p-7f /* = 7.8125e-3 */
78 #define SIND_SMALL 0x1.p-5f /* = 3.125e-2 */
79 #define COSD30 8.66025388e-01f
81 #define PIO180H 1.74560547e-02f /* high 12 bits. */
82 #define PIO180L -2.76216747e-06f /* Next 24 bits. */
84 #include "trigd_lib.inc"
99 /* Build _gfortran_sind_r8, _gfortran_cosd_r8, and _gfortran_tand_r8. */
101 #define FTYPE GFC_REAL_8
107 #define TINY 0x1.p-1000 /* ~= 9.33e-302 (min exp -1074) */
108 #define COSD_SMALL 0x1.p-21 /* ~= 4.768e-7 */
109 #define SIND_SMALL 0x1.p-19 /* ~= 9.537e-7 */
110 #define COSD30 8.6602540378443860e-01
112 #define PIO180H 1.7453283071517944e-02 /* high 21 bits. */
113 #define PIO180L 9.4484253514332993e-09 /* Next 53 bits. */
115 #include "trigd_lib.inc"
130 /* Build _gfortran_sind_r10, _gfortran_cosd_r10, and _gfortran_tand_r10. */
132 #ifdef HAVE_GFC_REAL_10
134 #define FTYPE GFC_REAL_10
135 #define SIND sind_r10
136 #define COSD cosd_r10
137 #define TAND tand_r10
138 #define SUFFIX(x) x ## l /* L */
140 #define TINY 0x1.p-16400L /* ~= 1.28e-4937 (min exp -16494) */
141 #define COSD_SMALL 0x1.p-26L /* ~= 1.490e-8 */
142 #undef SIND_SMALL /* not precise */
143 #define COSD30 8.66025403784438646787e-01L
145 #define PIO180H 1.74532925229868851602e-02L /* high 32 bits */
146 #define PIO180L -3.04358939097084072823e-12L /* Next 64 bits */
148 #include "trigd_lib.inc"
160 #endif /* HAVE_GFC_REAL_10 */
163 /* Build _gfortran_sind_r16, _gfortran_cosd_r16, and _gfortran_tand_r16. */
165 #ifdef HAVE_GFC_REAL_16
167 #define FTYPE GFC_REAL_16
168 #define SIND sind_r16
169 #define COSD cosd_r16
170 #define TAND tand_r16
172 #ifdef GFC_REAL_16_IS_FLOAT128 /* libquadmath. */
173 #define SUFFIX(x) x ## q
175 #define SUFFIX(x) x ## l
176 #endif /* GFC_REAL_16_IS_FLOAT128 */
178 #define TINY SUFFIX(0x1.p-16400) /* ~= 1.28e-4937 */
179 #define COSD_SMALL SUFFIX(0x1.p-51) /* ~= 4.441e-16 */
180 #undef SIND_SMALL /* not precise */
181 #define COSD30 SUFFIX(8.66025403784438646763723170752936183e-01)
182 #define PIO180H SUFFIX(1.74532925199433197605003442731685936e-02)
183 #define PIO180L SUFFIX(-2.39912634365882824665106671063098954e-17)
185 #include "trigd_lib.inc"
205 #endif /* HAVE_GFC_REAL_16 */