|
1 | 1 | #include <Python.h>
|
| 2 | +#include <sleef.h> |
| 3 | +#include <sleefquad.h> |
| 4 | +#include <string.h> |
2 | 5 |
|
3 | 6 | #define PY_ARRAY_UNIQUE_SYMBOL QuadPrecType_ARRAY_API
|
4 | 7 | #define PY_UFUNC_UNIQUE_SYMBOL QuadPrecType_UFUNC_API
|
@@ -26,8 +29,48 @@ static PyObject* py_is_longdouble_128(PyObject* self, PyObject* args) {
|
26 | 29 | }
|
27 | 30 | }
|
28 | 31 |
|
| 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 | + |
29 | 71 | static PyMethodDef module_methods[] = {
|
30 | 72 | {"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"}, |
31 | 74 | {NULL, NULL, 0, NULL}
|
32 | 75 | };
|
33 | 76 |
|
@@ -65,21 +108,6 @@ PyInit__quaddtype_main(void)
|
65 | 108 | goto error;
|
66 | 109 | }
|
67 | 110 |
|
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 |
| - |
83 | 111 | return m;
|
84 | 112 |
|
85 | 113 | error:
|
|
0 commit comments