Skip to content

Commit 5e3e1c4

Browse files
committed
undo storage scheme: It bites with output being the same as input
1 parent 847cbf4 commit 5e3e1c4

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

mpfdtype/src/casts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mpf_to_mof_strided_loop(PyArrayMethod_Context *context,
6262
mpfr_prec_t prec_in = ((MPFDTypeObject *)context->descriptors[0])->precision;
6363
mpfr_prec_t prec_out = ((MPFDTypeObject *)context->descriptors[1])->precision;
6464

65-
mpfr_t in, out;
65+
mpfr_ptr in, out;
6666

6767
while (N--) {
6868
mpf_load(in, in_ptr, prec_in);
@@ -176,7 +176,7 @@ numpy_to_mpf_strided_loop(PyArrayMethod_Context *context,
176176

177177
mpfr_prec_t prec_out = ((MPFDTypeObject *)context->descriptors[1])->precision;
178178

179-
mpfr_t out;
179+
mpfr_ptr out;
180180

181181
while (N--) {
182182
T *in = (T *)in_ptr;
@@ -308,7 +308,7 @@ mpf_to_numpy_strided_loop(PyArrayMethod_Context *context,
308308

309309
mpfr_prec_t prec_in = ((MPFDTypeObject *)context->descriptors[0])->precision;
310310

311-
mpfr_t in;
311+
mpfr_ptr in;
312312

313313
while (N--) {
314314
mpf_load(in, in_ptr, prec_in);

mpfdtype/src/dtype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ mpf_setitem(MPFDTypeObject *descr, PyObject *obj, char *dataptr)
135135
// TODO: This doesn't support unaligned access, maybe we should just
136136
// allow DTypes to say that they cannot be unaligned?!
137137

138-
mpfr_t res;
138+
mpfr_ptr res;
139139
mpf_load(res, dataptr, descr->precision);
140140
mpfr_set(res, value->mpf.x, MPFR_RNDN);
141141
mpf_store(dataptr, res);
@@ -160,7 +160,7 @@ mpf_getitem(MPFDTypeObject *descr, char *dataptr)
160160
if (new == NULL) {
161161
return NULL;
162162
}
163-
mpfr_t val;
163+
mpfr_ptr val;
164164
mpf_load(val, dataptr, descr->precision);
165165
mpfr_set(new->mpf.x, val, MPFR_RNDN);
166166

mpfdtype/src/dtype.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,47 @@ typedef struct {
1717

1818

1919
/*
20-
* It would be slightly easier/faster to store the internals, but use the
21-
* proper public API here:
20+
* It would be more compat to just store the kind, exponent and signfificand,
21+
* however. For in-place operations mpfr needs cannot share the same
22+
* significand for multiple ops (but can have an op repeat).
23+
* So not storeing only those (saving 16 bytes of 48 for a 128 bit number)
24+
* removes the need to worry about this.
2225
*/
23-
typedef struct {
24-
int kind;
25-
mpfr_exp_t exp;
26-
mp_limb_t significand[];
27-
} mpf_storage;
26+
static_assert(_Alignof(mpfr_t) >= _Alignof(mp_limb_t),
27+
"mpfr_t storage not aligned as much as limb_t?!");
28+
typedef mpfr_t mpf_storage;
2829

2930
extern PyArray_DTypeMeta MPFDType;
3031

3132

3233
/*
33-
* We currently use this also when init would suffice (to set significand).
34+
* Load into an mpfr_ptr, use a macro which may allow easier changing back
35+
* to a compact storage scheme.
3436
*/
3537
static inline void
36-
mpf_load(mpfr_t x, char *data_ptr, mpfr_prec_t precision) {
37-
mpf_storage *mpf_ptr = (mpf_storage *)data_ptr;
38-
/* if the kind is 0, reinitialize significand (presumably it never was) */
39-
if (mpf_ptr->kind == 0) {
40-
mpfr_custom_init(mpf_ptr->significand, precision);
41-
mpfr_custom_init_set(x, MPFR_NAN_KIND, 0, precision, mpf_ptr->significand);
42-
}
43-
else {
44-
mpfr_custom_init_set(
45-
x, mpf_ptr->kind, mpf_ptr->exp, precision, mpf_ptr->significand);
38+
_mpf_load(mpfr_ptr *x, char *data_ptr, mpfr_prec_t precision) {
39+
x[0] = (mpfr_ptr)data_ptr;
40+
/*
41+
* We must ensure the signficand is initialized, but NumPy only ensures
42+
* everything is NULL'ed.
43+
*/
44+
if (mpfr_custom_get_significand(x[0]) == NULL) {
45+
void *signficand = data_ptr + sizeof(mpf_storage);
46+
mpfr_custom_init(signficand, precision);
47+
mpfr_custom_init_set(x[0], MPFR_NAN_KIND, 0, precision, signficand);
4648
}
4749
}
50+
#define mpf_load(x, data_ptr, precision) _mpf_load(&x, data_ptr, precision)
51+
52+
4853

4954
/*
50-
* Signficand is always stored, but write back kind and exp
55+
* Not actually required in the current scheme, but keep for now.
56+
* (I had a more compat storage scheme at some point.)
5157
*/
5258
static inline void
5359
mpf_store(char *data_ptr, mpfr_t x) {
54-
mpf_storage *mpf_ptr = (mpf_storage *)data_ptr;
55-
assert(mpfr_custom_get_signficand(x) == mpf_ptr->Signficand);
56-
mpf_ptr->kind = mpfr_custom_get_kind(x);
57-
mpf_ptr->exp = mpfr_custom_get_exp(x);
60+
assert(data_ptr == mpfr_t);
5861
}
5962

6063

mpfdtype/src/umath.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ generic_unary_op_strided_loop(PyArrayMethod_Context *context,
3737
mpfr_prec_t prec1 = ((MPFDTypeObject *)context->descriptors[0])->precision;
3838
mpfr_prec_t prec2 = ((MPFDTypeObject *)context->descriptors[1])->precision;
3939

40-
mpfr_t in, out;
40+
mpfr_ptr in, out;
4141

4242
while (N--) {
4343
mpf_load(in, in_ptr, prec1);
@@ -213,7 +213,7 @@ generic_binop_strided_loop(PyArrayMethod_Context *context,
213213
mpfr_prec_t prec2 = ((MPFDTypeObject *)context->descriptors[0])->precision;
214214
mpfr_prec_t prec3 = ((MPFDTypeObject *)context->descriptors[0])->precision;
215215

216-
mpfr_t in1, in2, out;
216+
mpfr_ptr in1, in2, out;
217217

218218
while (N--) {
219219
mpf_load(in1, in1_ptr, prec1);
@@ -463,7 +463,7 @@ generic_comp_strided_loop(PyArrayMethod_Context *context,
463463
mpfr_prec_t prec1 = ((MPFDTypeObject *)context->descriptors[0])->precision;
464464
mpfr_prec_t prec2 = ((MPFDTypeObject *)context->descriptors[0])->precision;
465465

466-
mpfr_t in1, in2;
466+
mpfr_ptr in1, in2;
467467

468468
while (N--) {
469469
mpf_load(in1, in1_ptr, prec1);

0 commit comments

Comments
 (0)