Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fdaa54

Browse files
committed
MAINT: umath/npymath: Add ldexp and frexp to npymath
npy_frexp and npy_ldexp are used unconditionally in umath. (i.e. HAVE_LDEXPF, etc. no longer appears in umath.)
1 parent e6b99c9 commit 0fdaa54

4 files changed

Lines changed: 54 additions & 22 deletions

File tree

numpy/core/include/numpy/npy_math.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ double npy_copysign(double x, double y);
152152
double npy_nextafter(double x, double y);
153153
double npy_spacing(double x);
154154

155+
double npy_ldexp(double x, int exp);
156+
double npy_frexp(double x, int* exp);
157+
155158
/*
156159
* IEEE 754 fpu handling. Those are guaranteed to be macros
157160
*/
@@ -256,6 +259,9 @@ float npy_copysignf(float x, float y);
256259
float npy_nextafterf(float x, float y);
257260
float npy_spacingf(float x);
258261

262+
float npy_ldexpf(float x, int exp);
263+
float npy_frexpf(float x, int* exp);
264+
259265
/*
260266
* long double C99 math functions
261267
*/
@@ -297,6 +303,9 @@ npy_longdouble npy_copysignl(npy_longdouble x, npy_longdouble y);
297303
npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y);
298304
npy_longdouble npy_spacingl(npy_longdouble x);
299305

306+
npy_longdouble npy_ldexpl(npy_longdouble x, int exp);
307+
npy_longdouble npy_frexpl(npy_longdouble x, int* exp);
308+
300309
/*
301310
* Non standard functions
302311
*/

numpy/core/src/npymath/npy_math.c.src

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ double npy_log2(double x)
305305
* asinh, acosh, atanh
306306
*
307307
* hypot, atan2, pow, fmod, modf
308+
* ldexp, frexp
308309
*
309310
* We assume the above are always available in their double versions.
310311
*
@@ -367,6 +368,26 @@ double npy_log2(double x)
367368
}
368369
#endif
369370

371+
#ifdef ldexp@c@
372+
#undef ldexp@c@
373+
#endif
374+
#ifndef HAVE_LDEXP@C@
375+
@type@ npy_ldexp@c@(@type@ x, npy_int exp)
376+
{
377+
return (@type@) npy_ldexp((double)x, exp);
378+
}
379+
#endif
380+
381+
#ifdef frexp@c@
382+
#undef frexp@c@
383+
#endif
384+
#ifndef HAVE_FREXP@C@
385+
@type@ npy_frexp@c@(@type@ x, npy_int* exp)
386+
{
387+
return (@type@) npy_frexp(x, exp);
388+
}
389+
#endif
390+
370391
/**end repeat**/
371392

372393

@@ -413,6 +434,20 @@ double npy_log2(double x)
413434
}
414435
#endif
415436

437+
#ifdef HAVE_LDEXP@C@
438+
@type@ npy_ldexp@c@(@type@ x, npy_int exp)
439+
{
440+
return ldexp@c@(x, exp);
441+
}
442+
#endif
443+
444+
#ifdef HAVE_FREXP@C@
445+
@type@ npy_frexp@c@(@type@ x, npy_int* exp)
446+
{
447+
return frexp@c@(x, exp);
448+
}
449+
#endif
450+
416451
/**end repeat**/
417452

418453

numpy/core/src/umath/loops.c.src

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,25 +1717,22 @@ NPY_NO_EXPORT void
17171717
}
17181718
}
17191719

