//	utf-8

/*	Copyright  (c)	Günter Woigk   1995-2002
  					mailto:kio@little-bat.de

	This file is free software

 	This program is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

 	Permission to use, copy, modify, distribute, and sell this software and
 	its documentation for any purpose is hereby granted without fee, provided
 	that the above copyright notice appear in all copies and that both that
 	copyright notice and this permission notice appear in supporting
 	documentation, and that the name of the copyright holder not be used
 	in advertising or publicity pertaining to distribution of the software
 	without specific, written prior permission. The copyright holder makes no
 	representations about the suitability of this software for any purpose.
 	It is provided "as is" without express or implied warranty.

 	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 	PERFORMANCE OF THIS SOFTWARE.


	nice defines
	
	this file should be included from "kio.h"
*/


#ifndef _nice_defines_h_
#define _nice_defines_h_


#ifndef _standard_types_h_
typedef	unsigned long	ulong;
typedef	unsigned short	ushort;
typedef	unsigned int 	uint;
#endif


// ----	Nice defines -------------------------------------------------------

#ifndef MIN
	#define MIN(a,b)	((a)<(b)?(a):(b))
#endif
#ifndef MAX
	#define MAX(a,b)	((a)>(b)?(a):(b))
#endif
#ifndef ABS
	#define ABS(a)   	((a)<0?-(a):(a))
#endif

#define	LIMIT(A,N,E)	if (N<(A)) N=(A); else if (N<=(E)) {} else N=(E)
#define	SWAP(a,b)		((a)^=(b),(b)^=(a),(a)^=(b))			// (c) KIO !
#define	SORT(a,b)		if ((a)<=(b)) {} else SWAP(a,b)
#define	SIGN(a)			(((a)>0)-((a)<0))						// (c) KIO !
#define	NELEM(feld)		(sizeof(feld)/sizeof((feld)[0]))		// UNSIGNED !!!
#define	BIT(B,N)		(((N)>>(B))&1)
#define	RMASK(B)		(0xFFFFFFFFu>>(32-(B)))
#define	M4(a,b,c,d)		((ulong(a)<<24)+(ulong(b)<<16)+(ulong(c)<<8)+ulong(d))
#define	M3(b,c,d)		((ulong(b)<<16)+(ulong(c)<<8)+ulong(d))
#define	M2(c,d)			((ulong(c)<<8)+ulong(d))



// ----	Calculate base 2 logarithm or 'position' of leftmost '1' bit -----------------
//		note: for 0x00000000 the returned value is 0 as for 0x00000001
#define LOG2NIBBLE(N)	(((N)>7)+((N)>3)+((N)>1))											/* 0 ...  3 */
#define LOG2BYTE(N)		(uchar(N) >  0x0Fu ? 4 + LOG2NIBBLE( uchar(N)>>4 ) : LOG2NIBBLE(N))	/* 0 ...  7 */
#define LOG2SHORT(N)	(ushort(N)>0x00FFu ? 8 + LOG2BYTE  (ushort(N)>>8 ) : LOG2BYTE(N)  )	/* 0 ... 15 */
#define LOG2(N)			(ulong(N) >0xFFFFu ? 16+ LOG2SHORT ( ulong(N)>>16) : LOG2SHORT(N) )	/* 0 ... 31 */

#define	BITS(N)			LOG2((N)+1)
#define NIBBLES(N)		LOG2((N)/4+1)
#define BYTES(N)		LOG2((N)/8+1)


/* ----	scale conversion with proper rounding --------------------------
		limits:   oldval*newmax+oldmax/2 <= ulong_max
*/
inline ulong Scale ( ulong oldval, ulong oldmax, ulong newmax )	
	{ return ( oldval*newmax+oldmax/2 ) / oldmax; }



// ----	random number with range ----------------------------------------
#ifdef __cplusplus
inline ulong random ( ulong n )		// return random value [0 ... [n
	{ return ( n * ulong(ushort(random())) ) >> 16; }
