[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 |
---|
[ffaedcd] | 12 | // Last Modified On : Wed Apr 24 17:35:43 2019 |
---|
| 13 | // Update Count : 352 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[17e5e2b] | 17 | |
---|
[d46ed6e] | 18 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
[3ce0d440] | 19 | extern "C" { |
---|
[57fc7d8] | 20 | void * memalign( size_t align, size_t size ); // malloc.h |
---|
[b9c04946] | 21 | void * memset( void * dest, int fill, size_t size ); // string.h |
---|
[57fc7d8] | 22 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
[d46ed6e] | 23 | void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA |
---|
[3ce0d440] | 24 | } // extern "C" |
---|
[e672372] | 25 | |
---|
[bd85400] | 26 | //--------------------------------------- |
---|
| 27 | |
---|
[45161b4d] | 28 | #ifndef EXIT_FAILURE |
---|
| 29 | #define EXIT_FAILURE 1 // failing exit status |
---|
| 30 | #define EXIT_SUCCESS 0 // successful exit status |
---|
| 31 | #endif // ! EXIT_FAILURE |
---|
| 32 | |
---|
| 33 | //--------------------------------------- |
---|
| 34 | |
---|
[74b19fb] | 35 | static inline forall( dtype T | sized(T) ) { |
---|
[3ce0d440] | 36 | // C dynamic allocation |
---|
| 37 | |
---|
[74b19fb] | 38 | T * malloc( void ) { |
---|
| 39 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 40 | } // malloc |
---|
| 41 | |
---|
| 42 | T * calloc( size_t dim ) { |
---|
| 43 | return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc |
---|
| 44 | } // calloc |
---|
| 45 | |
---|
| 46 | T * realloc( T * ptr, size_t size ) { |
---|
| 47 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
| 48 | } // realloc |
---|
| 49 | |
---|
| 50 | T * memalign( size_t align ) { |
---|
| 51 | return (T *)memalign( align, sizeof(T) ); |
---|
| 52 | } // memalign |
---|
| 53 | |
---|
| 54 | T * aligned_alloc( size_t align ) { |
---|
| 55 | return (T *)aligned_alloc( align, sizeof(T) ); |
---|
| 56 | } // aligned_alloc |
---|
| 57 | |
---|
| 58 | int posix_memalign( T ** ptr, size_t align ) { |
---|
| 59 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
| 60 | } // posix_memalign |
---|
| 61 | |
---|
[3ce0d440] | 62 | |
---|
| 63 | // Cforall dynamic allocation |
---|
[74b19fb] | 64 | |
---|
| 65 | T * alloc( void ) { |
---|
| 66 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 67 | } // alloc |
---|
| 68 | |
---|
| 69 | T * alloc( char fill ) { |
---|
| 70 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
[ffaedcd] | 71 | return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value |
---|
[74b19fb] | 72 | } // alloc |
---|
| 73 | |
---|
| 74 | T * alloc( size_t dim ) { |
---|
| 75 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
| 76 | } // alloc |
---|
| 77 | |
---|
| 78 | T * alloc( size_t dim, char fill ) { |
---|
[ffaedcd] | 79 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C calloc |
---|
| 80 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initialize with fill value |
---|
[74b19fb] | 81 | } // alloc |
---|
| 82 | |
---|
| 83 | T * alloc( T ptr[], size_t dim ) { |
---|
| 84 | return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc |
---|
| 85 | } // alloc |
---|
| 86 | } // distribution |
---|
[6065b3aa] | 87 | |
---|
[f3fc631f] | 88 | |
---|
[6065b3aa] | 89 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); |
---|
| 90 | |
---|
[3ce0d440] | 91 | |
---|
| 92 | static inline forall( dtype T | sized(T) ) { |
---|
| 93 | T * align_alloc( size_t align ) { |
---|
| 94 | return (T *)memalign( align, sizeof(T) ); |
---|
| 95 | } // align_alloc |
---|
| 96 | |
---|
| 97 | T * align_alloc( size_t align, char fill ) { |
---|
| 98 | T * ptr = (T *)memalign( align, sizeof(T) ); |
---|
| 99 | return (T *)memset( ptr, (int)fill, sizeof(T) ); |
---|
| 100 | } // align_alloc |
---|
| 101 | |
---|
| 102 | T * align_alloc( size_t align, size_t dim ) { |
---|
| 103 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
| 104 | } // align_alloc |
---|
| 105 | |
---|
| 106 | T * align_alloc( size_t align, size_t dim, char fill ) { |
---|
[d46ed6e] | 107 | T * ptr; |
---|
| 108 | if ( fill == '\0' ) { |
---|
| 109 | ptr = (T *)cmemalign( align, dim, sizeof(T) ); |
---|
| 110 | } else { |
---|
| 111 | ptr = (T *)memalign( align, dim * sizeof(T) ); |
---|
| 112 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
| 113 | } // if |
---|
| 114 | return ptr; |
---|
[3ce0d440] | 115 | } // align_alloc |
---|
| 116 | } // distribution |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | static inline forall( dtype T | sized(T) ) { |
---|
| 120 | // data, non-array types |
---|
| 121 | |
---|
[b9c04946] | 122 | T * memset( T * dest, char fill ) { |
---|
| 123 | return (T *)memset( dest, fill, sizeof(T) ); |
---|
[3ce0d440] | 124 | } // memset |
---|
| 125 | |
---|
| 126 | T * memcpy( T * dest, const T * src ) { |
---|
| 127 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
| 128 | } // memcpy |
---|
| 129 | } // distribution |
---|
| 130 | |
---|
| 131 | static inline forall( dtype T | sized(T) ) { |
---|
| 132 | // data, array types |
---|
| 133 | |
---|
[b9c04946] | 134 | T * amemset( T dest[], char fill, size_t dim ) { |
---|
| 135 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
| 136 | } // amemset |
---|
[3ce0d440] | 137 | |
---|
[b9c04946] | 138 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
[3ce0d440] | 139 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
[b9c04946] | 140 | } // amemcpy |
---|
[3ce0d440] | 141 | } // distribution |
---|
[f3fc631f] | 142 | |
---|
[6065b3aa] | 143 | // allocation/deallocation and constructor/destructor, non-array types |
---|
[aca65621] | 144 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
[83a071f9] | 145 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
| 146 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
[627f585] | 147 | |
---|
[6065b3aa] | 148 | // allocation/deallocation and constructor/destructor, array types |
---|
[aca65621] | 149 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
| 150 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
| 151 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
[6065b3aa] | 152 | |
---|
[bd85400] | 153 | //--------------------------------------- |
---|
| 154 | |
---|
[57fc7d8] | 155 | static inline { |
---|
| 156 | int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
| 157 | unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
| 158 | long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
| 159 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
| 160 | long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
| 161 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
| 162 | |
---|
| 163 | float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } |
---|
| 164 | double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } |
---|
| 165 | long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } |
---|
| 166 | } // distribution |
---|
[e672372] | 167 | |
---|
[bd85400] | 168 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
| 169 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 170 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 171 | |
---|
[57fc7d8] | 172 | static inline { |
---|
[5ea5b28] | 173 | int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); } |
---|
[57fc7d8] | 174 | unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } |
---|
| 175 | long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } |
---|
| 176 | unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } |
---|
| 177 | long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } |
---|
| 178 | unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } |
---|
| 179 | |
---|
| 180 | float ato( const char * sptr ) { return strtof( sptr, 0 ); } |
---|
| 181 | double ato( const char * sptr ) { return strtod( sptr, 0 ); } |
---|
| 182 | long double ato( const char * sptr ) { return strtold( sptr, 0 ); } |
---|
| 183 | |
---|
| 184 | float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
| 185 | double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
| 186 | long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
| 187 | } // distribution |
---|
[e672372] | 188 | |
---|
[bd85400] | 189 | //--------------------------------------- |
---|
| 190 | |
---|
[3ce0d440] | 191 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 192 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
| 193 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
| 194 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
| 195 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
| 196 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
| 197 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
| 198 | } // distribution |
---|
[9c47a47] | 199 | |
---|
[3ce0d440] | 200 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
| 201 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
| 202 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
| 203 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
| 204 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
| 205 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
| 206 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
| 207 | } // distribution |
---|
[bd85400] | 208 | |
---|
[b9c04946] | 209 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 210 | void qsort( E * vals, size_t dim ); |
---|
| 211 | } // distribution |
---|
| 212 | |
---|
[bd85400] | 213 | //--------------------------------------- |
---|
| 214 | |
---|
[bbe1a87] | 215 | extern "C" { // override C version |
---|
| 216 | void srandom( unsigned int seed ); |
---|
| 217 | long int random( void ); |
---|
| 218 | } // extern "C" |
---|
| 219 | |
---|
| 220 | static inline { |
---|
| 221 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
| 222 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
| 223 | unsigned long int random( void ) { return lrand48(); } |
---|
| 224 | 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) |
---|
| 225 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
| 226 | |
---|
| 227 | char random( void ) { return (unsigned long int)random(); } |
---|
| 228 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
| 229 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 230 | int random( void ) { return (long int)random(); } |
---|
| 231 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
| 232 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
| 233 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
| 234 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
| 235 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 236 | } // distribution |
---|
| 237 | |
---|
| 238 | float random( void ); // [0.0, 1.0) |
---|
| 239 | double random( void ); // [0.0, 1.0) |
---|
| 240 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 241 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 242 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
[bd85400] | 243 | |
---|
| 244 | //--------------------------------------- |
---|
| 245 | |
---|
[58b6d1b] | 246 | #include "common.hfa" |
---|
[bd85400] | 247 | |
---|
| 248 | // Local Variables: // |
---|
| 249 | // mode: c // |
---|
| 250 | // tab-width: 4 // |
---|
| 251 | // End: // |
---|