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