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