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