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