Skip to content

Commit

Permalink
xforms converting output Pars make them output of conversion not inpu…
Browse files Browse the repository at this point in the history
…t - #511
  • Loading branch information
mohawk2 committed Dec 28, 2024
1 parent 77e9bd9 commit a45a268
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/PDL/Core/pdlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,21 @@ pdl *pdl_scalar(PDL_Anyval anyval) {
return it;
}

pdl_error pdl__converttypei_new_recprotect(pdl *PARENT, pdl *CHILD, pdl_datatypes totype, int recurse_count);
pdl_error pdl__get_convertedpdl_recprotect(pdl *old, pdl **retval, pdl_datatypes type, int recurse_count) {
pdl_error pdl__converttypei_new_recprotect(pdl *PARENT, pdl *CHILD, pdl_datatypes totype, pdl_datatypes force_intype, int recurse_count);
pdl_error pdl__get_convertedpdl_recprotect(pdl *old, pdl **retval, pdl_datatypes type, char switch_sense, int recurse_count) {
pdl_error PDL_err = {0, NULL, 0};
PDL_RECURSE_CHECK(recurse_count);
PDLDEBUG_f(printf("pdl_get_convertedpdl\n"));
if (old->datatype == type) { *retval = old; return PDL_err; }
char was_flowing = (old->state & PDL_DATAFLOW_F);
pdl *it = pdl_pdlnew();
if (!it) return pdl_make_error_simple(PDL_EFATAL, "Out of Memory\n");
PDL_err = pdl__converttypei_new_recprotect(old, it, type, recurse_count + 1);
if (switch_sense) {
PDL_err = pdl__converttypei_new_recprotect(it, old, old->datatype, type, recurse_count + 1);
if (PDL_err.error) { pdl_destroy(it); return PDL_err; }
PDL_err = pdl_setdims(it, old->dims, old->ndims);
} else
PDL_err = pdl__converttypei_new_recprotect(old, it, type, old->datatype, recurse_count + 1);
if (PDL_err.error) { pdl_destroy(it); return PDL_err; }
if (was_flowing)
it->state |= PDL_DATAFLOW_F;
Expand All @@ -147,7 +152,7 @@ pdl_error pdl__get_convertedpdl_recprotect(pdl *old, pdl **retval, pdl_datatypes
}
pdl *pdl_get_convertedpdl(pdl *old, pdl_datatypes type) {
pdl *retval;
pdl_error PDL_err = pdl__get_convertedpdl_recprotect(old, &retval, type, 0);
pdl_error PDL_err = pdl__get_convertedpdl_recprotect(old, &retval, type, 0, 0);
return PDL_err.error ? NULL : retval;
}

Expand Down Expand Up @@ -1183,7 +1188,7 @@ pdl_error pdl__type_convert(pdl_trans *trans, int recurse_count) {
"%s: cannot convert output ndarray %s from type %s to %s with parent",
vtable->name, vtable->par_names[i],
PDL_TYPENAME(pdl->datatype), PDL_TYPENAME(new_dtype));
PDL_RETERROR(PDL_err, pdl__get_convertedpdl_recprotect(pdl, &pdl, new_dtype, recurse_count + 1));
PDL_RETERROR(PDL_err, pdl__get_convertedpdl_recprotect(pdl, &pdl, new_dtype, i >= nparents, recurse_count + 1));
if (pdl->datatype != new_dtype)
return pdl_make_error_simple(PDL_EFATAL, "type not expected value after get_convertedpdl\n");
/* if type-convert output, put in end-area */
Expand Down
6 changes: 3 additions & 3 deletions lib/PDL/Core/pdlconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pdl_transvtable pdl_converttypei_vtable = {
};

pdl_error pdl__set_output_type_badvalue(pdl_trans *trans, int recurse_count);
pdl_error pdl__converttypei_new_recprotect(pdl *PARENT, pdl *CHILD, pdl_datatypes totype, int recurse_count) {
pdl_error pdl__converttypei_new_recprotect(pdl *PARENT, pdl *CHILD, pdl_datatypes totype, pdl_datatypes force_intype, int recurse_count) {
pdl_error PDL_err = {0, NULL, 0};
PDL_RECURSE_CHECK(recurse_count);
pdl_trans *trans = pdl_create_trans(&pdl_converttypei_vtable);
Expand All @@ -260,13 +260,13 @@ pdl_error pdl__converttypei_new_recprotect(pdl *PARENT, pdl *CHILD, pdl_datatype
trans->bvalflag = 1;
trans->pdls[1]->state |= PDL_BADVAL;
}
trans->__datatype = PARENT->datatype;
trans->__datatype = PARENT->datatype = force_intype;
PDL_RETERROR(PDL_err, pdl__set_output_type_badvalue(trans, recurse_count + 1));
trans->pdls[2] = trans->pdls[1]; /* copy for make_trans_mutual */
trans->pdls[1]->datatype = params->totype = totype;
PDL_RETERROR(PDL_err, pdl_make_trans_mutual((pdl_trans *)trans));
return PDL_err;
}
pdl_error pdl_converttypei_new(pdl *PARENT, pdl *CHILD, pdl_datatypes totype) {
return pdl__converttypei_new_recprotect(PARENT, CHILD, totype, 0);
return pdl__converttypei_new_recprotect(PARENT, CHILD, totype, PARENT->datatype, 0);
}
13 changes: 9 additions & 4 deletions t/core.t
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,15 @@ is 0+$o_float->trans_children, 0, '0 trans_children on output from flowing outpu
eval {PDL::eqvec(double([1,2]), double([1,2]), float(0)->slice(''))};
like $@, qr/cannot convert/, "error when flowing output to xform, out forcetype != supplied out type";

PDL::eqvec(double([1,2])->flowing, double([1,2]), $o_float = float([0]));
is 0+$o_float->trans_children, 1, 'converted output of flowing xform wrongly has trans_children';
is $o_float->trans_parent, undef, 'converted output of flowing xform wrongly has no trans_parent';
is_pdl $o_float, float([0]), 'converted output of flowing xform has currently wrong value';
PDL::eqvec(double([1,2])->flowing, double([1,2]), $o_float = float(0));
is 0+$o_float->trans_children, 0, 'converted output of flowing xform has no trans_children';
is $o_float->trans_parent->vtable->name, 'converttypei_new', 'converted output of flowing xform has convert trans_parent';
is_pdl $o_float, float(1), 'converted output of flowing xform has right value';

PDL::eqvec(double([1,2],3,4)->flowing, double([1,2],3,5), my $o_byte = byte([0,0,0]));
is 0+$o_byte->trans_children, 0, 'converted output of flowing xform has no trans_children';
is $o_byte->trans_parent->vtable->name, 'converttypei_new', 'converted output of flowing xform has convert trans_parent';
is_pdl $o_byte, byte([1,1,0]), 'converted output of flowing xform has right value';

for ([\&float,\&cfloat,\&cdouble], [\&double,\&cdouble,\&cfloat], [\&ldouble,\&cldouble]) {
my ($rt, $ct, $other_ct) = @$_;
Expand Down

0 comments on commit a45a268

Please sign in to comment.