Skip to content

Commit 41ca3f9

Browse files
committed
added 128-bit common constants
1 parent 012e90f commit 41ca3f9

File tree

4 files changed

+57
-98
lines changed

4 files changed

+57
-98
lines changed

quaddtype/numpy_quaddtype/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
QuadPrecision,
33
QuadPrecDType,
44
is_longdouble_128,
5-
pi, e, log2e, log10e, ln2, ln10,
6-
sqrt2, sqrt3, egamma, phi, quad_max, quad_min, quad_epsilon, quad_denorm_min
5+
get_sleef_constant
76
)
87

98
__all__ = [
109
'QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
11-
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128',
12-
'pi', 'e', 'log2e', 'log10e', 'ln2', 'ln10',
13-
'sqrt2', 'sqrt3', 'egamma', 'phi',
14-
'quad_max', 'quad_min', 'quad_epsilon', 'quad_denorm_min'
10+
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128', 'pi', 'e',
11+
'log2e', 'log10e', 'ln2', 'ln10', 'max_value', 'min_value', 'epsilon'
1512
]
1613

1714
def SleefQuadPrecision(value):
@@ -24,4 +21,14 @@ def SleefQuadPrecDType():
2421
return QuadPrecDType(backend='sleef')
2522

2623
def LongDoubleQuadPrecDType():
27-
return QuadPrecDType(backend='longdouble')
24+
return QuadPrecDType(backend='longdouble')
25+
26+
pi = get_sleef_constant("pi")
27+
e = get_sleef_constant("e")
28+
log2e = get_sleef_constant("log2e")
29+
log10e = get_sleef_constant("log10e")
30+
ln2 = get_sleef_constant("ln2")
31+
ln10 = get_sleef_constant("ln10")
32+
max_value = get_sleef_constant("quad_max")
33+
min_value = get_sleef_constant("quad_min")
34+
epsilon = get_sleef_constant("epsilon")

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <Python.h>
2+
#include <sleef.h>
3+
#include <sleefquad.h>
4+
#include <string.h>
25

36
#define PY_ARRAY_UNIQUE_SYMBOL QuadPrecType_ARRAY_API
47
#define PY_UFUNC_UNIQUE_SYMBOL QuadPrecType_UFUNC_API
@@ -26,8 +29,48 @@ static PyObject* py_is_longdouble_128(PyObject* self, PyObject* args) {
2629
}
2730
}
2831

32+
static PyObject* get_sleef_constant(PyObject* self, PyObject* args) {
33+
const char* constant_name;
34+
if (!PyArg_ParseTuple(args, "s", &constant_name)) {
35+
return NULL;
36+
}
37+
38+
QuadPrecisionObject* result = QuadPrecision_raw_new(BACKEND_SLEEF);
39+
if (result == NULL) {
40+
return NULL;
41+
}
42+
43+
if (strcmp(constant_name, "pi") == 0) {
44+
result->value.sleef_value = SLEEF_M_PIq;
45+
} else if (strcmp(constant_name, "e") == 0) {
46+
result->value.sleef_value = SLEEF_M_Eq;
47+
} else if (strcmp(constant_name, "log2e") == 0) {
48+
result->value.sleef_value = SLEEF_M_LOG2Eq;
49+
} else if (strcmp(constant_name, "log10e") == 0) {
50+
result->value.sleef_value = SLEEF_M_LOG10Eq;
51+
} else if (strcmp(constant_name, "ln2") == 0) {
52+
result->value.sleef_value = SLEEF_M_LN2q;
53+
} else if (strcmp(constant_name, "ln10") == 0) {
54+
result->value.sleef_value = SLEEF_M_LN10q;
55+
} else if (strcmp(constant_name, "quad_max") == 0) {
56+
result->value.sleef_value = SLEEF_QUAD_MAX;
57+
} else if (strcmp(constant_name, "quad_min") == 0) {
58+
result->value.sleef_value = SLEEF_QUAD_MIN;
59+
} else if (strcmp(constant_name, "epsilon") == 0) {
60+
result->value.sleef_value = SLEEF_QUAD_EPSILON;
61+
}
62+
else {
63+
PyErr_SetString(PyExc_ValueError, "Unknown constant name");
64+
Py_DECREF(result);
65+
return NULL;
66+
}
67+
68+
return (PyObject*)result;
69+
}
70+
2971
static PyMethodDef module_methods[] = {
3072
{"is_longdouble_128", py_is_longdouble_128, METH_NOARGS, "Check if long double is 128-bit"},
73+
{"get_sleef_constant", get_sleef_constant, METH_VARARGS, "Get Sleef constant by name"},
3174
{NULL, NULL, 0, NULL}
3275
};
3376

