From f33b102768c9eac0d926667ff2c063c4136a4253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ne=C4=8Dada?= Date: Thu, 23 Jan 2020 16:12:55 +0200 Subject: [PATCH] Extract and inline translation matrix reordering procedure. Former-commit-id: 9aee9e199b9c3aed76207b1314031278bc4614ea --- qpms/qpms_error.h | 17 +++++++++++++++++ qpms/translations.c | 20 +++++-------------- qpms/translations_inlines.h | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 qpms/translations_inlines.h diff --git a/qpms/qpms_error.h b/qpms/qpms_error.h index 393157c..6c46793 100644 --- a/qpms/qpms_error.h +++ b/qpms/qpms_error.h @@ -100,6 +100,13 @@ qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types); qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error (%d)", errorcode); \ } +// Same as previous, but with message. +#define QPMS_ENSURE_SUCCESS_M(x, msg, ...) { \ + int errorcode = (x); \ + if(QPMS_UNLIKELY(errorcode)) \ + qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); \ +} + #define QPMS_ENSURE(x, msg, ...) {if(QPMS_UNLIKELY(!(x))) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,msg, ##__VA_ARGS__); } @@ -109,6 +116,16 @@ qpms_dbgmsg_flags qpms_dbgmsg_enable(qpms_dbgmsg_flags types); "Unexpected error. This is most certainly a bug.");\ } +#ifdef QPMS_EVALUATE_PARANOID_ASSERTS + #define QPMS_PARANOID_ASSERT(x) {\ + if(QPMS_UNLIKELY(!(x)))\ + qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,\ + "Unexpected error. This is most certainly a bug.");\ +} +#else + #define QPMS_PARANOID_ASSERT(x) {;} +#endif + #define QPMS_NOT_IMPLEMENTED(msg, ...) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, \ "Not implemented:" msg, ##__VA_ARGS__) diff --git a/qpms/translations.c b/qpms/translations.c index 26e99e1..4564976 100644 --- a/qpms/translations.c +++ b/qpms/translations.c @@ -13,6 +13,7 @@ #include #include "qpms_error.h" #include "normalisation.h" +#include "translations_inlines.h" /* * Define macros with additional factors that "should not be there" according @@ -717,20 +718,8 @@ qpms_errno_t qpms_trans_calculator_get_trans_array(const qpms_trans_calculator * qpms_errno_t retval = qpms_trans_calculator_get_AB_arrays(c, A[0], B[0], c->nelem, 1, kdlj, r_ge_d, J); - for (size_t desti = 0; desti < destspec->n; ++desti) { - qpms_y_t desty; qpms_vswf_type_t destt; - if(QPMS_SUCCESS != qpms_uvswfi2ty(destspec->ilist[desti], &destt, &desty)) - qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, - "Invalid u. vswf index %llx.", destspec->ilist[desti]); - for (size_t srci = 0; srci < srcspec->n; ++srci){ - qpms_y_t srcy; qpms_vswf_type_t srct; - if(QPMS_SUCCESS != qpms_uvswfi2ty(srcspec->ilist[srci], &srct, &srcy)) - qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, - "Invalid u. vswf index %llx.", srcspec->ilist[srci]); - target[srci * srcstride + desti * deststride] - = (srct == destt) ? A[desty][srcy] : B[desty][srcy]; - } - } + qpms_trans_array_from_AB(target, destspec, deststride, srcspec, srcstride, + A[0], B[0], c->lMax); return retval; } @@ -766,7 +755,8 @@ qpms_errno_t qpms_trans_calculator_get_trans_array_e32_e(const qpms_trans_calcul qpms_errno_t retval = qpms_trans_calculator_get_AB_arrays_e32_e(c, A, Aerr, B, Berr, ldAB, 1, eta, k, b1, b2, beta, particle_shift, maxR, maxK, parts); - for (size_t desti = 0; desti < destspec->n; ++desti) { + for (size_t desti = 0; desti < destspec->n; ++desti) { + // TODO replace with (modified) qpms_trans_array_from_AB() qpms_y_t desty; qpms_vswf_type_t destt; if(QPMS_SUCCESS != qpms_uvswfi2ty(destspec->ilist[desti], &destt, &desty)) qpms_pr_error_at_flf(__FILE__,__LINE__,__func__, diff --git a/qpms/translations_inlines.h b/qpms/translations_inlines.h new file mode 100644 index 0000000..854bdc3 --- /dev/null +++ b/qpms/translations_inlines.h @@ -0,0 +1,38 @@ +#ifndef TRANSLATIONS_INLINES_H +#define TRANSLATIONS_INLINES_H +#include "translations.h" +#include "indexing.h" + +/// Rearranges the default-ordered "A,B" array elements into "bspec"-defined matrix. +// TODO DOC +static inline void qpms_trans_array_from_AB( + complex double *t, + const qpms_vswf_set_spec_t *const t_destspec, + const size_t t_deststride, + const qpms_vswf_set_spec_t *const t_srcspec, + const size_t t_srcstride, + const complex double *const A, const complex double *const B, + /// A and B matrices' lMax. + /** This also determines their size and stride: they are assumed to + * be square matrices of size `nelem * nelem` where + * `nelem = qpms_lMax2nelem(lMax_AB)` + */ + const qpms_l_t lMax_AB + ) { + QPMS_PARANOID_ASSERT(lMax_AB >= t_srcspec->lMax && lMax_AB >= t_destspec->lMax); + const qpms_y_t nelem_AB = qpms_lMax2nelem(lMax_AB); + for (size_t desti = 0; desti < t_destspec->n; ++desti) { + qpms_y_t desty; qpms_vswf_type_t destt; + QPMS_ENSURE_SUCCESS_M(qpms_uvswfi2ty(t_destspec->ilist[desti], &destt, &desty), + "Invalid u. vswf index %llx.", t_destspec->ilist[desti]); + for (size_t srci = 0; srci < t_srcspec->n; ++srci){ + qpms_y_t srcy; qpms_vswf_type_t srct; + QPMS_ENSURE_SUCCESS_M(qpms_uvswfi2ty(t_srcspec->ilist[srci], &srct, &srcy), + "Invalid u. vswf index %llx.", t_srcspec->ilist[srci]); t[srci * t_srcstride + desti * t_deststride] + = (srct == destt) ? A[desty*nelem_AB + srcy] : B[desty*nelem_AB + srcy]; + } + } +} + + +#endif