/*
 * dsp_fixpt_sim.c 
 *
 * DSP Blockset helper function.
 *
 * Check input/output port fixed-point data type information. 
 *
 * NOTE: This is a query on port information. Therefore, 
 * the information must already be set for the port.
 *
 *
 * Copyright (c) 1995-2000 The MathWorks, Inc.
 * $Revision: 1.9 $ $Date: 2000/03/16 03:16:51 $
 */

#include "dsp_fixpt_sim.h"

/* ------------------------------------------------ */
/* Include common Fixed-Point Data Type definitions */
/* ------------------------------------------------ */

/* ------------------------------------------------------------------------- */
/* xxx BEGIN: THIS WAS LIFTED (ALMOST) DIRECTLY FROM "Revision: 1.14"        */
/*            OF "../../fixpoint/fix_util.h"                                 */
/*                                                                           */
/* xxx FIND OUT HOW TO DO THIS CORRECTLY FOR BaT NOT TO ERROR!               */
/* #include "../../fixpoint/fix_util.h"                                      */
/* xxx BaT CAN'T CURRENTLY FIND THIS FILE DURING COMPILE TIME!               */
/* ------------------------------------------------------------------------- */
#define USE_FIX 0
#define FixPt_DataTypeIsFix(a)    ( (a)->FixClass == USE_FIX )
/* ------------------------------------------------------------------------- */
/* xxx END: THIS WAS LIFTED (ALMOST) DIRECTLY FROM "Revision: 1.14"          */
/*            OF "../../fixpoint/fix_util.h"                             xxx */
/* ------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
/* xxx BEGIN: THIS WAS LIFTED DIRECTLY FROM Wed Sep 29 18:54:08 1999 VERSION */
/*            OF "../../fixpoint/fixpt_common.h"                         xxx */
/*                                                                           */
/* xxx FIND OUT HOW TO DO THIS CORRECTLY FOR BaT NOT TO ERROR!               */
/* #include "../../fixpoint/fixpt_common.h"                                  */
/* xxx BaT CAN'T CURRENTLY FIND THIS FILE DURING COMPILE TIME!               */
/* ------------------------------------------------------------------------- */
/*
 *   Fixed Point Data Type Property Structure
 */
typedef struct fixProp 
{
    unsigned int    FixClass: 15;
    unsigned int    IsSigned:  1;
    unsigned int    MantBits: 16;

    int        FixExp;

    double    Slope;
    double    Bias;

} fixProp;
/* ------------------------------------------------------------------------- */
/* xxx END:   THIS WAS LIFTED DIRECTLY FROM Wed Sep 29 18:54:08 1999 VERSION */
/*            OF "../../fixpoint/fixpt_common.h"                         xxx */
/* ------------------------------------------------------------------------- */


/* "isFixedPointDspDataType" is a subset of "IsFixPtDataType", a function */
/* which is externally defined in "..\..\fixpoint\fixpt_common.c"         */
boolean_T isFixedPointDspDataType(const char *dtName)
{
    /* Note that we are purposefully NOT checing for:
     *
     * (strncmp(dtName, "flt", 3)  == 0)
     *
     * This would indicate a fixed-point data type containing
     * NO fixed-point characteristics, only quantized floating-point.
     */
    return( (strncmp(dtName, "sfix", 4) == 0) || (strncmp(dtName, "ufix", 4) == 0) );
}

/* "isQuantizedFltPtDspDataType" is a subset of "IsFixPtDataType", */
/* which is externally defined in "..\..\fixpoint\fixpt_common.c"  */
boolean_T isQuantizedFltPtDspDataType(const char *dtName) {
    /* Note that we are purposefully NOT checing for:
     *
     * (strncmp(dtName, "sfix", 4) == 0) || (strncmp(dtName, "ufix", 4) == 0)
     *
     * This would indicate a fixed-point data type containing
     * fixed-point characteristics, and NOT quantized floating-point.
     */
    return(strncmp(dtName, "flt", 3) == 0);
}

boolean_T isInputSignedFixedPoint(SimStruct *S, int_T port) {
    boolean_T     retValue   = false;
    const DTypeId dataTypeId = ssGetInputPortDataType(S, port);
    const char   *dtName     = ssGetDataTypeName(S, dataTypeId);

    if ( isFixedPointDspDataType(dtName) )
    {
        const fixProp *pFixPropSignal = (const fixProp *)ssGetDataTypeProperties(S, dataTypeId);
            
        if (FixPt_DataTypeIsFix(pFixPropSignal)) {
            if (pFixPropSignal->IsSigned) retValue = true;
        }
    }

    return(retValue);
}

boolean_T isOutputSignedFixedPoint(SimStruct *S, int_T port) {
    boolean_T     retValue   = false;
    const DTypeId dataTypeId = ssGetOutputPortDataType(S, port);
    const char   *dtName     = ssGetDataTypeName(S, dataTypeId);

    if ( isFixedPointDspDataType(dtName) )
    {
        const fixProp *pFixPropSignal = (const fixProp *)ssGetDataTypeProperties(S, dataTypeId);
            
        if (FixPt_DataTypeIsFix(pFixPropSignal)) {
            if (pFixPropSignal->IsSigned) retValue = true;
        }
    }

    return(retValue);
}

boolean_T isInputUnsignedFixedPoint(SimStruct *S, int_T port) {
    boolean_T     retValue   = false;
    const DTypeId dataTypeId = ssGetInputPortDataType(S, port);
    const char   *dtName     = ssGetDataTypeName(S, dataTypeId);

    if ( isFixedPointDspDataType(dtName) )
    {
        const fixProp *pFixPropSignal = (const fixProp *)ssGetDataTypeProperties(S, dataTypeId);
            
        if (FixPt_DataTypeIsFix(pFixPropSignal)) {
            if (!(pFixPropSignal->IsSigned)) retValue = true;
        }
    }

    return(retValue);
}

boolean_T isOutputUnsignedFixedPoint(SimStruct *S, int_T port) {
    boolean_T     retValue   = false;
    const DTypeId dataTypeId = ssGetOutputPortDataType(S, port);
    const char   *dtName     = ssGetDataTypeName(S, dataTypeId);

    if ( isFixedPointDspDataType(dtName) )
    {
        const fixProp *pFixPropSignal = (const fixProp *)ssGetDataTypeProperties(S, dataTypeId);
            
        if (FixPt_DataTypeIsFix(pFixPropSignal)) {
            if (!(pFixPropSignal->IsSigned)) retValue = true;
        }
    }

    return(retValue);
}

boolean_T isInputFixedPoint(SimStruct *S, int_T port) {
    return(isInputSignedFixedPoint(S, port) || isInputUnsignedFixedPoint(S, port));
}

boolean_T isOutputFixedPoint(SimStruct *S, int_T port) {
    return(isOutputSignedFixedPoint(S, port) || isOutputUnsignedFixedPoint(S, port));
}

/* [EOF] dsp_fixpt_sim.c */
