8000 MAINT: Simplify npymath by mattip · Pull Request #22090 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Simplify npymath #22090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
nextafter is mandatory
  • Loading branch information
mattip committed Aug 21, 2022
commit 704ca94f75246439c6b6575d654c96f5405b49d9
201 changes: 0 additions & 201 deletions numpy/core/src/npymath/ieee754.c.src
8000
Original file line number Diff line number Diff line change
Expand Up @@ -310,201 +310,6 @@ static npy_longdouble _nextl(npy_longdouble x, int p)
}
#endif

/*
* nextafter code taken from BSD math lib, the code contains the following
* notice:
*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/

#ifndef HAVE_NEXTAFTER
double npy_nextafter(double x, double y)
{
volatile double t;
npy_int32 hx, hy, ix, iy;
npy_uint32 lx, ly;

EXTRACT_WORDS(hx, lx, x);
EXTRACT_WORDS(hy, ly, y);
ix = hx & 0x7fffffff; /* |x| */
iy = hy & 0x7fffffff; /* |y| */

if (((ix >= 0x7ff00000) && ((ix - 0x7ff00000) | lx) != 0) || /* x is nan */
((iy >= 0x7ff00000) && ((iy - 0x7ff00000) | ly) != 0)) /* y is nan */
return x + y;
if (x == y)
return y; /* x=y, return y */
if ((ix | lx) == 0) { /* x == 0 */
INSERT_WORDS(x, hy & 0x80000000, 1); /* return +-minsubnormal */
t = x * x;
if (t == x)
return t;
else
return x; /* raise underflow flag */
}
if (hx >= 0) { /* x > 0 */
if (hx > hy || ((hx == hy) && (lx > ly))) { /* x > y, x -= ulp */
if (lx == 0)
hx -= 1;
lx -= 1;
} else { /* x < y, x += ulp */
lx += 1;
if (lx == 0)
hx += 1;
}
} else { /* x < 0 */
if (hy >= 0 || hx > hy || ((hx == hy) && (lx > ly))) { /* x < y, x -= ulp */
if (lx == 0)
hx -= 1;
lx -= 1;
} else { /* x > y, x += ulp */
lx += 1;
if (lx == 0)
hx += 1;
}
}
hy = hx & 0x7ff00000;
if (hy >= 0x7ff00000)
return x + x; /* overflow */
if (hy < 0x00100000) { /* underflow */
t = x * x;
if (t != x) { /* raise underflow flag */
INSERT_WORDS(y, hx, lx);
return y;
}
}
INSERT_WORDS(x, hx, lx);
return x;
}
#endif

#ifndef HAVE_NEXTAFTERF
float npy_nextafterf(float x, float y)
{
volatile float t;
npy_int32 hx, hy, ix, iy;

GET_FLOAT_WORD(hx, x);
GET_FLOAT_WORD(hy, y);
ix = hx & 0x7fffffff; /* |x| */
iy = hy & 0x7fffffff; /* |y| */

if ((ix > 0x7f800000) || /* x is nan */
(iy > 0x7f800000)) /* y is nan */
return x + y;
if (x == y)
return y; /* x=y, return y */
if (ix == 0) { /* x == 0 */
SET_FLOAT_WORD(x, (hy & 0x80000000) | 1); /* return +-minsubnormal */
t = x * x;
if (t == x)
return t;
else
return x; /* raise underflow flag */
}
if (hx >= 0) { /* x > 0 */
if (hx > hy) { /* x > y, x -= ulp */
hx -= 1;
} else { /* x < y, x += ulp */
hx += 1;
}
} else { /* x < 0 */
if (hy >= 0 || hx > hy) { /* x < y, x -= ulp */
hx -= 1;
} else { /* x > y, x += ulp */
hx += 1;
}
}
hy = hx & 0x7f800000;
if (hy >= 0x7f800000)
return x + x; /* overflow */
if (hy < 0x00800000) { /* underflow */
t = x * x;
if (t != x) { /* raise underflow flag */
SET_FLOAT_WORD(y, hx);
return y;
}
}
SET_FLOAT_WORD(x, hx);
return x;
}
#endif

