[bd85400] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[bb82c03] | 7 | // stdlib --
|
---|
[bd85400] | 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Thu Jan 28 17:12:35 2016
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[54aba8d] | 12 | // Last Modified On : Tue Jan 2 12:21:04 2018
|
---|
| 13 | // Update Count : 292
|
---|
[bd85400] | 14 | //
|
---|
| 15 |
|
---|
[53a6c2a] | 16 | #pragma once
|
---|
[17e5e2b] | 17 |
|
---|
[e672372] | 18 | //#define _XOPEN_SOURCE 600 // posix_memalign, *rand48
|
---|
| 19 | #include <stdlib.h> // strto*, *abs
|
---|
| 20 |
|
---|
[bd85400] | 21 | //---------------------------------------
|
---|
| 22 |
|
---|
[45161b4d] | 23 | #ifndef EXIT_FAILURE
|
---|
| 24 | #define EXIT_FAILURE 1 // failing exit status
|
---|
| 25 | #define EXIT_SUCCESS 0 // successful exit status
|
---|
| 26 | #endif // ! EXIT_FAILURE
|
---|
| 27 |
|
---|
| 28 | //---------------------------------------
|
---|
| 29 |
|
---|
[f3fc631f] | 30 | // allocation, non-array types
|
---|
| 31 | static inline forall( dtype T | sized(T) ) T * malloc( void ) {
|
---|
[bfc0f40] | 32 | // printf( "* malloc\n" );
|
---|
[f3fc631f] | 33 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 34 | } // malloc
|
---|
| 35 |
|
---|
[bfc0f40] | 36 | // static inline forall( dtype T | sized(T) ) T & malloc( void ) {
|
---|
| 37 | // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 38 | // printf( "& malloc %p\n", &p );
|
---|
| 39 | // return p;
|
---|
| 40 | // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 41 | // } // malloc
|
---|
| 42 |
|
---|
[6065b3aa] | 43 | extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
|
---|
[f3fc631f] | 44 | static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
|
---|
[6065b3aa] | 45 | //printf( "X2\n" );
|
---|
[f3fc631f] | 46 | return (T *)(void *)calloc( dim, sizeof(T) ); // C cmalloc
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[6065b3aa] | 49 | extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
|
---|
[f3fc631f] | 50 | static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
|
---|
[6065b3aa] | 51 | //printf( "X3\n" );
|
---|
[f3fc631f] | 52 | return (T *)(void *)realloc( (void *)ptr, size );
|
---|
| 53 | }
|
---|
[6065b3aa] | 54 |
|
---|
| 55 | extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
|
---|
| 56 | static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
|
---|
| 57 | //printf( "X4\n" );
|
---|
| 58 | return (T *)memalign( align, sizeof(T) );
|
---|
| 59 | } // memalign
|
---|
| 60 |
|
---|
| 61 | static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
|
---|
| 62 | //printf( "X5\n" );
|
---|
| 63 | return (T *)memalign( align, sizeof(T) );
|
---|
| 64 | } // aligned_alloc
|
---|
| 65 |
|
---|
| 66 | extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
|
---|
| 67 | static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
|
---|
| 68 | //printf( "X6\n" );
|
---|
| 69 | return posix_memalign( (void **)ptr, align, sizeof(T) );
|
---|
| 70 | } // posix_memalign
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
|
---|
| 74 |
|
---|
| 75 | static inline forall( dtype T | sized(T) ) T * alloc( void ) {
|
---|
[f3fc631f] | 76 | //printf( "X7\n" );
|
---|
[6065b3aa] | 77 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 78 | } // alloc
|
---|
| 79 | static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
|
---|
[f3fc631f] | 80 | //printf( "X8\n" );
|
---|
[6065b3aa] | 81 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
[e672372] | 82 | return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value
|
---|
[6065b3aa] | 83 | } // alloc
|
---|
[f3fc631f] | 84 |
|
---|
[6065b3aa] | 85 | static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
|
---|
[f3fc631f] | 86 | //printf( "X9\n" );
|
---|
[6065b3aa] | 87 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
|
---|
| 88 | } // alloc
|
---|
| 89 | static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
|
---|
[f3fc631f] | 90 | //printf( "X10\n" );
|
---|
[6065b3aa] | 91 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
|
---|
[cdbfab0] | 92 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
|
---|
[6065b3aa] | 93 | } // alloc
|
---|
[f3fc631f] | 94 |
|
---|
[6065b3aa] | 95 | static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
|
---|
[f3fc631f] | 96 | //printf( "X11\n" );
|
---|
[cdbfab0] | 97 | return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
|
---|
[6065b3aa] | 98 | } // alloc
|
---|
| 99 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
|
---|
| 100 |
|
---|
| 101 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
|
---|
[f3fc631f] | 102 | //printf( "X13\n" );
|
---|
[6065b3aa] | 103 | return (T *)memalign( align, sizeof(T) );
|
---|
| 104 | } // align_alloc
|
---|
| 105 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
|
---|
[f3fc631f] | 106 | //printf( "X14\n" );
|
---|
[6065b3aa] | 107 | T * ptr = (T *)memalign( align, sizeof(T) );
|
---|
[cdbfab0] | 108 | return (T *)memset( ptr, (int)fill, sizeof(T) );
|
---|
[6065b3aa] | 109 | } // align_alloc
|
---|
[f3fc631f] | 110 |
|
---|
[6065b3aa] | 111 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
|
---|
[f3fc631f] | 112 | //printf( "X15\n" );
|
---|
[6065b3aa] | 113 | return (T *)memalign( align, dim * sizeof(T) );
|
---|
| 114 | } // align_alloc
|
---|
| 115 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
|
---|
[f3fc631f] | 116 | //printf( "X16\n" );
|
---|
[6065b3aa] | 117 | T * ptr = (T *)memalign( align, dim * sizeof(T) );
|
---|
[cdbfab0] | 118 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
|
---|
[6065b3aa] | 119 | } // align_alloc
|
---|
| 120 |
|
---|
[f3fc631f] | 121 |
|
---|
| 122 | // data, non-array types
|
---|
| 123 | static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
|
---|
| 124 | //printf( "X17\n" );
|
---|
[cdbfab0] | 125 | return (T *)memset( dest, c, sizeof(T) );
|
---|
[f3fc631f] | 126 | } // memset
|
---|
| 127 | extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
|
---|
| 128 | static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
|
---|
| 129 | //printf( "X18\n" );
|
---|
[cdbfab0] | 130 | return (T *)memcpy( dest, src, sizeof(T) );
|
---|
[f3fc631f] | 131 | } // memcpy
|
---|
| 132 |
|
---|
| 133 | // data, array types
|
---|
[6065b3aa] | 134 | static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
|
---|
[f3fc631f] | 135 | //printf( "X19\n" );
|
---|
[cdbfab0] | 136 | return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
|
---|
[6065b3aa] | 137 | } // memset
|
---|
| 138 | static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
|
---|
[f3fc631f] | 139 | //printf( "X20\n" );
|
---|
[cdbfab0] | 140 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
|
---|
[6065b3aa] | 141 | } // memcpy
|
---|
[f3fc631f] | 142 |
|
---|
[6065b3aa] | 143 | // allocation/deallocation and constructor/destructor, non-array types
|
---|
[aca65621] | 144 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
|
---|
[83a071f9] | 145 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
|
---|
| 146 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
|
---|
[627f585] | 147 |
|
---|
[6065b3aa] | 148 | // allocation/deallocation and constructor/destructor, array types
|
---|
[aca65621] | 149 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
|
---|
| 150 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
|
---|
| 151 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
|
---|
[6065b3aa] | 152 |
|
---|
[bd85400] | 153 | //---------------------------------------
|
---|
| 154 |
|
---|
[e672372] | 155 | static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
|
---|
| 156 | static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
|
---|
| 157 | static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
|
---|
| 158 | static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
|
---|
| 159 | static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
|
---|
| 160 | static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
|
---|
| 161 |
|
---|
| 162 | static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
|
---|
| 163 | static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
|
---|
| 164 | static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
|
---|
| 165 |
|
---|
[bd85400] | 166 | float _Complex strto( const char * sptr, char ** eptr );
|
---|
| 167 | double _Complex strto( const char * sptr, char ** eptr );
|
---|
| 168 | long double _Complex strto( const char * sptr, char ** eptr );
|
---|
| 169 |
|
---|
[e672372] | 170 | static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); }
|
---|
| 171 | static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
|
---|
| 172 | static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
|
---|
| 173 | static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
|
---|
| 174 | static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
|
---|
| 175 | static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
|
---|
| 176 |
|
---|
| 177 | static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); }
|
---|
| 178 | static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); }
|
---|
| 179 | static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
|
---|
| 180 |
|
---|
| 181 | static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
|
---|
| 182 | static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
|
---|
| 183 | static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
|
---|
| 184 |
|
---|
[bd85400] | 185 | //---------------------------------------
|
---|
| 186 |
|
---|
[9c47a47] | 187 | forall( otype E | { int ?<?( E, E ); } )
|
---|
[93cdd5c] | 188 | E * bsearch( E key, const E * vals, size_t dim );
|
---|
[f3ddc21] | 189 |
|
---|
[9c47a47] | 190 | forall( otype E | { int ?<?( E, E ); } )
|
---|
[93cdd5c] | 191 | size_t bsearch( E key, const E * vals, size_t dim );
|
---|
[bd85400] | 192 |
|
---|
[93cdd5c] | 193 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
---|
| 194 | E * bsearch( K key, const E * vals, size_t dim );
|
---|
| 195 |
|
---|
| 196 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
---|
| 197 | size_t bsearch( K key, const E * vals, size_t dim );
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | forall( otype E | { int ?<?( E, E ); } )
|
---|
| 201 | E * bsearchl( E key, const E * vals, size_t dim );
|
---|
| 202 |
|
---|
| 203 | forall( otype E | { int ?<?( E, E ); } )
|
---|
| 204 | size_t bsearchl( E key, const E * vals, size_t dim );
|
---|
| 205 |
|
---|
| 206 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
---|
| 207 | E * bsearchl( K key, const E * vals, size_t dim );
|
---|
| 208 |
|
---|
| 209 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
---|
| 210 | size_t bsearchl( K key, const E * vals, size_t dim );
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | forall( otype E | { int ?<?( E, E ); } )
|
---|
| 214 | E * bsearchu( E key, const E * vals, size_t dim );
|
---|
| 215 |
|
---|
| 216 | forall( otype E | { int ?<?( E, E ); } )
|
---|
| 217 | size_t bsearchu( E key, const E * vals, size_t dim );
|
---|
| 218 |
|
---|
| 219 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
---|
| 220 | E * bsearchu( K key, const E * vals, size_t dim );
|
---|
| 221 |
|
---|
| 222 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
---|
| 223 | size_t bsearchu( K key, const E * vals, size_t dim );
|
---|
[f3ddc21] | 224 |
|
---|
[9c47a47] | 225 |
|
---|
| 226 | forall( otype E | { int ?<?( E, E ); } )
|
---|
[93cdd5c] | 227 | void qsort( E * vals, size_t dim );
|
---|
[bd85400] | 228 |
|
---|
| 229 | //---------------------------------------
|
---|
| 230 |
|
---|
[5ea26ed] | 231 | [ int, int ] div( int num, int denom );
|
---|
| 232 | [ long int, long int ] div( long int num, long int denom );
|
---|
| 233 | [ long long int, long long int ] div( long long int num, long long int denom );
|
---|
[4040425] | 234 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
---|
[5ea26ed] | 235 | [ T, T ] div( T num, T demon );
|
---|
[bd85400] | 236 |
|
---|
| 237 | //---------------------------------------
|
---|
| 238 |
|
---|
[e672372] | 239 | static inline unsigned char abs( signed char v ) { return abs( (int)v ); }
|
---|
[6e991d6] | 240 | extern "C" { int abs( int ); } // use default C routine for int
|
---|
[e672372] | 241 | static inline unsigned long int abs( long int v ) { return labs( v ); }
|
---|
| 242 | static inline unsigned long long int abs( long long int v ) { return llabs( v ); }
|
---|
| 243 |
|
---|
| 244 | extern "C" {
|
---|
| 245 | double fabs( double );
|
---|
| 246 | float fabsf( float );
|
---|
| 247 | long double fabsl( long double );
|
---|
| 248 | } // extern "C"
|
---|
| 249 | static inline float abs( float x ) { return fabsf( x ); }
|
---|
| 250 | static inline double abs( double x ) { return fabs( x ); }
|
---|
| 251 | static inline long double abs( long double x ) { return fabsl( x ); }
|
---|
| 252 |
|
---|
| 253 | extern "C" {
|
---|
| 254 | double cabs( double _Complex );
|
---|
| 255 | float cabsf( float _Complex );
|
---|
| 256 | long double cabsl( long double _Complex );
|
---|
| 257 | } // extern "C"
|
---|
| 258 | static inline float abs( float _Complex x ) { return cabsf( x ); }
|
---|
| 259 | static inline double abs( double _Complex x ) { return cabs( x ); }
|
---|
| 260 | static inline long double abs( long double _Complex x ) { return cabsl( x ); }
|
---|
| 261 |
|
---|
[aca65621] | 262 | forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
|
---|
[f3ddc21] | 263 | T abs( T );
|
---|
[53ba273] | 264 |
|
---|
| 265 | //---------------------------------------
|
---|
| 266 |
|
---|
[54aba8d] | 267 | extern "C" { void srandom( unsigned int seed ); } // override C version
|
---|
[70e4895d] | 268 | char random( void );
|
---|
[e672372] | 269 | char random( char u );
|
---|
[70e4895d] | 270 | char random( char l, char u );
|
---|
| 271 | int random( void );
|
---|
[e672372] | 272 | int random( int u );
|
---|
| 273 | int random( int l, int u );
|
---|
[70e4895d] | 274 | unsigned int random( void );
|
---|
| 275 | unsigned int random( unsigned int u );
|
---|
| 276 | unsigned int random( unsigned int l, unsigned int u );
|
---|
[54aba8d] | 277 | extern "C" { long int random( void ); } // override C version
|
---|
[e672372] | 278 | long int random( long int u );
|
---|
| 279 | long int random( long int l, long int u );
|
---|
[70e4895d] | 280 | unsigned long int random( void );
|
---|
| 281 | unsigned long int random( unsigned long int u );
|
---|
| 282 | unsigned long int random( unsigned long int l, unsigned long int u );
|
---|
| 283 | float random( void );
|
---|
| 284 | double random( void );
|
---|
| 285 | float _Complex random( void );
|
---|
| 286 | double _Complex random( void );
|
---|
| 287 | long double _Complex random( void );
|
---|
[bd85400] | 288 |
|
---|
| 289 | //---------------------------------------
|
---|
| 290 |
|
---|
[4040425] | 291 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[e672372] | 292 | static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }
|
---|
[bd85400] | 293 |
|
---|
[4040425] | 294 | forall( otype T | { int ?>?( T, T ); } )
|
---|
[e672372] | 295 | static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }
|
---|
[bd85400] | 296 |
|
---|
[a9f2c13] | 297 | forall( otype T | { T min( T, T ); T max( T, T ); } )
|
---|
[e672372] | 298 | static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }
|
---|
[a9f2c13] | 299 |
|
---|
[4040425] | 300 | forall( otype T )
|
---|
[e672372] | 301 | static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }
|
---|
[bd85400] | 302 |
|
---|
| 303 | // Local Variables: //
|
---|
| 304 | // mode: c //
|
---|
| 305 | // tab-width: 4 //
|
---|
| 306 | // End: //
|
---|