@@ -65,21 +108,6 @@ PyInit__quaddtype_main(void)
65108
goto error;
66109
}
67110

68-
if (PyModule_AddObject(m, "pi", (PyObject *)QuadPrecision_pi) < 0) goto error;
69-
if (PyModule_AddObject(m, "e", (PyObject *)QuadPrecision_e) < 0) goto error;
70-
if (PyModule_AddObject(m, "log2e", (PyObject *)QuadPrecision_log2e) < 0) goto error;
71-
if (PyModule_AddObject(m, "log10e", (PyObject *)QuadPrecision_log10e) < 0) goto error;
72-
if (PyModule_AddObject(m, "ln2", (PyObject *)QuadPrecision_ln2) < 0) goto error;
73-
if (PyModule_AddObject(m, "ln10", (PyObject *)QuadPrecision_ln10) < 0) goto error;
74-
if (PyModule_AddObject(m, "sqrt2", (PyObject *)QuadPrecision_sqrt2) < 0) goto error;
75-
if (PyModule_AddObject(m, "sqrt3", (PyObject *)QuadPrecision_sqrt3) < 0) goto error;
76-
if (PyModule_AddObject(m, "egamma", (PyObject *)QuadPrecision_egamma) < 0) goto error;
77-
if (PyModule_AddObject(m, "phi", (PyObject *)QuadPrecision_phi) < 0) goto error;
78-
if (PyModule_AddObject(m, "quad_max", (PyObject *)QuadPrecision_quad_max) < 0) goto error;
79-
if (PyModule_AddObject(m, "quad_min", (PyObject *)QuadPrecision_quad_min) < 0) goto error;
80-
if (PyModule_AddObject(m, "quad_epsilon", (PyObject *)QuadPrecision_quad_epsilon) < 0) goto error;
81-
if (PyModule_AddObject(m, "quad_denorm_min", (PyObject *)QuadPrecision_quad_denorm_min) < 0) goto error;
82-
83111
return m;
84112

85113
error:

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,6 @@ QuadPrecision_dealloc(QuadPrecisionObject *self)
236236
Py_TYPE(self)->tp_free((PyObject *)self);
237237
}
238238

