/*! \file Macro.h
*	\brief User Scol machine Macro conversion API
*/

/*! @defgroup group1 Macro conversion API
 *  User Scol machine Macro conversion API
 *  @{
 */

#ifndef _MACROS_H_
#define _MACROS_H_

/*! \brief Convert scol machine value to int
*   \param int : scol machine value
*   \return int : system int value
*/
#define MTOI(val) ((val)>>1)

/** \brief Convert scol machine value to system handle
* \param int : scol machine value
* \return int : system handle
*/
#define MTOP(val) ((val)>>1)

/*! \brief Convert int to scol machine value
*   \param int : system int value
*   \return int : scol machine value
*/
#define ITOM(val) ((val)<<1)

/*! \brief Convert system handle to scol machine value
*   \param int : system int value
*   \return int : scol machine value
*/
#define PTOM(val) (((val)<<1)+1)

/* SE conversions*/
#define SEW2I(w)             ((w)>>1)
#define SEW2P(w)             ((w)>>1)
#define SEI2W(n)             ((n)<<1)
#define SEP2W(p)             ((p)<<1 | _SEPTRBIT)

#ifndef SCOL_FLOAT_DEFINITION
/*! \brief Convert scol machine value to float
*   \param int : scol machine value
*   \return float : system float value
*/
#ifdef __cplusplus
_inline float MTOF(int val) {	return *(float*)&(val);	}   /* C++ */
#else
static inline float MTOF (int mot)
/*{	return *(float*)&(mot);	} */  /* C */
{ return (float) (mot); }
#endif

/*! \brief Convert float to scol machine value
*   \param float : system float value
*   \return int : scol machine value
*/
#ifdef __cplusplus
_inline int   FTOM(float val) {	return ((*(int*)&(val)) & 0xfffffffe);	}   /* C++ */
#else
extern inline int FTOM (float mot)
/*{	return ((*(int*)&(mot)) & 0xfffffffe);	}*/   /* C */
{ return ((int) (mot) & 0xfffffffe);	}
#endif

/* $ Iri : float support *//*
#define FSET(val, f)  {                 \
  float g = (f);                      \
  (val) = (*(int*)&g) & 0xfffffffe;     \
}
#define FGET(val)     (*(float*)&(val))*/
/* end */

#define SCOL_FLOAT_DEFINITION
#endif

/*! \brief Invert 2 stack positions in scol machine\n
*    need a int tmp_res local variable to work
*   \param Mmachine : the scol machine structure pointer
*	\param int : first position in stack
*   \param int : second position in stack
*/
#define INVERT(m, a, b) {tmp_res=MMget(m,a);MMset(m,a,MMget(m,b));MMset(m,b,tmp_res);}

/*! \brief Move the scol machine stack of n positions
*   \param Mmachine : the scol machine structure pointer
*	\param int : number of pull in stack
*/
#define SEDROP(m, n) ((m)->pp += (n))

/** @} */

#define STR_SIZE(size) (2+((size)>>2))
#define SIZE(t) ((sizeof(t)+3)>>2)
#define CHECK(m) if ((tmp_res=m)) return tmp_res

/** \brief libération
*/
#define SAFEfree (p)      { if (p) { free (p); (p) = NULL; } }

#endif /* _MACROS_H_ */

