[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 |
---|
[0dc954b] | 12 | // Last Modified On : Tue Oct 31 13:47:24 2017 |
---|
| 13 | // Update Count : 245 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[17e5e2b] | 17 | |
---|
[bd85400] | 18 | //--------------------------------------- |
---|
| 19 | |
---|
[45161b4d] | 20 | #ifndef EXIT_FAILURE |
---|
| 21 | #define EXIT_FAILURE 1 // failing exit status |
---|
| 22 | #define EXIT_SUCCESS 0 // successful exit status |
---|
| 23 | #endif // ! EXIT_FAILURE |
---|
| 24 | |
---|
| 25 | //--------------------------------------- |
---|
| 26 | |
---|
[f3fc631f] | 27 | // allocation, non-array types |
---|
| 28 | static inline forall( dtype T | sized(T) ) T * malloc( void ) { |
---|
[bfc0f40] | 29 | // printf( "* malloc\n" ); |
---|
[f3fc631f] | 30 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 31 | } // malloc |
---|
| 32 | |
---|
[bfc0f40] | 33 | // static inline forall( dtype T | sized(T) ) T & malloc( void ) { |
---|
| 34 | // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 35 | // printf( "& malloc %p\n", &p ); |
---|
| 36 | // return p; |
---|
| 37 | // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 38 | // } // malloc |
---|
| 39 | |
---|
[6065b3aa] | 40 | extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine |
---|
[f3fc631f] | 41 | static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) { |
---|
[6065b3aa] | 42 | //printf( "X2\n" ); |
---|
[f3fc631f] | 43 | return (T *)(void *)calloc( dim, sizeof(T) ); // C cmalloc |
---|
| 44 | } |
---|
| 45 | |
---|
[6065b3aa] | 46 | extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void * |
---|
[f3fc631f] | 47 | static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { |
---|
[6065b3aa] | 48 | //printf( "X3\n" ); |
---|
[f3fc631f] | 49 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
| 50 | } |
---|
[6065b3aa] | 51 | |
---|
| 52 | extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void * |
---|
| 53 | static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) { |
---|
| 54 | //printf( "X4\n" ); |
---|
| 55 | return (T *)memalign( align, sizeof(T) ); |
---|
| 56 | } // memalign |
---|
| 57 | |
---|
| 58 | static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) { |
---|
| 59 | //printf( "X5\n" ); |
---|
| 60 | return (T *)memalign( align, sizeof(T) ); |
---|
| 61 | } // aligned_alloc |
---|
| 62 | |
---|
| 63 | extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void * |
---|
| 64 | static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) { |
---|
| 65 | //printf( "X6\n" ); |
---|
| 66 | return posix_memalign( (void **)ptr, align, sizeof(T) ); |
---|
| 67 | } // posix_memalign |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void * |
---|
| 71 | |
---|
| 72 | static inline forall( dtype T | sized(T) ) T * alloc( void ) { |
---|
[f3fc631f] | 73 | //printf( "X7\n" ); |
---|
[6065b3aa] | 74 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 75 | } // alloc |
---|
| 76 | static inline forall( dtype T | sized(T) ) T * alloc( char fill ) { |
---|
[f3fc631f] | 77 | //printf( "X8\n" ); |
---|
[6065b3aa] | 78 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
[cdbfab0] | 79 | return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value |
---|
[6065b3aa] | 80 | } // alloc |
---|
[f3fc631f] | 81 | |
---|
[6065b3aa] | 82 | static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) { |
---|
[f3fc631f] | 83 | //printf( "X9\n" ); |
---|
[6065b3aa] | 84 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
| 85 | } // alloc |
---|
| 86 | static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) { |
---|
[f3fc631f] | 87 | //printf( "X10\n" ); |
---|
[6065b3aa] | 88 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
[cdbfab0] | 89 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
[6065b3aa] | 90 | } // alloc |
---|
[f3fc631f] | 91 | |
---|
[6065b3aa] | 92 | static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) { |
---|
[f3fc631f] | 93 | //printf( "X11\n" ); |
---|
[cdbfab0] | 94 | return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc |
---|
[6065b3aa] | 95 | } // alloc |
---|
| 96 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); |
---|
| 97 | |
---|
| 98 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) { |
---|
[f3fc631f] | 99 | //printf( "X13\n" ); |
---|
[6065b3aa] | 100 | return (T *)memalign( align, sizeof(T) ); |
---|
| 101 | } // align_alloc |
---|
| 102 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) { |
---|
[f3fc631f] | 103 | //printf( "X14\n" ); |
---|
[6065b3aa] | 104 | T * ptr = (T *)memalign( align, sizeof(T) ); |
---|
[cdbfab0] | 105 | return (T *)memset( ptr, (int)fill, sizeof(T) ); |
---|
[6065b3aa] | 106 | } // align_alloc |
---|
[f3fc631f] | 107 | |
---|
[6065b3aa] | 108 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) { |
---|
[f3fc631f] | 109 | //printf( "X15\n" ); |
---|
[6065b3aa] | 110 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
| 111 | } // align_alloc |
---|
| 112 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) { |
---|
[f3fc631f] | 113 | //printf( "X16\n" ); |
---|
[6065b3aa] | 114 | T * ptr = (T *)memalign( align, dim * sizeof(T) ); |
---|
[cdbfab0] | 115 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
[6065b3aa] | 116 | } // align_alloc |
---|
| 117 | |
---|
[f3fc631f] | 118 | |
---|
| 119 | // data, non-array types |
---|
| 120 | static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) { |
---|
| 121 | //printf( "X17\n" ); |
---|
[cdbfab0] | 122 | return (T *)memset( dest, c, sizeof(T) ); |
---|
[f3fc631f] | 123 | } // memset |
---|
| 124 | extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void * |
---|
| 125 | static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) { |
---|
| 126 | //printf( "X18\n" ); |
---|
[cdbfab0] | 127 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
[f3fc631f] | 128 | } // memcpy |
---|
| 129 | |
---|
| 130 | // data, array types |
---|
[6065b3aa] | 131 | static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) { |
---|
[f3fc631f] | 132 | //printf( "X19\n" ); |
---|
[cdbfab0] | 133 | return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset |
---|
[6065b3aa] | 134 | } // memset |
---|
| 135 | static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) { |
---|
[f3fc631f] | 136 | //printf( "X20\n" ); |
---|
[cdbfab0] | 137 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
[6065b3aa] | 138 | } // memcpy |
---|
[f3fc631f] | 139 | |
---|
[6065b3aa] | 140 | // allocation/deallocation and constructor/destructor, non-array types |
---|
[aca65621] | 141 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
[83a071f9] | 142 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
| 143 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
[627f585] | 144 | |
---|
[6065b3aa] | 145 | // allocation/deallocation and constructor/destructor, array types |
---|
[aca65621] | 146 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
| 147 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
| 148 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
[6065b3aa] | 149 | |
---|
[bd85400] | 150 | //--------------------------------------- |
---|
| 151 | |
---|
| 152 | int ato( const char * ptr ); |
---|
| 153 | unsigned int ato( const char * ptr ); |
---|
| 154 | long int ato( const char * ptr ); |
---|
| 155 | unsigned long int ato( const char * ptr ); |
---|
| 156 | long long int ato( const char * ptr ); |
---|
| 157 | unsigned long long int ato( const char * ptr ); |
---|
| 158 | float ato( const char * ptr ); |
---|
| 159 | double ato( const char * ptr ); |
---|
| 160 | long double ato( const char * ptr ); |
---|
| 161 | float _Complex ato( const char * ptr ); |
---|
| 162 | double _Complex ato( const char * ptr ); |
---|
| 163 | long double _Complex ato( const char * ptr ); |
---|
| 164 | |
---|
| 165 | int strto( const char * sptr, char ** eptr, int base ); |
---|
| 166 | unsigned int strto( const char * sptr, char ** eptr, int base ); |
---|
| 167 | long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 168 | unsigned long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 169 | long long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 170 | unsigned long long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 171 | float strto( const char * sptr, char ** eptr ); |
---|
| 172 | double strto( const char * sptr, char ** eptr ); |
---|
| 173 | long double strto( const char * sptr, char ** eptr ); |
---|
| 174 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
| 175 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 176 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 177 | |
---|
| 178 | //--------------------------------------- |
---|
| 179 | |
---|
[4040425] | 180 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[f3fc631f] | 181 | T * bsearch( T key, const T * arr, size_t dim ); |
---|
[f3ddc21] | 182 | |
---|
[707446a] | 183 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[f3fc631f] | 184 | unsigned int bsearch( T key, const T * arr, size_t dim ); |
---|
[bd85400] | 185 | |
---|
[f3ddc21] | 186 | |
---|
[4040425] | 187 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[f3fc631f] | 188 | void qsort( const T * arr, size_t dim ); |
---|
[bd85400] | 189 | |
---|
| 190 | //--------------------------------------- |
---|
| 191 | |
---|
[5ea26ed] | 192 | [ int, int ] div( int num, int denom ); |
---|
| 193 | [ long int, long int ] div( long int num, long int denom ); |
---|
| 194 | [ long long int, long long int ] div( long long int num, long long int denom ); |
---|
[4040425] | 195 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
[5ea26ed] | 196 | [ T, T ] div( T num, T demon ); |
---|
[bd85400] | 197 | |
---|
| 198 | //--------------------------------------- |
---|
| 199 | |
---|
[c3ebf37] | 200 | unsigned char abs( signed char ); |
---|
[6e991d6] | 201 | extern "C" { int abs( int ); } // use default C routine for int |
---|
[c3ebf37] | 202 | unsigned long int abs( long int ); |
---|
| 203 | unsigned long long int abs( long long int ); |
---|
[bd85400] | 204 | float abs( float ); |
---|
| 205 | double abs( double ); |
---|
| 206 | long double abs( long double ); |
---|
[6e991d6] | 207 | float abs( float _Complex ); |
---|
| 208 | double abs( double _Complex ); |
---|
| 209 | long double abs( long double _Complex ); |
---|
[aca65621] | 210 | forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } ) |
---|
[f3ddc21] | 211 | T abs( T ); |
---|
[53ba273] | 212 | |
---|
| 213 | //--------------------------------------- |
---|
| 214 | |
---|
[70e4895d] | 215 | void random_seed( long int s ); |
---|
| 216 | char random( void ); |
---|
| 217 | char random( char l, char u ); |
---|
| 218 | int random( void ); |
---|
| 219 | unsigned int random( void ); |
---|
| 220 | unsigned int random( unsigned int u ); |
---|
| 221 | unsigned int random( unsigned int l, unsigned int u ); |
---|
| 222 | //long int random( void ); |
---|
| 223 | unsigned long int random( void ); |
---|
| 224 | unsigned long int random( unsigned long int u ); |
---|
| 225 | unsigned long int random( unsigned long int l, unsigned long int u ); |
---|
| 226 | float random( void ); |
---|
| 227 | double random( void ); |
---|
| 228 | float _Complex random( void ); |
---|
| 229 | double _Complex random( void ); |
---|
| 230 | long double _Complex random( void ); |
---|
[bd85400] | 231 | |
---|
| 232 | //--------------------------------------- |
---|
| 233 | |
---|
[4040425] | 234 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[a797e2b1] | 235 | T min( T t1, T t2 ); |
---|
[bd85400] | 236 | |
---|
[4040425] | 237 | forall( otype T | { int ?>?( T, T ); } ) |
---|
[a797e2b1] | 238 | T max( T t1, T t2 ); |
---|
[bd85400] | 239 | |
---|
[a9f2c13] | 240 | forall( otype T | { T min( T, T ); T max( T, T ); } ) |
---|
[a797e2b1] | 241 | T clamp( T value, T min_val, T max_val ); |
---|
[a9f2c13] | 242 | |
---|
[4040425] | 243 | forall( otype T ) |
---|
[cb811ac] | 244 | void swap( T & t1, T & t2 ); |
---|
[bd85400] | 245 | |
---|
| 246 | // Local Variables: // |
---|
| 247 | // mode: c // |
---|
| 248 | // tab-width: 4 // |
---|
| 249 | // End: // |
---|