Skip to content

Commit bf96558

Browse files
committed
Issue python#28256: Cleanup _math.c
Only define fallback implementations when needed. It avoids producing deadcode when the system provides required math functions.
1 parent 22e36af commit bf96558

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

Modules/_math.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919
* ====================================================
2020
*/
2121

22+
#if !defined(HAVE_ACOSH) || !defined(HAVE_ASINH)
2223
static const double ln2 = 6.93147180559945286227E-01;
23-
static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
2424
static const double two_pow_p28 = 268435456.0; /* 2**28 */
25-
#ifndef Py_NAN
25+
#endif
26+
#if !defined(HAVE_ASINH) || !defined(HAVE_ATANH)
27+
static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
28+
#endif
29+
#if !defined(HAVE_ATANH) && !defined(Py_NAN)
2630
static const double zero = 0.0;
2731
#endif
2832

33+
34+
#ifndef HAVE_ACOSH
2935
/* acosh(x)
3036
* Method :
3137
* Based on
@@ -59,23 +65,25 @@ _Py_acosh(double x)
5965
return x+x;
6066
}
6167
else {
62-
return log(x)+ln2; /* acosh(huge)=log(2x) */
68+
return log(x) + ln2; /* acosh(huge)=log(2x) */
6369
}
6470
}
6571
else if (x == 1.) {
6672
return 0.0; /* acosh(1) = 0 */
6773
}
6874
else if (x > 2.) { /* 2 < x < 2**28 */
69-
double t = x*x;
70-
return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
75+
double t = x * x;
76+
return log(2.0 * x - 1.0 / (x + sqrt(t - 1.0)));
7177
}
7278
else { /* 1 < x <= 2 */
7379
double t = x - 1.0;
74-
return m_log1p(t + sqrt(2.0*t + t*t));
80+
return m_log1p(t + sqrt(2.0 * t + t * t));
7581
}
7682
}
83+
#endif /* HAVE_ACOSH */
7784

7885

86+
#ifndef HAVE_ASINH
7987
/* asinh(x)
8088
* Method :
8189
* Based on
@@ -100,10 +108,10 @@ _Py_asinh(double x)
100108
return x; /* return x inexact except 0 */
101109
}
102110
if (absx > two_pow_p28) { /* |x| > 2**28 */
103-
w = log(absx)+ln2;
111+
w = log(absx) + ln2;
104112
}
105113
else if (absx > 2.0) { /* 2 < |x| < 2**28 */
106-
w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
114+
w = log(2.0 * absx + 1.0 / (sqrt(x * x + 1.0) + absx));
107115
}
108116
else { /* 2**-28 <= |x| < 2= */
109117
double t = x*x;
@@ -112,7 +120,10 @@ _Py_asinh(double x)
112120
return copysign(w, x);
113121

114122
}
123+
#endif /* HAVE_ASINH */
124+
115125

126+
#ifndef HAVE_ATANH
116127
/* atanh(x)
117128
* Method :
118129
* 1.Reduced x to positive by atanh(-x) = -atanh(x)
@@ -145,7 +156,7 @@ _Py_atanh(double x)
145156
#ifdef Py_NAN
146157
return Py_NAN;
147158
#else
148-
return x/zero;
159+
return x / zero;
149160
#endif
150161
}
151162
if (absx < two_pow_m28) { /* |x| < 2**-28 */
@@ -160,7 +171,10 @@ _Py_atanh(double x)
160171
}
161172
return copysign(t, x);
162173
}
174+
#endif /* HAVE_ATANH */
175+
163176

177+
#ifndef HAVE_EXPM1
164178
/* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed
165179
to avoid the significant loss of precision that arises from direct
166180
evaluation of the expression exp(x) - 1, for x near 0. */
@@ -186,16 +200,17 @@ _Py_expm1(double x)
186200
else
187201
return exp(x) - 1.0;
188202
}
203+
#endif /* HAVE_EXPM1 */
204+
189205

190206
/* log1p(x) = log(1+x). The log1p function is designed to avoid the
191207
significant loss of precision that arises from direct evaluation when x is
192208
small. */
193209

194-
#ifdef HAVE_LOG1P
195-
196210
double
197211
_Py_log1p(double x)
198212
{
213+
#ifdef HAVE_LOG1P
199214
/* Some platforms supply a log1p function but don't respect the sign of
200215
zero: log1p(-0.0) gives 0.0 instead of the correct result of -0.0.
201216
@@ -208,13 +223,7 @@ _Py_log1p(double x)
208223
else {
209224
return log1p(x);
210225
}
211-
}
212-
213226
#else
214-
215-
double
216-
_Py_log1p(double x)
217-
{
218227
/* For x small, we use the following approach. Let y be the nearest float
219228
to 1+x, then
220229
@@ -236,7 +245,7 @@ _Py_log1p(double x)
236245
*/
237246

238247
double y;
239-
if (fabs(x) < DBL_EPSILON/2.) {
248+
if (fabs(x) < DBL_EPSILON / 2.) {
240249
return x;
241250
}
242251
else if (-0.5 <= x && x <= 1.) {
@@ -246,12 +255,12 @@ _Py_log1p(double x)
246255
happens, then results from log1p will be inaccurate
247256
for small x. */
248257
y = 1.+x;
249-
return log(y)-((y-1.)-x)/y;
258+
return log(y) - ((y - 1.) - x) / y;
250259
}
251260
else {
252261
/* NaNs and infinities should end up here */
253262
return log(1.+x);
254263
}
264+
#endif /* ifdef HAVE_LOG1P */
255265
}
256266

257-
#endif /* ifdef HAVE_LOG1P */

Modules/_math.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
double _Py_acosh(double x);
2-
double _Py_asinh(double x);
3-
double _Py_atanh(double x);
4-
double _Py_expm1(double x);
5-
double _Py_log1p(double x);
6-
71
#ifdef HAVE_ACOSH
8-
#define m_acosh acosh
2+
# define m_acosh acosh
93
#else
104
/* if the system doesn't have acosh, use the substitute
115
function defined in Modules/_math.c. */
12-
#define m_acosh _Py_acosh
6+
double _Py_acosh(double x);
7+
# define m_acosh _Py_acosh
138
#endif
149

1510
#ifdef HAVE_ASINH
16-
#define m_asinh asinh
11+
# define m_asinh asinh
1712
#else
1813
/* if the system doesn't have asinh, use the substitute
1914
function defined in Modules/_math.c. */
20-
#define m_asinh _Py_asinh
15+
double _Py_asinh(double x);
16+
# define m_asinh _Py_asinh
2117
#endif
2218

2319
#ifdef HAVE_ATANH
24-
#define m_atanh atanh
20+
# define m_atanh atanh
2521
#else
2622
/* if the system doesn't have atanh, use the substitute
2723
function defined in Modules/_math.c. */
24+
double _Py_atanh(double x);
2825
#define m_atanh _Py_atanh
2926
#endif
3027

3128
#ifdef HAVE_EXPM1
32-
#define m_expm1 expm1
29+
# define m_expm1 expm1
3330
#else
3431
/* if the system doesn't have expm1, use the substitute
3532
function defined in Modules/_math.c. */
33+
double _Py_expm1(double x);
3634
#define m_expm1 _Py_expm1
3735
#endif
3836

37+
double _Py_log1p(double x);
38+
3939
/* Use the substitute from _math.c on all platforms:
4040
it includes workarounds for buggy handling of zeros. */
4141
#define m_log1p _Py_log1p

0 commit comments

Comments
 (0)