//
// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// stdlib --
//
// Author           : Peter A. Buhr
// Created On       : Thu Jan 28 17:12:35 2016
// Last Modified By : Peter A. Buhr
// Last Modified On : Fri Jul  7 09:34:49 2017
// Update Count     : 219
//

#pragma once

//---------------------------------------

extern "C" {
#ifndef EXIT_FAILURE
#define	EXIT_FAILURE	1								// failing exit status
#define	EXIT_SUCCESS	0								// successful exit status
#endif // ! EXIT_FAILURE
} // extern "C"

//---------------------------------------

// allocation, non-array types
static inline forall( dtype T | sized(T) ) T * malloc( void ) {
	//printf( "X1\n" );
	return (T *)(void *)malloc( (size_t)sizeof(T) );	// C malloc
} // malloc

extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
	//printf( "X2\n" );
	return (T *)(void *)calloc( dim, sizeof(T) );		// C cmalloc
}

extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
	//printf( "X3\n" );
	return (T *)(void *)realloc( (void *)ptr, size );
}

extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
	//printf( "X4\n" );
	return (T *)memalign( align, sizeof(T) );
} // memalign

static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
	//printf( "X5\n" );
	return (T *)memalign( align, sizeof(T) );
} // aligned_alloc

extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
	//printf( "X6\n" );
	return posix_memalign( (void **)ptr, align, sizeof(T) );
} // posix_memalign


extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *

static inline forall( dtype T | sized(T) ) T * alloc( void ) {
	//printf( "X7\n" );
	return (T *)(void *)malloc( (size_t)sizeof(T) );	// C malloc
} // alloc
static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
	//printf( "X8\n" );
	T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );	// C malloc
    return memset( ptr, (int)fill, sizeof(T) );			// initial with fill value
} // alloc

static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
	//printf( "X9\n" );
	return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
} // alloc
static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
	//printf( "X10\n" );
	T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
    return memset( ptr, (int)fill, dim * sizeof(T) );
} // alloc

static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
	//printf( "X11\n" );
	return (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
} // alloc
forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );

static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
	//printf( "X13\n" );
	return (T *)memalign( align, sizeof(T) );
} // align_alloc
static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
	//printf( "X14\n" );
    T * ptr = (T *)memalign( align, sizeof(T) );
    return memset( ptr, (int)fill, sizeof(T) );
} // align_alloc

static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
	//printf( "X15\n" );
	return (T *)memalign( align, dim * sizeof(T) );
} // align_alloc
static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
	//printf( "X16\n" );
    T * ptr = (T *)memalign( align, dim * sizeof(T) );
    return memset( ptr, (int)fill, dim * sizeof(T) );
} // align_alloc


// data, non-array types
static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
	//printf( "X17\n" );
	return memset( dest, c, sizeof(T) );
} // memset
extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
	//printf( "X18\n" );
	return memcpy( dest, src, sizeof(T) );
} // memcpy

// data, array types
static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
	//printf( "X19\n" );
	return (void *)memset( dest, c, dim * sizeof(T) );	// C memset
} // memset
static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
	//printf( "X20\n" );
	return (void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
} // memcpy

// allocation/deallocation and constructor/destructor, non-array types
forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );
forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );
forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) void delete( T * ptr, Params rest );

// allocation/deallocation and constructor/destructor, array types
forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );
forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );
forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );

//---------------------------------------

int ato( const char * ptr );
unsigned int ato( const char * ptr );
long int ato( const char * ptr );
unsigned long int ato( const char * ptr );
long long int ato( const char * ptr );
unsigned long long int ato( const char * ptr );
float ato( const char * ptr );
double ato( const char * ptr );
long double ato( const char * ptr );
float _Complex ato( const char * ptr );
double _Complex ato( const char * ptr );
long double _Complex ato( const char * ptr );

int strto( const char * sptr, char ** eptr, int base );
unsigned int strto( const char * sptr, char ** eptr, int base );
long int strto( const char * sptr, char ** eptr, int base );
unsigned long int strto( const char * sptr, char ** eptr, int base );
long long int strto( const char * sptr, char ** eptr, int base );
unsigned long long int strto( const char * sptr, char ** eptr, int base );
float strto( const char * sptr, char ** eptr );
double strto( const char * sptr, char ** eptr );
long double strto( const char * sptr, char ** eptr );
float _Complex strto( const char * sptr, char ** eptr );
double _Complex strto( const char * sptr, char ** eptr );
long double _Complex strto( const char * sptr, char ** eptr );

//---------------------------------------

forall( otype T | { int ?<?( T, T ); } )
T * bsearch( T key, const T * arr, size_t dim );

forall( otype T | { int ?<?( T, T ); } )
unsigned int bsearch( T key, const T * arr, size_t dim );


forall( otype T | { int ?<?( T, T ); } )
void qsort( const T * arr, size_t dim );

//---------------------------------------

forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
[ T, T ] div( T t1, T t2 );

//---------------------------------------

unsigned char abs( signed char );
extern "C" { int abs( int ); }							// use default C routine for int
unsigned long int abs( long int );
unsigned long long int abs( long long int );
float abs( float );
double abs( double );
long double abs( long double );
float abs( float _Complex );
double abs( double _Complex );
long double abs( long double _Complex );
forall( otype T | { void ?{}( T *, zero_t ); int ?<?( T, T ); T -?( T ); } )
T abs( T );

//---------------------------------------

void rand48seed( long int s );
char rand48( void );
int rand48( void );
unsigned int rand48( void );
long int rand48( void );
unsigned long int rand48( void );
float rand48( void );
double rand48( void );
float _Complex rand48( void );
double _Complex rand48( void );
long double _Complex rand48( void );

//---------------------------------------

forall( otype T | { int ?<?( T, T ); } )
T min( T t1, T t2 );

forall( otype T | { int ?>?( T, T ); } )
T max( T t1, T t2 );

forall( otype T | { T min( T, T ); T max( T, T ); } )
T clamp( T value, T min_val, T max_val );

forall( otype T )
void swap( T * t1, T * t2 );

// Local Variables: //
// mode: c //
// tab-width: 4 //
// End: //
