[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 | // |
---|
| 7 | // algorithm.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Jan 28 17:10:29 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[52f85e0] | 12 | // Last Modified On : Wed Feb 10 15:45:56 2016 |
---|
| 13 | // Update Count : 140 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "stdlib" |
---|
| 17 | |
---|
| 18 | //--------------------------------------- |
---|
| 19 | |
---|
| 20 | extern "C" { |
---|
| 21 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 |
---|
| 22 | #include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch |
---|
| 23 | #include <string.h> // memset |
---|
| 24 | #include <malloc.h> // malloc_usable_size |
---|
| 25 | #include <math.h> // fabsf, fabs, fabsl |
---|
| 26 | #include <complex.h> // _Complex_I, cabsf, cabs, cabsl |
---|
| 27 | } // extern "C" |
---|
| 28 | |
---|
| 29 | forall( type T ) T * memset( T * ptr, unsigned char fill ) { // use default value '\0' for fill |
---|
| 30 | printf( "memset1\n" ); |
---|
| 31 | return (T *)memset( ptr, (int)fill, malloc_usable_size( ptr ) ); |
---|
| 32 | } // memset |
---|
| 33 | forall( type T ) T * memset( T * ptr ) { // remove when default value available |
---|
| 34 | printf( "memset2\n" ); |
---|
| 35 | return (T *)memset( ptr, 0, malloc_usable_size( ptr ) ); |
---|
| 36 | } // memset |
---|
| 37 | |
---|
| 38 | forall( type T ) T * malloc( void ) { |
---|
| 39 | printf( "malloc1\n" ); |
---|
| 40 | return (T *)malloc( sizeof(T) ); |
---|
| 41 | } // malloc |
---|
| 42 | forall( type T ) T * malloc( size_t size ) { |
---|
| 43 | printf( "malloc2\n" ); |
---|
| 44 | return (T *)(void *)malloc( size ); |
---|
| 45 | } // malloc |
---|
| 46 | forall( type T ) T * malloc( char fill ) { |
---|
| 47 | printf( "malloc3\n" ); |
---|
| 48 | T * ptr = (T *)malloc( sizeof(T) ); |
---|
| 49 | return memset( ptr ); |
---|
| 50 | } // malloc |
---|
| 51 | |
---|
| 52 | forall( type T ) T * calloc( size_t size ) { |
---|
| 53 | printf( "calloc\n" ); |
---|
| 54 | return (T *)calloc( size, sizeof(T) ); |
---|
| 55 | } // calloc |
---|
| 56 | |
---|
| 57 | forall( type T ) T * realloc( T * ptr, size_t size ) { |
---|
| 58 | printf( "realloc1\n" ); |
---|
| 59 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
| 60 | } // realloc |
---|
| 61 | forall( type T ) T * realloc( T * ptr, size_t size, unsigned char fill ) { |
---|
| 62 | printf( "realloc2\n" ); |
---|
| 63 | char * nptr = (T *)(void *)realloc( (void *)ptr, size ); |
---|
| 64 | size_t unused = malloc_usable_size( nptr ); |
---|
| 65 | memset( nptr + size - unused, (int)fill, unused ); // initialize any new storage |
---|
| 66 | return nptr; |
---|
| 67 | } // realloc |
---|
| 68 | |
---|
| 69 | forall( type T ) T * malloc( T * ptr, size_t size ) { |
---|
| 70 | printf( "malloc4\n" ); |
---|
| 71 | return (T *)realloc( ptr, size ); |
---|
| 72 | } // malloc |
---|
| 73 | forall( type T ) T * malloc( T * ptr, size_t size, unsigned char fill ) { |
---|
| 74 | printf( "malloc5\n" ); |
---|
| 75 | return (T *)realloc( ptr, size, fill ); |
---|
| 76 | } // malloc |
---|
| 77 | |
---|
| 78 | forall( type T ) T * aligned_alloc( size_t alignment ) { |
---|
| 79 | printf( "aligned_alloc\n" ); |
---|
| 80 | return (T *)memalign( alignment, sizeof(T) ); |
---|
| 81 | } // aligned_alloc |
---|
| 82 | |
---|
| 83 | forall( type T ) T * memalign( size_t alignment ) { |
---|
| 84 | printf( "memalign\n" ); |
---|
| 85 | return (T *)memalign( alignment, sizeof(T) ); |
---|
| 86 | } // memalign |
---|
| 87 | |
---|
| 88 | forall( type T ) int posix_memalign( T ** ptr, size_t alignment ) { |
---|
| 89 | printf( "posix_memalign\n" ); |
---|
| 90 | return posix_memalign( (void **)ptr, alignment, sizeof(T) ); |
---|
| 91 | } // posix_memalign |
---|
| 92 | |
---|
| 93 | //--------------------------------------- |
---|
| 94 | |
---|
| 95 | int ato( const char * ptr ) { |
---|
| 96 | int i; |
---|
| 97 | if ( sscanf( ptr, "%d", &i ) == EOF ) {} // check return code |
---|
| 98 | return i; |
---|
| 99 | } |
---|
| 100 | unsigned int ato( const char * ptr ) { |
---|
| 101 | unsigned int ui; |
---|
| 102 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {} // check return code |
---|
| 103 | return ui; |
---|
| 104 | } |
---|
| 105 | long int ato( const char * ptr ) { |
---|
| 106 | long int li; |
---|
[52f85e0] | 107 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {} // check return code |
---|
[bd85400] | 108 | return li; |
---|
| 109 | } |
---|
| 110 | unsigned long int ato( const char * ptr ) { |
---|
| 111 | unsigned long int uli; |
---|
[52f85e0] | 112 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} // check return code |
---|
[bd85400] | 113 | return uli; |
---|
| 114 | } |
---|
| 115 | long long int ato( const char * ptr ) { |
---|
| 116 | long long int lli; |
---|
[52f85e0] | 117 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} // check return code |
---|
[bd85400] | 118 | return lli; |
---|
| 119 | } |
---|
| 120 | unsigned long long int ato( const char * ptr ) { |
---|
| 121 | unsigned long long int ulli; |
---|
[52f85e0] | 122 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} // check return code |
---|
[bd85400] | 123 | return ulli; |
---|
| 124 | } |
---|
| 125 | float ato( const char * ptr ) { |
---|
| 126 | float f; |
---|
[52f85e0] | 127 | if ( sscanf( ptr, "%f", &f ) == EOF ) {} // check return code |
---|
[bd85400] | 128 | return f; |
---|
| 129 | } |
---|
| 130 | double ato( const char * ptr ) { |
---|
| 131 | double d; |
---|
[52f85e0] | 132 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {} // check return code |
---|
[bd85400] | 133 | return d; |
---|
| 134 | } |
---|
| 135 | long double ato( const char * ptr ) { |
---|
| 136 | long double ld; |
---|
[52f85e0] | 137 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} // check return code |
---|
[bd85400] | 138 | return ld; |
---|
| 139 | } |
---|
| 140 | float _Complex ato( const char * ptr ) { |
---|
| 141 | float re, im; |
---|
[52f85e0] | 142 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} // check return code |
---|
[bd85400] | 143 | return re + im * _Complex_I; |
---|
| 144 | } |
---|
| 145 | double _Complex ato( const char * ptr ) { |
---|
| 146 | double re, im; |
---|
[52f85e0] | 147 | if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} // check return code |
---|
[bd85400] | 148 | return re + im * _Complex_I; |
---|
| 149 | } |
---|
| 150 | long double _Complex ato( const char * ptr ) { |
---|
| 151 | long double re, im; |
---|
[52f85e0] | 152 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} // check return code |
---|
[bd85400] | 153 | return re + im * _Complex_I; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 157 | return (int)strtol( sptr, eptr, base ); |
---|
| 158 | } |
---|
| 159 | unsigned int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 160 | return (unsigned int)strtoul( sptr, eptr, base ); |
---|
| 161 | } |
---|
| 162 | long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 163 | return strtol( sptr, eptr, base ); |
---|
| 164 | } |
---|
| 165 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 166 | return strtoul( sptr, eptr, base ); |
---|
| 167 | } |
---|
| 168 | long long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 169 | return strtoll( sptr, eptr, base ); |
---|
| 170 | } |
---|
| 171 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 172 | return strtoull( sptr, eptr, base ); |
---|
| 173 | } |
---|
| 174 | float strto( const char * sptr, char ** eptr ) { |
---|
| 175 | return strtof( sptr, eptr ); |
---|
| 176 | } |
---|
| 177 | double strto( const char * sptr, char ** eptr ) { |
---|
| 178 | return strtod( sptr, eptr ); |
---|
| 179 | } |
---|
| 180 | long double strto( const char * sptr, char ** eptr ) { |
---|
| 181 | return strtold( sptr, eptr ); |
---|
| 182 | } |
---|
| 183 | float _Complex strto( const char * sptr, char ** eptr ) { |
---|
| 184 | float re, im; |
---|
| 185 | re = strtof( sptr, eptr ); |
---|
| 186 | if ( sptr == *eptr ) return 0.0; |
---|
| 187 | im = strtof( sptr, eptr ); |
---|
| 188 | if ( sptr == *eptr ) return 0.0; |
---|
| 189 | return re + im * _Complex_I; |
---|
| 190 | } |
---|
| 191 | double _Complex strto( const char * sptr, char ** eptr ) { |
---|
| 192 | double re, im; |
---|
| 193 | re = strtod( sptr, eptr ); |
---|
| 194 | if ( sptr == *eptr ) return 0.0; |
---|
| 195 | im = strtod( sptr, eptr ); |
---|
| 196 | if ( sptr == *eptr ) return 0.0; |
---|
| 197 | return re + im * _Complex_I; |
---|
| 198 | } |
---|
| 199 | long double _Complex strto( const char * sptr, char ** eptr ) { |
---|
| 200 | long double re, im; |
---|
| 201 | re = strtold( sptr, eptr ); |
---|
| 202 | if ( sptr == *eptr ) return 0.0; |
---|
| 203 | im = strtold( sptr, eptr ); |
---|
| 204 | if ( sptr == *eptr ) return 0.0; |
---|
| 205 | return re + im * _Complex_I; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | //--------------------------------------- |
---|
| 209 | |
---|
| 210 | forall( type T | { int ?<?( T, T ); } ) |
---|
| 211 | T * bsearch( const T key, const T * arr, size_t dimension ) { |
---|
| 212 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } |
---|
| 213 | return (T *)bsearch( &key, arr, dimension, sizeof(T), comp ); |
---|
| 214 | } // bsearch |
---|
| 215 | |
---|
| 216 | forall( type T | { int ?<?( T, T ); } ) |
---|
| 217 | void qsort( const T * arr, size_t dimension ) { |
---|
| 218 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } |
---|
| 219 | qsort( arr, dimension, sizeof(T), comp ); |
---|
| 220 | } // qsort |
---|
| 221 | |
---|
| 222 | //--------------------------------------- |
---|
| 223 | |
---|
| 224 | forall( type T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
| 225 | [ T, T ] div( T t1, T t2 ) { /* return [ t1 / t2, t1 % t2 ]; */ } |
---|
| 226 | |
---|
| 227 | //--------------------------------------- |
---|
| 228 | |
---|
| 229 | char abs( char v ) { return abs( (int)v ); } |
---|
| 230 | long int abs( long int v ) { return labs( v ); } |
---|
| 231 | long long int abs( long long int v ) { return llabs( v ); } |
---|
| 232 | float abs( float v ) { return fabsf( v ); } |
---|
| 233 | double abs( double v ) { return fabs( v ); } |
---|
| 234 | long double abs( long double v ) { return fabsl( v ); } |
---|
| 235 | float _Complex abs( float _Complex v ) { return cabsf( v ); } |
---|
| 236 | double _Complex abs( double _Complex v ) { return cabs( v ); } |
---|
| 237 | long double _Complex abs( long double _Complex v ) { return cabsl( v ); } |
---|
| 238 | |
---|
| 239 | //--------------------------------------- |
---|
| 240 | |
---|
| 241 | void randseed( long int s ) { srand48( s ); } |
---|
| 242 | char random() { return lrand48(); } |
---|
| 243 | int random() { return mrand48(); } |
---|
| 244 | unsigned int random() { return lrand48(); } |
---|
| 245 | long int random() { return mrand48(); } |
---|
| 246 | unsigned long int random() { return lrand48(); } |
---|
| 247 | float random() { return (float)drand48(); } // otherwise float uses lrand48 |
---|
| 248 | double random() { return drand48(); } |
---|
| 249 | float _Complex random() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
| 250 | double _Complex random() { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
| 251 | long double _Complex random() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
| 252 | |
---|
| 253 | //--------------------------------------- |
---|
| 254 | |
---|
| 255 | forall( type T | { int ?<?( T, T ); } ) |
---|
| 256 | T min( const T t1, const T t2 ) { |
---|
| 257 | return t1 < t2 ? t1 : t2; |
---|
| 258 | } // min |
---|
| 259 | |
---|
| 260 | forall( type T | { int ?>?( T, T ); } ) |
---|
| 261 | T max( const T t1, const T t2 ) { |
---|
| 262 | return t1 > t2 ? t1 : t2; |
---|
| 263 | } // max |
---|
| 264 | |
---|
| 265 | forall( type T ) |
---|
| 266 | void swap( T * t1, T * t2 ) { |
---|
| 267 | T temp = *t1; |
---|
| 268 | *t1 = *t2; |
---|
| 269 | *t2 = temp; |
---|
| 270 | } // swap |
---|
| 271 | |
---|
| 272 | // Local Variables: // |
---|
| 273 | // tab-width: 4 // |
---|
| 274 | // End: // |
---|