/******************************************************************************
 *
 * Copyright 2017, Lattice Semiconductor, Inc. All rights reserved.
 * No part of this work may be reproduced, modified, distributed, transmitted,
 * transcribed, or translated into any language or computer format, in any form
 * or by any means without written permission of Lattice Semiconductor Inc.,
 * 111 SW 5th Ave, Suite 700, Portland, OR 97204.
 *
 *****************************************************************************/
/**
 * @file sii_system_api.h
 *
 * @brief Generic system library interface
 *
 *****************************************************************************/
#ifndef SI_SYSTEM_API_H
#define SI_SYSTEM_API_H

/***** #include statements ***************************************************/

#include "sii_datatypes.h"

/***** public macro definitions **********************************************/

/* Construct to stringify defines */
#define SII_STRINGIFY(x)	#x
#define SII_DEF2STR(x)		SII_STRINGIFY(x)

/* #if 0 */
#ifdef SII_DEBUG
	#if (0 < SII_DEBUG)
		#define SII_MODULE_NAME_SET(name)	\
			static const char *sii_module_name_str = \
						SII_DEF2STR(name)
		#define SII_MODULE_NAME_GET()	sii_module_name_str
	#else
		#define SII_MODULE_NAME_SET(name)
		#define SII_MODULE_NAME_GET()	(NULL)
	#endif /* (0 < SII_DEBUG) */
#else
	#define SII_MODULE_NAME_SET(name)
	#define SII_MODULE_NAME_GET()	(NULL)
#endif /* SII_DEBUG */
/* #endif */

/* Bit manipulation macros */
#define SII_SET_BIT(p, bit)		(*(p) |= (1<<(bit)))
#define SII_CLR_BIT(p, bit)		(*(p) &= (~(1<<(bit))))
#define SII_PUT_BIT(p, bit, b)		(*(p) = (b) ? (*(p)|(1<<(bit))) : \
							(*(p)&(~(1<<(bit)))))
#define SII_MOD_BIT(p, msk, val)	(*(p) = ((*(p))&(~(msk))) | \
							((val)&(msk)))

#define SII_ABS_DIFF(A, B)		(((A) > (B)) ? ((A)-(B)) : ((B)-(A)))
#define SII_UINT_FRAC(num, den)		(((((num)<<1) / (den)) + 1) >> 1)
#define SII_INT_FRAC(num, den)		((0 > (((int)num)*((int)den))) ? \
					((((((int)num)*2)/((int)den))-1)/2) : \
					((((((int)num)*2)/((int)den))+1)/2))
#define SII_FLOAT2INT(flt)		((0.0 > flt) ? ((int_t)(flt-0.5)) : \
							((int_t)(flt+0.5)))

#endif /* SI_SYSTEM_API_H */