#endif


// ----	Debugging ----------------------------------------------------------
#define	LOL				fprintf(stderr,"LOL> \"%s\" - %d\n",__FILE__,__LINE__);



#ifdef __cplusplus



/* ----	byte sex swappers for intel et.al. ---------------------------
		=> 'abcd' == *(ulong*)"abcd"
*/
#if defined(_BIG_ENDIAN)
	inline ushort _x_(ushort n) { return n; }
	inline ulong  _x_(ulong  n) { return n; }
#elif defined(_LITTLE_ENDIAN)
	inline ushort _x_(ushort n) { return (n>>8)+(n<<8); }
	inline ulong  _x_(ulong  n) { return _x_(ushort(n>>16)) + (ulong(_x_(ushort(n)))<<16); }
#elif defined(_PDP_ENDIAN)
//	#error	not yet implemented. it's your job right now!
#else
//	#error	this file should be included from "kio.h".
#endif


/*	2002-04-08 kio	disabled type casters:
	there's a problem with these type casters breaking _x_()
	probably _x_(int) get's shorts, e.g. 'AB' too.
	problem detected by Fredrick Meunier while porting zasm 
	to be explored... 
	inline short _x_(short n) { return _x_((ushort)n); }
	inline long  _x_(long  n) { return _x_((ulong)n);  }
	inline int   _x_(int   n) { return _x_((ulong)n);  }
	inline uint  _x_(uint  n) { return _x_((ulong)n);  }
*/


  template <class T> inline T    Min    ( T a, T b )		{ return a<b ? a : b; }
  template <class T> inline T    Max    ( T a, T b )		{ return a>b ? a : b; }
  template <class T> inline void Limit  ( T a, T&n, T e )	{ if (n<a) n=a; else if (n>e) n=e; }
  template <class T> inline T    MinMax ( T a, T n, T e )	{ return n<=a ? a : n>=e ? e : n;  }
  template <class T> inline void Swap   ( T& a, T& b )		{ T c=a; a=b; b=c;    }
  template <class T> inline void Sort   ( T& a, T& b )		{ if (a>b) Swap(a,b); }

  template <class T> inline T	 Sign   ( T a )				{ return SIGN(a); }
  template <class T> inline T	 Abs    ( T a )				{ return ABS(a); }


  inline int CountBits ( unsigned char z ) 
  { 
    z = ((z & 0xAAu) >> 1) + (z & 0x55u);
    z = ((z & 0xCCu) >> 2) + (z & 0x33u);
    z = ((z & 0xF0u) >> 4) + (z & 0x0Fu);
	return z;
  }

 inline int CountBits ( unsigned short z ) 
  {
    z = ((z & 0xAAAAu) >>  1) + (z & 0x5555u);
    z = ((z & 0xCCCCu) >>  2) + (z & 0x3333u);
    z = ((z & 0xF0F0u) >>  4) + (z & 0x0F0Fu);
    z = ((z & 0xFF00u) >>  8) + (z & 0x00FFu);
    return z;
  }

  inline int CountBits ( unsigned long z )
  {
    z = ((z & 0xAAAAAAAAu) >>  1) + (z & 0x55555555u);
    z = ((z & 0xCCCCCCCCu) >>  2) + (z & 0x33333333u);
    z = ((z & 0xF0F0F0F0u) >>  4) + (z & 0x0F0F0F0Fu);
    z = ((z & 0xFF00FF00u) >>  8) + (z & 0x00FF00FFu);
    z = ((z & 0xFFFF0000u) >> 16) + (z & 0x0000FFFFu);
    return z;
  }

  inline int CountBits ( int z )			{ return CountBits((unsigned long)z);  }
  inline int CountBits ( signed long z )	{ return CountBits((unsigned long)z);  }
  inline int CountBits ( signed short z )	{ return CountBits((unsigned short)z); }
  inline int CountBits ( signed char z )	{ return CountBits((unsigned char)z);  }



#endif	// __cplusplus


#endif