239-
PyObject* QuadPrecision_get_pi(PyObject* self, void* closure) {
240-
QuadPrecisionObject* new = QuadPrecision_raw_new(BACKEND_SLEEF);
241-
if (new == NULL) return NULL;
242-
new->value.sleef_value = SLEEF_M_PIq;
243-
return (PyObject*)new;
244-
}
245-
246-
PyObject* QuadPrecision_get_e(PyObject* self, void* closure) {
247-
QuadPrecisionObject* new = QuadPrecision_raw_new(BACKEND_SLEEF);
248-
if (new == NULL) return NULL;
249-
new->value.sleef_value = SLEEF_M_Eq;
250-
return (PyObject*)new;
251-
}
252-
253-
// Add this to the existing QuadPrecision_Type definition
254-
static PyGetSetDef QuadPrecision_getset[] = {
255-
{"pi", (getter)QuadPrecision_get_pi, NULL, "Pi constant", NULL},
256-
{"e", (getter)QuadPrecision_get_e, NULL, "Euler's number", NULL},
257-
{NULL} /* Sentinel */
258-
};
259-
260239
PyTypeObject QuadPrecision_Type = {
261240
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "numpy_quaddtype.QuadPrecision",
262241
.tp_basicsize = sizeof(QuadPrecisionObject),
@@ -267,47 +246,10 @@ PyTypeObject QuadPrecision_Type = {
267246
.tp_str = (reprfunc)QuadPrecision_str_dragon4,
268247
.tp_as_number = &quad_as_scalar,
269248
.tp_richcompare = (richcmpfunc)quad_richcompare,
270-
.tp_getset = QuadPrecision_getset,
271249
};
272250

273-
QuadPrecisionObject* initialize_constants(const Sleef_quad value, QuadBackendType backend)
274-
{
275-
QuadPrecisionObject * obj = QuadPrecision_raw_new(backend);
276-
if (backend == BACKEND_SLEEF) {
277-
obj->value.sleef_value = value;
278-
}
279-
else {
280-
obj->value.longdouble_value = Sleef_cast_to_doubleq1(value);
281-
}
282-
283-
return obj;
284-
}
285-
286251
int
287252
init_quadprecision_scalar(void)
288253
{
289-
QuadPrecisionObject* QuadPrecision_pi = initialize_constants(SLEEF_M_PIq, BACKEND_SLEEF);
290-
QuadPrecisionObject* QuadPrecision_e = initialize_constants(SLEEF_M_Eq, BACKEND_SLEEF);
291-
QuadPrecisionObject* QuadPrecision_log2e = initialize_constants(SLEEF_M_LOG2Eq, BACKEND_SLEEF);
292-
QuadPrecisionObject* QuadPrecision_log10e = initialize_constants(SLEEF_M_LOG10Eq, BACKEND_SLEEF);
293-
QuadPrecisionObject* QuadPrecision_ln2 = initialize_constants(SLEEF_M_LN2q, BACKEND_SLEEF);
294-
QuadPrecisionObject* QuadPrecision_ln10 = initialize_constants(SLEEF_M_LN10q, BACKEND_SLEEF);
295-
QuadPrecisionObject* QuadPrecision_sqrt2 = initialize_constants(SLEEF_M_SQRT2q, BACKEND_SLEEF);
296-
QuadPrecisionObject* QuadPrecision_sqrt3 = initialize_constants(SLEEF_M_SQRT3q, BACKEND_SLEEF);
297-
QuadPrecisionObject* QuadPrecision_egamma = initialize_constants(SLEEF_M_EGAMMAq, BACKEND_SLEEF);
298-
QuadPrecisionObject* QuadPrecision_phi = initialize_constants(SLEEF_M_PHIq, BACKEND_SLEEF);
299-
QuadPrecisionObject* QuadPrecision_quad_max = initialize_constants(SLEEF_QUAD_MAX, BACKEND_SLEEF);
300-
QuadPrecisionObject* QuadPrecision_quad_min = initialize_constants(SLEEF_QUAD_MIN, BACKEND_SLEEF);
301-
QuadPrecisionObject* QuadPrecision_quad_epsilon = initialize_constants(SLEEF_QUAD_EPSILON, BACKEND_SLEEF);
302-
QuadPrecisionObject* QuadPrecision_quad_denorm_min = initialize_constants(SLEEF_QUAD_DENORM_MIN, BACKEND_SLEEF);
303-
304-
if (!QuadPrecision_pi || !QuadPrecision_e || !QuadPrecision_log2e || !QuadPrecision_log10e ||
305-
!QuadPrecision_ln2 || !QuadPrecision_ln10|| !QuadPrecision_sqrt2 || !QuadPrecision_sqrt3 ||
306-
!QuadPrecision_egamma || !QuadPrecision_phi || !QuadPrecision_quad_max || !QuadPrecision_quad_min ||
307-
!QuadPrecision_quad_epsilon || !QuadPrecision_quad_denorm_min) {
308-
PyErr_SetString(PyExc_RuntimeError, "Failed to initialize QuadPrecision constants");
309-
return -1;
310-
}
311-
312254
return PyType_Ready(&QuadPrecision_Type);
313255
}

quaddtype/numpy_quaddtype/src/scalar.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ PyObject* QuadPrecision_get_e(PyObject* self, void* closure);
3737
#define PyArray_IsScalar(obj, QuadPrecDType) PyObject_TypeCheck(obj, &QuadPrecision_Type)
3838
#define PyArrayScalar_VAL(obj, QuadPrecDType) (((QuadPrecisionObject *)obj)->value)
3939

40-
QuadPrecisionObject* initialize_constants(const Sleef_quad value, QuadBackendType backend);
41-
42-
// constant objects
43-
extern QuadPrecisionObject *QuadPrecision_pi;
44-
extern QuadPrecisionObject *QuadPrecision_e;
45-
extern QuadPrecisionObject *QuadPrecision_log2e;
46-
extern QuadPrecisionObject *QuadPrecision_log10e;
47-
extern QuadPrecisionObject *QuadPrecision_ln2;
48-
extern QuadPrecisionObject *QuadPrecision_ln10;
49-
extern QuadPrecisionObject *QuadPrecision_sqrt2;
50-
extern QuadPrecisionObject *QuadPrecision_sqrt3;
51-
extern QuadPrecisionObject *QuadPrecision_egamma;
52-
extern QuadPrecisionObject *QuadPrecision_phi;
53-
extern QuadPrecisionObject *QuadPrecision_quad_max;
54-
extern QuadPrecisionObject *QuadPrecision_quad_min;
55-
extern QuadPrecisionObject *QuadPrecision_quad_epsilon;
56-
extern QuadPrecisionObject *QuadPrecision_quad_denorm_min;
57-
5840
#ifdef __cplusplus
5941
}
6042
#endif

0 commit comments

Comments
 (0)