#ifndef HAVE_NEXTAFTERL
npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y)
{
volatile npy_longdouble t;
union IEEEl2bitsrep ux;
union IEEEl2bitsrep uy;

ux.e = x;
uy.e = y;

if ((GET_LDOUBLE_EXP(ux) == 0x7fff &&
((GET_LDOUBLE_MANH(ux) & ~LDBL_NBIT) | GET_LDOUBLE_MANL(ux)) != 0) ||
(GET_LDOUBLE_EXP(uy) == 0x7fff &&
((GET_LDOUBLE_MANH(uy) & ~LDBL_NBIT) | GET_LDOUBLE_MANL(uy)) != 0)) {
return ux.e + uy.e; /* x or y is nan */
}
if (ux.e == uy.e) {
return uy.e; /* x=y, return y */
}
if (ux.e == 0.0) {
SET_LDOUBLE_MANH(ux, 0); /* return +-minsubnormal */
SET_LDOUBLE_MANL(ux, 1);
SET_LDOUBLE_SIGN(ux, GET_LDOUBLE_SIGN(uy));
t = ux.e * ux.e;
if (t == ux.e) {
return t;
} else {
return ux.e; /* raise underflow flag */
}
}
if ((ux.e > 0.0) ^ (ux.e < uy.e)) { /* x -= ulp */
if (GET_LDOUBLE_MANL(ux) == 0) {
if ((GET_LDOUBLE_MANH(ux) & ~LDBL_NBIT) == 0) {
SET_LDOUBLE_EXP(ux, GET_LDOUBLE_EXP(ux) - 1);
}
SET_LDOUBLE_MANH(ux,
(GET_LDOUBLE_MANH(ux) - 1) |
(GET_LDOUBLE_MANH(ux) & LDBL_NBIT));
}
SET_LDOUBLE_MANL(ux, GET_LDOUBLE_MANL(ux) - 1);
} else { /* x += ulp */
SET_LDOUBLE_MANL(ux, GET_LDOUBLE_MANL(ux) + 1);
if (GET_LDOUBLE_MANL(ux) == 0) {
SET_LDOUBLE_MANH(ux,
(GET_LDOUBLE_MANH(ux) + 1) |
(GET_LDOUBLE_MANH(ux) & LDBL_NBIT));
if ((GET_LDOUBLE_MANH(ux) & ~LDBL_NBIT) == 0) {
SET_LDOUBLE_EXP(ux, GET_LDOUBLE_EXP(ux) + 1);
}
}
}
if (GET_LDOUBLE_EXP(ux) == 0x7fff) {
return ux.e + ux.e; /* overflow */
}
if (GET_LDOUBLE_EXP(ux) == 0) { /* underflow */
if (LDBL_NBIT) {
SET_LDOUBLE_MANH(ux, GET_LDOUBLE_MANH(ux) & ~LDBL_NBIT);
}
t = ux.e * ux.e;
if (t != ux.e) { /* raise underflow flag */
return ux.e;
}
}

return ux.e;
}
#endif

/**begin repeat
* #suff = f,,l#
* #SUFF = F,,L#
Expand All @@ -525,26 +330,20 @@ npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y)
* Decorate all the math functions which are available on the current platform
*/

#ifdef HAVE_NEXTAFTERF
float npy_nextafterf(float x, float y)
{
return nextafterf(x, y);
}
#endif

#ifdef HAVE_NEXTAFTER
double npy_nextafter(double x, double y)
{
return nextafter(x, y);
}
#endif

#ifdef HAVE_NEXTAFTERL
npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y)
{
return nextafterl(x, y);
}
#endif

int npy_clear_floatstatus() {
char x=0;
Expand Down
Loading
0