1720-
#ifdef HAVE_FREXP@C@
17211720
NPY_NO_EXPORT void
17221721
@TYPE@_frexp(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
17231722
{
17241723
UNARY_LOOP_TWO_OUT {
17251724
const @type@ in1 = *(@type@ *)ip1;
1726-
*((@type@ *)op1) = frexp@c@(in1, (int *)op2);
1725+
*((@type@ *)op1) = npy_frexp@c@(in1, (int *)op2);
17271726
}
17281727
}
1729-
#endif
17301728

1731-
#ifdef HAVE_LDEXP@C@
17321729
NPY_NO_EXPORT void
17331730
@TYPE@_ldexp(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
17341731
{
17351732
BINARY_LOOP {
17361733
const @type@ in1 = *(@type@ *)ip1;
17371734
const int in2 = *(int *)ip2;
1738-
*((@type@ *)op1) = ldexp@c@(in1, in2);
1735+
*((@type@ *)op1) = npy_ldexp@c@(in1, in2);
17391736
}
17401737
}
17411738

@@ -1752,23 +1749,22 @@ NPY_NO_EXPORT void
17521749
const long in2 = *(long *)ip2;
17531750
if (((int)in2) == in2) {
17541751
/* Range OK */
1755-
*((@type@ *)op1) = ldexp@c@(in1, ((int)in2));
1752+
*((@type@ *)op1) = npy_ldexp@c@(in1, ((int)in2));
17561753
}
17571754
else {
17581755
/*
17591756
* Outside npy_int range -- also ldexp will overflow in this case,
17601757
* given that exponent has less bits than npy_int.
17611758
*/
17621759
if (in2 > 0) {
1763-
*((@type@ *)op1) = ldexp@c@(in1, NPY_MAX_INT);
1760+
*((@type@ *)op1) = npy_ldexp@c@(in1, NPY_MAX_INT);
17641761
}
17651762
else {
1766-
*((@type@ *)op1) = ldexp@c@(in1, NPY_MIN_INT);
1763+
*((@type@ *)op1) = npy_ldexp@c@(in1, NPY_MIN_INT);
17671764
}
17681765
}
17691766
}
17701767
}
1771-
#endif
17721768

17731769
#define @TYPE@_true_divide @TYPE@_divide
17741770

@@ -2033,25 +2029,22 @@ HALF_modf(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(f
20332029
}
20342030
}
20352031

2036-
#ifdef HAVE_FREXPF
20372032
NPY_NO_EXPORT void
20382033
HALF_frexp(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
20392034
{
20402035
UNARY_LOOP_TWO_OUT {
20412036
const float in1 = npy_half_to_float(*(npy_half *)ip1);
2042-
*((npy_half *)op1) = npy_float_to_half(frexpf(in1, (int *)op2));
2037+
*((npy_half *)op1) = npy_float_to_half(npy_frexpf(in1, (int *)op2));
20432038
}
20442039
}
2045-
#endif
20462040

2047-
#ifdef HAVE_LDEXPF
20482041
NPY_NO_EXPORT void
20492042
HALF_ldexp(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
20502043
{
20512044
BINARY_LOOP {
20522045
const float in1 = npy_half_to_float(*(npy_half *)ip1);
20532046
const int in2 = *(int *)ip2;
2054-
*((npy_half *)op1) = npy_float_to_half(ldexpf(in1, in2));
2047+
*((npy_half *)op1) = npy_float_to_half(npy_ldexpf(in1, in2));
20552048
}
20562049
}
20572050

@@ -2068,23 +2061,22 @@ HALF_ldexp_long(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UN
20682061
const long in2 = *(long *)ip2;
20692062
if (((int)in2) == in2) {
20702063
/* Range OK */
2071-
*((npy_half *)op1) = npy_float_to_half(ldexpf(in1, ((int)in2)));
2064+
*((npy_half *)op1) = npy_float_to_half(npy_ldexpf(in1, ((int)in2)));
20722065
}
20732066
else {
20742067
/*
20752068
* Outside npy_int range -- also ldexp will overflow in this case,
20762069
* given that exponent has less bits than npy_int.
20772070
*/
20782071
if (in2 > 0) {
2079-
*((npy_half *)op1) = npy_float_to_half(ldexpf(in1, NPY_MAX_INT));
2072+
*((npy_half *)op1) = npy_float_to_half(npy_ldexpf(in1, NPY_MAX_INT));
20802073
}
20812074
else {
2082-
*((npy_half *)op1) = npy_float_to_half(ldexpf(in1, NPY_MIN_INT));
2075+
*((npy_half *)op1) = npy_float_to_half(npy_ldexpf(in1, NPY_MIN_INT));
20832076
}
20842077
}
20852078
}
20862079
}
2087-
#endif
20882080

20892081
#define HALF_true_divide HALF_divide
20902082

numpy/core/src/umath/loops.h.src

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,13 @@ NPY_NO_EXPORT void
277277
NPY_NO_EXPORT void
278278
@TYPE@_modf(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func));
279279

280-
#ifdef HAVE_FREXP@C@
281280
NPY_NO_EXPORT void
282281
@TYPE@_frexp(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func));
283-
#endif
284282

285-
#ifdef HAVE_LDEXP@C@
286283
NPY_NO_EXPORT void
287284
@TYPE@_ldexp(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func));
288285
NPY_NO_EXPORT void
289286
@TYPE@_ldexp_long(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func));
290-
#endif
291287

292288
#define @TYPE@_true_divide @TYPE@_divide
293289

0 commit comments

Comments
 (0)