Skip to content

Commit c304f6f

Browse files
committed
move hypot to optional for windows, fix pow and log2
Signed-off-by: mattip <[email protected]>
1 parent 16a6405 commit c304f6f

File tree

2 files changed

+108
-39
lines changed

2 files changed

+108
-39
lines changed

numpy/core/setup_common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ def set_sig(sig):
124124
"floor", "ceil", "sqrt", "log10", "log", "exp", "asin",
125125
"acos", "atan", "fmod", 'modf', 'frexp', 'ldexp',
126126
"expm1", "log1p", "acosh", "asinh", "atanh",
127-
"rint", "trunc", "exp2", "hypot", "atan2",
127+
"rint", "trunc", "exp2", "atan2",
128128
"copysign", "nextafter", "strtoll", "strtoull", "cbrt"]
129129

130130
OPTIONAL_STDFUNCS = [
131131
# cygwin
132132
"log2",
133133
# macos for powl
134134
"pow",
135+
# 32-bit windows
136+
"hypot"
135137
]
136138

137139
OPTIONAL_LOCALE_FUNCS = ["strtold_l"]

numpy/core/src/npymath/npy_math_internal.h.src

Lines changed: 105 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ static const npy_uint64 MAGIC64[] = {0x5555555555555555ull, 0x3333333333333333ul
9494
* classification instead of calling npy_isinf/npy_isnan: we should have some
9595
* macros for this, though, instead of doing it manually
9696
*/
97-
#ifndef HAVE_LOG2
9897
NPY_INPLACE double npy_log2(double x)
9998
{
100-
#ifdef HAVE_FREXP
99+
#ifdef HAVE_LOG2
100+
return log2(x);
101+
#elif defined(HAVE_FREXP)
101102
if (!npy_isfinite(x) || x <= 0.) {
102103
/* special value result */
103104
return npy_log(x);
@@ -127,7 +128,38 @@ NPY_INPLACE double npy_log2(double x)
127128
return NPY_LOG2E * npy_log(x);
128129
#endif
129130
}
131+
132+
NPY_INPLACE double npy_hypot(double x, double y)
133+
{
134+
#ifdef HAVE_HYPOT
135+
return hypot(x, y);
136+
#else
137+
double yx;
138+
139+
if (npy_isinf(x) || npy_isinf(y)) {
140+
return NPY_INFINITY;
141+
}
142+
143+
if (npy_isnan(x) || npy_isnan(y)) {
144+
return NPY_NAN;
145+
}
146+
147+
x = npy_fabs(x);
148+
y = npy_fabs(y);
149+
if (x < y) {
150+
double temp = x;
151+
x = y;
152+
y = temp;
153+
}
154+
if (x == 0.) {
155+
return 0.;
156+
}
157+
else {
158+
yx = y/x;
159+
return x*npy_sqrt(1.+yx*yx);
160+
}
130161
#endif
162+
}
131163

132164
/*
133165
*
@@ -194,12 +226,73 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
194226

195227
/**end repeat1**/
196228

229+
#undef WORKAROUND_APPLE_TRIG_BUG
230+
231+
/* C99 mandatory */
232+
233+
/**begin repeat1
234+
* #kind = atan2,fmod,copysign#
235+
* #KIND = ATAN2,FMOD,COPYSIGN#
236+
*/
237+
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
238+
{
239+
return NPY__FP_SFX(@kind@)(x, y);
240+
}
241+
/**end repeat1**/
242+
243+
244+
NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr)
245+
{
246+
return NPY__FP_SFX(modf)(x, iptr);
247+
}
248+
249+
NPY_INPLACE @type@ npy_ldexp@c@(@type@ x, int exp)
250+
{
251+
return NPY__FP_SFX(ldexp)(x, exp);
252+
}
253+
254+
NPY_INPLACE @type@ npy_frexp@c@(@type@ x, int* exp)
255+
{
256+
return NPY__FP_SFX(frexp)(x, exp);
257+
}
258+
259+
NPY_INPLACE @type@ npy_cbrt@c@(@type@ x)
260+
{
261+
return NPY__FP_SFX(cbrt)(x);
262+
}
263+
264+
/**end repeat**/
265+
197266
/* Optional C99 functions */
198267

268+
/**begin repeat
269+
* #type = npy_longdouble, npy_float#
270+
* #TYPE = LONGDOUBLE, FLOAT#
271+
* #c = l,f#
272+
* #C = L,F#
273+
*/
274+
#undef NPY__FP_SFX
275+
#if NPY_SIZEOF_@TYPE@ == NPY_SIZEOF_DOUBLE
276+
#define NPY__FP_SFX(X) X
277+
#else
278+
#define NPY__FP_SFX(X) NPY_CAT(X, @c@)
279+
#endif
280+
199281
/**begin repeat1
200282
* #kind = log2#
201283
* #KIND = LOG2#
202284
*/
285+
286+
#ifdef @kind@@c@
287+
#undef @kind@@c@
288+
#endif
289+
#ifndef HAVE_@KIND@@C@
290+
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
291+
{
292+
return (@type@) npy_@kind@((double)x);
293+
}
294+
#endif
295+
203296
#ifdef HAVE_@KIND@@C@
204297
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
205298
{
@@ -209,11 +302,10 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
209302

210303
/**end repeat1**/
211304

212-
#undef WORKAROUND_APPLE_TRIG_BUG
213305

214306
/**begin repeat1
215-
* #kind = pow#
216-
* #KIND = POW#
307+
* #kind = hypot,pow#
308+
* #KIND = HYPOT,POW#
217309
*/
218310
#ifdef @kind@@c@
219311
#undef @kind@@c@
@@ -232,42 +324,10 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
232324
}
233325
#endif
234326
/**end repeat1**/
327+
/**end repeat**/
235328

236329

237-
/* C99 mandatory */
238-
239-
/**begin repeat1
240-
* #kind = atan2,hypot,fmod,copysign#
241-
* #KIND = ATAN2,HYPOT,FMOD,COPYSIGN#
242-
*/
243-
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
244-
{
245-
return NPY__FP_SFX(@kind@)(x, y);
246-
}
247-
/**end repeat1**/
248-
249-
250-
NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr)
251-
{
252-
return NPY__FP_SFX(modf)(x, iptr);
253-
}
254-
255-
NPY_INPLACE @type@ npy_ldexp@c@(@type@ x, int exp)
256-
{
257-
return NPY__FP_SFX(ldexp)(x, exp);
258-
}
259-
260-
NPY_INPLACE @type@ npy_frexp@c@(@type@ x, int* exp)
261-
{
262-
return NPY__FP_SFX(frexp)(x, exp);
263-
}
264-
265-
NPY_INPLACE @type@ npy_cbrt@c@(@type@ x)
266-
{
267-
return NPY__FP_SFX(cbrt)(x);
268-
}
269330
#undef NPY__FP_SFX
270-
/**end repeat**/
271331

272332

273333
/*
@@ -603,3 +663,10 @@ npy_popcount@c@(npy_@type@ a)
603663
return npy_popcountu@c@(a < 0 ? -a : a);
604664
}
605665
/**end repeat**/
666+
667+
/* XXX: will moving this to a define break anything? */
668+
669+
NPY_INPLACE double npy_pow(double x, double y)
670+
{
671+
return pow(x, y);
672+
}

0 commit comments

Comments
 (0)