Skip to content

Commit 8fe110f

Browse files
committed
Move constant computation to C++
1 parent ce2b9e0 commit 8fe110f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

quaddtype/numpy_quaddtype/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
'set_num_threads', 'get_num_threads', 'get_quadblas_version'
1919
]
2020

21-
import numpy as np
22-
2321
def SleefQuadPrecision(value):
2422
return QuadPrecision(value, backend='sleef')
2523

@@ -42,6 +40,6 @@ def LongDoubleQuadPrecDType():
4240
epsilon = get_sleef_constant("epsilon")
4341
smallest_normal = get_sleef_constant("smallest_normal")
4442
smallest_subnormal = get_sleef_constant("smallest_subnormal")
45-
bits = SleefQuadPrecDType().itemsize * 8
46-
precision = int(-np.log10(epsilon))
47-
resolution = SleefQuadPrecision(10) ** (-precision)
43+
bits = get_sleef_constant("bits")
44+
precision = get_sleef_constant("precision")
45+
resolution = get_sleef_constant("resolution")

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ get_sleef_constant(PyObject *self, PyObject *args)
7373
else if (strcmp(constant_name, "smallest_subnormal") == 0) {
7474
result->value.sleef_value = SLEEF_QUAD_DENORM_MIN;
7575
}
76+
else if (strcmp(constant_name, "bits") == 0) {
77+
Py_DECREF(result);
78+
return PyLong_FromLong(sizeof(Sleef_quad) * CHAR_BIT);
79+
}
80+
else if (strcmp(constant_name, "precision") == 0) {
81+
Py_DECREF(result);
82+
// precision = int(-log10(epsilon))
83+
auto precision = Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON)));
84+
return PyLong_FromLong(precision);
85+
}
86+
else if (strcmp(constant_name, "resolution") == 0) {
87+
// precision = int(-log10(epsilon))
88+
auto precision = Sleef_cast_to_int64q1(Sleef_negq1(Sleef_log10q1_u10(SLEEF_QUAD_EPSILON)));
89+
// resolution = 10 ** (-precision)
90+
result->value.sleef_value = Sleef_powq1_u10(Sleef_cast_from_int64q1(10), Sleef_cast_from_int64q1(-precision));
91+
}
7692
else {
7793
PyErr_SetString(PyExc_ValueError, "Unknown constant name");
7894
Py_DECREF(result);

0 commit comments

Comments
 (0)