/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#ifndef _STDLIB_H
#define _STDLIB_H

#include <stddef.h>

/** @addtogroup stdlib */
/**	@{ */

/**
*	The largest random number returned by random_r().
*/

#define	RAND_MAX	0x7fffffff

/**
*	Structure holding the result for div()
*/

typedef struct {

	int		rem;					//!< Remainder
	int		quot;					//!< Quotient

} div_t;

/**
*	Structure holding the result for udiv()
*/

typedef struct {

	unsigned 	rem;				//!< Remainder
	unsigned	quot;				//!< Quotient

} udiv_t;

/**
*	Structure holding the result for lldiv()
*/

typedef struct {

	long long	rem;				//!< Remainder
	long long	quot;				//!< Quotient

} lldiv_t;

/**
*	Structure holding the result for ulldiv()
*/

typedef struct {

	unsigned long long	rem;		//!< Remainder
	unsigned long long	quot;		//!< Quotient

} ulldiv_t;

/**
*	Compute quotient and remainder of a signed integer (32-bit) division.
*
*	The function divides its \p numerator argument with its \p denominator
*	argument and returns a structure containing the quotient and the remainder.
*	The fields of the structure are called \p quot and \p rem, respectively.
*	The quotient is rounded towards zero and the result is such that the
*	following equation holds:
*
*	<tt>numerator = quotient * denominator + remainder</tt>
*
*	If the denominator is 0, then the behaviour is undefined.
*
*	@param	numerator	The number to divide
*	@param	denominator	The number to divide with
*	@return	A structure containing both the quotient and the remainder.
*
*	@attention
*		To use this function you <b>must</b> link the library that contains
*		the fast versions of the gcc support library functions (by default
*		this library is called gccs) because that contains the actual
*		division core.
*/

div_t div( int numerator, int denominator );

/**
*	Compute quotient and remainder of an unsigned integer (32-bit) division.
*
*	The function divides its \p numerator argument with its \p denominator
*	argument and returns a structure containing the quotient and the remainder.
*	The fields of the structure are called \p quot and \p rem, respectively.
*	The quotient is rounded towards zero and the result is such that the
*	following equation holds:
*
*	<tt>numerator = quotient * denominator + remainder</tt>
*
*	If the denominator is 0, then the behaviour is undefined.
*
*	@param	numerator	The number to divide
*	@param	denominator	The number to divide with
*	@return	A structure containing both the quotient and the remainder.
*
*	@attention
*		To use this function you <b>must</b> link the library that contains
*		the fast versions of the gcc support library functions (by default
*		this library is called gccs) because that contains the actual
*		division core.
*/

udiv_t udiv( unsigned numerator, unsigned denominator );

/**
*	Compute quotient and remainder of a signed long long (64-bit) division.
*
*	The function divides its \p numerator argument with its \p denominator
*	argument and returns a structure containing the quotient and the remainder.
*	The fields of the structure are called \p quot and \p rem, respectively.
*	The quotient is rounded towards zero and the result is such that the
*	following equation holds:
*
*	<tt>numerator = quotient * denominator + remainder</tt>
*
*	If the denominator is 0, then the behaviour is undefined.
*
*	@param	numerator	The number to divide
*	@param	denominator	The number to divide with
*	@return	A structure containing both the quotient and the remainder.
*
*	@attention
*		To use this function you <b>must</b> link the library that contains
*		the fast versions of the gcc support library functions (by default
*		this library is called gccs) because that contains the actual
*		division core.
*/

lldiv_t lldiv( long long numerator, long long denominator );

/**
*	Compute quotient and remainder of an unsigned long long (64-bit) division.
*
*	The function divides its \p numerator argument with its \p denominator
*	argument and returns a structure containing the quotient and the remainder.
*	The fields of the structure are called \p quot and \p rem, respectively.
*	The quotient is rounded towards zero and the result is such that the
*	following equation holds:
*
*	<tt>numerator = quotient * denominator + remainder</tt>
*
*	If the denominator is 0, then the behaviour is undefined.
*
*	@param	numerator	The number to divide
*	@param	denominator	The number to divide with
*	@return	A structure containing both the quotient and the remainder.
*
*	@attention
*		To use this function you <b>must</b> link the library that contains
*		the fast versions of the gcc support library functions (by default
*		this library is called gccs) because that contains the actual
*		division core.
*/

ulldiv_t ulldiv( unsigned long long numerator, unsigned long long denominator );

/** @} */

int	ultostr( char *buff, unsigned long numb, int radix, int uppc );
int	ulltostr( char *buff, unsigned long long numb, int radix, int uppc );

int	ultostr10( char *buff, unsigned long numb );
int	ulltostr10( char *buff, unsigned long long numb );

int	ultostr2n( char *buff, unsigned long numb, int base, int uppc );
int ulltostr2n( char *buff, unsigned long long numb, int base, int uppc );

unsigned long long	strtoull( const char *nptr, char **eptr, int base );
long long			strtoll( const char *nptr, char **eptr, int base );
unsigned long		strtoul( const char *nptr, char **eptr, int base );
long				strtol( const char *nptr, char **eptr, int base );
double 				strtod( const char *nptr, char **eptr );

unsigned long long	strtoulle( const char *nptr, char **eptr,
							   int base, void (*optr)(void) );
unsigned long		strtoule( const char *nptr, char **eptr,
							  int base, void (*optr)(void) );
long long			strtolle( const char *nptr, char **eptr,
							  int base, void (*optr)(void) );
long				strtole( const char *nptr, char **eptr,
							 int base, void (*optr)(void) );
double 				strtode( const char *nptr, char **eptr,
							 void (*optr)( void ) );

void	*bsearch( const void *key, const void *base, size_t nmemb, size_t size,
				  int (*comp)( const void *, const void * ) );

void	qsort( void *base, size_t nmemb, size_t size,
			   int (*comp)( const void *, const void * ) );

void	hsort( int numb, void *parm, int (*comp)( int, int, void *),
			   void (*copy)( int, int, void * ) );

int		*srandom_r( int seed, int *state, size_t size );
int		random_r( int *state );

#endif

