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