1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // stdlib -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu Jan 28 17:12:35 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Oct 30 17:30:09 2017 |
---|
13 | // Update Count : 242 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | //--------------------------------------- |
---|
19 | |
---|
20 | #ifndef EXIT_FAILURE |
---|
21 | #define EXIT_FAILURE 1 // failing exit status |
---|
22 | #define EXIT_SUCCESS 0 // successful exit status |
---|
23 | #endif // ! EXIT_FAILURE |
---|
24 | |
---|
25 | //--------------------------------------- |
---|
26 | |
---|
27 | // allocation, non-array types |
---|
28 | static inline forall( dtype T | sized(T) ) T * malloc( void ) { |
---|
29 | // printf( "* malloc\n" ); |
---|
30 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
31 | } // malloc |
---|
32 | |
---|
33 | // static inline forall( dtype T | sized(T) ) T & malloc( void ) { |
---|
34 | // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
35 | // printf( "& malloc %p\n", &p ); |
---|
36 | // return p; |
---|
37 | // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
38 | // } // malloc |
---|
39 | |
---|
40 | extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine |
---|
41 | static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) { |
---|
42 | //printf( "X2\n" ); |
---|
43 | return (T *)(void *)calloc( dim, sizeof(T) ); // C cmalloc |
---|
44 | } |
---|
45 | |
---|
46 | extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void * |
---|
47 | static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { |
---|
48 | //printf( "X3\n" ); |
---|
49 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
50 | } |
---|
51 | |
---|
52 | extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void * |
---|
53 | static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) { |
---|
54 | //printf( "X4\n" ); |
---|
55 | return (T *)memalign( align, sizeof(T) ); |
---|
56 | } // memalign |
---|
57 | |
---|
58 | static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) { |
---|
59 | //printf( "X5\n" ); |
---|
60 | return (T *)memalign( align, sizeof(T) ); |
---|
61 | } // aligned_alloc |
---|
62 | |
---|
63 | extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void * |
---|
64 | static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) { |
---|
65 | //printf( "X6\n" ); |
---|
66 | return posix_memalign( (void **)ptr, align, sizeof(T) ); |
---|
67 | } // posix_memalign |
---|
68 | |
---|
69 | |
---|
70 | extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void * |
---|
71 | |
---|
72 | static inline forall( dtype T | sized(T) ) T * alloc( void ) { |
---|
73 | //printf( "X7\n" ); |
---|
74 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
75 | } // alloc |
---|
76 | static inline forall( dtype T | sized(T) ) T * alloc( char fill ) { |
---|
77 | //printf( "X8\n" ); |
---|
78 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
79 | return memset( ptr, (int)fill, sizeof(T) ); // initial with fill value |
---|
80 | } // alloc |
---|
81 | |
---|
82 | static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) { |
---|
83 | //printf( "X9\n" ); |
---|
84 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
85 | } // alloc |
---|
86 | static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) { |
---|
87 | //printf( "X10\n" ); |
---|
88 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc |
---|
89 | return memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
90 | } // alloc |
---|
91 | |
---|
92 | static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) { |
---|
93 | //printf( "X11\n" ); |
---|
94 | return (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc |
---|
95 | } // alloc |
---|
96 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); |
---|
97 | |
---|
98 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) { |
---|
99 | //printf( "X13\n" ); |
---|
100 | return (T *)memalign( align, sizeof(T) ); |
---|
101 | } // align_alloc |
---|
102 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) { |
---|
103 | //printf( "X14\n" ); |
---|
104 | T * ptr = (T *)memalign( align, sizeof(T) ); |
---|
105 | return memset( ptr, (int)fill, sizeof(T) ); |
---|
106 | } // align_alloc |
---|
107 | |
---|
108 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) { |
---|
109 | //printf( "X15\n" ); |
---|
110 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
111 | } // align_alloc |
---|
112 | static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) { |
---|
113 | //printf( "X16\n" ); |
---|
114 | T * ptr = (T *)memalign( align, dim * sizeof(T) ); |
---|
115 | return memset( ptr, (int)fill, dim * sizeof(T) ); |
---|
116 | } // align_alloc |
---|
117 | |
---|
118 | |
---|
119 | // data, non-array types |
---|
120 | static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) { |
---|
121 | //printf( "X17\n" ); |
---|
122 | return memset( dest, c, sizeof(T) ); |
---|
123 | } // memset |
---|
124 | extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void * |
---|
125 | static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) { |
---|
126 | //printf( "X18\n" ); |
---|
127 | return memcpy( dest, src, sizeof(T) ); |
---|
128 | } // memcpy |
---|
129 | |
---|
130 | // data, array types |
---|
131 | static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) { |
---|
132 | //printf( "X19\n" ); |
---|
133 | return (void *)memset( dest, c, dim * sizeof(T) ); // C memset |
---|
134 | } // memset |
---|
135 | static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) { |
---|
136 | //printf( "X20\n" ); |
---|
137 | return (void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
138 | } // memcpy |
---|
139 | |
---|
140 | // allocation/deallocation and constructor/destructor, non-array types |
---|
141 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
142 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
143 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
144 | |
---|
145 | // allocation/deallocation and constructor/destructor, array types |
---|
146 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
147 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
148 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
149 | |
---|
150 | //--------------------------------------- |
---|
151 | |
---|
152 | int ato( const char * ptr ); |
---|
153 | unsigned int ato( const char * ptr ); |
---|
154 | long int ato( const char * ptr ); |
---|
155 | unsigned long int ato( const char * ptr ); |
---|
156 | long long int ato( const char * ptr ); |
---|
157 | unsigned long long int ato( const char * ptr ); |
---|
158 | float ato( const char * ptr ); |
---|
159 | double ato( const char * ptr ); |
---|
160 | long double ato( const char * ptr ); |
---|
161 | float _Complex ato( const char * ptr ); |
---|
162 | double _Complex ato( const char * ptr ); |
---|
163 | long double _Complex ato( const char * ptr ); |
---|
164 | |
---|
165 | int strto( const char * sptr, char ** eptr, int base ); |
---|
166 | unsigned int strto( const char * sptr, char ** eptr, int base ); |
---|
167 | long int strto( const char * sptr, char ** eptr, int base ); |
---|
168 | unsigned long int strto( const char * sptr, char ** eptr, int base ); |
---|
169 | long long int strto( const char * sptr, char ** eptr, int base ); |
---|
170 | unsigned long long int strto( const char * sptr, char ** eptr, int base ); |
---|
171 | float strto( const char * sptr, char ** eptr ); |
---|
172 | double strto( const char * sptr, char ** eptr ); |
---|
173 | long double strto( const char * sptr, char ** eptr ); |
---|
174 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
175 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
176 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
177 | |
---|
178 | //--------------------------------------- |
---|
179 | |
---|
180 | forall( otype T | { int ?<?( T, T ); } ) |
---|
181 | T * bsearch( T key, const T * arr, size_t dim ); |
---|
182 | |
---|
183 | forall( otype T | { int ?<?( T, T ); } ) |
---|
184 | unsigned int bsearch( T key, const T * arr, size_t dim ); |
---|
185 | |
---|
186 | |
---|
187 | forall( otype T | { int ?<?( T, T ); } ) |
---|
188 | void qsort( const T * arr, size_t dim ); |
---|
189 | |
---|
190 | //--------------------------------------- |
---|
191 | |
---|
192 | [ int, int ] div( int num, int denom ); |
---|
193 | [ long int, long int ] div( long int num, long int denom ); |
---|
194 | [ long long int, long long int ] div( long long int num, long long int denom ); |
---|
195 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
196 | [ T, T ] div( T num, T demon ); |
---|
197 | |
---|
198 | //--------------------------------------- |
---|
199 | |
---|
200 | unsigned char abs( signed char ); |
---|
201 | extern "C" { int abs( int ); } // use default C routine for int |
---|
202 | unsigned long int abs( long int ); |
---|
203 | unsigned long long int abs( long long int ); |
---|
204 | float abs( float ); |
---|
205 | double abs( double ); |
---|
206 | long double abs( long double ); |
---|
207 | float abs( float _Complex ); |
---|
208 | double abs( double _Complex ); |
---|
209 | long double abs( long double _Complex ); |
---|
210 | forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } ) |
---|
211 | T abs( T ); |
---|
212 | |
---|
213 | //--------------------------------------- |
---|
214 | |
---|
215 | void rand48seed( long int s ); |
---|
216 | char rand48( void ); |
---|
217 | char rand48( char l, char u ); |
---|
218 | int rand48( void ); |
---|
219 | unsigned int rand48( void ); |
---|
220 | unsigned int rand48( unsigned int u ); |
---|
221 | unsigned int rand48( unsigned int l, unsigned int u ); |
---|
222 | long int rand48( void ); |
---|
223 | unsigned long int rand48( void ); |
---|
224 | unsigned long int rand48( unsigned long int u ); |
---|
225 | unsigned long int rand48( unsigned long int l, unsigned long int u ); |
---|
226 | float rand48( void ); |
---|
227 | double rand48( void ); |
---|
228 | float _Complex rand48( void ); |
---|
229 | double _Complex rand48( void ); |
---|
230 | long double _Complex rand48( void ); |
---|
231 | |
---|
232 | //--------------------------------------- |
---|
233 | |
---|
234 | forall( otype T | { int ?<?( T, T ); } ) |
---|
235 | T min( T t1, T t2 ); |
---|
236 | |
---|
237 | forall( otype T | { int ?>?( T, T ); } ) |
---|
238 | T max( T t1, T t2 ); |
---|
239 | |
---|
240 | forall( otype T | { T min( T, T ); T max( T, T ); } ) |
---|
241 | T clamp( T value, T min_val, T max_val ); |
---|
242 | |
---|
243 | forall( otype T ) |
---|
244 | void swap( T & t1, T & t2 ); |
---|
245 | |
---|
246 | // Local Variables: // |
---|
247 | // mode: c // |
---|
248 | // tab-width: 4 // |
---|
249 | // End: // |
---|