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