Skip to content

Commit d5a15f1

Browse files
committed
Use sleef_q constants to avoid cast warnings
1 parent 93f7c0a commit d5a15f1

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

quaddtype/numpy_quaddtype/src/ops.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#include <sleef.h>
22
#include <sleefquad.h>
33
#include <cmath>
4+
#include <limits>
5+
6+
// Quad Constants, generated with qutil
7+
#define QUAD_ZERO sleef_q(+0x0000000000000LL, 0x0000000000000000ULL, -16383)
8+
#define QUAD_ONE sleef_q(+0x1000000000000LL, 0x0000000000000000ULL, 0)
9+
#define QUAD_POS_INF sleef_q(+0x1000000000000LL, 0x0000000000000000ULL, 16384)
410

511
// Unary Quad Operations
612
typedef Sleef_quad (*unary_op_quad_def)(const Sleef_quad *);
@@ -20,8 +26,7 @@ quad_positive(const Sleef_quad *op)
2026
static inline Sleef_quad
2127
quad_sign(const Sleef_quad *op)
2228
{
23-
Sleef_quad zero = Sleef_cast_from_doubleq1(0.0);
24-
int32_t sign = Sleef_icmpq1(*op, zero);
29+
int32_t sign = Sleef_icmpq1(*op, QUAD_ZERO);
2530
// sign(x=NaN) = x; otherwise sign(x) in { -1.0; 0.0; +1.0 }
2631
return Sleef_iunordq1(*op, *op) ? *op : Sleef_cast_from_int64q1(sign);
2732
}
@@ -287,27 +292,23 @@ quad_signbit(const Sleef_quad *op)
287292
{
288293
// FIXME @juntyr or @SwayamInSync: replace with binary implementation
289294
// once we test big and little endian in CI
290-
Sleef_quad zero = Sleef_cast_from_doubleq1(0.0);
291-
Sleef_quad one = Sleef_cast_from_doubleq1(1.0);
292-
Sleef_quad one_signed = Sleef_copysignq1(one, *op);
295+
Sleef_quad one_signed = Sleef_copysignq1(QUAD_ONE, *op);
293296
// signbit(x) = 1 iff copysign(1, x) == -1
294-
return Sleef_icmpltq1(one_signed, zero);
297+
return Sleef_icmpltq1(one_signed, QUAD_ZERO);
295298
}
296299

297300
static inline npy_bool
298301
quad_isfinite(const Sleef_quad *op)
299302
{
300303
// isfinite(x) = abs(x) < inf
301-
Sleef_quad inf = Sleef_cast_from_doubleq1(std::numeric_limits<double>::infinity());
302-
return Sleef_icmpltq1(Sleef_fabsq1(*op), inf);
304+
return Sleef_icmpltq1(Sleef_fabsq1(*op), QUAD_POS_INF);
303305
}
304306

305307
static inline npy_bool
306308
quad_isinf(const Sleef_quad *op)
307309
{
308310
// isinf(x) = abs(x) == inf
309-
Sleef_quad inf = Sleef_cast_from_doubleq1(std::numeric_limits<double>::infinity());
310-
return Sleef_icmpeqq1(Sleef_fabsq1(*op), inf);
311+
return Sleef_icmpeqq1(Sleef_fabsq1(*op), QUAD_POS_INF);
311312
}
312313

313314
static inline npy_bool

quaddtype/tests/test_quaddtype.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ def test_array_aminmax(op, a, b):
9797
np.testing.assert_array_equal(np.array(quad_res).astype(float), float_res)
9898

9999

100-
@pytest.mark.parametrize("op,nop", [("neg", "negative"), ("pos", "positive"), ("abs", "absolute"), (None, "sign"), (None, "signbit"), (None, "isfinite"), (None, "isinf"), (None, "isnan")])
100+
@pytest.mark.parametrize("op", ["negative", "positive", "absolute", "sign", "signbit", "isfinite", "isinf", "isnan"])
101101
@pytest.mark.parametrize("val", ["3.0", "-3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
102-
def test_unary_ops(op, nop, val):
103-
op_func = None if op is None else getattr(operator, op)
104-
nop_func = getattr(np, nop)
102+
def test_unary_ops(op, val):
103+
op_func = dict(negative=operator.neg, positive=operator.pos, absolute=operator.abs).get(op, None)
104+
nop_func = getattr(np, op)
105105

106106
quad_val = QuadPrecision(val)
107107
float_val = float(val)

0 commit comments

Comments
 (0)