/***************************************************************************************/ /* File: arrayOp.c */ /* Author: Sheng Peng */ /* Version: 1.1 */ /* */ /* Use aSub to merge number of inputs to one output array */ /* INPA has to be ULONG and indicate number of inputs */ /* The type of INPB and following must be same as OUTA */ /* */ /* Use aSub to split one array (INPA) to up to 20 subArrays/elements */ /* */ /* INPA should be an array of any type (FTA) and size (NOA) */ /* OUTA has to be same type as INPA array if NOVA is not 0 */ /* OUTA is always a subArray of INPA from index 0 */ /* */ /* For x = B ~ U */ /* INPx is to indicate the offset index ( #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef OK #define OK 0 /* no problems */ #endif #ifndef ERROR #define ERROR -1 #endif #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif #ifndef min #define min(a,b) (((a) < (b)) ? (a) : (b)) #endif /***********************************************************************************/ /* * (aSub has 21 inputs and 21 outputs) */ static char num2name[]= { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U' }; /***********************************************************************************/ /* Merge scalar and-or array into a big array */ long arrayMASubInit(struct aSubRecord *psub); long arrayMASubProc(struct aSubRecord *psub); /* Split array into scalar or subarray */ long arraySASubInit(struct aSubRecord *psub); long arraySASubProc(struct aSubRecord *psub); /* Overwrite one array over another */ long arrayOASubInit(struct aSubRecord *psub); long arrayOASubProc(struct aSubRecord *psub); #if EPICS_VERSION>=3 && EPICS_REVISION>=14 epicsRegisterFunction(arrayMASubInit); epicsRegisterFunction(arrayMASubProc); epicsRegisterFunction(arraySASubInit); epicsRegisterFunction(arraySASubProc); epicsRegisterFunction(arrayOASubInit); epicsRegisterFunction(arrayOASubProc); #endif /**************************************************************************/ /* aSub Function INAM: arrayMASubInit */ /**************************************************************************/ long arrayMASubInit(struct aSubRecord *psub) { #if 0 /* These pointers will allow us to access inputs/outputs sequentially as array */ DBLINK *pInputLink = &(psub->inpa);; /* Address of the input field we are currently checking */ DBLINK *pOutputLink = &(psub->outa);; /* Address of the input field we are currently checking */ double **pInput = (double **) &psub->a; /* where is my input vector */ double **pOutput = (double **) &psub->vala; /* where is my output vector */ epicsEnum16 *pFtInput = &psub->fta; /* what is the input type */ epicsEnum16 *pFtOutput = &psub->ftva; /* what is the output type */ epicsUInt32 *pNoInput = &psub->noa; /* number of components for input */ epicsUInt32 *pNoOutput = &psub->nova; /* number of components for output */ #endif /* We don't do anything here because we concern any type, size and value change during fly */ if (psub->tpro) printf("\n!!! arrayMASubInit %s was called !!!\n", psub->name); /* We don't really need dpvt in this case */ psub->dpvt = NULL; return OK; } /**************************************************************************/ /* aSub Function SNAM: arrayMASubProc */ /**************************************************************************/ /* In this function, we take inputs and merge them into an arry ... */ /* */ /* INPA: indicate number of inputs */ /* INPB ~ INPx: inputs */ /* */ /* VALA: output to array */ /* */ /**************************************************************************/ long arrayMASubProc(struct aSubRecord *psub) { /* We don't care link here, so we only care value, type and size */ int loop; epicsUInt32 nOfInputs = 0; unsigned int totalElements = 0; unsigned int sizeOfElement = 0; void **pInputs = (void **) &(psub->b); /* where are my inputs */ epicsEnum16 * pFtInputs = &(psub->ftb); /* what are the inputs type */ epicsUInt32 * pNoInputs = &(psub->nob); /* number of elements for inputs */ void * pOutput = psub->vala; /* where is my output vector */ epicsEnum16 FtOutput = psub->ftva; /* what is the output type */ epicsUInt32 nOfOutput = psub->nova; /* number of elements for output */ if (psub->tpro) printf("\n!!! arrayMASubProc [%s] was called !!!\n", psub->name); /* get nOfInputs from INPA */ if(psub->fta != DBF_ULONG || psub->noa != 1) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("There is no legal input for INPA of [%s], it must be one long!\n", psub->name); return ERROR; } nOfInputs = (*((unsigned long *)(psub->a))); if(nOfInputs < 1 || nOfInputs > 20) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("There is no legal input for INPA of [%s], it must be between [1,20]!\n", psub->name); return ERROR; } if (psub->tpro) printf("\n!!! [%s] expects %u inputs !!!\n", psub->name, nOfInputs); /* Check type match and calculate totalElements */ for(loop = 0; loop < nOfInputs; loop++) { totalElements += pNoInputs[loop]; if(pFtInputs[loop] != FtOutput) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("The type of INP%c does not match OUTA of '%s'!\n", num2name[loop+1], psub->name); return ERROR; } #if 0 if(totalElements > nOfOutput) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("The total number of elements is bigger than output size till INP%c of [%s]!\n", num2name[loop+1], psub->name); return ERROR; } #endif } totalElements = min(totalElements, nOfOutput); /* figure out sizeOfElement */ switch(FtOutput) { case DBF_CHAR: case DBF_UCHAR: sizeOfElement = 1; break; case DBF_SHORT: case DBF_USHORT: case DBF_ENUM: sizeOfElement = 2; break; case DBF_LONG: case DBF_ULONG: case DBF_FLOAT: sizeOfElement = 4; break; case DBF_DOUBLE: sizeOfElement = 8; break; default: recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("Illegal type of inputs of [%s]!\n", psub->name); return ERROR; } /* Start to merge */ for(loop = 0; loop < nOfInputs; loop++) { memcpy((void *)pOutput, (void *)pInputs[loop], min(totalElements,pNoInputs[loop])*sizeOfElement); if (psub->tpro) printf("\nMemcpy %u elements for %s\n", min(totalElements,pNoInputs[loop]), psub->name); pOutput = ((char *)pOutput) + pNoInputs[loop]*sizeOfElement; totalElements -= pNoInputs[loop]; } if (psub->tpro) printf("\narrayMerge was done for %s\n", psub->name); return OK; } /**************************************************************************/ /* aSub Function INAM: arraySASubInit */ /**************************************************************************/ long arraySASubInit(struct aSubRecord *psub) { #if 0 /* These pointers will allow us to access inputs/outputs sequentially as array */ DBLINK *pInputLink = &(psub->inpa);; /* Address of the input field we are currently checking */ DBLINK *pOutputLink = &(psub->outa);; /* Address of the input field we are currently checking */ double **pInput = (double **) &psub->a; /* where is my input vector */ double **pOutput = (double **) &psub->vala; /* where is my output vector */ epicsEnum16 *pFtInput = &psub->fta; /* what is the input type */ epicsEnum16 *pFtOutput = &psub->ftva; /* what is the output type */ epicsUInt32 *pNoInput = &psub->noa; /* number of components for input */ epicsUInt32 *pNoOutput = &psub->nova; /* number of components for output */ #endif /* We don't do anything here because we concern any type, size and value change during fly */ if (psub->tpro) printf("\n!!! arraySASubInit %s was called !!!\n", psub->name); /* We don't really need dpvt in this case */ psub->dpvt = NULL; return OK; } /***************************************************************************************/ /* aSub Function SNAM: arraySASubProc */ /***************************************************************************************/ /* Use aSub to split one array (INPA) to up to 20 subArrays/elements */ /* */ /* INPA should be an array of any type (FTA) and size (NOA) */ /* OUTA has to be same type as INPA array if NOVA is not 0 */ /* OUTA is always a subArray of INPA from index 0 */ /* */ /* For x = B ~ U */ /* INPx is to indicate the offset index (a); /* where are my inputs */ epicsEnum16 * pFtInputs = &(psub->fta); /* what are the inputs type */ epicsUInt32 * pNoInputs = &(psub->noa); /* number of elements for inputs */ void ** pOutputs = (void **) &(psub->vala); /* where is my output vector */ epicsEnum16 * pFtOutputs = &(psub->ftva); /* what is the output type */ epicsUInt32 * pNoOutputs = &(psub->nova); /* number of elements for output */ if (psub->tpro) printf("\n!!! arraySASubProc [%s] was called !!!\n", psub->name); /* figure out sizeOfElement of INPA */ switch(pFtInputs[0]) { case DBF_CHAR: case DBF_UCHAR: sizeOfElement = 1; break; case DBF_SHORT: case DBF_USHORT: case DBF_ENUM: sizeOfElement = 2; break; case DBF_LONG: case DBF_ULONG: case DBF_FLOAT: sizeOfElement = 4; break; case DBF_DOUBLE: sizeOfElement = 8; break; default: recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("Illegal type of INPA of [%s]!\n", psub->name); return ERROR; } totalElements = pNoInputs[0]; /* Check INPA and OUTA type. They have to be same if NOVA is not zero*/ if(pNoOutputs[0] && (pFtInputs[0] != pFtOutputs[0])) { recGblSetSevr(psub, CALC_ALARM, INVALID_ALARM); printf("INPA of [%s] has different type of OUTA!\n", psub->name); } else { memcpy((void *)pOutputs[0], (void *)pInputs[0], min(totalElements,pNoOutputs[0])*sizeOfElement); if (psub->tpro) printf("\nMemcpy %u elements for %s OUTA\n", min(totalElements,pNoOutputs[0]), psub->name); } /* Check INPB ~ INPU and OUTB ~ OUTU */ for(loop = 1; loop < 21; loop++) { unsigned int offsetIndex; /* if NOx is not 1, no way to figure out offset index, if NOVx is <= 0, no memcpy needed */ if(pNoOutputs[loop] <=0 || pNoInputs[loop] != 1) continue; /* INPx type has to be ULONG */ if(pFtInputs[loop] != DBF_ULONG) continue; /* offsetIndex has to be less than totalElements */ offsetIndex = (*((unsigned long *)pInputs[loop])); if(offsetIndex >= totalElements) { recGblSetSevr(psub, CALC_ALARM, INVALID_ALARM); printf("\nINP%c indicates an out-of-range offset for %s \n", num2name[loop], psub->name); continue; } /* Type has to match */ if(pFtInputs[0] != pFtOutputs[loop]) { recGblSetSevr(psub, CALC_ALARM, INVALID_ALARM); printf("INPA of [%s] has different type of OUT%c!\n", psub->name, num2name[loop]); continue; } else { memcpy((void *)pOutputs[loop], (void *)((char *)pInputs[0] + offsetIndex*sizeOfElement), min(totalElements-offsetIndex, pNoOutputs[loop])*sizeOfElement); if (psub->tpro) printf("\nMemcpy %u elements for %s OUT%c\n", min(totalElements-offsetIndex, pNoOutputs[loop]), psub->name, num2name[loop]); } } if (psub->tpro) printf("\narraySplit was done for %s\n", psub->name); return OK; } /**************************************************************************/ /* aSub Function INAM: arrayOASubInit */ /**************************************************************************/ long arrayOASubInit(struct aSubRecord *psub) { #if 0 /* These pointers will allow us to access inputs/outputs sequentially as array */ DBLINK *pInputLink = &(psub->inpa);; /* Address of the input field we are currently checking */ DBLINK *pOutputLink = &(psub->outa);; /* Address of the input field we are currently checking */ double **pInput = (double **) &psub->a; /* where is my input vector */ double **pOutput = (double **) &psub->vala; /* where is my output vector */ epicsEnum16 *pFtInput = &psub->fta; /* what is the input type */ epicsEnum16 *pFtOutput = &psub->ftva; /* what is the output type */ epicsUInt32 *pNoInput = &psub->noa; /* number of components for input */ epicsUInt32 *pNoOutput = &psub->nova; /* number of components for output */ #endif /* We don't do anything here because we concern any type, size and value change during fly */ if (psub->tpro) printf("\n!!! arrayOASubInit %s was called !!!\n", psub->name); /* We don't really need dpvt in this case */ psub->dpvt = NULL; return OK; } /**************************************************************************/ /* aSub Function SNAM: arrayOASubProc */ /**************************************************************************/ /* In this function, we take inputs and merge them into an arry ... */ /* */ /* INPA: indicate number of inputs */ /* INPB ~ INPx: inputs */ /* */ /* VALA: output to array */ /* */ /**************************************************************************/ long arrayOASubProc(struct aSubRecord *psub) { /* We don't care link here, so we only care value, type and size */ int loop; epicsUInt32 nOfInputs = 0; unsigned int totalElements = 0; unsigned int sizeOfElement = 0; void **pInputs = (void **) &(psub->b); /* where are my inputs */ epicsEnum16 * pFtInputs = &(psub->ftb); /* what are the inputs type */ epicsUInt32 * pNoInputs = &(psub->nob); /* number of elements for inputs */ void * pOutput = psub->vala; /* where is my output vector */ epicsEnum16 FtOutput = psub->ftva; /* what is the output type */ epicsUInt32 nOfOutput = psub->nova; /* number of elements for output */ if (psub->tpro) printf("\n!!! arrayOASubProc [%s] was called !!!\n", psub->name); /* get nOfInputs from INPA */ if(psub->fta != DBF_ULONG || psub->noa != 1) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("There is no legal input for INPA of [%s], it must be one long!\n", psub->name); return ERROR; } nOfInputs = (*((unsigned long *)(psub->a))); if(nOfInputs < 1 || nOfInputs > 20) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("There is no legal input for INPA of [%s], it must be between [1,20]!\n", psub->name); return ERROR; } if (psub->tpro) printf("\n!!! [%s] expects %u inputs !!!\n", psub->name, nOfInputs); /* Check type match and calculate totalElements */ for(loop = 0; loop < nOfInputs; loop++) { totalElements += pNoInputs[loop]; if(pFtInputs[loop] != FtOutput) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("The type of INP%c does not match OUTA of '%s'!\n", num2name[loop+1], psub->name); return ERROR; } #if 0 if(totalElements > nOfOutput) { recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("The total number of elements is bigger than output size till INP%c of [%s]!\n", num2name[loop+1], psub->name); return ERROR; } #endif } /*totalElements = min(totalElements, nOfOutput);*/ /* figure out sizeOfElement */ switch(FtOutput) { case DBF_CHAR: case DBF_UCHAR: sizeOfElement = 1; break; case DBF_SHORT: case DBF_USHORT: case DBF_ENUM: sizeOfElement = 2; break; case DBF_LONG: case DBF_ULONG: case DBF_FLOAT: sizeOfElement = 4; break; case DBF_DOUBLE: sizeOfElement = 8; break; default: recGblSetSevr(psub, LINK_ALARM, INVALID_ALARM); printf("Illegal type of inputs of [%s]!\n", psub->name); return ERROR; } /* Start to overlay */ for(loop = 0; loop < nOfInputs; loop++) { memcpy((void *)pOutput, (void *)pInputs[loop], min(nOfOutput,pNoInputs[loop])*sizeOfElement); if (psub->tpro) printf("\nMemcpy %u elements for %s\n", min(nOfOutput,pNoInputs[loop]), psub->name); /*pOutput = ((char *)pOutput) + pNoInputs[loop]*sizeOfElement; Don't increment starting place*/ totalElements -= pNoInputs[loop]; } if (psub->tpro) printf("\narrayOverlay was done for %s\n", psub->name); return OK; }