[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 |
---|
[f67b983] | 12 | // Last Modified On : Tue Sep 1 20:32:34 2020 |
---|
| 13 | // Update Count : 505 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[17e5e2b] | 17 | |
---|
[2026bb6] | 18 | #include "bits/defs.hfa" |
---|
[d6b03b7] | 19 | #include "bits/align.hfa" |
---|
[2026bb6] | 20 | |
---|
[d46ed6e] | 21 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
[4e7c0fc0] | 22 | #include <heap.hfa> |
---|
[d6b03b7] | 23 | |
---|
[ca7949b] | 24 | // Reduce includes by explicitly defining these routines. |
---|
[3ce0d440] | 25 | extern "C" { |
---|
[4e7c0fc0] | 26 | void * memalign( size_t alignment, size_t size ); // malloc.h |
---|
| 27 | void * pvalloc( size_t size ); // malloc.h |
---|
[b9c04946] | 28 | void * memset( void * dest, int fill, size_t size ); // string.h |
---|
[57fc7d8] | 29 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
[3ce0d440] | 30 | } // extern "C" |
---|
[e672372] | 31 | |
---|
[bd85400] | 32 | //--------------------------------------- |
---|
| 33 | |
---|
[45161b4d] | 34 | #ifndef EXIT_FAILURE |
---|
| 35 | #define EXIT_FAILURE 1 // failing exit status |
---|
| 36 | #define EXIT_SUCCESS 0 // successful exit status |
---|
| 37 | #endif // ! EXIT_FAILURE |
---|
| 38 | |
---|
| 39 | //--------------------------------------- |
---|
| 40 | |
---|
[c354108] | 41 | #include "common.hfa" |
---|
| 42 | |
---|
| 43 | //--------------------------------------- |
---|
| 44 | |
---|
[f67b983] | 45 | // Macro because of returns |
---|
[b0a0ee4] | 46 | #define $ARRAY_ALLOC( allocation, alignment, dim ) \ |
---|
| 47 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \ |
---|
| 48 | else return (T *)alignment( _Alignof(T), dim, sizeof(T) ) |
---|
| 49 | |
---|
[74b19fb] | 50 | static inline forall( dtype T | sized(T) ) { |
---|
[ca7949b] | 51 | // Cforall safe equivalents, i.e., implicit size specification |
---|
[3ce0d440] | 52 | |
---|
[74b19fb] | 53 | T * malloc( void ) { |
---|
[f67b983] | 54 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation |
---|
[68f0c4e] | 55 | else return (T *)memalign( _Alignof(T), sizeof(T) ); |
---|
[74b19fb] | 56 | } // malloc |
---|
| 57 | |
---|
[856fe3e] | 58 | T * aalloc( size_t dim ) { |
---|
[b0a0ee4] | 59 | $ARRAY_ALLOC( aalloc, amemalign, dim ); |
---|
[856fe3e] | 60 | } // aalloc |
---|
| 61 | |
---|
[74b19fb] | 62 | T * calloc( size_t dim ) { |
---|
[b0a0ee4] | 63 | $ARRAY_ALLOC( calloc, cmemalign, dim ); |
---|
[74b19fb] | 64 | } // calloc |
---|
| 65 | |
---|
[b89c7c2] | 66 | T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast |
---|
[60062be] | 67 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize |
---|
| 68 | else return (T *)(void *)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 |
---|
[60062be] | 72 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc |
---|
| 73 | else return (T *)(void *)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 | |
---|
| 104 | void free( T * addr ) { |
---|
| 105 | free( (void *) addr ); // C free |
---|
| 106 | } // free |
---|
| 107 | } // distribution |
---|
| 108 | |
---|
| 109 | static inline forall( ttype TT | { void free( TT ); } ) { |
---|
| 110 | // T* does not take void* and vice-versa |
---|
| 111 | |
---|
| 112 | void free( void * addr, TT rest ) { |
---|
| 113 | free( addr ); |
---|
| 114 | free( rest ); |
---|
| 115 | } // free |
---|
| 116 | |
---|
| 117 | forall( dtype T | sized(T) ) |
---|
| 118 | void free( T * addr, TT rest ) { |
---|
| 119 | free( addr ); |
---|
| 120 | free( rest ); |
---|
| 121 | } // free |
---|
[cfbc703d] | 122 | } // distribution |
---|
[74b19fb] | 123 | |
---|
[ceb7db8] | 124 | /* |
---|
| 125 | 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. |
---|
| 126 | Or, just follow the instructions below for that. |
---|
| 127 | |
---|
| 128 | 1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following: |
---|
| 129 | forall( dtype T | sized(T) ) { |
---|
| 130 | union U_fill { char c; T * a; T t; }; |
---|
[685810e] | 131 | struct S_fill { char tag; U_fill(T) fill; }; |
---|
[ceb7db8] | 132 | struct S_realloc { inline T *; }; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | 2. Replace all current postfix-fill functions with following for updated S_fill: |
---|
| 136 | S_fill(T) ?`fill( char a ) { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; } |
---|
| 137 | S_fill(T) ?`fill( T a ) { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; } |
---|
| 138 | S_fill(T) ?`fill( T a[], size_t nmemb ) { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; } |
---|
| 139 | |
---|
| 140 | 3. Replace the $alloc_internal function which is outside ttype forall-block with following function: |
---|
| 141 | T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) { |
---|
| 142 | T * ptr = NULL; |
---|
| 143 | size_t size = sizeof(T); |
---|
| 144 | size_t copy_end = 0; |
---|
| 145 | |
---|
| 146 | if(Resize) { |
---|
| 147 | ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size ); |
---|
| 148 | } else if (Realloc) { |
---|
| 149 | if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size); |
---|
| 150 | ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size ); |
---|
| 151 | } else { |
---|
| 152 | ptr = (T*) (void *) memalign( Align, Dim * size ); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | if(Fill.tag == 'c') { |
---|
| 156 | memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end ); |
---|
| 157 | } else if(Fill.tag == 't') { |
---|
| 158 | for ( int i = copy_end; i <= Dim * size - size ; i += size ) { |
---|
| 159 | memcpy( (char *)ptr + i, &Fill.fill.t, size ); |
---|
| 160 | } |
---|
| 161 | } else if(Fill.tag == 'a') { |
---|
| 162 | memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) ); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | return ptr; |
---|
| 166 | } // $alloc_internal |
---|
| 167 | */ |
---|
| 168 | |
---|
| 169 | typedef struct S_align { inline size_t; } T_align; |
---|
| 170 | typedef struct S_resize { inline void *; } T_resize; |
---|
| 171 | |
---|
| 172 | forall( dtype T ) { |
---|
| 173 | struct S_fill { char tag; char c; size_t size; T * at; char t[50]; }; |
---|
| 174 | struct S_realloc { inline T *; }; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | static inline T_align ?`align ( size_t a ) { return (T_align){a}; } |
---|
| 178 | static inline T_resize ?`resize ( void * a ) { return (T_resize){a}; } |
---|
[cfbc703d] | 179 | static inline forall( dtype T | sized(T) ) { |
---|
[74b19fb] | 180 | |
---|
[ceb7db8] | 181 | S_fill(T) ?`fill ( T t ) { |
---|
| 182 | S_fill(T) ret = { 't' }; |
---|
| 183 | size_t size = sizeof(T); |
---|
| 184 | if(size > sizeof(ret.t)) { printf("ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n"); exit(1); } |
---|
| 185 | memcpy( &ret.t, &t, size ); |
---|
| 186 | return ret; |
---|
| 187 | } |
---|
| 188 | S_fill(T) ?`fill ( char c ) { return (S_fill(T)){ 'c', c }; } |
---|
| 189 | S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } |
---|
| 190 | S_fill(T) ?`fill ( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; } |
---|
| 191 | |
---|
| 192 | S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; } |
---|
| 193 | |
---|
| 194 | T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) { |
---|
| 195 | T * ptr = NULL; |
---|
| 196 | size_t size = sizeof(T); |
---|
| 197 | size_t copy_end = 0; |
---|
[f67b983] | 198 | |
---|
| 199 | if ( Resize ) { |
---|
[68f0c4e] | 200 | ptr = (T*) (void *) resize( (void *)Resize, Align, Dim * size ); |
---|
[f67b983] | 201 | } else if ( Realloc ) { |
---|
[ceb7db8] | 202 | if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size); |
---|
[68f0c4e] | 203 | ptr = (T*) (void *) realloc( (void *)Realloc, Align, Dim * size ); |
---|
[cfbc703d] | 204 | } else { |
---|
[ceb7db8] | 205 | ptr = (T*) (void *) memalign( Align, Dim * size ); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | if(Fill.tag == 'c') { |
---|
| 209 | memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end ); |
---|
| 210 | } else if(Fill.tag == 't') { |
---|
[191a190] | 211 | for ( int i = copy_end; i < Dim * size; i += size ) { |
---|
[ceb7db8] | 212 | memcpy( (char *)ptr + i, &Fill.t, size ); |
---|
| 213 | } |
---|
| 214 | } else if(Fill.tag == 'a') { |
---|
| 215 | memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) ); |
---|
| 216 | } else if(Fill.tag == 'T') { |
---|
[191a190] | 217 | for ( int i = copy_end; i < Dim * size; i += size ) { |
---|
[ceb7db8] | 218 | memcpy( (char *)ptr + i, Fill.at, size ); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return ptr; |
---|
| 223 | } // $alloc_internal |
---|
| 224 | |
---|
| 225 | forall( ttype TT | { T * $alloc_internal( void *, T *, size_t, size_t, S_fill(T), TT ); } ) { |
---|
| 226 | |
---|
| 227 | T * $alloc_internal( void * , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) { |
---|
| 228 | return $alloc_internal( Resize, (T*)0p, Align, Dim, Fill, rest); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | T * $alloc_internal( void * Resize, T * , size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest) { |
---|
| 232 | return $alloc_internal( (void*)0p, Realloc, Align, Dim, Fill, rest); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | T * $alloc_internal( void * Resize, T * Realloc, size_t , size_t Dim, S_fill(T) Fill, T_align Align, TT rest) { |
---|
| 236 | return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) , S_fill(T) Fill, TT rest) { |
---|
| 240 | return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | T * alloc( TT all ) { |
---|
| 244 | return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | T * alloc( size_t dim, TT all ) { |
---|
| 248 | return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | } // distribution TT |
---|
| 252 | |
---|
| 253 | } // distribution T |
---|
[3ce0d440] | 254 | |
---|
| 255 | static inline forall( dtype T | sized(T) ) { |
---|
[ca7949b] | 256 | // Cforall safe initialization/copy, i.e., implicit size specification, non-array types |
---|
[b9c04946] | 257 | T * memset( T * dest, char fill ) { |
---|
| 258 | return (T *)memset( dest, fill, sizeof(T) ); |
---|
[3ce0d440] | 259 | } // memset |
---|
| 260 | |
---|
| 261 | T * memcpy( T * dest, const T * src ) { |
---|
| 262 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
| 263 | } // memcpy |
---|
| 264 | } // distribution |
---|
| 265 | |
---|
| 266 | static inline forall( dtype T | sized(T) ) { |
---|
[ca7949b] | 267 | // Cforall safe initialization/copy, i.e., implicit size specification, array types |
---|
[b9c04946] | 268 | T * amemset( T dest[], char fill, size_t dim ) { |
---|
| 269 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
| 270 | } // amemset |
---|
[3ce0d440] | 271 | |
---|
[b9c04946] | 272 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
[3ce0d440] | 273 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
[b9c04946] | 274 | } // amemcpy |
---|
[3ce0d440] | 275 | } // distribution |
---|
[f3fc631f] | 276 | |
---|
[ca7949b] | 277 | // Cforall allocation/deallocation and constructor/destructor, non-array types |
---|
[aca65621] | 278 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
[aabb846] | 279 | forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
| 280 | forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
[627f585] | 281 | |
---|
[ca7949b] | 282 | // Cforall allocation/deallocation and constructor/destructor, array types |
---|
[aca65621] | 283 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
[45444c3] | 284 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] ); |
---|
| 285 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( T arr[], Params rest ); |
---|
[6065b3aa] | 286 | |
---|
[bd85400] | 287 | //--------------------------------------- |
---|
| 288 | |
---|
[57fc7d8] | 289 | static inline { |
---|
[e3fea42] | 290 | int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
| 291 | unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
| 292 | long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
| 293 | unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
| 294 | long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
| 295 | unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
| 296 | |
---|
| 297 | float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); } |
---|
| 298 | double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); } |
---|
| 299 | long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); } |
---|
[57fc7d8] | 300 | } // distribution |
---|
[e672372] | 301 | |
---|
[e3fea42] | 302 | float _Complex strto( const char sptr[], char ** eptr ); |
---|
| 303 | double _Complex strto( const char sptr[], char ** eptr ); |
---|
| 304 | long double _Complex strto( const char sptr[], char ** eptr ); |
---|
[bd85400] | 305 | |
---|
[57fc7d8] | 306 | static inline { |
---|
[e3fea42] | 307 | int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); } |
---|
| 308 | unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); } |
---|
| 309 | long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); } |
---|
| 310 | unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); } |
---|
| 311 | long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); } |
---|
| 312 | unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); } |
---|
| 313 | |
---|
| 314 | float ato( const char sptr[] ) { return strtof( sptr, 0p ); } |
---|
| 315 | double ato( const char sptr[] ) { return strtod( sptr, 0p ); } |
---|
| 316 | long double ato( const char sptr[] ) { return strtold( sptr, 0p ); } |
---|
| 317 | |
---|
| 318 | float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
| 319 | double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
| 320 | long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } |
---|
[57fc7d8] | 321 | } // distribution |
---|
[e672372] | 322 | |
---|
[bd85400] | 323 | //--------------------------------------- |
---|
| 324 | |
---|
[3ce0d440] | 325 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 326 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
| 327 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
| 328 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
| 329 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
| 330 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
| 331 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
| 332 | } // distribution |
---|
[9c47a47] | 333 | |
---|
[3ce0d440] | 334 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
| 335 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
| 336 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
| 337 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
| 338 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
| 339 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
| 340 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
| 341 | } // distribution |
---|
[bd85400] | 342 | |
---|
[b9c04946] | 343 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 344 | void qsort( E * vals, size_t dim ); |
---|
| 345 | } // distribution |
---|
| 346 | |
---|
[bd85400] | 347 | //--------------------------------------- |
---|
| 348 | |
---|
[bbe1a87] | 349 | extern "C" { // override C version |
---|
| 350 | void srandom( unsigned int seed ); |
---|
[4e7c0fc0] | 351 | long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
| 352 | // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U; |
---|
[bbe1a87] | 353 | } // extern "C" |
---|
| 354 | |
---|
| 355 | static inline { |
---|
| 356 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
| 357 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
| 358 | unsigned long int random( void ) { return lrand48(); } |
---|
| 359 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
[4e7c0fc0] | 360 | unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
[bbe1a87] | 361 | |
---|
| 362 | char random( void ) { return (unsigned long int)random(); } |
---|
| 363 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
| 364 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 365 | int random( void ) { return (long int)random(); } |
---|
| 366 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
| 367 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
| 368 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
| 369 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
| 370 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 371 | } // distribution |
---|
| 372 | |
---|
| 373 | float random( void ); // [0.0, 1.0) |
---|
| 374 | double random( void ); // [0.0, 1.0) |
---|
| 375 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 376 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 377 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
[bd85400] | 378 | |
---|
| 379 | //--------------------------------------- |
---|
| 380 | |
---|
[2026bb6] | 381 | extern bool threading_enabled(void) OPTIONAL_THREAD; |
---|
| 382 | |
---|
[bd85400] | 383 | // Local Variables: // |
---|
| 384 | // mode: c // |
---|
| 385 | // tab-width: 4 // |
---|
| 386 | // End: // |
---|