Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Base - API [I].i_inc
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Common ============================================================================================================================================================ */

/* ---- F(tgCM_SWP) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID F(tgCM_SWP)( PCU_TYPE pA, PCU_TYPE pB )
{
    const TYPE uiTemp = *pA;
    *pA = *pB;
    *pB = uiTemp;
}


/* ---- F(tgCM_MAX) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_MAX)( const TYPE fA, const TYPE fB )
{
    return (fA >= fB ? fA : fB);
}


/* ---- F(tgCM_MIN) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_MIN)( const TYPE fA, const TYPE fB )
{
    return (fB >= fA ? fA : fB);
}


/* ---- F(tgCM_CLP) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_CLP)( const TYPE fA, const TYPE fLow, const TYPE fHigh )
{
    TgERROR(fLow <= fHigh);
    return (fA <= fLow ? fLow : (fA >= fHigh ? fHigh : fA));
}


/* ---- F(tgCM_CLP_MIN) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_CLP_MIN)( const TYPE fA, const TYPE fLow )
{
    return (fA <= fLow ? fLow : fA);
}


/* ---- F(tgCM_CLP_MAX) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_CLP_MAX)( const TYPE fA, const TYPE fHigh )
{
    return (fA >= fHigh ? fHigh : fA);
}


#if INT_SIGNED


/* ---- F(tgCM_SGN) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_SGN)( const TYPE fA )
{
    return ((fA > 0) ? 1 : ((fA < 0) ? -1 : 0));
}


/* ---- F(tgCM_FLR_ALGN_PW2) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_FLR_ALGN_PW2)( const TYPE fA, const TYPE fAlignment )
{
    TYPE                                fVal;

    fVal = F(tgPM_ABS)( fA ) & ~(fAlignment - 1 );
    TgERROR(fAlignment > 0 && TgTRUE == F(tgCM_IS_PW2)( fAlignment ));
    return (fA > 0 ? fVal : -fVal);
}


/* ---- F(tgCM_CEL_ALGN_PW2) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_CEL_ALGN_PW2)( const TYPE fA, const TYPE fAlignment )
{
    TYPE                                fVal;

    fVal = (F(tgPM_ABS)( fA ) + fAlignment - 1) & ~(fAlignment - 1 );
    TgERROR(fAlignment > 0 && TgTRUE == F(tgCM_IS_PW2)( fAlignment ));
    return (fA > 0 ? fVal : -fVal);
}


/* ---- F(tgCM_PRV_PW2) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_PRV_PW2)( const TYPE fA )
{
    TYPE                                fABS_A;
    TYPE                                fSCAN;
    TYPE                                fPW2;

    fABS_A = F(tgPM_ABS)( fA );
    fSCAN = (fA == 0) ? 0 : (TYPE)((UTYPE)1 << FU(tgPM_BSR)( (UTYPE)fABS_A ));
    fPW2 = TgTRUE == F(tgCM_IS_PW2)( fABS_A ) ? fABS_A : fSCAN;

    return (fA > 0 ? fPW2 : -fPW2);
}


/* ---- F(tgCM_NXT_PW2) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_NXT_PW2)( const TYPE fA )
{
    TYPE                                fABS_A;
    TYPE                                fSCAN;
    TYPE                                fPW2;

    fABS_A = F(tgPM_ABS)( fA );
    fSCAN = (TYPE)((fA == 0) ? 1 : (2 << FU(tgPM_BSR)( (UTYPE)fABS_A )));
    fPW2 = (TgTRUE == F(tgCM_IS_PW2)( fABS_A )) ? fABS_A : fSCAN;

    return (fA > 0 ? fPW2 : -fPW2);
}


/* ---- F(tgCM_IS_PW2) -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL F(tgCM_IS_PW2)( const TYPE fA )
{
    UTYPE                               fABS_A;

    fABS_A = (UTYPE)F(tgPM_ABS)( fA );
    return (fABS_A && ((fABS_A & (fABS_A - 1)) == 0) ? TgTRUE : TgFALSE);
}


/* ---- F(tgCM_RAND) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_RAND)( TgVOID )
{
    return ((TYPE)( (tgCM_RAND_MT_U32() & FU(KTgMAX)) - (FU(KTgMAX) >> 1)) );
}


#else /* UNSIGNED */


/* ---- F(tgCM_SGN) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_SGN)( const TYPE fA )
{
    return (fA > 0u ? 1u : 0u);
}


/* ---- F(tgCM_FLR_ALGN_PW2) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_FLR_ALGN_PW2)( const TYPE fA, const TYPE fAlignment )
{
    TgERROR(fAlignment > 0 && TgTRUE == F(tgCM_IS_PW2)( fAlignment ));
    return ((fA)& ~(fAlignment - 1u));
}


/* ---- F(tgCM_CEL_ALGN_PW2) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_CEL_ALGN_PW2)( const TYPE fA, const TYPE fAlignment )
{
    TgERROR(fAlignment > 0 && TgTRUE == F(tgCM_IS_PW2)( fAlignment ));
    return ((fA + fAlignment - 1u) & ~(fAlignment - 1u));
}


/* ---- F(tgCM_PRV_PW2) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_PRV_PW2)( const TYPE fA )
{
    return ((TgTRUE == F(tgCM_IS_PW2)( fA )) ? fA : (TYPE)((fA == 0) ? 0 : (1 << F(tgPM_BSR)( fA ))) );
}


/* ---- F(tgCM_NXT_PW2) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_NXT_PW2)( const TYPE fA )
{
    return ((TgTRUE == F(tgCM_IS_PW2)( fA )) ? fA : (TYPE)((fA == 0) ? 1 : (2 << F(tgPM_BSR)( fA ))) );
}


/* ---- F(tgCM_IS_PW2) -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL F(tgCM_IS_PW2)( const TYPE fA )
{
    return (fA && ((fA & (fA - 1u)) == 0u) ? TgTRUE : TgFALSE);
}


/* ---- F(tgCM_RAND) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE F(tgCM_RAND)( TgVOID )
{
    return (tgCM_RAND_MT_U32() & F(KTgMAX));
}


#endif  /* INT_SIGNED */