[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 |
---|
[b89c7c2] | 11 | // Last Modified By : Peter A. Buhr |
---|
[f6a4917] | 12 | // Last Modified On : Thu Aug 25 18:07:06 2022 |
---|
| 13 | // Update Count : 645 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[17e5e2b] | 17 | |
---|
[94429f8] | 18 | #include "bits/defs.hfa" // OPTIONAL_THREAD |
---|
| 19 | #include "bits/align.hfa" // libAlign |
---|
[12b5e94a] | 20 | #include "bits/random.hfa" // prng |
---|
[2026bb6] | 21 | |
---|
[d46ed6e] | 22 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
[4e7c0fc0] | 23 | #include <heap.hfa> |
---|
[d6b03b7] | 24 | |
---|
[2210cfc] | 25 | |
---|
[ca7949b] | 26 | // Reduce includes by explicitly defining these routines. |
---|
[3ce0d440] | 27 | extern "C" { |
---|
[4e7c0fc0] | 28 | void * memalign( size_t alignment, size_t size ); // malloc.h |
---|
| 29 | void * pvalloc( size_t size ); // malloc.h |
---|
[b9c04946] | 30 | void * memset( void * dest, int fill, size_t size ); // string.h |
---|
[57fc7d8] | 31 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
[3ce0d440] | 32 | } // extern "C" |
---|
[e672372] | 33 | |
---|
[bd85400] | 34 | //--------------------------------------- |
---|
| 35 | |
---|
[45161b4d] | 36 | #ifndef EXIT_FAILURE |
---|
| 37 | #define EXIT_FAILURE 1 // failing exit status |
---|
| 38 | #define EXIT_SUCCESS 0 // successful exit status |
---|
| 39 | #endif // ! EXIT_FAILURE |
---|
| 40 | |
---|
| 41 | //--------------------------------------- |
---|
| 42 | |
---|
[c354108] | 43 | #include "common.hfa" |
---|
| 44 | |
---|
| 45 | //--------------------------------------- |
---|
| 46 | |
---|
[fd54fef] | 47 | static inline forall( T & | sized(T) ) { |
---|
[4803a901] | 48 | // CFA safe equivalents, i.e., implicit size specification |
---|
[3ce0d440] | 49 | |
---|
[74b19fb] | 50 | T * malloc( void ) { |
---|
[aa0a1ad] | 51 | if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation |
---|
[68f0c4e] | 52 | else return (T *)memalign( _Alignof(T), sizeof(T) ); |
---|
[74b19fb] | 53 | } // malloc |
---|
| 54 | |
---|
[856fe3e] | 55 | T * aalloc( size_t dim ) { |
---|
[aa0a1ad] | 56 | if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation |
---|
| 57 | else return (T *)amemalign( _Alignof(T), dim, sizeof(T) ); |
---|
[856fe3e] | 58 | } // aalloc |
---|
| 59 | |
---|
[74b19fb] | 60 | T * calloc( size_t dim ) { |
---|
[aa0a1ad] | 61 | if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation |
---|
| 62 | else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); |
---|
[74b19fb] | 63 | } // calloc |
---|
| 64 | |
---|
[b89c7c2] | 65 | T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast |
---|
[aa0a1ad] | 66 | if ( _Alignof(T) <= libAlign() ) return (T *)resize( (void *)ptr, size ); // CFA resize |
---|
| 67 | else return (T *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize |
---|
[856fe3e] | 68 | } // resize |
---|
| 69 | |
---|
[d74369b] | 70 | T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast |
---|
[aa0a1ad] | 71 | if ( _Alignof(T) <= libAlign() ) return (T *)realloc( (void *)ptr, size ); // C realloc |
---|
| 72 | else return (T *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc |
---|
[74b19fb] | 73 | } // realloc |
---|
| 74 | |
---|
| 75 | T * memalign( size_t align ) { |
---|
[cafb687] | 76 | return (T *)memalign( align, sizeof(T) ); // C memalign |
---|
[74b19fb] | 77 | } // memalign |
---|
| 78 | |
---|
[856fe3e] | 79 | T * amemalign( size_t align, size_t dim ) { |
---|
| 80 | return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign |
---|
| 81 | } // amemalign |
---|
| 82 | |
---|
[d74369b] | 83 | T * cmemalign( size_t align, size_t dim ) { |
---|
| 84 | return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign |
---|
| 85 | } // cmemalign |
---|
| 86 | |
---|
[74b19fb] | 87 | T * aligned_alloc( size_t align ) { |
---|
[cafb687] | 88 | return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc |
---|
[74b19fb] | 89 | } // aligned_alloc |
---|
| 90 | |
---|
| 91 | int posix_memalign( T ** ptr, size_t align ) { |
---|
| 92 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
| 93 | } // posix_memalign |
---|
[ada0246d] | 94 | |
---|
| 95 | T * valloc( void ) { |
---|
| 96 | return (T *)valloc( sizeof(T) ); // C valloc |
---|
| 97 | } // valloc |
---|
| 98 | |
---|
| 99 | T * pvalloc( void ) { |
---|
| 100 | return (T *)pvalloc( sizeof(T) ); // C pvalloc |
---|
| 101 | } // pvalloc |
---|
[55acc3a] | 102 | } // distribution |
---|
| 103 | |
---|
[ceb7db8] | 104 | /* |
---|
| 105 | FIX ME : fix alloc interface after Ticker Number 214 is resolved, define and add union to S_fill. Then, modify postfix-fill functions to support T * with nmemb, char, and T object of any size. Finally, change alloc_internal. |
---|
| 106 | Or, just follow the instructions below for that. |
---|
| 107 | |
---|
| 108 | 1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following: |
---|
[fd54fef] | 109 | forall( T & | sized(T) ) { |
---|
[ceb7db8] | 110 | union U_fill { char c; T * a; T t; }; |
---|
[685810e] | 111 | struct S_fill { char tag; U_fill(T) fill; }; |
---|
[ceb7db8] | 112 | struct S_realloc { inline T *; }; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | 2. Replace all current postfix-fill functions with following for updated S_fill: |
---|
| 116 | S_fill(T) ?`fill( char a ) { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; } |
---|
| 117 | S_fill(T) ?`fill( T a ) { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; } |
---|
| 118 | S_fill(T) ?`fill( T a[], size_t nmemb ) { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; } |
---|
| 119 | |
---|
[6c5d92f] | 120 | 3. Replace the alloc_internal$ function which is outside ttype forall-block with following function: |
---|
| 121 | T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) { |
---|
[ceb7db8] | 122 | T * ptr = NULL; |
---|
| 123 | size_t size = sizeof(T); |
---|
| 124 | size_t copy_end = 0; |
---|
| 125 | |
---|
| 126 | if(Resize) { |
---|
| 127 | ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size ); |
---|
| 128 | } else if (Realloc) { |
---|
| 129 | if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size); |
---|
| 130 | ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size ); |
---|
| 131 | } else { |
---|
| 132 | ptr = (T*) (void *) memalign( Align, Dim * size ); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if(Fill.tag == 'c') { |
---|
| 136 | memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end ); |
---|
| 137 | } else if(Fill.tag == 't') { |
---|
| 138 | for ( int i = copy_end; i <= Dim * size - size ; i += size ) { |
---|
| 139 | memcpy( (char *)ptr + i, &Fill.fill.t, size ); |
---|
| 140 | } |
---|
| 141 | } else if(Fill.tag == 'a') { |
---|
| 142 | memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) ); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | return ptr; |
---|
[6c5d92f] | 146 | } // alloc_internal$ |
---|
[ceb7db8] | 147 | */ |
---|
| 148 | |
---|
| 149 | typedef struct S_align { inline size_t; } T_align; |
---|
| 150 | typedef struct S_resize { inline void *; } T_resize; |
---|
| 151 | |
---|
[fd54fef] | 152 | forall( T & ) { |
---|
[ceb7db8] | 153 | struct S_fill { char tag; char c; size_t size; T * at; char t[50]; }; |
---|
| 154 | struct S_realloc { inline T *; }; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | static inline T_align ?`align ( size_t a ) { return (T_align){a}; } |
---|
| 158 | static inline T_resize ?`resize ( void * a ) { return (T_resize){a}; } |
---|
[74b19fb] | 159 | |
---|
[fd54fef] | 160 | static inline forall( T & | sized(T) ) { |
---|
[ceb7db8] | 161 | S_fill(T) ?`fill ( T t ) { |
---|
| 162 | S_fill(T) ret = { 't' }; |
---|
| 163 | size_t size = sizeof(T); |
---|
[3d3d75e] | 164 | if ( size > sizeof(ret.t) ) { |
---|
| 165 | abort( "ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n" ); |
---|
| 166 | } // if |
---|
[ceb7db8] | 167 | memcpy( &ret.t, &t, size ); |
---|
| 168 | return ret; |
---|
| 169 | } |
---|
[7a6ae53] | 170 | S_fill(T) ?`fill ( zero_t ) = void; // FIX ME: remove this once ticket 214 is resolved |
---|
| 171 | S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } // FIX ME: remove this once ticket 214 is resolved |
---|
[ceb7db8] | 172 | S_fill(T) ?`fill ( char c ) { return (S_fill(T)){ 'c', c }; } |
---|
| 173 | S_fill(T) ?`fill ( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; } |
---|
| 174 | |
---|
| 175 | S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; } |
---|
| 176 | |
---|
[6c5d92f] | 177 | T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill ) { |
---|
[ceb7db8] | 178 | T * ptr = NULL; |
---|
| 179 | size_t size = sizeof(T); |
---|
| 180 | size_t copy_end = 0; |
---|
[f67b983] | 181 | |
---|
| 182 | if ( Resize ) { |
---|
[68f0c4e] | 183 | ptr = (T*) (void *) resize( (void *)Resize, Align, Dim * size ); |
---|
[f67b983] | 184 | } else if ( Realloc ) { |
---|
[3d3d75e] | 185 | if ( Fill.tag != '0' ) copy_end = min(malloc_size( Realloc ), Dim * size ); |
---|
| 186 | ptr = (T *) (void *) realloc( (void *)Realloc, Align, Dim * size ); |
---|
[cfbc703d] | 187 | } else { |
---|
[3d3d75e] | 188 | ptr = (T *) (void *) memalign( Align, Dim * size ); |
---|
[ceb7db8] | 189 | } |
---|
| 190 | |
---|
[3d3d75e] | 191 | if ( Fill.tag == 'c' ) { |
---|
[ceb7db8] | 192 | memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end ); |
---|
[3d3d75e] | 193 | } else if ( Fill.tag == 't' ) { |
---|
[f6a4917] | 194 | for ( i; copy_end ~ Dim * size ~ size ) { |
---|
[3d3d75e] | 195 | #pragma GCC diagnostic push |
---|
| 196 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
---|
[d1b70d4] | 197 | assert( size <= sizeof(Fill.t) ); |
---|
| 198 | memcpy( (char *)ptr + i, &Fill.t, size ); |
---|
[3d3d75e] | 199 | #pragma GCC diagnostic pop |
---|
[ceb7db8] | 200 | } |
---|
[3d3d75e] | 201 | } else if ( Fill.tag == 'a' ) { |
---|
[ceb7db8] | 202 | memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) ); |
---|
[3d3d75e] | 203 | } else if ( Fill.tag == 'T' ) { |
---|
| 204 | memcpy( (char *)ptr + copy_end, Fill.at, Dim * size ); |
---|
[ceb7db8] | 205 | } |
---|
| 206 | |
---|
| 207 | return ptr; |
---|
[6c5d92f] | 208 | } // alloc_internal$ |
---|
[ceb7db8] | 209 | |
---|
[6c5d92f] | 210 | forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) { |
---|
[58e97d9] | 211 | T * alloc_internal$( void *, T *, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest ) { |
---|
[6c5d92f] | 212 | return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest); |
---|
[ceb7db8] | 213 | } |
---|
| 214 | |
---|
[58e97d9] | 215 | T * alloc_internal$( void *, T *, size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest ) { |
---|
[6c5d92f] | 216 | return alloc_internal$( (void*)0p, Realloc, Align, Dim, Fill, rest); |
---|
[ceb7db8] | 217 | } |
---|
| 218 | |
---|
[58e97d9] | 219 | T * alloc_internal$( void * Resize, T * Realloc, size_t, size_t Dim, S_fill(T) Fill, T_align Align, TT rest ) { |
---|
[6c5d92f] | 220 | return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest); |
---|
[ceb7db8] | 221 | } |
---|
| 222 | |
---|
[58e97d9] | 223 | T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T), S_fill(T) Fill, TT rest ) { |
---|
| 224 | return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest ); |
---|
[ceb7db8] | 225 | } |
---|
| 226 | |
---|
| 227 | T * alloc( TT all ) { |
---|
[58e97d9] | 228 | return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all ); |
---|
[ceb7db8] | 229 | } |
---|
| 230 | |
---|
| 231 | T * alloc( size_t dim, TT all ) { |
---|
[58e97d9] | 232 | return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all ); |
---|
[ceb7db8] | 233 | } |
---|
| 234 | } // distribution TT |
---|
| 235 | } // distribution T |
---|
[3ce0d440] | 236 | |
---|
[fd54fef] | 237 | static inline forall( T & | sized(T) ) { |
---|
[4803a901] | 238 | // CFA safe initialization/copy, i.e., implicit size specification, non-array types |
---|
[b9c04946] | 239 | T * memset( T * dest, char fill ) { |
---|
| 240 | return (T *)memset( dest, fill, sizeof(T) ); |
---|
[3ce0d440] | 241 | } // memset |
---|
| 242 | |
---|
| 243 | T * memcpy( T * dest, const T * src ) { |
---|
| 244 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
| 245 | } // memcpy |
---|
| 246 | |
---|
[4803a901] | 247 | // CFA safe initialization/copy, i.e., implicit size specification, array types |
---|
[b9c04946] | 248 | T * amemset( T dest[], char fill, size_t dim ) { |
---|
| 249 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
| 250 | } // amemset |
---|
[3ce0d440] | 251 | |
---|
[b9c04946] | 252 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
[3ce0d440] | 253 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
[b9c04946] | 254 | } // amemcpy |
---|
[3ce0d440] | 255 | } // distribution |
---|
[f3fc631f] | 256 | |
---|
[4803a901] | 257 | // CFA deallocation for multiple objects |
---|
[fd54fef] | 258 | static inline forall( T & ) // FIX ME, problems with 0p in list |
---|
[4803a901] | 259 | void free( T * ptr ) { |
---|
| 260 | free( (void *)ptr ); // C free |
---|
| 261 | } // free |
---|
[fd54fef] | 262 | static inline forall( T &, TT... | { void free( TT ); } ) |
---|
[4803a901] | 263 | void free( T * ptr, TT rest ) { |
---|
| 264 | free( ptr ); |
---|
[94429f8] | 265 | free( rest ); |
---|
| 266 | } // free |
---|
| 267 | |
---|
[4803a901] | 268 | // CFA allocation/deallocation and constructor/destructor, non-array types |
---|
[fd54fef] | 269 | static inline forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) |
---|
[94429f8] | 270 | T * new( TT p ) { |
---|
[09ee131] | 271 | return &(*(T *)malloc()){ p }; // run constructor |
---|
[94429f8] | 272 | } // new |
---|
| 273 | |
---|
[fd54fef] | 274 | static inline forall( T & | { void ^?{}( T & ); } ) |
---|
[94429f8] | 275 | void delete( T * ptr ) { |
---|
[0f7a0ea] | 276 | // special case for 0-sized object => always call destructor |
---|
| 277 | if ( ptr || sizeof(ptr) == 0 ) { // ignore null but not 0-sized objects |
---|
[94429f8] | 278 | ^(*ptr){}; // run destructor |
---|
| 279 | } // if |
---|
[4803a901] | 280 | free( ptr ); // always call free |
---|
[94429f8] | 281 | } // delete |
---|
[fd54fef] | 282 | static inline forall( T &, TT... | { void ^?{}( T & ); void delete( TT ); } ) |
---|
[94429f8] | 283 | void delete( T * ptr, TT rest ) { |
---|
| 284 | delete( ptr ); |
---|
| 285 | delete( rest ); |
---|
| 286 | } // delete |
---|
[627f585] | 287 | |
---|
[4803a901] | 288 | // CFA allocation/deallocation and constructor/destructor, array types |
---|
[fd54fef] | 289 | forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p ); |
---|
| 290 | forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] ); |
---|
| 291 | forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest ); |
---|
[6065b3aa] | 292 | |
---|
[bd85400] | 293 | //--------------------------------------- |
---|
| 294 | |
---|
[57fc7d8] | 295 | static inline { |
---|
[e3fea42] | 296 | int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
| 297 | unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
| 298 | long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
| 299 | unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
| 300 | long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
| 301 | unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
| 302 | |
---|
| 303 | float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); } |
---|
| 304 | double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); } |
---|
| 305 | long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); } |
---|
[57fc7d8] | 306 | } // distribution |
---|
[e672372] | 307 | |
---|
[e3fea42] | 308 | float _Complex strto( const char sptr[], char ** eptr ); |
---|
| 309 | double _Complex strto( const char sptr[], char ** eptr ); |
---|
| 310 | long double _Complex strto( const char sptr[], char ** eptr ); |
---|
[bd85400] | 311 | |
---|
[57fc7d8] | 312 | static inline { |
---|
[e3fea42] | 313 | int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); } |
---|
| 314 | unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); } |
---|
| 315 | long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); } |
---|
| 316 | unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); } |
---|
| 317 | long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); } |
---|
| 318 | unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); } |
---|
| 319 | |
---|
| 320 | float ato( const char sptr[] ) { return strtof( sptr, 0p ); } |
---|
| 321 | double ato( const char sptr[] ) { return strtod( sptr, 0p ); } |
---|
| 322 | long double ato( const char sptr[] ) { return strtold( sptr, 0p ); } |
---|
| 323 | |
---|
| 324 | float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
| 325 | double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
| 326 | long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
[57fc7d8] | 327 | } // distribution |
---|
[e672372] | 328 | |
---|
[bd85400] | 329 | //--------------------------------------- |
---|
| 330 | |
---|
[fd54fef] | 331 | forall( E | { int ?<?( E, E ); } ) { |
---|
[3ce0d440] | 332 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
| 333 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
| 334 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
| 335 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
| 336 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
| 337 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
| 338 | } // distribution |
---|
[9c47a47] | 339 | |
---|
[fd54fef] | 340 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
[3ce0d440] | 341 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
| 342 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
| 343 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
| 344 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
| 345 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
| 346 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
| 347 | } // distribution |
---|
[bd85400] | 348 | |
---|
[fd54fef] | 349 | forall( E | { int ?<?( E, E ); } ) { |
---|
[b9c04946] | 350 | void qsort( E * vals, size_t dim ); |
---|
| 351 | } // distribution |
---|
| 352 | |
---|
[bd85400] | 353 | //--------------------------------------- |
---|
| 354 | |
---|
[bbe1a87] | 355 | extern "C" { // override C version |
---|
| 356 | void srandom( unsigned int seed ); |
---|
[4e7c0fc0] | 357 | long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
| 358 | // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U; |
---|
[bbe1a87] | 359 | } // extern "C" |
---|
| 360 | |
---|
| 361 | static inline { |
---|
[aa8e24c3] | 362 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u] |
---|
| 363 | long int random( long int u ) { return random( 0, u - 1 ); } // [0,u) |
---|
[bbe1a87] | 364 | unsigned long int random( void ) { return lrand48(); } |
---|
| 365 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
[aa8e24c3] | 366 | unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l + 1) + l; } // [l,u] |
---|
[bbe1a87] | 367 | |
---|
| 368 | char random( void ) { return (unsigned long int)random(); } |
---|
| 369 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
| 370 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 371 | int random( void ) { return (long int)random(); } |
---|
| 372 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
| 373 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
| 374 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
| 375 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
| 376 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 377 | } // distribution |
---|
| 378 | |
---|
| 379 | float random( void ); // [0.0, 1.0) |
---|
| 380 | double random( void ); // [0.0, 1.0) |
---|
| 381 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 382 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 383 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
[bd85400] | 384 | |
---|
| 385 | //--------------------------------------- |
---|
| 386 | |
---|
[1959528] | 387 | // Sequential Pseudo Random-Number Generator : generate repeatable sequence of values that appear random. |
---|
| 388 | // |
---|
| 389 | // Declaration : |
---|
| 390 | // PRNG sprng = { 1009 } - set starting seed versus random seed |
---|
[a892e61] | 391 | // |
---|
[1959528] | 392 | // Interface : |
---|
| 393 | // set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed |
---|
| 394 | // get_seed( sprng ) - read seed |
---|
| 395 | // prng( sprng ) - generate random value in range [0,UINT_MAX] |
---|
| 396 | // prng( sprng, u ) - generate random value in range [0,u) |
---|
| 397 | // prng( sprng, l, u ) - generate random value in range [l,u] |
---|
| 398 | // calls( sprng ) - number of generated random value so far |
---|
| 399 | // |
---|
| 400 | // Examples : generate random number between 5-21 |
---|
| 401 | // prng( sprng ) % 17 + 5; values 0-16 + 5 = 5-21 |
---|
| 402 | // prng( sprng, 16 + 1 ) + 5; |
---|
| 403 | // prng( sprng, 5, 21 ); |
---|
| 404 | // calls( sprng ); |
---|
| 405 | |
---|
[aa8e24c3] | 406 | struct PRNG { |
---|
| 407 | uint32_t callcnt; // call count |
---|
| 408 | uint32_t seed; // current seed |
---|
| 409 | uint32_t state; // random state |
---|
| 410 | }; // PRNG |
---|
| 411 | |
---|
[1959528] | 412 | void set_seed( PRNG & prng, uint32_t seed_ ); |
---|
[aa8e24c3] | 413 | static inline { |
---|
[0ebbca4] | 414 | void ?{}( PRNG & prng ) with( prng ) { callcnt = 0; set_seed( prng, rdtscl() ); } // random seed |
---|
| 415 | void ?{}( PRNG & prng, uint32_t seed ) with( prng ) { callcnt = 0; set_seed( prng, seed ); } // fixed seed |
---|
[aa8e24c3] | 416 | uint32_t get_seed( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return seed; } // get seed |
---|
[6a823241] | 417 | uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { callcnt += 1; return LCG( state ); } // [0,UINT_MAX] |
---|
[aa8e24c3] | 418 | uint32_t prng( PRNG & prng, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng ) % u; } // [0,u) |
---|
| 419 | uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( prng, u - l + 1 ) + l; } // [l,u] |
---|
| 420 | uint32_t calls( PRNG & prng ) __attribute__(( warn_unused_result )) with( prng ) { return callcnt; } |
---|
| 421 | } // distribution |
---|
| 422 | |
---|
[1959528] | 423 | // Concurrent Pseudo Random-Number Generator : generate repeatable sequence of values that appear random. |
---|
| 424 | // |
---|
| 425 | // Interface : |
---|
| 426 | // set_seed( 1009 ) - fixed seed for all kernel threads versus random seed |
---|
| 427 | // get_seed() - read seed |
---|
| 428 | // prng() - generate random value in range [0,UINT_MAX] |
---|
| 429 | // prng( u ) - generate random value in range [0,u) |
---|
| 430 | // prng( l, u ) - generate random value in range [l,u] |
---|
| 431 | // |
---|
| 432 | // Examples : generate random number between 5-21 |
---|
| 433 | // prng() % 17 + 5; values 0-16 + 5 = 5-21 |
---|
| 434 | // prng( 16 + 1 ) + 5; |
---|
| 435 | // prng( 5, 21 ); |
---|
| 436 | |
---|
[2210cfc] | 437 | void set_seed( uint32_t seed_ ) OPTIONAL_THREAD; |
---|
| 438 | uint32_t get_seed() __attribute__(( warn_unused_result )); |
---|
| 439 | uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX] |
---|
[aa8e24c3] | 440 | static inline { |
---|
[2210cfc] | 441 | uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u) |
---|
| 442 | uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u] |
---|
[aa8e24c3] | 443 | } // distribution |
---|
| 444 | |
---|
| 445 | //--------------------------------------- |
---|
| 446 | |
---|
[94429f8] | 447 | extern bool threading_enabled( void ) OPTIONAL_THREAD; |
---|
[2026bb6] | 448 | |
---|
[bd85400] | 449 | // Local Variables: // |
---|
| 450 | // mode: c // |
---|
| 451 | // tab-width: 4 // |
---|
| 452 | // End: // |
---|