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